using HeuristicLab.Operators; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using HeuristicLab.Common; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Parameters; using HeuristicLab.Optimization; using HeuristicLab.PluginInfrastructure; using HeuristicLab.Analysis; namespace HeuristicLab.Algorithms.SimulatedAnnealing { [Item("ConsecutiveRejectionTemperatureResetOperator", "The operator resets the temperature to the ResetTemperature when N consecutive solutions are rejected.")] [StorableClass] public class ConsecutiveRejectionTemperatureResetOperator : SingleSuccessorOperator, IReheatingOperator { #region Strings private const string AnnealingOperatorName = "AnnealingOperator"; private const string LowerTemperatureName = "LowerTemperature"; private const string IterationsName = "Iterations"; private const string TemperatureStartIndexName = "TemperatureStartIndex"; private const string StartTemperatureName = "StartTemperature"; private const string EndTemperatureName = "EndTemperature"; private const string TemperatureName = "Temperature"; private const string MaximumIterationsName = "MaximumIterations"; private const string InitialTemperatureName = "InitialTemperature"; private const string ThresholdName = "Threshold"; private const string IsAcceptedName = "IsAccepted"; private const string ConsecutiveRejectedSolutionsCountName = "ConsecutiveRejectedSolutions"; private const string ResetTemperatureName = "ResetTemperature"; private const string LastAcceptedQualityName = "LastAcceptedQuality"; private const string MoveQualityName = "MoveQuality"; #endregion #region Parameters private ValueLookupParameter ResetTemperatureParameter { get { return (ValueLookupParameter)Parameters[ResetTemperatureName]; } } public ILookupParameter TemperatureParameter { get { return (ILookupParameter)Parameters[TemperatureName]; } } public ILookupParameter LowerTemperatureParameter { get { return (ILookupParameter)Parameters[LowerTemperatureName]; } } public ILookupParameter StartTemperatureParameter { get { return (ILookupParameter)Parameters[StartTemperatureName]; } } public ILookupParameter EndTemperatureParameter { get { return (ILookupParameter)Parameters[EndTemperatureName]; } } public ILookupParameter TemperatureStartIndexParameter { get { return (ILookupParameter)Parameters[TemperatureStartIndexName]; } } public ILookupParameter IterationsParameter { get { return (ILookupParameter)Parameters[IterationsName]; } } public ILookupParameter AnnealingOperatorParameter { get { return (ILookupParameter)Parameters[AnnealingOperatorName]; } } public ILookupParameter MaximumIterationsParameter { get { return (ILookupParameter)Parameters[MaximumIterationsName]; } } private ValueParameter ThresholdParameter { get { return (ValueParameter)Parameters[ThresholdName]; } } public ILookupParameter IsAcceptedParameter { get { return (ILookupParameter)Parameters[IsAcceptedName]; } } public ILookupParameter ConsecutiveRejectedSolutionsCountParameter { get { return (ILookupParameter)Parameters[ConsecutiveRejectedSolutionsCountName]; } } public ILookupParameter MoveQualityParameter { get { return (ILookupParameter)Parameters[MoveQualityName]; } } public ILookupParameter LastAcceptedQualityParameter { get { return (ILookupParameter)Parameters[LastAcceptedQualityName]; } } #endregion public ConsecutiveRejectionTemperatureResetOperator() : base() { #region Create parameters Parameters.Add(new LookupParameter(TemperatureName, "The current temperature.")); Parameters.Add(new LookupParameter(LowerTemperatureName, "The lower bound of the temperature.")); Parameters.Add(new LookupParameter(IterationsName, "The number of iterations.")); Parameters.Add(new LookupParameter(AnnealingOperatorName, "The operator that cools the temperature.")); Parameters.Add(new LookupParameter(TemperatureStartIndexName, "The index where the annealing or heating was last changed.")); Parameters.Add(new LookupParameter(StartTemperatureName, "The temperature from which cooling should occur.")); Parameters.Add(new LookupParameter(EndTemperatureName, "The temperature to which should be cooled.")); Parameters.Add(new LookupParameter(MaximumIterationsName, "The maximum number of iterations which should be processed.")); Parameters.Add(new LookupParameter(IsAcceptedName, "Whether the move was accepted or not.")); Parameters.Add(new LookupParameter(ConsecutiveRejectedSolutionsCountName, "Amount of consecutive rejected solutions.")); Parameters.Add(new ValueParameter(ThresholdName, "How many consecutive rejected solutions must occur to trigger a reheat.", new IntValue(10))); Parameters.Add(new ValueLookupParameter(ResetTemperatureName, "The base reset temperature.", InitialTemperatureName)); Parameters.Add(new LookupParameter(LastAcceptedQualityName, "Quality of last accepted solution.")); Parameters.Add(new LookupParameter(MoveQualityName, "The value which represents the quality of a move.")); #endregion Parameterize(); } [StorableConstructor] protected ConsecutiveRejectionTemperatureResetOperator(bool deserializing) : base(deserializing) { } protected ConsecutiveRejectionTemperatureResetOperator(ConsecutiveRejectionTemperatureResetOperator original, Cloner cloner) : base(original, cloner) { } public override IDeepCloneable Clone(Cloner cloner) { return new ConsecutiveRejectionTemperatureResetOperator(this, cloner); } public void Parameterize() { } public override IOperation Apply() { if(!IsAcceptedParameter.ActualValue.Value || LastAcceptedQualityParameter.ActualValue.Value.Equals(MoveQualityParameter.ActualValue.Value)) { ConsecutiveRejectedSolutionsCountParameter.ActualValue.Value++; }else { ConsecutiveRejectedSolutionsCountParameter.ActualValue.Value = 0; } var heating = ConsecutiveRejectedSolutionsCountParameter.ActualValue.Value >= ThresholdParameter.Value.Value; if (heating) { ConsecutiveRejectedSolutionsCountParameter.ActualValue.Value = 0; } if (IsAcceptedParameter.ActualValue.Value) { LastAcceptedQualityParameter.ActualValue.Value = MoveQualityParameter.ActualValue.Value; } return heating ? Heat() : Cool(); } private IOperation Heat() { var temperature = Math.Max(ResetTemperatureParameter.ActualValue.Value, TemperatureParameter.ActualValue.Value); TemperatureParameter.ActualValue.Value = temperature; TemperatureStartIndexParameter.ActualValue.Value = Math.Max(0, IterationsParameter.ActualValue.Value - 1); StartTemperatureParameter.ActualValue.Value = TemperatureParameter.ActualValue.Value; return base.Apply(); } private IOperation Cool() { return new OperationCollection { ExecutionContext.CreateOperation(AnnealingOperatorParameter.ActualValue), base.Apply() }; } } }