Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Evaluators/GQAPNMoveEvaluator.cs @ 7970

Last change on this file since 7970 was 7970, checked in by abeham, 12 years ago

#1614: restructured architecture to allow for different evaluator with different penalty strategies

File size: 13.1 KB
Line 
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
22using HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Encodings.IntegerVectorEncoding;
26using HeuristicLab.Operators;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment {
32  [Item("N-Move Evaluator", "Evaluates an N-Move.")]
33  [StorableClass]
34  public class GQAPNMoveEvaluator : SingleSuccessorOperator, IGQAPNMoveEvaluator, IAssignmentAwareGQAPOperator,
35  IQualityAwareGQAPOperator, ITransportationCostsAwareGQAPOperator,
36  IExpectedRandomQualityAwareGQAPOperator, IWeightsAwareGQAPOperator, IDistancesAwareGQAPOperator,
37  IInstallationCostsAwareGQAPOperator, IDemandsAwareGQAPOperator, ICapacitiesAwareGQAPOperator,
38  IEvaluatorAwareGQAPOperator {
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> ExpectedRandomQualityParameter {
97      get { return (IValueLookupParameter<DoubleValue>)Parameters["ExpectedRandomQuality"]; }
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    public IValueLookupParameter<IGQAPEvaluator> EvaluatorParameter {
115      get { return (IValueLookupParameter<IGQAPEvaluator>)Parameters["Evaluator"]; }
116    }
117    #endregion
118
119    [StorableConstructor]
120    protected GQAPNMoveEvaluator(bool deserializing) : base(deserializing) { }
121    protected GQAPNMoveEvaluator(GQAPNMoveEvaluator original, Cloner cloner) : base(original, cloner) { }
122    public GQAPNMoveEvaluator()
123      : base() {
124      Parameters.Add(new LookupParameter<IntegerVector>("Assignment", GQAPSolutionCreator.AssignmentDescription));
125      Parameters.Add(new LookupParameter<NMove>("Move", GQAPNMoveGenerator.MoveDescription));
126      Parameters.Add(new LookupParameter<BoolValue>("Maximization", GeneralizedQuadraticAssignmentProblem.MaximizationDescription));
127      Parameters.Add(new LookupParameter<DoubleValue>("Quality", GQAPEvaluator.QualityDescription));
128      Parameters.Add(new LookupParameter<DoubleValue>("FlowDistanceQuality", GQAPEvaluator.FlowDistanceQualityDescription));
129      Parameters.Add(new LookupParameter<DoubleValue>("InstallationQuality", GQAPEvaluator.InstallationQualityDescription));
130      Parameters.Add(new LookupParameter<DoubleValue>("OverbookedCapacity", GQAPEvaluator.OverbookedCapacityDescription));
131      Parameters.Add(new LookupParameter<DoubleValue>("MoveQuality", MoveQualityDescription));
132      Parameters.Add(new LookupParameter<DoubleValue>("MoveFlowDistanceQuality", MoveFlowDistanceQualityDescription));
133      Parameters.Add(new LookupParameter<DoubleValue>("MoveInstallationQuality", MoveInstallationQualityDescription));
134      Parameters.Add(new LookupParameter<DoubleValue>("MoveOverbookedCapacity", MoveOverbookedCapacityDescription));
135      Parameters.Add(new ValueLookupParameter<DoubleValue>("ExpectedRandomQuality", GeneralizedQuadraticAssignmentProblem.ExpectedRandomQualityDescription));
136      Parameters.Add(new ValueLookupParameter<DoubleValue>("TransportationCosts", GeneralizedQuadraticAssignmentProblem.TransportationCostsDescription));
137      Parameters.Add(new LookupParameter<DoubleMatrix>("Weights", GeneralizedQuadraticAssignmentProblem.WeightsDescription));
138      Parameters.Add(new LookupParameter<DoubleMatrix>("Distances", GeneralizedQuadraticAssignmentProblem.DistancesDescription));
139      Parameters.Add(new LookupParameter<DoubleMatrix>("InstallationCosts", GeneralizedQuadraticAssignmentProblem.InstallationCostsDescription));
140      Parameters.Add(new LookupParameter<DoubleArray>("Demands", GeneralizedQuadraticAssignmentProblem.DemandsDescription));
141      Parameters.Add(new LookupParameter<DoubleArray>("Capacities", GeneralizedQuadraticAssignmentProblem.CapacitiesDescription));
142      Parameters.Add(new ValueLookupParameter<IGQAPEvaluator>("Evaluator", "The evaluator that is used to evaluate GQAP solutions."));
143    }
144
145    public override IDeepCloneable Clone(Cloner cloner) {
146      return new GQAPNMoveEvaluator(this, cloner);
147    }
148
149    public static double Evaluate(NMove move, IntegerVector assignment, DoubleMatrix weights, DoubleMatrix distances, DoubleMatrix installationCosts,
150      DoubleArray demands, DoubleArray capacities, double transportationCosts, double expectedRandomQuality, IGQAPEvaluator evaluator) {
151      double moveFlowDistanceQuality, moveInstallationQuality, moveOverbookedCapacity;
152      Evaluate(move, assignment, weights, distances, installationCosts, demands, capacities, evaluator,
153        out moveFlowDistanceQuality, out moveInstallationQuality, out moveOverbookedCapacity);
154      return evaluator.GetFitness(moveFlowDistanceQuality, moveInstallationQuality, moveOverbookedCapacity,
155        transportationCosts, expectedRandomQuality);
156    }
157
158    public static void Evaluate(NMove move, IntegerVector assignment, DoubleMatrix weights, DoubleMatrix distances, DoubleMatrix installationCosts,
159      DoubleArray demands, DoubleArray capacities, IGQAPEvaluator evaluator, out double moveFlowDistanceQuality, out double moveInstallationQuality, out double moveOverbookedCapacity) {
160      moveFlowDistanceQuality = moveInstallationQuality = moveOverbookedCapacity = 0.0;
161      int moves = move.N;
162      var slack = (DoubleArray)capacities.Clone();
163      var oldSlack = (DoubleArray)slack.Clone();
164      bool first = true;
165      foreach (var kvp in move.NewAssignments) {
166        int equip = kvp.Key;
167        int newLoc = kvp.Value;
168        moveInstallationQuality -= installationCosts[equip, assignment[equip]];
169        moveInstallationQuality += installationCosts[equip, newLoc];
170        for (int j = 0; j < assignment.Length; j++) {
171          if (!move.NewAssignments.ContainsKey(j)) {
172            moveFlowDistanceQuality += weights[equip, j] * distances[newLoc, assignment[j]];
173            moveFlowDistanceQuality -= weights[equip, j] * distances[assignment[equip], assignment[j]];
174            moveFlowDistanceQuality += weights[j, equip] * distances[assignment[j], newLoc];
175            moveFlowDistanceQuality -= weights[j, equip] * distances[assignment[j], assignment[equip]];
176            if (first) { // only once for each untouched equipment deduct the demand from the capacity
177              slack[assignment[j]] -= demands[j];
178              oldSlack[assignment[j]] -= demands[j];
179            }
180          } else {
181            moveFlowDistanceQuality += weights[equip, j] * distances[newLoc, move.NewAssignments[j]];
182            moveFlowDistanceQuality -= weights[equip, j] * distances[assignment[equip], assignment[j]];
183          }
184        }
185        first = false;
186        slack[newLoc] -= demands[equip];
187        oldSlack[assignment[equip]] -= demands[equip];
188      }
189
190      moveOverbookedCapacity = evaluator.EvaluateOverbooking(slack, capacities) - evaluator.EvaluateOverbooking(oldSlack, capacities);
191    }
192
193    public override IOperation Apply() {
194      var evaluator = EvaluatorParameter.ActualValue;
195      double moveFlowDistanceQuality, moveInstallationQuality, moveOverbookedCapacity;
196      double quality = QualityParameter.ActualValue.Value;
197      double transportationCosts = TransportationCostsParameter.ActualValue.Value;
198      double expectedRandomQuality = ExpectedRandomQualityParameter.ActualValue.Value;
199
200      double flowDistanceQuality = FlowDistanceQualityParameter.ActualValue.Value;
201      double installationQuality = InstallationQualityParameter.ActualValue.Value;
202      double overbookedCapacity = OverbookedCapacityParameter.ActualValue.Value;
203
204      Evaluate(MoveParameter.ActualValue,
205               AssignmentParameter.ActualValue,
206               WeightsParameter.ActualValue, DistancesParameter.ActualValue,
207               InstallationCostsParameter.ActualValue,
208               DemandsParameter.ActualValue, CapacitiesParameter.ActualValue,
209               evaluator,
210               out moveFlowDistanceQuality, out moveInstallationQuality, out moveOverbookedCapacity);
211
212      MoveFlowDistanceQualityParameter.ActualValue = new DoubleValue(flowDistanceQuality + moveFlowDistanceQuality);
213      MoveInstallationQualityParameter.ActualValue = new DoubleValue(installationQuality + moveInstallationQuality);
214      MoveOverbookedCapacityParameter.ActualValue = new DoubleValue(overbookedCapacity + moveOverbookedCapacity);
215
216      MoveQualityParameter.ActualValue = new DoubleValue(evaluator.GetFitness(
217        flowDistanceQuality + moveFlowDistanceQuality,
218        installationQuality + moveInstallationQuality,
219        overbookedCapacity + moveOverbookedCapacity,
220        transportationCosts, expectedRandomQuality));
221      return base.Apply();
222    }
223  }
224}
Note: See TracBrowser for help on using the repository browser.