Changeset 15001 for branches/jschiess/HeuristicLab.Algorithms.SimulatedAnnealing/3.4/AcceptanceRatioReheatingOperator.cs
- Timestamp:
- 05/30/17 07:50:37 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/jschiess/HeuristicLab.Algorithms.SimulatedAnnealing/3.4/AcceptanceRatioReheatingOperator.cs
r14555 r15001 37 37 private const string IsAcceptedName = "IsAccepted"; 38 38 private const string AcceptanceMemoryName = "AcceptanceMemory"; 39 private const string AverageAcceptanceRatioName = "AverageAcceptanceRatio"; 39 40 40 41 … … 73 74 get { return (ILookupParameter<IntValue>)Parameters[MaximumIterationsName]; } 74 75 } 75 private Value Parameter<DoubleValue> UpperTemperatureParameter76 { 77 get { return (Value Parameter<DoubleValue>)Parameters[UpperTemperatureName]; }76 private ValueLookupParameter<DoubleValue> UpperTemperatureParameter 77 { 78 get { return (ValueLookupParameter<DoubleValue>)Parameters[UpperTemperatureName]; } 78 79 } 79 80 private IConstrainedValueParameter<IDiscreteDoubleValueModifier> ReheatingOperatorParameter … … 100 101 { 101 102 get { return (ILookupParameter<ItemList<BoolValue>>)Parameters[AcceptanceMemoryName]; } 103 } 104 public ILookupParameter<DoubleValue> AverageAcceptanceRatioParameter 105 { 106 get { return (ILookupParameter<DoubleValue>)Parameters[AverageAcceptanceRatioName]; } 102 107 } 103 108 … … 118 123 Parameters.Add(new LookupParameter<ItemList<BoolValue>>(AcceptanceMemoryName, "Memorizes the last N acceptance decisions.")); 119 124 Parameters.Add(new LookupParameter<BoolValue>(CoolingName, "True when the temperature should be cooled, false otherwise.")); 120 Parameters.Add(new Value Parameter<DoubleValue>(UpperTemperatureName, "The upper bound of the temperature.", new DoubleValue(100d)));125 Parameters.Add(new ValueLookupParameter<DoubleValue>(UpperTemperatureName, "The upper bound of the temperature.", "InitialTemperature")); 121 126 Parameters.Add(new ConstrainedValueParameter<IDiscreteDoubleValueModifier>(ReheatingOperatorName, "The operator that reheats the temperature.")); 122 Parameters.Add(new ValueParameter<DoubleRange>(ThresholdName, "The threshold controls the temperature. 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.", new DoubleRange(0. 2, 0.1)));127 Parameters.Add(new ValueParameter<DoubleRange>(ThresholdName, "The threshold controls the temperature. 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.", new DoubleRange(0.01, 0.1))); 123 128 Parameters.Add(new ValueParameter<IntValue>(MemorySizeName, "The maximum size of the acceptance memory.", new IntValue(100))); 124 129 Parameters.Add(new LookupParameter<DoubleValue>(AverageAcceptanceRatioName, "Average acceptance over full acceptance memory.")); 125 130 #endregion 126 131 … … 155 160 op.StartValueParameter.ActualName = StartTemperatureName; 156 161 op.StartValueParameter.Hidden = true; 157 op.EndValueParameter.ActualName = UpperTemperatureName; 158 op.EndValueParameter.Value = UpperTemperatureParameter.Value; 162 op.EndValueParameter.ActualName = EndTemperatureName; 159 163 op.EndValueParameter.Hidden = true; 160 164 } … … 163 167 public override IOperation Apply() 164 168 { 165 169 var isAccepted = IsAcceptedParameter.ActualValue; 166 170 var acceptances = AcceptanceMemoryParameter.ActualValue; 167 171 var cooling = CoolingParameter.ActualValue.Value; 168 169 acceptances.Add(IsAcceptedParameter.ActualValue); 170 171 if (acceptances.Count > MemorySizeParameter.Value.Value) acceptances.RemoveAt(0); 172 173 var ratioStart = ThresholdParameter.Value.Start; 174 var ratioEnd = ThresholdParameter.Value.End; 175 var ratio = acceptances.Average(x => x.Value ? 1.0 : 0.0); 176 177 178 if (!cooling && ratio >= ratioEnd) 172 var ratioAmount = AverageAcceptanceRatioParameter.ActualValue; 173 174 acceptances.Add(isAccepted); 175 176 ratioAmount.Value += isAccepted.Value ? 1 : 0; 177 178 179 if (acceptances.Count > MemorySizeParameter.Value.Value) { 180 ratioAmount.Value -= acceptances.ElementAt(0).Value ? 1 : 0; 181 acceptances.RemoveAt(0); 182 } 183 184 // only reheat when at least MemorySizeParameter.Value iterations have passed 185 if (acceptances.Count == MemorySizeParameter.Value.Value) 179 186 { 180 // if we are heating, but should be cooling 181 cooling = true; 182 TemperatureStartIndexParameter.ActualValue.Value = Math.Max(0, IterationsParameter.ActualValue.Value - 1); 183 StartTemperatureParameter.ActualValue.Value = TemperatureParameter.ActualValue.Value; 184 EndTemperatureParameter.ActualValue.Value = LowerTemperatureParameter.ActualValue.Value; 185 } 186 else if (cooling && ratio <= ratioStart) 187 { 188 // if we are cooling but should be heating 189 cooling = false; 190 TemperatureStartIndexParameter.ActualValue.Value = Math.Max(0, IterationsParameter.ActualValue.Value - 1); 191 StartTemperatureParameter.ActualValue.Value = TemperatureParameter.ActualValue.Value; 192 EndTemperatureParameter.ActualValue.Value = UpperTemperatureParameter.Value.Value; 193 } 194 195 CoolingParameter.ActualValue.Value = cooling; 196 187 var ratio = ratioAmount.Value / MemorySizeParameter.Value.Value; 188 var ratioStart = ThresholdParameter.Value.Start; 189 var ratioEnd = ThresholdParameter.Value.End; 190 191 if (!cooling && ratio >= ratioEnd) 192 { 193 // if we are heating, but should be cooling 194 cooling = true; 195 TemperatureStartIndexParameter.ActualValue.Value = Math.Max(0, IterationsParameter.ActualValue.Value - 1); 196 StartTemperatureParameter.ActualValue.Value = TemperatureParameter.ActualValue.Value; 197 EndTemperatureParameter.ActualValue.Value = LowerTemperatureParameter.ActualValue.Value; 198 } 199 else if (cooling && ratio <= ratioStart) 200 { 201 // if we are cooling but should be heating 202 cooling = false; 203 TemperatureStartIndexParameter.ActualValue.Value = Math.Max(0, IterationsParameter.ActualValue.Value - 1); 204 StartTemperatureParameter.ActualValue.Value = TemperatureParameter.ActualValue.Value; 205 EndTemperatureParameter.ActualValue.Value = UpperTemperatureParameter.ActualValue.Value; 206 } 207 208 CoolingParameter.ActualValue.Value = cooling; 209 } 197 210 return cooling ? Cool() : Heat(); 198 211 }
Note: See TracChangeset
for help on using the changeset viewer.