source: java/ecj/cecj/app/BoardGameProblem.java @ 714

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