Exemplo n.º 1
0
  public NSGAII.Solutions.Solution mutate() {

    NSGAII.Solutions.Solution mutatedOffspring =
        new NSGAII.Solutions.QRTPSolution.OneSolution(alg.chromosomeSize);
    int[] mutatedChromosome = new int[alg.chromosomeSize];

    int[] offspringChromosome = offspring.getChromosome();

    for (int i = 0; i < offspringChromosome.length; i++) {
      mutatedChromosome[i] = offspringChromosome[i];
    }

    for (int i = 0; i < mutatedChromosome.length; i++) {

      double randM = (double) randomGen.nextInt(100 + 1) / 100;

      if (randM < mutationRate) {

        int swapPositionA = i;
        int swapPositionB = i;

        while (swapPositionA == swapPositionB) {
          swapPositionB = randomGen.nextInt(alg.chromosomeSize);
        }

        int geneA = (int) mutatedChromosome[swapPositionA];
        int geneB = (int) mutatedChromosome[swapPositionB];

        mutatedChromosome[swapPositionA] = geneB;
        mutatedChromosome[swapPositionB] = geneA;

      } else {
        mutatedChromosome[i] = (int) mutatedChromosome[i];
      }
    }

    mutatedOffspring.setChromosome(mutatedChromosome);
    return mutatedOffspring;
  }
Exemplo n.º 2
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;

 }