1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
29 | using HeuristicLab.Parameters;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 | using HeuristicLab.Problems.GeneralizedQuadraticAssignment.Common;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment {
|
---|
34 | [Item("GQAPPathRelinking", "Operator that performs path relinking between two solutions. It is described in Mateus, G., Resende, M., and Silva, R. 2011. GRASP with path-relinking for the generalized quadratic assignment problem. Journal of Heuristics 17, Springer Netherlands, pp. 527-565.")]
|
---|
35 | [StorableClass]
|
---|
36 | public class GQAPPathRelinking : GQAPCrossover, IQualitiesAwareGQAPOperator, IDemandsAwareGQAPOperator, ICapacitiesAwareGQAPOperator,
|
---|
37 | IWeightsAwareGQAPOperator, IDistancesAwareGQAPOperator, IInstallationCostsAwareGQAPOperator, ITransportationCostsAwareGQAPOperator,
|
---|
38 | IOverbookedCapacityPenaltyAwareGQAPOperator {
|
---|
39 |
|
---|
40 | public ILookupParameter<BoolValue> MaximizationParameter {
|
---|
41 | get { return (ILookupParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
42 | }
|
---|
43 | public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
|
---|
44 | get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
45 | }
|
---|
46 | public IScopeTreeLookupParameter<DoubleValue> FlowDistanceQualityParameter {
|
---|
47 | get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["FlowDistanceQuality"]; }
|
---|
48 | }
|
---|
49 | public IScopeTreeLookupParameter<DoubleValue> InstallationQualityParameter {
|
---|
50 | get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["InstallationQuality"]; }
|
---|
51 | }
|
---|
52 | public IScopeTreeLookupParameter<DoubleValue> OverbookedCapacityParameter {
|
---|
53 | get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["OverbookedCapacity"]; }
|
---|
54 | }
|
---|
55 | public ILookupParameter<DoubleArray> DemandsParameter {
|
---|
56 | get { return (ILookupParameter<DoubleArray>)Parameters["Demands"]; }
|
---|
57 | }
|
---|
58 | public ILookupParameter<DoubleArray> CapacitiesParameter {
|
---|
59 | get { return (ILookupParameter<DoubleArray>)Parameters["Capacities"]; }
|
---|
60 | }
|
---|
61 | public ILookupParameter<DoubleMatrix> WeightsParameter {
|
---|
62 | get { return (ILookupParameter<DoubleMatrix>)Parameters["Weights"]; }
|
---|
63 | }
|
---|
64 | public ILookupParameter<DoubleMatrix> DistancesParameter {
|
---|
65 | get { return (ILookupParameter<DoubleMatrix>)Parameters["Distances"]; }
|
---|
66 | }
|
---|
67 | public ILookupParameter<DoubleMatrix> InstallationCostsParameter {
|
---|
68 | get { return (ILookupParameter<DoubleMatrix>)Parameters["InstallationCosts"]; }
|
---|
69 | }
|
---|
70 | public IValueLookupParameter<DoubleValue> TransportationCostsParameter {
|
---|
71 | get { return (IValueLookupParameter<DoubleValue>)Parameters["TransportationCosts"]; }
|
---|
72 | }
|
---|
73 | public IValueLookupParameter<DoubleValue> OverbookedCapacityPenaltyParameter {
|
---|
74 | get { return (IValueLookupParameter<DoubleValue>)Parameters["OverbookedCapacityPenalty"]; }
|
---|
75 | }
|
---|
76 |
|
---|
77 | public IValueParameter<PercentValue> CandidateSizeFactorParameter {
|
---|
78 | get { return (IValueParameter<PercentValue>)Parameters["CandidateSizeFactor"]; }
|
---|
79 | }
|
---|
80 |
|
---|
81 | [StorableConstructor]
|
---|
82 | protected GQAPPathRelinking(bool deserializing) : base(deserializing) { }
|
---|
83 | protected GQAPPathRelinking(GQAPPathRelinking original, Cloner cloner) : base(original, cloner) { }
|
---|
84 | public GQAPPathRelinking()
|
---|
85 | : base() {
|
---|
86 | Parameters.Add(new LookupParameter<BoolValue>("Maximization", GeneralizedQuadraticAssignmentProblem.MaximizationDescription));
|
---|
87 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", GQAPEvaluator.QualityDescription));
|
---|
88 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("FlowDistanceQuality", GQAPEvaluator.FlowDistanceQualityDescription));
|
---|
89 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("InstallationQuality", GQAPEvaluator.InstallationQualityDescription));
|
---|
90 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("OverbookedCapacity", GQAPEvaluator.OverbookedCapacityDescription));
|
---|
91 | Parameters.Add(new LookupParameter<DoubleArray>("Demands", GeneralizedQuadraticAssignmentProblem.DemandsDescription));
|
---|
92 | Parameters.Add(new LookupParameter<DoubleArray>("Capacities", GeneralizedQuadraticAssignmentProblem.CapacitiesDescription));
|
---|
93 | Parameters.Add(new LookupParameter<DoubleMatrix>("Weights", GeneralizedQuadraticAssignmentProblem.WeightsDescription));
|
---|
94 | Parameters.Add(new LookupParameter<DoubleMatrix>("Distances", GeneralizedQuadraticAssignmentProblem.DistancesDescription));
|
---|
95 | Parameters.Add(new LookupParameter<DoubleMatrix>("InstallationCosts", GeneralizedQuadraticAssignmentProblem.InstallationCostsDescription));
|
---|
96 | Parameters.Add(new ValueLookupParameter<DoubleValue>("TransportationCosts", GeneralizedQuadraticAssignmentProblem.TransportationCostsDescription));
|
---|
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)));
|
---|
99 | }
|
---|
100 |
|
---|
101 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
102 | return new GQAPPathRelinking(this, cloner);
|
---|
103 | }
|
---|
104 |
|
---|
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) {
|
---|
109 | if (random == null) throw new ArgumentNullException("random", "No IRandom provider is given.");
|
---|
110 | if (parents == null || !parents.Any()) throw new ArgumentException("No parents given for path relinking.", "parents");
|
---|
111 | if (parents.Length != 2) throw new ArgumentException(String.Format("Two parents were expected for path relinking, but {0} was/were given.", parents.Length.ToString()), "parents");
|
---|
112 | if (parents[0].Length != parents[1].Length) throw new ArgumentException("The length of the parents is not equal.", "parents");
|
---|
113 | if (qualities == null || qualities.Length == 0) throw new ArgumentException("The qualities are not given.", "qualities");
|
---|
114 | if (qualities.Length != parents.Length) throw new ArgumentException(String.Format("There are a different number of parents ({0}) than quality values ({1})", parents.Length.ToString(), qualities.Length.ToString()));
|
---|
115 |
|
---|
116 | var source = parents[0];
|
---|
117 | var target = parents[1];
|
---|
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 |
|
---|
132 | var pi_prime = (IntegerVector)source.Clone();
|
---|
133 | var fix = new HashSet<int>();
|
---|
134 | var nonFix = new HashSet<int>(Enumerable.Range(0, demands.Length));
|
---|
135 | var phi = new HashSet<int>(IntegerVectorEqualityComparer.GetDifferingIndices(pi_prime, target));
|
---|
136 |
|
---|
137 | while (phi.Any()) {
|
---|
138 | var B = new List<GQAPSolution>();
|
---|
139 | foreach (var v in phi) {
|
---|
140 | int oldLocation = pi_prime[v];
|
---|
141 | pi_prime[v] = target[v];
|
---|
142 | var pi2 = makeFeasible(random, pi_prime, v, nonFix, demands, capacities);
|
---|
143 | pi_prime[v] = oldLocation;
|
---|
144 |
|
---|
145 | double currentFDQ, currentIQ, currentOC;
|
---|
146 | GQAPEvaluator.Evaluate(pi2, weights, distances, installationCosts, demands, capacities,
|
---|
147 | out currentFDQ, out currentIQ, out currentOC);
|
---|
148 |
|
---|
149 | if (currentOC <= 0.0) {
|
---|
150 | var quality = GQAPEvaluator.GetCombinedQuality(currentFDQ, currentIQ, currentOC,
|
---|
151 | transportationCosts.Value, overbookedCapacityPenalty.Value);
|
---|
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 | }
|
---|
168 | }
|
---|
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);
|
---|
180 | }
|
---|
181 | }
|
---|
182 | }
|
---|
183 | if (B.Any()) {
|
---|
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);
|
---|
190 | fix.Add(i);
|
---|
191 | nonFix.Remove(i);
|
---|
192 | pi_prime = pi.Assignment;
|
---|
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));
|
---|
202 | }
|
---|
203 |
|
---|
204 | return result;
|
---|
205 | }
|
---|
206 |
|
---|
207 | protected override IntegerVector Cross(IRandom random, ItemArray<IntegerVector> parents) {
|
---|
208 | return Apply(random, parents, MaximizationParameter.ActualValue, QualityParameter.ActualValue, FlowDistanceQualityParameter.ActualValue,
|
---|
209 | InstallationQualityParameter.ActualValue, OverbookedCapacityParameter.ActualValue, DemandsParameter.ActualValue,
|
---|
210 | CapacitiesParameter.ActualValue, WeightsParameter.ActualValue, DistancesParameter.ActualValue,
|
---|
211 | InstallationCostsParameter.ActualValue, TransportationCostsParameter.ActualValue,
|
---|
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;
|
---|
250 | }
|
---|
251 | }
|
---|
252 | }
|
---|