Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package msrcpsp;
- import msrcpsp.evaluation.BaseEvaluator;
- import msrcpsp.evaluation.DurationEvaluator;
- import msrcpsp.io.MSRCPSPIO;
- import msrcpsp.scheduling.Resource;
- import msrcpsp.scheduling.Schedule;
- import msrcpsp.scheduling.Task;
- import msrcpsp.scheduling.greedy.Greedy;
- import org.junit.runner.Runner;
- import java.io.*;
- import java.util.HashSet;
- import java.util.List;
- import java.util.Random;
- import java.util.concurrent.ThreadLocalRandom;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- * Created by Marcin on 2017-03-23.
- */
- public class RandomSolution {
- private static final Logger LOGGER = Logger.getLogger( Runner.class.getName() );
- //private static final String pathDefFile = "assets/def_small/10_3_5_3.def";
- private static final String pathDefFile = "assets/dataset_def/200_20_145_15.def";
- //private static final String pathWriteFile = "assets/solutions_small/10_7_10_7.sol";
- private static MSRCPSPIO reader = new MSRCPSPIO();
- /** parameters */
- private final static int popSize = 100;
- private final static int amountOfPopulations = 200;
- private final static int tournamentIter = 5;
- private final static int mutationProb = 2; /** % */
- private final static int crossProb = 20; /** % */
- private final static int iterationAmount = 10; /** iteration for create new populations - to statistics*/
- /** end of parameters section */
- /** main */
- public static void main(String[] args){
- long startTime = System.currentTimeMillis();
- int[] avgMax = new int[amountOfPopulations];
- int[] avgMin = new int[amountOfPopulations];
- int[] avg = new int[amountOfPopulations];
- for(int iter=0;iter<iterationAmount;iter++){
- Schedule[] population = new Schedule[popSize];
- Random rd = new Random(Integer.MAX_VALUE);
- for(int i=0;i<popSize;i++){ /** initialize new population */
- Schedule schedule = reader.readDefinition(pathDefFile); /** get the same schedule from file for each iteration */
- List<Resource> capableResources;
- if (schedule!=null) {
- /** get array of upper bounds of each task assignment, that does
- // violate skill constraint */
- int[] upperBounds = schedule.getUpperBounds(schedule.getTasks().length);
- /** create an evaluator */
- //BaseEvaluator evaluator = new DurationEvaluator(schedule);
- Task[] tasks = schedule.getTasks();
- //Resource[] resources = schedule.getResources();
- /** create population */
- /** assign schedule */
- for(int iterForAssign=0;iterForAssign<tasks.length;iterForAssign++){
- capableResources = schedule.getCapableResources(tasks[iterForAssign]);
- schedule.assign(tasks[iterForAssign], capableResources.get((int)(rd.nextDouble() * (upperBounds[iterForAssign])))); /** create 'random' population */
- }
- population[i] = schedule; /** set schedule to population */
- }
- else{
- LOGGER.log(Level.WARNING, "Could not read the Definition " + pathDefFile);
- }
- }
- /** prepare statistic data */
- int best = Integer.MAX_VALUE;
- int worst = Integer.MIN_VALUE;
- int avgValue = 0;
- int[] max = new int[amountOfPopulations];
- int[] min = new int[amountOfPopulations];
- int[] average = new int[amountOfPopulations];
- /** !important */
- for(int i=0;i<amountOfPopulations;i++){
- int[] resultOfEval = evaluate(population);
- for(int j=0;j<popSize;j++){ /** prepare statistics */
- if(best>resultOfEval[j]){ /** find the shortest time */
- best = resultOfEval[j];
- }
- if(worst<resultOfEval[j]){ /** find the longest time */
- worst = resultOfEval[j];
- }
- avgValue += resultOfEval[j];
- }
- avgValue = avgValue / resultOfEval.length;
- max[i] = worst; /** the longest */
- min[i] = best; /** the shortest */
- average[i] = avgValue;
- avgMax[i] += max[i];
- avgMin[i] += min[i];
- avg[i] += average[i];
- best=Integer.MAX_VALUE; /** reset value !important */
- worst=Integer.MIN_VALUE; /** reset value !important */
- avgValue=0; /** reset value !important */
- }
- }
- try (PrintWriter out = new PrintWriter("temp.txt")) {
- out.println("AVG BEST");
- for(int i=0;i<amountOfPopulations;i++){
- out.println((avgMin[i] / iterationAmount)+"");
- }
- out.println("--------");
- out.println("AVG WORST");
- for(int i=0;i<amountOfPopulations;i++){
- out.println((avgMax[i] / iterationAmount)+"");
- }
- out.println("--------");
- out.println("AVG");
- for(int i=0;i<amountOfPopulations;i++){
- out.println((avg[i] / iterationAmount)+"");
- }
- out.close();
- }
- catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- int bestResult = Integer.MAX_VALUE;
- for(int i=0;i<amountOfPopulations;i++) {
- avgMax[i] = (avgMax[i] / iterationAmount);
- avgMin[i] = (avgMin[i] / iterationAmount);
- avg[i] = (avg[i] / iterationAmount);
- System.out.print("Pokolenie: "+(i+1)+" | ");
- System.out.print("Najlepszy: "+avgMin[i]+" | ");
- System.out.print("Najgorszy: "+avgMax[i]+" | ");
- System.out.print("Sredni: "+avg[i]);
- System.out.println();
- if(avgMin[i]<bestResult) bestResult=avgMin[i];
- if(i==amountOfPopulations-1) System.out.println("Best result: "+bestResult);
- }
- long stopTime = System.currentTimeMillis();
- System.out.println("Done in: "+(stopTime-startTime)/1000+" s");
- }
- /** end of main section */
- /** methods */
- public static int[] evaluate(Schedule[] s){
- int[] resultOfEval = new int[s.length];
- BaseEvaluator be;
- for(int i=0;i<popSize;i++){
- Schedule schedule = s[i];
- Greedy g = new Greedy(schedule.getSuccesors()); /** successor = nastepca, dziedzic
- * Creates successor table. True on the nth place
- * means that nth task has successors.
- */
- g.buildTimestamps(schedule); /** iterate for each tasks and set their timestamps /
- * Determines order of tasks by setting their start
- * and finish. Does not change task / resource assignment.
- * Assumes that all assignments are set. Uses knowledge about
- * successors of each task to first place the tasks with successors
- * and then rest of the tasks.
- */
- be = new DurationEvaluator(schedule); /** eval of time */
- resultOfEval[i] = be.getDuration(); /** @return total duration of the project */
- }
- return resultOfEval;
- }
- /** end of methods section */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement