use of ga.SimpleRMHC in project SimpleAsteroids by ljialin.
the class AsteroidsTest method runOnce.
public static double runOnce() throws Exception {
// make an agent to test
// AsteroidsLinkState.defaultStartLevel = 1;
AsteroidsLinkState stateObs = new AsteroidsLinkState();
System.out.println(stateObs.getGameScore());
System.out.println(stateObs.copy().getGameScore());
ElapsedCpuTimer timer = new ElapsedCpuTimer();
AbstractPlayer player;
controllers.singlePlayer.sampleOLMCTS.Agent olmcts = new controllers.singlePlayer.sampleOLMCTS.Agent(stateObs, timer);
controllers.singlePlayer.discountOLMCTS.Agent discountOlmcts = new controllers.singlePlayer.discountOLMCTS.Agent(stateObs, timer);
controllers.singlePlayer.nestedMC.Agent nestedMC = new controllers.singlePlayer.nestedMC.Agent(stateObs, timer);
int depth = 100;
int ticks = 2000;
controllers.singlePlayer.discountOLMCTS.SingleTreeNode.DEFAULT_ROLLOUT_DEPTH = depth;
SingleTreeNode.scoreDiscountFactor = 0.999;
SingleTreeNode.useScoreDiscount = true;
controllers.singlePlayer.discountOLMCTS.SingleTreeNode.DEFAULT_ROLLOUT_DEPTH = depth;
controllers.singlePlayer.discountOLMCTS.Agent.MCTS_ITERATIONS = ticks / depth;
// player = olmcts;
player = discountOlmcts;
int nResamples = 1;
EvoAlg evoAlg = new SimpleRMHC(nResamples);
int nEvals = 20;
double kExplore = 10;
int nNeighbours = 100;
// evoAlg = new NTupleBanditEA(kExplore, nNeighbours);
// evoAlg = new SlidingMeanEDA();
DefaultMutator.totalRandomChaosMutation = true;
Agent.useShiftBuffer = true;
controllers.singlePlayer.ea.Agent.SEQUENCE_LENGTH = ticks / nEvals;
player = new controllers.singlePlayer.ea.Agent(stateObs, timer, evoAlg, nEvals);
// player = new controllers.singlePlayer.ea.Agent(stateObs, timer);
// nestedMC.maxRolloutLength = 5;
// nestedMC.nestDepth = 5;
// player = nestedMC;
// in milliseconds
int thinkingTime = 10;
int delay = 20;
// player = new controllers.singlePlayer.sampleRandom.Agent(stateObs, timer);
// check that we can play the game
Random random = new Random();
// this is how many steps we'll take in the actual game ...
int nSteps = 1000;
ElapsedTimer t = new ElapsedTimer();
View view = new View(stateObs.state);
// set view to null to run fast with no visuals
view = null;
JEasyFrame frame;
if (view != null) {
frame = new JEasyFrame(view, "Asteroids");
}
for (int i = 0; i < nSteps && !stateObs.isGameOver(); i++) {
timer = new ElapsedCpuTimer();
timer.setMaxTimeMillis(thinkingTime);
Types.ACTIONS action = player.act(stateObs.copy(), timer);
// System.out.println("Selected: " + action); // + "\t " + action.ordinal());
stateObs.advance(action);
// System.out.println(stateObs.getGameScore());
if (view != null) {
view.repaint();
Thread.sleep(delay);
}
}
System.out.println(stateObs.getGameScore());
System.out.println(stateObs.isGameOver());
System.out.println(t);
System.out.println("Agent of type: " + player.getClass().getSimpleName());
return stateObs.getGameScore();
}
use of ga.SimpleRMHC in project SimpleAsteroids by ljialin.
the class Game method getEvoAgent.
Controller getEvoAgent() {
//
// todo Add in the code t make this
int nResamples = 1;
DefaultMutator mutator = new DefaultMutator(null);
// setting to true may give best performance
mutator.totalRandomChaosMutation = true;
SimpleRMHC simpleRMHC = new SimpleRMHC();
simpleRMHC.setSamplingRate(nResamples);
simpleRMHC.setMutator(mutator);
EvoAlg evoAlg = simpleRMHC;
// evoAlg = new SlidingMeanEDA();
int nEvals = 20;
int seqLength = 100;
EvoAgent evoAgent = new EvoAgent().setEvoAlg(evoAlg, nEvals).setSequenceLength(seqLength);
evoAgent.setUseShiftBuffer(true);
evoAgent.setVisual();
return new EvoAgentAdapter().setAgent(evoAgent);
}
use of ga.SimpleRMHC in project SimpleAsteroids by ljialin.
the class GameRunner method getEvoAgent.
public static EvoAgent getEvoAgent() {
int nResamples = 1;
EvoAlg evoAlg = new SimpleRMHC(nResamples);
// evoAlg = new SlidingMeanEDA();
evoAlg = new NTupleBanditEA();
int nEvals = 20;
int seqLength = 100;
EvoAgent evoAgent = new EvoAgent().setEvoAlg(evoAlg, nEvals).setSequenceLength(seqLength);
evoAgent.setUseShiftBuffer(true);
return evoAgent;
}
use of ga.SimpleRMHC in project SimpleAsteroids by ljialin.
the class PlanetWarsLinkTest method runTest.
public static double runTest() throws Exception {
PlanetWarsLinkState state = new PlanetWarsLinkState();
// state.state.
GameState.includeBuffersInScore = true;
PlanetWarView view = null;
view = new PlanetWarView((GameState) state.state);
// JEasyFrame frame = new JEasyFrame(view, "Simple Planet Wars");
// KeyController controller = new KeyController();
// frame.addKeyListener(controller);
// now play
Random random = new Random();
AbstractMultiPlayer player1, player2;
GameActionSpaceAdapterMulti.visual = false;
// DefaultMutator.totalRandomChaosMutation = true;
// controllers.singlePlayer.sampleOLMCTS.Agent olmcts =
// new controllers.singlePlayer.sampleOLMCTS.Agent(linkState, timer);
int idPlayer1 = 0;
int idPlayer2 = 1;
ElapsedCpuTimer timer = new ElapsedCpuTimer();
player2 = new controllers.multiPlayer.discountOLMCTS.Agent(state.copy(), timer, idPlayer2);
// player2 = new controllers.multiPlayer.sampleOLMCTS.Agent(state.copy(), timer, idPlayer2);
// try the evolutionary players
int nResamples = 1;
EvoAlg evoAlg = new SimpleRMHC(nResamples);
int nEvals = 133;
// evoAlg = new SlidingMeanEDA().setHistoryLength(20);
Agent evoAgent = new controllers.multiPlayer.ea.Agent(state.copy(), timer, evoAlg, idPlayer1, nEvals);
evoAgent.sequenceLength = 15;
evoAgent.setUseShiftBuffer(true);
player1 = evoAgent;
// player2 = new controllers.multiPlayer.ea.Agent(linkState, timer, evoAlg2, idPlayer2, nEvals);
// player2 = new controllers.multiPlayer.ea.Agent(linkState, timer, new SimpleRMHC(nResamples), idPlayer2, nEvals);
// player1 = new controllers.multiPlayer.smlrand.Agent();
// player2 = new controllers.multiPlayer.smlrand.Agent();
// player2 = new controllers.multiPlayer.doNothing.Agent(state, timer, 1);
// EvoAlg evoAlg2 = new SimpleRMHC(2);
// player1 = new controllers.multiPlayer.ea.Agent(linkState, timer, evoAlg2, idPlayer1, nEvals);
// player1 =
// in milliseconds
int thinkingTime = 50;
int delay = 200;
// player = new controllers.singlePlayer.sampleRandom.Agent(stateObs, timer);
// check that we can play the game
int nSteps = 200;
view = null;
for (int i = 0; i < nSteps; i++) {
timer = new ElapsedCpuTimer();
timer.setMaxTimeMillis(thinkingTime);
Types.ACTIONS action1 = player1.act(state.copy(), timer);
timer = new ElapsedCpuTimer();
timer.setMaxTimeMillis(thinkingTime);
Types.ACTIONS action2 = player2.act(state.copy(), timer);
// §action2 =
state.advance(new Types.ACTIONS[] { action1, action2 });
if (view != null) {
view.update((GameState) state.state);
Thread.sleep(delay);
}
// System.out.println("Game tick: " + i);
}
System.out.println("Game Score: " + state.getGameScore());
// System.out.println("MCTS Evals: " + TreeNode);
return state.getGameScore() > 0 ? 1 : 0;
}
use of ga.SimpleRMHC in project SimpleAsteroids by ljialin.
the class SimpleGridTest method runOnce.
public static double runOnce() {
// make an agent to test
StateObservation gridGame = new GridModel();
System.out.println(gridGame.getGameScore());
System.out.println(gridGame.copy().getGameScore());
// System.exit(0);
ElapsedCpuTimer timer = new ElapsedCpuTimer();
AbstractPlayer player;
controllers.singlePlayer.sampleOLMCTS.Agent olmcts = new controllers.singlePlayer.sampleOLMCTS.Agent(gridGame, timer);
controllers.singlePlayer.discountOLMCTS.Agent discountOlmcts = new controllers.singlePlayer.discountOLMCTS.Agent(gridGame, timer);
controllers.singlePlayer.nestedMC.Agent nestedMC = new controllers.singlePlayer.nestedMC.Agent(gridGame, timer);
player = olmcts;
// player = discountOlmcts;
// for the following we can pass the Evolutionary algorithm to use
int nResamples = 2;
EvoAlg evoAlg = new SimpleRMHC(nResamples);
int nEvals = 2000;
double kExplore = 10;
int nNeighbours = 100;
evoAlg = new NTupleBanditEA(kExplore, nNeighbours);
evoAlg = new SlidingMeanEDA();
// DefaultMutator.totalRandomChaosMutation = false;
Agent.useShiftBuffer = true;
Agent.SEQUENCE_LENGTH = 30;
player = new Agent(gridGame, timer, evoAlg, nEvals);
nestedMC.maxRolloutLength = 30;
nestedMC.nestDepth = 3;
// player = nestedMC;
// in milliseconds
int thinkingTime = 50;
int delay = 30;
// player = new controllers.singlePlayer.sampleRandom.Agent(stateObs, timer);
// check that we can play the game
Random random = new Random();
// this is how many steps we'll take in the actual game ...
int nSteps = 30;
ElapsedTimer t = new ElapsedTimer();
for (int i = 0; i < nSteps && !gridGame.isGameOver(); i++) {
timer = new ElapsedCpuTimer();
timer.setMaxTimeMillis(thinkingTime);
Types.ACTIONS action = player.act(gridGame.copy(), timer);
System.out.println();
// + "\t " + action.ordinal());
System.out.println("Selected: " + action);
gridGame.advance(action);
System.out.println("Game state: " + gridGame);
System.out.println();
}
System.out.println(gridGame.getGameScore());
return gridGame.getGameScore();
}
Aggregations