예제 #1
0
파일: gatsp.cpp 프로젝트: aliceylan/camnest
	void Popu::mutate(int pos){
	 
	 int pos1=0,pos2=3;//size=offspring.elements.size();
	  //QTime t;
	  int rd=3;
	  Chromosome mutant;
	 
	 for (int i=0;i<2;i++){
		 if (i==0)
				mutant=parentOne;
			else 
				mutant=parentTwo;

		 for (int j=0;j<chromoSize;j++){
			 ///@todo make it dependant on a variable value Mut_prob the probability should be small
			 rd=qrand()%chromoSize;
			 if (rd >chromoSize-(chromoSize/5)) {
				 //qDebug()<<"creating a mutant";
				 pos1=qrand()%chromoSize;
				 //qsrand(QTime::msec ());
				 pos2=qrand()%chromoSize;
				 //qDebug()<<pos2<<pos1;
				 swapVals(mutant.elements,pos1,pos2);
				}		
			 //qDebug()<<mutant.elements<<"route"<< mutant.routeLength;			 
		    }
		 mutant.calcRouteLength();
		 //qDebug()<<"creating a mutant at "<<pos+i<<"routes"<< mutant.routeLength;
	///@NOTE: As we kept the 2 best parents as are (ellitisme) and i>0 at while loop end we keep the elite
		 //newGen.replace(pos+i,mutant);
		newGen.replace(pos,mutant);		 
		//qDebug()<<newGen.size();
		}
	}
예제 #2
0
파일: gatsp.cpp 프로젝트: aliceylan/camnest
	 /// todo: create a function that return the pop/chromosome size for brievty
	void Popu::mutate(int pos){
	 /// given an offspring we mutate it at a certain rate and create the new population
	 int pos1=0,pos2=3;//size=offspring.elements.size();
	  QTime t;int rd=3;
	  Chromosome mutant;
	 ///NOTE: As we kept the 2 best parents as are (ellitisme) we start from 2
	 for (int i=0;i<2;i++){
		 if (i==0)
				mutant=parentOne;
			else 
				mutant=parentTwo;

		 for (int j=0;j<chromoSize;j++){
			 rd=qrand()%chromoSize;
			 //qDebug()<<rd;
			 ///DFFIXME : make it dependant on a variable value Mut_prob the probability should be small
			 if (rd >chromoSize-(chromoSize/5)) {
				 //qDebug()<<"creating a mutant";
				 pos1=qrand()%chromoSize;
				 //qsrand(QTime::msec ());
				 pos2=qrand()%chromoSize;
				 //qDebug()<<pos2<<pos1;
				 //mutant.elements.swap(pos1,pos2);
				 swapVals(mutant.elements,pos1,pos2);
				}		
			 ///qDebug()<<mutant.elements<<"route"<< mutant.routeLength;
			 
		    }
		 mutant.setRoutelength();
		 ///qDebug()<<"creating a mutant at "<<pos+i<<"routes"<< mutant.routeLength;
		 newGen.replace(pos+i,mutant);
		//qDebug()<<newGen.size();
		}
	}
예제 #3
0
파일: gatsp.cpp 프로젝트: aliceylan/camnest
	void Popu::mutateImprove(int pos){
	  
	  Chromosome toImprove;
		 /// Time to improve the route (randomly for now) even the elite are concerned
		 for (int i=0;i<2;i++){
			 if (i==0)
				toImprove=parentOne;
			else 
				toImprove=parentTwo;
			///qDebug()<<"Trying to improve parent n°"<<pos+i;
			///FOR debug remove me!
			 int orignalRouteLength=toImprove.routeLength;
				for (int j=0;j<chromoSize*2;j++){
				 int pos1=qrand()%chromoSize;
				 int pos2=qrand()%chromoSize;
				 int oldRouteLength=toImprove.routeLength;
				 //toImprove.elements.swap(pos1,pos2);
				 swapVals(toImprove.elements,pos1,pos2);
				 toImprove.calcRouteLength();
				// qDebug()<<toImprove.routeLength<<oldRouteLength;
				 ///undo changes as this become worse than before
				 if ( toImprove.routeLength>oldRouteLength){
					 //toImprove.elements.swap(pos1,pos2);
					 swapVals(toImprove.elements,pos1,pos2);
					 toImprove.calcRouteLength();/// could be simplier =oldRouteLength ?
					 //qDebug()<<"no changes";
					}
					//else{
					 ///keep changes
					//}
				}
				newGen.replace(i+pos,toImprove);
				//newGen.replace(pos,toImprove);
				//qDebug()<<"Improved*********"<<toImprove.routeLength<<orignalRouteLength;
				//qDebug()<<toImprove.elements;
				 //qDebug()<<"improved mutant routes"<< toImprove.routeLength;
			 	
			}
	 }
예제 #4
0
파일: gatsp.cpp 프로젝트: aliceylan/camnest
         void Popu::mutateImproveMe(int pos){
		Chromosome toImprove;
                //we mutate beginig from an elite parent
                if (qrand()%3==0) {
                toImprove=content[0];
                nbrElite1++;
                }
              // We mutate a random chromosome
                else if (qrand()%3==1){
                toImprove=content.at(pos);
                nbimporte++;
            }
                else {
                toImprove=content[1];
                nbrElite2++;
                }

		int orignalRouteLength=toImprove.routeLength;
		int pos1=1;
		int pos2=2;
		int oldRouteLength=orignalRouteLength;
		for (int j=0;j<chromoSize;j++){
			pos1=qrand()%chromoSize;
			pos2=qrand()%chromoSize;
			while (pos2==pos1){
			pos2=qrand()%chromoSize;
			}
			oldRouteLength=toImprove.routeLength;
			swapVals(toImprove.elements,pos1,pos2);
			toImprove.calcRouteLength();
			
                        if ( toImprove.routeLength>oldRouteLength){
                                swapVals(toImprove.elements,pos1,pos2);
                                toImprove.calcRouteLength();/// could be simplier =oldRouteLength ?
                        }
		}
	 newGen.replace(pos,toImprove);
	 
	 }
예제 #5
0
파일: gatsp.cpp 프로젝트: aliceylan/camnest
	void Popu::mutateImproveOffspring(Chromosome &toImprove, int pos){
		
                
		int orignalRouteLength=toImprove.routeLength;
		int pos1=1;
		int pos2=2;
		int oldRouteLength=orignalRouteLength;
		for (int j=0;j<chromoSize;j++){
			pos1=qrand()%chromoSize;
			pos2=qrand()%chromoSize;
			while (pos2==pos1){
			pos2=qrand()%chromoSize;
			}
			oldRouteLength=toImprove.routeLength;
			swapVals(toImprove.elements,pos1,pos2);
			toImprove.calcRouteLength();
			
                         if ( toImprove.routeLength>oldRouteLength){
                                 swapVals(toImprove.elements,pos1,pos2);
                                 toImprove.calcRouteLength();// could be simplier =oldRouteLength ?
                         }
		}
	 newGen.replace(pos,toImprove);	 
	 }
예제 #6
0
int partition(int a[], int left, int right) {

	int ind1 = left;
	int ind2 = (right - 1);
	int pivot = (left + right) / 2;

	//Orients the left, right, and pivot elements
	if (a[left] >= a[pivot] && a[left] >= a[right]) 
		swapVals(a, left, right);
	else if (a[pivot] >= a[left] && a[pivot] >= a[right]) 
		swapVals(a, pivot, right);
	if (a[left] >= a[pivot]) 
		swapVals(a, left, pivot);
		
	//Double checks that left <= pivot <= right
	assert(a[0] <= a[right]);
	assert(a[pivot] <= a[right]);
	
	//If the array only has 3 elements, then return
	if (right - left <= 2) 
		return pivot;

	//Otherwise, it puts the pivot element at the end and changes pivot
	//to pivot-1, since the pivot point has moved out of the way
	swapVals(a, pivot, (right - 1));
	pivot = right - 1;
	
	do {
		//Make sure that a at ind2 is <= a at ind1, then swaps them
		if (a[ind1] >= a[pivot] && a[ind2] <= a[pivot]) {
			assert(a[ind2] <= a[ind1]);
			swapVals(a,ind1,ind2);
		}

		if (a[ind1] <= a[pivot])
			ind1++;
		if (a[ind2] >= a[pivot])
			ind2--;
	} while (ind1 <= ind2);
	
	swapVals(a,ind1,pivot);
	
	int temp2 = pivot;
	pivot = ind1;
	ind1 = temp2;
	
	//Makes sure that pivot is in the right spot
	assert(a[pivot] >= a[pivot-1] && a[pivot] <= a[pivot+1]);
	
	return pivot;
}
예제 #7
0
파일: gatsp.cpp 프로젝트: aliceylan/camnest
	/// todo: create a function that return the pop/chromosome size for brievty
	void Population::mutate(Chromosome *offspring){
	 /// given an offspring we mutate it at a certain rate and create the new population
	 int i=0,pos1=0,pos2=3,size=offspring->elements.size();content.clear();
	 
	 for (i=0;i<20;i++){
		 mutated->elements=offspring->elements;
		 if (i%3==0) {
		 pos1=qrand()%size;
		 pos2=qrand()%size;
		 //qDebug()<<"swapping values at"<<pos1 <<"and"<<pos2 ;
		 //mutated->elements.swap(pos1,pos2);
		  swapVals(mutated->elements,pos1,pos2);
		 }
		  ///qDebug()<<mutated->elements;
		  
		 content.append(mutated);
		}
		qDebug()<<content;
	}