1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2014 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.Collections.Generic;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Optimization;
|
---|
27 | using HeuristicLab.Parameters;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 | using HeuristicLab.Problems.VehicleRouting.Interfaces;
|
---|
30 |
|
---|
31 | namespace 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 | }
|
---|
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 | }
|
---|
94 | finally {
|
---|
95 | foreach (IParameter parameter in addedParameters) {
|
---|
96 | this.Parameters.Remove(parameter);
|
---|
97 | }
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | public override IOperation InstrumentedApply() {
|
---|
102 | PerformMove();
|
---|
103 | UpdateMoveEvaluation();
|
---|
104 |
|
---|
105 | return base.InstrumentedApply();
|
---|
106 | }
|
---|
107 | }
|
---|
108 | }
|
---|