use of ga.SimpleRMHC 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 ga.SimpleRMHC 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 ga.SimpleRMHC in project SimpleAsteroids by ljialin.
the class EvolvePatternTest method main.
public static void main(String[] args) {
int nTrials = 1;
SimpleRMHC evoAlg = new SimpleRMHC();
evoAlg.setMutator(new ConvMutator());
// evoAlg = new SlidingMeanEDA().setHistoryLength(30);
// evoAlg = new CompactSlidingGA();
int nEvals = 50000;
StatSummary results = new StatSummary();
EvolvePatternTest ept = new EvolvePatternTest();
for (int i = 0; i < nTrials; i++) {
ElapsedTimer timer = new ElapsedTimer();
results.add(ept.runTrial(evoAlg, nEvals));
System.out.println(timer);
}
}
use of ga.SimpleRMHC in project SimpleAsteroids by ljialin.
the class TestEA method sweepSamplingRate.
public static void sweepSamplingRate(int from, int to) {
EvoAlg ea = new MBanditEA();
// SolutionEvaluator evaluator = new EvalMaxM(nDims, mValues, 1.0);
// SolutionEvaluator noiseFree = new EvalMaxM(nDims, mValues, 0.0);
SolutionEvaluator evaluator = new EvalNoisyWinRate(nDims, mValues, 1.0);
SolutionEvaluator noiseFree = new EvalNoisyWinRate(nDims, mValues, 0.0);
for (int i = from; i <= to; i++) {
ea = new SimpleRMHC(i);
// ea = new MBanditEA();
ea = new NTupleBanditEA();
StatSummary ss = runTrials(ea, evaluator, noiseFree, nTrials, nFitnessEvals);
// System.out.format("%d\t %.2f \t %.3f \t %.3f \t %.2f\n", i, ss.mean(), ss.stdErr(), ssOpt.mean(), nOpt.mean());
System.out.format("%d\t %.3f \t %.3f \t %.3f \t %.2f\t %.3f\n", i, ss.mean(), nOpt.mean(), ssOpt.mean(), ntOpt.mean(), ntPerf.mean());
// System.out.format("%d\t %.2f \t %.2f \t %.2f \n", i, ss.mean(), ss.stdErr(), ssOpt.mean());
}
}
use of ga.SimpleRMHC in project SimpleAsteroids by ljialin.
the class TestEAGraphRunTrials method main.
public static void main(String[] args) throws Exception {
// create and run a test
// showing flexibility to create multiple graphs
int nDims = 100, mValues = 2;
double noise = 1.0;
int nEvals = 1000;
int nTrials = 10;
NoisySolutionEvaluator solutionEvaluator = new EvalNoisyWinRate(nDims, mValues, noise);
solutionEvaluator = new EvalMaxM(nDims, mValues, noise);
// solutionEvaluator = new Eval2DNonLinear(8, noise);
TestEAGraph tester = new TestEAGraph(solutionEvaluator, nEvals).setColor(Color.red);
// Set up all the algorithms to test
SimpleRMHC rmhc1 = new SimpleRMHC(1);
SimpleRMHC rmhc5 = new SimpleRMHC(5);
SimpleGA sga = new SimpleGA().setPopulationSize(20).setCrossoverRate(0.5);
int windowLength = 40;
CompactSlidingGA slidingGA = new CompactSlidingGA().setHistoryLength(windowLength);
slidingGA.useBayesUpdates = false;
// nDims * windowLength / 2;
slidingGA.K = 1000;
int nParents = 2;
CompactBinaryGA cga = new CompactBinaryGA().setParents(nParents);
// cga.K = nDims / 2; // * nParents; // setting from Jialin
// nDims * nParents;
cga.K = 1000;
// cga.nToFlip = 2;
// add them to the test list
ArrayList<EvoAlg> evos = new ArrayList<>();
// evos.add(new SlidingMeanEDA().setHistoryLength(30));
evos.add(sga);
evos.add(rmhc1);
evos.add(rmhc5);
evos.add(slidingGA);
evos.add(cga);
nParents = 20;
CompactBinaryGA cga2 = new CompactBinaryGA().setParents(nParents);
cga2.K = nDims * nParents * 2;
// evos.add(cga2);
// evos.add(rmhc1);
// evos.add(rmhc5);
// evos.add(new SlidingMeanEDA().setHistoryLength(30));
Color[] colors = { Color.red, Color.green, Color.blue, Color.yellow, Color.cyan, Color.pink, Color.magenta };
LineChart lineChart = new LineChart().setTitle(String.format("Noisy OneMax, %d dimensions", nDims));
lineChart.setXLabel("Fitness Evaluations").setYLabel("Noise-Free Fitness");
lineChart.xAxis = new LineChartAxis(new double[] { 0, 200, 400, 600, 800, 1000 });
lineChart.yAxis = new LineChartAxis(new double[] { 40, 50, 60, 70, 80, 90, 100 });
for (int i = 0; i < evos.size(); i++) {
ElapsedTimer elapsedTimer = new ElapsedTimer();
tester.setColor(colors[i]);
TestEvoResults results = tester.runTrials(evos.get(i), nTrials);
System.out.println("Tested: " + evos.get(i).getClass().getName());
System.out.println(results.trueOpt);
System.out.println(elapsedTimer);
System.out.println();
lineChart.addLines(results.linePlots);
lineChart.addLineGroup(results.getLineGroup().setColor(colors[i]).setName(results.name));
}
LineGroup pVec = new LineGroup().setName("P-Vec").setColor(Color.white);
for (ArrayList<Double> extra : extras) {
// lineChart.addLine(new LinePlot().setColor(Color.white).setData(extra));
pVec.add(extra);
}
// lineChart.addLineGroup(pVec);
new JEasyFrame(lineChart, "Fitness Evolution");
String dir = "results/javares/sweda/";
// String filename = "resultsOneMax.png";
String filename = "resultsOneMaxPVec.png";
lineChart.saveImage(dir, filename);
}
Aggregations