#region License Information
/* HeuristicLab
* Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Optimization;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
using HeuristicLab.Problems.VehicleRouting.Encodings;
using HeuristicLab.Problems.VehicleRouting.Encodings.Potvin;
using HeuristicLab.Problems.VehicleRouting.Interfaces;
using HeuristicLab.Problems.VehicleRouting.Variants;
namespace HeuristicLab.Problems.VehicleRouting {
///
/// A base class for operators which improve VRP solutions.
///
[Item("VRPImprovementOperator", "A base class for operators which improve VRP solutions.")]
[StorableType("E45EFE04-EFFE-43A9-A390-BFCDAE0F1EA9")]
public abstract class VRPImprovementOperator : VRPOperator, IGeneralVRPOperator, ISingleObjectiveImprovementOperator {
#region Parameter properties
public ScopeParameter CurrentScopeParameter {
get { return (ScopeParameter)Parameters["CurrentScope"]; }
}
public IValueParameter ImprovementAttemptsParameter {
get { return (IValueParameter)Parameters["ImprovementAttempts"]; }
}
public IValueLookupParameter LocalEvaluatedSolutions {
get { return (IValueLookupParameter)Parameters["LocalEvaluatedSolutions"]; }
}
public ILookupParameter RandomParameter {
get { return (ILookupParameter)Parameters["Random"]; }
}
public IValueParameter SampleSizeParameter {
get { return (IValueParameter)Parameters["SampleSize"]; }
}
public IValueLookupParameter SolutionParameter {
get { return (IValueLookupParameter)Parameters["Solution"]; }
}
#endregion
[StorableConstructor]
protected VRPImprovementOperator(bool deserializing) : base(deserializing) { }
protected VRPImprovementOperator(VRPImprovementOperator original, Cloner cloner) : base(original, cloner) { }
protected VRPImprovementOperator()
: base() {
#region Create parameters
Parameters.Add(new ScopeParameter("CurrentScope", "The current scope that contains the solution to be improved."));
Parameters.Add(new ValueParameter("ImprovementAttempts", "The number of improvement attempts the operator should perform.", new IntValue(100)));
Parameters.Add(new ValueLookupParameter("LocalEvaluatedSolutions", "The number of evaluated solutions."));
Parameters.Add(new LookupParameter("Random", "A pseudo random number generator."));
Parameters.Add(new ValueParameter("SampleSize", "The number of moves that should be executed.", new IntValue(25)));
Parameters.Add(new ValueLookupParameter("Solution", "The solution to be improved. This parameter is used for name translation only."));
#endregion
}
public override IOperation InstrumentedApply() {
var solution = SolutionParameter.ActualValue as IVRPEncoding;
var potvinSolution = solution is PotvinEncoding ? solution as PotvinEncoding : PotvinEncoding.ConvertFrom(solution, ProblemInstance);
if (solution == null)
throw new ArgumentException("Cannot improve solution because it has the wrong type.");
int evaluatedSolutions = Improve(potvinSolution);
SolutionParameter.ActualValue = solution;
LocalEvaluatedSolutions.ActualValue = new IntValue(evaluatedSolutions);
return base.InstrumentedApply();
}
protected abstract int Improve(PotvinEncoding solution);
}
}