Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.50 KB | None | 0 0
  1. package msrcpsp;
  2.  
  3. import msrcpsp.evaluation.BaseEvaluator;
  4. import msrcpsp.evaluation.DurationEvaluator;
  5. import msrcpsp.io.MSRCPSPIO;
  6. import msrcpsp.scheduling.Resource;
  7. import msrcpsp.scheduling.Schedule;
  8. import msrcpsp.scheduling.Task;
  9. import msrcpsp.scheduling.greedy.Greedy;
  10. import org.junit.runner.Runner;
  11.  
  12.  
  13. import java.io.*;
  14. import java.util.HashSet;
  15. import java.util.List;
  16. import java.util.Random;
  17. import java.util.concurrent.ThreadLocalRandom;
  18. import java.util.logging.Level;
  19. import java.util.logging.Logger;
  20.  
  21. /**
  22.  * Created by Marcin on 2017-03-23.
  23.  */
  24. public class RandomSolution {
  25.     private static final Logger LOGGER = Logger.getLogger( Runner.class.getName() );
  26.     //private static final String pathDefFile = "assets/def_small/10_3_5_3.def";
  27.     private static final String pathDefFile = "assets/dataset_def/200_20_145_15.def";
  28.     //private static final String pathWriteFile = "assets/solutions_small/10_7_10_7.sol";
  29.     private static MSRCPSPIO reader = new MSRCPSPIO();
  30.  
  31.     /** parameters */
  32.     private final static int popSize = 100;
  33.     private final static int amountOfPopulations = 200;
  34.     private final static int tournamentIter = 5;
  35.     private final static int mutationProb = 2; /** % */
  36.     private final static int crossProb = 20; /** % */
  37.     private final static int iterationAmount = 10; /** iteration for create new populations - to statistics*/
  38.     /** end of parameters section */
  39.  
  40.     /** main */
  41.  
  42.     public static void main(String[] args){
  43.         long startTime = System.currentTimeMillis();
  44.         int[] avgMax = new int[amountOfPopulations];
  45.         int[] avgMin = new int[amountOfPopulations];
  46.         int[] avg = new int[amountOfPopulations];
  47.         for(int iter=0;iter<iterationAmount;iter++){
  48.             Schedule[] population = new Schedule[popSize];
  49.             Random rd = new Random(Integer.MAX_VALUE);
  50.  
  51.             for(int i=0;i<popSize;i++){ /** initialize new population */
  52.                 Schedule schedule = reader.readDefinition(pathDefFile); /** get the same schedule from file for each iteration */
  53.                 List<Resource> capableResources;
  54.  
  55.                 if (schedule!=null) {
  56.                     /** get array of upper bounds of each task assignment, that does
  57.                      // violate skill constraint */
  58.                     int[] upperBounds = schedule.getUpperBounds(schedule.getTasks().length);
  59.                     /** create an evaluator */
  60.                     //BaseEvaluator evaluator = new DurationEvaluator(schedule);
  61.  
  62.                     Task[] tasks = schedule.getTasks();
  63.                     //Resource[] resources = schedule.getResources();
  64.                     /** create population */
  65.                     /** assign schedule */
  66.                     for(int iterForAssign=0;iterForAssign<tasks.length;iterForAssign++){
  67.                         capableResources = schedule.getCapableResources(tasks[iterForAssign]);
  68.                         schedule.assign(tasks[iterForAssign], capableResources.get((int)(rd.nextDouble() * (upperBounds[iterForAssign])))); /** create 'random' population */
  69.                     }
  70.                     population[i] = schedule; /** set schedule to population */
  71.                 }
  72.                 else{
  73.                     LOGGER.log(Level.WARNING, "Could not read the Definition " + pathDefFile);
  74.                 }
  75.             }
  76.             /** prepare statistic data */
  77.             int best = Integer.MAX_VALUE;
  78.             int worst = Integer.MIN_VALUE;
  79.             int avgValue = 0;
  80.             int[] max = new int[amountOfPopulations];
  81.             int[] min = new int[amountOfPopulations];
  82.             int[] average = new int[amountOfPopulations];
  83.             /** !important */
  84.             for(int i=0;i<amountOfPopulations;i++){
  85.                 int[] resultOfEval = evaluate(population);
  86.                 for(int j=0;j<popSize;j++){ /** prepare statistics */
  87.                     if(best>resultOfEval[j]){ /** find the shortest time */
  88.                         best = resultOfEval[j];
  89.                     }
  90.                     if(worst<resultOfEval[j]){ /** find the longest time */
  91.                         worst = resultOfEval[j];
  92.                     }
  93.                     avgValue += resultOfEval[j];
  94.                 }
  95.                 avgValue = avgValue / resultOfEval.length;
  96.                 max[i] = worst; /** the longest */
  97.                 min[i] = best; /** the shortest */
  98.                 average[i] = avgValue;
  99.                 avgMax[i] += max[i];
  100.                 avgMin[i] += min[i];
  101.                 avg[i] += average[i];
  102.                 best=Integer.MAX_VALUE; /** reset value !important */
  103.                 worst=Integer.MIN_VALUE; /** reset value !important */
  104.                 avgValue=0; /** reset value !important */
  105.             }
  106.         }
  107.  
  108.         try (PrintWriter out = new PrintWriter("temp.txt")) {
  109.             out.println("AVG BEST");
  110.             for(int i=0;i<amountOfPopulations;i++){
  111.                 out.println((avgMin[i] / iterationAmount)+"");
  112.             }
  113.             out.println("--------");
  114.             out.println("AVG WORST");
  115.             for(int i=0;i<amountOfPopulations;i++){
  116.                 out.println((avgMax[i] / iterationAmount)+"");
  117.             }
  118.             out.println("--------");
  119.             out.println("AVG");
  120.             for(int i=0;i<amountOfPopulations;i++){
  121.                 out.println((avg[i] / iterationAmount)+"");
  122.             }
  123.             out.close();
  124.         }
  125.         catch (FileNotFoundException e) {
  126.             e.printStackTrace();
  127.         }
  128.         int bestResult = Integer.MAX_VALUE;
  129.         for(int i=0;i<amountOfPopulations;i++) {
  130.             avgMax[i] = (avgMax[i] / iterationAmount);
  131.             avgMin[i] = (avgMin[i] / iterationAmount);
  132.             avg[i] = (avg[i] / iterationAmount);
  133.             System.out.print("Pokolenie: "+(i+1)+" | ");
  134.             System.out.print("Najlepszy: "+avgMin[i]+" | ");
  135.             System.out.print("Najgorszy: "+avgMax[i]+" | ");
  136.             System.out.print("Sredni: "+avg[i]);
  137.             System.out.println();
  138.             if(avgMin[i]<bestResult) bestResult=avgMin[i];
  139.             if(i==amountOfPopulations-1) System.out.println("Best result: "+bestResult);
  140.         }
  141.         long stopTime = System.currentTimeMillis();
  142.         System.out.println("Done in: "+(stopTime-startTime)/1000+" s");
  143.     }
  144.  
  145.     /** end of main section */
  146.  
  147.     /** methods */
  148.  
  149.     public static int[] evaluate(Schedule[] s){
  150.         int[] resultOfEval = new int[s.length];
  151.         BaseEvaluator be;
  152.         for(int i=0;i<popSize;i++){
  153.             Schedule schedule = s[i];
  154.             Greedy g = new Greedy(schedule.getSuccesors()); /** successor = nastepca, dziedzic
  155.              * Creates successor table. True on the nth place
  156.              * means that nth task has successors.
  157.              */
  158.             g.buildTimestamps(schedule);                    /** iterate for each tasks and set their timestamps /
  159.              * Determines order of tasks by setting their start
  160.              * and finish. Does not change task / resource assignment.
  161.              * Assumes that all assignments are set. Uses knowledge about
  162.              * successors of each task to first place the tasks with successors
  163.              * and then rest of the tasks.
  164.              */
  165.             be = new DurationEvaluator(schedule); /** eval of time */
  166.             resultOfEval[i] = be.getDuration(); /** @return total duration of the project */
  167.         }
  168.         return resultOfEval;
  169.     }
  170.     /** end of methods section */
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement