Changeset 15001


Ignore:
Timestamp:
05/30/17 07:50:37 (5 years ago)
Author:
jschiess
Message:

#1836 SA reheating strategies: Newest version of reheating

Location:
branches/jschiess/HeuristicLab.Algorithms.SimulatedAnnealing/3.4
Files:
1 added
4 deleted
6 edited

Legend:

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

    r14555 r15001  
    3737        private const string IsAcceptedName = "IsAccepted";
    3838        private const string AcceptanceMemoryName = "AcceptanceMemory";
     39        private const string AverageAcceptanceRatioName = "AverageAcceptanceRatio";
    3940
    4041
     
    7374            get { return (ILookupParameter<IntValue>)Parameters[MaximumIterationsName]; }
    7475        }
    75         private ValueParameter<DoubleValue> UpperTemperatureParameter
    76         {
    77             get { return (ValueParameter<DoubleValue>)Parameters[UpperTemperatureName]; }
     76        private ValueLookupParameter<DoubleValue> UpperTemperatureParameter
     77        {
     78            get { return (ValueLookupParameter<DoubleValue>)Parameters[UpperTemperatureName]; }
    7879        }
    7980        private IConstrainedValueParameter<IDiscreteDoubleValueModifier> ReheatingOperatorParameter
     
    100101        {
    101102            get { return (ILookupParameter<ItemList<BoolValue>>)Parameters[AcceptanceMemoryName]; }
     103        }
     104        public ILookupParameter<DoubleValue> AverageAcceptanceRatioParameter
     105        {
     106            get { return (ILookupParameter<DoubleValue>)Parameters[AverageAcceptanceRatioName]; }
    102107        }
    103108
     
    118123            Parameters.Add(new LookupParameter<ItemList<BoolValue>>(AcceptanceMemoryName, "Memorizes the last N acceptance decisions."));
    119124            Parameters.Add(new LookupParameter<BoolValue>(CoolingName, "True when the temperature should be cooled, false otherwise."));
    120             Parameters.Add(new ValueParameter<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"));
    121126            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)));
    123128            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."));
    125130            #endregion
    126131
     
    155160                op.StartValueParameter.ActualName = StartTemperatureName;
    156161                op.StartValueParameter.Hidden = true;
    157                 op.EndValueParameter.ActualName = UpperTemperatureName;
    158                 op.EndValueParameter.Value = UpperTemperatureParameter.Value;
     162                op.EndValueParameter.ActualName = EndTemperatureName;
    159163                op.EndValueParameter.Hidden = true;
    160164            }
     
    163167        public override IOperation Apply()
    164168        {
    165 
     169            var isAccepted = IsAcceptedParameter.ActualValue;
    166170            var acceptances = AcceptanceMemoryParameter.ActualValue;
    167171            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)
    179186            {
    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            }
    197210            return cooling ? Cool() : Heat();
    198211        }
  • branches/jschiess/HeuristicLab.Algorithms.SimulatedAnnealing/3.4/ConsecutiveRejectionTemperatureResetOperator.cs

    r14702 r15001  
    2929        private const string TemperatureName = "Temperature";
    3030        private const string MaximumIterationsName = "MaximumIterations";
     31        private const string InitialTemperatureName = "InitialTemperature";
    3132
    3233        private const string ThresholdName = "Threshold";
    3334        private const string IsAcceptedName = "IsAccepted";
    34         private const string AcceptanceMemoryName = "AcceptanceMemory";
     35        private const string ConsecutiveRejectedSolutionsCountName = "ConsecutiveRejectedSolutions";
    3536        private const string BaseResetTemperatureName = "BaseResetTemperature";
    36         private const string MultiplyWithEachReheatName = "MultiplyWithEachReheat";
    37         private const string ReheatCountName = "ReheatCount";
    38         private const string MaximumNumberOfReheatsName = "MaximumNumberOfReheats";
    3937        #endregion
    4038
    4139        #region Parameters
    42         private ValueParameter<DoubleValue> BaseResetTemperatureParameter
     40        private ValueLookupParameter<DoubleValue> BaseResetTemperatureParameter
    4341        {
    44             get { return (ValueParameter<DoubleValue>)Parameters[BaseResetTemperatureName]; }
    45         }
    46         private ValueParameter<DoubleValue> MultiplyWithEachReheatParameter
    47         {
    48             get { return (ValueParameter<DoubleValue>)Parameters[MultiplyWithEachReheatName]; }
     42            get { return (ValueLookupParameter<DoubleValue>)Parameters[BaseResetTemperatureName]; }
    4943        }
    5044        public ILookupParameter<DoubleValue> TemperatureParameter
     
    8478            get { return (ValueParameter<IntValue>)Parameters[ThresholdName]; }
    8579        }
    86         private ValueParameter<IntValue> MaxNumberOfReheatsParameter
    87         {
    88             get { return (ValueParameter<IntValue>)Parameters[MaximumNumberOfReheatsName]; }
    89         }
    9080        public ILookupParameter<BoolValue> IsAcceptedParameter
    9181        {
    9282            get { return (ILookupParameter<BoolValue>)Parameters[IsAcceptedName]; }
    9383        }
    94         public ILookupParameter<ItemList<BoolValue>> AcceptanceMemoryParameter
     84        public ILookupParameter<IntValue> ConsecutiveRejectedSolutionsCountParameter
    9585        {
    96             get { return (ILookupParameter<ItemList<BoolValue>>)Parameters[AcceptanceMemoryName]; }
    97         }
    98         public ILookupParameter<IntValue> ReheatCountParameter
    99         {
    100             get { return (ILookupParameter<IntValue>)Parameters[ReheatCountName]; }
     86            get { return (ILookupParameter<IntValue>)Parameters[ConsecutiveRejectedSolutionsCountName]; }
    10187        }
    10288        #endregion
     
    114100            Parameters.Add(new LookupParameter<IntValue>(MaximumIterationsName, "The maximum number of iterations which should be processed."));
    115101            Parameters.Add(new LookupParameter<BoolValue>(IsAcceptedName, "Whether the move was accepted or not."));
    116             Parameters.Add(new LookupParameter<ItemList<BoolValue>>(AcceptanceMemoryName, "Memorizes the last N acceptance decisions."));
    117             Parameters.Add(new LookupParameter<IntValue>(ReheatCountName, "The number of reheats."));
     102            Parameters.Add(new LookupParameter<IntValue>(ConsecutiveRejectedSolutionsCountName, "Amount of consecutive rejected solutions."));
    118103            Parameters.Add(new ValueParameter<IntValue>(ThresholdName, "How many consecutive rejected solutions must occur to trigger a reheat.", new IntValue(10)));
    119             Parameters.Add(new ValueParameter<IntValue>(MaximumNumberOfReheatsName, "The maximum number of reheats which can be triggered", new IntValue(10)));
    120             Parameters.Add(new ValueParameter<DoubleValue>(BaseResetTemperatureName, "The base reset temperature.", new DoubleValue(100d)));
    121             Parameters.Add(new ValueParameter<DoubleValue>(MultiplyWithEachReheatName, "The amount the last reset temperature is multiplied with each reheat.", new DoubleValue(1d)));
    122 
     104            Parameters.Add(new ValueLookupParameter<DoubleValue>(BaseResetTemperatureName, "The base reset temperature.", InitialTemperatureName));
     105         
    123106            #endregion
    124107
     
    142125        public override IOperation Apply()
    143126        {
    144             var acceptances = AcceptanceMemoryParameter.ActualValue;
     127            var count = ConsecutiveRejectedSolutionsCountParameter.ActualValue;
    145128
    146             acceptances.Add(IsAcceptedParameter.ActualValue);
     129            count.Value = !IsAcceptedParameter.ActualValue.Value ? count.Value + 1 : 0;
    147130
    148             if (acceptances.Count > ThresholdParameter.Value.Value) acceptances.RemoveAt(0);
     131            var heating = count.Value == ThresholdParameter.Value.Value;
    149132
    150             var heating = acceptances.Count == ThresholdParameter.Value.Value && !acceptances.Any(x => x.Value) && !MaxNumberOfReheatsReached();
     133            if(heating) count.Value = 0;
    151134
    152135            return heating ? Heat() : Cool();
    153136        }
    154137
    155         private bool MaxNumberOfReheatsReached()
    156         {
    157             return ReheatCountParameter.ActualValue.Value >= MaxNumberOfReheatsParameter.Value.Value;
    158         }
    159 
    160138        private IOperation Heat()
    161139        {
    162             ReheatCountParameter.ActualValue.Value++;
    163 
    164             AcceptanceMemoryParameter.ActualValue.Clear();
    165 
    166             var temperature = Math.Max(BaseResetTemperatureParameter.Value.Value * Math.Pow(MultiplyWithEachReheatParameter.Value.Value, ReheatCountParameter.ActualValue.Value), TemperatureParameter.ActualValue.Value);
     140            var temperature = Math.Max(BaseResetTemperatureParameter.ActualValue.Value, TemperatureParameter.ActualValue.Value);
    167141
    168142            TemperatureParameter.ActualValue.Value = temperature;
  • branches/jschiess/HeuristicLab.Algorithms.SimulatedAnnealing/3.4/HeuristicLab.Algorithms.SimulatedAnnealing-3.4.csproj

    r14743 r15001  
    115115  <ItemGroup>
    116116    <Compile Include="AcceptanceRatioReheatingOperator.cs" />
    117     <Compile Include="ConnollyReheatingOperator.cs" />
     117    <Compile Include="ConsecutiveRejectionTemperatureResetBasedOnAverageHillsizeOperator.cs" />
    118118    <Compile Include="ConsecutiveRejectionTemperatureResetOperator.cs" />
    119     <Compile Include="ConsecutiveRejectionReheatingOperator.cs" />
     119    <Compile Include="HybridReheatingOperator.cs" />
    120120    <Compile Include="NoReheatingOperator.cs" />
    121121    <Compile Include="IReheatingOperator.cs" />
  • branches/jschiess/HeuristicLab.Algorithms.SimulatedAnnealing/3.4/SimulatedAnnealing.cs

    r14555 r15001  
    5151        private const string MaximumIterationsName = "MaximumIterations";
    5252        private const string InitialTemperatureName = "InitialTemperature";
     53        private const string InitialAcceptanceRateName = "InitialAcceptanceRate";
    5354        private const string LowerTemperatureName = "LowerTemperature";
    5455        private const string AnalyzerName = "Analyzer";
     
    115116            get { return (ValueParameter<IntValue>)Parameters[MaximumIterationsName]; }
    116117        }
    117         private ValueParameter<DoubleValue> InitialTemperatureParameter
    118         {
    119             get { return (ValueParameter<DoubleValue>)Parameters[InitialTemperatureName]; }
     118        private ValueParameter<DoubleValue> InitialAcceptanceRateParameter
     119        {
     120            get { return (ValueParameter<DoubleValue>)Parameters[InitialAcceptanceRateName]; }
    120121        }
    121122        private ValueParameter<DoubleValue> LowerTemperatureParameter
     
    172173            set { MaximumIterationsParameter.Value = value; }
    173174        }
    174         public DoubleValue InitialTemperature
    175         {
    176             get { return InitialTemperatureParameter.Value; }
    177             set { InitialTemperatureParameter.Value = value; }
     175        public DoubleValue InitialAcceptanceRate
     176        {
     177            get { return InitialAcceptanceRateParameter.Value; }
     178            set { InitialAcceptanceRateParameter.Value = value; }
    178179        }
    179180        public DoubleValue LowerTemperature
     
    219220            Parameters.Add(new ConstrainedValueParameter<IReheatingOperator>(ReheatingOperatorName, "The operator used to reheat the temperature, if necessary."));
    220221            Parameters.Add(new ValueParameter<IntValue>(MaximumIterationsName, "The maximum number of generations which should be processed.", new IntValue(10000)));
    221             Parameters.Add(new ValueParameter<DoubleValue>(InitialTemperatureName, "The initial temperature.", new DoubleValue(100)));
     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)));
    222223            Parameters.Add(new ValueParameter<DoubleValue>(LowerTemperatureName, "The lower bound for the temperature.", new DoubleValue(1e-6)));
    223224            Parameters.Add(new ValueParameter<MultiAnalyzer>(AnalyzerName, "The operator used to analyze each iteration.", new MultiAnalyzer()));
     
    244245
    245246            variableCreator.Name = "Initialize Variables";
     247            variableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>(InitialTemperatureName, new DoubleValue(Double.PositiveInfinity)));
    246248            variableCreator.CollectedValues.Add(new ValueParameter<IntValue>(EvaluatedMovesName, new IntValue()));
    247249            variableCreator.CollectedValues.Add(new ValueParameter<IntValue>(IterationsName, new IntValue(0)));
    248250            variableCreator.CollectedValues.Add(new ValueParameter<IntValue>(TemperatureStartIndexName, new IntValue(0)));
    249251            variableCreator.CollectedValues.Add(new ValueParameter<BoolValue>(CoolingName, new BoolValue(true)));
     252
    250253            variableCreator.Successor = startTemperatureAssigner;
    251254
    252255            startTemperatureAssigner.Name = "Assign Start Temperature";
    253256            startTemperatureAssigner.LeftSideParameter.ActualName = StartTemperatureName;
    254             startTemperatureAssigner.RightSideParameter.ActualName = InitialTemperatureParameter.Name;
    255             startTemperatureAssigner.Successor = endTemperatureAssigner;
     257            startTemperatureAssigner.RightSideParameter.ActualName = InitialTemperatureName;
     258            startTemperatureAssigner.Successor = endTemperatureAssigner; 
    256259
    257260            endTemperatureAssigner.Name = "Assign End Temperature";
    258261            endTemperatureAssigner.LeftSideParameter.ActualName = EndTemperatureName;
    259262            endTemperatureAssigner.RightSideParameter.ActualName = LowerTemperatureParameter.Name;
    260             endTemperatureAssigner.Successor = temperatureAssigner;
     263            endTemperatureAssigner.Successor = temperatureAssigner; 
    261264
    262265            temperatureAssigner.Name = "Initialize Temperature";
    263266            temperatureAssigner.LeftSideParameter.ActualName = TemperatureName;
    264267            temperatureAssigner.RightSideParameter.ActualName = StartTemperatureName;
    265             temperatureAssigner.Successor = resultsCollector;
     268            temperatureAssigner.Successor =  resultsCollector;
    266269
    267270            resultsCollector.CopyValue = new BoolValue(false);
  • branches/jschiess/HeuristicLab.Algorithms.SimulatedAnnealing/3.4/SimulatedAnnealingMainLoop.cs

    r14702 r15001  
    4848        private const string TemperatureStartIndexName = "TemperatureStartIndex";
    4949        private const string CoolingName = "Cooling";
     50        private const string InitialTemperatureName = "InitialTemperature";
     51        private const string InitialAcceptanceRateName = "InitialAcceptanceRate";
    5052        private const string StartTemperatureName = "StartTemperature";
    5153        private const string EndTemperatureName = "EndTemperature";
     
    5961        private const string TerminateName = "Terminate";
    6062        private const string AcceptanceMemoryName = "AcceptanceMemory";
    61         private const string ReheatCountName = "ReheatCount";
     63        private const string ConsecutiveRejectedSolutionsCountName = "ConsecutiveRejectedSolutions";
     64        private const string AverageAcceptanceRatioName = "AverageAcceptanceRatio";
     65        private const string LastQualityName = "LastQuality";
     66        private const string UphillMovesMemoryName = "UphillMovesMemory";
    6267        #endregion
    6368
     
    7883        {
    7984            get { return (IValueLookupParameter<DoubleValue>)Parameters[BestKnownQualityName]; }
     85        }
     86        public IValueLookupParameter<DoubleValue> LastQualityParameter
     87        {
     88            get { return (IValueLookupParameter<DoubleValue>)Parameters[LastQualityName]; }
     89        }
     90        public ILookupParameter<DoubleValue> InitialTemperatureParameter
     91        {
     92            get { return (ILookupParameter<DoubleValue>)Parameters[InitialTemperatureName]; }
     93        }
     94        public ILookupParameter<DoubleValue> InitialAcceptanceRateParameter
     95        {
     96            get { return (ILookupParameter<DoubleValue>)Parameters[InitialAcceptanceRateName]; }
    8097        }
    8198        public ILookupParameter<DoubleValue> MoveQualityParameter
     
    188205            Parameters.Add(new LookupParameter<IntValue>(EvaluatedMovesName, "The number of evaluated moves."));
    189206
     207            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."));
    190209            Parameters.Add(new LookupParameter<IntValue>(TemperatureStartIndexName, "The index where the annealing or heating was last changed."));
    191210            Parameters.Add(new LookupParameter<BoolValue>(CoolingName, "True when the temperature should be cooled, false otherwise."));
    192211            Parameters.Add(new LookupParameter<DoubleValue>(StartTemperatureName, "The temperature from which cooling or reheating should occur."));
    193212            Parameters.Add(new LookupParameter<DoubleValue>(EndTemperatureName, "The temperature to which should be cooled or heated."));
     213
    194214            #endregion
    195215
     
    217237
    218238            variableCreator.Name = "Initialize Memory";
     239            variableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>(AverageAcceptanceRatioName, new DoubleValue(0d)));
     240            variableCreator.CollectedValues.Add(new ValueParameter<IntValue>(ConsecutiveRejectedSolutionsCountName, new IntValue(0)));
    219241            variableCreator.CollectedValues.Add(new ValueParameter<ItemList<BoolValue>>(AcceptanceMemoryName, new ItemList<BoolValue>()));
    220             variableCreator.CollectedValues.Add(new ValueParameter<IntValue>(ReheatCountName, new IntValue(0)));
     242            variableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>(LastQualityName, new DoubleValue(-1)));
     243            variableCreator.CollectedValues.Add(new ValueParameter<ItemList<DoubleValue>>(UphillMovesMemoryName, new ItemList<DoubleValue>()));
    221244
    222245            analyzer1.Name = "Analyzer";
  • branches/jschiess/HeuristicLab.Algorithms.SimulatedAnnealing/3.4/TemperatureController.cs

    r14702 r15001  
    3030using HeuristicLab.Parameters;
    3131using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     32using HeuristicLab.Optimization;
    3233
    3334namespace HeuristicLab.Algorithms.SimulatedAnnealing
     
    4142        private const string AnnealingOperatorName = "AnnealingOperator";
    4243        private const string MaximumIterationsName = "MaximumIterations";
     44        private const string InitialTemperatureName = "InitialTemperature";
    4345        private const string LowerTemperatureName = "LowerTemperature";
    4446        private const string IterationsName = "Iterations";
     
    4951        private const string TemperatureName = "Temperature";
    5052        private const string IsAcceptedName = "IsAccepted";
     53        private const string ConsecutiveRejectedSolutionsCountName = "ConsecutiveRejectedSolutions";
     54        private const string AverageAcceptanceRatioName = "AverageAcceptanceRatio";
    5155        private const string AcceptanceMemoryName = "AcceptanceMemory";
    52         private const string ReheatCountName = "ReheatCount";
     56        private const string LastQualityName = "LastQuality";
     57        private const string UphillMovesMemoryName = "UphillMovesMemory";
     58        private const string MaximizationName = "Maximization";
     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
    5364        #endregion
    5465
     
    5869            get { return (ILookupParameter<DoubleValue>)Parameters[TemperatureName]; }
    5970        }
     71        public ILookupParameter<DoubleValue> InitialTemperatureParameter
     72        {
     73            get { return (ILookupParameter<DoubleValue>)Parameters[InitialTemperatureName]; }
     74        }
    6075        public IValueLookupParameter<DoubleValue> LowerTemperatureParameter
    6176        {
     
    102117            get { return (ILookupParameter<ItemList<BoolValue>>)Parameters[AcceptanceMemoryName]; }
    103118        }
    104         public ILookupParameter<IntValue> ReheatCountParameter
    105         {
    106             get { return (ILookupParameter<IntValue>)Parameters[ReheatCountName]; }
     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]; }
    107146        }
    108147        #endregion
     
    118157            Parameters.Add(new LookupParameter<IntValue>(IterationsName, "The number of iterations."));
    119158            Parameters.Add(new ValueLookupParameter<IntValue>(MaximumIterationsName, "The maximum number of iterations which should be processed."));
     159            Parameters.Add(new ValueLookupParameter<BoolValue>(MaximizationName, "True if the problem is a maximization problem, otherwise false."));
    120160            Parameters.Add(new ValueLookupParameter<IOperator>(AnnealingOperatorName, "The operator that cools the temperature."));
    121161            Parameters.Add(new ValueLookupParameter<IReheatingOperator>(ReheatingOperatorName, "The operator that reheats the temperature if necessary."));
     
    126166            Parameters.Add(new LookupParameter<BoolValue>(IsAcceptedName, "Whether the move was accepted or not."));
    127167            Parameters.Add(new LookupParameter<ItemList<BoolValue>>(AcceptanceMemoryName, "Memorizes the last N acceptance decisions."));
    128             Parameters.Add(new LookupParameter<IntValue>(ReheatCountName, "The number of reheats."));
     168            Parameters.Add(new LookupParameter<DoubleValue>(InitialTemperatureName, "The initial temperature."));
     169
     170            Parameters.Add(new LookupParameter<ItemList<DoubleValue>>(UphillMovesMemoryName, "Memorizes the last 100 uphill moves."));
     171            Parameters.Add(new LookupParameter<DoubleValue>(MoveQualityName, "The value which represents the quality of a move."));
     172            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            Parameters.Add(new LookupParameter<IntValue>(ConsecutiveRejectedSolutionsCountName, "Amount of consecutive rejected solutions."));
     175            Parameters.Add(new LookupParameter<DoubleValue>(AverageAcceptanceRatioName, "Average acceptance over full acceptance memory."));
     176
    129177        }
    130178
     
    136184        public override IOperation Apply()
    137185        {
    138             return new OperationCollection {
     186            var uphillMoves = UphillMovesMemoryParameter.ActualValue;
     187            if (!InitialTemperatureFound())
     188            {
     189                return findInitialTemperature();
     190            }else
     191            {
     192                return new OperationCollection {
    139193                    ExecutionContext.CreateOperation(ReheatingOperatorParameter.ActualValue),
    140194                    base.Apply()
    141195                };
     196            }
     197        }
     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;
    142245        }
    143246    }
Note: See TracChangeset for help on using the changeset viewer.