0

我正在实施遗传算法。我从这里上课,但我不明白如何为这些课程提供输入。我有动态输入。我有三个需要优化的输入。如果有人知道,请给我提供指导。

这是代码

<?php

class GA {

    var $population;            //Objects array (same classes)
    var $fitness_function;      //The fitness function name (string)
    var $crossover_functions;   //The crossover function name (string) or array
    var $mutation_function;     //The mutation function name (string)
    var $mutation_rate;         //Mutation rate per child (%)
    var $generations;           //Number of generations
    var $num_couples;           //Number of couples for each generation
    var $death_rate;            //Number of killed objects for each generation


    function crossover($parent1,$parent2,$cross_functions)
     {
        $class = get_class($parent1);
        if ($class != get_class($parent2)) 
        return false;
        if (!is_array($cross_functions)) {
            $cross_function = $cross_functions;
            $cross_functions = array();
        }
        $child = new $class();
        $properties = get_object_vars($parent1);
        foreach ($properties as $propertie => $value)
         {
            if ($cross_function) $cross_functions[$propertie] = $cross_function;

            if (function_exists($cross_functions[$propertie]))
                $child->$propertie = $cross_functions[$propertie]($parent1->$propertie,$parent2->$propertie);
        }
        return $child;
    }

    function mutate(&$object,$mutation_function) {
        $properties = get_object_vars($object);
        foreach ($properties as $propertie => $value) {
                $object->$propertie = $mutation_function($object->$propertie);
        }
    }

    function fitness($object,$fitness_function) {
        return $fitness_function($object);
    }

    //PRIVATE
    function best($a, $b) {   
        if ($a[1] == $b[1]) return 0;
        return ($a[1] < $b[1]) ? 1 : -1;
    }


    function select($objects,$fitness_function,$n=2) {
        foreach ($objects as $object) {
            $selection[] = array($object,$fitness_function($object));
        }
        usort($selection,array("GA", "best"));
        $selection = array_slice($selection,0,$n);
        foreach ($selection as $selected) {
            $winners[] = $selected[0];
        }
        return $winners;
    }

    //PRIVATE
    function worst($a, $b) {   
        if ($a[1] == $b[1]) return 0;
        return ($a[1] < $b[1]) ? -1 : 1;
    }

    function kill(&$objects,$fitness_function,$n=2) {
        foreach ($objects as $object) {
            $selection[] = array($object,$fitness_function($object));
        }
        usort($selection,array("GA", "worst"));
        $selection = array_slice($selection,0,count($selection)-$n);
        $objects = array();
        foreach ($selection as $selected) {
            $objects[] = $selected[0];
        }
    }

    //PRIVATE
    function mass_crossover($objects,$cross_functions) {
        foreach ($objects as $object) {
            if (!$obj1) $obj1 = $object;
            else {
                $children[] = $this->crossover($obj1,$object,$this->crossover_functions);
                $obj1 = null;
            }
        }
        return $children;
    }

    //PRIVATE
    function mass_mutation(&$objects) {
        foreach($objects as $key => $object) {
            if (rand(1,100) <= $this->mutation_rate) $this->mutate($objects[$key],$this->mutation_function);
        }
    }

    function evolve() {
        for ($i=0;$i<$this->generations;$i++) {
            $couples = $this->select($this->population,$this->fitness_function,2*min($this->num_couples,floor(count($this->population)/2)));
            $children = $this->mass_crossover($couples,$this->crossover_functions);
            $this->mass_mutation($children);
            $this->population = array_merge($this->population,$children);
            $this->kill($this->population,$this->fitness_function,min($this->death_rate,count($this->population)-2));
        }
    }
}
?>

这里 gaexample2.php

<?php
require_once('ga.php');
class Human {
    var $strength;
    var $dexterity;
    var $resistance;
    var $intelligence;

    function Human($strength=0,$dexterity=0,$resistance=0,$intelligence=0)
     {
        $this->strength = $strength;
        $this->dexterity = $dexterity;
        $this->resistance = $resistance;
        $this->intelligence = $intelligence;
    }
}

function debug($x) {
    echo "<pre style='border: 1px solid black'>";
    print_r($x);
    echo '</pre>';
}

//This will be the mutation function. Just increments the property.
function inc($x) {
    return $x+1;
}
//This will be the crossover function. Is just the average of all properties.
function avg($a,$b) {
    return round(($a+$b)/2);
}
//This will be the fitness function. Is just the sum of all properties.
function total($obj) {
    return $obj->strength + $obj->dexterity + $obj->resistance + $obj->intelligence;
}

$adam = new Human(4,2,3,1);
$eve = new Human(1,4,2,3);
$ga = new GA();
$ga->population = array($adam,$eve);
debug($ga->population);
$ga->fitness_function = 'total';    //Uses the 'total' function as fitness function
$ga->num_couples = 1;               //4 couples per generation (when possible)
$ga->death_rate = 0;                //No kills per generation
$ga->generations = 100;             //Executes 100 generations
$ga->crossover_functions = 'avg';   //Uses the 'avg' function as crossover function
$ga->mutation_function = 'inc';     //Uses the 'inc' function as mutation function
$ga->mutation_rate = 10;            //10% mutation rate
$ga->evolve();                      //Run
debug($ga->population);
debug(GA::select($ga->population,'total',1)); //The best
?>

有人可以帮助显示每个步骤的单独输出吗

4

1 回答 1

0

向类发送值的方法是使用类的构造函数,您可以在其中检索参数并将其分配给类变量。

在此处阅读有关构造函数的更多信息。

它会是这样的:

class GA {

    var $population;            //Objects array (same classes)
    var $fitness_function;      //The fitness function name (string)
    var $crossover_functions;   //The crossover function name (string) or array
    var $mutation_function;     //The mutation function name (string)
    var $mutation_rate;         //Mutation rate per child (%)
    var $generations;           //Number of generations
    var $num_couples;           //Number of couples for each generation
    var $death_rate;            //Number of killed objects for each generation

    //$params would be the array of params to receive
    function __construct($params) {
        $this->population = $params['population'];
        $this->fitness_function = $params['fitness_function'];
        ...
    }

    ...

}
于 2013-05-29T14:51:57.417 回答