Free cookie consent management tool by TermsFeed Policy Generator

source: branches/jschiess/HeuristicLab.Algorithms.SimulatedAnnealing/3.4/ConsecutiveRejectionTemperatureResetBasedOnAverageHillsizeOperator.cs @ 16099

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

#1836 SA reheating strategies: Newest version of reheating

File size: 11.2 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("ConsecutiveRejectionTemperatureResetBasedOnAverageHillsizeOperator", "The operator resets the temperature to X when N consecutive solutions are rejected. X is calculated like the initial temperature based on average hillsize.")]
19    [StorableClass]
20    public class ConsecutiveRejectionTemperatureResetBasedOnAverageHillsizeOperator : 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 ReheatRatioName = "ReheatRatio";
35        private const string IsAcceptedName = "IsAccepted";
36
37        private const string ConsecutiveRejectedSolutionsCountName = "ConsecutiveRejectedSolutions";
38        private const string LastQualityName = "LastQuality";
39        private const string UphillMovesMemoryName = "UphillMovesMemory";
40        private const string MaximizationName = "Maximization";
41        private const string MoveQualityName = "MoveQuality";
42        private const string InitialAcceptanceRateName = "InitialAcceptanceRate";
43        #endregion
44
45        #region Parameters
46        public ILookupParameter<DoubleValue> TemperatureParameter
47        {
48            get { return (ILookupParameter<DoubleValue>)Parameters[TemperatureName]; }
49        }
50        public ILookupParameter<DoubleValue> LowerTemperatureParameter
51        {
52            get { return (ILookupParameter<DoubleValue>)Parameters[LowerTemperatureName]; }
53        }
54        public ILookupParameter<DoubleValue> StartTemperatureParameter
55        {
56            get { return (ILookupParameter<DoubleValue>)Parameters[StartTemperatureName]; }
57        }
58        public ILookupParameter<DoubleValue> EndTemperatureParameter
59        {
60            get { return (ILookupParameter<DoubleValue>)Parameters[EndTemperatureName]; }
61        }
62        public ILookupParameter<IntValue> TemperatureStartIndexParameter
63        {
64            get { return (ILookupParameter<IntValue>)Parameters[TemperatureStartIndexName]; }
65        }
66        public ILookupParameter<IntValue> IterationsParameter
67        {
68            get { return (ILookupParameter<IntValue>)Parameters[IterationsName]; }
69        }
70        public ILookupParameter<IOperator> AnnealingOperatorParameter
71        {
72            get { return (ILookupParameter<IOperator>)Parameters[AnnealingOperatorName]; }
73        }
74        public ILookupParameter<IntValue> MaximumIterationsParameter
75        {
76            get { return (ILookupParameter<IntValue>)Parameters[MaximumIterationsName]; }
77        }
78        private ValueParameter<IntValue> ThresholdParameter
79        {
80            get { return (ValueParameter<IntValue>)Parameters[ThresholdName]; }
81        }
82        private ValueParameter<DoubleValue> ReheatRatioParameter
83        {
84            get { return (ValueParameter<DoubleValue>)Parameters[ReheatRatioName]; }
85        }
86        public ILookupParameter<BoolValue> IsAcceptedParameter
87        {
88            get { return (ILookupParameter<BoolValue>)Parameters[IsAcceptedName]; }
89        }
90        public ILookupParameter<IntValue> ConsecutiveRejectedSolutionsCountParameter
91        {
92            get { return (ILookupParameter<IntValue>)Parameters[ConsecutiveRejectedSolutionsCountName]; }
93        }
94        public ILookupParameter<ItemList<DoubleValue>> UphillMovesMemoryParameter
95        {
96            get { return (ILookupParameter<ItemList<DoubleValue>>)Parameters[UphillMovesMemoryName]; }
97        }
98        public ILookupParameter<DoubleValue> MoveQualityParameter
99        {
100            get { return (ILookupParameter<DoubleValue>)Parameters[MoveQualityName]; }
101        }
102        public ILookupParameter<DoubleValue> LastQualityParameter
103        {
104            get { return (ILookupParameter<DoubleValue>)Parameters[LastQualityName]; }
105        }
106        public IValueLookupParameter<BoolValue> MaximizationParameter
107        {
108            get { return (IValueLookupParameter<BoolValue>)Parameters[MaximizationName]; }
109        }
110        public ILookupParameter<DoubleValue> InitialAcceptanceRateParameter
111        {
112            get { return (ILookupParameter<DoubleValue>)Parameters[InitialAcceptanceRateName]; }
113        }
114        #endregion
115
116        public ConsecutiveRejectionTemperatureResetBasedOnAverageHillsizeOperator() : base()
117        {
118            #region Create parameters
119            Parameters.Add(new LookupParameter<DoubleValue>(TemperatureName, "The current temperature."));
120            Parameters.Add(new LookupParameter<DoubleValue>(LowerTemperatureName, "The lower bound of the temperature."));
121            Parameters.Add(new LookupParameter<IntValue>(IterationsName, "The number of iterations."));
122            Parameters.Add(new LookupParameter<IOperator>(AnnealingOperatorName, "The operator that cools the temperature."));
123            Parameters.Add(new LookupParameter<IntValue>(TemperatureStartIndexName, "The index where the annealing or heating was last changed."));
124            Parameters.Add(new LookupParameter<DoubleValue>(StartTemperatureName, "The temperature from which cooling should occur."));
125            Parameters.Add(new LookupParameter<DoubleValue>(EndTemperatureName, "The temperature to which should be cooled."));
126            Parameters.Add(new LookupParameter<IntValue>(MaximumIterationsName, "The maximum number of iterations which should be processed."));
127            Parameters.Add(new LookupParameter<BoolValue>(IsAcceptedName, "Whether the move was accepted or not."));
128            Parameters.Add(new LookupParameter<IntValue>(ConsecutiveRejectedSolutionsCountName, "Amount of consecutive rejected solutions."));
129            Parameters.Add(new ValueParameter<IntValue>(ThresholdName, "How many consecutive rejected solutions must occur to trigger a reheat.", new IntValue(10)));
130            Parameters.Add(new ValueParameter<DoubleValue>(ReheatRatioName, "The acceptance rate of average-sized hills used to calculate the reheat temperature.", new DoubleValue(0.4d)));
131
132            Parameters.Add(new ValueLookupParameter<BoolValue>(MaximizationName, "True if the problem is a maximization problem, otherwise false."));
133            Parameters.Add(new LookupParameter<ItemList<DoubleValue>>(UphillMovesMemoryName, "Memorizes the last 100 uphill moves."));
134            Parameters.Add(new LookupParameter<DoubleValue>(MoveQualityName, "The value which represents the quality of a move."));
135            Parameters.Add(new LookupParameter<DoubleValue>(LastQualityName, "The value which represents the quality of the last move."));
136            Parameters.Add(new LookupParameter<DoubleValue>(InitialAcceptanceRateName, "The initial acceptance rate of average-sized hills used to calculate the initial temperature."));
137
138            #endregion
139
140            Parameterize();
141        }
142
143        [StorableConstructor]
144        protected ConsecutiveRejectionTemperatureResetBasedOnAverageHillsizeOperator(bool deserializing) : base(deserializing) { }
145        protected ConsecutiveRejectionTemperatureResetBasedOnAverageHillsizeOperator(ConsecutiveRejectionTemperatureResetBasedOnAverageHillsizeOperator original, Cloner cloner) : base(original, cloner) { }
146
147
148        public override IDeepCloneable Clone(Cloner cloner)
149        {
150            return new ConsecutiveRejectionTemperatureResetBasedOnAverageHillsizeOperator(this, cloner);
151        }
152
153        public void Parameterize()
154        {
155        }
156
157        public override IOperation Apply()
158        {
159            var currentQuality = MoveQualityParameter.ActualValue.Value;
160            var lastQuality = LastQualityParameter.ActualValue.Value;
161            var isMaximation = MaximizationParameter.ActualValue.Value;
162            var hillMemory = UphillMovesMemoryParameter.ActualValue;
163
164            var currentHillSize = CalculateHillSize(lastQuality, currentQuality, isMaximation);
165
166            if (currentHillSize > 0)
167            {
168                hillMemory.Add(new DoubleValue(currentHillSize));
169                if (hillMemory.Count > 100) hillMemory.RemoveAt(0);
170            }
171
172            var count = ConsecutiveRejectedSolutionsCountParameter.ActualValue;
173
174            count.Value = !IsAcceptedParameter.ActualValue.Value ? count.Value + 1 : 0;
175
176            var heating = count.Value == ThresholdParameter.Value.Value;
177
178            if (heating) count.Value = 0;
179
180            return heating ? Heat(hillMemory) : Cool();
181        }
182
183        private IOperation Heat(ItemList<DoubleValue> hillMemory)
184        {
185            var temperature = Math.Max(CalculateTemperatureBasedOnAverageHillSize(hillMemory), TemperatureParameter.ActualValue.Value);
186            TemperatureParameter.ActualValue.Value = temperature;
187            TemperatureStartIndexParameter.ActualValue.Value = Math.Max(0, IterationsParameter.ActualValue.Value - 1);
188            StartTemperatureParameter.ActualValue.Value = TemperatureParameter.ActualValue.Value;
189            return base.Apply();
190        }
191
192        private IOperation Cool()
193        {
194            return new OperationCollection {
195                    ExecutionContext.CreateOperation(AnnealingOperatorParameter.ActualValue),
196                    base.Apply()
197                };
198        }
199
200        private double CalculateHillSize(double lastQuality, double currentQuality, bool isMaximation)
201        {
202            return lastQuality < 0 ? -1 : isMaximation ? lastQuality - currentQuality : currentQuality - lastQuality;
203        }
204
205        private double CalculateTemperatureBasedOnAverageHillSize(ItemList<DoubleValue> hillMemory)
206        {
207            var averageHillSize = hillMemory.Average(x => x.Value);
208            var medianHillsize = hillMemory.OrderByDescending(x => x.Value).ElementAt(hillMemory.Count / 2 - 1).Value;
209            var avgtemp = -(averageHillSize / Math.Log(ReheatRatioParameter.Value.Value));
210            var mediantemp = -(medianHillsize / Math.Log(ReheatRatioParameter.Value.Value));
211            Console.Out.WriteLine("AVG:    " + averageHillSize + " " + avgtemp);
212            Console.Out.WriteLine("MEDIAN: " + medianHillsize + " " + mediantemp);
213            Console.WriteLine();
214
215            return mediantemp;
216        }
217    }
218}
Note: See TracBrowser for help on using the repository browser.