use of ntuple.NTupleBanditEA in project SimpleAsteroids by ljialin.
the class SimpleMaxNTest method runOnce.
public static double runOnce() {
// make an agent to test
StateObservation noiseFree = new SimpleMaxGame();
// new NoisyMaxGame();
StateObservation stateObs = new SimpleMaxGame();
System.out.println(stateObs.getGameScore());
System.out.println(stateObs.copy().getGameScore());
// System.exit(0);
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);
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 = 1000;
double kExplore = 10;
int nNeighbours = 100;
evoAlg = new NTupleBanditEA(kExplore, nNeighbours);
// DefaultMutator.totalRandomChaosMutation = true;
Agent.useShiftBuffer = false;
controllers.singlePlayer.ea.Agent.SEQUENCE_LENGTH = 100;
player = new controllers.singlePlayer.ea.Agent(stateObs, timer, evoAlg, nEvals);
nestedMC.maxRolloutLength = 5;
nestedMC.nestDepth = 5;
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 = 10;
ElapsedTimer t = new ElapsedTimer();
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);
noiseFree.advance(action);
// System.out.println(stateObs.getGameScore());
}
System.out.println(stateObs.getGameScore());
System.out.println(noiseFree.getGameScore());
System.out.println(stateObs.isGameOver());
System.out.println(t);
return noiseFree.getGameScore();
}
use of ntuple.NTupleBanditEA in project SimpleAsteroids by ljialin.
the class SpaceBattleLinkTest method runTrial.
public static double runTrial(boolean runVisible) {
// make an agent to test
StateObservation stateObs = new SimpleMaxGame();
// BattleGameSearchSpace.inject(BattleGameSearchSpace.getRandomPoint());
// SampleEvolvedParams.solutions[1][2] = 5;
// BattleGameSearchSpace.inject(SampleEvolvedParams.solutions[1]);
// BattleGameSearchSpace.inject(SampleEvolvedParams.solutions[2]);
BattleGameSearchSpace.inject(SampleEvolvedParams.solutions[1]);
System.out.println("Params are:");
System.out.println(BattleGameParameters.params);
// can also overide parameters by setting them directly as follows:
// BattleGameParameters.loss = 1.1;
SpaceBattleLinkState linkState = new SpaceBattleLinkState();
// set some parameters for the experiment
GameActionSpaceAdapter.useHeuristic = false;
Agent.useShiftBuffer = true;
// DefaultMutator.totalRandomChaosMutation = false;
// // supercl
// StateObservation stateObs = linkState;
ElapsedCpuTimer timer = new ElapsedCpuTimer();
AbstractPlayer player;
// controllers.singlePlayer.sampleOLMCTS.Agent olmcts =
// new controllers.singlePlayer.sampleOLMCTS.Agent(linkState, timer);
player = new controllers.singlePlayer.discountOLMCTS.Agent(linkState, timer);
// try the evolutionary players
int nResamples = 2;
EvoAlg evoAlg = new SimpleRMHC(nResamples);
double kExplore = 10;
int nNeighbours = 100;
int nEvals = 200;
evoAlg = new NTupleBanditEA(kExplore, nNeighbours);
// player = new controllers.singlePlayer.ea.Agent(linkState, timer, evoAlg, nEvals);
controllers.singlePlayer.nestedMC.Agent nestedMC = new controllers.singlePlayer.nestedMC.Agent(linkState, timer);
nestedMC.maxRolloutLength = 10;
nestedMC.nestDepth = 2;
player = nestedMC;
// in milliseconds
int thinkingTime = 50;
int delay = 10;
// player = new controllers.singlePlayer.sampleRandom.Agent(stateObs, timer);
// check that we can play the game
Random random = new Random();
int nSteps = 500;
ElapsedTimer t = new ElapsedTimer();
BattleView view = new BattleView(linkState.state);
// set view to null to run fast with no visuals
if (!runVisible)
view = null;
if (view != null) {
new JEasyFrame(view, "Simple Battle Game");
}
boolean verbose = false;
for (int i = 0; i < nSteps && !linkState.isGameOver(); i++) {
ArrayList<Types.ACTIONS> actions = linkState.getAvailableActions();
timer = new ElapsedCpuTimer();
timer.setMaxTimeMillis(thinkingTime);
Types.ACTIONS action = player.act(linkState.copy(), timer);
// action = actions.get(random.nextInt(actions.size()));
if (verbose)
// + "\t " + action.ordinal());
System.out.println(i + "\t Selected: " + action);
linkState.advance(action);
if (view != null) {
view.repaint();
try {
Thread.sleep(delay);
} catch (Exception e) {
}
}
if (verbose)
System.out.println(linkState.getGameScore());
}
System.out.println("Game score: " + linkState.getGameScore());
return linkState.getGameScore();
}
use of ntuple.NTupleBanditEA in project SimpleAsteroids by ljialin.
the class HyperParamTuneRunner method runTrials.
public void runTrials(EvoAlg evoAlg, AnnotatedFitnessSpace annotatedFitnessSpace) {
ElapsedTimer timer = new ElapsedTimer();
StatSummary ss = new StatSummary("Overall results: " + evoAlg.getClass().getSimpleName());
for (int i = 0; i < nTrials; i++) {
System.out.println();
System.out.println("Running trial: " + (i + 1));
try {
ss.add(runTrial(evoAlg, annotatedFitnessSpace));
System.out.println("Stats so far");
System.out.println(ss);
if (verbose) {
plotFitnessEvolution(annotatedFitnessSpace.logger(), annotatedFitnessSpace, plotChecks);
// annotatedFitnessSpace.logger()
// ((NTupleSystem) ((NTupleBanditEA) evoAlg).banditLandscapeModel).printDetailedReport(new EvoAgentSearchSpaceAsteroids().getParams());
NTupleSystem nTupleSystem = ((NTupleSystem) ((NTupleBanditEA) evoAlg).banditLandscapeModel);
nTupleSystem.printDetailedReport(annotatedFitnessSpace.getParams());
// new Plotter().setModel(nTupleSystem).defaultPlot().plot1Tuples();
}
} catch (Exception e) {
e.printStackTrace();
}
}
if (verbose) {
lineChart.addLineGroup(sampleEvolution);
if (plotChecks > 0)
lineChart.addLineGroup(bestGuess);
new JEasyFrame(lineChart, "Sample Evolution");
}
System.out.println("nEvals per run: " + nEvals);
System.out.println(ss);
System.out.println("Total time for experiment: " + timer);
}
use of ntuple.NTupleBanditEA in project SimpleAsteroids by ljialin.
the class TestHyperParamAsteroids method main.
public static void main(String[] args) {
AnnotatedFitnessSpace testAsteroids = new EvoAgentSearchSpaceAsteroids();
EvoAlg[] evoAlgs = { new NTupleBanditEA().setKExplore(10000), // new CompactSlidingGA(),
new SlidingMeanEDA() };
int nChecks = 50;
int nEvals = 50;
int nTrials = 2;
for (EvoAlg evoAlg : evoAlgs) {
HyperParamTuneRunner runner = new HyperParamTuneRunner();
runner.nChecks = nChecks;
runner.nTrials = nTrials;
runner.nEvals = nEvals;
runner.runTrials(evoAlg, testAsteroids);
}
}
use of ntuple.NTupleBanditEA in project SimpleAsteroids by ljialin.
the class TestHyperParamPlanetWars method main.
public static void main(String[] args) {
int nEvals = 288;
if (args.length == 1) {
nEvals = Integer.parseInt(args[0]);
}
System.out.println("Optimization budget: " + nEvals);
NTupleBanditEA ntbea = new NTupleBanditEA().setKExplore(1);
GameState.includeBuffersInScore = true;
EvoAgentSearchSpace.tickBudget = 2000;
EvoAlg[] evoAlgs = { // new SimpleRMHC(5),
ntbea };
int nChecks = 100;
int nTrials = 100;
ElapsedTimer timer = new ElapsedTimer();
for (EvoAlg evoAlg : evoAlgs) {
// LineChart lineChart = new LineChart();
// lineChart.yAxis = new LineChartAxis(new double[]{-2, -1, 0, 1, 2});
// lineChart.setYLabel("Fitness");
HyperParamTuneRunner runner = new HyperParamTuneRunner();
// runner.verbose = true;
// runner.setLineChart(lineChart);
runner.nChecks = nChecks;
runner.nTrials = nTrials;
runner.nEvals = nEvals;
runner.plotChecks = 0;
AnnotatedFitnessSpace testPlanetWars = new EvoAgentSearchSpace();
System.out.println("Testing: " + evoAlg);
runner.runTrials(evoAlg, testPlanetWars);
System.out.println("Finished testing: " + evoAlg);
// note, this is a bit of a hack: it only reports the final solution
// System.out.println(new EvoAgentSearchSpace().report(runner.solution));
}
// System.out.println(ntbea.getModel().s);
System.out.println("Time for all experiments: " + timer);
}
Aggregations