Changeset 15333 for branches/jschiess/HeuristicLab.Algorithms.SimulatedAnnealing/3.4/RandomWalkReheatingOperator.cs
- Timestamp:
- 08/21/17 11:17:24 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/jschiess/HeuristicLab.Algorithms.SimulatedAnnealing/3.4/RandomWalkReheatingOperator.cs
r15315 r15333 29 29 private const string TemperatureName = "Temperature"; 30 30 private const string MaximumIterationsName = "MaximumIterations"; 31 32 private const string TemperatureBeforeReheatName = "TemperatureBeforeReheat";33 31 private const string CurrentRandomWalkStepName = "CurrentRandomWalkStep"; 34 32 private const string RandomWalkLengthName = "RandomWalkLength"; … … 36 34 private const string IsAcceptedName = "IsAccepted"; 37 35 private const string ConsecutiveRejectedSolutionsCountName = "ConsecutiveRejectedSolutions"; 38 private const string QualitiesBeforeReheatingName = "QualitiesBeforeReheating";39 private const string AmountOfReheatsName = "AmountOfReheats";40 private const string RetrappedName = "Retrapped";41 36 private const string LastAcceptedQualityName = "LastAcceptedQuality"; 42 private const string ResultsName = "Results";43 37 private const string MoveQualityName = "MoveQuality"; 44 38 private const string CoolingName = "Cooling"; 45 private const string TabuListActiveName = "TabuListActive";46 39 47 40 … … 105 98 get { return (ILookupParameter<IntValue>)Parameters[CurrentRandomWalkStepName]; } 106 99 } 107 public ILookupParameter<DoubleValue> TemperatureBeforeReheatParameter108 {109 get { return (ILookupParameter<DoubleValue>)Parameters[TemperatureBeforeReheatName]; }110 }111 private LookupParameter<ItemList<DoubleValue>> QualitiesBeforeReheatingParameter112 {113 get { return (LookupParameter<ItemList<DoubleValue>>)Parameters[QualitiesBeforeReheatingName]; }114 }115 100 public ILookupParameter<BoolValue> CoolingParameter 116 101 { … … 124 109 { 125 110 get { return (ILookupParameter<DoubleValue>)Parameters[LastAcceptedQualityName]; } 126 }127 public IValueParameter<BoolValue> TabuListActiveParameter128 {129 get { return (IValueParameter<BoolValue>)Parameters[TabuListActiveName]; }130 111 } 131 112 … … 145 126 Parameters.Add(new LookupParameter<BoolValue>(IsAcceptedName, "Whether the move was accepted or not.")); 146 127 Parameters.Add(new LookupParameter<IntValue>(ConsecutiveRejectedSolutionsCountName, "Amount of consecutive rejected solutions.")); 147 Parameters.Add(new ValueParameter<IntValue>(ThresholdName, "How many consecutive rejected solutions must occur to trigger a reheat.", new IntValue(10 )));148 Parameters.Add(new ValueParameter<IntValue>(RandomWalkLengthName, "Amount of randomly accepted moves upon reheat.", new IntValue( 2)));128 Parameters.Add(new ValueParameter<IntValue>(ThresholdName, "How many consecutive rejected solutions must occur to trigger a reheat.", new IntValue(10000))); 129 Parameters.Add(new ValueParameter<IntValue>(RandomWalkLengthName, "Amount of randomly accepted moves upon reheat.", new IntValue(1))); 149 130 Parameters.Add(new LookupParameter<IntValue>(CurrentRandomWalkStepName, "Current random walk step.")); 150 Parameters.Add(new LookupParameter<DoubleValue>(TemperatureBeforeReheatName, "Temperature before the reheat occured."));151 Parameters.Add(new LookupParameter<ItemList<DoubleValue>>(QualitiesBeforeReheatingName, "Quality of last optimum."));152 131 Parameters.Add(new LookupParameter<DoubleValue>(MoveQualityName, "The value which represents the quality of a move.")); 153 132 Parameters.Add(new LookupParameter<BoolValue>(CoolingName, "True when the temperature should be cooled, false otherwise.")); 154 133 Parameters.Add(new LookupParameter<DoubleValue>(LastAcceptedQualityName, "Quality of last accepted solution.")); 155 Parameters.Add(new ValueParameter<BoolValue>(TabuListActiveName, "Determines whether visiting a 'frozen' quality instantly triggers a reheat or not", new BoolValue(true)));156 157 134 #endregion 158 135 … … 176 153 public override IOperation Apply() 177 154 { 178 var isAccepted = IsAcceptedParameter.ActualValue.Value;179 var consecutiveRejectedCount = ConsecutiveRejectedSolutionsCountParameter.ActualValue;180 155 var cooling = CoolingParameter.ActualValue.Value; 181 var frozenSolutions = QualitiesBeforeReheatingParameter.ActualValue;182 156 var quality = MoveQualityParameter.ActualValue; 183 157 var lastAcceptedQuality = LastAcceptedQualityParameter.ActualValue; 184 var tabuListActive = TabuListActiveParameter.Value.Value; 185 186 if(isAccepted) 158 var isAccepted = IsAcceptedParameter.ActualValue.Value; 159 var consecutiveRejectedSolutionsCount = ConsecutiveRejectedSolutionsCountParameter.ActualValue; 160 161 if (cooling) 162 { 163 // add acceptance value to consecutive rejected solutions count 164 // if quality hasnt changed, we might be stuck on a plateau 165 if (!isAccepted || quality.Value.Equals(lastAcceptedQuality.Value)) 166 { 167 consecutiveRejectedSolutionsCount.Value++; 168 } 169 else 170 { 171 consecutiveRejectedSolutionsCount.Value = 0; 172 } 173 174 // check if we are trapped in a local optimum 175 if (consecutiveRejectedSolutionsCount.Value >= ThresholdParameter.Value.Value) 176 { 177 cooling = false; 178 consecutiveRejectedSolutionsCount.Value = 0; 179 } 180 } 181 182 183 if (!cooling) 184 { 185 // random walk finished, start cooling again 186 if (RandomWalkLengthParameter.Value.Value <= CurrentRandomWalkStepParameter.ActualValue.Value) 187 { 188 cooling = true; 189 CurrentRandomWalkStepParameter.ActualValue.Value = 0; 190 } 191 else 192 { 193 CurrentRandomWalkStepParameter.ActualValue.Value++; 194 } 195 } 196 197 if (isAccepted) 187 198 { 188 199 LastAcceptedQualityParameter.ActualValue.Value = quality.Value; 189 200 } 190 201 191 if (cooling)192 {193 // check if we are re-trapped194 if (isAccepted && frozenSolutions.Any(frozen => frozen.Value.Equals(quality.Value)) && tabuListActive)195 {196 cooling = false;197 198 IntValue retrapped = new IntValue(0);199 IResult retrappedAmount;200 var results = (ResultCollection)ExecutionContext.Parent.Parent.Scope.Variables[ResultsName].Value;201 if (results.TryGetValue(RetrappedName, out retrappedAmount))202 {203 retrapped = (IntValue)retrappedAmount.Value;204 retrapped.Value += 1;205 }206 else207 {208 retrapped.Value += 1;209 results.Add(new Result(RetrappedName, retrapped));210 }211 // remember local optimum212 frozenSolutions.Add(new DoubleValue(lastAcceptedQuality.Value));213 }214 else215 {216 // add acceptance value to consecutive rejected solutions count217 consecutiveRejectedCount.Value = !IsAcceptedParameter.ActualValue.Value ? consecutiveRejectedCount.Value + 1 : 0;218 219 // check if we are trapped in a new local optimum220 if (consecutiveRejectedCount.Value == ThresholdParameter.Value.Value)221 {222 cooling = false;223 consecutiveRejectedCount.Value = 0;224 // remember local optimum225 frozenSolutions.Add(new DoubleValue(lastAcceptedQuality.Value));226 }227 }228 }229 else230 {231 // random walk not yet finished232 if (RandomWalkLengthParameter.Value.Value != CurrentRandomWalkStepParameter.ActualValue.Value)233 {234 if (CurrentRandomWalkStepParameter.ActualValue.Value == 0)235 {236 DebugIncreaseReheatCounter();237 }238 CurrentRandomWalkStepParameter.ActualValue.Value++;239 }240 else241 {242 // random walk finished start cooling again243 cooling = true;244 TemperatureStartIndexParameter.ActualValue.Value = Math.Max(0, IterationsParameter.ActualValue.Value - 1);245 StartTemperatureParameter.ActualValue.Value = TemperatureBeforeReheatParameter.ActualValue.Value;246 EndTemperatureParameter.ActualValue.Value = LowerTemperatureParameter.ActualValue.Value;247 consecutiveRejectedCount.Value = isAccepted ? 0 : 1;248 CurrentRandomWalkStepParameter.ActualValue.Value = 0;249 250 }251 }252 202 CoolingParameter.ActualValue.Value = cooling; 253 203 return cooling ? Cool() : Heat(); 254 204 } 255 205 256 private void DebugIncreaseReheatCounter()257 {258 var reheats = new IntValue(0);259 var frozen = new ItemList<DoubleValue>();260 var results = (ResultCollection)ExecutionContext.Parent.Parent.Scope.Variables[ResultsName].Value;261 IResult amountOfReheats;262 IResult frozenSolutions;263 if (results.TryGetValue(AmountOfReheatsName, out amountOfReheats))264 {265 reheats = (IntValue)amountOfReheats.Value;266 reheats.Value += 1;267 }268 else269 {270 reheats.Value += 1;271 results.Add(new Result(AmountOfReheatsName, reheats));272 }273 274 if (results.TryGetValue(QualitiesBeforeReheatingName, out frozenSolutions))275 {276 277 frozen = (ItemList<DoubleValue>) frozenSolutions.Value;278 frozen.Add(new DoubleValue(LastAcceptedQualityParameter.ActualValue.Value));279 }280 else281 {282 frozen.Add(new DoubleValue(LastAcceptedQualityParameter.ActualValue.Value));283 results.Add(new Result(QualitiesBeforeReheatingName, frozen));284 }285 }286 287 206 private IOperation Heat() 288 207 { … … 293 212 private IOperation Cool() 294 213 { 295 TemperatureBeforeReheatParameter.ActualValue.Value = TemperatureParameter.ActualValue.Value;296 214 return new OperationCollection { 297 215 ExecutionContext.CreateOperation(AnnealingOperatorParameter.ActualValue),
Note: See TracChangeset
for help on using the changeset viewer.