Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/18/17 14:38:18 (6 years ago)
Author:
ddorfmei
Message:

#2747: Improved hard-coded genetic algorithm

  • implemented restart strategy
  • implemented 1-Opt algorithm
  • added parameters to control restart strategy and optimal assignment strategy
  • additional results: best solution found after number of generations, jobs/nests (for evaluation only)
File:
1 copied

Legend:

Unmodified
Added
Removed
  • branches/CFSAP/HeuristicLab.Problems.Scheduling.CFSAP/3.3/OneOptAssignment.cs

    r15493 r15533  
    2323using HeuristicLab.Core;
    2424using HeuristicLab.Data;
     25using HeuristicLab.Encodings.BinaryVectorEncoding;
    2526using HeuristicLab.Encodings.PermutationEncoding;
    2627
    2728namespace HeuristicLab.Problems.Scheduling.CFSAP {
    28   public static class OptimalAssignment {
     29  public static class OneOptAssignment {
    2930
    30     public static int[] MakeAssignment(Permutation order, IntMatrix processingTimes, ItemList<IntMatrix> setupTimes, out int T) {
    31       var N = order.Length;
     31    public static BinaryVector MakeAssignment(Permutation order, BinaryVector assignment, IntMatrix processingTimes, ItemList<IntMatrix> setupTimes, out int T) {
     32      var oneOptAssignment = (BinaryVector)assignment.Clone();
     33      T = CFSAP.EvaluateAssignment(order, assignment, processingTimes, setupTimes);
    3234
    33       int[,,] weights = new int[2, 2 * N, 2 * N];
    34       int[,] graph = new int[2, N];
    35       int[,] prevPath = new int[2, N + 1];                     //Only for optimal assignment evaluation
    36       int[] optimalAssignment = new int[N];                //Only for optimal assignment evaluation
     35      while (true) {
     36        var foundBetterSolution = false;
    3737
    38       //Calculate weights in the graph
    39       for (int S = 0; S < N; S++) { //Starting point of the arc
    40         for (int sM = 0; sM < 2; sM++) {    //Starting point machine
    41           int eM = sM == 0 ? 1 : 0;
    42           weights[sM, S, S + 1] = 0;
    43 
    44           for (int E = S + 2; E < S + N; E++)
    45             weights[sM, S, E] =
    46               weights[sM, S, E - 1] +
    47               processingTimes[eM, order[(E - 1) % N]] +
    48               setupTimes[eM][order[(E - 1) % N], order[E % N]];
    49 
    50           for (int E = S + 1; E < S + N; E++)
    51             weights[sM, S, E] += (
    52               processingTimes[sM, order[S % N]] +
    53               setupTimes[sM][order[S % N], order[(E + 1) % N]]
    54             );
    55         }
    56       }
    57 
    58       //Determine the shortest path in the graph
    59       T = int.MaxValue / 2;
    60       for (int S = 0; S < N - 1; S++)      //Start node in graph          O(N)
    61         for (int SM = 0; SM < 2; SM++) {   //Start node machine in graph  O(1)
    62           graph[SM, S] = 0;
    63           graph[SM == 0 ? 1 : 0, S] = int.MaxValue / 2;
    64           prevPath[SM, 0] = -1;
    65           for (int E = S + 1; E < N; E++)      //Currently calculated node          O(N)
    66             for (int EM = 0; EM < 2; EM++) {   //Currently calculated node machine  O(1)
    67               graph[EM, E] = int.MaxValue / 2;
    68               for (int EC = S; EC < E; EC++) { //Nodes connected to node E          O(N)
    69                 int newWeight = graph[EM == 0 ? 1 : 0, EC] + weights[EM == 0 ? 1 : 0, EC, E];
    70                 if (newWeight < graph[EM, E]) {
    71                   graph[EM, E] = newWeight;
    72                   prevPath[EM, E] = EC;
    73                 }
    74               }
    75             }
    76 
    77           int EP = S + N; //End point.
    78           int newT = int.MaxValue / 2;
    79           for (int EC = S + 1; EC < N; EC++) { //Nodes connected to EP O(N)
    80             int newWeight = graph[SM == 0 ? 1 : 0, EC] + weights[SM == 0 ? 1 : 0, EC, EP];
    81             if (newWeight < newT) {
    82               newT = newWeight;
    83               prevPath[SM, S] = EC;
    84             }
    85           }
    86 
    87           if (newT < T) {
     38        for (var i = 0; i < oneOptAssignment.Length; ++i) {
     39          var newAssignment = (BinaryVector)oneOptAssignment.Clone();
     40          newAssignment[i] = !newAssignment[i];
     41          int newT = CFSAP.EvaluateAssignment(order, newAssignment, processingTimes, setupTimes);
     42          if (newT < T) { // new best solution has been found
     43            oneOptAssignment = newAssignment;
    8844            T = newT;
    89             optimalAssignment = MakeAssignement(S, SM, prevPath, order);
     45            foundBetterSolution = true;
     46            break;
    9047          }
    9148        }
    9249
    93       //Omitted solutions
    94       for (int machine = 0; machine < 2; machine++) {
    95         int[] assignment = Enumerable.Repeat(machine, N).ToArray();
    96         int newT = CFSAP.EvaluateAssignement(order, assignment, processingTimes, setupTimes);
    97         if (newT < T) { //New best solution has been found
    98           T = newT;
    99           optimalAssignment = assignment;
    100         }
     50        if (!foundBetterSolution)
     51          return oneOptAssignment;
    10152      }
    102 
    103       return optimalAssignment;
    104     }
    105 
    106     private static int[] MakeAssignement(int start, int startMach, int[,] prevPath, Permutation order) {
    107       var N = order.Length;
    108       int[] assignment = Enumerable.Repeat(-1, N).ToArray();
    109       var inverseOrder = new int[N];
    110 
    111       for (int i = 0; i < N; i++)
    112         inverseOrder[order[i]] = i;
    113 
    114       int end = start + N;
    115 
    116       int currMach = startMach;
    117       int currNode = start;
    118       while (true) {
    119         assignment[inverseOrder[currNode]] = currMach;
    120         currNode = prevPath[currMach, currNode];
    121         currMach = currMach == 0 ? 1 : 0;
    122         if (currNode == start)
    123           break;
    124       }
    125 
    126       currMach = startMach;
    127       for (int i = 0; i < N; i++) {
    128         if (assignment[inverseOrder[i]] != -1)
    129           currMach = currMach == 0 ? 1 : 0;
    130         else
    131           assignment[inverseOrder[i]] = currMach;
    132       }
    133 
    134       return assignment;
    13553    }
    13654  }
Note: See TracChangeset for help on using the changeset viewer.