Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1614_GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Evaluation/GQAPNMoveEvaluator.cs @ 17485

Last change on this file since 17485 was 16728, checked in by abeham, 6 years ago

#1614: updated to new persistence and .NET 4.6.1

File size: 6.5 KB
RevLine 
[7407]1#region License Information
2/* HeuristicLab
[16728]3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[7407]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
[15504]22using System;
23using System.Linq;
[7407]24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.IntegerVectorEncoding;
28using HeuristicLab.Operators;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[16728]31using HEAL.Attic;
[7407]32
33namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment {
[7505]34  [Item("N-Move Evaluator", "Evaluates an N-Move.")]
[16728]35  [StorableType("EB4CCB5D-8FAC-4DFD-944E-1AC418AAE091")]
[15504]36  public class GQAPNMoveEvaluator : SingleSuccessorOperator, IGQAPNMoveEvaluator,
[15506]37    IQualityAwareGQAPOperator, IProblemInstanceAwareGQAPOperator, IAssignmentAwareGQAPOperator {
[7407]38
[7419]39    #region Parameter Descriptions
40    public static readonly string MoveQualityDescription = "The quality of the move if it would be applied.";
[15504]41    public static readonly string MoveEvaluationDescription = "The results of evaluating the move.";
[7419]42    #endregion
43
44    #region Parameter Properties
45    public ILookupParameter<DoubleValue> MoveQualityParameter {
46      get { return (ILookupParameter<DoubleValue>)Parameters["MoveQuality"]; }
47    }
[15504]48    public ILookupParameter<Evaluation> MoveEvaluationParameter {
49      get { return (ILookupParameter<Evaluation>)Parameters["MoveEvaluation"]; }
[7419]50    }
[7407]51    public ILookupParameter<NMove> MoveParameter {
52      get { return (ILookupParameter<NMove>)Parameters["Move"]; }
53    }
[7419]54    public ILookupParameter<IntegerVector> AssignmentParameter {
55      get { return (ILookupParameter<IntegerVector>)Parameters["Assignment"]; }
56    }
[7407]57    public ILookupParameter<DoubleValue> QualityParameter {
58      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
59    }
[15504]60    public ILookupParameter<Evaluation> EvaluationParameter {
61      get { return (ILookupParameter<Evaluation>)Parameters["Evaluation"]; }
[7412]62    }
[15504]63    public ILookupParameter<GQAPInstance> ProblemInstanceParameter {
64      get { return (ILookupParameter<GQAPInstance>)Parameters["ProblemInstance"]; }
[7412]65    }
[7419]66    #endregion
[7407]67
68    [StorableConstructor]
[16728]69    protected GQAPNMoveEvaluator(StorableConstructorFlag _) : base(_) { }
[7407]70    protected GQAPNMoveEvaluator(GQAPNMoveEvaluator original, Cloner cloner) : base(original, cloner) { }
71    public GQAPNMoveEvaluator()
72      : base() {
[7419]73      Parameters.Add(new LookupParameter<IntegerVector>("Assignment", GQAPSolutionCreator.AssignmentDescription));
74      Parameters.Add(new LookupParameter<NMove>("Move", GQAPNMoveGenerator.MoveDescription));
[15504]75      Parameters.Add(new LookupParameter<DoubleValue>("Quality", ""));
76      Parameters.Add(new LookupParameter<Evaluation>("Evaluation", GQAP.EvaluationDescription));
[7419]77      Parameters.Add(new LookupParameter<DoubleValue>("MoveQuality", MoveQualityDescription));
[15504]78      Parameters.Add(new LookupParameter<Evaluation>("MoveEvaluation", MoveEvaluationDescription));
79      Parameters.Add(new LookupParameter<GQAPInstance>("ProblemInstance", GQAP.ProblemInstanceDescription));
[7407]80    }
81
82    public override IDeepCloneable Clone(Cloner cloner) {
83      return new GQAPNMoveEvaluator(this, cloner);
84    }
85
[15504]86    public static Evaluation Evaluate(NMove move, IntegerVector assignment, Evaluation evaluation, GQAPInstance problemInstance) {
[7407]87      int moves = move.N;
[15510]88      var excessDemand = evaluation.ExcessDemand;
89      var flowCosts = evaluation.FlowCosts;
90      var installationCosts = evaluation.InstallationCosts;
91      var slack = evaluation.Slack.ToArray();
92
[15511]93      foreach (var equip in move.Indices) {
94        int newLoc = move.Reassignments[equip] - 1; // locations are given 1-based
[15504]95        int oldLoc = assignment[equip];
96        var equipDemand = problemInstance.Demands[equip];
[15510]97        installationCosts -= problemInstance.InstallationCosts[equip, oldLoc];
98        installationCosts += problemInstance.InstallationCosts[equip, newLoc];
[7407]99        for (int j = 0; j < assignment.Length; j++) {
[15511]100          var reassignedLoc = move.Reassignments[j] - 1; // 0 indicates that this equipment is not reassigned
101          if (reassignedLoc < 0) {
[15510]102            flowCosts += problemInstance.Weights[equip, j] * problemInstance.Distances[newLoc, assignment[j]];
103            flowCosts -= problemInstance.Weights[equip, j] * problemInstance.Distances[oldLoc, assignment[j]];
104            flowCosts += problemInstance.Weights[j, equip] * problemInstance.Distances[assignment[j], newLoc];
105            flowCosts -= problemInstance.Weights[j, equip] * problemInstance.Distances[assignment[j], oldLoc];
[7407]106          } else {
[15511]107            flowCosts += problemInstance.Weights[equip, j] * problemInstance.Distances[newLoc, reassignedLoc];
[15510]108            flowCosts -= problemInstance.Weights[equip, j] * problemInstance.Distances[oldLoc, assignment[j]];
[7407]109          }
110        }
[15510]111        slack[oldLoc] += equipDemand;
112        if (slack[oldLoc] < equipDemand)
113          excessDemand -= Math.Min(equipDemand, equipDemand - slack[oldLoc]);
114        slack[newLoc] -= equipDemand;
115        if (slack[newLoc] < 0)
116          excessDemand += Math.Min(equipDemand, -slack[newLoc]);
[7407]117      }
118
[15510]119      return new Evaluation(flowCosts, installationCosts, excessDemand, slack);
[7407]120    }
121
122    public override IOperation Apply() {
[15504]123      var problemInstance = ProblemInstanceParameter.ActualValue;
124      var evaluation = EvaluationParameter.ActualValue;
[7412]125
[15504]126      var moveEvaluation = Evaluate(MoveParameter.ActualValue,
[7412]127               AssignmentParameter.ActualValue,
[15504]128               evaluation,
129               problemInstance);
[7412]130
[15504]131      MoveEvaluationParameter.ActualValue = moveEvaluation;
[15507]132      MoveQualityParameter.ActualValue = new DoubleValue(problemInstance.ToSingleObjective(moveEvaluation));
[7407]133      return base.Apply();
134    }
135  }
136}
Note: See TracBrowser for help on using the repository browser.