Ga_time_table/classes/hospital.php
/*
	|--------------------------------------------------------------------------
	| Mutation function
	|--------------------------------------------------------------------------
	|	The mutation function takes two random classes and swaps them
	*/
|
| Mutation function
|
|       The mutation function takes two random classes and swaps them
| | Mutation function | | The mutation function takes two random classes and swaps them
public function mutation(){
		$rnds = $this->randClass();		
		$cls = array(0,0);
		do{	
				$cls[0] = array_rand($this->_sched[$rnds[0]],1);		
				$cls[1] = array_rand($this->_sched[$rnds[1]],1);		
				if($cls[0]!=$cls[1]){		
					if($this->_sched[$rnds[0]][$cls[0]]->getDuration() == $this->_sched[$rnds[1]][$cls[1]]->getDuration()){	
						break;
					}
				}
		}while(1); 
		$dur = $this->_sched[$rnds[0]][$cls[0]]->getDuration();
		$new_time = array(0,0);
		$sel_cls_time = array();
		for($i=0;$i<2;$i++){
			$time = 0;
			do{
				$time = rand()%60;
				if(!is_object($this->_classes[$rnds[$i]][$time])){
					break;
				}
			}while(is_object($this->_classes[$rnds[$i]][$time]));
			$new_time[$i] = $time;
		}
		for($i=0; $i<2; $i++){
			$sel_cls_time[] = $this->_classes[$rnds[$i]][$cls[$i]];
		}
		for($i=0;$i<2;$i++){
			$buff = $this->_classes[$rnds[$i]];
			$test = array_fill(0,$dur,$sel_cls_time[abs($i-1)]);
			array_splice($buff,$new_time[$i],0,$test);
			$this->_classes[$rnds[$i]] = $buff;	
		}
		for($i=0;$i<2;$i++){	
			$sel_cls_time[$i] = $this->fill_arr($cls[$i],$dur,0);
			array_replace($this->_classes[$rnds[$i]],$sel_cls_time[$i]);
		}
		$this->_sched = $this->map_class_to_hours($this->_classes);	
	}
public function mutation ( ) { $ rnds = $ this -> randClass ( ) ; $ cls = array ( 0 , 0 ) ; do { $ cls [ 0 ] = array_rand ( $ this -> _sched [ $ rnds [ 0 ] ] , 1 ) ; $ cls [ 1 ] = array_rand ( $ this -> _sched [ $ rnds [ 1 ] ] , 1 ) ; if ( $ cls [ 0 ] != $ cls [ 1 ] ) { if ( $ this -> _sched [ $ rnds [ 0 ] ] [ $ cls [ 0 ] ] -> getDuration ( ) == $ this -> _sched [ $ rnds [ 1 ] ] [ $ cls [ 1 ] ] -> getDuration ( ) ) { break ; } } } while ( 1 ) ; $ dur = $ this -> _sched [ $ rnds [ 0 ] ] [ $ cls [ 0 ] ] -> getDuration ( ) ; $ new_time = array ( 0 , 0 ) ; $ sel_cls_time = array ( ) ; for ( $ i = 0 ; $ i < 2 ; $ i ++ ) { $ time = 0 ; do { $ time = rand ( ) % 60 ; if ( ! is_object ( $ this -> _classes [ $ rnds [ $ i ] ] [ $ time ] ) ) { break ; } } while ( is_object ( $ this -> _classes [ $ rnds [ $ i ] ] [ $ time ] ) ) ; $ new_time [ $ i ] = $ time ; } for ( $ i = 0 ; $ i < 2 ; $ i ++ ) { $ sel_cls_time [ ] = $ this -> _classes [ $ rnds [ $ i ] ] [ $ cls [ $ i ] ] ; } for ( $ i = 0 ; $ i < 2 ; $ i ++ ) { $ buff = $ this -> _classes [ $ rnds [ $ i ] ] ; $ test = array_fill ( 0 , $ dur , $ sel_cls_time [ abs ( $ i - 1 ) ] ) ; array_splice ( $ buff , $ new_time [ $ i ] , 0 , $ test ) ; $ this -> _classes [ $ rnds [ $ i ] ] = $ buff ; } for ( $ i = 0 ; $ i < 2 ; $ i ++ ) { $ sel_cls_time [ $ i ] = $ this -> fill_arr ( $ cls [ $ i ] , $ dur , 0 ) ; array_replace ( $ this -> _classes [ $ rnds [ $ i ] ] , $ sel_cls_time [ $ i ] ) ; } $ this -> _sched = $ this -> map_class_to_hours ( $ this -> _classes ) ; }
|
| Mutation function
|
|       The mutation function takes two random classes and swaps them
| | Mutation function | | The mutation function takes two random classes and swaps them
//Generate two random classrooms //while the duration of both classes are not equal			 //Generate a random hour within classroom //Generate a random hour within classroom //if both hours are not thesame //and the duration are thesame					 //create an infinite loop and only break when conditions are met //This little hack inverts 1 to 0 and 0 to 1		 //Remove the content of moved class