source: trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeProblem.cs @ 12904

Last change on this file since 12904 was 12904, checked in by gkronber, 4 years ago

#2469: implemented override for Analyze(...) method in SymbolicExpressionTreeProblem

File size: 2.8 KB
Line 
1#region License Information
2
3/* HeuristicLab
4 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
5 *
6 * This file is part of HeuristicLab.
7 *
8 * HeuristicLab is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * HeuristicLab is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#endregion
23
24using System;
25using System.Linq;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Optimization;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
32  [StorableClass]
33  public abstract class SymbolicExpressionTreeProblem : SingleObjectiveBasicProblem<SymbolicExpressionTreeEncoding> {
34
35    // persistence
36    [StorableConstructor]
37    protected SymbolicExpressionTreeProblem(bool deserializing) : base(deserializing) { }
38    [StorableHook(HookType.AfterDeserialization)]
39    private void AfterDeserialization() { }
40
41
42    // cloning
43    protected SymbolicExpressionTreeProblem(SymbolicExpressionTreeProblem original, Cloner cloner)
44      : base(original, cloner) {
45    }
46
47    protected SymbolicExpressionTreeProblem() : base() { }
48
49    public virtual bool IsBetter(double quality, double bestQuality) {
50      return (Maximization && quality > bestQuality || !Maximization && quality < bestQuality);
51    }
52
53    public abstract double Evaluate(ISymbolicExpressionTree tree, IRandom random);
54    public sealed override double Evaluate(Individual individual, IRandom random) {
55      return Evaluate(individual.SymbolicExpressionTree(), random);
56    }
57
58    public virtual void Analyze(ISymbolicExpressionTree[] trees, double[] qualities, ResultCollection results, IRandom random) {
59      var bestQuality = Maximization ? qualities.Max() : qualities.Min();
60      var bestIdx = Array.IndexOf(qualities, bestQuality);
61      var best = trees[bestIdx];
62      if (!results.ContainsKey("Best Solution")) {
63        results.Add(new Result("Best Solution", typeof(ISymbolicExpressionTree)));
64      }
65      results["Best Solution"].Value = (IItem)best.Clone();
66    }
67    public sealed override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) {
68      Analyze(individuals.Select(ind => ind.SymbolicExpressionTree()).ToArray(), qualities, results, random);
69    }
70  }
71}
Note: See TracBrowser for help on using the repository browser.