2016-08-02 18:30:20 +00:00
|
|
|
<?php
|
|
|
|
|
2016-11-20 21:53:17 +00:00
|
|
|
declare(strict_types=1);
|
2016-08-02 18:30:20 +00:00
|
|
|
|
2016-08-05 08:20:31 +00:00
|
|
|
namespace tests\Phpml\NeuralNetwork\Node\Neuron;
|
2016-08-02 18:30:20 +00:00
|
|
|
|
2016-08-05 08:20:31 +00:00
|
|
|
use Phpml\NeuralNetwork\Node\Neuron\Synapse;
|
2016-08-02 18:30:20 +00:00
|
|
|
use Phpml\NeuralNetwork\Node\Neuron;
|
|
|
|
|
|
|
|
class SynapseTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
|
|
|
public function testSynapseInitialization()
|
|
|
|
{
|
|
|
|
$node = $this->getNodeMock($nodeOutput = 0.5);
|
|
|
|
|
|
|
|
$synapse = new Synapse($node, $weight = 0.75);
|
|
|
|
|
|
|
|
$this->assertEquals($node, $synapse->getNode());
|
|
|
|
$this->assertEquals($weight, $synapse->getWeight());
|
|
|
|
$this->assertEquals($weight * $nodeOutput, $synapse->getOutput());
|
|
|
|
|
|
|
|
$synapse = new Synapse($node);
|
|
|
|
|
|
|
|
$this->assertInternalType('float', $synapse->getWeight());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSynapseWeightChange()
|
|
|
|
{
|
|
|
|
$node = $this->getNodeMock();
|
|
|
|
$synapse = new Synapse($node, $weight = 0.75);
|
|
|
|
$synapse->changeWeight(1.0);
|
|
|
|
|
|
|
|
$this->assertEquals(1.75, $synapse->getWeight());
|
|
|
|
|
|
|
|
$synapse->changeWeight(-2.0);
|
|
|
|
|
|
|
|
$this->assertEquals(-0.25, $synapse->getWeight());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $output
|
|
|
|
*
|
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject
|
|
|
|
*/
|
|
|
|
private function getNodeMock($output = 1)
|
|
|
|
{
|
2016-09-21 19:46:16 +00:00
|
|
|
$node = $this->getMockBuilder(Neuron::class)->getMock();
|
|
|
|
$node->method('getOutput')->willReturn($output);
|
2016-08-02 18:30:20 +00:00
|
|
|
|
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
}
|