1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2018 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.Linq;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
28 | using HeuristicLab.Parameters;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 | using HeuristicLab.Random;
|
---|
31 | using HEAL.Attic;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment {
|
---|
34 | [Item("CordeauCrossover", "The merge procedure that is described in Cordeau, J.-F., Gaudioso, M., Laporte, G., Moccia, L. 2006. A memetic heuristic for the generalized quadratic assignment problem. INFORMS Journal on Computing, 18, pp. 433–443.")]
|
---|
35 | [StorableType("05D7FC4C-EF71-4118-9FF1-B8B71B501A99")]
|
---|
36 | public class CordeauCrossover : GQAPCrossover,
|
---|
37 | IQualitiesAwareGQAPOperator, IProblemInstanceAwareGQAPOperator {
|
---|
38 |
|
---|
39 | public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
|
---|
40 | get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
41 | }
|
---|
42 | public IScopeTreeLookupParameter<Evaluation> EvaluationParameter {
|
---|
43 | get { return (IScopeTreeLookupParameter<Evaluation>)Parameters["Evaluation"]; }
|
---|
44 | }
|
---|
45 | public ILookupParameter<IntValue> EvaluatedSolutionsParameter {
|
---|
46 | get { return (ILookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
|
---|
47 | }
|
---|
48 |
|
---|
49 | [StorableConstructor]
|
---|
50 | protected CordeauCrossover(StorableConstructorFlag _) : base(_) { }
|
---|
51 | protected CordeauCrossover(CordeauCrossover original, Cloner cloner)
|
---|
52 | : base(original, cloner) {
|
---|
53 | }
|
---|
54 | public CordeauCrossover()
|
---|
55 | : base() {
|
---|
56 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The quality of the parents", 1));
|
---|
57 | Parameters.Add(new ScopeTreeLookupParameter<Evaluation>("Evaluation", GQAP.EvaluationDescription, 1));
|
---|
58 | Parameters.Add(new LookupParameter<IntValue>("EvaluatedSolutions", "The number of evaluated solutions."));
|
---|
59 | }
|
---|
60 |
|
---|
61 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
62 | return new CordeauCrossover(this, cloner);
|
---|
63 | }
|
---|
64 |
|
---|
65 | public static IntegerVector Apply(IRandom random,
|
---|
66 | IntegerVector parent1, DoubleValue quality1,
|
---|
67 | IntegerVector parent2, DoubleValue quality2,
|
---|
68 | GQAPInstance problemInstance, IntValue evaluatedSolutions) {
|
---|
69 | var distances = problemInstance.Distances;
|
---|
70 | var capacities = problemInstance.Capacities;
|
---|
71 | var demands = problemInstance.Demands;
|
---|
72 |
|
---|
73 | var medianDistances = Enumerable.Range(0, distances.Rows).Select(x => distances.GetRow(x).Median()).ToArray();
|
---|
74 |
|
---|
75 | int m = capacities.Length;
|
---|
76 | int n = demands.Length;
|
---|
77 |
|
---|
78 | bool onefound = false;
|
---|
79 | double fbest, fbestAttempt = double.MaxValue;
|
---|
80 | IntegerVector bestAttempt = null;
|
---|
81 | IntegerVector result = null;
|
---|
82 |
|
---|
83 | fbest = quality1.Value;
|
---|
84 | if (quality1.Value > quality2.Value) {
|
---|
85 | var temp = parent1;
|
---|
86 | parent1 = parent2;
|
---|
87 | parent2 = temp;
|
---|
88 | fbest = quality2.Value;
|
---|
89 | }
|
---|
90 | var cap = new double[m];
|
---|
91 | for (var i = 0; i < m; i++) {
|
---|
92 | int unassigned;
|
---|
93 | Array.Clear(cap, 0, m);
|
---|
94 | var child = Merge(parent1, parent2, distances, demands, medianDistances, m, n, i, cap, out unassigned);
|
---|
95 | if (unassigned > 0)
|
---|
96 | TryRandomAssignment(random, demands, capacities, m, n, cap, child, ref unassigned);
|
---|
97 | if (unassigned == 0) {
|
---|
98 | var childFit = problemInstance.ToSingleObjective(problemInstance.Evaluate(child));
|
---|
99 | evaluatedSolutions.Value++;
|
---|
100 | if (childFit <= fbest) {
|
---|
101 | fbest = childFit;
|
---|
102 | result = child;
|
---|
103 | onefound = true;
|
---|
104 | }
|
---|
105 | if (!onefound && fbestAttempt > childFit) {
|
---|
106 | bestAttempt = child;
|
---|
107 | fbestAttempt = childFit;
|
---|
108 | }
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 | if (!onefound) {
|
---|
113 | var i = random.Next(m);
|
---|
114 | int unassigned;
|
---|
115 | Array.Clear(cap, 0, m);
|
---|
116 | var child = Merge(parent1, parent2, distances, demands, medianDistances, m, n, i, cap, out unassigned);
|
---|
117 | RandomAssignment(random, demands, capacities, m, n, cap, child, ref unassigned);
|
---|
118 |
|
---|
119 | var childFit = problemInstance.ToSingleObjective(problemInstance.Evaluate(child));
|
---|
120 | evaluatedSolutions.Value++;
|
---|
121 | if (childFit < fbest) {
|
---|
122 | fbest = childFit;
|
---|
123 | result = child;
|
---|
124 | onefound = true;
|
---|
125 | }
|
---|
126 |
|
---|
127 | if (!onefound && fbestAttempt > childFit) {
|
---|
128 | bestAttempt = child;
|
---|
129 | fbestAttempt = childFit;
|
---|
130 | }
|
---|
131 | }
|
---|
132 | /*if (tabufix(&son, 0.5 * sqrt(n * m), round(n * m * log10(n)), &tabufix_it)) {
|
---|
133 | solution_cost(&son);
|
---|
134 | if (son.cost < fbest) {
|
---|
135 | fbest = son.cost;
|
---|
136 | *sptr = son;
|
---|
137 | onefound = TRUE;
|
---|
138 | merge_fixed++;
|
---|
139 | }*/
|
---|
140 | return result ?? bestAttempt;
|
---|
141 | }
|
---|
142 |
|
---|
143 | private static IntegerVector Merge(IntegerVector p1, IntegerVector p2,
|
---|
144 | DoubleMatrix distances, DoubleArray demands, double[] mediana,
|
---|
145 | int m, int n, int i, double[] cap, out int unassigned) {
|
---|
146 | unassigned = n;
|
---|
147 | var child = new IntegerVector(n);
|
---|
148 | for (var k = 0; k < n; k++) {
|
---|
149 | child[k] = -1;
|
---|
150 | var ik1 = p1[k];
|
---|
151 | var ik2 = p2[k];
|
---|
152 | if (distances[i, ik1] < mediana[i]) {
|
---|
153 | child[k] = ik1;
|
---|
154 | cap[ik1] += demands[k];
|
---|
155 | unassigned--;
|
---|
156 | } else if (distances[i, ik2] > mediana[i]) {
|
---|
157 | child[k] = ik2;
|
---|
158 | cap[ik2] += demands[k];
|
---|
159 | unassigned--;
|
---|
160 | };
|
---|
161 | }
|
---|
162 | return child;
|
---|
163 | }
|
---|
164 |
|
---|
165 | private static bool TryRandomAssignment(IRandom random, DoubleArray demands, DoubleArray capacities, int m, int n, double[] cap, IntegerVector child, ref int unassigned) {
|
---|
166 | var unbiasedOrder = Enumerable.Range(0, n).Shuffle(random).ToList();
|
---|
167 | for (var idx = 0; idx < n; idx++) {
|
---|
168 | var k = unbiasedOrder[idx];
|
---|
169 | if (child[k] < 0) {
|
---|
170 | var feasibleInserts = Enumerable.Range(0, m)
|
---|
171 | .Select((v, i) => new { Pos = i, Slack = capacities[i] - cap[i] })
|
---|
172 | .Where(x => x.Slack >= demands[k]).ToList();
|
---|
173 | if (feasibleInserts.Count == 0) return false;
|
---|
174 | var j = feasibleInserts.SampleRandom(random).Pos;
|
---|
175 | child[k] = j;
|
---|
176 | cap[j] += demands[k];
|
---|
177 | unassigned--;
|
---|
178 | }
|
---|
179 | }
|
---|
180 | return true;
|
---|
181 | }
|
---|
182 |
|
---|
183 | private static void RandomAssignment(IRandom random, DoubleArray demands, DoubleArray capacities, int m, int n, double[] cap, IntegerVector child, ref int unassigned) {
|
---|
184 | for (var k = 0; k < n; k++) {
|
---|
185 | if (child[k] < 0) {
|
---|
186 | var j = random.Next(m);
|
---|
187 | child[k] = j;
|
---|
188 | cap[j] += demands[k];
|
---|
189 | unassigned--;
|
---|
190 | }
|
---|
191 | }
|
---|
192 | }
|
---|
193 |
|
---|
194 | protected override IntegerVector Cross(IRandom random, ItemArray<IntegerVector> parents,
|
---|
195 | GQAPInstance problemInstance) {
|
---|
196 | if (parents == null) throw new ArgumentNullException("parents");
|
---|
197 | if (parents.Length != 2) throw new ArgumentException(Name + " works only with exactly two parents.");
|
---|
198 |
|
---|
199 | var qualities = QualityParameter.ActualValue;
|
---|
200 | return Apply(random,
|
---|
201 | parents[0], qualities[0],
|
---|
202 | parents[1], qualities[1],
|
---|
203 | problemInstance,
|
---|
204 | EvaluatedSolutionsParameter.ActualValue);
|
---|
205 | }
|
---|
206 | }
|
---|
207 | }
|
---|