1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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 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.Encodings;
|
---|
30 | using HeuristicLab.Problems.VehicleRouting.Encodings.Alba;
|
---|
31 | using HeuristicLab.Problems.VehicleRouting.Encodings.Potvin;
|
---|
32 | using HeuristicLab.Problems.VehicleRouting.Interfaces;
|
---|
33 | using HeuristicLab.Problems.VehicleRouting.Variants;
|
---|
34 |
|
---|
35 | namespace HeuristicLab.Problems.VehicleRouting {
|
---|
36 | /// <summary>
|
---|
37 | /// An operator that improves vehicle routing solutions.
|
---|
38 | /// </summary>
|
---|
39 | [Item("VRPImprovementOperator", "An operator that improves vehicle routing solutions.")]
|
---|
40 | [StorableClass]
|
---|
41 | public sealed class VRPImprovementOperator : VRPOperator, IGeneralVRPOperator, ISingleObjectiveImprovementOperator {
|
---|
42 | #region Parameter properties
|
---|
43 | public ScopeParameter CurrentScopeParameter {
|
---|
44 | get { return (ScopeParameter)Parameters["CurrentScope"]; }
|
---|
45 | }
|
---|
46 | public IValueParameter<IntValue> ImprovementAttemptsParameter {
|
---|
47 | get { return (IValueParameter<IntValue>)Parameters["ImprovementAttempts"]; }
|
---|
48 | }
|
---|
49 | public IValueParameter<IntValue> LambdaParameter {
|
---|
50 | get { return (IValueParameter<IntValue>)Parameters["Lambda"]; }
|
---|
51 | }
|
---|
52 | public ILookupParameter<IRandom> RandomParameter {
|
---|
53 | get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
|
---|
54 | }
|
---|
55 | public IValueParameter<IntValue> SampleSizeParameter {
|
---|
56 | get { return (IValueParameter<IntValue>)Parameters["SampleSize"]; }
|
---|
57 | }
|
---|
58 | public IValueLookupParameter<IItem> SolutionParameter {
|
---|
59 | get { return (IValueLookupParameter<IItem>)Parameters["Solution"]; }
|
---|
60 | }
|
---|
61 | #endregion
|
---|
62 |
|
---|
63 | [StorableConstructor]
|
---|
64 | private VRPImprovementOperator(bool deserializing) : base(deserializing) { }
|
---|
65 | private VRPImprovementOperator(VRPImprovementOperator original, Cloner cloner) : base(original, cloner) { }
|
---|
66 | public VRPImprovementOperator()
|
---|
67 | : base() {
|
---|
68 | #region Create parameters
|
---|
69 | Parameters.Add(new ScopeParameter("CurrentScope", "The current scope that contains the solution to be improved."));
|
---|
70 | Parameters.Add(new ValueParameter<IntValue>("ImprovementAttempts", "The number of improvement attempts the operator should perform.", new IntValue(100)));
|
---|
71 | Parameters.Add(new ValueParameter<IntValue>("Lambda", "The number of neighbors that should be exchanged.", new IntValue(1)));
|
---|
72 | Parameters.Add(new LookupParameter<IRandom>("Random", "A pseudo random number generator."));
|
---|
73 | Parameters.Add(new ValueParameter<IntValue>("SampleSize", "The number of moves that should be executed.", new IntValue(100)));
|
---|
74 | Parameters.Add(new ValueLookupParameter<IItem>("Solution", "The solution to be improved. This parameter is used for name translation only."));
|
---|
75 | #endregion
|
---|
76 | }
|
---|
77 |
|
---|
78 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
79 | return new VRPImprovementOperator(this, cloner);
|
---|
80 | }
|
---|
81 |
|
---|
82 | public override IOperation Apply() {
|
---|
83 | AlbaEncoding solution = SolutionParameter.ActualValue is AlbaEncoding
|
---|
84 | ? SolutionParameter.ActualValue as AlbaEncoding
|
---|
85 | : AlbaEncoding.ConvertFrom(SolutionParameter.ActualValue as IVRPEncoding, ProblemInstance);
|
---|
86 |
|
---|
87 | if (solution == null)
|
---|
88 | throw new ArgumentException("Cannot improve solution because it has the wrong type.");
|
---|
89 |
|
---|
90 | double quality = -1;
|
---|
91 | int evaluatedSolutions;
|
---|
92 |
|
---|
93 | AlbaLambdaInterchangeLocalImprovementOperator.Apply(solution, ImprovementAttemptsParameter.Value.Value,
|
---|
94 | LambdaParameter.Value.Value, SampleSizeParameter.Value.Value,
|
---|
95 | RandomParameter.ActualValue, ProblemInstance, ref quality,
|
---|
96 | out evaluatedSolutions);
|
---|
97 |
|
---|
98 | SolutionParameter.ActualValue = PotvinEncoding.ConvertFrom(solution, ProblemInstance);
|
---|
99 | CurrentScopeParameter.ActualValue.Variables.Add(new Variable("LocalEvaluatedSolutions", new IntValue(evaluatedSolutions)));
|
---|
100 |
|
---|
101 | return base.Apply();
|
---|
102 | }
|
---|
103 | }
|
---|
104 | }
|
---|