[44] | 1 | package cecj.app; |
---|
| 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 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 | } |
---|