Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1836_jschiess/HeuristicLab.Algorithms.SimulatedAnnealing/3.4/ConsecutiveRejectionTemperatureResetOperator.cs @ 16113

Last change on this file since 16113 was 15333, checked in by jschiess, 7 years ago

#1836 SA reheating strategies
+ added an adaptive temperature control strategy
+ some refactorings

File size: 8.7 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 the ResetTemperature when N consecutive solutions are rejected.")]
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        private const string ThresholdName = "Threshold";
33        private const string IsAcceptedName = "IsAccepted";
34        private const string ConsecutiveRejectedSolutionsCountName = "ConsecutiveRejectedSolutions";
35        private const string ResetTemperatureName = "ResetTemperature";
36        private const string LastAcceptedQualityName = "LastAcceptedQuality";
37        private const string MoveQualityName = "MoveQuality";
38        #endregion
39
40        #region Parameters
41        private ValueLookupParameter<DoubleValue> ResetTemperatureParameter
42        {
43            get { return (ValueLookupParameter<DoubleValue>)Parameters[ResetTemperatureName]; }
44        }
45        public ILookupParameter<DoubleValue> TemperatureParameter
46        {
47            get { return (ILookupParameter<DoubleValue>)Parameters[TemperatureName]; }
48        }
49        public ILookupParameter<DoubleValue> LowerTemperatureParameter
50        {
51            get { return (ILookupParameter<DoubleValue>)Parameters[LowerTemperatureName]; }
52        }
53        public ILookupParameter<DoubleValue> StartTemperatureParameter
54        {
55            get { return (ILookupParameter<DoubleValue>)Parameters[StartTemperatureName]; }
56        }
57        public ILookupParameter<DoubleValue> EndTemperatureParameter
58        {
59            get { return (ILookupParameter<DoubleValue>)Parameters[EndTemperatureName]; }
60        }
61        public ILookupParameter<IntValue> TemperatureStartIndexParameter
62        {
63            get { return (ILookupParameter<IntValue>)Parameters[TemperatureStartIndexName]; }
64        }
65        public ILookupParameter<IntValue> IterationsParameter
66        {
67            get { return (ILookupParameter<IntValue>)Parameters[IterationsName]; }
68        }
69        public ILookupParameter<IOperator> AnnealingOperatorParameter
70        {
71            get { return (ILookupParameter<IOperator>)Parameters[AnnealingOperatorName]; }
72        }
73        public ILookupParameter<IntValue> MaximumIterationsParameter
74        {
75            get { return (ILookupParameter<IntValue>)Parameters[MaximumIterationsName]; }
76        }
77        private ValueParameter<IntValue> ThresholdParameter
78        {
79            get { return (ValueParameter<IntValue>)Parameters[ThresholdName]; }
80        }
81        public ILookupParameter<BoolValue> IsAcceptedParameter
82        {
83            get { return (ILookupParameter<BoolValue>)Parameters[IsAcceptedName]; }
84        }
85        public ILookupParameter<IntValue> ConsecutiveRejectedSolutionsCountParameter
86        {
87            get { return (ILookupParameter<IntValue>)Parameters[ConsecutiveRejectedSolutionsCountName]; }
88        }
89        public ILookupParameter<DoubleValue> MoveQualityParameter
90        {
91            get { return (ILookupParameter<DoubleValue>)Parameters[MoveQualityName]; }
92        }
93        public ILookupParameter<DoubleValue> LastAcceptedQualityParameter
94        {
95            get { return (ILookupParameter<DoubleValue>)Parameters[LastAcceptedQualityName]; }
96        }
97        #endregion
98
99        public ConsecutiveRejectionTemperatureResetOperator() : base()
100        {
101            #region Create parameters
102            Parameters.Add(new LookupParameter<DoubleValue>(TemperatureName, "The current temperature."));
103            Parameters.Add(new LookupParameter<DoubleValue>(LowerTemperatureName, "The lower bound of the temperature."));
104            Parameters.Add(new LookupParameter<IntValue>(IterationsName, "The number of iterations."));
105            Parameters.Add(new LookupParameter<IOperator>(AnnealingOperatorName, "The operator that cools the temperature."));
106            Parameters.Add(new LookupParameter<IntValue>(TemperatureStartIndexName, "The index where the annealing or heating was last changed."));
107            Parameters.Add(new LookupParameter<DoubleValue>(StartTemperatureName, "The temperature from which cooling should occur."));
108            Parameters.Add(new LookupParameter<DoubleValue>(EndTemperatureName, "The temperature to which should be cooled."));
109            Parameters.Add(new LookupParameter<IntValue>(MaximumIterationsName, "The maximum number of iterations which should be processed."));
110            Parameters.Add(new LookupParameter<BoolValue>(IsAcceptedName, "Whether the move was accepted or not."));
111            Parameters.Add(new LookupParameter<IntValue>(ConsecutiveRejectedSolutionsCountName, "Amount of consecutive rejected solutions."));
112            Parameters.Add(new ValueParameter<IntValue>(ThresholdName, "How many consecutive rejected solutions must occur to trigger a reheat.", new IntValue(10)));
113            Parameters.Add(new ValueLookupParameter<DoubleValue>(ResetTemperatureName, "The base reset temperature.", InitialTemperatureName));
114            Parameters.Add(new LookupParameter<DoubleValue>(LastAcceptedQualityName, "Quality of last accepted solution."));
115            Parameters.Add(new LookupParameter<DoubleValue>(MoveQualityName, "The value which represents the quality of a move."));
116
117            #endregion
118
119            Parameterize();
120        }
121
122        [StorableConstructor]
123        protected ConsecutiveRejectionTemperatureResetOperator(bool deserializing) : base(deserializing) { }
124        protected ConsecutiveRejectionTemperatureResetOperator(ConsecutiveRejectionTemperatureResetOperator original, Cloner cloner) : base(original, cloner) { }
125
126
127        public override IDeepCloneable Clone(Cloner cloner)
128        {
129            return new ConsecutiveRejectionTemperatureResetOperator(this, cloner);
130        }
131
132        public void Parameterize()
133        {
134        }
135
136        public override IOperation Apply()
137        {
138            if(!IsAcceptedParameter.ActualValue.Value || LastAcceptedQualityParameter.ActualValue.Value.Equals(MoveQualityParameter.ActualValue.Value))
139            {
140                ConsecutiveRejectedSolutionsCountParameter.ActualValue.Value++;
141            }else
142            {
143                ConsecutiveRejectedSolutionsCountParameter.ActualValue.Value = 0;
144            }
145
146
147            var heating = ConsecutiveRejectedSolutionsCountParameter.ActualValue.Value >= ThresholdParameter.Value.Value;
148
149            if (heating)
150            {
151                ConsecutiveRejectedSolutionsCountParameter.ActualValue.Value = 0;
152            }
153
154            if (IsAcceptedParameter.ActualValue.Value)
155            {
156                LastAcceptedQualityParameter.ActualValue.Value = MoveQualityParameter.ActualValue.Value;
157            }
158
159            return heating ? Heat() : Cool();
160        }
161
162        private IOperation Heat()
163        {
164            var temperature = Math.Max(ResetTemperatureParameter.ActualValue.Value, TemperatureParameter.ActualValue.Value);
165
166            TemperatureParameter.ActualValue.Value = temperature;
167            TemperatureStartIndexParameter.ActualValue.Value = Math.Max(0, IterationsParameter.ActualValue.Value - 1);
168            StartTemperatureParameter.ActualValue.Value = TemperatureParameter.ActualValue.Value;
169            return base.Apply();
170        }
171
172        private IOperation Cool()
173        {
174            return new OperationCollection {
175                    ExecutionContext.CreateOperation(AnnealingOperatorParameter.ActualValue),
176                    base.Apply()
177                };
178        }
179    }
180}
Note: See TracBrowser for help on using the repository browser.