use of utilities.JEasyFrame 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 utilities.JEasyFrame 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 utilities.JEasyFrame in project SimpleAsteroids by ljialin.
the class Plotter method plot1Tuples.
public Plotter plot1Tuples() {
TreeSet<IntArrayPattern> orderedKeys = new TreeSet<>();
int tupleSize = 1;
// iterate over all the tuples, picking ones of the correct size
for (NTuple nTuple : nTupleSystem.tuples) {
if (nTuple.tuple.length == tupleSize) {
ArrayList<StatSummary> ssa = new ArrayList<>();
orderedKeys.addAll(nTuple.ntMap.keySet());
// iterate in key order to provide a sensible looking plot
double[] xTicks = new double[orderedKeys.size()];
int ix = 0;
StatSummary stats = new StatSummary();
for (IntArrayPattern key : orderedKeys) {
StatSummary ss = nTuple.ntMap.get(key);
stats.add(ss);
xTicks[ix++] = key.v[0];
ssa.add(ss);
System.out.format("%s\t %d\t %.2f\t %.2f\n", key, ss.n(), ss.mean(), ss.stdErr());
}
// System.out.println(ssa);
System.out.println();
// create a LineGroup
LineGroup lineGroup = new LineGroup().setColor(Color.black);
lineGroup.stats = ssa;
LineChart lineChart = new LineChart().addLineGroup(lineGroup);
lineChart.setYLabel("Average Fitness");
lineChart.setXLabel("Parameter index");
lineChart.xAxis = new LineChartAxis(xTicks);
lineChart.bg = Color.gray;
lineChart.plotBG = Color.white;
int lower = (int) stats.min();
int upper = 1 + (int) stats.max();
double[] yTicks = new double[] { lower, (upper + lower) / 2, upper };
lineChart.yAxis = new LineChartAxis(yTicks);
lineChart.title = Arrays.toString(nTuple.tuple);
new JEasyFrame(lineChart, "Line Chart");
}
}
return this;
}
use of utilities.JEasyFrame in project SimpleAsteroids by ljialin.
the class UrnSystem method main.
public static void main(String[] args) {
ElapsedTimer t = new ElapsedTimer();
UrnSystem us = new UrnSystem();
System.out.println(t);
double nWins = 15;
double nPicks = 15;
// nWins *=2;
// nPicks *= 2;
BarChart bc = new BarChart();
bc.update(us.pVec(nWins, nPicks));
new JEasyFrame(bc, nWins + " / " + nPicks);
System.out.println(Arrays.toString(us.pVec(nWins, nPicks)));
}
use of utilities.JEasyFrame in project SimpleAsteroids by ljialin.
the class EvolveMarioLevelTest method update.
@Override
public void update(EvolutionLogger logger, int[] solution, double fitness) {
if (logger.nEvals() % freq != 0)
return;
if (levelFrame == null) {
// set them both up
levelView = new LevelView(LevelView.toRect(solution, imageWidth, imageHeight));
levelView.setColorMap(MarioReader.tileColors);
levelFrame = new JEasyFrame(levelView, "Title");
}
levelView.setTiles(solution);
levelFrame.setTitle(String.format("Evals: %d;\t fitness: %.3f", logger.nEvals(), fitness));
}
Aggregations