[8346] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[11576] | 3 | * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[8346] | 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.Potvin;
|
---|
| 31 | using HeuristicLab.Problems.VehicleRouting.Interfaces;
|
---|
| 32 | using HeuristicLab.Problems.VehicleRouting.Variants;
|
---|
| 33 |
|
---|
| 34 | namespace HeuristicLab.Problems.VehicleRouting {
|
---|
| 35 | /// <summary>
|
---|
[8894] | 36 | /// A base class for operators which improve VRP solutions.
|
---|
[8346] | 37 | /// </summary>
|
---|
[8894] | 38 | [Item("VRPImprovementOperator", "A base class for operators which improve VRP solutions.")]
|
---|
[8346] | 39 | [StorableClass]
|
---|
[8894] | 40 | public abstract class VRPImprovementOperator : VRPOperator, IGeneralVRPOperator, ISingleObjectiveImprovementOperator {
|
---|
[8346] | 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 | }
|
---|
[8894] | 48 | public IValueLookupParameter<IntValue> LocalEvaluatedSolutions {
|
---|
| 49 | get { return (IValueLookupParameter<IntValue>)Parameters["LocalEvaluatedSolutions"]; }
|
---|
[8346] | 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]
|
---|
[8894] | 63 | protected VRPImprovementOperator(bool deserializing) : base(deserializing) { }
|
---|
| 64 | protected VRPImprovementOperator(VRPImprovementOperator original, Cloner cloner) : base(original, cloner) { }
|
---|
| 65 | protected VRPImprovementOperator()
|
---|
[8346] | 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)));
|
---|
[8894] | 70 | Parameters.Add(new ValueLookupParameter<IntValue>("LocalEvaluatedSolutions", "The number of evaluated solutions."));
|
---|
[8346] | 71 | Parameters.Add(new LookupParameter<IRandom>("Random", "A pseudo random number generator."));
|
---|
[8894] | 72 | Parameters.Add(new ValueParameter<IntValue>("SampleSize", "The number of moves that should be executed.", new IntValue(25)));
|
---|
[8346] | 73 | Parameters.Add(new ValueLookupParameter<IItem>("Solution", "The solution to be improved. This parameter is used for name translation only."));
|
---|
| 74 | #endregion
|
---|
| 75 | }
|
---|
| 76 |
|
---|
[10298] | 77 | public override IOperation InstrumentedApply() {
|
---|
[8894] | 78 | var solution = SolutionParameter.ActualValue as IVRPEncoding;
|
---|
| 79 | var potvinSolution = solution is PotvinEncoding ? solution as PotvinEncoding : PotvinEncoding.ConvertFrom(solution, ProblemInstance);
|
---|
[8346] | 80 |
|
---|
| 81 | if (solution == null)
|
---|
| 82 | throw new ArgumentException("Cannot improve solution because it has the wrong type.");
|
---|
| 83 |
|
---|
[8894] | 84 | int evaluatedSolutions = Improve(potvinSolution);
|
---|
[8346] | 85 |
|
---|
[8894] | 86 | SolutionParameter.ActualValue = solution;
|
---|
| 87 | LocalEvaluatedSolutions.ActualValue = new IntValue(evaluatedSolutions);
|
---|
[8346] | 88 |
|
---|
[10298] | 89 | return base.InstrumentedApply();
|
---|
[8346] | 90 | }
|
---|
[8894] | 91 |
|
---|
| 92 | protected abstract int Improve(PotvinEncoding solution);
|
---|
[8346] | 93 | }
|
---|
| 94 | }
|
---|