- import msrcpsp.evaluation.DurationEvaluator;
- import msrcpsp.io.MSRCPSPIO;
- import msrcpsp.scheduling.BaseIndividual;
- import msrcpsp.scheduling.Resource;
- import msrcpsp.scheduling.Schedule;
- import msrcpsp.scheduling.Task;
- import msrcpsp.scheduling.greedy.Greedy;
- import java.util.List;
- import java.util.Random;
- /**
- * Created by Glizda on 19.03.2017.
- */
- public class Population {
- BaseIndividual[] population;
- double crossProb;
- double mutProb;
- int diver;
- int size;
- public Population(int size, double crossProb, double mutProb, int diver)
- {
- this.diver = diver;
- population = new BaseIndividual[size];
- this.crossProb = crossProb;
- this.mutProb = mutProb;
- this.size = size;
- }
- public void initialize(String definitionFile)
- {
- for(int i = 0; i < size; i++)
- {
- MSRCPSPIO reader = new MSRCPSPIO();
- Schedule sch = reader.readDefinition(definitionFile);
- sch = schedule(sch);
- DurationEvaluator durEv = new DurationEvaluator(sch);
- population[i] = new BaseIndividual(sch, durEv);
- setDuration(population[i]);
- }
- }
- public void setDuration(BaseIndividual bi)
- {
- Greedy greedy = new Greedy(bi.getSchedule().getSuccesors());
- greedy.buildTimestamps(bi.getSchedule());
- bi.setDurationAndCost();
- }
- public Schedule schedule(Schedule schedule)
- {
- for(Task task : schedule.getTasks())
- {
- List<Resource> capRes = schedule.getCapableResources(task);
- Resource r = capRes.get((int)(Math.random() * 100) % capRes.size());
- task.setResourceId(r.getId());
- }
- return schedule;
- }
- public BaseIndividual select()
- {
- BaseIndividual[] temp = new BaseIndividual[diver];
- int min;
- int ind = 0;
- Random rand = new Random();
- if(diver <= population.length)
- {
- for(int i = 0; i < diver; i++)
- {
- int j = rand.nextInt(population.length);
- temp[i] = population[j];
- }
- min = temp[0].getDuration();
- for(int i = 0; i < diver; i++)
- if(temp[i].getDuration() < min)
- {
- min = temp[i].getDuration();
- ind = i;
- }
- }
- return temp[ind];
- }
- public BaseIndividual[] crossOver(BaseIndividual selected, BaseIndividual selected2)
- {
- boolean done = false;
- double d = Math.random();
- int tasksLength = selected.getSchedule().getTasks().length;
- if(d < crossProb - 0.01)
- {
- done = true;
- Task temp;
- int mutInd = (int)(Math.random() * 100) % tasksLength;
- for(int i = mutInd; i < tasksLength; i++)
- {
- temp = selected.getSchedule().getTasks()[i];
- selected.getSchedule().getTasks()[i] = selected2.getSchedule().getTasks()[i];
- selected2.getSchedule().getTasks()[i] = temp;
- }
- }
- return done ? new BaseIndividual[] {selected, selected2} : new BaseIndividual[] {selected};
- }
- public BaseIndividual mutate(BaseIndividual one)
- {
- int len = one.getSchedule().getTasks().length;
- for(int i = 0; i < len; i++)
- {
- if(Math.random() < mutProb - 0.01)
- {
- Task task = one.getSchedule().getTasks()[i];
- List<Resource> capRes = one.getSchedule().getCapableResources(task);
- Resource r = capRes.get((int)(Math.random() * 100) % capRes.size());
- task.setResourceId(r.getId());
- }
- }
- Greedy greedy = new Greedy(one.getSchedule().getSuccesors());
- greedy.buildTimestamps(one.getSchedule());
- return one;
- }
- public Population evolve(Population ini)
- {
- Population fin = new Population(ini.size, crossProb, mutProb, diver);
- for(int finLen = 0; finLen < ini.size; finLen++)
- {
- BaseIndividual bi1 = ini.select();
- BaseIndividual bi2 = ini.select();
- BaseIndividual[] crossed = crossOver(bi1, bi2);
- for(int j = 0; j < crossed.length; j++)
- {
- BaseIndividual b = mutate(crossed[j]);
- fin.population[finLen] = b;
- if(j > 0)
- finLen++;
- }
- }
- return fin;
- }
- public void evolution(int gen)
- {
- Population p = this;
- for(int i = 0; i < gen; i++)
- {
- p = evolve(p);
- }
- }
- }