Free cookie consent management tool by TermsFeed Policy Generator

source: branches/jschiess/HeuristicLab.Algorithms.SimulatedAnnealing/3.4/AdaptiveReheatingOperator.cs @ 15428

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

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

File size: 8.9 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("AdaptiveReheatingOperator", "Next to the annealing schedule, a hotter schedule is generated. Whenever a solution is accepted, the temperature of the annealing schedule is used. Whenever a solution is rejected, the temperature of the heating schedule is chosen.")]
19    [StorableClass]
20    public class AdaptiveReheatingOperator : 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 IsAcceptedName = "IsAccepted";
33        private const string LastAcceptedQualityName = "LastAcceptedQuality";
34        private const string MoveQualityName = "MoveQuality";
35
36        private const string ReheatingOperatorName = "ReheatingOperator";
37        private const string OtherLowerTemperatureName = "OtherLowerTemperature";
38
39        #endregion
40
41        #region Parameters
42        public ILookupParameter<DoubleValue> TemperatureParameter
43        {
44            get { return (ILookupParameter<DoubleValue>)Parameters[TemperatureName]; }
45        }
46        public ILookupParameter<DoubleValue> LowerTemperatureParameter
47        {
48            get { return (ILookupParameter<DoubleValue>)Parameters[LowerTemperatureName]; }
49        }
50        public ILookupParameter<DoubleValue> StartTemperatureParameter
51        {
52            get { return (ILookupParameter<DoubleValue>)Parameters[StartTemperatureName]; }
53        }
54        public ILookupParameter<DoubleValue> EndTemperatureParameter
55        {
56            get { return (ILookupParameter<DoubleValue>)Parameters[EndTemperatureName]; }
57        }
58        public ILookupParameter<IntValue> TemperatureStartIndexParameter
59        {
60            get { return (ILookupParameter<IntValue>)Parameters[TemperatureStartIndexName]; }
61        }
62        public ILookupParameter<IntValue> IterationsParameter
63        {
64            get { return (ILookupParameter<IntValue>)Parameters[IterationsName]; }
65        }
66        public ILookupParameter<IDiscreteDoubleValueModifier> AnnealingOperatorParameter
67        {
68            get { return (ILookupParameter<IDiscreteDoubleValueModifier>)Parameters[AnnealingOperatorName]; }
69        }
70        public ILookupParameter<IntValue> MaximumIterationsParameter
71        {
72            get { return (ILookupParameter<IntValue>)Parameters[MaximumIterationsName]; }
73        }
74        public ILookupParameter<BoolValue> IsAcceptedParameter
75        {
76            get { return (ILookupParameter<BoolValue>)Parameters[IsAcceptedName]; }
77        }
78        public ILookupParameter<DoubleValue> MoveQualityParameter
79        {
80            get { return (ILookupParameter<DoubleValue>)Parameters[MoveQualityName]; }
81        }
82        public ILookupParameter<DoubleValue> LastAcceptedQualityParameter
83        {
84            get { return (ILookupParameter<DoubleValue>)Parameters[LastAcceptedQualityName]; }
85        }
86        public ILookupParameter<DoubleValue> InitialTemperatureParameter
87        {
88            get { return (ILookupParameter<DoubleValue>)Parameters[InitialTemperatureName]; }
89        }
90        private IConstrainedValueParameter<IDiscreteDoubleValueModifier> ReheatingOperatorParameter
91        {
92            get { return (IConstrainedValueParameter<IDiscreteDoubleValueModifier>)Parameters[ReheatingOperatorName]; }
93        }
94        private ValueParameter<DoubleValue> OtherLowerTemperatureParameter
95        {
96            get { return (ValueParameter<DoubleValue>)Parameters[OtherLowerTemperatureName]; }
97        }
98
99        #endregion
100
101        public AdaptiveReheatingOperator() : base()
102        {
103            #region Create parameters
104            Parameters.Add(new LookupParameter<DoubleValue>(TemperatureName, "The current temperature."));
105            Parameters.Add(new LookupParameter<DoubleValue>(LowerTemperatureName, "The lower bound of the temperature."));
106            Parameters.Add(new LookupParameter<IntValue>(IterationsName, "The number of iterations."));
107            Parameters.Add(new LookupParameter<IDiscreteDoubleValueModifier>(AnnealingOperatorName, "The operator that cools the temperature."));
108            Parameters.Add(new LookupParameter<IntValue>(TemperatureStartIndexName, "The index where the annealing or heating was last changed."));
109            Parameters.Add(new LookupParameter<DoubleValue>(StartTemperatureName, "The temperature from which cooling should occur."));
110            Parameters.Add(new LookupParameter<DoubleValue>(EndTemperatureName, "The temperature to which should be cooled."));
111            Parameters.Add(new LookupParameter<IntValue>(MaximumIterationsName, "The maximum number of iterations which should be processed."));
112            Parameters.Add(new LookupParameter<BoolValue>(IsAcceptedName, "Whether the move was accepted or not."));
113            Parameters.Add(new LookupParameter<DoubleValue>(LastAcceptedQualityName, "Quality of last accepted solution."));
114            Parameters.Add(new LookupParameter<DoubleValue>(MoveQualityName, "The value which represents the quality of a move."));
115            Parameters.Add(new LookupParameter<DoubleValue>(InitialTemperatureName, "The initial temperature."));
116            Parameters.Add(new ConstrainedValueParameter<IDiscreteDoubleValueModifier>(ReheatingOperatorName, "The operator that reheats the temperature."));
117            Parameters.Add(new ValueParameter<DoubleValue>(OtherLowerTemperatureName, "The upper bound of the temperature of the heating schedule.", new DoubleValue(10)));
118
119
120            foreach (var op in ApplicationManager.Manager.GetInstances<IDiscreteDoubleValueModifier>().OrderBy(x => x.Name))
121            {
122                ReheatingOperatorParameter.ValidValues.Add((IDiscreteDoubleValueModifier)op);
123            }
124
125            #endregion
126
127            Parameterize();
128        }
129
130        [StorableConstructor]
131        protected AdaptiveReheatingOperator(bool deserializing) : base(deserializing) { }
132        protected AdaptiveReheatingOperator(AdaptiveReheatingOperator original, Cloner cloner) : base(original, cloner) { }
133
134
135        public override IDeepCloneable Clone(Cloner cloner)
136        {
137            return new AdaptiveReheatingOperator(this, cloner);
138        }
139
140        public void Parameterize()
141        {
142            foreach (var op in ReheatingOperatorParameter.ValidValues)
143            {
144                op.IndexParameter.ActualName = IterationsName;
145                op.IndexParameter.Hidden = true;
146                op.StartIndexParameter.Value = null;
147                op.StartIndexParameter.ActualName = TemperatureStartIndexName;
148                op.EndIndexParameter.ActualName = MaximumIterationsParameter.Name;
149                op.ValueParameter.ActualName = TemperatureName;
150                op.ValueParameter.Hidden = true;
151                op.StartValueParameter.ActualName = StartTemperatureName;
152                op.StartValueParameter.Hidden = true;
153                op.EndValueParameter.Value = OtherLowerTemperatureParameter.Value;
154                op.EndValueParameter.Hidden = true;
155            }
156        }
157
158        public override IOperation Apply()
159        {     
160            if(!IsAcceptedParameter.ActualValue.Value || LastAcceptedQualityParameter.ActualValue.Value.Equals(MoveQualityParameter.ActualValue.Value))
161            {
162                return Heat();
163            }else
164            {
165                return Cool();
166            }
167        }
168
169
170        private IOperation Heat()
171        {
172            return new OperationCollection {
173                    ExecutionContext.CreateOperation(ReheatingOperatorParameter.Value),
174                    base.Apply()
175                };
176        }
177
178        private IOperation Cool()
179        {
180            return new OperationCollection {
181                    ExecutionContext.CreateOperation(AnnealingOperatorParameter.ActualValue),
182                    base.Apply()
183                };
184        }
185    }
186}
Note: See TracBrowser for help on using the repository browser.