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