package uu.mal.skate.model; import java.util.Random; import java.util.concurrent.TimeUnit; import uu.mal.skate.simtype.AdaptiveEpsilonGreedy; import uu.mal.skate.simtype.EpsilonDecreasing; import uu.mal.skate.simtype.EpsilonFirst; import uu.mal.skate.simtype.EpsilonGreedy; import uu.mal.skate.simtype.LinearUpdate; import uu.mal.skate.simtype.SimulationType; import uu.mal.skate.threading.PausableScheduledTask; import uu.mal.skate.view.ViewPort; public class Simulation extends PausableScheduledTask { private static final Random R = new Random(); private SimulationType simType; private final ViewPort vp; private final Options options; private final Agent[] agents; private long iteration = 1; public Simulation(Options options, ViewPort vp) { this.vp = vp; this.options = options; agents = new Agent[options.agentCount]; for (int i = 0; i < options.agentCount; i++) { Agent a = new Agent(this, i); // Give agent a random position a.setX(R.nextInt(options.torusWidth)); a.setY(R.nextInt(options.torusHeight)); // Give agent a random starting direction a.setDirection(R.nextInt(options.directionCount)); // Don't put two agents in collision range while(a.collide(a.getX(), a.getY(), agents)) { a.setX(R.nextInt(options.torusWidth)); a.setY(R.nextInt(options.torusHeight)); } agents[i] = a; } if(options.simType.equals(EpsilonFirst.class.getSimpleName())) { simType = new EpsilonFirst(this); }else if(options.simType.equals(EpsilonGreedy.class.getSimpleName())){ simType = new EpsilonGreedy(this); }else if(options.simType.equals(EpsilonDecreasing.class.getSimpleName())) { simType = new EpsilonDecreasing(this); }else if(options.simType.equals(LinearUpdate.class.getSimpleName())){ simType = new LinearUpdate(this); }else if(options.simType.equals(AdaptiveEpsilonGreedy.class.getSimpleName())){ simType = new AdaptiveEpsilonGreedy(this); } } public Agent[] getAgents() { return this.agents; } public Options getOptions() { return this.options; } public SimulationType getSimType() { return this.simType; } public long getIteration() { return iteration; } @Override public void task() { simType.nextState(iteration, agents); vp.update(iteration, simType.getRewardMatrix()); iteration++; } @Override public long getInitialDelay() { return 0; } @Override public long getPeriodDelay() { return 1000 / options.simSpeed; } @Override public TimeUnit getTimeUnit() { return TimeUnit.MILLISECONDS; } }