Free cookie consent management tool by TermsFeed Policy Generator

source: branches/jschiess/HeuristicLab.Algorithms.SimulatedAnnealing/3.3/ConsecutiveRejectionReheatingOperator.cs @ 14702

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

#1836 SA reheating strategies: Added basic temperature reset strategy

File size: 7.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("ConsecutiveRejectionReheatingOperator", "The operator reheats the temperature by X when N consecutive solutions are rejected.")]
19    [StorableClass]
20    public class ConsecutiveRejectionReheatingOperator : 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
32        private const string ThresholdName = "Threshold";
33        private const string IsAcceptedName = "IsAccepted";
34        private const string AcceptanceMemoryName = "AcceptanceMemory";
35        private const string AmountName = "Amount";
36
37
38        #endregion
39
40        #region Parameters
41        private ValueParameter<DoubleValue> AmountParameter
42        {
43            get { return (ValueParameter<DoubleValue>)Parameters[AmountName]; }
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<ItemList<BoolValue>> AcceptanceMemoryParameter
86        {
87            get { return (ILookupParameter<ItemList<BoolValue>>)Parameters[AcceptanceMemoryName]; }
88        }
89
90        #endregion
91
92        public ConsecutiveRejectionReheatingOperator() : base()
93        {
94            #region Create parameters
95            Parameters.Add(new LookupParameter<DoubleValue>(TemperatureName, "The current temperature."));
96            Parameters.Add(new LookupParameter<DoubleValue>(LowerTemperatureName, "The lower bound of the temperature."));
97            Parameters.Add(new LookupParameter<IntValue>(IterationsName, "The number of iterations."));
98            Parameters.Add(new LookupParameter<IOperator>(AnnealingOperatorName, "The operator that cools the temperature."));
99            Parameters.Add(new LookupParameter<IntValue>(TemperatureStartIndexName, "The index where the annealing or heating was last changed."));
100            Parameters.Add(new LookupParameter<DoubleValue>(StartTemperatureName, "The temperature from which cooling should occur."));
101            Parameters.Add(new LookupParameter<DoubleValue>(EndTemperatureName, "The temperature to which should be cooled."));
102            Parameters.Add(new LookupParameter<IntValue>(MaximumIterationsName, "The maximum number of iterations which should be processed."));
103            Parameters.Add(new LookupParameter<BoolValue>(IsAcceptedName, "Whether the move was accepted or not."));
104            Parameters.Add(new LookupParameter<ItemList<BoolValue>>(AcceptanceMemoryName, "Memorizes the last N acceptance decisions."));
105            Parameters.Add(new ValueParameter<IntValue>(ThresholdName, "How many consecutive rejected solutions must occur to trigger a reheat.", new IntValue(10)));
106            Parameters.Add(new ValueParameter<DoubleValue>(AmountName, "The amount the temperature gets increased with every reheat.", new DoubleValue(20d)));
107           
108            #endregion
109
110            Parameterize();
111        }
112
113        [StorableConstructor]
114        protected ConsecutiveRejectionReheatingOperator(bool deserializing) : base(deserializing) { }
115        protected ConsecutiveRejectionReheatingOperator(ConsecutiveRejectionReheatingOperator original, Cloner cloner) : base(original, cloner) { }
116
117
118        public override IDeepCloneable Clone(Cloner cloner)
119        {
120            return new ConsecutiveRejectionReheatingOperator(this, cloner);
121        }
122
123        public void Parameterize()
124        {
125        }
126
127        public override IOperation Apply()
128        {
129            var acceptances = AcceptanceMemoryParameter.ActualValue;
130
131            acceptances.Add(IsAcceptedParameter.ActualValue);
132
133            if (acceptances.Count > ThresholdParameter.Value.Value) acceptances.RemoveAt(0);
134
135            var heating = acceptances.Count == ThresholdParameter.Value.Value && !acceptances.Any(x => x.Value);
136
137            return heating ? Heat() : Cool();
138        }
139
140        private IOperation Heat()
141        {
142            AcceptanceMemoryParameter.ActualValue.Clear();
143            TemperatureParameter.ActualValue.Value += AmountParameter.Value.Value;
144            TemperatureStartIndexParameter.ActualValue.Value = Math.Max(0, IterationsParameter.ActualValue.Value - 1);
145            StartTemperatureParameter.ActualValue.Value = TemperatureParameter.ActualValue.Value;
146            return base.Apply();
147        }
148
149        private IOperation Cool()
150        {
151            return new OperationCollection {
152                    ExecutionContext.CreateOperation(AnnealingOperatorParameter.ActualValue),
153                    base.Apply()
154                };
155        }
156    }
157}
Note: See TracBrowser for help on using the repository browser.