1 | package cecj.app.go; |
---|
2 | |
---|
3 | import cecj.app.othello.OthelloBoard; |
---|
4 | import cecj.app.othello.OthelloGame; |
---|
5 | import cecj.app.othello.OthelloPlayer; |
---|
6 | import cecj.interaction.InteractionResult; |
---|
7 | import cecj.interaction.WinDrawLossResult; |
---|
8 | import cecj.interaction.WinDrawLossResult.Result; |
---|
9 | import cecj.problems.SymmetricTestBasedProblem; |
---|
10 | import cecj.utils.Pair; |
---|
11 | import ec.EvolutionState; |
---|
12 | import ec.Individual; |
---|
13 | import ec.util.Parameter; |
---|
14 | import ec.vector.DoubleVectorIndividual; |
---|
15 | import games.Player; |
---|
16 | import games.scenarios.GameScenario; |
---|
17 | import games.scenarios.RandomizedTwoPlayersGameScenario; |
---|
18 | import games.scenarios.SimpleTwoPlayersGameScenario; |
---|
19 | |
---|
20 | public 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 | } |
---|