source: java/ecj/games/OthelloTDL.java @ 28

Last change on this file since 28 was 28, checked in by mszubert, 15 years ago

cecj - coEvolutionary Computation in Java with additional games package

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