Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.VehicleRouting/3.4/Improver/VRPImprovementOperator.cs @ 8346

Last change on this file since 8346 was 8346, checked in by jkarder, 12 years ago

#1331:

  • added operators for the VehicleRouting problem
  • minor code improvements
File size: 5.0 KB
Line 
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
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.Alba;
31using HeuristicLab.Problems.VehicleRouting.Encodings.Potvin;
32using HeuristicLab.Problems.VehicleRouting.Interfaces;
33using HeuristicLab.Problems.VehicleRouting.Variants;
34
35namespace 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}
Note: See TracBrowser for help on using the repository browser.