use of ga.SimpleRMHC in project SimpleAsteroids by ljialin.
the class EvoSequenceTest method main.
// simple example of NOT doing rolling horizon, but instead
// of evolving a complete sequence
public static void main(String[] args) throws Exception {
GameState game = new GameState().defaultState();
int nResamples = 1;
EvoAlg evoAlg = new SimpleRMHC(nResamples);
int nEvals = 2000;
int seqLength = 30;
// evoAlg = new SlidingMeanEDA().setHistoryLength(20);
System.out.println("Initial score: " + game.getScore());
EvoAgent evoAgent = new EvoAgent().setEvoAlg(evoAlg, nEvals).setSequenceLength(seqLength);
int[] seq = evoAgent.getActions(game.copy(), 0);
// System.out.println(evoAgent.simpleGameAdapter.logger().fa);
// new JEasyFrame( LineChart.easyPlot(evoAgent.simpleGameAdapter.logger().fa), "Evo Plot");
// visiPlay(game, seq);
// check the sequence fitness
System.out.println(evoAgent.simpleGameAdapter.logger().ss);
System.out.println("Returned sequence: " + Arrays.toString(seq));
for (int i = 0; i < 5; i++) {
System.out.println(evoAgent.simpleGameAdapter.evaluate(seq));
}
System.out.println();
System.out.println("Now using the other one");
for (int i = 0; i < 5; i++) {
PlanetWarsLinkState linkState = new PlanetWarsLinkState(game.copy());
GameActionSpaceAdapterMulti adapter = new GameActionSpaceAdapterMulti(linkState, seqLength, 0, 1);
System.out.println(adapter.evaluate(seq));
}
System.out.println();
for (int i = 0; i < 5; i++) {
System.out.println(evoAgent.simpleGameAdapter.evaluate(seq));
}
System.out.println();
}
use of ga.SimpleRMHC in project SimpleAsteroids by ljialin.
the class GameLoggerTest method main.
public static void main(String[] args) {
System.out.println("n Actions: " + GameState.nActions);
GameState.includeBuffersInScore = false;
GameRunner gameRunner = new GameRunner().setLength(50);
SimplePlayerInterface p1, p2;
p1 = new RandomAgent();
p2 = new DoNothingAgent();
EvoAlg evoAlg1 = new SimpleRMHC();
int nEvals1 = 500;
int seqLength1 = 15;
EvoAgent evoAgent1 = new EvoAgent().setEvoAlg(evoAlg1, nEvals1).setSequenceLength(seqLength1);
evoAgent1.setUseShiftBuffer(false);
EvoAgent evoAgentShadow1 = new EvoAgent().setEvoAlg(new SimpleRMHC(), nEvals1).setSequenceLength(seqLength1);
evoAgentShadow1.setUseShiftBuffer(false);
EvoAgent evoAgentShadow2 = new EvoAgent().setEvoAlg(new SimpleRMHC(), nEvals1).setSequenceLength(3);
evoAgentShadow2.setUseShiftBuffer(false);
AbstractGameLogger abstractGameLogger = new AbstractGameLogger().setAgent(evoAgent1);
abstractGameLogger.addShadow(evoAgentShadow1);
abstractGameLogger.addShadow(evoAgentShadow2);
abstractGameLogger.addShadow(new RandomAgent());
abstractGameLogger.addShadow(new RandomAgent());
abstractGameLogger.addShadow(new DoNothingAgent());
abstractGameLogger.addShadow(new DoNothingAgent());
// p1 = new AbstractGameLogger().setAgent(new DoNothingAgent());
// p1 = new AbstractGameLogger().setAgent(evoAgent1);
p1 = abstractGameLogger;
p2 = new AbstractGameLogger().setAgent(new RandomAgent());
SimplePlayerInterface opponentModel;
opponentModel = new RandomAgent();
// p2 = getMCTSAgent(new GameState().defaultState(), 1);
p2 = evoAgent1;
gameRunner.setPlayers(p1, p2);
int nGames = 20;
gameRunner.playGames(nGames);
gameRunner.plotGameScores();
// System.out.println(p1);
// System.out.println(p2);
}
use of ga.SimpleRMHC in project SimpleAsteroids by ljialin.
the class GameRunnerTest method main.
public static void main(String[] args) {
GameState.includeBuffersInScore = false;
GameRunner gameRunner = new GameRunner().setLength(200);
SimplePlayerInterface p1, p2;
p1 = new RandomAgent();
// p2 = new DoNothingAgent();
EvoAlg evoAlg1 = new SimpleRMHC();
// evoAlg1.mu
int nEvals1 = 200;
int seqLength1 = 10;
int nEvals2 = 400;
int seqLength2 = 5;
SlidingMeanEDA evoAlg2 = new SlidingMeanEDA().setHistoryLength(40);
SimpleGA simpleGA = new SimpleGA().setPopulationSize(20);
EvoAgent evoAgent1 = new EvoAgent().setEvoAlg(evoAlg1, nEvals1).setSequenceLength(seqLength1);
evoAgent1.setUseShiftBuffer(true);
// evoAgent1.mu
// evoAgent1.u
// evoAgent1.setOpponent(new RandomAgent()).setUseShiftBuffer(false);
// evoAgent1.setOpponent(new RandomAgent());
p1 = evoAgent1;
SimplePlayerInterface opponentModel;
opponentModel = new DoNothingAgent();
opponentModel = new RandomAgent();
// p2 = new EvoAgent().setEvoAlg(simpleGA, nEvals).setSequenceLength(seqLength).setOpponent(opponentModel);
// p2 = new EvoAgent().setEvoAlg(evoAlg1, nEvals/2).setSequenceLength(seqLength*2).setOpponent(opponentModel);
EvoAgent evoAgent2 = new EvoAgent().setEvoAlg(evoAlg1, nEvals2).setSequenceLength(seqLength2).setOpponent(opponentModel);
evoAgent2.setUseShiftBuffer(true);
p2 = evoAgent2;
p2 = getMCTSAgent(new GameState().defaultState(), 1);
// p2 = new RandomAgent();
gameRunner.setPlayers(p1, p2);
// now play a number of games and observe the outcomes
// verbose is set to true by default so after the games have been played
// it will report the outcomes
int nGames = 20;
gameRunner.playGames(nGames);
// now access the game logs to plot the scores
gameRunner.plotGameScores();
// System.out.println(evoAlg2.pVec);
}
use of ga.SimpleRMHC in project SimpleAsteroids by ljialin.
the class RandomTestPlanetWars method evaluate.
public static double evaluate(SimplePlayerInterface player) {
GameRunner gameRunner = new GameRunner();
EvoAlg evoAlgOpponent = new SimpleRMHC();
// set up some defaults for opponent
int nOpponentEvals = 400;
int opponentSeqLength = 5;
EvoAgent evoOpponent = new EvoAgent().setEvoAlg(evoAlgOpponent, nOpponentEvals).setSequenceLength(opponentSeqLength);
// setting to false provides a much weaker opponent
evoOpponent.setUseShiftBuffer(true);
// now run a game and return the result
gameRunner.verbose = false;
gameRunner.reset();
gameRunner.setPlayers(player, evoOpponent);
gameRunner.playGame();
double fitness = gameRunner.scores.mean();
double value = 0;
if (fitness > 0)
value = 1;
if (fitness < 0)
value = -1;
System.out.println("Fitness: " + (int) fitness + " : " + value);
System.out.println();
return value;
}
use of ga.SimpleRMHC in project SimpleAsteroids by ljialin.
the class TwoPlayerTest method main.
public static void main(String[] args) throws Exception {
// AsteroidsGameState game = new AsteroidsGameState().setNPlanets(10).setRandomOwnerships().setRandomGrowthRates();
// .setNPlanets(10).setDOwnerships().setRandomGrowthRates();
GameState game = new GameState().defaultState();
GameState.includeBuffersInScore = true;
GameState.wrapAround = false;
PlanetWarView view = new PlanetWarView(game);
JEasyFrame frame = new JEasyFrame(view, "Test View");
KeyController controller = new KeyController();
frame.addKeyListener(controller);
int nResamples = 1;
EvoAlg evoAlg = new SimpleRMHC(nResamples);
int nEvals = 100;
int seqLength = 20;
// evoAlg = new SlidingMeanEDA().setHistoryLength(20);
System.out.println("Initial score: " + game.getScore());
EvoAgent evoAgent = new EvoAgent().setEvoAlg(evoAlg, nEvals).setSequenceLength(seqLength);
evoAgent.setUseShiftBuffer(true);
evoAgent.setVisual();
int delay = 2000;
GameActionSpaceAdapterMulti.visual = true;
Random random = new Random();
for (int i = 0; i < 500; i++) {
int p1, p2;
p1 = evoAgent.getAction(game, 0);
// p1 = random.nextInt(AsteroidsGameState.nActions);
p2 = controller.action(game);
// p2 = AsteroidsGameState.doNothing;
int[] a = new int[] { p1, p2 };
game.next(a);
// game.update();
view.update(game);
Thread.sleep(delay);
}
}
Aggregations