Test Failed
Pull Request — master (#98)
by
unknown
05:26
created

Anneal   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 80
Duplicated Lines 7.5 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 6
loc 80
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
C run() 6 33 8

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Phpml\Optimization;
6
7
class Anneal
8
{
9
    /**
10
    * @var array
11
    */
12
    private $domain;
13
14
    /**
15
    * @var Cost
16
    */    
17
    private $cost;
18
19
    /**
20
    * @var int
21
    */
22
    private $temperature;
23
24
    /**
25
    * @var float
26
    */
27
    private $cool;
28
29
    /**
30
    * @var int
31
    */
32
    private $step;
33
34
    /**
35
     * @param array      $domain
36
     * @param Cost       $cost
37
     * @param int        $temperature
38
     * @param float      $cool
39
     * @param int        $step
40
     */
41
    public function __construct(array $domain, Cost $cost, int $temperature = 1000, float $cool = 0.99, int $step=1)
42
    {
43
        $this->domain = $domain;
44
        $this->cost = $cost;
45
        $this->temperature = $temperature;
46
        $this->cool = $cool;
47
        $this->step = $step;
48
    }
49
50
    /**
51
     * @return array
52
     */
53
    public function run()
54
    {
55
        $i=0;
0 ignored issues
show
Unused Code introduced by
$i is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
56
        $vec = [];
57
        for ($i=0 ; $i<count($this->domain); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
58
            $vec[]=rand($this->domain[$i][0], $this->domain[$i][1]);
59
        }
60
        while ($this->temperature > 0.1) {
61
            $idx = rand(0, count($this->domain) - 1);
62
            $dir = rand(-$this->step, $this->step);
63
            $newVec = [];
64
            for ($i=0; $i<count($vec); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
65
                $newVec[]=($vec[$i]);
66
            }
67
            $newVec[$idx]+=$dir;
68 View Code Duplication
            if ($newVec[$idx] < $this->domain[$idx][0]) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
69
                $newVec[$idx] = $this->domain[$idx][0];
70
            }
71 View Code Duplication
            if ($newVec[$idx] > $this->domain[$idx][1]) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
72
                $newVec[$idx] = $this->domain[$idx][1];
73
            }
74
75
            $ea = $this->cost->calc($vec);
76
            $eb = $this->cost->calc($newVec);
77
            $p = exp(-1*($eb-$ea)/$this->temperature);
78
            if ($eb < $ea || (rand(0, 1000)/1000) < $p) {
79
                $vec = $newVec;
80
            }
81
82
            $this->temperature *= $this->cool;
83
        }
84
        return $vec;
85
    }
86
}
87