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.Linq;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
27 | using HeuristicLab.Operators;
|
---|
28 | using HeuristicLab.Optimization;
|
---|
29 | using HeuristicLab.Parameters;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment {
|
---|
33 | [Item("N-Move Evaluator", "Evaluates an N-Move.")]
|
---|
34 | [StorableClass]
|
---|
35 | public class GQAPNMoveEvaluator : SingleSuccessorOperator, IGQAPNMoveEvaluator, IAssignmentAwareGQAPOperator,
|
---|
36 | IQualityAwareGQAPOperator, ITransportationCostsAwareGQAPOperator,
|
---|
37 | IOverbookedCapacityPenaltyAwareGQAPOperator, IWeightsAwareGQAPOperator, IDistancesAwareGQAPOperator,
|
---|
38 | IInstallationCostsAwareGQAPOperator, IDemandsAwareGQAPOperator, ICapacitiesAwareGQAPOperator {
|
---|
39 |
|
---|
40 | #region Parameter Descriptions
|
---|
41 | public static readonly string MoveQualityDescription = "The quality of the move if it would be applied.";
|
---|
42 | public static readonly string MoveFlowDistanceQualityDescription = "The quality of the move regarding the flow-distance criteria.";
|
---|
43 | public static readonly string MoveInstallationQualityDescription = "The quality of the move regarding the installation costs.";
|
---|
44 | public static readonly string MoveOverbookedCapacityDescription = "The sum of the overbooked capacities of the move relative to the capacity of each location.";
|
---|
45 | #endregion
|
---|
46 |
|
---|
47 | #region Parameter Properties
|
---|
48 | ILookupParameter<BoolValue> IQualityAwareGQAPOperator.MaximizationParameter {
|
---|
49 | get { return MaximizationParameter; }
|
---|
50 | }
|
---|
51 | ILookupParameter<BoolValue> IGQAPMoveEvaluator.MaximizationParameter {
|
---|
52 | get { return MaximizationParameter; }
|
---|
53 | }
|
---|
54 | public ILookupParameter<BoolValue> MaximizationParameter {
|
---|
55 | get { return (ILookupParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
56 | }
|
---|
57 | public ILookupParameter<DoubleValue> MoveQualityParameter {
|
---|
58 | get { return (ILookupParameter<DoubleValue>)Parameters["MoveQuality"]; }
|
---|
59 | }
|
---|
60 | public ILookupParameter<DoubleValue> MoveFlowDistanceQualityParameter {
|
---|
61 | get { return (ILookupParameter<DoubleValue>)Parameters["MoveFlowDistanceQuality"]; }
|
---|
62 | }
|
---|
63 | public ILookupParameter<DoubleValue> MoveInstallationQualityParameter {
|
---|
64 | get { return (ILookupParameter<DoubleValue>)Parameters["MoveInstallationQuality"]; }
|
---|
65 | }
|
---|
66 | public ILookupParameter<DoubleValue> MoveOverbookedCapacityParameter {
|
---|
67 | get { return (ILookupParameter<DoubleValue>)Parameters["MoveOverbookedCapacity"]; }
|
---|
68 | }
|
---|
69 | public ILookupParameter<NMove> MoveParameter {
|
---|
70 | get { return (ILookupParameter<NMove>)Parameters["Move"]; }
|
---|
71 | }
|
---|
72 | public ILookupParameter<IntegerVector> AssignmentParameter {
|
---|
73 | get { return (ILookupParameter<IntegerVector>)Parameters["Assignment"]; }
|
---|
74 | }
|
---|
75 | ILookupParameter<DoubleValue> ISingleObjectiveMoveEvaluator.QualityParameter {
|
---|
76 | get { return QualityParameter; }
|
---|
77 | }
|
---|
78 | ILookupParameter<DoubleValue> IQualityAwareGQAPOperator.QualityParameter {
|
---|
79 | get { return QualityParameter; }
|
---|
80 | }
|
---|
81 | public ILookupParameter<DoubleValue> QualityParameter {
|
---|
82 | get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
83 | }
|
---|
84 | public ILookupParameter<DoubleValue> FlowDistanceQualityParameter {
|
---|
85 | get { return (ILookupParameter<DoubleValue>)Parameters["FlowDistanceQuality"]; }
|
---|
86 | }
|
---|
87 | public ILookupParameter<DoubleValue> InstallationQualityParameter {
|
---|
88 | get { return (ILookupParameter<DoubleValue>)Parameters["InstallationQuality"]; }
|
---|
89 | }
|
---|
90 | public ILookupParameter<DoubleValue> OverbookedCapacityParameter {
|
---|
91 | get { return (ILookupParameter<DoubleValue>)Parameters["OverbookedCapacity"]; }
|
---|
92 | }
|
---|
93 | public IValueLookupParameter<DoubleValue> TransportationCostsParameter {
|
---|
94 | get { return (IValueLookupParameter<DoubleValue>)Parameters["TransportationCosts"]; }
|
---|
95 | }
|
---|
96 | public IValueLookupParameter<DoubleValue> OverbookedCapacityPenaltyParameter {
|
---|
97 | get { return (IValueLookupParameter<DoubleValue>)Parameters["OverbookedCapacityPenalty"]; }
|
---|
98 | }
|
---|
99 | public ILookupParameter<DoubleMatrix> WeightsParameter {
|
---|
100 | get { return (ILookupParameter<DoubleMatrix>)Parameters["Weights"]; }
|
---|
101 | }
|
---|
102 | public ILookupParameter<DoubleMatrix> DistancesParameter {
|
---|
103 | get { return (ILookupParameter<DoubleMatrix>)Parameters["Distances"]; }
|
---|
104 | }
|
---|
105 | public ILookupParameter<DoubleMatrix> InstallationCostsParameter {
|
---|
106 | get { return (ILookupParameter<DoubleMatrix>)Parameters["InstallationCosts"]; }
|
---|
107 | }
|
---|
108 | public ILookupParameter<DoubleArray> DemandsParameter {
|
---|
109 | get { return (ILookupParameter<DoubleArray>)Parameters["Demands"]; }
|
---|
110 | }
|
---|
111 | public ILookupParameter<DoubleArray> CapacitiesParameter {
|
---|
112 | get { return (ILookupParameter<DoubleArray>)Parameters["Capacities"]; }
|
---|
113 | }
|
---|
114 | #endregion
|
---|
115 |
|
---|
116 | [StorableConstructor]
|
---|
117 | protected GQAPNMoveEvaluator(bool deserializing) : base(deserializing) { }
|
---|
118 | protected GQAPNMoveEvaluator(GQAPNMoveEvaluator original, Cloner cloner) : base(original, cloner) { }
|
---|
119 | public GQAPNMoveEvaluator()
|
---|
120 | : base() {
|
---|
121 | Parameters.Add(new LookupParameter<IntegerVector>("Assignment", GQAPSolutionCreator.AssignmentDescription));
|
---|
122 | Parameters.Add(new LookupParameter<NMove>("Move", GQAPNMoveGenerator.MoveDescription));
|
---|
123 | Parameters.Add(new LookupParameter<BoolValue>("Maximization", GeneralizedQuadraticAssignmentProblem.MaximizationDescription));
|
---|
124 | Parameters.Add(new LookupParameter<DoubleValue>("Quality", GQAPEvaluator.QualityDescription));
|
---|
125 | Parameters.Add(new LookupParameter<DoubleValue>("FlowDistanceQuality", GQAPEvaluator.FlowDistanceQualityDescription));
|
---|
126 | Parameters.Add(new LookupParameter<DoubleValue>("InstallationQuality", GQAPEvaluator.InstallationQualityDescription));
|
---|
127 | Parameters.Add(new LookupParameter<DoubleValue>("OverbookedCapacity", GQAPEvaluator.OverbookedCapacityDescription));
|
---|
128 | Parameters.Add(new LookupParameter<DoubleValue>("MoveQuality", MoveQualityDescription));
|
---|
129 | Parameters.Add(new LookupParameter<DoubleValue>("MoveFlowDistanceQuality", MoveFlowDistanceQualityDescription));
|
---|
130 | Parameters.Add(new LookupParameter<DoubleValue>("MoveInstallationQuality", MoveInstallationQualityDescription));
|
---|
131 | Parameters.Add(new LookupParameter<DoubleValue>("MoveOverbookedCapacity", MoveOverbookedCapacityDescription));
|
---|
132 | Parameters.Add(new ValueLookupParameter<DoubleValue>("OverbookedCapacityPenalty", GeneralizedQuadraticAssignmentProblem.OverbookedCapacityPenaltyDescription));
|
---|
133 | Parameters.Add(new ValueLookupParameter<DoubleValue>("TransportationCosts", GeneralizedQuadraticAssignmentProblem.TransportationCostsDescription));
|
---|
134 | Parameters.Add(new LookupParameter<DoubleMatrix>("Weights", GeneralizedQuadraticAssignmentProblem.WeightsDescription));
|
---|
135 | Parameters.Add(new LookupParameter<DoubleMatrix>("Distances", GeneralizedQuadraticAssignmentProblem.DistancesDescription));
|
---|
136 | Parameters.Add(new LookupParameter<DoubleMatrix>("InstallationCosts", GeneralizedQuadraticAssignmentProblem.InstallationCostsDescription));
|
---|
137 | Parameters.Add(new LookupParameter<DoubleArray>("Demands", GeneralizedQuadraticAssignmentProblem.DemandsDescription));
|
---|
138 | Parameters.Add(new LookupParameter<DoubleArray>("Capacities", GeneralizedQuadraticAssignmentProblem.CapacitiesDescription));
|
---|
139 | }
|
---|
140 |
|
---|
141 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
142 | return new GQAPNMoveEvaluator(this, cloner);
|
---|
143 | }
|
---|
144 |
|
---|
145 | public static double Evaluate(NMove move, IntegerVector assignment, DoubleMatrix weights, DoubleMatrix distances, DoubleMatrix installationCosts,
|
---|
146 | DoubleArray demands, DoubleArray capacities, DoubleValue transportationCosts, DoubleValue overbookedCapacityPenalty) {
|
---|
147 | double moveFlowDistanceQuality, moveInstallationQuality, moveOverbookedCapacity;
|
---|
148 | Evaluate(move, assignment, weights, distances, installationCosts, demands, capacities,
|
---|
149 | out moveFlowDistanceQuality, out moveInstallationQuality, out moveOverbookedCapacity);
|
---|
150 | return GQAPEvaluator.GetCombinedQuality(moveFlowDistanceQuality, moveInstallationQuality, moveOverbookedCapacity,
|
---|
151 | transportationCosts.Value, overbookedCapacityPenalty.Value);
|
---|
152 | }
|
---|
153 |
|
---|
154 | public static void Evaluate(NMove move, IntegerVector assignment, DoubleMatrix weights, DoubleMatrix distances, DoubleMatrix installationCosts,
|
---|
155 | DoubleArray demands, DoubleArray capacities, out double moveFlowDistanceQuality, out double moveInstallationQuality, out double moveOverbookedCapacity) {
|
---|
156 | moveFlowDistanceQuality = moveInstallationQuality = moveOverbookedCapacity = 0.0;
|
---|
157 | int moves = move.N;
|
---|
158 | var slack = (DoubleArray)capacities.Clone();
|
---|
159 | var oldSlack = (DoubleArray)slack.Clone();
|
---|
160 | bool first = true;
|
---|
161 | foreach (var kvp in move.NewAssignments) {
|
---|
162 | int equip = kvp.Key;
|
---|
163 | int newLoc = kvp.Value;
|
---|
164 | moveInstallationQuality -= installationCosts[equip, assignment[equip]];
|
---|
165 | moveInstallationQuality += installationCosts[equip, newLoc];
|
---|
166 | for (int j = 0; j < assignment.Length; j++) {
|
---|
167 | if (!move.NewAssignments.ContainsKey(j)) {
|
---|
168 | moveFlowDistanceQuality += weights[equip, j] * distances[newLoc, assignment[j]];
|
---|
169 | moveFlowDistanceQuality -= weights[equip, j] * distances[assignment[equip], assignment[j]];
|
---|
170 | moveFlowDistanceQuality += weights[j, equip] * distances[assignment[j], newLoc];
|
---|
171 | moveFlowDistanceQuality -= weights[j, equip] * distances[assignment[j], assignment[equip]];
|
---|
172 | if (first) { // only once for each untouched equipment deduct the demand from the capacity
|
---|
173 | slack[assignment[j]] -= demands[j];
|
---|
174 | oldSlack[assignment[j]] -= demands[j];
|
---|
175 | }
|
---|
176 | } else {
|
---|
177 | moveFlowDistanceQuality += weights[equip, j] * distances[newLoc, move.NewAssignments[j]];
|
---|
178 | moveFlowDistanceQuality -= weights[equip, j] * distances[assignment[equip], assignment[j]];
|
---|
179 | }
|
---|
180 | }
|
---|
181 | first = false;
|
---|
182 | slack[newLoc] -= demands[equip];
|
---|
183 | oldSlack[assignment[equip]] -= demands[equip];
|
---|
184 | }
|
---|
185 |
|
---|
186 | moveOverbookedCapacity = slack.Select((v, i) => new { V = v, Index = i }).Where(x => x.V < 0.0).Select(x => -x.V / capacities[x.Index]).Sum()
|
---|
187 | - oldSlack.Select((v, i) => new { V = v, Index = i }).Where(x => x.V < 0.0).Select(x => -x.V / capacities[x.Index]).Sum();
|
---|
188 | }
|
---|
189 |
|
---|
190 | public override IOperation Apply() {
|
---|
191 | double moveFlowDistanceQuality, moveInstallationQuality, moveOverbookedCapacity;
|
---|
192 | double quality = QualityParameter.ActualValue.Value;
|
---|
193 | double transportationCosts = TransportationCostsParameter.ActualValue.Value;
|
---|
194 | double overbookedCapacityPenalty = OverbookedCapacityPenaltyParameter.ActualValue.Value;
|
---|
195 |
|
---|
196 | double flowDistanceQuality = FlowDistanceQualityParameter.ActualValue.Value;
|
---|
197 | double installationQuality = InstallationQualityParameter.ActualValue.Value;
|
---|
198 | double overbookedCapacity = OverbookedCapacityParameter.ActualValue.Value;
|
---|
199 |
|
---|
200 | Evaluate(MoveParameter.ActualValue,
|
---|
201 | AssignmentParameter.ActualValue,
|
---|
202 | WeightsParameter.ActualValue, DistancesParameter.ActualValue,
|
---|
203 | InstallationCostsParameter.ActualValue,
|
---|
204 | DemandsParameter.ActualValue, CapacitiesParameter.ActualValue,
|
---|
205 | out moveFlowDistanceQuality, out moveInstallationQuality, out moveOverbookedCapacity);
|
---|
206 |
|
---|
207 | MoveFlowDistanceQualityParameter.ActualValue = new DoubleValue(flowDistanceQuality + moveFlowDistanceQuality);
|
---|
208 | MoveInstallationQualityParameter.ActualValue = new DoubleValue(installationQuality + moveInstallationQuality);
|
---|
209 | MoveOverbookedCapacityParameter.ActualValue = new DoubleValue(overbookedCapacity + moveOverbookedCapacity);
|
---|
210 |
|
---|
211 | MoveQualityParameter.ActualValue = new DoubleValue(GQAPEvaluator.GetCombinedQuality(
|
---|
212 | flowDistanceQuality + moveFlowDistanceQuality,
|
---|
213 | installationQuality + moveInstallationQuality,
|
---|
214 | overbookedCapacity + moveOverbookedCapacity,
|
---|
215 | transportationCosts,
|
---|
216 | overbookedCapacityPenalty));
|
---|
217 | return base.Apply();
|
---|
218 | }
|
---|
219 | }
|
---|
220 | }
|
---|