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 X when N consecutive solutions are rejected. X is calculated by BaseResetTemperature * (MultiplyWithEachReheat to the power of the current number of reheats).")] [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 BaseResetTemperatureName = "BaseResetTemperature"; #endregion #region Parameters private ValueLookupParameter BaseResetTemperatureParameter { get { return (ValueLookupParameter)Parameters[BaseResetTemperatureName]; } } 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]; } } #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(BaseResetTemperatureName, "The base reset temperature.", InitialTemperatureName)); #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() { var count = ConsecutiveRejectedSolutionsCountParameter.ActualValue; count.Value = !IsAcceptedParameter.ActualValue.Value ? count.Value + 1 : 0; var heating = count.Value == ThresholdParameter.Value.Value; if(heating) count.Value = 0; return heating ? Heat() : Cool(); } private IOperation Heat() { var temperature = Math.Max(BaseResetTemperatureParameter.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() }; } } }