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.Collections.Generic;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
28 | using HeuristicLab.Operators;
|
---|
29 | using HeuristicLab.Parameters;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment {
|
---|
33 | [Item("GQAPNMoveEvaluator", "Evaluates an n-move.")]
|
---|
34 | [StorableClass]
|
---|
35 | public class GQAPNMoveEvaluator : SingleSuccessorOperator, IGQAPNMoveEvaluator {
|
---|
36 |
|
---|
37 | public ILookupParameter<IntegerVector> AssignmentParameter {
|
---|
38 | get { return (ILookupParameter<IntegerVector>)Parameters["Assignment"]; }
|
---|
39 | }
|
---|
40 | public ILookupParameter<NMove> MoveParameter {
|
---|
41 | get { return (ILookupParameter<NMove>)Parameters["Move"]; }
|
---|
42 | }
|
---|
43 | public ILookupParameter<DoubleValue> QualityParameter {
|
---|
44 | get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
45 | }
|
---|
46 | public ILookupParameter<DoubleValue> FlowDistanceQualityParameter {
|
---|
47 | get { return (ILookupParameter<DoubleValue>)Parameters["FlowDistanceQuality"]; }
|
---|
48 | }
|
---|
49 | public ILookupParameter<DoubleValue> InstallationQualityParameter {
|
---|
50 | get { return (ILookupParameter<DoubleValue>)Parameters["InstallationQuality"]; }
|
---|
51 | }
|
---|
52 | public ILookupParameter<DoubleValue> OverbookedCapacityParameter {
|
---|
53 | get { return (ILookupParameter<DoubleValue>)Parameters["OverbookedCapacity"]; }
|
---|
54 | }
|
---|
55 | public ILookupParameter<DoubleValue> MoveQualityParameter {
|
---|
56 | get { return (ILookupParameter<DoubleValue>)Parameters["MoveQuality"]; }
|
---|
57 | }
|
---|
58 | public ILookupParameter<DoubleValue> MoveFlowDistanceQualityParameter {
|
---|
59 | get { return (ILookupParameter<DoubleValue>)Parameters["MoveFlowDistanceQuality"]; }
|
---|
60 | }
|
---|
61 | public ILookupParameter<DoubleValue> MoveInstallationQualityParameter {
|
---|
62 | get { return (ILookupParameter<DoubleValue>)Parameters["MoveInstallationQuality"]; }
|
---|
63 | }
|
---|
64 | public ILookupParameter<DoubleValue> MoveOverbookedCapacityParameter {
|
---|
65 | get { return (ILookupParameter<DoubleValue>)Parameters["MoveOverbookedCapacity"]; }
|
---|
66 | }
|
---|
67 | public IValueLookupParameter<DoubleValue> TransportationCostsParameter {
|
---|
68 | get { return (IValueLookupParameter<DoubleValue>)Parameters["TransportationCosts"]; }
|
---|
69 | }
|
---|
70 | public IValueLookupParameter<DoubleValue> OverbookedCapacityPenaltyParameter {
|
---|
71 | get { return (IValueLookupParameter<DoubleValue>)Parameters["OverbookedCapacityPenalty"]; }
|
---|
72 | }
|
---|
73 | public ILookupParameter<DoubleMatrix> WeightsParameter {
|
---|
74 | get { return (ILookupParameter<DoubleMatrix>)Parameters["Weights"]; }
|
---|
75 | }
|
---|
76 | public ILookupParameter<DoubleMatrix> DistancesParameter {
|
---|
77 | get { return (ILookupParameter<DoubleMatrix>)Parameters["Distances"]; }
|
---|
78 | }
|
---|
79 | public ILookupParameter<DoubleMatrix> InstallationCostsParameter {
|
---|
80 | get { return (ILookupParameter<DoubleMatrix>)Parameters["InstallationCosts"]; }
|
---|
81 | }
|
---|
82 | public ILookupParameter<DoubleArray> DemandsParameter {
|
---|
83 | get { return (ILookupParameter<DoubleArray>)Parameters["Demands"]; }
|
---|
84 | }
|
---|
85 | public ILookupParameter<DoubleArray> CapacitiesParameter {
|
---|
86 | get { return (ILookupParameter<DoubleArray>)Parameters["Capacities"]; }
|
---|
87 | }
|
---|
88 |
|
---|
89 | [StorableConstructor]
|
---|
90 | protected GQAPNMoveEvaluator(bool deserializing) : base(deserializing) { }
|
---|
91 | protected GQAPNMoveEvaluator(GQAPNMoveEvaluator original, Cloner cloner) : base(original, cloner) { }
|
---|
92 | public GQAPNMoveEvaluator()
|
---|
93 | : base() {
|
---|
94 | Parameters.Add(new LookupParameter<IntegerVector>("Assignment", "The equipment-location assignment vector."));
|
---|
95 | Parameters.Add(new LookupParameter<NMove>("Move", "The move to perform."));
|
---|
96 | Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The solution quality."));
|
---|
97 | Parameters.Add(new LookupParameter<DoubleValue>("FlowDistanceQuality", "The quality regarding the flow-distance criteria."));
|
---|
98 | Parameters.Add(new LookupParameter<DoubleValue>("InstallationQuality", "The quality regarding the installation costs."));
|
---|
99 | Parameters.Add(new LookupParameter<DoubleValue>("OverbookedCapacity", "The sum of the overbooked capacities relative to the capacity of each location."));
|
---|
100 | Parameters.Add(new LookupParameter<DoubleValue>("MoveQuality", "The quality of the move if it would be applied."));
|
---|
101 | Parameters.Add(new LookupParameter<DoubleValue>("MoveFlowDistanceQuality", "The quality of the move regarding the flow-distance criteria."));
|
---|
102 | Parameters.Add(new LookupParameter<DoubleValue>("MoveInstallationQuality", "The quality of the move regarding the installation costs."));
|
---|
103 | Parameters.Add(new LookupParameter<DoubleValue>("MoveOverbookedCapacity", "The sum of the overbooked capacities of the move relative to the capacity of each location."));
|
---|
104 | Parameters.Add(new ValueLookupParameter<DoubleValue>("OverbookedCapacityPenalty", "The multiplier for the constraint violation when added to the quality."));
|
---|
105 | Parameters.Add(new ValueLookupParameter<DoubleValue>("TransportationCosts", "The transportation cost represents the flow-unit per distance-unit cost factor. This value can also be set to 1 if these costs are factored into the weights or distance matrix already."));
|
---|
106 | Parameters.Add(new LookupParameter<DoubleMatrix>("Weights", "The weights matrix describes the flows between the equipments."));
|
---|
107 | Parameters.Add(new LookupParameter<DoubleMatrix>("Distances", "The distances matrix describes the distances between the locations at which the equipment can be installed."));
|
---|
108 | Parameters.Add(new LookupParameter<DoubleMatrix>("InstallationCosts", "The installation costs matrix describes the installation costs of installing equipment i at location j"));
|
---|
109 | Parameters.Add(new LookupParameter<DoubleArray>("Demands", "The demands vector describes the space requirements of the equipments."));
|
---|
110 | Parameters.Add(new LookupParameter<DoubleArray>("Capacities", "The capacities vector describes the available space at the locations."));
|
---|
111 | }
|
---|
112 |
|
---|
113 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
114 | return new GQAPNMoveEvaluator(this, cloner);
|
---|
115 | }
|
---|
116 |
|
---|
117 | public static double Evaluate(NMove move, IntegerVector assignment, DoubleMatrix weights, DoubleMatrix distances, DoubleMatrix installationCosts,
|
---|
118 | DoubleArray demands, DoubleArray capacities, DoubleValue transportationCosts, DoubleValue overbookedCapacityPenalty) {
|
---|
119 | double moveFlowDistanceQuality, moveInstallationQuality, moveOverbookedCapacity;
|
---|
120 | Evaluate(move, assignment, weights, distances, installationCosts, demands, capacities,
|
---|
121 | out moveFlowDistanceQuality, out moveInstallationQuality, out moveOverbookedCapacity);
|
---|
122 | return GQAPEvaluator.GetCombinedQuality(moveFlowDistanceQuality, moveInstallationQuality, moveOverbookedCapacity,
|
---|
123 | transportationCosts.Value, overbookedCapacityPenalty.Value);
|
---|
124 | }
|
---|
125 |
|
---|
126 | public static void Evaluate(NMove move, IntegerVector assignment, DoubleMatrix weights, DoubleMatrix distances, DoubleMatrix installationCosts,
|
---|
127 | DoubleArray demands, DoubleArray capacities, out double moveFlowDistanceQuality, out double moveInstallationQuality, out double moveOverbookedCapacity) {
|
---|
128 | moveFlowDistanceQuality = moveInstallationQuality = moveOverbookedCapacity = 0.0;
|
---|
129 | int moves = move.N;
|
---|
130 | var slack = (DoubleArray)capacities.Clone();
|
---|
131 | var oldSlack = (DoubleArray)slack.Clone();
|
---|
132 | Dictionary<int, int> moving = new Dictionary<int, int>();
|
---|
133 | for (int i = 0; i < moves; i++) moving.Add(move.Equipments[i], move.Locations[i]);
|
---|
134 |
|
---|
135 | for (int i = 0; i < moves; i++) {
|
---|
136 | int equip = move.Equipments[i];
|
---|
137 | int newLoc = move.Locations[i];
|
---|
138 | moveInstallationQuality -= installationCosts[equip, assignment[equip]];
|
---|
139 | moveInstallationQuality += installationCosts[equip, newLoc];
|
---|
140 | for (int j = 0; j < assignment.Length; j++) {
|
---|
141 | if (!moving.ContainsKey(j)) {
|
---|
142 | moveFlowDistanceQuality += weights[equip, j] * distances[newLoc, assignment[j]];
|
---|
143 | moveFlowDistanceQuality -= weights[equip, j] * distances[assignment[equip], assignment[j]];
|
---|
144 | moveFlowDistanceQuality += weights[j, equip] * distances[assignment[j], newLoc];
|
---|
145 | moveFlowDistanceQuality -= weights[j, equip] * distances[assignment[j], assignment[equip]];
|
---|
146 | if (i == 0) { // only once for each untouched equipment deduct the demand from the capacity
|
---|
147 | slack[assignment[j]] -= demands[j];
|
---|
148 | oldSlack[assignment[j]] -= demands[j];
|
---|
149 | }
|
---|
150 | } else {
|
---|
151 | moveFlowDistanceQuality += weights[equip, j] * distances[newLoc, moving[j]];
|
---|
152 | moveFlowDistanceQuality -= weights[equip, j] * distances[assignment[equip], assignment[j]];
|
---|
153 | }
|
---|
154 | }
|
---|
155 | slack[newLoc] -= demands[equip];
|
---|
156 | oldSlack[assignment[equip]] -= demands[equip];
|
---|
157 | }
|
---|
158 |
|
---|
159 | moveOverbookedCapacity = slack.Select((v, i) => new { V = v, Index = i }).Where(x => x.V < 0.0).Select(x => -x.V / capacities[x.Index]).Sum()
|
---|
160 | - oldSlack.Select((v, i) => new { V = v, Index = i }).Where(x => x.V < 0.0).Select(x => -x.V / capacities[x.Index]).Sum();
|
---|
161 | }
|
---|
162 |
|
---|
163 | public override IOperation Apply() {
|
---|
164 | double moveFlowDistanceQuality, moveInstallationQuality, moveOverbookedCapacity;
|
---|
165 | double quality = QualityParameter.ActualValue.Value;
|
---|
166 | double transportationCosts = TransportationCostsParameter.ActualValue.Value;
|
---|
167 | double overbookedCapacityPenalty = OverbookedCapacityPenaltyParameter.ActualValue.Value;
|
---|
168 |
|
---|
169 | double flowDistanceQuality = FlowDistanceQualityParameter.ActualValue.Value;
|
---|
170 | double installationQuality = InstallationQualityParameter.ActualValue.Value;
|
---|
171 | double overbookedCapacity = OverbookedCapacityParameter.ActualValue.Value;
|
---|
172 |
|
---|
173 | Evaluate(MoveParameter.ActualValue,
|
---|
174 | AssignmentParameter.ActualValue,
|
---|
175 | WeightsParameter.ActualValue, DistancesParameter.ActualValue,
|
---|
176 | InstallationCostsParameter.ActualValue,
|
---|
177 | DemandsParameter.ActualValue, CapacitiesParameter.ActualValue,
|
---|
178 | out moveFlowDistanceQuality, out moveInstallationQuality, out moveOverbookedCapacity);
|
---|
179 |
|
---|
180 | MoveFlowDistanceQualityParameter.ActualValue = new DoubleValue(flowDistanceQuality + moveFlowDistanceQuality);
|
---|
181 | MoveInstallationQualityParameter.ActualValue = new DoubleValue(installationQuality + moveInstallationQuality);
|
---|
182 | MoveOverbookedCapacityParameter.ActualValue = new DoubleValue(moveOverbookedCapacity);
|
---|
183 |
|
---|
184 | MoveQualityParameter.ActualValue = new DoubleValue(GQAPEvaluator.GetCombinedQuality(
|
---|
185 | flowDistanceQuality + moveFlowDistanceQuality,
|
---|
186 | installationQuality + moveInstallationQuality,
|
---|
187 | moveOverbookedCapacity,
|
---|
188 | transportationCosts,
|
---|
189 | overbookedCapacityPenalty));
|
---|
190 | return base.Apply();
|
---|
191 | }
|
---|
192 | }
|
---|
193 | }
|
---|