[14555] | 1 | using HeuristicLab.Operators;
|
---|
| 2 | using System;
|
---|
| 3 | using System.Collections.Generic;
|
---|
| 4 | using System.Linq;
|
---|
| 5 | using System.Text;
|
---|
| 6 | using System.Threading.Tasks;
|
---|
| 7 | using HeuristicLab.Common;
|
---|
| 8 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 9 | using HeuristicLab.Core;
|
---|
| 10 | using HeuristicLab.Data;
|
---|
| 11 | using HeuristicLab.Parameters;
|
---|
| 12 | using HeuristicLab.Optimization;
|
---|
| 13 | using HeuristicLab.PluginInfrastructure;
|
---|
| 14 | using HeuristicLab.Analysis;
|
---|
| 15 |
|
---|
| 16 | namespace HeuristicLab.Algorithms.SimulatedAnnealing
|
---|
| 17 | {
|
---|
| 18 | [Item("ConnollyReheatingOperator", "Reheats the temperature only once after N consecutive solutions are rejected. It reheats to the temperature where the best solutions was found and the temperature stays there.")]
|
---|
| 19 | [StorableClass]
|
---|
| 20 | public class ConnollyReheatingOperator : 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 IdealTemperatureName = "IdealTemperature";
|
---|
| 34 | private const string AcceptanceMemoryName = "AcceptanceMemory";
|
---|
| 35 | private const string IsAcceptedName = "IsAccepted";
|
---|
| 36 | private const string QualityName = "Quality";
|
---|
| 37 | private const string CoolingName = "Cooling";
|
---|
| 38 | private const string ResultsName = "Results";
|
---|
| 39 |
|
---|
| 40 | private const string BestQualityName = "BestQuality";
|
---|
| 41 | private const string TemperatureChartName = "Temperature Chart";
|
---|
| 42 | private const string QualitiesName = "Qualities";
|
---|
| 43 | private const string OptimalTemperatureName = "Optimal Temperature";
|
---|
| 44 |
|
---|
| 45 | #endregion
|
---|
| 46 | #region Parameters
|
---|
| 47 | public ILookupParameter<DoubleValue> TemperatureParameter
|
---|
| 48 | {
|
---|
| 49 | get { return (ILookupParameter<DoubleValue>)Parameters[TemperatureName]; }
|
---|
| 50 | }
|
---|
| 51 | public ILookupParameter<DoubleValue> LowerTemperatureParameter
|
---|
| 52 | {
|
---|
| 53 | get { return (ILookupParameter<DoubleValue>)Parameters[LowerTemperatureName]; }
|
---|
| 54 | }
|
---|
| 55 | public ILookupParameter<DoubleValue> StartTemperatureParameter
|
---|
| 56 | {
|
---|
| 57 | get { return (ILookupParameter<DoubleValue>)Parameters[StartTemperatureName]; }
|
---|
| 58 | }
|
---|
| 59 | public ILookupParameter<DoubleValue> EndTemperatureParameter
|
---|
| 60 | {
|
---|
| 61 | get { return (ILookupParameter<DoubleValue>)Parameters[EndTemperatureName]; }
|
---|
| 62 | }
|
---|
| 63 | public ILookupParameter<IntValue> TemperatureStartIndexParameter
|
---|
| 64 | {
|
---|
| 65 | get { return (ILookupParameter<IntValue>)Parameters[TemperatureStartIndexName]; }
|
---|
| 66 | }
|
---|
| 67 | public ILookupParameter<IntValue> IterationsParameter
|
---|
| 68 | {
|
---|
| 69 | get { return (ILookupParameter<IntValue>)Parameters[IterationsName]; }
|
---|
| 70 | }
|
---|
| 71 | public ILookupParameter<IDiscreteDoubleValueModifier> AnnealingOperatorParameter
|
---|
| 72 | {
|
---|
| 73 | get { return (ILookupParameter<IDiscreteDoubleValueModifier>)Parameters[AnnealingOperatorName]; }
|
---|
| 74 | }
|
---|
| 75 | public ILookupParameter<IntValue> MaximumIterationsParameter
|
---|
| 76 | {
|
---|
| 77 | get { return (ILookupParameter<IntValue>)Parameters[MaximumIterationsName]; }
|
---|
| 78 | }
|
---|
| 79 | private ValueParameter<IntValue> ThresholdParameter
|
---|
| 80 | {
|
---|
| 81 | get { return (ValueParameter<IntValue>)Parameters[ThresholdName]; }
|
---|
| 82 | }
|
---|
| 83 | public ILookupParameter<BoolValue> IsAcceptedParameter
|
---|
| 84 | {
|
---|
| 85 | get { return (ILookupParameter<BoolValue>)Parameters[IsAcceptedName]; }
|
---|
| 86 | }
|
---|
| 87 | public ILookupParameter<ItemList<BoolValue>> AcceptanceMemoryParameter
|
---|
| 88 | {
|
---|
| 89 | get { return (ILookupParameter<ItemList<BoolValue>>)Parameters[AcceptanceMemoryName]; }
|
---|
| 90 | }
|
---|
| 91 | public ILookupParameter<BoolValue> CoolingParameter
|
---|
| 92 | {
|
---|
| 93 | get { return (ILookupParameter<BoolValue>)Parameters[CoolingName]; }
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | #endregion
|
---|
| 97 |
|
---|
| 98 | public ConnollyReheatingOperator() : base()
|
---|
| 99 | {
|
---|
| 100 | #region Create parameters
|
---|
| 101 | Parameters.Add(new LookupParameter<DoubleValue>(TemperatureName, "The current temperature."));
|
---|
| 102 | Parameters.Add(new LookupParameter<DoubleValue>(LowerTemperatureName, "The lower bound of the temperature."));
|
---|
| 103 | Parameters.Add(new LookupParameter<IntValue>(IterationsName, "The number of iterations."));
|
---|
| 104 | Parameters.Add(new LookupParameter<IDiscreteDoubleValueModifier>(AnnealingOperatorName, "The operator that cools the temperature."));
|
---|
| 105 | Parameters.Add(new LookupParameter<IntValue>(TemperatureStartIndexName, "The index where the annealing or heating was last changed."));
|
---|
| 106 | Parameters.Add(new LookupParameter<DoubleValue>(StartTemperatureName, "The temperature from which cooling or reheating should occur."));
|
---|
| 107 | Parameters.Add(new LookupParameter<DoubleValue>(EndTemperatureName, "The temperature to which should be cooled or heated."));
|
---|
| 108 | Parameters.Add(new LookupParameter<IntValue>(MaximumIterationsName, "The maximum number of iterations which should be processed."));
|
---|
| 109 | Parameters.Add(new LookupParameter<BoolValue>(IsAcceptedName, "Whether the move was accepted or not."));
|
---|
| 110 | Parameters.Add(new LookupParameter<ItemList<BoolValue>>(AcceptanceMemoryName, "Memorizes the last N acceptance decisions."));
|
---|
| 111 | Parameters.Add(new ValueParameter<IntValue>(ThresholdName, "How many consecutive solutions have to be rejected to trigger the reheat.", new IntValue(10)));
|
---|
| 112 | Parameters.Add(new LookupParameter<BoolValue>(CoolingName, "True when the temperature should be cooled, false otherwise."));
|
---|
| 113 | #endregion
|
---|
| 114 |
|
---|
| 115 | Parameterize();
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | [StorableConstructor]
|
---|
| 119 | protected ConnollyReheatingOperator(bool deserializing) : base(deserializing) { }
|
---|
| 120 | protected ConnollyReheatingOperator(ConnollyReheatingOperator original, Cloner cloner) : base(original, cloner) { }
|
---|
| 121 |
|
---|
| 122 |
|
---|
| 123 | public override IDeepCloneable Clone(Cloner cloner)
|
---|
| 124 | {
|
---|
| 125 | return new ConnollyReheatingOperator(this, cloner);
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | public void Parameterize()
|
---|
| 129 | {
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | public override IOperation Apply()
|
---|
| 133 | {
|
---|
| 134 | var cooling = CoolingParameter.ActualValue.Value;
|
---|
| 135 |
|
---|
| 136 | if(cooling)
|
---|
| 137 | {
|
---|
| 138 | var acceptances = AcceptanceMemoryParameter.ActualValue;
|
---|
| 139 | acceptances.Add(IsAcceptedParameter.ActualValue);
|
---|
| 140 |
|
---|
| 141 | if (acceptances.Count > ThresholdParameter.Value.Value) acceptances.RemoveAt(0);
|
---|
| 142 | var stopCooling = acceptances.Count == ThresholdParameter.Value.Value && !acceptances.Any(x => x.Value);
|
---|
| 143 |
|
---|
| 144 | if(stopCooling)
|
---|
| 145 | {
|
---|
| 146 | CoolingParameter.ActualValue.Value = false;
|
---|
| 147 | return ReheatToIdealTemperature();
|
---|
| 148 | }else
|
---|
| 149 | {
|
---|
| 150 | return Cool();
|
---|
| 151 | }
|
---|
| 152 | }
|
---|
| 153 | else
|
---|
| 154 | {
|
---|
| 155 | return KeepTemperature();
|
---|
| 156 | }
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | private IOperation ReheatToIdealTemperature()
|
---|
| 160 | {
|
---|
| 161 | var algorithm = ExecutionContext.Parent.Parent.Parent;
|
---|
| 162 | var results = (ResultCollection)algorithm.Scope.Variables[ResultsName].Value;
|
---|
| 163 | var bestQualitySoFar = (DoubleValue)results[BestQualityName].Value;
|
---|
| 164 | var temperatures = (DataTable)results[TemperatureChartName].Value;
|
---|
| 165 | var qualities = (DataTable)results[QualitiesName].Value;
|
---|
| 166 | var bestQualities = qualities.Rows[BestQualityName];
|
---|
| 167 | var indexOfBestQuality = bestQualities.Values.IndexOf(bestQualitySoFar.Value);
|
---|
| 168 | var bestTemperature = temperatures.Rows[TemperatureName].Values[indexOfBestQuality];
|
---|
| 169 |
|
---|
| 170 | TemperatureParameter.ActualValue.Value = bestTemperature;
|
---|
| 171 |
|
---|
| 172 | if (!results.ContainsKey(OptimalTemperatureName))
|
---|
| 173 | {
|
---|
| 174 | results.Add(new Result(OptimalTemperatureName, new DoubleValue(bestTemperature)));
|
---|
| 175 | }
|
---|
| 176 | else
|
---|
| 177 | {
|
---|
| 178 | results[OptimalTemperatureName].Value = new DoubleValue(bestTemperature);
|
---|
| 179 | }
|
---|
| 180 | return base.Apply();
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | private IOperation Cool()
|
---|
| 184 | {
|
---|
| 185 | return new OperationCollection {
|
---|
| 186 | ExecutionContext.CreateOperation(AnnealingOperatorParameter.ActualValue),
|
---|
| 187 | base.Apply()
|
---|
| 188 | };
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | private IOperation KeepTemperature()
|
---|
| 192 | {
|
---|
| 193 | return base.Apply();
|
---|
| 194 | }
|
---|
| 195 | }
|
---|
| 196 | }
|
---|