public function run()
 {
     if ($this->generation === null) {
         $this->generation = new Generation($this->chromosome);
     }
     $this->generation->init_population($this->max_population, $this->goal);
     // first generation
     $this->generation->fitness_function($this->goal);
     $this->solution = $this->generation->best_chromosome->get_data();
     Hook::call(self::HOOK_REGENERATION, array('object' => $this));
     while (intval($this->generation->__toString()) < $this->max_generation) {
         // for some reason we have to work on this, Notice: Indirect modification of overloaded property
         //$this->dependency['generation_history'][] = $this->generation->get_population();
         $this->generation->selection($this->selection);
         $this->generation->crossover($this->max_population, $this->goal);
         // regeneration
         $this->generation->mutation($this->mutation, $this->goal);
         $this->generation->fitness_function($this->goal);
         $this->solution = $this->generation->best_chromosome->get_data();
         Hook::call(self::HOOK_REGENERATION, array('object' => $this));
         if ($this->solution == $this->goal) {
             Hook::call(self::HOOK_FINISH_GOAL, array('object' => $this));
             return true;
         }
     }
     Hook::call(self::HOOK_FINISH_NOGOAL, array('object' => $this));
     return false;
 }
示例#2
0
文件: example5.php 项目: ryanhs/hook
 public function start()
 {
     Hook::call('process');
     // some logic here
     // some logic here
     // some logic here
     Hook::call('finish');
 }
 public function __construct($chromosome_class)
 {
     $this->chromosome_class = $chromosome_class;
     $this->best_chromosome = null;
     $this->population = array();
     $this->population_fitness = array();
     $this->population_history = array();
     Hook::call(self::HOOK_INIT, array('object' => $this));
 }
示例#4
0
文件: Test.php 项目: ryanhs/hook
 public function test_pass_param()
 {
     Hook::clear();
     $return = null;
     Hook::on('param_test', function ($params) use(&$return) {
         $return = $params['return'];
     });
     Hook::call('param_test', array('return' => 'test param'));
     $this->assertEquals('test param', $return);
 }
示例#5
0
文件: example3.php 项目: ryanhs/hook
<?php

require 'vendor/autoload.php';
use Ryanhs\Hook\Hook;
Hook::on('test', function () {
    return 1;
});
Hook::on('test', function () {
    return '3';
});
Hook::on('test', function () {
    return 5;
});
echo 'call_yield:' . PHP_EOL . PHP_EOL;
foreach (Hook::call_yield('test') as $return) {
    echo $return . PHP_EOL;
}
echo PHP_EOL . str_repeat('=', 10) . PHP_EOL;
echo 'call: (return as array)' . PHP_EOL . PHP_EOL;
var_dump(Hook::call('test'));
<?php

require 'vendor/autoload.php';
use Ryanhs\Hook\Hook;
use Ryanhs\GAToolkit\Toolkit;
use Ryanhs\GAToolkit\Chromosome\SimpleString;
$ga = new Toolkit();
$ga->goal = isset($argv['1']) ? $argv['1'] : 'test';
//$ga->chromosome = '\Ryanhs\GAToolkit\Chromosome\SimpleStringOptimized';
$ga->chromosome = '\\Ryanhs\\GAToolkit\\Chromosome\\SimpleStringStd';
$ga->selection = 0.9;
$ga->mutation = 0.1;
//$ga->max_generation = 20;
$ga->max_population = 20;
Hook::on(Toolkit::HOOK_REGENERATION, function ($params) {
    $ga = $params['object'];
    echo 'Generation #' . $ga->generation . ' -> ' . $ga->solution . PHP_EOL;
    time_nanosleep(0, 50000000);
});
Hook::on(Toolkit::HOOK_FINISH_GOAL, function ($params) {
    $ga = $params['object'];
    echo 'Solution get on generation #' . $ga->generation . ' -> ' . $ga->solution . PHP_EOL;
});
Hook::on(Toolkit::HOOK_FINISH_NOGOAL, function ($params) {
    $ga = $params['object'];
    echo 'No Solution! reach max generation #' . $ga->generation . PHP_EOL;
});
$ga->run();
示例#7
0
文件: example2.php 项目: ryanhs/hook
<?php

require 'vendor/autoload.php';
use Ryanhs\Hook\Hook;
Hook::on('test', function () {
    echo 'hook 1!' . PHP_EOL;
});
Hook::on('test', function () {
    echo 'hook 2!' . PHP_EOL;
});
Hook::call('test');
示例#8
0
 public function __unset($var)
 {
     unset($this->dependencies[$var]);
     Hook::call(self::HOOK_UNSET . $var);
 }
示例#9
0
文件: example1.php 项目: ryanhs/hook
<?php

require 'vendor/autoload.php';
use Ryanhs\Hook\Hook;
Hook::on('init', function () {
    echo 'hello world!' . PHP_EOL;
});
Hook::call('init');
示例#10
0
文件: example4.php 项目: ryanhs/hook
<?php

require 'vendor/autoload.php';
use Ryanhs\Hook\Hook;
Hook::on('foo', function () {
    return 1;
});
Hook::on('bar', function () {
    return '3';
});
Hook::on('foo', function () {
    return 5;
});
// clear hook "bar"
Hook::clear('bar');
echo PHP_EOL . 'hook "bar":' . PHP_EOL;
var_dump(Hook::call('bar'));
echo PHP_EOL . 'hook "foo":' . PHP_EOL;
var_dump(Hook::call('foo'));