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