Changeset 15504 for branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Evaluation/GQAPNMoveEvaluator.cs
- Timestamp:
- 12/10/17 22:11:10 (6 years ago)
- Location:
- branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Evaluation
- Files:
-
- 1 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Evaluation/GQAPNMoveEvaluator.cs
r7970 r15504 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-201 2Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) 2002-2017 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. … … 20 20 #endregion 21 21 22 using System; 23 using System.Linq; 22 24 using HeuristicLab.Common; 23 25 using HeuristicLab.Core; … … 25 27 using HeuristicLab.Encodings.IntegerVectorEncoding; 26 28 using HeuristicLab.Operators; 27 using HeuristicLab.Optimization;28 29 using HeuristicLab.Parameters; 29 30 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; … … 32 33 [Item("N-Move Evaluator", "Evaluates an N-Move.")] 33 34 [StorableClass] 34 public class GQAPNMoveEvaluator : SingleSuccessorOperator, IGQAPNMoveEvaluator, IAssignmentAwareGQAPOperator, 35 IQualityAwareGQAPOperator, ITransportationCostsAwareGQAPOperator, 36 IExpectedRandomQualityAwareGQAPOperator, IWeightsAwareGQAPOperator, IDistancesAwareGQAPOperator, 37 IInstallationCostsAwareGQAPOperator, IDemandsAwareGQAPOperator, ICapacitiesAwareGQAPOperator, 38 IEvaluatorAwareGQAPOperator { 35 public class GQAPNMoveEvaluator : SingleSuccessorOperator, IGQAPNMoveEvaluator, 36 IQualityAwareGQAPOperator, IProblemInstanceAwareGQAPOperator { 39 37 40 38 #region Parameter Descriptions 41 39 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."; 40 public static readonly string MoveEvaluationDescription = "The results of evaluating the move."; 45 41 #endregion 46 42 47 43 #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 44 public ILookupParameter<DoubleValue> MoveQualityParameter { 58 45 get { return (ILookupParameter<DoubleValue>)Parameters["MoveQuality"]; } 59 46 } 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"]; } 47 public ILookupParameter<Evaluation> MoveEvaluationParameter { 48 get { return (ILookupParameter<Evaluation>)Parameters["MoveEvaluation"]; } 68 49 } 69 50 public ILookupParameter<NMove> MoveParameter { … … 73 54 get { return (ILookupParameter<IntegerVector>)Parameters["Assignment"]; } 74 55 } 75 ILookupParameter<DoubleValue> ISingleObjectiveMoveEvaluator.QualityParameter {76 get { return QualityParameter; }77 }78 ILookupParameter<DoubleValue> IQualityAwareGQAPOperator.QualityParameter {79 get { return QualityParameter; }80 }81 56 public ILookupParameter<DoubleValue> QualityParameter { 82 57 get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; } 83 58 } 84 public ILookupParameter< DoubleValue> FlowDistanceQualityParameter {85 get { return (ILookupParameter< DoubleValue>)Parameters["FlowDistanceQuality"]; }59 public ILookupParameter<Evaluation> EvaluationParameter { 60 get { return (ILookupParameter<Evaluation>)Parameters["Evaluation"]; } 86 61 } 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"]; } 62 public ILookupParameter<GQAPInstance> ProblemInstanceParameter { 63 get { return (ILookupParameter<GQAPInstance>)Parameters["ProblemInstance"]; } 116 64 } 117 65 #endregion … … 124 72 Parameters.Add(new LookupParameter<IntegerVector>("Assignment", GQAPSolutionCreator.AssignmentDescription)); 125 73 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)); 74 Parameters.Add(new LookupParameter<DoubleValue>("Quality", "")); 75 Parameters.Add(new LookupParameter<Evaluation>("Evaluation", GQAP.EvaluationDescription)); 131 76 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.")); 77 Parameters.Add(new LookupParameter<Evaluation>("MoveEvaluation", MoveEvaluationDescription)); 78 Parameters.Add(new LookupParameter<GQAPInstance>("ProblemInstance", GQAP.ProblemInstanceDescription)); 143 79 } 144 80 … … 147 83 } 148 84 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; 85 public static Evaluation Evaluate(NMove move, IntegerVector assignment, Evaluation evaluation, GQAPInstance problemInstance) { 161 86 int moves = move.N; 162 var slack = (DoubleArray)capacities.Clone(); 163 var oldSlack = (DoubleArray)slack.Clone(); 164 bool first = true; 87 var newEvaluation = new Evaluation() { 88 ExcessDemand = evaluation.ExcessDemand, 89 FlowCosts = evaluation.FlowCosts, 90 InstallationCosts = evaluation.InstallationCosts, 91 Slack = evaluation.Slack.ToArray() 92 }; 165 93 foreach (var kvp in move.NewAssignments) { 166 94 int equip = kvp.Key; 167 95 int newLoc = kvp.Value; 168 moveInstallationQuality -= installationCosts[equip, assignment[equip]]; 169 moveInstallationQuality += installationCosts[equip, newLoc]; 96 int oldLoc = assignment[equip]; 97 var equipDemand = problemInstance.Demands[equip]; 98 newEvaluation.InstallationCosts -= problemInstance.InstallationCosts[equip, oldLoc]; 99 newEvaluation.InstallationCosts += problemInstance.InstallationCosts[equip, newLoc]; 170 100 for (int j = 0; j < assignment.Length; j++) { 171 101 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 } 102 newEvaluation.FlowCosts += problemInstance.Weights[equip, j] * problemInstance.Distances[newLoc, assignment[j]]; 103 newEvaluation.FlowCosts -= problemInstance.Weights[equip, j] * problemInstance.Distances[oldLoc, assignment[j]]; 104 newEvaluation.FlowCosts += problemInstance.Weights[j, equip] * problemInstance.Distances[assignment[j], newLoc]; 105 newEvaluation.FlowCosts -= problemInstance.Weights[j, equip] * problemInstance.Distances[assignment[j], oldLoc]; 180 106 } else { 181 moveFlowDistanceQuality += weights[equip, j] * distances[newLoc, move.NewAssignments[j]];182 moveFlowDistanceQuality -= weights[equip, j] * distances[assignment[equip], assignment[j]];107 newEvaluation.FlowCosts += problemInstance.Weights[equip, j] * problemInstance.Distances[newLoc, move.NewAssignments[j]]; 108 newEvaluation.FlowCosts -= problemInstance.Weights[equip, j] * problemInstance.Distances[oldLoc, assignment[j]]; 183 109 } 184 110 } 185 first = false; 186 slack[newLoc] -= demands[equip]; 187 oldSlack[assignment[equip]] -= demands[equip]; 111 newEvaluation.Slack[oldLoc] += equipDemand; 112 if (newEvaluation.Slack[oldLoc] < equipDemand) 113 newEvaluation.ExcessDemand -= Math.Min(equipDemand, equipDemand - newEvaluation.Slack[oldLoc]); 114 newEvaluation.Slack[newLoc] -= equipDemand; 115 if (newEvaluation.Slack[newLoc] < 0) 116 newEvaluation.ExcessDemand += Math.Min(equipDemand, -newEvaluation.Slack[newLoc]); 188 117 } 189 118 190 moveOverbookedCapacity = evaluator.EvaluateOverbooking(slack, capacities) - evaluator.EvaluateOverbooking(oldSlack, capacities);119 return newEvaluation; 191 120 } 192 121 193 122 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; 123 var problemInstance = ProblemInstanceParameter.ActualValue; 124 var evaluation = EvaluationParameter.ActualValue; 199 125 200 double flowDistanceQuality = FlowDistanceQualityParameter.ActualValue.Value; 201 double installationQuality = InstallationQualityParameter.ActualValue.Value; 202 double overbookedCapacity = OverbookedCapacityParameter.ActualValue.Value; 126 var moveEvaluation = Evaluate(MoveParameter.ActualValue, 127 AssignmentParameter.ActualValue, 128 evaluation, 129 problemInstance); 203 130 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)); 131 MoveEvaluationParameter.ActualValue = moveEvaluation; 221 132 return base.Apply(); 222 133 }
Note: See TracChangeset
for help on using the changeset viewer.