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 | [StorableConstructor]
|
---|
78 | protected GQAPPathRelinking(bool deserializing) : base(deserializing) { }
|
---|
79 | protected GQAPPathRelinking(GQAPPathRelinking original, Cloner cloner) : base(original, cloner) { }
|
---|
80 | public GQAPPathRelinking()
|
---|
81 | : base() {
|
---|
82 | Parameters.Add(new LookupParameter<BoolValue>("Maximization", GeneralizedQuadraticAssignmentProblem.MaximizationDescription));
|
---|
83 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", GQAPEvaluator.QualityDescription));
|
---|
84 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("FlowDistanceQuality", GQAPEvaluator.FlowDistanceQualityDescription));
|
---|
85 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("InstallationQuality", GQAPEvaluator.InstallationQualityDescription));
|
---|
86 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("OverbookedCapacity", GQAPEvaluator.OverbookedCapacityDescription));
|
---|
87 | Parameters.Add(new LookupParameter<DoubleArray>("Demands", GeneralizedQuadraticAssignmentProblem.DemandsDescription));
|
---|
88 | Parameters.Add(new LookupParameter<DoubleArray>("Capacities", GeneralizedQuadraticAssignmentProblem.CapacitiesDescription));
|
---|
89 | Parameters.Add(new LookupParameter<DoubleMatrix>("Weights", GeneralizedQuadraticAssignmentProblem.WeightsDescription));
|
---|
90 | Parameters.Add(new LookupParameter<DoubleMatrix>("Distances", GeneralizedQuadraticAssignmentProblem.DistancesDescription));
|
---|
91 | Parameters.Add(new LookupParameter<DoubleMatrix>("InstallationCosts", GeneralizedQuadraticAssignmentProblem.InstallationCostsDescription));
|
---|
92 | Parameters.Add(new ValueLookupParameter<DoubleValue>("TransportationCosts", GeneralizedQuadraticAssignmentProblem.TransportationCostsDescription));
|
---|
93 | Parameters.Add(new ValueLookupParameter<DoubleValue>("OverbookedCapacityPenalty", GeneralizedQuadraticAssignmentProblem.OverbookedCapacityPenaltyDescription));
|
---|
94 | }
|
---|
95 |
|
---|
96 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
97 | return new GQAPPathRelinking(this, cloner);
|
---|
98 | }
|
---|
99 |
|
---|
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) {
|
---|
103 | if (random == null) throw new ArgumentNullException("random", "No IRandom provider is given.");
|
---|
104 | if (parents == null || !parents.Any()) throw new ArgumentException("No parents given for path relinking.", "parents");
|
---|
105 | 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");
|
---|
106 | if (parents[0].Length != parents[1].Length) throw new ArgumentException("The length of the parents is not equal.", "parents");
|
---|
107 | if (qualities == null || qualities.Length == 0) throw new ArgumentException("The qualities are not given.", "qualities");
|
---|
108 | 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()));
|
---|
109 |
|
---|
110 | var source = parents[0];
|
---|
111 | var target = parents[1];
|
---|
112 |
|
---|
113 | var nu = 1.0;
|
---|
114 | var pi_prime = (IntegerVector)source.Clone();
|
---|
115 | var fix = new HashSet<int>();
|
---|
116 | var nonFix = new HashSet<int>(Enumerable.Range(0, demands.Length));
|
---|
117 | var phi = new HashSet<int>(GQAPIntegerVectorProximityCalculator.GetDifference(pi_prime, target));
|
---|
118 |
|
---|
119 | while (phi.Any()) {
|
---|
120 | var B = new HashSet<GQAPSolution>(new GQAPSolutionStructuralEqualityComparer());
|
---|
121 | foreach (var v in phi) {
|
---|
122 | pi_prime[v] = target[v];
|
---|
123 | var pi2 = makeFeasible(pi_prime, v);
|
---|
124 |
|
---|
125 | double flowDistanceQuality, installationQuality, overbookedCapacity;
|
---|
126 | 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,
|
---|
131 | 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
|
---|
136 | }
|
---|
137 | } else if (!B.Contains(solution)) {
|
---|
138 | B.Add(solution);
|
---|
139 | }
|
---|
140 | }
|
---|
141 | }
|
---|
142 | 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);
|
---|
147 | fix.Add(i);
|
---|
148 | nonFix.Remove(i);
|
---|
149 | pi_prime = pi.Assignment;
|
---|
150 | // TODO
|
---|
151 | }
|
---|
152 | }
|
---|
153 |
|
---|
154 | return pi_prime;
|
---|
155 | }
|
---|
156 |
|
---|
157 | protected override IntegerVector Cross(IRandom random, ItemArray<IntegerVector> parents) {
|
---|
158 | return Apply(random, parents, QualityParameter.ActualValue, DemandsParameter.ActualValue,
|
---|
159 | CapacitiesParameter.ActualValue, WeightsParameter.ActualValue, DistancesParameter.ActualValue,
|
---|
160 | InstallationCostsParameter.ActualValue, TransportationCostsParameter.ActualValue,
|
---|
161 | OverbookedCapacityPenaltyParameter.ActualValue);
|
---|
162 | }
|
---|
163 |
|
---|
164 | private static IntegerVector makeFeasible(IntegerVector assignment, int equipment) {
|
---|
165 | // TODO: implement
|
---|
166 | return assignment;
|
---|
167 | }
|
---|
168 | }
|
---|
169 | }
|
---|