source: java/ecj/app/go/Go.java @ 42

Last change on this file since 42 was 42, checked in by mszubert, 14 years ago

refactored cecj; framsticks package added

File size: 2.6 KB
Line 
1package cecj.app.go;
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 Go extends SymmetricTestBasedProblem {
21
22        private static final int WPC_LENGTH = 25;
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            GoPlayer player1 = new GoPlayer(wpc1);
57                GoPlayer player2 = new GoPlayer(wpc2);
58                GoGame game = new GoGame(new GoBoard());
59
60                GameScenario scenario;
61                if (randomizedPlay) {
62                        scenario = new RandomizedTwoPlayersGameScenario(state.random[0], new Player[] {
63                                        player1, player2 }, new double[] { randomness, randomness });
64                } else {
65                        scenario = new SimpleTwoPlayersGameScenario(new Player[] { player1, player2 });
66                }
67
68                int result = scenario.play(game);
69                if (result > 0) {
70                        return new Pair<WinDrawLossResult>(new WinDrawLossResult(Result.WIN),
71                                        new WinDrawLossResult(Result.LOSS));
72                } else if (result < 0) {
73                        return new Pair<WinDrawLossResult>(new WinDrawLossResult(Result.LOSS),
74                                        new WinDrawLossResult(Result.WIN));
75                } else {
76                        return new Pair<WinDrawLossResult>(new WinDrawLossResult(Result.DRAW),
77                                        new WinDrawLossResult(Result.DRAW));
78                }
79        }
80
81}
Note: See TracBrowser for help on using the repository browser.