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