Skip to content

Commit 0146050

Browse files
author
ali
committed
cumulative probability
1 parent 8942c12 commit 0146050

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

GeneticAlgorithm.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ class GeneticAlgorithm
1818
public $chromosome = array();
1919
public $fx = array();
2020
public $fitness = array();
21+
public $probability = array();
22+
public $cumulative_probability = array();
23+
2124
public $total_fitness = 0;
2225

2326
public function __construct()
@@ -28,8 +31,9 @@ public function __construct()
2831
$this->calcFx();
2932

3033
$this->calcFitness();
34+
$this->calcProbability();
3135

32-
var_dump($this->fitness);
36+
var_dump($this->cumulative_probability);
3337
}
3438

3539
public function calcFx(){
@@ -43,4 +47,13 @@ public function calcFitness(){
4347
$this->total_fitness += $this->fitness[$i];
4448
}
4549
}
50+
51+
public function calcProbability(){
52+
$sum = 0;
53+
for ($i=0;$i<5;$i++) {
54+
$this->probability[$i] = $this->fitness[$i] / $this->total_fitness;
55+
$sum += $this->probability[$i];
56+
$this->cumulative_probability[$i] = $sum;
57+
}
58+
}
4659
}

0 commit comments

Comments
 (0)