Skip to content

Commit 0911943

Browse files
author
ali
committed
Merge branch 'rziw'
# Conflicts: # GeneticAlgorithm.php
2 parents 1c38c04 + 67a60e1 commit 0911943

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

.idea/~$vcs.xml

162 Bytes
Binary file not shown.

GeneticAlgorithm.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ class GeneticAlgorithm
2020
public $fitness = array();
2121
public $probability = array();
2222
public $cumulative_probability = array();
23+
public $parents = array();
2324

25+
public $crossover_rate = 0.25;
26+
public $mutation_rate = 0.1;
27+
public $population = 200;
28+
public $iteration = 0;
2429
public $total_fitness = 0;
2530

2631
public function __construct()
@@ -76,4 +81,32 @@ public function selection(){
7681
}
7782
$this->chromosome = $new_chromosome;
7883
}
84+
85+
public function crossOver() {
86+
while($this->iteration < $this->population) {
87+
for($m=0; $m<4 ; $m++) {
88+
$random[$m]=mt_rand() / mt_getrandmax();
89+
if($random[$m] < $this->crossover_rate) {
90+
// $this->parents[$m] = $this->chromosome[$m];
91+
$ids = $m;
92+
}
93+
}
94+
$cuts =
95+
$this->iteration += 1;
96+
}
97+
}
98+
99+
public function mutation(){
100+
101+
$mutation_count = round((3 * 5) * $this->mutation_rate, 0, PHP_ROUND_HALF_DOWN);
102+
103+
for ($i=0;$i<$mutation_count;$i++){
104+
$position = rand(1,(3 * 5));
105+
$chosen_chromosome = round($position / 3 , 0 , PHP_ROUND_HALF_UP) - 1;
106+
$chosen_gen = (3 * 5) - ($chosen_chromosome * 3);
107+
108+
$this->chromosome[$chosen_chromosome][$chosen_gen] = rand(0,10);
109+
}
110+
}
111+
79112
}

0 commit comments

Comments
 (0)