$found = true;
         break;
     }
 }
 // create a new population by selecting two parents at a time and creating offspring
 // by applying crossover and mutation. Do this until the desired number of offspring
 // have been created.
 $temppop = array();
 $cpop = 0;
 while ($cpop < $POP_SIZE) {
     // we are going to create the new population by grabbing members of the old population
     // two at a time via roulette wheel selection.
     $offspring1 = roulette($totalfitness, $population, $fitness);
     $offspring2 = roulette($totalfitness, $population, $fitness);
     // and crossover dependent on the crossover rate
     $crossresult = crossover($offspring1, $offspring2);
     if (isset($crossresult[0])) {
         $offspring1 = $crossresult[0];
     }
     if (isset($crossresult[1])) {
         $offspring2 = $crossresult[1];
     }
     // now mutate dependen on the mutation rate
     $offspring1 = mutate($offspring1);
     $offspring2 = mutate($offspring2);
     // add the offsprings in the population with fitness at 0
     $temppop[$cpop++] = $offspring1;
     $temppop[$cpop++] = $offspring2;
 }
 // Reset fitness
 $fitness = array();
mutate($ms[2], $ms[3]);
$sql = "SELECT * FROM sorted order by RAND() ";
$sql = mysql_query($sql, $db) or die(mysql_error($db));
$t = 0;
while ($info = mysql_fetch_array($sql)) {
    $cos[$t] = $info['id'];
    $t++;
}
crossover($cos[0], $cos[1]);
crossover($cos[2], $cos[3]);
crossover($cos[4], $cos[5]);
crossover($cos[6], $cos[7]);
crossover($cos[8], $cos[9]);
crossover($cos[10], $cos[11]);
crossover($cos[12], $cos[13]);
crossover($cos[14], $cos[15]);
$sql = "SELECT * FROM weights where id <> 'USERFIRST'  order by fn DESC";
$sql = mysql_query($sql, $db) or die(mysql_error($db));
$t = 0;
while (($info = mysql_fetch_array($sql)) && $t < 1) {
    $fw0[$t] = $info['w0'];
    $fw1[$t] = $info['w1'];
    $fw2[$t] = $info['w2'];
    $fw3[$t] = $info['w3'];
    $fw4[$t] = $info['w4'];
    $fw5[$t] = $info['w5'];
    $fw6[$t] = $info['w6'];
    $fw7[$t] = $info['w7'];
    $fw8[$t] = $info['w8'];
    $fw9[$t] = $info['w9'];
    $fw10[$t] = $info['w10'];
示例#3
0
function order_crossover($array)
{
    #echo "<h2>Crossover</h2>";
    $array_offspring = array();
    for ($i = 0; $i < count($array); $i++) {
        $array_parent = array();
        if ($i != count($array) - 1) {
            $array_parent[] = $array[$i];
            $array_parent[] = $array[$i + 1];
        } else {
            $array_parent[] = $array[$i];
            $array_parent[] = $array[0];
        }
        $array_hasil_co = crossover($array_parent);
        foreach ($array_hasil_co as $key => $value) {
            $array_offspring[] = $value;
        }
    }
    return $array_offspring;
}