Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/General/Moves/VRPMoveMaker.cs @ 7895

Last change on this file since 7895 was 7852, checked in by svonolfe, 12 years ago

Moved parameters from the ProblemInstance to the Problem (#1177)

File size: 4.4 KB
RevLine 
[4369]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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27using HeuristicLab.Core;
28using HeuristicLab.Optimization;
29using HeuristicLab.Data;
30using HeuristicLab.Parameters;
31using HeuristicLab.Problems.VehicleRouting.Interfaces;
[4752]32using HeuristicLab.Common;
[4369]33
34namespace 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    }
[5127]44    public ILookupParameter<DoubleValue> MovePenaltyParameter {
45      get { return (ILookupParameter<DoubleValue>)Parameters["MovePenalty"]; }
46    }
[4369]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."));
[5127]55      Parameters.Add(new LookupParameter<DoubleValue>("MovePenalty", "The penalty applied to the move."));
[4369]56    }
57
[4752]58    protected VRPMoveMaker(VRPMoveMaker original, Cloner cloner)
59      : base(original, cloner) {
60    }
61
[4369]62    protected abstract void PerformMove();
63
64    private void UpdateMoveEvaluation() {
[7852]65      IVRPEvaluator evaluator = ProblemInstance.SolutionEvaluator;
[4370]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;
[4369]74         
[4370]75            string resultName = evaluatorParameter.ActualName;
[6607]76            if (!this.Parameters.ContainsKey(resultName)) {
[4370]77              ILookupParameter resultParameter = new LookupParameter<IItem>(resultName);
78              resultParameter.ExecutionContext = ExecutionContext;
79              this.Parameters.Add(resultParameter);
80              addedParameters.Add(resultParameter);
81            }
[4369]82
[4370]83            string moveResultName = VRPMoveEvaluator.MovePrefix + resultName;
[6607]84            if (!this.Parameters.ContainsKey(moveResultName)) {
[4370]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;
[6607]93            result.ActualValue = moveResult.ActualValue;
[4369]94          }
95        }
96      }
[4370]97      finally {
98        foreach(IParameter parameter in addedParameters) {
99          this.Parameters.Remove(parameter);
100        }
101      }
[4369]102    }
103
104    public override IOperation Apply() {
105      PerformMove();
106      UpdateMoveEvaluation();
107     
108      return base.Apply();
109    }
110  }
111}
Note: See TracBrowser for help on using the repository browser.