source: java/ecj/cecj/app/othello/Othello.java @ 741

Last change on this file since 741 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.5 KB
Line 
1package cecj.app.othello;
2
3import cecj.interaction.InteractionResult;
4import cecj.interaction.WinDrawLossResult;
5import cecj.interaction.WinDrawLossResult.Result;
6import cecj.problems.SymmetricTestBasedProblem;
7import cecj.utils.Pair;
8import ec.EvolutionState;
9import ec.Individual;
10import ec.util.Parameter;
11import ec.vector.DoubleVectorIndividual;
12import games.Player;
13import games.scenarios.GameScenario;
14import games.scenarios.RandomizedTwoPlayersGameScenario;
15import games.scenarios.SimpleTwoPlayersGameScenario;
16
17public class Othello extends SymmetricTestBasedProblem {
18
19        private static final int WPC_LENGTH = 64;
20        private static final String P_RANDOMNESS = "randomness";
21
22        private double randomness;
23        private boolean randomizedPlay;
24
25        @Override
26        public void setup(EvolutionState state, Parameter base) {
27                super.setup(state, base);
28
29                Parameter randomnessParam = base.push(P_RANDOMNESS);
30                if (state.parameters.exists(randomnessParam)) {
31                        randomness = state.parameters.getDoubleWithDefault(randomnessParam, null, 0);
32                        randomizedPlay = true;
33                } else {
34                        randomizedPlay = false;
35                }
36        }
37
38        @Override
39        public Pair<? extends InteractionResult> test(EvolutionState state, Individual candidate,
40                        Individual test) {
41                if (!(candidate instanceof DoubleVectorIndividual)
42                                || !(test instanceof DoubleVectorIndividual)) {
43                        state.output.error("Othello players should be represented by floats vectors\n");
44                }
45
46                double[] wpc1 = ((DoubleVectorIndividual) candidate).genome;
47                double[] wpc2 = ((DoubleVectorIndividual) test).genome;
48
49                if (wpc1.length != WPC_LENGTH || wpc2.length != WPC_LENGTH) {
50                        state.output.error("Players WPC vectors length should be 64\n");
51                }
52
53                OthelloPlayer player1 = new OthelloPlayer(wpc1);
54                OthelloPlayer player2 = new OthelloPlayer(wpc2);
55                OthelloGame game = new OthelloGame(new OthelloBoard());
56
57                GameScenario scenario;
58                if (randomizedPlay) {
59                        scenario = new RandomizedTwoPlayersGameScenario(state.random[0], new Player[] {
60                                        player1, player2 }, new double[] { randomness, randomness });
61                } else {
62                        scenario = new SimpleTwoPlayersGameScenario(new Player[] { player1, player2 });
63                }
64
65                int result = scenario.play(game);
66                if (result > 0) {
67                        return new Pair<WinDrawLossResult>(new WinDrawLossResult(Result.WIN),
68                                        new WinDrawLossResult(Result.LOSS));
69                } else if (result < 0) {
70                        return new Pair<WinDrawLossResult>(new WinDrawLossResult(Result.LOSS),
71                                        new WinDrawLossResult(Result.WIN));
72                } else {
73                        return new Pair<WinDrawLossResult>(new WinDrawLossResult(Result.DRAW),
74                                        new WinDrawLossResult(Result.DRAW));
75                }
76        }
77}
Note: See TracBrowser for help on using the repository browser.