- Timestamp:
- 08/09/17 10:37:45 (7 years ago)
- Location:
- branches/jschiess/HeuristicLab.Algorithms.SimulatedAnnealing/3.4
- Files:
-
- 4 added
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/jschiess/HeuristicLab.Algorithms.SimulatedAnnealing/3.4/AcceptanceRatioReheatingOperator.cs
r15001 r15315 16 16 namespace HeuristicLab.Algorithms.SimulatedAnnealing 17 17 { 18 [Item("AcceptanceRatioReheatingOperator", "Reheats the temperature if the acceptance is below a threshold .")]18 [Item("AcceptanceRatioReheatingOperator", "Reheats the temperature if the acceptance is below a threshold until it is above another one.")] 19 19 [StorableClass] 20 20 public class AcceptanceRatioReheatingOperator : SingleSuccessorOperator, IReheatingOperator … … 38 38 private const string AcceptanceMemoryName = "AcceptanceMemory"; 39 39 private const string AverageAcceptanceRatioName = "AverageAcceptanceRatio"; 40 private const string ReheatWindowSizeName = "ReheatWindowSize"; 40 41 41 42 … … 105 106 { 106 107 get { return (ILookupParameter<DoubleValue>)Parameters[AverageAcceptanceRatioName]; } 108 } 109 public IValueParameter<IntValue> ReheatWindowSizeParameter 110 { 111 get { return (IValueParameter<IntValue>)Parameters[ReheatWindowSizeName]; } 107 112 } 108 113 … … 128 133 Parameters.Add(new ValueParameter<IntValue>(MemorySizeName, "The maximum size of the acceptance memory.", new IntValue(100))); 129 134 Parameters.Add(new LookupParameter<DoubleValue>(AverageAcceptanceRatioName, "Average acceptance over full acceptance memory.")); 135 Parameters.Add(new ValueParameter<IntValue>(ReheatWindowSizeName, "The amount of iterations each reheat needs to heat the current temperature to upper temperature.", new IntValue(10000))); 130 136 #endregion 131 137 … … 204 210 StartTemperatureParameter.ActualValue.Value = TemperatureParameter.ActualValue.Value; 205 211 EndTemperatureParameter.ActualValue.Value = UpperTemperatureParameter.ActualValue.Value; 212 ReheatingOperatorParameter.Value.EndIndexParameter.Value = new IntValue( 213 Math.Min(MaximumIterationsParameter.ActualValue.Value, IterationsParameter.ActualValue.Value + ReheatWindowSizeParameter.Value.Value)); 206 214 } 207 215 -
branches/jschiess/HeuristicLab.Algorithms.SimulatedAnnealing/3.4/ConsecutiveRejectionTemperatureResetOperator.cs
r15001 r15315 16 16 namespace HeuristicLab.Algorithms.SimulatedAnnealing 17 17 { 18 [Item("ConsecutiveRejectionTemperatureResetOperator", "The operator resets the temperature to X when N consecutive solutions are rejected. X is calculated by BaseResetTemperature * (MultiplyWithEachReheat to the power of the current number of reheats).")]18 [Item("ConsecutiveRejectionTemperatureResetOperator", "The operator resets the temperature to the ResetTemperature when N consecutive solutions are rejected.")] 19 19 [StorableClass] 20 20 public class ConsecutiveRejectionTemperatureResetOperator : SingleSuccessorOperator, IReheatingOperator … … 34 34 private const string IsAcceptedName = "IsAccepted"; 35 35 private const string ConsecutiveRejectedSolutionsCountName = "ConsecutiveRejectedSolutions"; 36 private const string BaseResetTemperatureName = "BaseResetTemperature";36 private const string ResetTemperatureName = "ResetTemperature"; 37 37 #endregion 38 38 39 39 #region Parameters 40 private ValueLookupParameter<DoubleValue> BaseResetTemperatureParameter40 private ValueLookupParameter<DoubleValue> ResetTemperatureParameter 41 41 { 42 get { return (ValueLookupParameter<DoubleValue>)Parameters[ BaseResetTemperatureName]; }42 get { return (ValueLookupParameter<DoubleValue>)Parameters[ResetTemperatureName]; } 43 43 } 44 44 public ILookupParameter<DoubleValue> TemperatureParameter … … 102 102 Parameters.Add(new LookupParameter<IntValue>(ConsecutiveRejectedSolutionsCountName, "Amount of consecutive rejected solutions.")); 103 103 Parameters.Add(new ValueParameter<IntValue>(ThresholdName, "How many consecutive rejected solutions must occur to trigger a reheat.", new IntValue(10))); 104 Parameters.Add(new ValueLookupParameter<DoubleValue>( BaseResetTemperatureName, "The base reset temperature.", InitialTemperatureName));104 Parameters.Add(new ValueLookupParameter<DoubleValue>(ResetTemperatureName, "The base reset temperature.", InitialTemperatureName)); 105 105 106 106 #endregion … … 138 138 private IOperation Heat() 139 139 { 140 var temperature = Math.Max( BaseResetTemperatureParameter.ActualValue.Value, TemperatureParameter.ActualValue.Value);140 var temperature = Math.Max(ResetTemperatureParameter.ActualValue.Value, TemperatureParameter.ActualValue.Value); 141 141 142 142 TemperatureParameter.ActualValue.Value = temperature; -
branches/jschiess/HeuristicLab.Algorithms.SimulatedAnnealing/3.4/SimulatedAnnealing.cs
r15001 r15315 51 51 private const string MaximumIterationsName = "MaximumIterations"; 52 52 private const string InitialTemperatureName = "InitialTemperature"; 53 private const string InitialAcceptanceRateName = "InitialAcceptanceRate";54 53 private const string LowerTemperatureName = "LowerTemperature"; 55 54 private const string AnalyzerName = "Analyzer"; … … 65 64 private const string TemperatureChartName = "Temperature Chart"; 66 65 private const string TemperatureAnalyzerName = "Temperature Analyzer"; 66 private const string TemperatureInitializerName = "TemperatureInitializer"; 67 67 #endregion 68 68 … … 111 111 get { return (IConstrainedValueParameter<IReheatingOperator>)Parameters[ReheatingOperatorName]; } 112 112 } 113 public IConstrainedValueParameter<ITemperatureInitializer> TemperatureInitializerParameter 114 { 115 get { return (IConstrainedValueParameter<ITemperatureInitializer>)Parameters[TemperatureInitializerName]; } 116 } 113 117 114 118 private ValueParameter<IntValue> MaximumIterationsParameter 115 119 { 116 120 get { return (ValueParameter<IntValue>)Parameters[MaximumIterationsName]; } 117 }118 private ValueParameter<DoubleValue> InitialAcceptanceRateParameter119 {120 get { return (ValueParameter<DoubleValue>)Parameters[InitialAcceptanceRateName]; }121 121 } 122 122 private ValueParameter<DoubleValue> LowerTemperatureParameter … … 167 167 set { ReheatingOperatorParameter.Value = value; } 168 168 } 169 public ITemperatureInitializer TemperatureInitializer 170 { 171 get { return TemperatureInitializerParameter.Value; } 172 set { TemperatureInitializerParameter.Value = value; } 173 } 169 174 170 175 public IntValue MaximumIterations … … 172 177 get { return MaximumIterationsParameter.Value; } 173 178 set { MaximumIterationsParameter.Value = value; } 174 }175 public DoubleValue InitialAcceptanceRate176 {177 get { return InitialAcceptanceRateParameter.Value; }178 set { InitialAcceptanceRateParameter.Value = value; }179 179 } 180 180 public DoubleValue LowerTemperature … … 219 219 Parameters.Add(new ConstrainedValueParameter<IDiscreteDoubleValueModifier>(AnnealingOperatorName, "The operator used to cool the temperature.")); 220 220 Parameters.Add(new ConstrainedValueParameter<IReheatingOperator>(ReheatingOperatorName, "The operator used to reheat the temperature, if necessary.")); 221 Parameters.Add(new ConstrainedValueParameter<ITemperatureInitializer>(TemperatureInitializerName, "The operator used to initialize the temperature.")); 221 222 Parameters.Add(new ValueParameter<IntValue>(MaximumIterationsName, "The maximum number of generations which should be processed.", new IntValue(10000))); 222 Parameters.Add(new ValueParameter<DoubleValue>(InitialAcceptanceRateName, "The initial acceptance rate of average-sized hills used to calculate the initial temperature", new DoubleValue(0.4)));223 223 Parameters.Add(new ValueParameter<DoubleValue>(LowerTemperatureName, "The lower bound for the temperature.", new DoubleValue(1e-6))); 224 224 Parameters.Add(new ValueParameter<MultiAnalyzer>(AnalyzerName, "The operator used to analyze each iteration.", new MultiAnalyzer())); … … 300 300 { 301 301 AnnealingOperatorParameter.ValidValues.Add(op); 302 } 303 304 foreach(var op in ApplicationManager.Manager.GetInstances<ITemperatureInitializer>().OrderBy(x => x.Name)) 305 { 306 TemperatureInitializerParameter.ValidValues.Add(op); 302 307 } 303 308 -
branches/jschiess/HeuristicLab.Algorithms.SimulatedAnnealing/3.4/SimulatedAnnealingMainLoop.cs
r15001 r15315 49 49 private const string CoolingName = "Cooling"; 50 50 private const string InitialTemperatureName = "InitialTemperature"; 51 private const string InitialAcceptanceRateName = "InitialAcceptanceRate";52 51 private const string StartTemperatureName = "StartTemperature"; 53 52 private const string EndTemperatureName = "EndTemperature"; … … 65 64 private const string LastQualityName = "LastQuality"; 66 65 private const string UphillMovesMemoryName = "UphillMovesMemory"; 66 private const string TemperatureBeforeReheatName = "TemperatureBeforeReheat"; 67 private const string CurrentRandomWalkStepName = "CurrentRandomWalkStep"; 68 private const string QualitiesBeforeReheatingName = "QualitiesBeforeReheating"; 69 private const string LastAcceptedQualityName = "LastAcceptedQuality"; 70 private const string TemperatureInitializerName = "TemperatureInitializer"; 71 private const string TemperatureInitializedName = "TemperatureInitialized"; 72 67 73 #endregion 68 74 … … 91 97 { 92 98 get { return (ILookupParameter<DoubleValue>)Parameters[InitialTemperatureName]; } 93 }94 public ILookupParameter<DoubleValue> InitialAcceptanceRateParameter95 {96 get { return (ILookupParameter<DoubleValue>)Parameters[InitialAcceptanceRateName]; }97 99 } 98 100 public ILookupParameter<DoubleValue> MoveQualityParameter … … 200 202 Parameters.Add(new ValueLookupParameter<IOperator>(AnnealingOperatorName, "The operator that cools the temperature.")); 201 203 Parameters.Add(new ValueLookupParameter<IOperator>(ReheatingOperatorName, "The operator that reheats the temperature if necessary.")); 204 Parameters.Add(new ValueLookupParameter<IOperator>(TemperatureInitializerName, "The operator that initialized the temperature.")); 202 205 203 206 Parameters.Add(new ValueLookupParameter<IOperator>(AnalyzerName, "The operator used to analyze each generation.")); … … 206 209 207 210 Parameters.Add(new LookupParameter<DoubleValue>(InitialTemperatureName, "The initial temperature.")); 208 Parameters.Add(new LookupParameter<DoubleValue>(InitialAcceptanceRateName, "The initial acceptance rate of average-sized hills used to calculate the initial temperature."));209 211 Parameters.Add(new LookupParameter<IntValue>(TemperatureStartIndexName, "The index where the annealing or heating was last changed.")); 210 212 Parameters.Add(new LookupParameter<BoolValue>(CoolingName, "True when the temperature should be cooled, false otherwise.")); 211 213 Parameters.Add(new LookupParameter<DoubleValue>(StartTemperatureName, "The temperature from which cooling or reheating should occur.")); 212 214 Parameters.Add(new LookupParameter<DoubleValue>(EndTemperatureName, "The temperature to which should be cooled or heated.")); 215 213 216 214 217 #endregion … … 237 240 238 241 variableCreator.Name = "Initialize Memory"; 242 variableCreator.CollectedValues.Add(new ValueParameter<BoolValue>(TemperatureInitializedName, new BoolValue(false))); 239 243 variableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>(AverageAcceptanceRatioName, new DoubleValue(0d))); 240 244 variableCreator.CollectedValues.Add(new ValueParameter<IntValue>(ConsecutiveRejectedSolutionsCountName, new IntValue(0))); … … 242 246 variableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>(LastQualityName, new DoubleValue(-1))); 243 247 variableCreator.CollectedValues.Add(new ValueParameter<ItemList<DoubleValue>>(UphillMovesMemoryName, new ItemList<DoubleValue>())); 248 variableCreator.CollectedValues.Add(new ValueParameter<IntValue>(CurrentRandomWalkStepName, new IntValue(0))); 249 variableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>(TemperatureBeforeReheatName, new DoubleValue(0))); 250 variableCreator.CollectedValues.Add(new ValueParameter<ItemList<DoubleValue>>(QualitiesBeforeReheatingName, new ItemList<DoubleValue>())); 251 variableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>(LastAcceptedQualityName, new DoubleValue(-1))); 244 252 245 253 analyzer1.Name = "Analyzer"; … … 267 275 268 276 269 270 277 subScopesRemover.RemoveAllSubScopes = true; 271 278 -
branches/jschiess/HeuristicLab.Algorithms.SimulatedAnnealing/3.4/TemperatureController.cs
r15001 r15315 34 34 namespace HeuristicLab.Algorithms.SimulatedAnnealing 35 35 { 36 [Item("TemperatureController", " Decides based on information from the heatingoperator, whether to use cooling or heating and calls the appropriate operator.")]36 [Item("TemperatureController", "Responsible to first initialize the temperature and then control the temperature.")] 37 37 [StorableClass] 38 38 public class TemperatureController : SingleSuccessorOperator … … 58 58 private const string MaximizationName = "Maximization"; 59 59 private const string MoveQualityName = "MoveQuality"; 60 private const string InitialAcceptanceRateName = "InitialAcceptanceRate"; 61 private const string ResultsName = "Results"; 62 private const string CalculatedInitialTemperatureName = "CalculatedInitialTemperature"; 63 60 private const string TemperatureBeforeReheatName = "TemperatureBeforeReheat"; 61 private const string CurrentRandomWalkStepName = "CurrentRandomWalkStep"; 62 private const string QualitiesBeforeReheatingName = "QualitiesBeforeReheating"; 63 private const string LastAcceptedQualityName = "LastAcceptedQuality"; 64 private const string TemperatureInitializerName = "TemperatureInitializer"; 65 private const string TemperatureInitializedName = "TemperatureInitialized"; 64 66 #endregion 65 67 66 68 #region Parameter Properties 67 public ILookupParameter<DoubleValue> TemperatureParameter68 {69 get { return (ILookupParameter<DoubleValue>)Parameters[TemperatureName]; }70 }71 public ILookupParameter<DoubleValue> InitialTemperatureParameter72 {73 get { return (ILookupParameter<DoubleValue>)Parameters[InitialTemperatureName]; }74 }75 public IValueLookupParameter<DoubleValue> LowerTemperatureParameter76 {77 get { return (IValueLookupParameter<DoubleValue>)Parameters[LowerTemperatureName]; }78 }79 public ILookupParameter<DoubleValue> StartTemperatureParameter80 {81 get { return (ILookupParameter<DoubleValue>)Parameters[StartTemperatureName]; }82 }83 public ILookupParameter<DoubleValue> EndTemperatureParameter84 {85 get { return (ILookupParameter<DoubleValue>)Parameters[EndTemperatureName]; }86 }87 public ILookupParameter<IntValue> TemperatureStartIndexParameter88 {89 get { return (ILookupParameter<IntValue>)Parameters[TemperatureStartIndexName]; }90 }91 public ILookupParameter<BoolValue> CoolingParameter92 {93 get { return (ILookupParameter<BoolValue>)Parameters[CoolingName]; }94 }95 public ILookupParameter<IntValue> IterationsParameter96 {97 get { return (ILookupParameter<IntValue>)Parameters[IterationsName]; }98 }99 public IValueLookupParameter<IntValue> MaximumIterationsParameter100 {101 get { return (IValueLookupParameter<IntValue>)Parameters[MaximumIterationsName]; }102 }103 public IValueLookupParameter<IOperator> AnnealingOperatorParameter104 {105 get { return (IValueLookupParameter<IOperator>)Parameters[AnnealingOperatorName]; }106 }107 69 public IValueLookupParameter<IReheatingOperator> ReheatingOperatorParameter 108 70 { 109 71 get { return (IValueLookupParameter<IReheatingOperator>)Parameters[ReheatingOperatorName]; } 110 72 } 111 public I LookupParameter<BoolValue> IsAcceptedParameter73 public IValueLookupParameter<ITemperatureInitializer> TemperatureInitializerParameter 112 74 { 113 get { return (I LookupParameter<BoolValue>)Parameters[IsAcceptedName]; }75 get { return (IValueLookupParameter<ITemperatureInitializer>)Parameters[TemperatureInitializerName]; } 114 76 } 115 public ILookupParameter< ItemList<BoolValue>> AcceptanceMemoryParameter77 public ILookupParameter<BoolValue> TemperatureInitializedParameter 116 78 { 117 get { return (ILookupParameter<ItemList<BoolValue>>)Parameters[AcceptanceMemoryName]; } 118 } 119 public ILookupParameter<ItemList<DoubleValue>> UphillMovesMemoryParameter 120 { 121 get { return (ILookupParameter<ItemList<DoubleValue>>)Parameters[UphillMovesMemoryName]; } 122 } 123 public ILookupParameter<DoubleValue> MoveQualityParameter 124 { 125 get { return (ILookupParameter<DoubleValue>)Parameters[MoveQualityName]; } 126 } 127 public ILookupParameter<DoubleValue> LastQualityParameter 128 { 129 get { return (ILookupParameter<DoubleValue>)Parameters[LastQualityName]; } 130 } 131 public IValueLookupParameter<BoolValue> MaximizationParameter 132 { 133 get { return (IValueLookupParameter<BoolValue>)Parameters[MaximizationName]; } 134 } 135 public ILookupParameter<DoubleValue> InitialAcceptanceRateParameter 136 { 137 get { return (ILookupParameter<DoubleValue>)Parameters[InitialAcceptanceRateName]; } 138 } 139 public ILookupParameter<IntValue> ConsecutiveRejectedSolutionsCountParameter 140 { 141 get { return (ILookupParameter<IntValue>)Parameters[ConsecutiveRejectedSolutionsCountName]; } 142 } 143 public ILookupParameter<DoubleValue> AverageAcceptanceRatioParameter 144 { 145 get { return (ILookupParameter<DoubleValue>)Parameters[AverageAcceptanceRatioName]; } 79 get { return (ILookupParameter<BoolValue>)Parameters[TemperatureInitializedName]; } 146 80 } 147 81 #endregion … … 171 105 Parameters.Add(new LookupParameter<DoubleValue>(MoveQualityName, "The value which represents the quality of a move.")); 172 106 Parameters.Add(new LookupParameter<DoubleValue>(LastQualityName, "The value which represents the quality of the last move.")); 173 Parameters.Add(new LookupParameter<DoubleValue>(InitialAcceptanceRateName, "The initial acceptance rate of average-sized hills used to calculate the initial temperature."));174 107 Parameters.Add(new LookupParameter<IntValue>(ConsecutiveRejectedSolutionsCountName, "Amount of consecutive rejected solutions.")); 175 108 Parameters.Add(new LookupParameter<DoubleValue>(AverageAcceptanceRatioName, "Average acceptance over full acceptance memory.")); 109 Parameters.Add(new LookupParameter<IntValue>(CurrentRandomWalkStepName, "Current random walk step.")); 110 Parameters.Add(new LookupParameter<DoubleValue>(TemperatureBeforeReheatName, "Temperature before the reheat occured.")); 111 Parameters.Add(new LookupParameter<ItemList<DoubleValue>>(QualitiesBeforeReheatingName, "List of qualities where the algorithm has been stuck.")); 112 Parameters.Add(new LookupParameter<DoubleValue>(LastAcceptedQualityName, "Quality of last accepted solution.")); 113 114 Parameters.Add(new ValueLookupParameter<ITemperatureInitializer>(TemperatureInitializerName, "The operator that initilized the temperature.")); 115 Parameters.Add(new LookupParameter<BoolValue>(TemperatureInitializedName, "True, if the temperature has already been initialized.")); 176 116 177 117 } … … 184 124 public override IOperation Apply() 185 125 { 186 var uphillMoves = UphillMovesMemoryParameter.ActualValue; 187 if (! InitialTemperatureFound())126 127 if (!TemperatureInitializedParameter.ActualValue.Value) 188 128 { 189 return findInitialTemperature(); 190 }else 129 return new OperationCollection 130 { 131 ExecutionContext.CreateOperation(TemperatureInitializerParameter.ActualValue), 132 base.Apply() 133 }; 134 } 135 else 191 136 { 192 137 return new OperationCollection { … … 196 141 } 197 142 } 198 199 private IOperation findInitialTemperature()200 {201 var currentQuality = MoveQualityParameter.ActualValue.Value;202 var lastQuality = LastQualityParameter.ActualValue.Value;203 var isMaximation = MaximizationParameter.ActualValue.Value;204 var hillMemory = UphillMovesMemoryParameter.ActualValue;205 206 if(lastQuality != -1)207 {208 double hillSize = CalculateHillSize(lastQuality, currentQuality, isMaximation);209 210 if(hillSize > 0)211 {212 hillMemory.Add(new DoubleValue(hillSize));213 if(InitialTemperatureFound())214 {215 var results = (ResultCollection)ExecutionContext.Parent.Scope.Variables[ResultsName].Value;216 var initialTemperature = CalculateInitialTemperatureBasedOnAverageHillSize(hillMemory);217 InitialTemperatureParameter.ActualValue.Value = initialTemperature;218 TemperatureParameter.ActualValue.Value = initialTemperature;219 TemperatureStartIndexParameter.ActualValue.Value = Math.Max(0, IterationsParameter.ActualValue.Value - 1);220 StartTemperatureParameter.ActualValue.Value = TemperatureParameter.ActualValue.Value;221 results.Add(new Result(CalculatedInitialTemperatureName, new DoubleValue(initialTemperature)));222 }223 }224 }225 226 LastQualityParameter.ActualValue.Value = currentQuality;227 return base.Apply();228 }229 230 private double CalculateInitialTemperatureBasedOnAverageHillSize(ItemList<DoubleValue> hillMemory)231 {232 var averageHillSize = hillMemory.Average(x => x.Value);233 return -(averageHillSize / Math.Log(InitialAcceptanceRateParameter.ActualValue.Value));234 235 }236 237 private double CalculateHillSize(double lastQuality, double currentQuality, bool isMaximation)238 {239 return isMaximation ? lastQuality - currentQuality : currentQuality - lastQuality;240 }241 242 private bool InitialTemperatureFound()243 {244 return UphillMovesMemoryParameter.ActualValue.Count == 100;245 }246 143 } 247 144 }
Note: See TracChangeset
for help on using the changeset viewer.