Completed
Push — develop ( 7062ee...12ee62 )
by Arkadiusz
02:50
created

Synapse::getNode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
declare (strict_types = 1);
4
5
namespace Phpml\NeuralNetwork\Node\Neuron;
6
7
use Phpml\NeuralNetwork\Node;
8
9
class Synapse
10
{
11
    /**
12
     * @var float
13
     */
14
    protected $weight;
15
16
    /**
17
     * @var Node
18
     */
19
    protected $node;
20
21
    /**
22
     * @param Node       $node
23
     * @param float|null $weight
24
     */
25
    public function __construct(Node $node, float $weight = null)
26
    {
27
        $this->node = $node;
28
        $this->weight = $weight ?: $this->generateRandomWeight();
29
    }
30
31
    /**
32
     * @return float
33
     */
34
    protected function generateRandomWeight(): float
35
    {
36
        return 1 / rand(5, 25) * (rand(0, 1) ? -1 : 1);
37
    }
38
39
    /**
40
     * @return float
41
     */
42
    public function getOutput(): float
43
    {
44
        return $this->weight * $this->node->getOutput();
45
    }
46
47
    /**
48
     * @param float $delta
49
     */
50
    public function changeWeight($delta)
51
    {
52
        $this->weight += $delta;
53
    }
54
55
    /**
56
     * @return float
57
     */
58
    public function getWeight()
59
    {
60
        return $this->weight;
61
    }
62
63
    /**
64
     * @return Node
65
     */
66
    public function getNode()
67
    {
68
        return $this->node;
69
    }
70
}
71