1 | using HeuristicLab.Operators;
|
---|
2 | using System;
|
---|
3 | using System.Collections.Generic;
|
---|
4 | using System.Linq;
|
---|
5 | using System.Text;
|
---|
6 | using System.Threading.Tasks;
|
---|
7 | using HeuristicLab.Common;
|
---|
8 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
9 | using HeuristicLab.Core;
|
---|
10 | using HeuristicLab.Data;
|
---|
11 | using HeuristicLab.Parameters;
|
---|
12 | using HeuristicLab.Optimization;
|
---|
13 | using HeuristicLab.PluginInfrastructure;
|
---|
14 |
|
---|
15 | namespace 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 | }
|
---|