QPFWPVector Popu::init(QPFWPVector points,bool randomtartupPop){ int i=0; chromoSize=points.size(); qDebug()<<"Creating initial Rpop"; ///create a population of @see popSize chromosome. The chromosome elements are taken randomly from parent @see points while (i<popSize){ Chromosome member; member.setElements(points,randomtartupPop); content.append(member); //fixme: need to be computed only once totalRoute=member.routeLength; i++; } qDebug()<<"Created an initial Random pop with route:"<<totalRoute; sortContent(); /// copy the created population into newGen newGen=content; /// Keep creating new genrations until @see Max_iter for (curGen=0;curGen<Max_iter;curGen++){ createNewGen(); } return content[0].elements; }
QPFWPVector Popu::init(QPFWPVector points,bool randomtartupPop){ Chromosome initialRoute; initialRoute.setElements(points,false); qDebug()<<"Without optimizationthe route length is"<<initialRoute.routeLength; int i=0; chromoSize=points.size(); // qDebug()<<"Creating initial Rpop"; //create a population of @see popSize chromosome. The chromosome elements are taken randomly from parent @see points while (i<popSize){ Chromosome member; member.setElements(points,randomtartupPop); content.append(member); //fixme: need to be computed only once totalRoute=member.routeLength; i++; } sortContent(); // copy the created population into newGen newGen=content; int sameVal=0; double lastRoute=totalRoute; // Keep creating new genrations until @see Max_iter or we stay in a local optimum fo n times for (curGen=0;curGen<Max_iter && sameVal< 3;curGen++){ createNewGen(); if (lastRoute==content[0].getRouteLength()){ sameVal++; } else{ lastRoute=content[0].getRouteLength(); sameVal=0; } //qDebug()<<sameVal; //qDebug()<< content[0].getRouteLength(); } return content[0].elements; }
/// creates a population from a given set of Qpoints QPFWPVector Popu::init(QPFWPVector points){ Chromosome member;int i=0;chromoSize=points.size(); while (i<popSize){ //qDebug()<<"Adding the "<<i<<"chromo"; Chromosome member; member.setElements(points,true); content.append(member); totalRoute+=member.routeLength; i++; } qDebug()<<"Created an initial Random pop with route:"<<totalRoute; ///We now sort the population according to its content fitness sortContent(); for (int k=0;k<popSize;k++){ //qDebug()<< content.at(k).elements; qDebug()<< content.at(k).fitness; } qDebug()<<"Population sorted"; //content.replace(0,parentOne); //content.replace(1,parentTwo); newGen=content; for (int j=0;j<Max_iter;j++){ createNewGen(); } ///we return the best encountred child ever! /// if (child.routeLength >parentOne.routeLength) {child=parentOne;} /// if (child.routeLength >parentTwo.routeLength) {child=parentTwo;} qDebug()<<"The ultimate route: "<<content[0].routeLength; // for (int k=0;k<chromoSize;k++){ //qDebug()<< content[0].elements.at(k).parentLoop; //} ///return child.elements; return content[0].elements; }
Chromosome Popu::crossOver(Chromosome parent1, Chromosome parent2){ int pos=3,chromoSize=parent2.elements.size()-1; Chromosome child; int childSize=0; pos=qrand()%chromoSize; // check that we can make crossover if (pos<=2 && chromoSize>2) pos=2; child.setElements(parent1.elements,pos); for (int i=1; i< (chromoSize-pos) ; i++) { if (!child.elements.contains(parent2.elements.at(pos+i))) { child.elements<<parent2.elements.at(pos+i); } } /// now we have to complete the route thus to make a valid one childSize=child.elements.size(); for (int i=0; i<=chromoSize ; i++) { if (!child.elements.contains(parent2.elements.at(i))) { child.elements.append(parent2.elements.at(i)); } } return child; }