Skip to content

Method: requestedMemoryCapacity()

1: package de.fhdw.gaming.ipspiel23.dilemma.strategy.internals.reactive;
2:
3: import java.util.Optional;
4: import java.util.Random;
5:
6: import de.fhdw.gaming.ipspiel23.dilemma.domain.DilemmaAnswerType;
7: import de.fhdw.gaming.ipspiel23.dilemma.domain.IDilemmaPlayer;
8: import de.fhdw.gaming.ipspiel23.dilemma.domain.IDilemmaState;
9: import de.fhdw.gaming.ipspiel23.dilemma.domain.IDilemmaStrategy;
10: import de.fhdw.gaming.ipspiel23.dilemma.moves.IDilemmaMove;
11: import de.fhdw.gaming.ipspiel23.dilemma.moves.IDilemmaMoveFactory;
12: import de.fhdw.gaming.ipspiel23.dilemma.strategy.internals.DilemmaMemoryStrategy;
13: import de.fhdw.gaming.ipspiel23.dilemma.strategy.internals.DilemmaRoundData;
14: import de.fhdw.gaming.ipspiel23.dilemma.strategy.internals.DilemmaRoundPlayerData;
15: import de.fhdw.gaming.ipspiel23.memory.GameMemoryCapacity;
16: import de.fhdw.gaming.ipspiel23.memory.IGameMemory;
17: import de.fhdw.gaming.ipspiel23.memory.IGameMemoryCapacity;
18:
19: /**
20: * Implements {@link IDilemmaStrategy} by cooperating with probability y in first round
21: * and with probabilities p or q after opponent cooperates or defects.
22: */
23: public class DilemmaReactiveStrategy extends DilemmaMemoryStrategy {
24:
25: /**
26: * Probability to cooperate on the first round.
27: */
28: static final double COOPERATION_FIRST_ROUND_PROB = 0.5d;
29:
30: /**
31: * Probability to cooperate after the opponent cooperated.
32: */
33: static final double COOPERATION_AFTER_COOPERATION_PROB = 0.8d;
34:
35: /**
36: * Probability to cooperate after the opponent defected.
37: */
38: static final double COOPERATION_AFTER_DEFECT_PROB = 0.2d;
39:
40: /**
41: * A random number generator to implement playing moves based on probabilities.
42: */
43: private static final Random RANDOM = new Random();
44:
45: /**
46: * Creates an {@link DilemmaReactiveStrategy}.
47: *
48: * @param moveFactory The factory for creating Dilemma moves.
49: */
50: DilemmaReactiveStrategy(final IDilemmaMoveFactory moveFactory) {
51: super(moveFactory);
52: }
53:
54: @Override
55: public Optional<IDilemmaMove> computeNextMove(final int gameId,
56: final IDilemmaPlayer player,
57: final IDilemmaState state) {
58: final IGameMemory<DilemmaRoundData> memory = getMemoryForPlayer(player, state);
59: final IDilemmaMoveFactory moveFactory = getMoveFactory();
60: // cooperate on first round with a probability of y
61: if (memory.size() == 0) {
62: if (RANDOM.nextDouble() < COOPERATION_FIRST_ROUND_PROB) {
63: return Optional.of(moveFactory.createCooperateMove());
64: }
65: return Optional.of(moveFactory.createDefectMove());
66: }
67: // cooperate with probability of p if opponent cooperated last round
68: final DilemmaRoundData previousRound = memory.getRound(0, true);
69: final DilemmaRoundPlayerData otherPlayersAction = previousRound.forOpponentOf(player);
70: if (otherPlayersAction.answer().equals(DilemmaAnswerType.COOPERATE)) {
71: if (RANDOM.nextDouble() < COOPERATION_AFTER_COOPERATION_PROB) {
72: return Optional.of(moveFactory.createCooperateMove());
73: }
74: return Optional.of(moveFactory.createDefectMove());
75: }
76: // cooperate with probability of y if opponent defected last round
77: if (RANDOM.nextDouble() < COOPERATION_AFTER_DEFECT_PROB) {
78: return Optional.of(moveFactory.createCooperateMove());
79: }
80: return Optional.of(moveFactory.createDefectMove());
81: }
82:
83: @Override
84: protected IGameMemoryCapacity requestedMemoryCapacity() {
85: return GameMemoryCapacity.unlimited();
86: }
87: }