Free cookie consent management tool by TermsFeed Policy Generator

source: branches/jschiess/HeuristicLab.Algorithms.SimulatedAnnealing/3.3/FixedReheater.cs @ 14530

Last change on this file since 14530 was 14452, checked in by jschiess, 8 years ago

#1836 SA reheating strategies: First version of continuous reheater and fixed reheater, both reheat based on acceptance of solutions.

File size: 3.7 KB
Line 
1using HeuristicLab.Operators;
2using System;
3using System.Collections.Generic;
4using System.Linq;
5using System.Text;
6using System.Threading.Tasks;
7using HeuristicLab.Common;
8using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
9using HeuristicLab.Core;
10using HeuristicLab.Data;
11using HeuristicLab.Parameters;
12using HeuristicLab.Optimization;
13using HeuristicLab.PluginInfrastructure;
14
15namespace HeuristicLab.Algorithms.SimulatedAnnealing
16{
17    [Item("Fixed Reheater", "Increases temperature by a fixed amount, each time n consecutive solutions are not accepted.")]
18    [StorableClass]
19    public class FixedReheater : SingleSuccessorOperator, ISimulatedAnnealingHeatingStrategy
20    {
21        #region Strings
22        private const string AmountName = "Amount";
23        private const string ThresholdName = "Threshold";
24        private const string TemperatureName = "Temperature";
25        private const string AcceptanceMemoryName = "AcceptanceMemory";
26
27
28        #endregion
29        #region Parameters
30        private ValueParameter<DoubleValue> AmountParameter
31        {
32            get { return (ValueParameter<DoubleValue>)Parameters[AmountName]; }
33        }
34        private ValueParameter<IntValue> ThresholdParameter
35        {
36            get { return (ValueParameter<IntValue>)Parameters[ThresholdName]; }
37        }
38        public ILookupParameter<DoubleValue> TemperatureParameter
39        {
40            get { return (ILookupParameter<DoubleValue>)Parameters[TemperatureName]; }
41        }
42        public ILookupParameter<ItemList<BoolValue>> AcceptanceMemoryParameter
43        {
44            get { return (ILookupParameter<ItemList<BoolValue>>)Parameters[AcceptanceMemoryName]; }
45        }
46
47        #endregion
48        #region Properties
49        public DoubleValue Amount
50        {
51            get { return AmountParameter.Value; }
52            set { AmountParameter.Value = value; }
53        }
54        public IntValue AcceptanceThreshold
55        {
56            get { return ThresholdParameter.Value; }
57            set { ThresholdParameter.Value = value; }
58        }
59        #endregion
60
61        public FixedReheater() : base()
62        {
63            #region Create parameters
64            Parameters.Add(new ValueParameter<DoubleValue>(AmountName, "The amount the temperature gets increased with every reheat.", new DoubleValue(20d)));
65            Parameters.Add(new ValueParameter<IntValue>(ThresholdName, "Amount of consecutive rejected solutions that will trigger a reheat.", new IntValue(5)));
66            Parameters.Add(new LookupParameter<DoubleValue>(TemperatureName, "The current temperature."));
67            Parameters.Add(new LookupParameter<ItemList<BoolValue>>(AcceptanceMemoryName, "Memorizes the last N acceptance decisions."));
68            #endregion
69        }
70
71        [StorableConstructor]
72        protected FixedReheater(bool deserializing) : base(deserializing) { }
73        protected FixedReheater(FixedReheater original, Cloner cloner) : base(original, cloner) { }
74
75
76        public override IDeepCloneable Clone(Cloner cloner)
77        {
78            return new FixedReheater(this, cloner);
79        }
80
81        public void Parameterize() { }
82
83        public bool ShouldReheat(bool cooling, ItemList<BoolValue> acceptances) {
84            if (acceptances.Count > ThresholdParameter.Value.Value) acceptances.RemoveAt(0);
85            return acceptances.Count == ThresholdParameter.Value.Value && !acceptances.Any(x => x.Value);
86        }
87        public override IOperation Apply()
88        {
89            AcceptanceMemoryParameter.ActualValue.Clear();
90            TemperatureParameter.ActualValue.Value += AmountParameter.Value.Value;
91            return base.Apply();
92        }
93    }
94}
Note: See TracBrowser for help on using the repository browser.