Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Evaluation/GQAPNMoveEvaluator.cs @ 15504

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

#1614: refactored code

  • change problem to derive from basic problem
  • using a combined instance class instead of individual parameters
File size: 6.4 KB
RevLine 
[7407]1#region License Information
2/* HeuristicLab
[15504]3 * Copyright (C) 2002-2017 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;
31
32namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment {
[7505]33  [Item("N-Move Evaluator", "Evaluates an N-Move.")]
[7407]34  [StorableClass]
[15504]35  public class GQAPNMoveEvaluator : SingleSuccessorOperator, IGQAPNMoveEvaluator,
36    IQualityAwareGQAPOperator, IProblemInstanceAwareGQAPOperator {
[7407]37
[7419]38    #region Parameter Descriptions
39    public static readonly string MoveQualityDescription = "The quality of the move if it would be applied.";
[15504]40    public static readonly string MoveEvaluationDescription = "The results of evaluating the move.";
[7419]41    #endregion
42
43    #region Parameter Properties
44    public ILookupParameter<DoubleValue> MoveQualityParameter {
45      get { return (ILookupParameter<DoubleValue>)Parameters["MoveQuality"]; }
46    }
[15504]47    public ILookupParameter<Evaluation> MoveEvaluationParameter {
48      get { return (ILookupParameter<Evaluation>)Parameters["MoveEvaluation"]; }
[7419]49    }
[7407]50    public ILookupParameter<NMove> MoveParameter {
51      get { return (ILookupParameter<NMove>)Parameters["Move"]; }
52    }
[7419]53    public ILookupParameter<IntegerVector> AssignmentParameter {
54      get { return (ILookupParameter<IntegerVector>)Parameters["Assignment"]; }
55    }
[7407]56    public ILookupParameter<DoubleValue> QualityParameter {
57      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
58    }
[15504]59    public ILookupParameter<Evaluation> EvaluationParameter {
60      get { return (ILookupParameter<Evaluation>)Parameters["Evaluation"]; }
[7412]61    }
[15504]62    public ILookupParameter<GQAPInstance> ProblemInstanceParameter {
63      get { return (ILookupParameter<GQAPInstance>)Parameters["ProblemInstance"]; }
[7412]64    }
[7419]65    #endregion
[7407]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() {
[7419]72      Parameters.Add(new LookupParameter<IntegerVector>("Assignment", GQAPSolutionCreator.AssignmentDescription));
73      Parameters.Add(new LookupParameter<NMove>("Move", GQAPNMoveGenerator.MoveDescription));
[15504]74      Parameters.Add(new LookupParameter<DoubleValue>("Quality", ""));
75      Parameters.Add(new LookupParameter<Evaluation>("Evaluation", GQAP.EvaluationDescription));
[7419]76      Parameters.Add(new LookupParameter<DoubleValue>("MoveQuality", MoveQualityDescription));
[15504]77      Parameters.Add(new LookupParameter<Evaluation>("MoveEvaluation", MoveEvaluationDescription));
78      Parameters.Add(new LookupParameter<GQAPInstance>("ProblemInstance", GQAP.ProblemInstanceDescription));
[7407]79    }
80
81    public override IDeepCloneable Clone(Cloner cloner) {
82      return new GQAPNMoveEvaluator(this, cloner);
83    }
84
[15504]85    public static Evaluation Evaluate(NMove move, IntegerVector assignment, Evaluation evaluation, GQAPInstance problemInstance) {
[7407]86      int moves = move.N;
[15504]87      var newEvaluation = new Evaluation() {
88        ExcessDemand = evaluation.ExcessDemand,
89        FlowCosts = evaluation.FlowCosts,
90        InstallationCosts = evaluation.InstallationCosts,
91        Slack = evaluation.Slack.ToArray()
92      };
[7505]93      foreach (var kvp in move.NewAssignments) {
94        int equip = kvp.Key;
95        int newLoc = kvp.Value;
[15504]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];
[7407]100        for (int j = 0; j < assignment.Length; j++) {
[7505]101          if (!move.NewAssignments.ContainsKey(j)) {
[15504]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];
[7407]106          } else {
[15504]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]];
[7407]109          }
110        }
[15504]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]);
[7407]117      }
118
[15504]119      return newEvaluation;
[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;
[7407]132      return base.Apply();
133    }
134  }
135}
Note: See TracBrowser for help on using the repository browser.