Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1836_jschiess/HeuristicLab.Algorithms.SimulatedAnnealing/3.4/TemperatureInitializerBasedOnAverageHillsize.cs @ 16824

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

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

File size: 9.5 KB
Line 
1using HeuristicLab.Common;
2using HeuristicLab.Core;
3using HeuristicLab.Data;
4using HeuristicLab.Operators;
5using HeuristicLab.Optimization;
6using HeuristicLab.Parameters;
7using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
8using System;
9using System.Collections.Generic;
10using System.Linq;
11using System.Text;
12using System.Threading.Tasks;
13
14namespace HeuristicLab.Algorithms.SimulatedAnnealing
15{
16    /*  Temperature initializer based on Johnson, David S., et al. "Optimization by simulated annealing: an experimental evaluation; part I, graph partitioning." Operations research 37.6 (1989): 865-892.
17     *  http://faculty.washington.edu/aragon/pubs/annealing-pt1.pdf (see Section 5.4, Paragraph 3, Parameter INITPROB)
18     * 
19     *  The initial temperature gets calculated by
20     *      1. Performing a random walk and collection of (UphillMovesMemorySize) amount of hills
21     *      2. Calculating the average hillsize
22     *      3. Define initial temperature as the temperature that lets the algorithm overcome a hill of average size with a percentage of INITIALACCEPTANCERATE
23     */
24
25    [Item("TemperatureInitializerBasedOnAverageHillsize", "Sets the initial temperature to the temperature that initialy lets the algorithm accept an averaged sized hill with a certain probability.")]
26    [StorableClass]
27    public class TemperatureInitializerBasedOnAverageHillsize : SingleSuccessorOperator, ITemperatureInitializer
28    {
29        #region Strings
30        private const string InitialTemperatureName = "InitialTemperature";
31        private const string TemperatureInitializedName = "TemperatureInitialized";
32        private const string StartTemperatureName = "StartTemperature";
33        private const string TemperatureStartIndexName = "TemperatureStartIndex";
34        private const string TemperatureName = "Temperature";
35        private const string IterationsName = "Iterations";
36
37        private const string LastQualityName = "LastQuality";
38        private const string UphillMovesMemoryName = "UphillMovesMemory";
39        private const string UphillMovesMemorySizeName = "UphillMovesMemorySize";
40        private const string MaximizationName = "Maximization";
41        private const string MoveQualityName = "MoveQuality";
42        private const string InitialAcceptanceRateName = "InitialAcceptanceRate";
43
44        private const string ResultsName = "Results";
45
46
47        #endregion
48
49        #region Properties
50        public ILookupParameter<DoubleValue> InitialTemperatureParameter
51        {
52            get { return (ILookupParameter<DoubleValue>)Parameters[InitialTemperatureName]; }
53        }
54        public ILookupParameter<BoolValue> TemperatureInitializedParameter
55        {
56            get { return (ILookupParameter<BoolValue>)Parameters[TemperatureInitializedName]; }
57        }
58        public ILookupParameter<DoubleValue> StartTemperatureParameter
59        {
60            get { return (ILookupParameter<DoubleValue>)Parameters[StartTemperatureName]; }
61        }
62        public ILookupParameter<IntValue> TemperatureStartIndexParameter
63        {
64            get { return (ILookupParameter<IntValue>)Parameters[TemperatureStartIndexName]; }
65        }
66        public ILookupParameter<DoubleValue> TemperatureParameter
67        {
68            get { return (ILookupParameter<DoubleValue>)Parameters[TemperatureName]; }
69        }
70        public ILookupParameter<IntValue> IterationsParameter
71        {
72            get { return (ILookupParameter<IntValue>)Parameters[IterationsName]; }
73        }
74        public ILookupParameter<ItemList<DoubleValue>> UphillMovesMemoryParameter
75        {
76            get { return (ILookupParameter<ItemList<DoubleValue>>)Parameters[UphillMovesMemoryName]; }
77        }
78        public ILookupParameter<DoubleValue> MoveQualityParameter
79        {
80            get { return (ILookupParameter<DoubleValue>)Parameters[MoveQualityName]; }
81        }
82        public ILookupParameter<DoubleValue> LastQualityParameter
83        {
84            get { return (ILookupParameter<DoubleValue>)Parameters[LastQualityName]; }
85        }
86        public IValueLookupParameter<BoolValue> MaximizationParameter
87        {
88            get { return (IValueLookupParameter<BoolValue>)Parameters[MaximizationName]; }
89        }
90        public IValueParameter<DoubleValue> InitialAcceptanceRateParameter
91        {
92            get { return (IValueParameter<DoubleValue>)Parameters[InitialAcceptanceRateName]; }
93        }
94        public IValueParameter<IntValue> UphillMovesMemorySizeParameter
95        {
96            get { return (IValueParameter<IntValue>)Parameters[UphillMovesMemorySizeName]; }
97        }
98
99        #endregion
100
101        public TemperatureInitializerBasedOnAverageHillsize() : base()
102        {
103            Parameters.Add(new LookupParameter<DoubleValue>(InitialTemperatureName, "Temporary initial temperature field."));
104            Parameters.Add(new LookupParameter<BoolValue>(TemperatureInitializedName, "True, if the temperature has already been initialized."));
105            Parameters.Add(new LookupParameter<DoubleValue>(StartTemperatureName, "The temperature from which cooling or reheating should occur."));
106            Parameters.Add(new LookupParameter<IntValue>(TemperatureStartIndexName, "The index where the annealing or heating was last changed."));
107            Parameters.Add(new LookupParameter<DoubleValue>(MoveQualityName, "The value which represents the quality of a move."));
108            Parameters.Add(new LookupParameter<DoubleValue>(LastQualityName, "The value which represents the quality of the last move."));
109            Parameters.Add(new LookupParameter<DoubleValue>(TemperatureName, "The current temperature."));
110            Parameters.Add(new ValueLookupParameter<BoolValue>(MaximizationName, "True if the problem is a maximization problem, otherwise false."));
111            Parameters.Add(new LookupParameter<IntValue>(IterationsName, "The number of iterations."));
112            Parameters.Add(new ValueParameter<DoubleValue>(InitialAcceptanceRateName, "The initial acceptance rate of average-sized hills used to calculate the initial temperature.", new DoubleValue(0.1)));
113            Parameters.Add(new ValueParameter<IntValue>(UphillMovesMemorySizeName, "The amount of uphill moves necessary to compute the initial temperature.", new IntValue(100)));
114            Parameters.Add(new LookupParameter<ItemList<DoubleValue>>(UphillMovesMemoryName, "The field that stores the uphill moves."));
115        }
116
117        [StorableConstructor]
118        protected TemperatureInitializerBasedOnAverageHillsize(bool deserializing) : base(deserializing) { }
119        protected TemperatureInitializerBasedOnAverageHillsize(TemperatureInitializerBasedOnAverageHillsize original, Cloner cloner) : base(original, cloner) { }
120
121        public override IDeepCloneable Clone(Cloner cloner)
122        {
123            return new TemperatureInitializerBasedOnAverageHillsize(this, cloner);
124        }
125
126        public override IOperation Apply()
127        {
128            var currentQuality = MoveQualityParameter.ActualValue.Value;
129            var lastQuality = LastQualityParameter.ActualValue.Value;
130            var isMaximation = MaximizationParameter.ActualValue.Value;
131            var hillMemory = UphillMovesMemoryParameter.ActualValue;
132
133            if (lastQuality != -1)
134            {
135                double hillSize = CalculateHillSize(lastQuality, currentQuality, isMaximation);
136               
137                // if there was an uphill move
138                if (hillSize > 0)
139                {
140                    // add it to the list
141                    hillMemory.Add(new DoubleValue(hillSize));
142                   
143                    // if enough hills have been collected
144                    if (hillMemory.Count >= UphillMovesMemorySizeParameter.Value.Value)
145                    {
146                        // calculate and set initial temperature
147                        var initialTemperature = CalculateInitialTemperatureBasedOnAverageHillSize(hillMemory);
148                        InitialTemperatureParameter.ActualValue.Value = initialTemperature;
149                        TemperatureParameter.ActualValue.Value = initialTemperature;
150                        TemperatureStartIndexParameter.ActualValue.Value = Math.Max(0, IterationsParameter.ActualValue.Value - 1);
151                        StartTemperatureParameter.ActualValue.Value = TemperatureParameter.ActualValue.Value;
152
153                        var results = (ResultCollection)ExecutionContext.Parent.Scope.Variables[ResultsName].Value;
154                        results.Add(new Result(InitialTemperatureName, new DoubleValue(initialTemperature)));
155
156                        // indicate that initial temperature is now set
157                        TemperatureInitializedParameter.ActualValue.Value = true;
158                    }
159                }
160            }
161
162            LastQualityParameter.ActualValue.Value = currentQuality;
163            return base.Apply();
164        }
165
166        private double CalculateInitialTemperatureBasedOnAverageHillSize(ItemList<DoubleValue> hillMemory)
167        {
168            var averageHillSize = hillMemory.Average(x => x.Value);
169            return -(averageHillSize / Math.Log(InitialAcceptanceRateParameter.Value.Value));
170
171        }
172
173        private double CalculateHillSize(double lastQuality, double currentQuality, bool isMaximation)
174        {
175            return isMaximation ? lastQuality - currentQuality : currentQuality - lastQuality;
176        }
177    }
178}
Note: See TracBrowser for help on using the repository browser.