Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 7952 was 7505, checked in by abeham, 13 years ago

#1614

  • added instances of Cordeau et al. as given by L. Moccia
  • added operators for tabu search
File size: 12.9 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 System.Linq;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.IntegerVectorEncoding;
27using HeuristicLab.Operators;
[7419]28using HeuristicLab.Optimization;
[7407]29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment {
[7505]33  [Item("N-Move Evaluator", "Evaluates an N-Move.")]
[7407]34  [StorableClass]
[7419]35  public class GQAPNMoveEvaluator : SingleSuccessorOperator, IGQAPNMoveEvaluator, IAssignmentAwareGQAPOperator,
36  IQualityAwareGQAPOperator, ITransportationCostsAwareGQAPOperator,
37  IOverbookedCapacityPenaltyAwareGQAPOperator, IWeightsAwareGQAPOperator, IDistancesAwareGQAPOperator,
38  IInstallationCostsAwareGQAPOperator, IDemandsAwareGQAPOperator, ICapacitiesAwareGQAPOperator {
[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    }
96    public IValueLookupParameter<DoubleValue> OverbookedCapacityPenaltyParameter {
97      get { return (IValueLookupParameter<DoubleValue>)Parameters["OverbookedCapacityPenalty"]; }
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    }
[7419]114    #endregion
[7407]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() {
[7419]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));
[7407]139    }
140
141    public override IDeepCloneable Clone(Cloner cloner) {
142      return new GQAPNMoveEvaluator(this, cloner);
143    }
144
[7412]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;
[7407]157      int moves = move.N;
158      var slack = (DoubleArray)capacities.Clone();
[7412]159      var oldSlack = (DoubleArray)slack.Clone();
[7505]160      bool first = true;
161      foreach (var kvp in move.NewAssignments) {
162        int equip = kvp.Key;
163        int newLoc = kvp.Value;
[7412]164        moveInstallationQuality -= installationCosts[equip, assignment[equip]];
165        moveInstallationQuality += installationCosts[equip, newLoc];
[7407]166        for (int j = 0; j < assignment.Length; j++) {
[7505]167          if (!move.NewAssignments.ContainsKey(j)) {
[7412]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]];
[7505]172            if (first) { // only once for each untouched equipment deduct the demand from the capacity
[7407]173              slack[assignment[j]] -= demands[j];
[7412]174              oldSlack[assignment[j]] -= demands[j];
[7407]175            }
176          } else {
[7505]177            moveFlowDistanceQuality += weights[equip, j] * distances[newLoc, move.NewAssignments[j]];
[7412]178            moveFlowDistanceQuality -= weights[equip, j] * distances[assignment[equip], assignment[j]];
[7407]179          }
180        }
[7505]181        first = false;
[7407]182        slack[newLoc] -= demands[equip];
[7412]183        oldSlack[assignment[equip]] -= demands[equip];
[7407]184      }
185
[7412]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();
[7407]188    }
189
190    public override IOperation Apply() {
[7412]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);
[7413]209      MoveOverbookedCapacityParameter.ActualValue = new DoubleValue(overbookedCapacity + moveOverbookedCapacity);
[7412]210
211      MoveQualityParameter.ActualValue = new DoubleValue(GQAPEvaluator.GetCombinedQuality(
212        flowDistanceQuality + moveFlowDistanceQuality,
213        installationQuality + moveInstallationQuality,
[7413]214        overbookedCapacity + moveOverbookedCapacity,
[7412]215        transportationCosts,
216        overbookedCapacityPenalty));
[7407]217      return base.Apply();
218    }
219  }
220}
Note: See TracBrowser for help on using the repository browser.