Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
08/21/17 11:17:24 (7 years ago)
Author:
jschiess
Message:

#1836 SA reheating strategies
+ added an adaptive temperature control strategy
+ some refactorings

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/jschiess/HeuristicLab.Algorithms.SimulatedAnnealing/3.4/RandomWalkReheatingOperator.cs

    r15315 r15333  
    2929        private const string TemperatureName = "Temperature";
    3030        private const string MaximumIterationsName = "MaximumIterations";
    31 
    32         private const string TemperatureBeforeReheatName = "TemperatureBeforeReheat";
    3331        private const string CurrentRandomWalkStepName = "CurrentRandomWalkStep";
    3432        private const string RandomWalkLengthName = "RandomWalkLength";
     
    3634        private const string IsAcceptedName = "IsAccepted";
    3735        private const string ConsecutiveRejectedSolutionsCountName = "ConsecutiveRejectedSolutions";
    38         private const string QualitiesBeforeReheatingName = "QualitiesBeforeReheating";
    39         private const string AmountOfReheatsName = "AmountOfReheats";
    40         private const string RetrappedName = "Retrapped";
    4136        private const string LastAcceptedQualityName = "LastAcceptedQuality";
    42         private const string ResultsName = "Results";
    4337        private const string MoveQualityName = "MoveQuality";
    4438        private const string CoolingName = "Cooling";
    45         private const string TabuListActiveName = "TabuListActive";
    4639
    4740
     
    10598            get { return (ILookupParameter<IntValue>)Parameters[CurrentRandomWalkStepName]; }
    10699        }
    107         public ILookupParameter<DoubleValue> TemperatureBeforeReheatParameter
    108         {
    109             get { return (ILookupParameter<DoubleValue>)Parameters[TemperatureBeforeReheatName]; }
    110         }
    111         private LookupParameter<ItemList<DoubleValue>> QualitiesBeforeReheatingParameter
    112         {
    113             get { return (LookupParameter<ItemList<DoubleValue>>)Parameters[QualitiesBeforeReheatingName]; }
    114         }
    115100        public ILookupParameter<BoolValue> CoolingParameter
    116101        {
     
    124109        {
    125110            get { return (ILookupParameter<DoubleValue>)Parameters[LastAcceptedQualityName]; }
    126         }
    127         public IValueParameter<BoolValue> TabuListActiveParameter
    128         {
    129             get { return (IValueParameter<BoolValue>)Parameters[TabuListActiveName];  }
    130111        }
    131112
     
    145126            Parameters.Add(new LookupParameter<BoolValue>(IsAcceptedName, "Whether the move was accepted or not."));
    146127            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)));
    149130            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."));
    152131            Parameters.Add(new LookupParameter<DoubleValue>(MoveQualityName, "The value which represents the quality of a move."));
    153132            Parameters.Add(new LookupParameter<BoolValue>(CoolingName, "True when the temperature should be cooled, false otherwise."));
    154133            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 
    157134            #endregion
    158135
     
    176153        public override IOperation Apply()
    177154        {
    178             var isAccepted = IsAcceptedParameter.ActualValue.Value;
    179             var consecutiveRejectedCount = ConsecutiveRejectedSolutionsCountParameter.ActualValue;
    180155            var cooling = CoolingParameter.ActualValue.Value;
    181             var frozenSolutions = QualitiesBeforeReheatingParameter.ActualValue;
    182156            var quality = MoveQualityParameter.ActualValue;
    183157            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)
    187198            {
    188199                LastAcceptedQualityParameter.ActualValue.Value = quality.Value;
    189200            }
    190201
    191             if (cooling)
    192             {
    193                 // check if we are re-trapped
    194                 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                     else
    207                     {
    208                         retrapped.Value += 1;
    209                         results.Add(new Result(RetrappedName, retrapped));
    210                     }
    211                     // remember local optimum
    212                     frozenSolutions.Add(new DoubleValue(lastAcceptedQuality.Value));
    213                 }
    214                 else
    215                 {
    216                     // add acceptance value to consecutive rejected solutions count
    217                     consecutiveRejectedCount.Value = !IsAcceptedParameter.ActualValue.Value ? consecutiveRejectedCount.Value + 1 : 0;
    218 
    219                     // check if we are trapped in a new local optimum
    220                     if (consecutiveRejectedCount.Value == ThresholdParameter.Value.Value)
    221                     {
    222                         cooling = false;
    223                         consecutiveRejectedCount.Value = 0;
    224                         // remember local optimum
    225                         frozenSolutions.Add(new DoubleValue(lastAcceptedQuality.Value));
    226                     }
    227                 }
    228             }
    229             else
    230             {
    231                 // random walk not yet finished
    232                 if (RandomWalkLengthParameter.Value.Value != CurrentRandomWalkStepParameter.ActualValue.Value)
    233                 {
    234                     if (CurrentRandomWalkStepParameter.ActualValue.Value == 0)
    235                     {
    236                         DebugIncreaseReheatCounter();
    237                     }
    238                     CurrentRandomWalkStepParameter.ActualValue.Value++;
    239                 }
    240                 else
    241                 {
    242                     // random walk finished start cooling again
    243                     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             }
    252202            CoolingParameter.ActualValue.Value = cooling;
    253203            return cooling ? Cool() : Heat();
    254204        }
    255205
    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             else
    269             {
    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             else
    281             {
    282                 frozen.Add(new DoubleValue(LastAcceptedQualityParameter.ActualValue.Value));
    283                 results.Add(new Result(QualitiesBeforeReheatingName, frozen));
    284             }
    285         }
    286 
    287206        private IOperation Heat()
    288207        {
     
    293212        private IOperation Cool()
    294213        {
    295             TemperatureBeforeReheatParameter.ActualValue.Value = TemperatureParameter.ActualValue.Value;
    296214            return new OperationCollection {
    297215                    ExecutionContext.CreateOperation(AnnealingOperatorParameter.ActualValue),
Note: See TracChangeset for help on using the changeset viewer.