Free cookie consent management tool by TermsFeed Policy Generator

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

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

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

File size: 13.1 KB
RevLine 
[7407]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;
[7419]27using HeuristicLab.Optimization;
[7407]28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment {
[7505]32  [Item("N-Move Evaluator", "Evaluates an N-Move.")]
[7407]33  [StorableClass]
[7419]34  public class GQAPNMoveEvaluator : SingleSuccessorOperator, IGQAPNMoveEvaluator, IAssignmentAwareGQAPOperator,
35  IQualityAwareGQAPOperator, ITransportationCostsAwareGQAPOperator,
[7970]36  IExpectedRandomQualityAwareGQAPOperator, IWeightsAwareGQAPOperator, IDistancesAwareGQAPOperator,
37  IInstallationCostsAwareGQAPOperator, IDemandsAwareGQAPOperator, ICapacitiesAwareGQAPOperator,
38  IEvaluatorAwareGQAPOperator {
[7407]39
[7419]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; }
[7407]50    }
[7419]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    }
[7407]69    public ILookupParameter<NMove> MoveParameter {
70      get { return (ILookupParameter<NMove>)Parameters["Move"]; }
71    }
[7419]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    }
[7407]81    public ILookupParameter<DoubleValue> QualityParameter {
82      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
83    }
[7412]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    }
[7970]96    public IValueLookupParameter<DoubleValue> ExpectedRandomQualityParameter {
97      get { return (IValueLookupParameter<DoubleValue>)Parameters["ExpectedRandomQuality"]; }
[7412]98    }
[7407]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    }
[7970]114    public IValueLookupParameter<IGQAPEvaluator> EvaluatorParameter {
115      get { return (IValueLookupParameter<IGQAPEvaluator>)Parameters["Evaluator"]; }
116    }
[7419]117    #endregion
[7407]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() {
[7419]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));
[7970]135      Parameters.Add(new ValueLookupParameter<DoubleValue>("ExpectedRandomQuality", GeneralizedQuadraticAssignmentProblem.ExpectedRandomQualityDescription));
[7419]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));
[7970]142      Parameters.Add(new ValueLookupParameter<IGQAPEvaluator>("Evaluator", "The evaluator that is used to evaluate GQAP solutions."));
[7407]143    }
144
145    public override IDeepCloneable Clone(Cloner cloner) {
146      return new GQAPNMoveEvaluator(this, cloner);
147    }
148
[7412]149    public static double Evaluate(NMove move, IntegerVector assignment, DoubleMatrix weights, DoubleMatrix distances, DoubleMatrix installationCosts,
[7970]150      DoubleArray demands, DoubleArray capacities, double transportationCosts, double expectedRandomQuality, IGQAPEvaluator evaluator) {
[7412]151      double moveFlowDistanceQuality, moveInstallationQuality, moveOverbookedCapacity;
[7970]152      Evaluate(move, assignment, weights, distances, installationCosts, demands, capacities, evaluator,
[7412]153        out moveFlowDistanceQuality, out moveInstallationQuality, out moveOverbookedCapacity);
[7970]154      return evaluator.GetFitness(moveFlowDistanceQuality, moveInstallationQuality, moveOverbookedCapacity,
155        transportationCosts, expectedRandomQuality);
[7412]156    }
157
158    public static void Evaluate(NMove move, IntegerVector assignment, DoubleMatrix weights, DoubleMatrix distances, DoubleMatrix installationCosts,
[7970]159      DoubleArray demands, DoubleArray capacities, IGQAPEvaluator evaluator, out double moveFlowDistanceQuality, out double moveInstallationQuality, out double moveOverbookedCapacity) {
[7412]160      moveFlowDistanceQuality = moveInstallationQuality = moveOverbookedCapacity = 0.0;
[7407]161      int moves = move.N;
162      var slack = (DoubleArray)capacities.Clone();
[7412]163      var oldSlack = (DoubleArray)slack.Clone();
[7505]164      bool first = true;
165      foreach (var kvp in move.NewAssignments) {
166        int equip = kvp.Key;
167        int newLoc = kvp.Value;
[7412]168        moveInstallationQuality -= installationCosts[equip, assignment[equip]];
169        moveInstallationQuality += installationCosts[equip, newLoc];
[7407]170        for (int j = 0; j < assignment.Length; j++) {
[7505]171          if (!move.NewAssignments.ContainsKey(j)) {
[7412]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]];
[7505]176            if (first) { // only once for each untouched equipment deduct the demand from the capacity
[7407]177              slack[assignment[j]] -= demands[j];
[7412]178              oldSlack[assignment[j]] -= demands[j];
[7407]179            }
180          } else {
[7505]181            moveFlowDistanceQuality += weights[equip, j] * distances[newLoc, move.NewAssignments[j]];
[7412]182            moveFlowDistanceQuality -= weights[equip, j] * distances[assignment[equip], assignment[j]];
[7407]183          }
184        }
[7505]185        first = false;
[7407]186        slack[newLoc] -= demands[equip];
[7412]187        oldSlack[assignment[equip]] -= demands[equip];
[7407]188      }
189
[7970]190      moveOverbookedCapacity = evaluator.EvaluateOverbooking(slack, capacities) - evaluator.EvaluateOverbooking(oldSlack, capacities);
[7407]191    }
192
193    public override IOperation Apply() {
[7970]194      var evaluator = EvaluatorParameter.ActualValue;
[7412]195      double moveFlowDistanceQuality, moveInstallationQuality, moveOverbookedCapacity;
196      double quality = QualityParameter.ActualValue.Value;
197      double transportationCosts = TransportationCostsParameter.ActualValue.Value;
[7970]198      double expectedRandomQuality = ExpectedRandomQualityParameter.ActualValue.Value;
[7412]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,
[7970]209               evaluator,
[7412]210               out moveFlowDistanceQuality, out moveInstallationQuality, out moveOverbookedCapacity);
211
212      MoveFlowDistanceQualityParameter.ActualValue = new DoubleValue(flowDistanceQuality + moveFlowDistanceQuality);
213      MoveInstallationQualityParameter.ActualValue = new DoubleValue(installationQuality + moveInstallationQuality);
[7413]214      MoveOverbookedCapacityParameter.ActualValue = new DoubleValue(overbookedCapacity + moveOverbookedCapacity);
[7412]215
[7970]216      MoveQualityParameter.ActualValue = new DoubleValue(evaluator.GetFitness(
[7412]217        flowDistanceQuality + moveFlowDistanceQuality,
218        installationQuality + moveInstallationQuality,
[7413]219        overbookedCapacity + moveOverbookedCapacity,
[7970]220        transportationCosts, expectedRandomQuality));
[7407]221      return base.Apply();
222    }
223  }
224}
Note: See TracBrowser for help on using the repository browser.