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 |
|
---|
24 | using System;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Operators;
|
---|
29 | using HeuristicLab.Parameters;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Algorithms.SimulatedAnnealing {
|
---|
33 | [Item("TemperatureController", "Decides whether to use cooling or heating and calls the appropriate operator.")]
|
---|
34 | [StorableClass]
|
---|
35 | public class TemperatureController : SingleSuccessorOperator {
|
---|
36 | #region Strings
|
---|
37 | private const string AnnealingOperatorName = "AnnealingOperator";
|
---|
38 | private const string HeatingOperatorName = "HeatingOperator";
|
---|
39 | private const string MaximumIterationsName = "MaximumIterations";
|
---|
40 | private const string UpperTemperatureName = "UpperTemperature";
|
---|
41 | private const string LowerTemperatureName = "LowerTemperature";
|
---|
42 | private const string IterationsName = "Iterations";
|
---|
43 | private const string TemperatureStartIndexName = "TemperatureStartIndex";
|
---|
44 | private const string CoolingName = "Cooling";
|
---|
45 | private const string StartTemperatureName = "StartTemperature";
|
---|
46 | private const string EndTemperatureName = "EndTemperature";
|
---|
47 | private const string TemperatureName = "Temperature";
|
---|
48 | private const string IsAcceptedName = "IsAccepted";
|
---|
49 | private const string ChangeInertiaName = "ChangeInertia";
|
---|
50 | #endregion
|
---|
51 |
|
---|
52 | #region Parameter Properties
|
---|
53 | public ILookupParameter<DoubleValue> TemperatureParameter {
|
---|
54 | get { return (ILookupParameter<DoubleValue>)Parameters[TemperatureName]; }
|
---|
55 | }
|
---|
56 | public IValueLookupParameter<DoubleValue> UpperTemperatureParameter {
|
---|
57 | get { return (IValueLookupParameter<DoubleValue>)Parameters[UpperTemperatureName]; }
|
---|
58 | }
|
---|
59 | public IValueLookupParameter<DoubleValue> LowerTemperatureParameter {
|
---|
60 | get { return (IValueLookupParameter<DoubleValue>)Parameters[LowerTemperatureName]; }
|
---|
61 | }
|
---|
62 | public ILookupParameter<DoubleValue> StartTemperatureParameter {
|
---|
63 | get { return (ILookupParameter<DoubleValue>)Parameters[StartTemperatureName]; }
|
---|
64 | }
|
---|
65 | public ILookupParameter<DoubleValue> EndTemperatureParameter {
|
---|
66 | get { return (ILookupParameter<DoubleValue>)Parameters[EndTemperatureName]; }
|
---|
67 | }
|
---|
68 | public ILookupParameter<IntValue> TemperatureStartIndexParameter {
|
---|
69 | get { return (ILookupParameter<IntValue>)Parameters[TemperatureStartIndexName]; }
|
---|
70 | }
|
---|
71 | public ILookupParameter<BoolValue> CoolingParameter {
|
---|
72 | get { return (ILookupParameter<BoolValue>)Parameters[CoolingName]; }
|
---|
73 | }
|
---|
74 | public ILookupParameter<IntValue> IterationsParameter {
|
---|
75 | get { return (ILookupParameter<IntValue>)Parameters[IterationsName]; }
|
---|
76 | }
|
---|
77 | public IValueLookupParameter<IntValue> MaximumIterationsParameter {
|
---|
78 | get { return (IValueLookupParameter<IntValue>)Parameters[MaximumIterationsName]; }
|
---|
79 | }
|
---|
80 | public IValueLookupParameter<IOperator> AnnealingOperatorParameter {
|
---|
81 | get { return (IValueLookupParameter<IOperator>)Parameters[AnnealingOperatorName]; }
|
---|
82 | }
|
---|
83 | public IValueLookupParameter<IOperator> HeatingOperatorParameter {
|
---|
84 | get { return (IValueLookupParameter<IOperator>)Parameters[HeatingOperatorName]; }
|
---|
85 | }
|
---|
86 | public ILookupParameter<BoolValue> IsAcceptedParameter {
|
---|
87 | get { return (ILookupParameter<BoolValue>)Parameters[IsAcceptedName]; }
|
---|
88 | }
|
---|
89 | public IValueLookupParameter<IntValue> ChangeInertiaParameter {
|
---|
90 | get { return (IValueLookupParameter<IntValue>) Parameters[ChangeInertiaName]; }
|
---|
91 | }
|
---|
92 | #endregion
|
---|
93 |
|
---|
94 | [StorableConstructor]
|
---|
95 | protected TemperatureController(bool deserializing) : base(deserializing) {}
|
---|
96 | protected TemperatureController(TemperatureController original, Cloner cloner) : base(original, cloner) {}
|
---|
97 | public TemperatureController()
|
---|
98 | : base() {
|
---|
99 | Parameters.Add(new LookupParameter<DoubleValue>(TemperatureName, "The current temperature."));
|
---|
100 | Parameters.Add(new ValueLookupParameter<DoubleValue>(UpperTemperatureName, "The upper bound of the temperature."));
|
---|
101 | Parameters.Add(new ValueLookupParameter<DoubleValue>(LowerTemperatureName, "The lower bound of the temperature."));
|
---|
102 | Parameters.Add(new LookupParameter<IntValue>(IterationsName, "The number of iterations."));
|
---|
103 | Parameters.Add(new ValueLookupParameter<IntValue>(MaximumIterationsName, "The maximum number of iterations which should be processed."));
|
---|
104 | Parameters.Add(new ValueLookupParameter<IOperator>(AnnealingOperatorName, "The operator that cools the temperature."));
|
---|
105 | Parameters.Add(new ValueLookupParameter<IOperator>(HeatingOperatorName, "The operator that heats the temperature."));
|
---|
106 | Parameters.Add(new LookupParameter<IntValue>(TemperatureStartIndexName, "The index where the annealing or heating was last changed."));
|
---|
107 | Parameters.Add(new LookupParameter<BoolValue>(CoolingName, "True when the temperature should be cooled, false otherwise."));
|
---|
108 | Parameters.Add(new LookupParameter<DoubleValue>(StartTemperatureName, "The temperature from which cooling or reheating should occur."));
|
---|
109 | Parameters.Add(new LookupParameter<DoubleValue>(EndTemperatureName, "The temperature to which should be cooled or heated."));
|
---|
110 | Parameters.Add(new LookupParameter<BoolValue>(IsAcceptedName, "Whether the move was accepted or not."));
|
---|
111 | Parameters.Add(new ValueLookupParameter<IntValue>(ChangeInertiaName, "The minimum iterations that need to be passed, before the process can change between heating and cooling."));
|
---|
112 | }
|
---|
113 |
|
---|
114 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
115 | return new TemperatureController(this, cloner);
|
---|
116 | }
|
---|
117 |
|
---|
118 | public override IOperation Apply() {
|
---|
119 | var accepted = IsAcceptedParameter.ActualValue;
|
---|
120 | var heatingOperator = HeatingOperatorParameter.ActualValue;
|
---|
121 | if (accepted == null || heatingOperator == null) { // annealing in case no heating operator is given
|
---|
122 | return new OperationCollection {
|
---|
123 | ExecutionContext.CreateOperation(AnnealingOperatorParameter.ActualValue),
|
---|
124 | base.Apply()
|
---|
125 | };
|
---|
126 | }
|
---|
127 |
|
---|
128 | var cooling = CoolingParameter.ActualValue.Value;
|
---|
129 | var lastChange = TemperatureStartIndexParameter.ActualValue.Value;
|
---|
130 | var iterations = IterationsParameter.ActualValue.Value;
|
---|
131 | var inertia = ChangeInertiaParameter.ActualValue.Value;
|
---|
132 |
|
---|
133 | if (accepted.Value && !cooling && (iterations - (lastChange+1)) > inertia) { // temperature is heated, but should be cooled
|
---|
134 | cooling = true;
|
---|
135 | TemperatureStartIndexParameter.ActualValue.Value = Math.Max(0, iterations - 1);
|
---|
136 | StartTemperatureParameter.ActualValue.Value = TemperatureParameter.ActualValue.Value;
|
---|
137 | EndTemperatureParameter.ActualValue.Value = LowerTemperatureParameter.ActualValue.Value;
|
---|
138 | } else if (!accepted.Value && cooling && (iterations - (lastChange+1)) > inertia) { // temperature is cooled, but should be heated
|
---|
139 | cooling = false;
|
---|
140 | TemperatureStartIndexParameter.ActualValue.Value = Math.Max(0, iterations - 1);
|
---|
141 | StartTemperatureParameter.ActualValue.Value = TemperatureParameter.ActualValue.Value;
|
---|
142 | EndTemperatureParameter.ActualValue.Value = UpperTemperatureParameter.ActualValue.Value;
|
---|
143 | }
|
---|
144 |
|
---|
145 | CoolingParameter.ActualValue.Value = cooling;
|
---|
146 |
|
---|
147 | if (cooling) {
|
---|
148 | return new OperationCollection {
|
---|
149 | ExecutionContext.CreateOperation(AnnealingOperatorParameter.ActualValue),
|
---|
150 | base.Apply()
|
---|
151 | };
|
---|
152 | }
|
---|
153 | // heating
|
---|
154 | return new OperationCollection {
|
---|
155 | ExecutionContext.CreateOperation(HeatingOperatorParameter.ActualValue),
|
---|
156 | base.Apply()
|
---|
157 | };
|
---|
158 | }
|
---|
159 | }
|
---|
160 | } |
---|