init.php代码中有3个编译错误:
未定义的变量 $ind
未定义的变量 $popsize
未定义变量 $chrom
如何以正确的方式解决这个问题?
主文件
include_once 'init.php';
class Individual {
public $genes = array();
//...
}
class Population {
public $ind = array();
public $ind_ptr;
public function setIndPtr(Individual $ind) {
$this->ind_ptr = $ind;
}
}
$popsize = 10;
$chrom = 5;
$pop = new Population();
$pop_ptr = new Population();
$pop = init(pop_ptr);
初始化文件
function init(Population $pop_ptr) {
$pop_ptr->setIndPtr($ind[0]);
for ($i = 0 ; $i < $popsize ; $i++) {
for ($j = 0; $j < $chrom; $j++) {
$d = rand(0,1);
if($d >= 0.5) {
$pop_ptr->ind_ptr->genes[$j] = 1;
}
else {
$pop_ptr->ind_ptr->genes[$j] = 0;
}
}
$pop_ptr->setIndPtr($ind[$i+1]);
}
$pop_ptr->setIndPtr($ind[0]);
return $pop_ptr;
}