Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.SimulatedAnnealing/3.4/TemperatureController.cs @ 9086

Last change on this file since 9086 was 9086, checked in by abeham, 11 years ago

#1836: Changed reheating strategy

File size: 9.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  [Item("TemperatureController", "Decides whether to use cooling or heating and calls the appropriate operator.")]
35  [StorableClass]
36  public class TemperatureController : SingleSuccessorOperator {
37    #region Strings
38    private const string AnnealingOperatorName = "AnnealingOperator";
39    private const string HeatingOperatorName = "HeatingOperator";
40    private const string MaximumIterationsName = "MaximumIterations";
41    private const string UpperTemperatureName = "UpperTemperature";
42    private const string LowerTemperatureName = "LowerTemperature";
43    private const string IterationsName = "Iterations";
44    private const string TemperatureStartIndexName = "TemperatureStartIndex";
45    private const string CoolingName = "Cooling";
46    private const string StartTemperatureName = "StartTemperature";
47    private const string EndTemperatureName = "EndTemperature";
48    private const string TemperatureName = "Temperature";
49    private const string IsAcceptedName = "IsAccepted";
50    private const string ThresholdName = "Threshold";
51    private const string AcceptanceMemoryName = "AcceptanceMemory";
52    private const string MemorySizeName = "MemorySize";
53    #endregion
54
55    #region Parameter Properties
56    public ILookupParameter<DoubleValue> TemperatureParameter {
57      get { return (ILookupParameter<DoubleValue>)Parameters[TemperatureName]; }
58    }
59    public IValueLookupParameter<DoubleValue> UpperTemperatureParameter {
60      get { return (IValueLookupParameter<DoubleValue>)Parameters[UpperTemperatureName]; }
61    }
62    public IValueLookupParameter<DoubleValue> LowerTemperatureParameter {
63      get { return (IValueLookupParameter<DoubleValue>)Parameters[LowerTemperatureName]; }
64    }
65    public ILookupParameter<DoubleValue> StartTemperatureParameter {
66      get { return (ILookupParameter<DoubleValue>)Parameters[StartTemperatureName]; }
67    }
68    public ILookupParameter<DoubleValue> EndTemperatureParameter {
69      get { return (ILookupParameter<DoubleValue>)Parameters[EndTemperatureName]; }
70    }
71    public ILookupParameter<IntValue> TemperatureStartIndexParameter {
72      get { return (ILookupParameter<IntValue>)Parameters[TemperatureStartIndexName]; }
73    }
74    public ILookupParameter<BoolValue> CoolingParameter {
75      get { return (ILookupParameter<BoolValue>)Parameters[CoolingName]; }
76    }
77    public ILookupParameter<IntValue> IterationsParameter {
78      get { return (ILookupParameter<IntValue>)Parameters[IterationsName]; }
79    }
80    public IValueLookupParameter<IntValue> MaximumIterationsParameter {
81      get { return (IValueLookupParameter<IntValue>)Parameters[MaximumIterationsName]; }
82    }
83    public IValueLookupParameter<IOperator> AnnealingOperatorParameter {
84      get { return (IValueLookupParameter<IOperator>)Parameters[AnnealingOperatorName]; }
85    }
86    public IValueLookupParameter<IOperator> HeatingOperatorParameter {
87      get { return (IValueLookupParameter<IOperator>)Parameters[HeatingOperatorName]; }
88    }
89    public ILookupParameter<BoolValue> IsAcceptedParameter {
90      get { return (ILookupParameter<BoolValue>)Parameters[IsAcceptedName]; }
91    }
92    public IValueLookupParameter<DoubleRange> ThresholdParameter {
93      get { return (IValueLookupParameter<DoubleRange>)Parameters[ThresholdName]; }
94    }
95    public ILookupParameter<ItemList<BoolValue>> AcceptanceMemoryParameter {
96      get { return (ILookupParameter<ItemList<BoolValue>>)Parameters[AcceptanceMemoryName]; }
97    }
98    public IValueLookupParameter<IntValue> MemorySizeParameter {
99      get { return (IValueLookupParameter<IntValue>)Parameters[MemorySizeName]; }
100    }
101    #endregion
102
103    [StorableConstructor]
104    protected TemperatureController(bool deserializing) : base(deserializing) {}
105    protected TemperatureController(TemperatureController original, Cloner cloner) : base(original, cloner) {}
106    public TemperatureController()
107      : base() {
108      Parameters.Add(new LookupParameter<DoubleValue>(TemperatureName, "The current temperature."));
109      Parameters.Add(new ValueLookupParameter<DoubleValue>(UpperTemperatureName, "The upper bound of the temperature."));
110      Parameters.Add(new ValueLookupParameter<DoubleValue>(LowerTemperatureName, "The lower bound of the temperature."));
111      Parameters.Add(new LookupParameter<IntValue>(IterationsName, "The number of iterations."));
112      Parameters.Add(new ValueLookupParameter<IntValue>(MaximumIterationsName, "The maximum number of iterations which should be processed."));
113      Parameters.Add(new ValueLookupParameter<IOperator>(AnnealingOperatorName, "The operator that cools the temperature."));
114      Parameters.Add(new ValueLookupParameter<IOperator>(HeatingOperatorName, "The operator that heats the temperature."));
115      Parameters.Add(new LookupParameter<IntValue>(TemperatureStartIndexName, "The index where the annealing or heating was last changed."));
116      Parameters.Add(new LookupParameter<BoolValue>(CoolingName, "True when the temperature should be cooled, false otherwise."));
117      Parameters.Add(new LookupParameter<DoubleValue>(StartTemperatureName, "The temperature from which cooling or reheating should occur."));
118      Parameters.Add(new LookupParameter<DoubleValue>(EndTemperatureName, "The temperature to which should be cooled or heated."));
119      Parameters.Add(new LookupParameter<BoolValue>(IsAcceptedName, "Whether the move was accepted or not."));
120      Parameters.Add(new ValueLookupParameter<DoubleRange>(ThresholdName, "The threshold controls the temperature in case a heating operator is specified. If the average ratio of accepted moves goes below the start of the range the temperature is heated. If the the average ratio of accepted moves goes beyond the end of the range the temperature is cooled again."));
121      Parameters.Add(new LookupParameter<ItemList<BoolValue>>(AcceptanceMemoryName, "Memorizes the last N acceptance decisions."));
122      Parameters.Add(new ValueLookupParameter<IntValue>(MemorySizeName, "The maximum size of the acceptance memory."));
123    }
124
125    public override IDeepCloneable Clone(Cloner cloner) {
126      return new TemperatureController(this, cloner);
127    }
128
129    public override IOperation Apply() {
130      var accepted = IsAcceptedParameter.ActualValue;
131      var heatingOperator = HeatingOperatorParameter.ActualValue;
132      if (accepted == null || heatingOperator == null) { // perform only annealing in case no heating operator is given
133        return new OperationCollection {
134          ExecutionContext.CreateOperation(AnnealingOperatorParameter.ActualValue),
135          base.Apply()
136        };
137      }
138
139      var cooling = CoolingParameter.ActualValue.Value;
140      var iterations = IterationsParameter.ActualValue.Value;
141      var ratioStart = ThresholdParameter.ActualValue.Start;
142      var ratioEnd = ThresholdParameter.ActualValue.End;
143
144      var acceptances = AcceptanceMemoryParameter.ActualValue;
145      acceptances.Add(new BoolValue(accepted.Value));
146      if (acceptances.Count > MemorySizeParameter.ActualValue.Value) acceptances.RemoveAt(0);
147      var ratio = acceptances.Average(x => x.Value ? 1.0 : 0.0);
148     
149
150      if (!cooling && ratio >= ratioEnd) { // temperature is heated, but should be cooled
151        cooling = true;
152        TemperatureStartIndexParameter.ActualValue.Value = Math.Max(0, iterations - 1);
153        StartTemperatureParameter.ActualValue.Value = TemperatureParameter.ActualValue.Value;
154        EndTemperatureParameter.ActualValue.Value = LowerTemperatureParameter.ActualValue.Value;
155      } else if (cooling && ratio <= ratioStart) {  // temperature is cooled, but should be heated
156        cooling = false;
157        TemperatureStartIndexParameter.ActualValue.Value = Math.Max(0, iterations - 1);
158        StartTemperatureParameter.ActualValue.Value = TemperatureParameter.ActualValue.Value;
159        EndTemperatureParameter.ActualValue.Value = UpperTemperatureParameter.ActualValue.Value;
160      }
161
162      CoolingParameter.ActualValue.Value = cooling;
163
164      if (cooling) {
165        return new OperationCollection {
166          ExecutionContext.CreateOperation(AnnealingOperatorParameter.ActualValue),
167          base.Apply()
168        };
169      }
170      // heating
171      return new OperationCollection {
172        ExecutionContext.CreateOperation(HeatingOperatorParameter.ActualValue),
173        base.Apply()
174      };
175    }
176  }
177}
Note: See TracBrowser for help on using the repository browser.