1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2017 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;
|
---|
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("N-Move Evaluator", "Evaluates an N-Move.")]
|
---|
34 | [StorableClass]
|
---|
35 | public class GQAPNMoveEvaluator : SingleSuccessorOperator, IGQAPNMoveEvaluator,
|
---|
36 | IQualityAwareGQAPOperator, IProblemInstanceAwareGQAPOperator, IAssignmentAwareGQAPOperator {
|
---|
37 |
|
---|
38 | #region Parameter Descriptions
|
---|
39 | public static readonly string MoveQualityDescription = "The quality of the move if it would be applied.";
|
---|
40 | public static readonly string MoveEvaluationDescription = "The results of evaluating the move.";
|
---|
41 | #endregion
|
---|
42 |
|
---|
43 | #region Parameter Properties
|
---|
44 | public ILookupParameter<DoubleValue> MoveQualityParameter {
|
---|
45 | get { return (ILookupParameter<DoubleValue>)Parameters["MoveQuality"]; }
|
---|
46 | }
|
---|
47 | public ILookupParameter<Evaluation> MoveEvaluationParameter {
|
---|
48 | get { return (ILookupParameter<Evaluation>)Parameters["MoveEvaluation"]; }
|
---|
49 | }
|
---|
50 | public ILookupParameter<NMove> MoveParameter {
|
---|
51 | get { return (ILookupParameter<NMove>)Parameters["Move"]; }
|
---|
52 | }
|
---|
53 | public ILookupParameter<IntegerVector> AssignmentParameter {
|
---|
54 | get { return (ILookupParameter<IntegerVector>)Parameters["Assignment"]; }
|
---|
55 | }
|
---|
56 | public ILookupParameter<DoubleValue> QualityParameter {
|
---|
57 | get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
58 | }
|
---|
59 | public ILookupParameter<Evaluation> EvaluationParameter {
|
---|
60 | get { return (ILookupParameter<Evaluation>)Parameters["Evaluation"]; }
|
---|
61 | }
|
---|
62 | public ILookupParameter<GQAPInstance> ProblemInstanceParameter {
|
---|
63 | get { return (ILookupParameter<GQAPInstance>)Parameters["ProblemInstance"]; }
|
---|
64 | }
|
---|
65 | #endregion
|
---|
66 |
|
---|
67 | [StorableConstructor]
|
---|
68 | protected GQAPNMoveEvaluator(bool deserializing) : base(deserializing) { }
|
---|
69 | protected GQAPNMoveEvaluator(GQAPNMoveEvaluator original, Cloner cloner) : base(original, cloner) { }
|
---|
70 | public GQAPNMoveEvaluator()
|
---|
71 | : base() {
|
---|
72 | Parameters.Add(new LookupParameter<IntegerVector>("Assignment", GQAPSolutionCreator.AssignmentDescription));
|
---|
73 | Parameters.Add(new LookupParameter<NMove>("Move", GQAPNMoveGenerator.MoveDescription));
|
---|
74 | Parameters.Add(new LookupParameter<DoubleValue>("Quality", ""));
|
---|
75 | Parameters.Add(new LookupParameter<Evaluation>("Evaluation", GQAP.EvaluationDescription));
|
---|
76 | Parameters.Add(new LookupParameter<DoubleValue>("MoveQuality", MoveQualityDescription));
|
---|
77 | Parameters.Add(new LookupParameter<Evaluation>("MoveEvaluation", MoveEvaluationDescription));
|
---|
78 | Parameters.Add(new LookupParameter<GQAPInstance>("ProblemInstance", GQAP.ProblemInstanceDescription));
|
---|
79 | }
|
---|
80 |
|
---|
81 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
82 | return new GQAPNMoveEvaluator(this, cloner);
|
---|
83 | }
|
---|
84 |
|
---|
85 | public static Evaluation Evaluate(NMove move, IntegerVector assignment, Evaluation evaluation, GQAPInstance problemInstance) {
|
---|
86 | int moves = move.N;
|
---|
87 | var excessDemand = evaluation.ExcessDemand;
|
---|
88 | var flowCosts = evaluation.FlowCosts;
|
---|
89 | var installationCosts = evaluation.InstallationCosts;
|
---|
90 | var slack = evaluation.Slack.ToArray();
|
---|
91 |
|
---|
92 | foreach (var equip in move.Indices) {
|
---|
93 | int newLoc = move.Reassignments[equip] - 1; // locations are given 1-based
|
---|
94 | int oldLoc = assignment[equip];
|
---|
95 | var equipDemand = problemInstance.Demands[equip];
|
---|
96 | installationCosts -= problemInstance.InstallationCosts[equip, oldLoc];
|
---|
97 | installationCosts += problemInstance.InstallationCosts[equip, newLoc];
|
---|
98 | for (int j = 0; j < assignment.Length; j++) {
|
---|
99 | var reassignedLoc = move.Reassignments[j] - 1; // 0 indicates that this equipment is not reassigned
|
---|
100 | if (reassignedLoc < 0) {
|
---|
101 | flowCosts += problemInstance.Weights[equip, j] * problemInstance.Distances[newLoc, assignment[j]];
|
---|
102 | flowCosts -= problemInstance.Weights[equip, j] * problemInstance.Distances[oldLoc, assignment[j]];
|
---|
103 | flowCosts += problemInstance.Weights[j, equip] * problemInstance.Distances[assignment[j], newLoc];
|
---|
104 | flowCosts -= problemInstance.Weights[j, equip] * problemInstance.Distances[assignment[j], oldLoc];
|
---|
105 | } else {
|
---|
106 | flowCosts += problemInstance.Weights[equip, j] * problemInstance.Distances[newLoc, reassignedLoc];
|
---|
107 | flowCosts -= problemInstance.Weights[equip, j] * problemInstance.Distances[oldLoc, assignment[j]];
|
---|
108 | }
|
---|
109 | }
|
---|
110 | slack[oldLoc] += equipDemand;
|
---|
111 | if (slack[oldLoc] < equipDemand)
|
---|
112 | excessDemand -= Math.Min(equipDemand, equipDemand - slack[oldLoc]);
|
---|
113 | slack[newLoc] -= equipDemand;
|
---|
114 | if (slack[newLoc] < 0)
|
---|
115 | excessDemand += Math.Min(equipDemand, -slack[newLoc]);
|
---|
116 | }
|
---|
117 |
|
---|
118 | return new Evaluation(flowCosts, installationCosts, excessDemand, slack);
|
---|
119 | }
|
---|
120 |
|
---|
121 | public override IOperation Apply() {
|
---|
122 | var problemInstance = ProblemInstanceParameter.ActualValue;
|
---|
123 | var evaluation = EvaluationParameter.ActualValue;
|
---|
124 |
|
---|
125 | var moveEvaluation = Evaluate(MoveParameter.ActualValue,
|
---|
126 | AssignmentParameter.ActualValue,
|
---|
127 | evaluation,
|
---|
128 | problemInstance);
|
---|
129 |
|
---|
130 | MoveEvaluationParameter.ActualValue = moveEvaluation;
|
---|
131 | MoveQualityParameter.ActualValue = new DoubleValue(problemInstance.ToSingleObjective(moveEvaluation));
|
---|
132 | return base.Apply();
|
---|
133 | }
|
---|
134 | }
|
---|
135 | }
|
---|