public void sortFrontbyR(){ sortByR = new ArrayList(); for (int i = 0 ; i < Fronts.size() ; i++){ ArrayList tempfront = (ArrayList) Fronts.get(i); for (int j = 0 ; j < tempfront.size() ; j++){ for (int a = 0 ; a < tempfront.size() ; a++){ if (a > j){ NSGAII.Solutions.QRTPSolution.OneSolution first = (NSGAII.Solutions.QRTPSolution.OneSolution) tempfront.get(j); double x = first.getRecall(); NSGAII.Solutions.QRTPSolution.OneSolution second = (NSGAII.Solutions.QRTPSolution.OneSolution) tempfront.get(a); double y = second.getRecall(); if (y > x){ tempfront.set(j, second); tempfront.set(a, first); }else{} } } } sortByR.add(tempfront); } }
//This is nonDominated comparison for Maximization only //that means the max for both objectives are the best public int compare(Object o1, Object o2) { NSGAII.Solutions.QRTPSolution.OneSolution temp1 = (NSGAII.Solutions.QRTPSolution.OneSolution) o1; NSGAII.Solutions.QRTPSolution.OneSolution temp2 = (NSGAII.Solutions.QRTPSolution.OneSolution) o2; //temp2 dominates temp1 if((temp2.getEnergy() > temp1.getEnergy() && temp2.getTime() > temp1.getTime() && temp2.getRecall() < temp1.getRecall())|| (temp2.getEnergy() > temp1.getEnergy() && temp2.getTime() == temp1.getTime() && temp2.getRecall() == temp1.getRecall())|| (temp2.getEnergy() == temp1.getEnergy()&& temp2.getTime() > temp1.getTime() && temp2.getRecall() == temp1.getRecall())|| (temp2.getEnergy() == temp1.getEnergy()&& temp2.getTime() == temp1.getTime() && temp2.getRecall() < temp1.getRecall())|| (temp2.getEnergy() > temp1.getEnergy()&& temp2.getTime() > temp1.getTime() && temp2.getRecall() == temp1.getRecall())|| (temp2.getEnergy() > temp1.getEnergy()&&temp2.getTime() == temp1.getTime() && temp2.getRecall() < temp1.getRecall())|| (temp2.getEnergy() == temp1.getEnergy()&& temp2.getTime() > temp1.getTime() && temp2.getRecall() < temp1.getRecall())){ return 1; }else if((temp1.getEnergy()> temp2.getEnergy()&&temp1.getTime()> temp2.getTime()&&temp1.getRecall()< temp2.getRecall() )|| (temp1.getEnergy() > temp2.getEnergy()&&temp1.getTime() == temp2.getTime()&& temp1.getRecall() == temp2.getRecall() )|| (temp1.getEnergy() == temp2.getEnergy()&& temp1.getTime() > temp2.getTime() && temp1.getRecall() == temp2.getRecall())|| (temp1.getEnergy() == temp2.getEnergy() &&temp1.getTime() == temp2.getTime()&& temp1.getRecall() < temp2.getRecall())|| (temp1.getEnergy() > temp2.getEnergy() && temp1.getTime() > temp2.getTime() && temp1.getRecall() == temp2.getRecall())|| (temp1.getEnergy() > temp2.getEnergy() && temp1.getTime() == temp2.getTime() && temp1.getRecall() < temp2.getRecall())|| (temp1.getEnergy() == temp2.getEnergy() && temp1.getTime() > temp2.getTime() && temp1.getRecall() < temp2.getRecall())) { return -1; }else{ return 0; } }
//the general list with the fronts is sorted based on the crowded distances //in each front separately public void sortByCrowdingDistance(){ for (int i = 0 ; i < Fronts.size() ; i++){ ArrayList tempfront = (ArrayList) Fronts.get(i); for (int j = 0 ; j < tempfront.size() ; j++){ for (int a = 0 ; a < tempfront.size() ; a++){ if (a > j){ NSGAII.Solutions.QRTPSolution.OneSolution first = (NSGAII.Solutions.QRTPSolution.OneSolution) tempfront.get(j); double x = first.getCrowdingDistanceTotal(); NSGAII.Solutions.QRTPSolution.OneSolution second = (NSGAII.Solutions.QRTPSolution.OneSolution) tempfront.get(a); double y = second.getCrowdingDistanceTotal(); if (y >= x){ tempfront.set(j, second); tempfront.set(a, first); }else{ tempfront.set(j, first); tempfront.set(a, second); } } } } //System.out.println("After sorting there are "+((ArrayList)Fronts.get(i)).size()+" in "+(i)); Fronts.set(i,tempfront); } int count = 0; // System.out.println("Fronts "+Fronts.size()); for (int i = 0 ; i < Fronts.size() ; i++){ ArrayList temp = (ArrayList) Fronts.get(i); // System.out.println("temp "+temp.size()); int divRank = 0; for (int j = 0 ; j < temp.size() ; j++){ if (count < alg.popSize){ ((NSGAII.Solutions.QRTPSolution.OneSolution) temp.get(j)).setDivRank(divRank); newPop[count] = (NSGAII.Solutions.QRTPSolution.OneSolution) temp.get(j); count = count+1; divRank = divRank+1; } } } ArrayList tempfrontToPrint = new ArrayList(); ArrayList oldfrontToPrint = frontToPrint; ArrayList newfrontToPrint = new ArrayList(); //System.out.println(); //System.out.println("In Generation "+alg.gen+" the PF is: "); for(int j = 0 ; j < newPop.length ; j++){ if(((NSGAII.Solutions.QRTPSolution.OneSolution)newPop[j]).getRank() == 0){ //System.out.println("Solution "+j+" with COV: "+((NSGAII.Solutions.QRTPSolution.OneSolution)popForGA[j]).getCoverage()+" and Life:"+((NSGAII.Solutions.QRTPSolution.OneSolution)popForGA[j]).getLifetime()); tempfrontToPrint.add(((NSGAII.Solutions.QRTPSolution.OneSolution)newPop[j])); } } for(int i = 0 ; i < tempfrontToPrint.size() ; i++){ NSGAII.Solutions.QRTPSolution.OneSolution temp = (NSGAII.Solutions.QRTPSolution.OneSolution)tempfrontToPrint.get(i); oldfrontToPrint.add(temp); } //System.out.println("The size is "+oldfrontToPrint.size()); //WE COMPARE ALL THE SOLUTIONS TO KEEP ONLY THE BEST for(int i = 0 ; i < oldfrontToPrint.size() ; i++){ boolean dominated = false; boolean exist = false; NSGAII.Solutions.QRTPSolution.OneSolution temp1 = temp1 = (NSGAII.Solutions.QRTPSolution.OneSolution)oldfrontToPrint.get(i); for(int j = 0 ; j < oldfrontToPrint.size() ; j++){ NSGAII.Solutions.QRTPSolution.OneSolution temp2 = (NSGAII.Solutions.QRTPSolution.OneSolution)oldfrontToPrint.get(j); if (i != j && temp1 != null && temp2 != null){ if(temp1.getEnergy() <= 0 || temp1.getTime() <= 0 || temp1.getRecall() <= 0){ temp1 = null; oldfrontToPrint.set(i,null); }} if (i != j && temp1 != null && temp2 != null){ if((temp1.getEnergy() > temp2.getEnergy() && temp1.getTime() > temp2.getTime() && temp1.getRecall() < temp2.getRecall())|| (temp1.getEnergy() == temp2.getEnergy() && temp1.getTime() == temp2.getTime() && temp1.getRecall() == temp2.getRecall())|| (temp1.getEnergy() > temp2.getEnergy() && temp1.getTime() == temp2.getTime() && temp1.getRecall() == temp2.getRecall())|| (temp1.getEnergy() == temp2.getEnergy() && temp1.getTime() > temp2.getTime() && temp1.getRecall() == temp2.getRecall())|| (temp1.getEnergy() == temp2.getEnergy() && temp1.getTime() == temp2.getTime() && temp1.getRecall() < temp2.getRecall())|| (temp1.getEnergy() > temp2.getEnergy() && temp1.getTime() > temp2.getTime() && temp1.getRecall() == temp2.getRecall())|| (temp1.getEnergy() > temp2.getEnergy() && temp1.getTime() == temp2.getTime() && temp1.getRecall() < temp2.getRecall())|| (temp1.getEnergy() == temp2.getEnergy() && temp1.getTime() > temp2.getTime() && temp1.getRecall() < temp2.getRecall())) { temp1 = null; oldfrontToPrint.set(i,null); } }} } for(int i = 0 ; i < oldfrontToPrint.size() ; i++){ if ((NSGAII.Solutions.QRTPSolution.OneSolution)oldfrontToPrint.get(i) != null){ NSGAII.Solutions.Solution newSol = new NSGAII.Solutions.QRTPSolution.OneSolution(alg.chromosomeSize); newSol.setChromosome(((NSGAII.Solutions.QRTPSolution.OneSolution)oldfrontToPrint.get(i)).getChromosome()); newSol.setRecall(((NSGAII.Solutions.QRTPSolution.OneSolution)oldfrontToPrint.get(i)).getRecall()); newSol.setEnergy(((NSGAII.Solutions.QRTPSolution.OneSolution)oldfrontToPrint.get(i)).getEnergy()); newSol.setTime(((NSGAII.Solutions.QRTPSolution.OneSolution)oldfrontToPrint.get(i)).getTime()); newfrontToPrint.add(newSol); }} if(alg.gen == alg.termination-1){ System.out.println(); System.out.println("In Generation "+alg.gen+" the PF is: "); for(int j = 0 ; j < newfrontToPrint.size() ; j++){ NSGAII.Solutions.QRTPSolution.OneSolution temp = (NSGAII.Solutions.QRTPSolution.OneSolution)newfrontToPrint.get(j); System.out.println("Solution "+j+" with E: "+nf.format(temp.getEnergy())+" T:"+nf.format(temp.getTime())+" R:"+nf.format(temp.getRecall())); } //System.out.println(); } frontToPrint = newfrontToPrint; }