1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Ctefan\Kiwi; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* A linear constraint equation. |
8
|
|
|
* |
9
|
|
|
* A constraint equation is composed of an expression, an operator, and a strength. |
10
|
|
|
* The right hand side of the equation is implicitly zero. |
11
|
|
|
*/ |
12
|
|
|
class Constraint |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var Expression |
16
|
|
|
*/ |
17
|
|
|
protected $expression; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var float |
21
|
|
|
*/ |
22
|
|
|
protected $strength; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var int |
26
|
|
|
*/ |
27
|
|
|
protected $operator; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Constraint constructor. |
31
|
|
|
* |
32
|
|
|
* @param Expression $expression |
33
|
|
|
* @param int $operator |
34
|
|
|
* @param float|null $strength |
35
|
|
|
*/ |
36
|
21 |
|
public function __construct(Expression $expression, int $operator, ?float $strength = null) |
37
|
|
|
{ |
38
|
21 |
|
$this->expression = self::reduce($expression); |
39
|
21 |
|
$this->operator = $operator; |
40
|
21 |
|
$this->strength = $strength ? Strength::clip($strength) : Strength::required(); |
41
|
21 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Create a Constraint from another Constraint. |
45
|
|
|
* |
46
|
|
|
* @param self $otherConstraint |
47
|
|
|
* @param float $strength |
48
|
|
|
* @return Constraint |
49
|
|
|
*/ |
50
|
|
|
static public function createFromConstraint(self $otherConstraint, float $strength): self |
51
|
|
|
{ |
52
|
|
|
return new self($otherConstraint->getExpression(), $otherConstraint->getOperator(), $strength); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param Expression $expression |
57
|
|
|
* @return Expression |
58
|
|
|
*/ |
59
|
21 |
|
static private function reduce(Expression $expression): Expression |
60
|
|
|
{ |
61
|
21 |
|
$variables = new \SplObjectStorage(); |
62
|
21 |
|
foreach ($expression->getTerms() as $term) { |
63
|
21 |
|
$variable = $term->getVariable(); |
64
|
21 |
|
if (false === $variables->contains($variable)) { |
65
|
21 |
|
$value = 0.0; |
66
|
|
|
} else { |
67
|
|
|
$value = $variables->offsetGet($variable); |
68
|
|
|
} |
69
|
21 |
|
$value += $term->getCoefficient(); |
70
|
21 |
|
$variables->attach($variable, $value); |
71
|
|
|
} |
72
|
|
|
|
73
|
21 |
|
$reducedTerms = []; |
74
|
21 |
|
foreach ($variables as $variable) { |
75
|
21 |
|
$reducedTerms[] = new Term($variable, $variables->offsetGet($variable)); |
76
|
|
|
} |
77
|
|
|
|
78
|
21 |
|
return new Expression($reducedTerms, $expression->getConstant()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return Expression |
83
|
|
|
*/ |
84
|
21 |
|
public function getExpression(): Expression |
85
|
|
|
{ |
86
|
21 |
|
return $this->expression; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param Expression $expression |
91
|
|
|
*/ |
92
|
|
|
public function setExpression(Expression $expression): void |
93
|
|
|
{ |
94
|
|
|
$this->expression = $expression; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return float |
99
|
|
|
*/ |
100
|
21 |
|
public function getStrength(): float |
101
|
|
|
{ |
102
|
21 |
|
return $this->strength; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param float $strength |
107
|
|
|
* @return Constraint |
108
|
|
|
*/ |
109
|
3 |
|
public function setStrength(float $strength): self |
110
|
|
|
{ |
111
|
3 |
|
$this->strength = $strength; |
112
|
3 |
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return int |
117
|
|
|
*/ |
118
|
21 |
|
public function getOperator(): int |
119
|
|
|
{ |
120
|
21 |
|
return $this->operator; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param int $operator |
125
|
|
|
*/ |
126
|
|
|
public function setOperator(int $operator): void |
127
|
|
|
{ |
128
|
|
|
$this->operator = $operator; |
129
|
|
|
} |
130
|
|
|
} |