Changeset 15490 for branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Operators/Crossovers
- Timestamp:
- 12/04/17 23:00:03 (7 years ago)
- Location:
- branches/GeneralizedQAP
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/GeneralizedQAP
- Property svn:ignore
-
old new 2 2 TestResults 3 3 *.user 4 .vs
-
- Property svn:ignore
-
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Operators/Crossovers/CordeauCrossover.cs
r7970 r15490 21 21 22 22 using System; 23 using System.Linq; 23 24 using HeuristicLab.Common; 24 25 using HeuristicLab.Core; … … 27 28 using HeuristicLab.Parameters; 28 29 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 30 using HeuristicLab.Random; 29 31 30 32 namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment { … … 74 76 public IValueLookupParameter<IGQAPEvaluator> EvaluatorParameter { 75 77 get { return (IValueLookupParameter<IGQAPEvaluator>)Parameters["Evaluator"]; } 78 } 79 public ILookupParameter<IntValue> EvaluatedSolutionsParameter { 80 get { return (ILookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; } 76 81 } 77 82 … … 96 101 Parameters.Add(new ValueLookupParameter<DoubleValue>("ExpectedRandomQuality", GeneralizedQuadraticAssignmentProblem.ExpectedRandomQualityDescription)); 97 102 Parameters.Add(new ValueLookupParameter<IGQAPEvaluator>("Evaluator", "The evaluator used to evaluate solutions.")); 103 Parameters.Add(new LookupParameter<IntValue>("EvaluatedSolutions", "The number of evaluated solutions.")); 98 104 } 99 105 … … 107 113 DoubleMatrix weights, DoubleMatrix distances, DoubleMatrix installationCosts, 108 114 DoubleArray demands, DoubleArray capacities, 109 double transportationCosts, double expectedRandomQuality, IGQAPEvaluator evaluator) { 110 var mediana = Inizialize(distances); 115 double transportationCosts, double expectedRandomQuality, IGQAPEvaluator evaluator, IntValue evaluatedSolutions) { 116 var medianDistances = Enumerable.Range(0, distances.Rows).Select(x => distances.GetRow(x).Median()).ToArray(); 117 111 118 int m = capacities.Length; 112 119 int n = demands.Length; 113 int i, j, k, ik1, ik2;114 int control;115 bool nofound = false, onefound = false;116 double fbest;117 IntegerVector son = new IntegerVector(parent1.Length),result = null;120 121 bool onefound = false; 122 double fbest, fbestAttempt = maximization.Value ? double.MinValue : double.MaxValue; 123 IntegerVector bestAttempt = null; 124 IntegerVector result = null; 118 125 119 126 fbest = quality1.Value; … … 126 133 } 127 134 var cap = new double[m]; 128 for (i = 0; i < m; i++) { 129 130 for (j = 0; j < m; j++) cap[j] = 0; 131 132 for (k = 0; k < n; k++) { 133 son[k] = -1; 134 ik1 = parent1[k]; 135 ik2 = parent2[k]; 136 if (distances[i, ik1] < mediana[i]) { 137 son[k] = ik1; 138 cap[ik1] += demands[k]; 139 } else if (distances[i, ik2] > mediana[i]) { 140 son[k] = ik2; 141 cap[ik2] += demands[k]; 142 } 143 } 144 k = 0; 145 nofound = false; 146 while (k < n && !nofound) { 147 if (son[k] < 0) { 148 control = 0; 149 do { 150 j = random.Next(m); 151 control++; 152 } 153 while (cap[j] + demands[k] > capacities[j] && control < 3 * m); 154 if (cap[j] + demands[k] <= capacities[j]) { 155 son[k] = j; 156 cap[j] += demands[k]; 157 } else { 158 nofound = true; 159 } 160 } 161 k++; 162 } 163 if (!nofound) { 164 double sonQual = evaluator.Evaluate(son, weights, distances, installationCosts, demands, capacities, transportationCosts, expectedRandomQuality); 165 if (sonQual < fbest) { 166 fbest = sonQual; 167 result = (IntegerVector)son.Clone(); 135 for (var i = 0; i < m; i++) { 136 int unassigned; 137 Array.Clear(cap, 0, m); 138 var child = Merge(parent1, parent2, distances, demands, medianDistances, m, n, i, cap, out unassigned); 139 if (unassigned > 0) 140 TryRandomAssignment(random, demands, capacities, m, n, cap, child, ref unassigned); 141 if (unassigned == 0) { 142 var childFit = evaluator.Evaluate(child, weights, distances, installationCosts, demands, capacities, transportationCosts, expectedRandomQuality); 143 evaluatedSolutions.Value++; 144 if (maximization.Value && childFit >= fbest 145 || !maximization.Value && childFit <= fbest) { 146 fbest = childFit; 147 result = child; 168 148 onefound = true; 169 149 } 170 }/* else { 171 abortedmerge++; 172 printf("aborted merge %8d\n", abortedmerge); 173 } */ 174 } 175 if (!onefound && !nofound) { 176 i = random.Next(m); 177 for (j = 0; j < m; j++) { 178 cap[j] = 0.0; 179 } 180 for (k = 0; k < n; k++) { 181 son[k] = -1; 182 ik1 = parent1[k]; 183 ik2 = parent2[k]; 184 if (distances[i, ik1] < mediana[i]) { 185 son[k] = ik1; 186 cap[ik1] += demands[k]; 187 } else if (distances[i, ik2] > mediana[i]) { 188 son[k] = ik2; 189 cap[ik2] += demands[k]; 150 if (!onefound && (maximization.Value && fbestAttempt < childFit || !maximization.Value && fbestAttempt > childFit)) { 151 bestAttempt = child; 152 fbestAttempt = childFit; 190 153 } 191 154 } 192 for (k = 0; k < n; k++) { 193 if (son[k] < 0) { 194 j = random.Next(m); 195 son[k] = j; 196 cap[j] += demands[k]; 197 } 198 } 199 if (result == null) { 200 result = (IntegerVector)son.Clone(); 155 } 156 157 if (!onefound) { 158 var i = random.Next(m); 159 int unassigned; 160 Array.Clear(cap, 0, m); 161 var child = Merge(parent1, parent2, distances, demands, medianDistances, m, n, i, cap, out unassigned); 162 RandomAssignment(random, demands, capacities, m, n, cap, child, ref unassigned); 163 164 var childFit = evaluator.Evaluate(child, weights, distances, installationCosts, demands, capacities, transportationCosts, expectedRandomQuality); 165 evaluatedSolutions.Value++; 166 if (childFit < fbest) { 167 fbest = childFit; 168 result = child; 201 169 onefound = true; 202 } else { 203 double sonQual = evaluator.Evaluate(son, weights, distances, installationCosts, demands, capacities, transportationCosts, expectedRandomQuality); 204 if (sonQual < fbest) { 205 fbest = sonQual; 206 result = (IntegerVector)son.Clone(); 207 onefound = true; 208 } 209 } 170 } 171 172 if (!onefound && (maximization.Value && fbestAttempt < childFit || !maximization.Value && fbestAttempt > childFit)) { 173 bestAttempt = child; 174 fbestAttempt = childFit; 175 } 176 } 210 177 /*if (tabufix(&son, 0.5 * sqrt(n * m), round(n * m * log10(n)), &tabufix_it)) { 211 178 solution_cost(&son); … … 216 183 merge_fixed++; 217 184 }*/ 218 } 219 if (result == null) throw new InvalidOperationException("Child could not be created."); 220 return result; 185 return result ?? bestAttempt; 186 } 187 188 private static IntegerVector Merge(IntegerVector p1, IntegerVector p2, 189 DoubleMatrix distances, DoubleArray demands, double[] mediana, 190 int m, int n, int i, double[] cap, out int unassigned) { 191 unassigned = n; 192 var child = new IntegerVector(n); 193 for (var k = 0; k < n; k++) { 194 child[k] = -1; 195 var ik1 = p1[k]; 196 var ik2 = p2[k]; 197 if (distances[i, ik1] < mediana[i]) { 198 child[k] = ik1; 199 cap[ik1] += demands[k]; 200 unassigned--; 201 } else if (distances[i, ik2] > mediana[i]) { 202 child[k] = ik2; 203 cap[ik2] += demands[k]; 204 unassigned--; 205 }; 206 } 207 return child; 208 } 209 210 private static bool TryRandomAssignment(IRandom random, DoubleArray demands, DoubleArray capacities, int m, int n, double[] cap, IntegerVector child, ref int unassigned) { 211 var unbiasedOrder = Enumerable.Range(0, n).Shuffle(random).ToList(); 212 for (var idx = 0; idx < n; idx++) { 213 var k = unbiasedOrder[idx]; 214 if (child[k] < 0) { 215 var feasibleInserts = Enumerable.Range(0, m) 216 .Select((v, i) => new { Pos = i, Slack = capacities[i] - cap[i] }) 217 .Where(x => x.Slack >= demands[k]).ToList(); 218 if (feasibleInserts.Count == 0) return false; 219 var j = feasibleInserts.SampleRandom(random).Pos; 220 child[k] = j; 221 cap[j] += demands[k]; 222 unassigned--; 223 } 224 } 225 return true; 226 } 227 228 private static void RandomAssignment(IRandom random, DoubleArray demands, DoubleArray capacities, int m, int n, double[] cap, IntegerVector child, ref int unassigned) { 229 for (var k = 0; k < n; k++) { 230 if (child[k] < 0) { 231 var j = random.Next(m); 232 child[k] = j; 233 cap[j] += demands[k]; 234 unassigned--; 235 } 236 } 221 237 } 222 238 … … 232 248 DemandsParameter.ActualValue, CapacitiesParameter.ActualValue, 233 249 TransportationCostsParameter.ActualValue.Value, ExpectedRandomQualityParameter.ActualValue.Value, 234 EvaluatorParameter.ActualValue); 235 } 236 237 private static double[] Inizialize(DoubleMatrix distances) { 238 int i, j; 239 double mdi; 240 double delta; 241 int ilow, ihigh; 242 bool balance; 243 int operation; 244 int count; 245 int m = distances.Rows; 246 247 double[] mediana = new double[distances.Rows]; 248 249 for (i = 0; i < m; i++) { 250 mdi = 0; 251 for (j = 0; j < m; j++) { 252 mdi += distances[i, j]; 253 } 254 mediana[i] = mdi / m; 255 balance = false; 256 delta = 1; 257 operation = 0; 258 count = 0; 259 while (!balance && count < 200) { 260 ilow = 0; 261 ihigh = 0; 262 count++; 263 for (j = 0; j < m; j++) { 264 if (distances[i, j] < mediana[i]) ilow++; 265 else if (distances[i, j] > mediana[i]) ihigh++; 266 } 267 if (ilow > ((m + 1) / 2)) { 268 mediana[i] = mediana[i] - delta; 269 if (operation == 1) delta = delta / 2; 270 operation = -1; 271 } else if (ihigh > ((m + 1) / 2)) { 272 mediana[i] = mediana[i] + delta; 273 if (operation == -1) delta = delta / 2; 274 operation = 1; 275 } else balance = true; 276 } 277 } 278 return mediana; 250 EvaluatorParameter.ActualValue, EvaluatedSolutionsParameter.ActualValue); 279 251 } 280 252 } -
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Operators/Crossovers/GQAPPathRelinking.cs
r7970 r15490 29 29 using HeuristicLab.Parameters; 30 30 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 31 using HeuristicLab.Problems.GeneralizedQuadraticAssignment.Common;32 31 using HeuristicLab.Random; 33 32 … … 138 137 var fix = new HashSet<int>(); 139 138 var nonFix = new HashSet<int>(Enumerable.Range(0, demands.Length)); 140 var phi = new HashSet<int>( IntegerVectorEqualityComparer.GetDifferingIndices(pi_prime, target));139 var phi = new HashSet<int>((pi_prime.GetDifferingIndices(target))); 141 140 142 141 while (phi.Any()) { … … 162 161 for (int i = 0; i < B.Count; i++) { 163 162 if (IsBetter(maximization.Value, solution.Quality.Value, B[i].Quality.Value)) { 164 var similarity = IntegerVectorEqualityComparer.GetSimilarity(solution.Assignment, B[i].Assignment);163 var similarity = HammingSimilarityCalculator.CalculateSimilarity(solution.Assignment, B[i].Assignment); 165 164 if (similarity == 1.0) { 166 165 mostSimilarIndex = -1; … … 177 176 bool contains = false; 178 177 foreach (var b in B) { 179 if (! IntegerVectorEqualityComparer.GetDifferingIndices(solution.Assignment,b.Assignment).Any()) {178 if (!solution.Assignment.GetDifferingIndices(b.Assignment).Any()) { 180 179 contains = true; 181 180 break; … … 190 189 if (maximization.Value) pi = B.SampleProportional(random, 1, B.Select(x => x.Quality.Value), false).First(); 191 190 else pi = B.SampleProportional(random, 1, B.Select(x => 1.0 / x.Quality.Value), false).First(); 192 var diff = IntegerVectorEqualityComparer.GetDifferingIndices(pi.Assignment,target);191 var diff = pi.Assignment.GetDifferingIndices(target); 193 192 var I = phi.Except(diff); 194 193 var i = I.SampleRandom(random); … … 204 203 } 205 204 } else return result; 206 phi = new HashSet<int>( IntegerVectorEqualityComparer.GetDifferingIndices(pi_prime,target));205 phi = new HashSet<int>(pi_prime.GetDifferingIndices(target)); 207 206 } 208 207
Note: See TracChangeset
for help on using the changeset viewer.