Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2892_LR-prediction-intervals/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/General/Moves/VRPMoveMaker.cs @ 15802

Last change on this file since 15802 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 4.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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
22using System.Collections.Generic;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Optimization;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
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, ISingleObjectiveOperator {
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    }
41    public ILookupParameter<DoubleValue> MovePenaltyParameter {
42      get { return (ILookupParameter<DoubleValue>)Parameters["MovePenalty"]; }
43    }
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."));
52      Parameters.Add(new LookupParameter<DoubleValue>("MovePenalty", "The penalty applied to the move."));
53    }
54
55    protected VRPMoveMaker(VRPMoveMaker original, Cloner cloner)
56      : base(original, cloner) {
57    }
58
59    protected abstract void PerformMove();
60
61    private void UpdateMoveEvaluation() {
62      IVRPEvaluator evaluator = ProblemInstance.SolutionEvaluator;
63      ICollection<IParameter> addedParameters = new List<IParameter>();
64
65      try {
66        foreach (IParameter parameter in evaluator.Parameters) {
67          if (parameter is ILookupParameter
68            && parameter != evaluator.VRPToursParameter
69            && parameter != evaluator.ProblemInstanceParameter) {
70            ILookupParameter evaluatorParameter = parameter as ILookupParameter;
71
72            string resultName = evaluatorParameter.ActualName;
73            if (!this.Parameters.ContainsKey(resultName)) {
74              ILookupParameter resultParameter = new LookupParameter<IItem>(resultName);
75              resultParameter.ExecutionContext = ExecutionContext;
76              this.Parameters.Add(resultParameter);
77              addedParameters.Add(resultParameter);
78            }
79
80            string moveResultName = VRPMoveEvaluator.MovePrefix + resultName;
81            if (!this.Parameters.ContainsKey(moveResultName)) {
82              ILookupParameter moveResultParameter = new LookupParameter<IItem>(moveResultName);
83              moveResultParameter.ExecutionContext = ExecutionContext;
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;
90            result.ActualValue = moveResult.ActualValue;
91          }
92        }
93      } finally {
94        foreach (IParameter parameter in addedParameters) {
95          this.Parameters.Remove(parameter);
96        }
97      }
98    }
99
100    public override IOperation InstrumentedApply() {
101      PerformMove();
102      UpdateMoveEvaluation();
103
104      return base.InstrumentedApply();
105    }
106  }
107}
Note: See TracBrowser for help on using the repository browser.