- Timestamp:
- 01/31/12 15:17:16 (13 years ago)
- Location:
- branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3
- Files:
-
- 2 deleted
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Analyzers/BestGQAPSolutionAnalyzer.cs
r7423 r7432 155 155 int bestIndex; 156 156 var tmp = qualities.Select((x, index) => new { Index = index, Value = x.Value }); 157 if (maximization) bestIndex = tmp. SelectMax(x => x.Value).Index;158 else bestIndex = tmp. SelectMin(x => x.Value).Index;157 if (maximization) bestIndex = tmp.ChooseMax(x => x.Value).Index; 158 else bestIndex = tmp.ChooseMin(x => x.Value).Index; 159 159 160 160 if (bestKnownQuality == null || HasSolutionImproved(bestKnownQuality.Value, qualities[bestIndex].Value, maximization)) { -
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/GQAPAssignment.cs
r7418 r7432 28 28 29 29 namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment { 30 [Item("Generalized QAP Assignment", "Represents a solution to the generalized QAP.")]30 [Item("Generalized QAP Assignment", "Represents the solution as well as the problem parameters of a GQAP instance.")] 31 31 [StorableClass] 32 32 public sealed class GQAPAssignment : Item, INotifyPropertyChanged { -
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/GQAPAssignmentArchive.cs
r7418 r7432 145 145 private GQAPAssignmentArchive(GQAPAssignmentArchive original, Cloner cloner) 146 146 : base(original, cloner) { 147 solutions = cloner.Clone(original.solutions); 147 148 equipmentNames = cloner.Clone(original.equipmentNames); 148 149 locationNames = cloner.Clone(original.locationNames); -
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/GeneralizedQuadraticAssignmentProblem.cs
r7423 r7432 142 142 private GeneralizedQuadraticAssignmentProblem(GeneralizedQuadraticAssignmentProblem original, Cloner cloner) 143 143 : base(original, cloner) { 144 AttachEventHandlers();144 RegisterEventHandlers(); 145 145 } 146 146 public GeneralizedQuadraticAssignmentProblem() … … 186 186 187 187 InitializeOperators(); 188 AttachEventHandlers();188 RegisterEventHandlers(); 189 189 } 190 190 … … 220 220 [StorableHook(HookType.AfterDeserialization)] 221 221 private void AfterDeserialization() { 222 AttachEventHandlers();223 } 224 225 private void AttachEventHandlers() {222 RegisterEventHandlers(); 223 } 224 225 private void RegisterEventHandlers() { 226 226 Evaluator.QualityParameter.ActualNameChanged += new System.EventHandler(Evaluator_QualityParameter_ActualNameChanged); 227 227 SolutionCreator.AssignmentParameter.ActualNameChanged += new EventHandler(SolutionCreator_IntegerVectorParameter_ActualNameChanged); -
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/HeuristicLab.Problems.GeneralizedQuadraticAssignment-3.3.csproj
r7419 r7432 97 97 <Compile Include="GeneralizedQuadraticAssignmentProblem.cs" /> 98 98 <Compile Include="GQAPAssignmentArchive.cs" /> 99 <Compile Include="GQAPIntegerVectorProximityCalculator.cs" />100 99 <Compile Include="GQAPSolution.cs" /> 101 100 <Compile Include="Interfaces\IQualitiesAwareGQAPOperator.cs" /> -
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Operators/DemandEquivalentSwapEquipmentManipluator.cs
r7419 r7432 79 79 assignment[equipment2] = location1; 80 80 groupedLocations[location2].Remove(equipment2); 81 bool selected; 82 equipment2 = groupedLocations[location2].ChooseRandomOrDefault(random, out selected); 83 if (!selected) break; 81 if (!groupedLocations[location2].Any()) break; 82 equipment2 = groupedLocations[location2].ChooseUniformRandom(random); 84 83 } 85 84 } -
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Operators/GQAPPathRelinking.cs
r7425 r7432 73 73 public IValueLookupParameter<DoubleValue> OverbookedCapacityPenaltyParameter { 74 74 get { return (IValueLookupParameter<DoubleValue>)Parameters["OverbookedCapacityPenalty"]; } 75 } 76 77 public IValueParameter<PercentValue> CandidateSizeFactorParameter { 78 get { return (IValueParameter<PercentValue>)Parameters["CandidateSizeFactor"]; } 75 79 } 76 80 … … 92 96 Parameters.Add(new ValueLookupParameter<DoubleValue>("TransportationCosts", GeneralizedQuadraticAssignmentProblem.TransportationCostsDescription)); 93 97 Parameters.Add(new ValueLookupParameter<DoubleValue>("OverbookedCapacityPenalty", GeneralizedQuadraticAssignmentProblem.OverbookedCapacityPenaltyDescription)); 98 Parameters.Add(new ValueParameter<PercentValue>("CandidateSizeFactor", "(η) Determines the size of the set of feasible moves in each path-relinking step relative to the maximum size. A value of 50% means that only half of all possible moves are considered each step.", new PercentValue(0.5))); 94 99 } 95 100 … … 98 103 } 99 104 100 public static IntegerVector Apply(IRandom random, ItemArray<IntegerVector> parents, ItemArray<DoubleValue> qualities, DoubleArray demands, 101 DoubleArray capacities, DoubleMatrix weights, DoubleMatrix distances, DoubleMatrix installationCosts, DoubleValue transportationCosts, 102 DoubleValue overbookedCapacityPenalty) { 105 public static IntegerVector Apply(IRandom random, ItemArray<IntegerVector> parents, BoolValue maximization, ItemArray<DoubleValue> qualities, 106 ItemArray<DoubleValue> flowDistanceQualities, ItemArray<DoubleValue> installationQualities, ItemArray<DoubleValue> overbookedCapacities, 107 DoubleArray demands, DoubleArray capacities, DoubleMatrix weights, DoubleMatrix distances, DoubleMatrix installationCosts, 108 DoubleValue transportationCosts, DoubleValue overbookedCapacityPenalty, DoubleValue candidateSizeFactor) { 103 109 if (random == null) throw new ArgumentNullException("random", "No IRandom provider is given."); 104 110 if (parents == null || !parents.Any()) throw new ArgumentException("No parents given for path relinking.", "parents"); … … 110 116 var source = parents[0]; 111 117 var target = parents[1]; 112 113 var nu = 1.0; 118 IntegerVector result; 119 double resultQuality, resultFDQ, resultIQ, resultOC; 120 121 int betterParent = 1; 122 if ((maximization.Value && qualities[0].Value >= qualities[1].Value) 123 || (!maximization.Value && qualities[0].Value <= qualities[1].Value)) 124 betterParent = 0; 125 126 result = (IntegerVector)parents[betterParent].Clone(); 127 resultQuality = qualities[betterParent].Value; 128 resultFDQ = flowDistanceQualities[betterParent].Value; 129 resultIQ = installationQualities[betterParent].Value; 130 resultOC = overbookedCapacities[betterParent].Value; 131 114 132 var pi_prime = (IntegerVector)source.Clone(); 115 133 var fix = new HashSet<int>(); 116 134 var nonFix = new HashSet<int>(Enumerable.Range(0, demands.Length)); 117 var phi = new HashSet<int>( GQAPIntegerVectorProximityCalculator.GetDifference(pi_prime, target));135 var phi = new HashSet<int>(IntegerVectorEqualityComparer.GetDifferingIndices(pi_prime, target)); 118 136 119 137 while (phi.Any()) { 120 var B = new HashSet<GQAPSolution>(new GQAPSolutionStructuralEqualityComparer());138 var B = new List<GQAPSolution>(); 121 139 foreach (var v in phi) { 140 int oldLocation = pi_prime[v]; 122 141 pi_prime[v] = target[v]; 123 var pi2 = makeFeasible(pi_prime, v); 124 125 double flowDistanceQuality, installationQuality, overbookedCapacity; 142 var pi2 = makeFeasible(random, pi_prime, v, nonFix, demands, capacities); 143 pi_prime[v] = oldLocation; 144 145 double currentFDQ, currentIQ, currentOC; 126 146 GQAPEvaluator.Evaluate(pi2, weights, distances, installationCosts, demands, capacities, 127 out flowDistanceQuality, out installationQuality, out overbookedCapacity);128 129 if ( overbookedCapacity<= 0.0) {130 var quality = GQAPEvaluator.GetCombinedQuality( flowDistanceQuality, installationQuality, overbookedCapacity,147 out currentFDQ, out currentIQ, out currentOC); 148 149 if (currentOC <= 0.0) { 150 var quality = GQAPEvaluator.GetCombinedQuality(currentFDQ, currentIQ, currentOC, 131 151 transportationCosts.Value, overbookedCapacityPenalty.Value); 132 var solution = new GQAPSolution(pi2, new DoubleValue(quality), new DoubleValue(flowDistanceQuality), new DoubleValue(installationQuality), new DoubleValue(overbookedCapacity)); 133 if (B.Count >= nu * phi.Count) { 134 if (!B.Contains(solution) && quality <= B.Select(x => x.Quality.Value).Max()) { 135 // TODO: replace most similar element in B with worse cost 152 var solution = new GQAPSolution(pi2, new DoubleValue(quality), new DoubleValue(currentFDQ), 153 new DoubleValue(currentIQ), new DoubleValue(currentOC)); 154 if (B.Count >= candidateSizeFactor.Value * phi.Count) { 155 int mostSimilarIndex = -1; 156 double mostSimilarValue = 1.0; 157 for (int i = 0; i < B.Count; i++) { 158 if (IsBetter(maximization.Value, solution.Quality.Value, B[i].Quality.Value)) { 159 var similarity = IntegerVectorEqualityComparer.GetSimilarity(solution.Assignment, B[i].Assignment); 160 if (similarity == 1.0) { 161 mostSimilarIndex = -1; 162 break; 163 } else if (similarity < mostSimilarValue) { 164 mostSimilarValue = similarity; 165 mostSimilarIndex = i; 166 } 167 } 136 168 } 137 } else if (!B.Contains(solution)) { 138 B.Add(solution); 169 if (mostSimilarIndex >= 0) 170 B[mostSimilarIndex] = solution; 171 } else { 172 bool contains = false; 173 foreach (var b in B) { 174 if (!IntegerVectorEqualityComparer.GetDifferingIndices(solution.Assignment, b.Assignment).Any()) { 175 contains = true; 176 break; 177 } 178 } 179 if (!contains) B.Add(solution); 139 180 } 140 181 } 141 182 } 142 183 if (B.Any()) { 143 var pi = B.ChooseRandom(random); 144 var diff = GQAPIntegerVectorProximityCalculator.GetDifference(pi.Assignment, target); 145 var I = phi.Except(phi.Intersect(diff)); 146 var i = I.ChooseRandom(random); 184 GQAPSolution pi; 185 if (maximization.Value) pi = B.ChooseProportionalRandom(random, 1, x => x.Quality.Value, false, true).First(); 186 else pi = B.ChooseProportionalRandom(random, 1, x => 1.0 / x.Quality.Value, false, true).First(); 187 var diff = IntegerVectorEqualityComparer.GetDifferingIndices(pi.Assignment, target); 188 var I = phi.Except(diff); 189 var i = I.ChooseUniformRandom(random); 147 190 fix.Add(i); 148 191 nonFix.Remove(i); 149 192 pi_prime = pi.Assignment; 150 // TODO 151 } 193 if (IsBetter(maximization.Value, pi.Quality.Value, resultQuality)) { 194 result = pi.Assignment; 195 resultQuality = pi.Quality.Value; 196 resultFDQ = pi.FlowDistanceQuality.Value; 197 resultIQ = pi.InstallationQuality.Value; 198 resultOC = pi.OverbookedCapacity.Value; 199 } 200 } else return result; 201 phi = new HashSet<int>(IntegerVectorEqualityComparer.GetDifferingIndices(pi_prime, target)); 152 202 } 153 203 154 return pi_prime;204 return result; 155 205 } 156 206 157 207 protected override IntegerVector Cross(IRandom random, ItemArray<IntegerVector> parents) { 158 return Apply(random, parents, QualityParameter.ActualValue, DemandsParameter.ActualValue, 208 return Apply(random, parents, MaximizationParameter.ActualValue, QualityParameter.ActualValue, FlowDistanceQualityParameter.ActualValue, 209 InstallationQualityParameter.ActualValue, OverbookedCapacityParameter.ActualValue, DemandsParameter.ActualValue, 159 210 CapacitiesParameter.ActualValue, WeightsParameter.ActualValue, DistancesParameter.ActualValue, 160 211 InstallationCostsParameter.ActualValue, TransportationCostsParameter.ActualValue, 161 OverbookedCapacityPenaltyParameter.ActualValue); 162 } 163 164 private static IntegerVector makeFeasible(IntegerVector assignment, int equipment) { 165 // TODO: implement 166 return assignment; 212 OverbookedCapacityPenaltyParameter.ActualValue, CandidateSizeFactorParameter.Value); 213 } 214 215 private static IntegerVector makeFeasible(IRandom random, IntegerVector assignment, int equipment, HashSet<int> nonFix, DoubleArray demands, DoubleArray capacities, int maximumTries = 1000) { 216 int l = assignment[equipment]; 217 var slack = ComputeSlack(assignment, demands, capacities); 218 if (slack[l] >= 0) return (IntegerVector)assignment.Clone(); 219 220 IntegerVector result = null; 221 int k = 0; 222 while (k < maximumTries && slack[l] < 0) { 223 result = (IntegerVector)assignment.Clone(); 224 do { 225 var maxSlack = slack.Max(); 226 var T = nonFix.Where(x => x != equipment && assignment[x] == l && demands[x] <= maxSlack); 227 if (T.Any()) { 228 int i = T.ChooseProportionalRandom(random, 1, x => demands[x], false, true).First(); 229 var j = Enumerable.Range(0, capacities.Length).Where(x => slack[x] >= demands[i]).ChooseUniformRandom(random); 230 result[i] = j; 231 slack[j] -= demands[i]; 232 slack[l] += demands[i]; 233 } else break; 234 } while (slack[l] < 0); 235 k++; 236 } 237 return result; 238 } 239 240 private static DoubleArray ComputeSlack(IntegerVector assignment, DoubleArray demands, DoubleArray capacities) { 241 var slack = (DoubleArray)capacities.Clone(); 242 for (int i = 0; i < assignment.Length; i++) { 243 slack[assignment[i]] -= demands[i]; 244 } 245 return slack; 246 } 247 248 private static bool IsBetter(bool maximization, double quality, double otherQuality) { 249 return maximization && quality > otherQuality || !maximization && quality < otherQuality; 167 250 } 168 251 } -
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Operators/GreedyRandomizedSolutionCreator.cs
r7415 r7432 70 70 do { 71 71 if (L.Any() && random.NextDouble() < threshold) { 72 int l = L.Choose Random(random);72 int l = L.ChooseUniformRandom(random); 73 73 L.Remove(l); 74 74 CL.Add(l); … … 76 76 } 77 77 if (T.Any()) { 78 int f = T.Choose Random(random);78 int f = T.ChooseUniformRandom(random); 79 79 T.Remove(f); 80 80 F.Remove(f); 81 81 CF.Add(f); 82 int l = WithSlackGreaterOrEqual(CL, demands[f], slack).Choose Random(random);82 int l = WithSlackGreaterOrEqual(CL, demands[f], slack).ChooseUniformRandom(random); 83 83 assignment.Add(f, l); 84 84 slack[l] -= demands[f]; -
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Operators/QualitySimilarityMerger.cs
r7419 r7432 94 94 95 95 foreach (var candidate in candidates) { 96 double[] similarities = population.Select(x => GQAPIntegerVectorProximityCalculator.CalculateGenotypeSimilarity(x.Solution, candidate.Solution)).ToArray();96 double[] similarities = population.Select(x => IntegerVectorEqualityComparer.GetSimilarity(x.Solution, candidate.Solution)).ToArray(); 97 97 if (!similarities.Any()) { 98 98 population.Add(candidate); -
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Operators/RelocateEquipmentManipluator.cs
r7419 r7432 72 72 } 73 73 74 bool selected; 75 var overstuffedLocation = usedCapacities.Where(x => capacities[x.Key] < x.Value).ChooseRandomOrDefault(random, out selected); 76 if (selected) { 77 int equipment = groupedLocations[overstuffedLocation.Key].ChooseRandomOrDefault(random, out selected); 78 var freeLocations = usedCapacities.Where(x => capacities[x.Key] > x.Value).ToList(); 79 var location = freeLocations.Where(x => capacities[x.Key] > x.Value + demands[equipment]).ChooseRandomOrDefault(random, out selected); 80 if (selected) { 81 assignment[equipment] = location.Key; 82 } else { 83 location = freeLocations.ChooseRandomOrDefault(random, out selected); 84 if (selected) assignment[equipment] = location.Key; 85 else assignment[equipment] = random.Next(locations); 86 } 87 } else { 74 var overbookedLocations = usedCapacities.Where(x => capacities[x.Key] < x.Value); 75 var freeLocations = usedCapacities.Where(x => capacities[x.Key] > x.Value); 76 if (!overbookedLocations.Any() || !freeLocations.Any()) { // perform a random relocation 88 77 assignment[random.Next(assignment.Length)] = random.Next(locations); 78 return; 79 } 80 81 var sourceLocation = overbookedLocations.ChooseUniformRandom(random); 82 int equipmentToRelocate = groupedLocations[sourceLocation.Key].ChooseUniformRandom(random); 83 var bestFreeLocations = freeLocations.Where(x => capacities[x.Key] > x.Value + demands[equipmentToRelocate]); 84 85 if (bestFreeLocations.Any()) { // a feasible solution will still be feasible, an infeasible solution might become feasible 86 var selected = bestFreeLocations.ChooseUniformRandom(random); 87 assignment[equipmentToRelocate] = sourceLocation.Key; 88 } else { // the solution will become infeasible 89 sourceLocation = freeLocations.ChooseUniformRandom(random); 90 assignment[equipmentToRelocate] = sourceLocation.Key; 89 91 } 90 92 }
Note: See TracChangeset
for help on using the changeset viewer.