source: branches/jschiess/HeuristicLab.Algorithms.SimulatedAnnealing/3.4/ConsecutiveRejectionTemperatureResetOperator.cs @ 15001

Last change on this file since 15001 was 15001, checked in by jschiess, 5 years ago

#1836 SA reheating strategies: Newest version of reheating

File size: 7.5 KB
Line 
1using HeuristicLab.Operators;
2using System;
3using System.Collections.Generic;
4using System.Linq;
5using System.Text;
6using System.Threading.Tasks;
7using HeuristicLab.Common;
8using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
9using HeuristicLab.Core;
10using HeuristicLab.Data;
11using HeuristicLab.Parameters;
12using HeuristicLab.Optimization;
13using HeuristicLab.PluginInfrastructure;
14using HeuristicLab.Analysis;
15
16namespace 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";
31        private const string InitialTemperatureName = "InitialTemperature";
32
33        private const string ThresholdName = "Threshold";
34        private const string IsAcceptedName = "IsAccepted";
35        private const string ConsecutiveRejectedSolutionsCountName = "ConsecutiveRejectedSolutions";
36        private const string BaseResetTemperatureName = "BaseResetTemperature";
37        #endregion
38
39        #region Parameters
40        private ValueLookupParameter<DoubleValue> BaseResetTemperatureParameter
41        {
42            get { return (ValueLookupParameter<DoubleValue>)Parameters[BaseResetTemperatureName]; }
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        }
84        public ILookupParameter<IntValue> ConsecutiveRejectedSolutionsCountParameter
85        {
86            get { return (ILookupParameter<IntValue>)Parameters[ConsecutiveRejectedSolutionsCountName]; }
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."));
102            Parameters.Add(new LookupParameter<IntValue>(ConsecutiveRejectedSolutionsCountName, "Amount of consecutive rejected solutions."));
103            Parameters.Add(new ValueParameter<IntValue>(ThresholdName, "How many consecutive rejected solutions must occur to trigger a reheat.", new IntValue(10)));
104            Parameters.Add(new ValueLookupParameter<DoubleValue>(BaseResetTemperatureName, "The base reset temperature.", InitialTemperatureName));
105         
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        {
127            var count = ConsecutiveRejectedSolutionsCountParameter.ActualValue;
128
129            count.Value = !IsAcceptedParameter.ActualValue.Value ? count.Value + 1 : 0;
130
131            var heating = count.Value == ThresholdParameter.Value.Value;
132
133            if(heating) count.Value = 0;
134
135            return heating ? Heat() : Cool();
136        }
137
138        private IOperation Heat()
139        {
140            var temperature = Math.Max(BaseResetTemperatureParameter.ActualValue.Value, TemperatureParameter.ActualValue.Value);
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}
Note: See TracBrowser for help on using the repository browser.