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