Skip to content

Commit 165db72

Browse files
authored
Merge pull request #1 from voodooism/update-php-version
Update php version
2 parents 5147a07 + 8c46717 commit 165db72

13 files changed

+89
-198
lines changed

composer.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
{
22
"name": "voodooism/php-genetic-algorithm",
33
"type": "library",
4-
"description": "Simple php algorithm",
4+
"description": "Simple php genetic algorithm",
55
"require": {
6-
"php": "^7.2"
6+
"php": "^7.4"
77
},
88
"require-dev": {
9-
"phpunit/phpunit": "^8.5"
9+
"phpunit/phpunit": "^8.5",
10+
"vimeo/psalm": "^4.7"
1011
},
1112
"autoload": {
1213
"psr-4": {

psalm.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0"?>
2+
<psalm
3+
errorLevel="2"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xmlns="https://getpsalm.org/schema/config"
6+
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
7+
>
8+
<projectFiles>
9+
<directory name="src" />
10+
<ignoreFiles>
11+
<directory name="vendor" />
12+
</ignoreFiles>
13+
</projectFiles>
14+
</psalm>

src/DNA/ASCIIStringDNA.php

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,10 @@
55
namespace Voodooism\Genetic\DNA;
66

77
use Exception;
8+
use InvalidArgumentException;
89
use RuntimeException;
910
use Voodooism\Genetic\DNA\Gene\ASCIIStringGene;
10-
use Webmozart\Assert\Assert;
1111

12-
/**
13-
* Class ASCIIStringDNA
14-
*
15-
* @package Voodooism\Genetic\DNA
16-
*/
1712
class ASCIIStringDNA extends AbstractDNA
1813
{
1914
/**
@@ -24,36 +19,37 @@ class ASCIIStringDNA extends AbstractDNA
2419

2520
/**
2621
* Contains the goal of evolution.
27-
*
28-
* @var string
2922
*/
30-
private $target;
23+
private string $target;
3124

3225
/**
3326
* The length of the goal string.
34-
*
35-
* @var int
3627
*/
37-
private $length;
28+
private int $length;
3829

3930
/**
4031
* Every character in a phrase is a gene.
4132
*
42-
* @var ASCIIStringGene[]
33+
* @psalm-suppress NonInvariantDocblockPropertyType
34+
* @var array<int, ASCIIStringGene>
4335
*/
44-
protected $genes;
36+
protected array $genes;
4537

4638
/**
47-
* ASCIIStringDNA constructor.
48-
*
49-
* @param string $target
50-
* @param array $genes
39+
* @psalm-suppress DocblockTypeContradiction
40+
* @param array<int, ASCIIStringGene> $genes
5141
*
5242
* @throws Exception
5343
*/
5444
public function __construct(string $target, array $genes = [])
5545
{
56-
Assert::allIsInstanceOf($genes, ASCIIStringGene::class);
46+
foreach ($genes as $gene) {
47+
if (!$gene instanceof ASCIIStringGene) {
48+
throw new InvalidArgumentException(
49+
sprintf('Gene should be instance of `%s`.', ASCIIStringGene::class)
50+
);
51+
}
52+
}
5753

5854
$this->target = $target;
5955
$this->length = strlen($target);
@@ -101,9 +97,6 @@ public function mutate(float $mutationRate): void
10197
}
10298
}
10399

104-
/**
105-
* @inheritDoc
106-
*/
107100
public function evaluateFitness(): void
108101
{
109102
$score = 0;
@@ -124,18 +117,18 @@ public function evaluateFitness(): void
124117
*/
125118
public function crossover(AbstractDNA $partner): AbstractDNA
126119
{
120+
/** @var array<int, ASCIIStringGene> $genes */
127121
$genes = [];
128122

129123
for ($i = 0; $i < $this->length; $i++) {
130-
$genes[$i] = random_int(0, 1) ? $this->getGene($i) : $partner->getGene($i);
124+
/** @var ASCIIStringGene $gene */
125+
$gene = random_int(0, 1) ? $this->getGene($i) : $partner->getGene($i);
126+
$genes[$i] = $gene;
131127
}
132128

133129
return new self($this->target, $genes);
134130
}
135131

136-
/**
137-
* @return string
138-
*/
139132
public function getValue(): string
140133
{
141134
$value = '';

src/DNA/AbstractDNA.php

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,12 @@
77
use RuntimeException;
88
use Voodooism\Genetic\DNA\Gene\AbstractGene;
99

10-
/**
11-
* Class AbstractDNA
12-
*
13-
* @package Voodooism\Genetic\DNA
14-
*/
1510
abstract class AbstractDNA
1611
{
1712
/**
1813
* The fitness of this DNA.
19-
*
20-
* @var float
2114
*/
22-
protected $fitness = 0;
15+
protected float $fitness = 0;
2316

2417
/**
2518
* The value of whole DNA.
@@ -32,22 +25,18 @@ abstract class AbstractDNA
3225
/**
3326
* Array of genes this DNA.
3427
*
35-
* @var AbstractGene[]
28+
* @var array<int, AbstractGene>
3629
*/
37-
protected $genes;
30+
protected array $genes = [];
3831

3932
/**
4033
* Replicates this DNA with a different set of genes.
41-
*
42-
* @return $this
4334
*/
4435
abstract public function replicate(): self;
4536

4637
/**
4738
* Mutate every gene of this DNA with some probability, depends on given "mutation rate".
4839
* It is an optional step. Mutation is unnecessary in some cases.
49-
*
50-
* @param float $mutationRate
5140
*/
5241
abstract public function mutate(float $mutationRate): void;
5342

@@ -61,16 +50,9 @@ abstract public function evaluateFitness(): void;
6150

6251
/**
6352
* Creates a new DNA by crossing this DNA with the given one.
64-
*
65-
* @param AbstractDNA $partner
66-
*
67-
* @return $this
6853
*/
6954
abstract public function crossover(self $partner): self;
7055

71-
/**
72-
* @return float
73-
*/
7456
public function getFitness(): float
7557
{
7658
return $this->fitness;
@@ -84,11 +66,6 @@ public function getValue()
8466
return $this->value;
8567
}
8668

87-
/**
88-
* @param int $number
89-
*
90-
* @return AbstractGene
91-
*/
9269
public function getGene(int $number): AbstractGene
9370
{
9471
if (!array_key_exists($number, $this->genes)) {

src/DNA/Gene/ASCIIStringGene.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,13 @@
44

55
namespace Voodooism\Genetic\DNA\Gene;
66

7+
use RuntimeException;
78
use Exception;
89

9-
/**
10-
* Class ASCIIStringGene
11-
*
12-
* @package Voodooism\Genetic\DNA\Gene
13-
*/
1410
class ASCIIStringGene extends AbstractGene
1511
{
1612
/**
17-
* ASCIIStringGene constructor.
18-
*
19-
* @param int|null $ASCIINumber
20-
*
13+
* @throws RuntimeException
2114
* @throws Exception
2215
*/
2316
public function __construct(?int $ASCIINumber = null)
@@ -27,7 +20,7 @@ public function __construct(?int $ASCIINumber = null)
2720
}
2821

2922
if ($ASCIINumber > 126 || $ASCIINumber < 32) {
30-
throw new \RuntimeException('Wrong ASCII number! Only numbers from 32 to 126 are allowed');
23+
throw new RuntimeException('Wrong ASCII number! Only numbers from 32 to 126 are allowed');
3124
}
3225

3326
$this->value = chr($ASCIINumber);

src/DNA/Gene/AbstractGene.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,10 @@
44

55
namespace Voodooism\Genetic\DNA\Gene;
66

7-
/**
8-
* Class AbstractGene
9-
*
10-
* @package Voodooism\Genetic\DNA\Gene
11-
*/
127
abstract class AbstractGene
138
{
149
/**
15-
* The value this gene.
10+
* The value of this gene.
1611
* Can contain any value you want.
1712
*
1813
* @var mixed

src/DNA/Gene/MathGene.php

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@
66

77
use Exception;
88

9-
/**
10-
* Class MathGene
11-
*
12-
* @package Voodooism\Genetic\DNA\Gene
13-
*/
149
class MathGene extends AbstractGene
1510
{
1611
/**
@@ -20,25 +15,15 @@ class MathGene extends AbstractGene
2015

2116
/**
2217
* The lowest possible value (inclusive).
23-
*
24-
* @var float
2518
*/
26-
private $from;
19+
private float $from;
2720

2821
/**
29-
* The lowest possible value (inclusive).
30-
*
31-
* @var float
22+
* The highest possible value (inclusive).
3223
*/
33-
private $to;
24+
private float $to;
3425

3526
/**
36-
* MathGene constructor.
37-
*
38-
* @param float $from
39-
* @param float $to
40-
* @param float|null $value
41-
*
4227
* @throws Exception
4328
*/
4429
public function __construct(float $from, float $to, ?float $value = null)
@@ -48,9 +33,12 @@ public function __construct(float $from, float $to, ?float $value = null)
4833
$this->value = $value ?? $this->generate();
4934
}
5035

36+
public function getValue(): float
37+
{
38+
return $this->value;
39+
}
40+
5141
/**
52-
* @return $this
53-
*
5442
* @throws Exception
5543
*/
5644
public function mutate(): self
@@ -61,16 +49,14 @@ public function mutate(): self
6149
/**
6250
* Generates random float accordingly to "from" and "to" parameters
6351
*
64-
* @return float
65-
*
6652
* @throws Exception
6753
*/
6854
private function generate(): float
6955
{
7056
$multiplier = 10 ** self::PRECISION;
7157

72-
$intFrom = (int)$this->from * $multiplier;
73-
$intTo = (int)$this->to * $multiplier;
58+
$intFrom = (int)($this->from * $multiplier);
59+
$intTo = (int)($this->to * $multiplier);
7460

7561
return random_int($intFrom, $intTo) / $multiplier;
7662
}

src/DNA/Gene/NullGene.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
/**
88
* Gene stub
9-
*
10-
* @package Voodooism\Genetic\DNA\Gene
119
*/
1210
class NullGene extends AbstractGene
1311
{

src/DNA/Math/Equation.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@
66

77
use Voodooism\Genetic\DNA\Gene\MathGene;
88

9-
/**
10-
* Class Equation
11-
*
12-
* @package Voodooism\Genetic\DNA\Math
13-
*/
149
class Equation
1510
{
1611
/**
@@ -21,20 +16,13 @@ class Equation
2116
*/
2217
private $func;
2318

24-
/**
25-
* Equation constructor.
26-
*
27-
* @param callable $f
28-
*/
2919
public function __construct(callable $f)
3020
{
3121
$this->func = $f;
3222
}
3323

3424
/**
3525
* @param MathGene ...$genes Each of gene corresponds to a variable of equation.
36-
*
37-
* @return float
3826
*/
3927
public function __invoke(MathGene ...$genes): float
4028
{

0 commit comments

Comments
 (0)