|
|
|
ขอสอบถามเรื่่อง genetic algorithm เกี่ยวกับเรื่องการจัดตารางเรียนหน่อยค่ะ |
|
|
|
|
|
|
|
หนูจะทำเรื่องเกี่ยวกับการจัดตารางเรียนค่ะ แล้วใช้วิธี genetic algorithm ค่ะ คือหนูต้องการให้เลือกแค่ผลลัพธ์อันที่ดีที่สุดค่ะ แต่ผลลัพธ์ที่ได้ข้อมูล เหมือนมีการบวกลบกันค่ะ หนูเขียนโค้ดตรงไหนผิดหรือเปล่าค่ะ ช่วยแนะนำหน่อยค่ะ
ข้อมูลที่ นำเข้า
Class group : 1
subject ID : 320
teacher name : 1
labroom Name :6601
Major Name : 1
Major year : 1
Duration : 3
Class group : 2
subject ID : 321
teacher name : 1
labroom Name :6602
Major Name : 2
Major year : 1
Duration : 3
ผลลัพธ์
Code
[class_group] => 0
[timeslot_no] => 1
[subject_id] => 321
[teacher_name] => 2
[major_year] => 6602
[major_name] => 2
[labroom_name] => 1
[duration] => 3
หน้าที่ส่งข้อมูล
Code (PHP)
<form name="form1" method="post" action="setsubj.php">
<table>
<tr><td colspan="2" align="center"><b>Clss Schedule</b></td></tr>
<tr><td colspan="2"></td></tr>
<tr><td>year :</td><td><input type="text" name="year" value="" /></td></tr>
<tr><td>semester :</td><td><input type="text" name="semester" value="" /></td></tr>
</table>
<br/>
<br/>
<table>
<tr><td>Class group :</td><td><input type="text" name="class_group" value="" /></td></tr>
<tr><td>subject ID :</td><td><input type="text" name="subject_id" value="" /></td></tr>
<tr><td>teacher name :</td><td><input type="text" name="teacher_name" value="" /></td></tr>
<tr><td>labroom Name :</td><td><input type="text" name="labroom_name" value="" /></td></tr>
<tr><td>Major Name :</td><td><input type="text" name="major_name" value="" /></td></tr>
<tr><td>Major year :</td><td><input type="text" name="major_year" value="" /></td></tr>
<tr><td>Duration :</td><td><input type="text" name="Duration" value="" /></td></tr>
</table>
<br/>
<br/>
<table>
<tr><td>Class group :</td><td><input type="text" name="class_group1" value="" /></td></tr>
<tr><td>subject ID :</td><td><input type="text" name="subject_id1" value="" /></td></tr>
<tr><td>teacher name :</td><td><input type="text" name="teacher_name1" value="" /></td></tr>
<tr><td>labroom Name :</td><td><input type="text" name="labroom_name1" value="" /></td></tr>
<tr><td>Major Name :</td><td><input type="text" name="major_name1" value="" /></td></tr>
<tr><td>Major year :</td><td><input type="text" name="major_year1" value="" /></td></tr>
<tr><td>Duration :</td><td><input type="text" name="duration1" value="" /></td></tr>
<tr><td colspan="3" align="center"><input type="submit" name="submit" value="OK" /></td></tr>
<tr><td></td></tr>
</table>
</form>
หน้าที่รับข้อมูล
<?php
require_once('algo.php');
class subj {
var $class_group;
var $timeslot_no;
var $subject_id;
var $teacher_name;
var $major_year;
var $major_name;
var $labroom_name;
var $duration;
function subj ($class_group=0,$timeslot_no=0,$subject_id=0,$teacher_name=0,$major_year=0,$major_name=0,$labroom_name=0,$duration=0) {
$this->class_group = $class_group;
$this->timeslot_no = $timeslot_no;
$this->subject_id = $subject_id;
$this->teacher_name = $teacher_name;
$this->major_year = $major_year;
$this->major_name = $major_name;
$this->labroom_name = $labroom_name;
$this->duration= $duration;
}
}
$year = $_POST['year'];
$semester = $_POST['semester'];
$class_group = $_POST['$class_group'];
$subject_id = $_POST['subject_id'];
$teacher_name = $_POST['teacher_name'];
$labroom_name = $_POST['labroom_name'];
$major_name = $_POST['major_name'];
$major_year = $_POST['major_year'];
$duration = $_POST['duration'];
$class_group1 =$_POST['$class_group1'];
$subject_id1 = $_POST['subject_id1'];
$teacher_name1 = $_POST['teacher_name1'];
$labroom_name1 = $_POST['labroom_name1'];
$major_name1 = $_POST['major_name1'];
$major_year1 = $_POST['major_year1'];
$duration1 = $_POST['duration1'];
function inc($x) {
return $x+1;
}
function avg($a,$b) {
return round(($a+$b)/2);
}
function total($obj) {
if ($obj1['timeslot_no'] == $obj2['timeslot_no']) {
$w=500;
if($obj1['major_name'] == $obj2['major_name'] and $obj1['major_year'] == $obj2['major_year']){
$w = $w+500;
return $w;
} else if($obj1['teacher_name']== $obj2['teacher_name']){
$w = $w+500;
return $w;
} else if($obj1['labroom_name'] == $obj2['labroom_name']){
$w = $w+500;
return $obj;
} else {
$w=0;
return $w;
}
} else {
$w=0;
return $w;
}
}
$chro1 = new subj($class_group,$timeslot_no=1,$subject_id,$teacher_name,$labroom_name,$major_name,$major_year,$duration);
$chro2 = new subj($class_group1,$timeslot_no=1,$subject_id1,$teacher_name1,$labroom_name1,$major_name1,$major_year1,$duration1);
$algo = new algo();
$algo->population = array($chro1,$chro2);
$algo->fitness_function = 'total';
$algo->num_couples = 1;
$algo->death_rate = 0;
$algo->generations = 100;
$algo->crossover_functions = 'avg';
$algo->mutation_function = 'inc';
$algo->mutation_rate = 10;
$algo->evolve();
debug($algo->population);
debug(algo::select($algo->population,'total',1));
หน้าของ algorithm
class algo {
var $population;
var $fitness_function;
var $crossover_functions;
var $mutation_function;
var $mutation_rate;
var $generations;
var $num_couples;
var $death_rate;
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);
}
function best($a,$b) {
$max=$a;
if ($b[1] > $a[1]){
$max=$b;
return $max;}
else{
return $max;}
}
function select($objects,$fitness_function,$n=2) {
foreach ($objects as $object) {
$selection[] = array($object,$fitness_function($object));
}
usort($selection,array("algo", "best"));
$selection = array_slice($selection,0,$n);
foreach ($selection as $selected) {
$winners[] = $selected[0];
}
return $winners;
}
function worst($a, $b) {
$min=$a;
if ($b[1] < $a[1]) {
$max=$b;
return $min;}
else{
return $min;}
}
function kill(&$objects,$fitness_function,$n=2) {
foreach ($objects as $object) {
$selection[] = array($object,$fitness_function($object));
}
usort($selection,array("algo", "worst"));
$selection = array_slice($selection,0,count($selection)-$n);
$objects = array();
foreach ($selection as $selected) {
$objects[] = $selected[0];
}
}
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;
}
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));
}
}
}
Tag : PHP, MySQL, CakePHP
|
|
|
|
|
|
Date :
2012-12-01 23:58:40 |
By :
nok |
View :
1491 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code เยอะไล่ดูยากน่ะครับ
|
|
|
|
|
Date :
2012-12-02 08:21:44 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 04
|