source: java/ecj/cecj/app/othello/OthelloPlayerFitnessCalculator.java @ 1134

Last change on this file since 1134 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.7 KB
Line 
1package cecj.app.othello;
2
3import cecj.statistics.ObjectiveFitnessCalculator;
4import ec.EvolutionState;
5import ec.Individual;
6import ec.util.Parameter;
7import ec.vector.DoubleVectorIndividual;
8import games.BoardGame;
9import games.Player;
10import games.scenarios.GameScenario;
11import games.scenarios.RandomizedTwoPlayersGameScenario;
12
13public abstract class OthelloPlayerFitnessCalculator implements ObjectiveFitnessCalculator {
14
15        protected static final int WPC_LENGTH = 64;
16       
17        protected static final String P_EVALUATOR_RANDOMNESS = "evaluator-randomness";
18        protected static final String P_EVALUATED_RANDOMNESS = "evaluated-randomness";
19
20        private static final String P_PLAY_BOTH = "play-both";
21        private static final String P_REPEATS = "repeats";
22
23        protected double evaluatedRandomness;
24        protected double evaluatorRandomness;
25        private boolean playBoth;
26        private int repeats;
27
28        public void setup(EvolutionState state, Parameter base) {
29                Parameter randomnessParam = base.push(P_EVALUATED_RANDOMNESS);
30                evaluatedRandomness = state.parameters.getDoubleWithDefault(randomnessParam, null, 0);
31
32                randomnessParam = base.push(P_EVALUATOR_RANDOMNESS);
33                evaluatorRandomness = state.parameters.getDoubleWithDefault(randomnessParam, null, 0);
34
35                Parameter repetitionsParam = base.push(P_REPEATS);
36                repeats = state.parameters.getIntWithDefault(repetitionsParam, null, 1);
37
38                Parameter playBothParam = base.push(P_PLAY_BOTH);
39                playBoth = state.parameters.getBoolean(playBothParam, null, false);
40        }
41
42        public float calculateObjectiveFitness(EvolutionState state, Individual ind) {
43                if (!(ind instanceof DoubleVectorIndividual)) {
44                        state.output.error("Othello players should be represented by floats vectors\n");
45                }
46
47                double[] wpc1 = ((DoubleVectorIndividual) ind).genome;
48
49                if (wpc1.length != WPC_LENGTH) {
50                        state.output.error("Players WPC vectors length should be 64\n");
51                }
52
53                Player player1 = new OthelloPlayer(wpc1);
54                Player player2 = getPlayer();
55                BoardGame game = new OthelloGame(new OthelloBoard());
56
57                GameScenario scenario1 = new RandomizedTwoPlayersGameScenario(state.random[0], new Player[] { player1,
58                                player2 }, new double[] { evaluatedRandomness, evaluatorRandomness });
59                GameScenario scenario2 = new RandomizedTwoPlayersGameScenario(state.random[0], new Player[] { player2,
60                                player1 }, new double[] { evaluatorRandomness, evaluatedRandomness });
61
62                float sum = 0;
63                for (int r = 0; r < repeats; r++) {
64                        game.reset();
65                        sum += ((scenario1.play(game) > 0) ? 1 : 0);
66                        if (playBoth) {
67                                game.reset();
68                                sum += ((scenario2.play(game) < 0) ? 1 : 0);
69                        }
70                }
71
72                if (playBoth) {
73                        return sum / (repeats * 2);
74                } else {
75                        return sum / repeats;
76                }
77        }
78
79        protected abstract OthelloPlayer getPlayer();
80}
Note: See TracBrowser for help on using the repository browser.