Facebook
From Soft Iguana, 7 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 275
  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.  
  32.     }
  33.     public void initialize(String definitionFile)
  34.     {
  35.         for(int i = 0; i < size; i++)
  36.         {
  37.             MSRCPSPIO reader = new MSRCPSPIO();
  38.             Schedule sch = reader.readDefinition(definitionFile);
  39.             sch = schedule(sch);
  40.             DurationEvaluator durEv = new DurationEvaluator(sch);
  41.             population[i] = new BaseIndividual(sch, durEv);
  42.             setDuration(population[i]);
  43.         }
  44.     }
  45.     public void setDuration(BaseIndividual bi)
  46.     {
  47.         Greedy greedy = new Greedy(bi.getSchedule().getSuccesors());
  48.         greedy.buildTimestamps(bi.getSchedule());
  49.         bi.setDurationAndCost();
  50.     }
  51.     public Schedule schedule(Schedule schedule)
  52.     {
  53.         for(Task task : schedule.getTasks())
  54.         {
  55.             List<Resource> capRes = schedule.getCapableResources(task);
  56.             Resource r = capRes.get((int)(Math.random() * 100) % capRes.size());
  57.             task.setResourceId(r.getId());
  58.         }
  59.         return schedule;
  60.     }
  61.  
  62.     public BaseIndividual select()
  63.     {
  64.         BaseIndividual[] temp = new BaseIndividual[diver];
  65.         int min;
  66.         int ind = 0;
  67.         Random rand = new Random();
  68.         if(diver <= population.length)
  69.         {
  70.             for(int i = 0; i < diver; i++)
  71.             {
  72.                 int j = rand.nextInt(population.length);
  73.  
  74.                 temp[i] = population[j];
  75.             }
  76.             min = temp[0].getDuration();
  77.             for(int i = 0; i < diver; i++)
  78.                 if(temp[i].getDuration() < min)
  79.                 {
  80.                     min = temp[i].getDuration();
  81.                     ind = i;
  82.                 }
  83.         }
  84.         return temp[ind];
  85.     }
  86.  
  87.     public BaseIndividual[] crossOver(BaseIndividual selected, BaseIndividual selected2)
  88.     {
  89.         boolean done = false;
  90.         double d = Math.random();
  91.         int tasksLength = selected.getSchedule().getTasks().length;
  92.         if(d < crossProb - 0.01)
  93.         {
  94.             done = true;
  95.             Task temp;
  96.             int mutInd = (int)(Math.random() * 100) % tasksLength;
  97.             for(int i = mutInd; i < tasksLength; i++)
  98.             {
  99.                 temp = selected.getSchedule().getTasks()[i];
  100.                 selected.getSchedule().getTasks()[i] = selected2.getSchedule().getTasks()[i];
  101.                 selected2.getSchedule().getTasks()[i] = temp;
  102.             }
  103.         }
  104.         return done ? new BaseIndividual[] {selected, selected2} : new BaseIndividual[] {selected};
  105.     }
  106.  
  107.     public BaseIndividual mutate(BaseIndividual one)
  108.     {
  109.         int len = one.getSchedule().getTasks().length;
  110.         for(int i = 0; i < len; i++)
  111.         {
  112.             if(Math.random() < mutProb - 0.01)
  113.             {
  114.                     Task task = one.getSchedule().getTasks()[i];
  115.                     List<Resource> capRes = one.getSchedule().getCapableResources(task);
  116.                     Resource r = capRes.get((int)(Math.random() * 100) % capRes.size());
  117.                     task.setResourceId(r.getId());
  118.             }
  119.         }
  120.         Greedy greedy = new Greedy(one.getSchedule().getSuccesors());
  121.         greedy.buildTimestamps(one.getSchedule());
  122.  
  123.         return one;
  124.     }
  125.  
  126.     public Population evolve(Population ini)
  127.     {
  128.         Population fin = new Population(ini.size, crossProb, mutProb, diver);
  129.         for(int finLen = 0; finLen < ini.size; finLen++)
  130.         {
  131.             BaseIndividual bi1 = ini.select();
  132.             BaseIndividual bi2 = ini.select();
  133.             BaseIndividual[] crossed = crossOver(bi1, bi2);
  134.             for(int j = 0; j < crossed.length; j++)
  135.             {
  136.                 BaseIndividual b = mutate(crossed[j]);
  137.                 fin.population[finLen] = b;
  138.                 if(j > 0)
  139.                     finLen++;
  140.             }
  141.         }
  142.         return fin;
  143.     }
  144.  
  145.     public void evolution(int gen)
  146.     {
  147.         Population p = this;
  148.         for(int i = 0; i < gen; i++)
  149.         {
  150.             p = evolve(p);
  151.         }
  152.     }
  153. }
  154.