@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types=1); |
|
| 3 | +declare(strict_types = 1); |
|
| 4 | 4 | |
| 5 | 5 | namespace Phpml\Optimization; |
| 6 | 6 | |
@@ -7,28 +7,28 @@ |
||
| 7 | 7 | class Anneal |
| 8 | 8 | { |
| 9 | 9 | /** |
| 10 | - * @var array |
|
| 11 | - */ |
|
| 10 | + * @var array |
|
| 11 | + */ |
|
| 12 | 12 | private $domain; |
| 13 | 13 | |
| 14 | 14 | /** |
| 15 | - * @var Cost |
|
| 16 | - */ |
|
| 15 | + * @var Cost |
|
| 16 | + */ |
|
| 17 | 17 | private $cost; |
| 18 | 18 | |
| 19 | 19 | /** |
| 20 | - * @var int |
|
| 21 | - */ |
|
| 20 | + * @var int |
|
| 21 | + */ |
|
| 22 | 22 | private $temperature; |
| 23 | 23 | |
| 24 | 24 | /** |
| 25 | - * @var float |
|
| 26 | - */ |
|
| 25 | + * @var float |
|
| 26 | + */ |
|
| 27 | 27 | private $cool; |
| 28 | 28 | |
| 29 | 29 | /** |
| 30 | - * @var int |
|
| 31 | - */ |
|
| 30 | + * @var int |
|
| 31 | + */ |
|
| 32 | 32 | private $step; |
| 33 | 33 | |
| 34 | 34 | /** |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types=1); |
|
| 3 | +declare(strict_types = 1); |
|
| 4 | 4 | |
| 5 | 5 | namespace Phpml\Optimization; |
| 6 | 6 | |
@@ -38,7 +38,7 @@ discard block |
||
| 38 | 38 | * @param float $cool |
| 39 | 39 | * @param int $step |
| 40 | 40 | */ |
| 41 | - public function __construct(array $domain, Cost $cost, int $temperature = 1000, float $cool = 0.99, int $step=1) |
|
| 41 | + public function __construct(array $domain, Cost $cost, int $temperature = 1000, float $cool = 0.99, int $step = 1) |
|
| 42 | 42 | { |
| 43 | 43 | $this->domain = $domain; |
| 44 | 44 | $this->cost = $cost; |
@@ -52,19 +52,19 @@ discard block |
||
| 52 | 52 | */ |
| 53 | 53 | public function run() |
| 54 | 54 | { |
| 55 | - $i=0; |
|
| 55 | + $i = 0; |
|
| 56 | 56 | $vec = []; |
| 57 | - for ($i=0 ; $i<count($this->domain); $i++) { |
|
| 58 | - $vec[]=rand($this->domain[$i][0], $this->domain[$i][1]); |
|
| 57 | + for ($i = 0; $i < count($this->domain); $i++) { |
|
| 58 | + $vec[] = rand($this->domain[$i][0], $this->domain[$i][1]); |
|
| 59 | 59 | } |
| 60 | 60 | while ($this->temperature > 0.1) { |
| 61 | 61 | $idx = rand(0, count($this->domain) - 1); |
| 62 | 62 | $dir = rand(-$this->step, $this->step); |
| 63 | 63 | $newVec = []; |
| 64 | - for ($i=0; $i<count($vec); $i++) { |
|
| 65 | - $newVec[]=($vec[$i]); |
|
| 64 | + for ($i = 0; $i < count($vec); $i++) { |
|
| 65 | + $newVec[] = ($vec[$i]); |
|
| 66 | 66 | } |
| 67 | - $newVec[$idx]+=$dir; |
|
| 67 | + $newVec[$idx] += $dir; |
|
| 68 | 68 | if ($newVec[$idx] < $this->domain[$idx][0]) { |
| 69 | 69 | $newVec[$idx] = $this->domain[$idx][0]; |
| 70 | 70 | } |
@@ -74,8 +74,8 @@ discard block |
||
| 74 | 74 | |
| 75 | 75 | $ea = $this->cost->calc($vec); |
| 76 | 76 | $eb = $this->cost->calc($newVec); |
| 77 | - $p = exp(-1*($eb-$ea)/$this->temperature); |
|
| 78 | - if ($eb < $ea || (rand(0, 1000)/1000) < $p) { |
|
| 77 | + $p = exp(-1 * ($eb - $ea) / $this->temperature); |
|
| 78 | + if ($eb < $ea || (rand(0, 1000) / 1000) < $p) { |
|
| 79 | 79 | $vec = $newVec; |
| 80 | 80 | } |
| 81 | 81 | |