[44] | 1 | package cecj.app.othello; |
---|
| 2 | |
---|
| 3 | import ec.EvolutionState; |
---|
| 4 | import ec.Individual; |
---|
| 5 | import ec.Population; |
---|
| 6 | import ec.Statistics; |
---|
| 7 | import ec.Subpopulation; |
---|
| 8 | import ec.simple.SimpleEvolutionState; |
---|
| 9 | import ec.simple.SimpleFitness; |
---|
| 10 | import ec.util.MersenneTwisterFast; |
---|
| 11 | import ec.util.Output; |
---|
| 12 | import ec.util.Parameter; |
---|
| 13 | import ec.util.ParameterDatabase; |
---|
| 14 | import ec.vector.DoubleVectorIndividual; |
---|
| 15 | import games.BoardGame; |
---|
| 16 | import games.scenarios.SelfPlayTDLScenario; |
---|
| 17 | |
---|
| 18 | public class OthelloTDL { |
---|
| 19 | |
---|
| 20 | private static final String P_TDL = "tdl"; |
---|
| 21 | |
---|
| 22 | private static final String P_STAT = "stat"; |
---|
| 23 | private static final String P_SEED = "seed"; |
---|
| 24 | private static final String P_VERBOSITY = "verbosity"; |
---|
| 25 | |
---|
| 26 | private static final String P_RANDOMNESS = "randomness"; |
---|
| 27 | private static final String P_LEARNING_RATE = "learning-rate"; |
---|
| 28 | |
---|
| 29 | private static final String P_GAMES = "games"; |
---|
| 30 | |
---|
| 31 | public static void main(String[] args) { |
---|
| 32 | ParameterDatabase parameters = ec.Evolve.loadParameterDatabase(args); |
---|
| 33 | |
---|
| 34 | Parameter verbosityParam = new Parameter(P_VERBOSITY); |
---|
| 35 | int verbosity = parameters.getInt(verbosityParam, null, 0); |
---|
| 36 | if (verbosity < 0) { |
---|
| 37 | Output.initialError("Verbosity should be an integer >= 0.\n", verbosityParam); |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | Output output = new Output(true, verbosity); |
---|
| 41 | output.addLog(ec.util.Log.D_STDOUT, Output.V_VERBOSE, false); |
---|
| 42 | output.addLog(ec.util.Log.D_STDERR, Output.V_VERBOSE, true); |
---|
| 43 | |
---|
| 44 | int time = (int) (System.currentTimeMillis()); |
---|
| 45 | Parameter seedParam = new Parameter(P_SEED); |
---|
| 46 | int seed = ec.Evolve.determineSeed(output, parameters, seedParam, time, 0, false); |
---|
| 47 | MersenneTwisterFast random = new MersenneTwisterFast(seed); |
---|
| 48 | |
---|
| 49 | EvolutionState state = new SimpleEvolutionState(); |
---|
| 50 | state.parameters = parameters; |
---|
| 51 | state.random = new MersenneTwisterFast[] { random }; |
---|
| 52 | state.output = output; |
---|
| 53 | |
---|
| 54 | state.generation = 0; |
---|
| 55 | state.population = new Population(); |
---|
| 56 | state.population.subpops = new Subpopulation[1]; |
---|
| 57 | state.population.subpops[0] = new Subpopulation(); |
---|
| 58 | state.population.subpops[0].individuals = new Individual[1]; |
---|
| 59 | |
---|
| 60 | OthelloPlayer player = new OthelloPlayer(); |
---|
| 61 | DoubleVectorIndividual ind = new DoubleVectorIndividual(); |
---|
| 62 | ind.genome = player.getWPC(); |
---|
| 63 | ind.fitness = new SimpleFitness(); |
---|
| 64 | state.population.subpops[0].individuals[0] = ind; |
---|
| 65 | |
---|
| 66 | new OthelloTDL(state).run(player); |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | private Statistics stat; |
---|
| 70 | private EvolutionState state; |
---|
| 71 | private MersenneTwisterFast random; |
---|
| 72 | |
---|
| 73 | private int numGames; |
---|
| 74 | private double randomness; |
---|
| 75 | private double learningRate; |
---|
| 76 | |
---|
| 77 | public OthelloTDL(EvolutionState state) { |
---|
| 78 | this.state = state; |
---|
| 79 | this.random = state.random[0]; |
---|
| 80 | |
---|
| 81 | Parameter base = new Parameter(P_TDL); |
---|
| 82 | Parameter randomnessParam = base.push(P_RANDOMNESS); |
---|
| 83 | randomness = state.parameters.getDoubleWithDefault(randomnessParam, null, 0.1); |
---|
| 84 | |
---|
| 85 | Parameter learningRateParam = base.push(P_LEARNING_RATE); |
---|
| 86 | learningRate = state.parameters.getDoubleWithDefault(learningRateParam, null, 0.01); |
---|
| 87 | |
---|
| 88 | Parameter numGamesParam = base.push(P_GAMES); |
---|
| 89 | numGames = state.parameters.getIntWithDefault(numGamesParam, null, 1000000); |
---|
| 90 | state.numGenerations = numGames; |
---|
| 91 | |
---|
| 92 | Parameter statParam = base.push(P_STAT); |
---|
| 93 | stat = (Statistics) state.parameters.getInstanceForParameterEq(statParam, null, |
---|
| 94 | Statistics.class); |
---|
| 95 | stat.setup(state, statParam); |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | public void run(OthelloPlayer player) { |
---|
| 99 | BoardGame othelloGame = new OthelloGame(new OthelloBoard()); |
---|
| 100 | SelfPlayTDLScenario scenario = new SelfPlayTDLScenario(random, player, randomness, |
---|
| 101 | learningRate); |
---|
| 102 | |
---|
| 103 | for (int game = 0; game < numGames; game++) { |
---|
| 104 | stat.postEvaluationStatistics(state); |
---|
| 105 | othelloGame.reset(); |
---|
| 106 | scenario.play(othelloGame); |
---|
| 107 | state.generation++; |
---|
| 108 | } |
---|
| 109 | } |
---|
| 110 | } |
---|