source: java/ecj/cecj/app/othello/OthelloTDLImprover.java @ 438

Last change on this file since 438 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: 2.0 KB
RevLine 
[44]1package cecj.app.othello;
2
3import ec.EvolutionState;
4import ec.Individual;
5import ec.util.Parameter;
6import ec.vector.DoubleVectorIndividual;
7import games.BoardGame;
8import games.Player;
9import cecj.eval.TDLImprover;
10import games.scenarios.SelfPlayTDLScenario;
11
12public class OthelloTDLImprover implements TDLImprover {
13
14        private static final int WPC_LENGTH = 64;
15
16        private static final String P_REPEATS = "repeats";
17        private static final String P_RANDOMNESS = "randomness";
18        private static final String P_LEARNING_RATE = "learning-rate";
19
20        private int repeats;
21        private double randomness;
22        private double learningRate;
23
24        public void setup(EvolutionState state, Parameter base) {
25                Parameter randomnessParam = base.push(P_RANDOMNESS);
26                randomness = state.parameters.getDoubleWithDefault(randomnessParam, null, 0.1);
27
28                Parameter learningRateParam = base.push(P_LEARNING_RATE);
29                learningRate = state.parameters.getDoubleWithDefault(learningRateParam, null, 0.01);
30
31                Parameter repeatsParam = base.push(P_REPEATS);
32                repeats = state.parameters.getIntWithDefault(repeatsParam, null, 10);
33        }
34
35        public void improve(EvolutionState state, Individual ind) {
36                Player player = new OthelloPlayer(getWPC(state, ind));
37                BoardGame game = new OthelloGame(new OthelloBoard());
38                SelfPlayTDLScenario selfPlayScenario = new SelfPlayTDLScenario(state.random[0], player,
39                        randomness, learningRate);
40
41                for (int r = 0; r < repeats; r++) {
42                        game.reset();
43                        selfPlayScenario.play(game);
44                }
45        }
46
47        public void prepareForImproving(EvolutionState state, Individual ind) {
48                double[] wpc = getWPC(state, ind);
49                for (int i = 0; i < WPC_LENGTH; i++) {
50                        wpc[i] = 0.0;
51                }
52        }
53
54        private double[] getWPC(EvolutionState state, Individual ind) {
55                if (!(ind instanceof DoubleVectorIndividual)) {
56                        state.output.error("Othello players should be represented by floats vectors\n");
57                }
58
59                double[] wpc = ((DoubleVectorIndividual) ind).genome;
60                if (wpc.length != WPC_LENGTH) {
61                        state.output.error("Players WPC vectors length should be 64\n");
62                }
63
64                return wpc;
65        }
66}
Note: See TracBrowser for help on using the repository browser.