source: branches/SpectralKernelForGaussianProcesses/HeuristicLab.Problems.VehicleRouting/3.4/Improver/VRPImprovementOperator.cs @ 10479

Last change on this file since 10479 was 10479, checked in by gkronber, 6 years ago

#2124 merged all changes from trunk to prepare for trunk-reintegration

File size: 4.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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 HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Optimization;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.Problems.VehicleRouting.Encodings;
30using HeuristicLab.Problems.VehicleRouting.Encodings.Potvin;
31using HeuristicLab.Problems.VehicleRouting.Interfaces;
32using HeuristicLab.Problems.VehicleRouting.Variants;
33
34namespace HeuristicLab.Problems.VehicleRouting {
35  /// <summary>
36  /// A base class for operators which improve VRP solutions.
37  /// </summary>
38  [Item("VRPImprovementOperator", "A base class for operators which improve VRP solutions.")]
39  [StorableClass]
40  public abstract class VRPImprovementOperator : VRPOperator, IGeneralVRPOperator, ISingleObjectiveImprovementOperator {
41    #region Parameter properties
42    public ScopeParameter CurrentScopeParameter {
43      get { return (ScopeParameter)Parameters["CurrentScope"]; }
44    }
45    public IValueParameter<IntValue> ImprovementAttemptsParameter {
46      get { return (IValueParameter<IntValue>)Parameters["ImprovementAttempts"]; }
47    }
48    public IValueLookupParameter<IntValue> LocalEvaluatedSolutions {
49      get { return (IValueLookupParameter<IntValue>)Parameters["LocalEvaluatedSolutions"]; }
50    }
51    public ILookupParameter<IRandom> RandomParameter {
52      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
53    }
54    public IValueParameter<IntValue> SampleSizeParameter {
55      get { return (IValueParameter<IntValue>)Parameters["SampleSize"]; }
56    }
57    public IValueLookupParameter<IItem> SolutionParameter {
58      get { return (IValueLookupParameter<IItem>)Parameters["Solution"]; }
59    }
60    #endregion
61
62    [StorableConstructor]
63    protected VRPImprovementOperator(bool deserializing) : base(deserializing) { }
64    protected VRPImprovementOperator(VRPImprovementOperator original, Cloner cloner) : base(original, cloner) { }
65    protected VRPImprovementOperator()
66      : base() {
67      #region Create parameters
68      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope that contains the solution to be improved."));
69      Parameters.Add(new ValueParameter<IntValue>("ImprovementAttempts", "The number of improvement attempts the operator should perform.", new IntValue(100)));
70      Parameters.Add(new ValueLookupParameter<IntValue>("LocalEvaluatedSolutions", "The number of evaluated solutions."));
71      Parameters.Add(new LookupParameter<IRandom>("Random", "A pseudo random number generator."));
72      Parameters.Add(new ValueParameter<IntValue>("SampleSize", "The number of moves that should be executed.", new IntValue(25)));
73      Parameters.Add(new ValueLookupParameter<IItem>("Solution", "The solution to be improved. This parameter is used for name translation only."));
74      #endregion
75    }
76
77    public override IOperation InstrumentedApply() {
78      var solution = SolutionParameter.ActualValue as IVRPEncoding;
79      var potvinSolution = solution is PotvinEncoding ? solution as PotvinEncoding : PotvinEncoding.ConvertFrom(solution, ProblemInstance);
80
81      if (solution == null)
82        throw new ArgumentException("Cannot improve solution because it has the wrong type.");
83
84      int evaluatedSolutions = Improve(potvinSolution);
85
86      SolutionParameter.ActualValue = solution;
87      LocalEvaluatedSolutions.ActualValue = new IntValue(evaluatedSolutions);
88
89      return base.InstrumentedApply();
90    }
91
92    protected abstract int Improve(PotvinEncoding solution);
93  }
94}
Note: See TracBrowser for help on using the repository browser.