[14702] | 1 | using HeuristicLab.Operators;
|
---|
| 2 | using System;
|
---|
| 3 | using System.Collections.Generic;
|
---|
| 4 | using System.Linq;
|
---|
| 5 | using System.Text;
|
---|
| 6 | using System.Threading.Tasks;
|
---|
| 7 | using HeuristicLab.Common;
|
---|
| 8 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 9 | using HeuristicLab.Core;
|
---|
| 10 | using HeuristicLab.Data;
|
---|
| 11 | using HeuristicLab.Parameters;
|
---|
| 12 | using HeuristicLab.Optimization;
|
---|
| 13 | using HeuristicLab.PluginInfrastructure;
|
---|
| 14 | using HeuristicLab.Analysis;
|
---|
| 15 |
|
---|
| 16 | namespace HeuristicLab.Algorithms.SimulatedAnnealing
|
---|
| 17 | {
|
---|
| 18 | [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).")]
|
---|
| 19 | [StorableClass]
|
---|
| 20 | public class ConsecutiveRejectionTemperatureResetOperator : SingleSuccessorOperator, IReheatingOperator
|
---|
| 21 | {
|
---|
| 22 | #region Strings
|
---|
| 23 | private const string AnnealingOperatorName = "AnnealingOperator";
|
---|
| 24 | private const string LowerTemperatureName = "LowerTemperature";
|
---|
| 25 | private const string IterationsName = "Iterations";
|
---|
| 26 | private const string TemperatureStartIndexName = "TemperatureStartIndex";
|
---|
| 27 | private const string StartTemperatureName = "StartTemperature";
|
---|
| 28 | private const string EndTemperatureName = "EndTemperature";
|
---|
| 29 | private const string TemperatureName = "Temperature";
|
---|
| 30 | private const string MaximumIterationsName = "MaximumIterations";
|
---|
[15001] | 31 | private const string InitialTemperatureName = "InitialTemperature";
|
---|
[14702] | 32 |
|
---|
| 33 | private const string ThresholdName = "Threshold";
|
---|
| 34 | private const string IsAcceptedName = "IsAccepted";
|
---|
[15001] | 35 | private const string ConsecutiveRejectedSolutionsCountName = "ConsecutiveRejectedSolutions";
|
---|
[14702] | 36 | private const string BaseResetTemperatureName = "BaseResetTemperature";
|
---|
| 37 | #endregion
|
---|
| 38 |
|
---|
| 39 | #region Parameters
|
---|
[15001] | 40 | private ValueLookupParameter<DoubleValue> BaseResetTemperatureParameter
|
---|
[14702] | 41 | {
|
---|
[15001] | 42 | get { return (ValueLookupParameter<DoubleValue>)Parameters[BaseResetTemperatureName]; }
|
---|
[14702] | 43 | }
|
---|
| 44 | public ILookupParameter<DoubleValue> TemperatureParameter
|
---|
| 45 | {
|
---|
| 46 | get { return (ILookupParameter<DoubleValue>)Parameters[TemperatureName]; }
|
---|
| 47 | }
|
---|
| 48 | public ILookupParameter<DoubleValue> LowerTemperatureParameter
|
---|
| 49 | {
|
---|
| 50 | get { return (ILookupParameter<DoubleValue>)Parameters[LowerTemperatureName]; }
|
---|
| 51 | }
|
---|
| 52 | public ILookupParameter<DoubleValue> StartTemperatureParameter
|
---|
| 53 | {
|
---|
| 54 | get { return (ILookupParameter<DoubleValue>)Parameters[StartTemperatureName]; }
|
---|
| 55 | }
|
---|
| 56 | public ILookupParameter<DoubleValue> EndTemperatureParameter
|
---|
| 57 | {
|
---|
| 58 | get { return (ILookupParameter<DoubleValue>)Parameters[EndTemperatureName]; }
|
---|
| 59 | }
|
---|
| 60 | public ILookupParameter<IntValue> TemperatureStartIndexParameter
|
---|
| 61 | {
|
---|
| 62 | get { return (ILookupParameter<IntValue>)Parameters[TemperatureStartIndexName]; }
|
---|
| 63 | }
|
---|
| 64 | public ILookupParameter<IntValue> IterationsParameter
|
---|
| 65 | {
|
---|
| 66 | get { return (ILookupParameter<IntValue>)Parameters[IterationsName]; }
|
---|
| 67 | }
|
---|
| 68 | public ILookupParameter<IOperator> AnnealingOperatorParameter
|
---|
| 69 | {
|
---|
| 70 | get { return (ILookupParameter<IOperator>)Parameters[AnnealingOperatorName]; }
|
---|
| 71 | }
|
---|
| 72 | public ILookupParameter<IntValue> MaximumIterationsParameter
|
---|
| 73 | {
|
---|
| 74 | get { return (ILookupParameter<IntValue>)Parameters[MaximumIterationsName]; }
|
---|
| 75 | }
|
---|
| 76 | private ValueParameter<IntValue> ThresholdParameter
|
---|
| 77 | {
|
---|
| 78 | get { return (ValueParameter<IntValue>)Parameters[ThresholdName]; }
|
---|
| 79 | }
|
---|
| 80 | public ILookupParameter<BoolValue> IsAcceptedParameter
|
---|
| 81 | {
|
---|
| 82 | get { return (ILookupParameter<BoolValue>)Parameters[IsAcceptedName]; }
|
---|
| 83 | }
|
---|
[15001] | 84 | public ILookupParameter<IntValue> ConsecutiveRejectedSolutionsCountParameter
|
---|
[14702] | 85 | {
|
---|
[15001] | 86 | get { return (ILookupParameter<IntValue>)Parameters[ConsecutiveRejectedSolutionsCountName]; }
|
---|
[14702] | 87 | }
|
---|
| 88 | #endregion
|
---|
| 89 |
|
---|
| 90 | public ConsecutiveRejectionTemperatureResetOperator() : base()
|
---|
| 91 | {
|
---|
| 92 | #region Create parameters
|
---|
| 93 | Parameters.Add(new LookupParameter<DoubleValue>(TemperatureName, "The current temperature."));
|
---|
| 94 | Parameters.Add(new LookupParameter<DoubleValue>(LowerTemperatureName, "The lower bound of the temperature."));
|
---|
| 95 | Parameters.Add(new LookupParameter<IntValue>(IterationsName, "The number of iterations."));
|
---|
| 96 | Parameters.Add(new LookupParameter<IOperator>(AnnealingOperatorName, "The operator that cools the temperature."));
|
---|
| 97 | Parameters.Add(new LookupParameter<IntValue>(TemperatureStartIndexName, "The index where the annealing or heating was last changed."));
|
---|
| 98 | Parameters.Add(new LookupParameter<DoubleValue>(StartTemperatureName, "The temperature from which cooling should occur."));
|
---|
| 99 | Parameters.Add(new LookupParameter<DoubleValue>(EndTemperatureName, "The temperature to which should be cooled."));
|
---|
| 100 | Parameters.Add(new LookupParameter<IntValue>(MaximumIterationsName, "The maximum number of iterations which should be processed."));
|
---|
| 101 | Parameters.Add(new LookupParameter<BoolValue>(IsAcceptedName, "Whether the move was accepted or not."));
|
---|
[15001] | 102 | Parameters.Add(new LookupParameter<IntValue>(ConsecutiveRejectedSolutionsCountName, "Amount of consecutive rejected solutions."));
|
---|
[14702] | 103 | Parameters.Add(new ValueParameter<IntValue>(ThresholdName, "How many consecutive rejected solutions must occur to trigger a reheat.", new IntValue(10)));
|
---|
[15001] | 104 | Parameters.Add(new ValueLookupParameter<DoubleValue>(BaseResetTemperatureName, "The base reset temperature.", InitialTemperatureName));
|
---|
| 105 |
|
---|
[14702] | 106 | #endregion
|
---|
| 107 |
|
---|
| 108 | Parameterize();
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | [StorableConstructor]
|
---|
| 112 | protected ConsecutiveRejectionTemperatureResetOperator(bool deserializing) : base(deserializing) { }
|
---|
| 113 | protected ConsecutiveRejectionTemperatureResetOperator(ConsecutiveRejectionTemperatureResetOperator original, Cloner cloner) : base(original, cloner) { }
|
---|
| 114 |
|
---|
| 115 |
|
---|
| 116 | public override IDeepCloneable Clone(Cloner cloner)
|
---|
| 117 | {
|
---|
| 118 | return new ConsecutiveRejectionTemperatureResetOperator(this, cloner);
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | public void Parameterize()
|
---|
| 122 | {
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | public override IOperation Apply()
|
---|
| 126 | {
|
---|
[15001] | 127 | var count = ConsecutiveRejectedSolutionsCountParameter.ActualValue;
|
---|
[14702] | 128 |
|
---|
[15001] | 129 | count.Value = !IsAcceptedParameter.ActualValue.Value ? count.Value + 1 : 0;
|
---|
[14702] | 130 |
|
---|
[15001] | 131 | var heating = count.Value == ThresholdParameter.Value.Value;
|
---|
[14702] | 132 |
|
---|
[15001] | 133 | if(heating) count.Value = 0;
|
---|
[14702] | 134 |
|
---|
| 135 | return heating ? Heat() : Cool();
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | private IOperation Heat()
|
---|
| 139 | {
|
---|
[15001] | 140 | var temperature = Math.Max(BaseResetTemperatureParameter.ActualValue.Value, TemperatureParameter.ActualValue.Value);
|
---|
[14702] | 141 |
|
---|
| 142 | TemperatureParameter.ActualValue.Value = temperature;
|
---|
| 143 | TemperatureStartIndexParameter.ActualValue.Value = Math.Max(0, IterationsParameter.ActualValue.Value - 1);
|
---|
| 144 | StartTemperatureParameter.ActualValue.Value = TemperatureParameter.ActualValue.Value;
|
---|
| 145 | return base.Apply();
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | private IOperation Cool()
|
---|
| 149 | {
|
---|
| 150 | return new OperationCollection {
|
---|
| 151 | ExecutionContext.CreateOperation(AnnealingOperatorParameter.ActualValue),
|
---|
| 152 | base.Apply()
|
---|
| 153 | };
|
---|
| 154 | }
|
---|
| 155 | }
|
---|
| 156 | }
|
---|