Free cookie consent management tool by TermsFeed Policy Generator

source: branches/jschiess/HeuristicLab.Algorithms.SimulatedAnnealing/3.3/TemperatureController.cs @ 14452

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

#1836 SA reheating strategies: First version of continuous reheater and fixed reheater, both reheat based on acceptance of solutions.

File size: 8.4 KB
Line 
1#region License Information
2
3/* HeuristicLab
4 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
5 *
6 * This file is part of HeuristicLab.
7 *
8 * HeuristicLab is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * HeuristicLab is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#endregion
23
24using System;
25using System.Linq;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Operators;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32
33namespace HeuristicLab.Algorithms.SimulatedAnnealing
34{
35    [Item("TemperatureController", "Decides based on information from the heatingoperator, whether to use cooling or heating and calls the appropriate operator.")]
36    [StorableClass]
37    public class TemperatureController : SingleSuccessorOperator
38    {
39        #region Strings
40        private const string AnnealingOperatorName = "AnnealingOperator";
41        private const string HeatingStrategyOperatorName = "HeatingStrategyOperator";
42        private const string MaximumIterationsName = "MaximumIterations";
43        private const string LowerTemperatureName = "LowerTemperature";
44        private const string IterationsName = "Iterations";
45        private const string TemperatureStartIndexName = "TemperatureStartIndex";
46        private const string CoolingName = "Cooling";
47        private const string StartTemperatureName = "StartTemperature";
48        private const string EndTemperatureName = "EndTemperature";
49        private const string TemperatureName = "Temperature";
50        private const string IsAcceptedName = "IsAccepted";
51        private const string AcceptanceMemoryName = "AcceptanceMemory";
52        #endregion
53
54        #region Parameter Properties
55        public ILookupParameter<DoubleValue> TemperatureParameter
56        {
57            get { return (ILookupParameter<DoubleValue>)Parameters[TemperatureName]; }
58        }
59        public IValueLookupParameter<DoubleValue> LowerTemperatureParameter
60        {
61            get { return (IValueLookupParameter<DoubleValue>)Parameters[LowerTemperatureName]; }
62        }
63        public ILookupParameter<DoubleValue> StartTemperatureParameter
64        {
65            get { return (ILookupParameter<DoubleValue>)Parameters[StartTemperatureName]; }
66        }
67        public ILookupParameter<DoubleValue> EndTemperatureParameter
68        {
69            get { return (ILookupParameter<DoubleValue>)Parameters[EndTemperatureName]; }
70        }
71        public ILookupParameter<IntValue> TemperatureStartIndexParameter
72        {
73            get { return (ILookupParameter<IntValue>)Parameters[TemperatureStartIndexName]; }
74        }
75        public ILookupParameter<BoolValue> CoolingParameter
76        {
77            get { return (ILookupParameter<BoolValue>)Parameters[CoolingName]; }
78        }
79        public ILookupParameter<IntValue> IterationsParameter
80        {
81            get { return (ILookupParameter<IntValue>)Parameters[IterationsName]; }
82        }
83        public IValueLookupParameter<IntValue> MaximumIterationsParameter
84        {
85            get { return (IValueLookupParameter<IntValue>)Parameters[MaximumIterationsName]; }
86        }
87        public IValueLookupParameter<IOperator> AnnealingOperatorParameter
88        {
89            get { return (IValueLookupParameter<IOperator>)Parameters[AnnealingOperatorName]; }
90        }
91        public IValueLookupParameter<ISimulatedAnnealingHeatingStrategy> HeatingStrategyOperatorParameter
92        {
93            get { return (IValueLookupParameter<ISimulatedAnnealingHeatingStrategy>)Parameters[HeatingStrategyOperatorName]; }
94        }
95        public ILookupParameter<BoolValue> IsAcceptedParameter
96        {
97            get { return (ILookupParameter<BoolValue>)Parameters[IsAcceptedName]; }
98        }
99        public ILookupParameter<ItemList<BoolValue>> AcceptanceMemoryParameter
100        {
101            get { return (ILookupParameter<ItemList<BoolValue>>)Parameters[AcceptanceMemoryName]; }
102        }
103        #endregion
104
105        [StorableConstructor]
106        protected TemperatureController(bool deserializing) : base(deserializing) { }
107        protected TemperatureController(TemperatureController original, Cloner cloner) : base(original, cloner) { }
108        public TemperatureController()
109          : base()
110        {
111            Parameters.Add(new LookupParameter<DoubleValue>(TemperatureName, "The current temperature."));
112            Parameters.Add(new ValueLookupParameter<DoubleValue>(LowerTemperatureName, "The lower bound of the temperature."));
113            Parameters.Add(new LookupParameter<IntValue>(IterationsName, "The number of iterations."));
114            Parameters.Add(new ValueLookupParameter<IntValue>(MaximumIterationsName, "The maximum number of iterations which should be processed."));
115            Parameters.Add(new ValueLookupParameter<IOperator>(AnnealingOperatorName, "The operator that cools the temperature."));
116            Parameters.Add(new ValueLookupParameter<ISimulatedAnnealingHeatingStrategy>(HeatingStrategyOperatorName, "The operator that heats the temperature."));
117            Parameters.Add(new LookupParameter<IntValue>(TemperatureStartIndexName, "The index where the annealing or heating was last changed."));
118            Parameters.Add(new LookupParameter<BoolValue>(CoolingName, "True when the temperature should be cooled, false otherwise."));
119            Parameters.Add(new LookupParameter<DoubleValue>(StartTemperatureName, "The temperature from which cooling or reheating should occur."));
120            Parameters.Add(new LookupParameter<DoubleValue>(EndTemperatureName, "The temperature to which should be cooled or heated."));
121            Parameters.Add(new LookupParameter<BoolValue>(IsAcceptedName, "Whether the move was accepted or not."));
122            Parameters.Add(new LookupParameter<ItemList<BoolValue>>(AcceptanceMemoryName, "Memorizes the last N acceptance decisions."));
123        }
124
125        public override IDeepCloneable Clone(Cloner cloner)
126        {
127            return new TemperatureController(this, cloner);
128        }
129
130        public override IOperation Apply()
131        {
132            var iterations = IterationsParameter.ActualValue.Value;
133            var heatingStrategyOperator = HeatingStrategyOperatorParameter.ActualValue;
134            var accepted = IsAcceptedParameter.ActualValue;
135
136            // if there is no heatingstrategyoperator just do cooling     
137            if (accepted == null || heatingStrategyOperator == null)
138            {
139                CoolingParameter.ActualValue.Value = true;
140            }else
141            {
142                // else, let the heatingstrategyoperator decide if it is time to reheat
143                var acceptances = AcceptanceMemoryParameter.ActualValue;
144                acceptances.Add(accepted);
145                CoolingParameter.ActualValue.Value = !heatingStrategyOperator.
146                    ShouldReheat(CoolingParameter.ActualValue.Value, acceptances);
147            }
148
149            // if we want to cool, use the cooling parameter
150            if (CoolingParameter.ActualValue.Value == true)
151            {
152                TemperatureStartIndexParameter.ActualValue.Value = Math.Max(0, iterations - 1);
153                StartTemperatureParameter.ActualValue.Value = TemperatureParameter.ActualValue.Value;
154                EndTemperatureParameter.ActualValue.Value = LowerTemperatureParameter.ActualValue.Value;
155                return new OperationCollection {
156                    ExecutionContext.CreateOperation(AnnealingOperatorParameter.ActualValue),
157                    base.Apply()
158                };
159            }else
160            {
161                // else just use the heating parameter
162                return new OperationCollection {
163                    ExecutionContext.CreateOperation(HeatingStrategyOperatorParameter.ActualValue),
164                    base.Apply()
165                };
166            }
167        }
168    }
169}
Note: See TracBrowser for help on using the repository browser.