Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/General/Moves/VRPMoveMaker.cs @ 11497

Last change on this file since 11497 was 11170, checked in by ascheibe, 10 years ago

#2115 updated copyright year in stable branch

File size: 4.3 KB
RevLine 
[4369]1#region License Information
2/* HeuristicLab
[11170]3 * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[4369]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.Collections.Generic;
[8053]23using HeuristicLab.Common;
[4369]24using HeuristicLab.Core;
[8053]25using HeuristicLab.Data;
[4369]26using HeuristicLab.Optimization;
27using HeuristicLab.Parameters;
[8053]28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[4369]29using HeuristicLab.Problems.VehicleRouting.Interfaces;
30
31namespace HeuristicLab.Problems.VehicleRouting.Encodings.General {
32  [Item("VRPMoveMaker", "Performs a VRP move.")]
33  [StorableClass]
34  public abstract class VRPMoveMaker : VRPMoveOperator, IMoveMaker {
35    public ILookupParameter<DoubleValue> QualityParameter {
36      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
37    }
38    public ILookupParameter<DoubleValue> MoveQualityParameter {
39      get { return (ILookupParameter<DoubleValue>)Parameters["MoveQuality"]; }
40    }
[5127]41    public ILookupParameter<DoubleValue> MovePenaltyParameter {
42      get { return (ILookupParameter<DoubleValue>)Parameters["MovePenalty"]; }
43    }
[4369]44
45    [StorableConstructor]
46    protected VRPMoveMaker(bool deserializing) : base(deserializing) { }
47
48    public VRPMoveMaker()
49      : base() {
50      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality of the solution."));
51      Parameters.Add(new LookupParameter<DoubleValue>("MoveQuality", "The relative quality of the move."));
[5127]52      Parameters.Add(new LookupParameter<DoubleValue>("MovePenalty", "The penalty applied to the move."));
[4369]53    }
54
[4752]55    protected VRPMoveMaker(VRPMoveMaker original, Cloner cloner)
56      : base(original, cloner) {
57    }
58
[4369]59    protected abstract void PerformMove();
60
61    private void UpdateMoveEvaluation() {
[7852]62      IVRPEvaluator evaluator = ProblemInstance.SolutionEvaluator;
[4370]63      ICollection<IParameter> addedParameters = new List<IParameter>();
[8053]64
[4370]65      try {
66        foreach (IParameter parameter in evaluator.Parameters) {
[8053]67          if (parameter is ILookupParameter
68            && parameter != evaluator.VRPToursParameter
[4370]69            && parameter != evaluator.ProblemInstanceParameter) {
70            ILookupParameter evaluatorParameter = parameter as ILookupParameter;
[8053]71
[4370]72            string resultName = evaluatorParameter.ActualName;
[6607]73            if (!this.Parameters.ContainsKey(resultName)) {
[4370]74              ILookupParameter resultParameter = new LookupParameter<IItem>(resultName);
75              resultParameter.ExecutionContext = ExecutionContext;
76              this.Parameters.Add(resultParameter);
77              addedParameters.Add(resultParameter);
78            }
[4369]79
[4370]80            string moveResultName = VRPMoveEvaluator.MovePrefix + resultName;
[6607]81            if (!this.Parameters.ContainsKey(moveResultName)) {
[4370]82              ILookupParameter moveResultParameter = new LookupParameter<IItem>(moveResultName);
[8053]83              moveResultParameter.ExecutionContext = ExecutionContext;
[4370]84              this.Parameters.Add(moveResultParameter);
85              addedParameters.Add(moveResultParameter);
86            }
87
88            ILookupParameter result = Parameters[resultName] as ILookupParameter;
89            ILookupParameter moveResult = Parameters[moveResultName] as ILookupParameter;
[6607]90            result.ActualValue = moveResult.ActualValue;
[4369]91          }
92        }
93      }
[4370]94      finally {
[8053]95        foreach (IParameter parameter in addedParameters) {
[4370]96          this.Parameters.Remove(parameter);
97        }
98      }
[4369]99    }
100
[10507]101    public override IOperation InstrumentedApply() {
[4369]102      PerformMove();
103      UpdateMoveEvaluation();
[8053]104
[10507]105      return base.InstrumentedApply();
[4369]106    }
107  }
108}
Note: See TracBrowser for help on using the repository browser.