1 | using HeuristicLab.Analysis;
|
---|
2 | using HeuristicLab.Common;
|
---|
3 | using HeuristicLab.Core;
|
---|
4 | using HeuristicLab.Data;
|
---|
5 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
6 | using HeuristicLab.Optimization;
|
---|
7 | using HeuristicLab.Parameters;
|
---|
8 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
9 | using HeuristicLab.Problems.TestFunctions;
|
---|
10 | using HeuristicLab.Random;
|
---|
11 | using System;
|
---|
12 | using System.Linq;
|
---|
13 | using System.Threading;
|
---|
14 |
|
---|
15 | namespace HeuristicLab.Algorithms.DifferentialEvolution
|
---|
16 | {
|
---|
17 |
|
---|
18 | [Item("Differential Evolution (DE)", "")]
|
---|
19 | [StorableClass]
|
---|
20 | [Creatable(CreatableAttribute.Categories.PopulationBasedAlgorithms, Priority = 400)]
|
---|
21 | public class DifferentialEvolution : BasicAlgorithm
|
---|
22 | {
|
---|
23 |
|
---|
24 | public override Type ProblemType
|
---|
25 | {
|
---|
26 | get { return typeof(SingleObjectiveTestFunctionProblem); }
|
---|
27 | }
|
---|
28 | public new SingleObjectiveTestFunctionProblem Problem
|
---|
29 | {
|
---|
30 | get { return (SingleObjectiveTestFunctionProblem)base.Problem; }
|
---|
31 | set { base.Problem = value; }
|
---|
32 | }
|
---|
33 |
|
---|
34 | private readonly IRandom random = new MersenneTwister();
|
---|
35 |
|
---|
36 | #region ParameterNames
|
---|
37 | private const string MaximumEvaluationsParameterName = "Maximum Evaluations";
|
---|
38 | private const string SeedParameterName = "Seed";
|
---|
39 | private const string SetSeedRandomlyParameterName = "SetSeedRandomly";
|
---|
40 | #endregion
|
---|
41 |
|
---|
42 | #region ParameterProperties
|
---|
43 | public IFixedValueParameter<IntValue> MaximumEvaluationsParameter
|
---|
44 | {
|
---|
45 | get { return (IFixedValueParameter<IntValue>)Parameters[MaximumEvaluationsParameterName]; }
|
---|
46 | }
|
---|
47 | public IFixedValueParameter<IntValue> SeedParameter
|
---|
48 | {
|
---|
49 | get { return (IFixedValueParameter<IntValue>)Parameters[SeedParameterName]; }
|
---|
50 | }
|
---|
51 | public FixedValueParameter<BoolValue> SetSeedRandomlyParameter
|
---|
52 | {
|
---|
53 | get { return (FixedValueParameter<BoolValue>)Parameters[SetSeedRandomlyParameterName]; }
|
---|
54 | }
|
---|
55 | #endregion
|
---|
56 |
|
---|
57 | #region Properties
|
---|
58 | public int MaximumEvaluations
|
---|
59 | {
|
---|
60 | get { return MaximumEvaluationsParameter.Value.Value; }
|
---|
61 | set { MaximumEvaluationsParameter.Value.Value = value; }
|
---|
62 | }
|
---|
63 | public int Seed
|
---|
64 | {
|
---|
65 | get { return SeedParameter.Value.Value; }
|
---|
66 | set { SeedParameter.Value.Value = value; }
|
---|
67 | }
|
---|
68 | public bool SetSeedRandomly
|
---|
69 | {
|
---|
70 | get { return SetSeedRandomlyParameter.Value.Value; }
|
---|
71 | set { SetSeedRandomlyParameter.Value.Value = value; }
|
---|
72 | }
|
---|
73 | #endregion
|
---|
74 |
|
---|
75 | #region ResultsProperties
|
---|
76 | private double ResultsBestQuality
|
---|
77 | {
|
---|
78 | get { return ((DoubleValue)Results["Best Quality"].Value).Value; }
|
---|
79 | set { ((DoubleValue)Results["Best Quality"].Value).Value = value; }
|
---|
80 | }
|
---|
81 |
|
---|
82 | private RealVector ResultsBestSolution
|
---|
83 | {
|
---|
84 | get { return (RealVector)Results["Best Solution"].Value; }
|
---|
85 | set { Results["Best Solution"].Value = value; }
|
---|
86 | }
|
---|
87 |
|
---|
88 |
|
---|
89 | private int ResultsEvaluations
|
---|
90 | {
|
---|
91 | get { return ((IntValue)Results["Evaluations"].Value).Value; }
|
---|
92 | set { ((IntValue)Results["Evaluations"].Value).Value = value; }
|
---|
93 | }
|
---|
94 | private int ResultsIterations
|
---|
95 | {
|
---|
96 | get { return ((IntValue)Results["Iterations"].Value).Value; }
|
---|
97 | set { ((IntValue)Results["Iterations"].Value).Value = value; }
|
---|
98 | }
|
---|
99 |
|
---|
100 | private DataTable ResultsQualities
|
---|
101 | {
|
---|
102 | get { return ((DataTable)Results["Qualities"].Value); }
|
---|
103 | }
|
---|
104 | private DataRow ResultsQualitiesBest
|
---|
105 | {
|
---|
106 | get { return ResultsQualities.Rows["Best Quality"]; }
|
---|
107 | }
|
---|
108 |
|
---|
109 | #endregion
|
---|
110 |
|
---|
111 | [StorableConstructor]
|
---|
112 | protected DifferentialEvolution(bool deserializing) : base(deserializing) { }
|
---|
113 |
|
---|
114 | protected DifferentialEvolution(DifferentialEvolution original, Cloner cloner)
|
---|
115 | : base(original, cloner)
|
---|
116 | {
|
---|
117 | }
|
---|
118 |
|
---|
119 | public override IDeepCloneable Clone(Cloner cloner)
|
---|
120 | {
|
---|
121 | return new DifferentialEvolution(this, cloner);
|
---|
122 | }
|
---|
123 |
|
---|
124 | public DifferentialEvolution()
|
---|
125 | {
|
---|
126 | Parameters.Add(new FixedValueParameter<IntValue>(MaximumEvaluationsParameterName, "", new IntValue(Int32.MaxValue)));
|
---|
127 | Parameters.Add(new FixedValueParameter<IntValue>(SeedParameterName, "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
|
---|
128 | Parameters.Add(new FixedValueParameter<BoolValue>(SetSeedRandomlyParameterName, "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
|
---|
129 | }
|
---|
130 |
|
---|
131 |
|
---|
132 |
|
---|
133 | protected override void Run(CancellationToken cancellationToken)
|
---|
134 | {
|
---|
135 |
|
---|
136 | // Set up the algorithm
|
---|
137 | if (SetSeedRandomly) Seed = new System.Random().Next();
|
---|
138 | random.Reset(Seed);
|
---|
139 |
|
---|
140 | // Set up the results display
|
---|
141 | Results.Add(new Result("Iterations", new IntValue(0)));
|
---|
142 | Results.Add(new Result("Evaluations", new IntValue(0)));
|
---|
143 | Results.Add(new Result("Best Solution", new RealVector()));
|
---|
144 | Results.Add(new Result("Best Quality", new DoubleValue(double.NaN)));
|
---|
145 | var table = new DataTable("Qualities");
|
---|
146 | table.Rows.Add(new DataRow("Best Quality"));
|
---|
147 | Results.Add(new Result("Qualities", table));
|
---|
148 |
|
---|
149 | var dim = Problem.ProblemSize.Value;
|
---|
150 | var lb = Problem.Bounds[0, 0];
|
---|
151 | var ub = Problem.Bounds[0, 1];
|
---|
152 | var range = ub - lb;
|
---|
153 | var randomEnumerable = Enumerable.Range(0, dim).Select(_ => random.NextDouble()*range + lb);
|
---|
154 | int evals = 0;
|
---|
155 | double bestSolutionQuality = double.PositiveInfinity;
|
---|
156 | RealVector cand = new RealVector(randomEnumerable.ToArray());
|
---|
157 | RealVector bestSolution = cand;
|
---|
158 | // Loop until iteration limit reached or canceled.
|
---|
159 | while (evals < MaximumEvaluations && !cancellationToken.IsCancellationRequested)
|
---|
160 | {
|
---|
161 | cand = new RealVector(randomEnumerable.ToArray());
|
---|
162 | var q = Obj(cand);
|
---|
163 | evals++;
|
---|
164 | if (q < bestSolutionQuality) {
|
---|
165 | bestSolutionQuality = q;
|
---|
166 | bestSolution = cand;
|
---|
167 | }
|
---|
168 | // cancellationToken.ThrowIfCancellationRequested();
|
---|
169 | if(evals % 1000 == 0) ResultsQualitiesBest.Values.Add(bestSolutionQuality);
|
---|
170 | }
|
---|
171 | ResultsEvaluations = evals;
|
---|
172 | ResultsBestSolution = bestSolution;
|
---|
173 | ResultsBestQuality = bestSolutionQuality;
|
---|
174 | }
|
---|
175 |
|
---|
176 | public double Obj(RealVector x)
|
---|
177 | {
|
---|
178 | if(Problem.Maximization.Value)
|
---|
179 | return -Problem.Evaluator.Evaluate(x);
|
---|
180 |
|
---|
181 | return Problem.Evaluator.Evaluate(x);
|
---|
182 | }
|
---|
183 | }
|
---|
184 | }
|
---|
185 |
|
---|