Facebook
From Mungo Leopard, 7 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 221
  1. import msrcpsp.evaluation.DurationEvaluator;
  2. import msrcpsp.io.MSRCPSPIO;
  3. import msrcpsp.scheduling.BaseIndividual;
  4. import msrcpsp.scheduling.Resource;
  5. import msrcpsp.scheduling.Schedule;
  6. import msrcpsp.scheduling.Task;
  7. import msrcpsp.scheduling.greedy.Greedy;
  8.  
  9. import java.util.List;
  10. import java.util.Random;
  11.  
  12.  
  13. /**
  14.  * Created by Glizda on 19.03.2017.
  15.  */
  16. public class Population {
  17.  
  18.     BaseIndividual[] population;
  19.     double crossProb;
  20.     double mutProb;
  21.     int diver;
  22.     int size;
  23.     public Population(int size, double crossProb, double mutProb, int diver)
  24.     {
  25.         this.diver = diver;
  26.         population = new BaseIndividual[size];
  27.         this.crossProb = crossProb;
  28.         this.mutProb = mutProb;
  29.         this.size = size;
  30.     }
  31.     public void initialize(String definitionFile)
  32.     {
  33.         int i = 0;
  34.         while(i < size)
  35.         {
  36.             MSRCPSPIO reader = new MSRCPSPIO();
  37.             Schedule sch = reader.readDefinition(definitionFile);
  38.             sch = schedule(sch);
  39.             DurationEvaluator durEv = new DurationEvaluator(sch);
  40.             BaseIndividual b = new BaseIndividual(sch, durEv);
  41.             population[i] = b;
  42.             setDuration(population[i]);
  43.             i++;
  44.         }
  45.         //System.out.println("Jestem w initialize");
  46.         //printPop();
  47.     }
  48.     public void printPop()
  49.     {
  50.         int i = 0;
  51.         for(BaseIndividual b : population)
  52.         {
  53.  
  54.             System.out.println(i++ + ". " + b);
  55.         }
  56.     }
  57.     public void setDuration(BaseIndividual bi)
  58.     {
  59.         Greedy greedy = new Greedy(bi.getSchedule().getSuccesors());
  60.         greedy.buildTimestamps(bi.getSchedule());
  61.         bi.setDurationAndCost();
  62.  
  63.     }
  64.     public Schedule schedule(Schedule schedule)
  65.     {
  66.         for(Task task : schedule.getTasks())
  67.         {
  68.             List<Resource> capRes = schedule.getCapableResources(task);
  69.             Resource r = capRes.get((int)(Math.random() * 100) % capRes.size());
  70.             task.setResourceId(r.getId());
  71.         }
  72.         return schedule;
  73.     }
  74.  
  75.     public BaseIndividual select()
  76.     {
  77.         BaseIndividual[] temp = new BaseIndividual[diver];
  78.         int min;
  79.         int ind = 0;
  80.         Random rand = new Random();
  81.         if(diver <= population.length)
  82.         {
  83.             for(int i = 0; i < diver; i++)
  84.             {
  85.                 int j = rand.nextInt(population.length);
  86.                 temp[i] = population[j];
  87.             }
  88.             min = temp[0].getDuration();
  89.  
  90.             for(int k = 0; k < diver; k++)
  91.             {
  92.                 if(temp[k].getDuration() < min)
  93.                 {
  94.                     min = temp[k].getDuration();
  95.                     ind = k;
  96.                 }
  97.             }
  98.  
  99.         }
  100.         return temp[ind];
  101.     }
  102.  
  103.     public BaseIndividual[] crossOver(BaseIndividual selected, BaseIndividual selected2)
  104.     {
  105.         boolean done = false;
  106.         double d = Math.random();
  107.         int tasksLength = selected.getSchedule().getTasks().length;
  108.         if(d < crossProb - 0.01)
  109.         {
  110.             done = true;
  111.             Task temp;
  112.             int mutInd = (int)(Math.random() * 100) % tasksLength;
  113.             for(int i = mutInd; i < tasksLength; i++)
  114.             {
  115.                 temp = selected.getSchedule().getTasks()[i];
  116.                 selected.getSchedule().getTasks()[i] = selected2.getSchedule().getTasks()[i];
  117.                 selected2.getSchedule().getTasks()[i] = temp;
  118.             }
  119.         }
  120.         return done ? new BaseIndividual[] {selected, selected2} : new BaseIndividual[] {selected};
  121.     }
  122.  
  123.     public BaseIndividual mutate(BaseIndividual one)
  124.     {
  125.         int len = one.getSchedule().getTasks().length;
  126.         for(int i = 0; i < len; i++)
  127.         {
  128.             if(Math.random() < mutProb - 0.01)
  129.             {
  130.                     Task task = one.getSchedule().getTasks()[i];
  131.                     List<Resource> capRes = one.getSchedule().getCapableResources(task);
  132.                     Resource r = capRes.get((int)(Math.random() * 100) % capRes.size());
  133.                     task.setResourceId(r.getId());
  134.             }
  135.         }
  136.         setDuration(one);
  137.  
  138.         return one;
  139.     }
  140.  
  141.     public Population evolve(Population ini)
  142.     {
  143.         Population fin = new Population(ini.size, crossProb, mutProb, diver);
  144.         for(int finLen = 0; finLen < ini.size; finLen++)
  145.         {
  146.             BaseIndividual bi1 = ini.select();
  147.             System.out.println(bi1.getDuration());
  148.             BaseIndividual bi2 = ini.select();
  149.             System.out.println(bi2.getDuration());
  150.             BaseIndividual[] crossed = crossOver(bi1, bi2);
  151.  
  152.             //System.out.println("Jestem w evolve po crossie");
  153.             //printPop();
  154.             for(int j = 0; j < crossed.length; j++)
  155.             {
  156.                 BaseIndividual b = mutate(crossed[j]);
  157.                 fin.population[finLen] = b;
  158.                 if(j > 0)
  159.                     finLen++;
  160.             }
  161.             //System.out.println("Jestem w evolve po mutate");
  162.             //printPop();
  163.         }
  164.         return fin;
  165.     }
  166.  
  167.     public void evolution(int gen)
  168.     {
  169.         Population p = this;
  170.         for(int i = 0; i < gen; i++)
  171.         {
  172.             p = evolve(p);
  173.         }
  174.     }
  175. }
  176.