Facebook
From Rude Crocodile, 7 Years ago, written in Java.
This paste is a reply to Untitled from Aqua Earthworm - view diff
Embed
Download Paste or View Raw
Hits: 400
  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.             System.out.print("To ten skurwiel ");
  89.             System.out.println(min = temp[0].getDuration());
  90.             System.out.println("Jestem w select");
  91.             printPop();
  92.             for(int k = 0; k < diver; k++)
  93.             {
  94.                 if(temp[k].getDuration() < min)
  95.                 {
  96.                     min = temp[k].getDuration();
  97.                     ind = k;
  98.                 }
  99.             }
  100.  
  101.         }
  102.         return temp[ind];
  103.     }
  104.  
  105.     public BaseIndividual[] crossOver(BaseIndividual selected, BaseIndividual selected2)
  106.     {
  107.         boolean done = false;
  108.         double d = Math.random();
  109.         int tasksLength = selected.getSchedule().getTasks().length;
  110.         if(d < crossProb - 0.01)
  111.         {
  112.             done = true;
  113.             Task temp;
  114.             int mutInd = (int)(Math.random() * 100) % tasksLength;
  115.             for(int i = mutInd; i < tasksLength; i++)
  116.             {
  117.                 temp = selected.getSchedule().getTasks()[i];
  118.                 selected.getSchedule().getTasks()[i] = selected2.getSchedule().getTasks()[i];
  119.                 selected2.getSchedule().getTasks()[i] = temp;
  120.             }
  121.         }
  122.         return done ? new BaseIndividual[] {selected, selected2} : new BaseIndividual[] {selected};
  123.     }
  124.  
  125.     public BaseIndividual mutate(BaseIndividual one)
  126.     {
  127.         int len = one.getSchedule().getTasks().length;
  128.         for(int i = 0; i < len; i++)
  129.         {
  130.             if(Math.random() < mutProb)
  131.             {
  132.                     Task task = one.getSchedule().getTasks()[i];
  133.                     List<Resource> capRes = one.getSchedule().getCapableResources(task);
  134.                     Resource r = capRes.get((int)(Math.random() * 100) % capRes.size());
  135.                     task.setResourceId(r.getId());
  136.             }
  137.         }
  138.         setDuration(one);
  139.  
  140.         return one;
  141.     }
  142.  
  143.     public Population evolve(Population ini)
  144.     {
  145.         Population fin = new Population(ini.size, crossProb, mutProb, diver);
  146.         for(int finLen = 0; finLen < ini.size; finLen++)
  147.         {
  148.             BaseIndividual bi1 = ini.select();
  149.             setDuration(bi1);
  150.             System.out.println(bi1.getDuration() + "bi1");
  151.             BaseIndividual bi2 = ini.select();
  152.             setDuration(bi2);
  153.             System.out.println(bi2.getDuration() + "bi2");
  154.             BaseIndividual[] crossed = crossOver(bi1, bi2);
  155.  
  156.             //System.out.println("Jestem w evolve po crossie");
  157.             //printPop();
  158.             for(int j = 0; j < crossed.length; j++)
  159.             {
  160.                 BaseIndividual b = mutate(crossed[j]);
  161.                 fin.population[finLen] = b;
  162.                 if(j > 0)
  163.                     finLen++;
  164.             }
  165.             //System.out.println("Jestem w evolve po mutate");
  166.             //printPop();
  167.         }
  168.         return fin;
  169.     }
  170.  
  171.     public void evolution(int gen)
  172.     {
  173.         Population p = this;
  174.         for(int i = 0; i < gen; i++)
  175.         {
  176.             System.out.println("Populacja nr " + i);
  177.             p = evolve(p);
  178.             int j = 0;
  179.             for(BaseIndividual b : population)
  180.             {
  181.                 if(b == null)
  182.                     j++;
  183.             }
  184.             System.out.println("Liczba nulli po generacji: " + j);
  185.         }
  186.     }
  187. }
  188.