1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace lucidtaz\minimax\engine; |
6
|
|
|
|
7
|
|
|
use lucidtaz\minimax\game\GameState; |
8
|
|
|
use lucidtaz\minimax\game\Player; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Node in the decision search tree |
12
|
|
|
* |
13
|
|
|
* An object of this class can be queried for its ideal decision (and according |
14
|
|
|
* score) by calling the decide() method. It will recursively construct child |
15
|
|
|
* nodes and evaluate them using that method as well. |
16
|
|
|
*/ |
17
|
|
|
class DecisionNode |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var Player The player to optimize for. |
21
|
|
|
*/ |
22
|
|
|
private $objectivePlayer; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var GameState The current GameState to base future decisions on. |
26
|
|
|
*/ |
27
|
|
|
private $state; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var int Limit on how deep we can continue to search, recursion limiter. |
31
|
|
|
*/ |
32
|
|
|
private $depthLeft; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var NodeType Whether we are a min-node or a max-node. This enables the |
36
|
|
|
* caller to select either the most favorable or the least favorable |
37
|
|
|
* outcome. |
38
|
|
|
*/ |
39
|
|
|
private $type; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var AlphaBeta Constraints for alpha-beta pruning |
43
|
|
|
*/ |
44
|
|
|
private $alphaBeta; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param Player $objectivePlayer The Player to optimize for |
48
|
|
|
* @param GameState $state Current GameState to base decisions on |
49
|
|
|
* @param int $depthLeft Recursion limiter |
50
|
|
|
* @param NodeType $type Signifies whether to minimize or maximize the score |
51
|
|
|
* @param AlphaBeta $alphaBeta Range of potential scores to check |
52
|
|
|
*/ |
53
|
14 |
|
public function __construct(Player $objectivePlayer, GameState $state, int $depthLeft, NodeType $type, AlphaBeta $alphaBeta) |
54
|
|
|
{ |
55
|
14 |
|
$this->objectivePlayer = $objectivePlayer; |
56
|
14 |
|
$this->state = $state; |
57
|
14 |
|
$this->depthLeft = $depthLeft; |
58
|
14 |
|
$this->type = $type; |
59
|
14 |
|
$this->alphaBeta = $alphaBeta; |
60
|
14 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Determine the ideal move for this node |
64
|
|
|
* |
65
|
|
|
* This means either the best or the worst possible outcome for the |
66
|
|
|
* objective player, based on who is actually playing. (If the objective |
67
|
|
|
* player is currently playing, we take the best outcome, otherwise we take |
68
|
|
|
* the worst. This reflects that the opponent also plays optimally.) |
69
|
|
|
*/ |
70
|
14 |
|
public function traverseGameTree(): TraversalResult |
71
|
|
|
{ |
72
|
14 |
|
if ($this->depthLeft === 0) { |
73
|
13 |
|
return TraversalResult::withoutMove($this->makeLeafEvaluation(), Analytics::forLeafNode()); |
74
|
|
|
} |
75
|
|
|
|
76
|
13 |
|
$possibleMoves = $this->state->getPossibleMoves(); |
77
|
13 |
|
if (empty($possibleMoves)) { |
78
|
9 |
|
return TraversalResult::withoutMove($this->makeLeafEvaluation(), Analytics::forLeafNode()); |
79
|
|
|
} |
80
|
|
|
|
81
|
13 |
|
$analytics = Analytics::forInternalNode(); |
82
|
13 |
|
$idealMove = null; |
83
|
13 |
|
$idealMoveResult = null; |
84
|
13 |
|
foreach ($possibleMoves as $move) { |
85
|
13 |
|
if (!$this->alphaBeta->isPositiveRange()) { |
86
|
|
|
// Subtree became fruitless, return to caller asap |
87
|
12 |
|
break; |
88
|
|
|
} |
89
|
|
|
|
90
|
13 |
|
$moveResult = $this->getChildResult($move); |
91
|
13 |
|
$analytics->add($moveResult->analytics); |
92
|
13 |
|
$this->alphaBeta->update($moveResult->evaluation, $this->type); |
93
|
13 |
|
if ($idealMoveResult === null || $this->isIdealOver($moveResult->evaluation, $idealMoveResult->evaluation)) { |
94
|
13 |
|
$idealMove = $move; |
95
|
13 |
|
$idealMoveResult = $moveResult; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
13 |
|
return TraversalResult::create($idealMove, $idealMoveResult->evaluation, $analytics); |
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Formulate the evaluation, this node being a leaf node |
104
|
|
|
*/ |
105
|
14 |
|
private function makeLeafEvaluation(): Evaluation |
106
|
|
|
{ |
107
|
14 |
|
$result = new Evaluation(); |
108
|
14 |
|
$result->age = $this->depthLeft; |
109
|
14 |
|
$result->score = $this->state->evaluateScore($this->objectivePlayer); |
110
|
14 |
|
return $result; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Recursively evaluate a child decision |
115
|
|
|
* |
116
|
|
|
* Apply a move and evaluate the outcome |
117
|
|
|
* |
118
|
|
|
* @param GameState $stateAfterMove The GameState that was created as a |
119
|
|
|
* result of a possible move. |
120
|
|
|
*/ |
121
|
13 |
|
private function getChildResult(GameState $stateAfterMove): TraversalResult |
122
|
|
|
{ |
123
|
13 |
|
$nextPlayerIsFriendly = $stateAfterMove->getNextPlayer()->isFriendsWith($this->objectivePlayer); |
124
|
13 |
|
$nextDecisionPoint = new static( |
125
|
13 |
|
$this->objectivePlayer, |
126
|
13 |
|
$stateAfterMove, |
127
|
13 |
|
$this->depthLeft - 1, |
128
|
13 |
|
$nextPlayerIsFriendly ? NodeType::MAX() : NodeType::MIN(), |
129
|
13 |
|
clone $this->alphaBeta |
130
|
|
|
); |
131
|
13 |
|
return $nextDecisionPoint->traverseGameTree(); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Compare two evaluations |
136
|
|
|
* |
137
|
|
|
* The meaning of "best" is decided by the "ideal" member variable |
138
|
|
|
* comparator |
139
|
|
|
*/ |
140
|
12 |
|
private function isIdealOver(Evaluation $a, Evaluation $b): bool |
141
|
|
|
{ |
142
|
12 |
|
$ideal = $this->type == NodeType::MIN() |
143
|
11 |
|
? Evaluation::getWorstComparator() |
144
|
12 |
|
: Evaluation::getBestComparator(); |
145
|
12 |
|
$idealEvaluationResult = $ideal($a, $b); |
146
|
12 |
|
return $idealEvaluationResult > 0; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: