- Timestamp:
- 12/18/17 14:38:18 (6 years ago)
- File:
-
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
branches/CFSAP/HeuristicLab.Problems.Scheduling.CFSAP/3.3/OneOptAssignment.cs
r15493 r15533 23 23 using HeuristicLab.Core; 24 24 using HeuristicLab.Data; 25 using HeuristicLab.Encodings.BinaryVectorEncoding; 25 26 using HeuristicLab.Encodings.PermutationEncoding; 26 27 27 28 namespace HeuristicLab.Problems.Scheduling.CFSAP { 28 public static class O ptimalAssignment {29 public static class OneOptAssignment { 29 30 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); 32 34 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; 37 37 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; 88 44 T = newT; 89 optimalAssignment = MakeAssignement(S, SM, prevPath, order); 45 foundBetterSolution = true; 46 break; 90 47 } 91 48 } 92 49 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; 101 52 } 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 else131 assignment[inverseOrder[i]] = currMach;132 }133 134 return assignment;135 53 } 136 54 }
Note: See TracChangeset
for help on using the changeset viewer.