source: java/ecj/cecj/app/othello/OthelloTDL.java @ 473

Last change on this file since 473 was 193, checked in by Maciej Komosinski, 11 years ago

Set svn:eol-style native for all textual files

  • Property svn:eol-style set to native
File size: 3.5 KB
Line 
1package cecj.app.othello;
2
3import ec.EvolutionState;
4import ec.Individual;
5import ec.Population;
6import ec.Statistics;
7import ec.Subpopulation;
8import ec.simple.SimpleEvolutionState;
9import ec.simple.SimpleFitness;
10import ec.util.MersenneTwisterFast;
11import ec.util.Output;
12import ec.util.Parameter;
13import ec.util.ParameterDatabase;
14import ec.vector.DoubleVectorIndividual;
15import games.BoardGame;
16import games.scenarios.SelfPlayTDLScenario;
17
18public 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}
Note: See TracBrowser for help on using the repository browser.