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

Last change on this file since 12901 was 12901, checked in by gkronber, 6 years ago

#2422 reverse merge of r12900 (unintended commit)

File size: 2.7 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.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Optimization;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
31  [StorableClass]
32  public abstract class SymbolicExpressionTreeProblem : SingleObjectiveBasicProblem<SymbolicExpressionTreeEncoding> {
33
34    // persistence
35    [StorableConstructor]
36    protected SymbolicExpressionTreeProblem(bool deserializing) : base(deserializing) { }
37    [StorableHook(HookType.AfterDeserialization)]
38    private void AfterDeserialization() { }
39
40
41    // cloning
42    protected SymbolicExpressionTreeProblem(SymbolicExpressionTreeProblem original, Cloner cloner)
43      : base(original, cloner) {
44    }
45
46    protected SymbolicExpressionTreeProblem() : base() { }
47
48    public virtual bool IsBetter(double quality, double bestQuality) {
49      return (Maximization && quality > bestQuality || !Maximization && quality < bestQuality);
50    }
51
52    public abstract double Evaluate(ISymbolicExpressionTree vector, IRandom random);
53    public sealed override double Evaluate(Individual individual, IRandom random) {
54      return Evaluate(individual.SymbolicExpressionTree(), random);
55    }
56
57    public override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) {
58      base.Analyze(individuals, qualities, results, random);
59      var orderedIndividuals = individuals.Zip(qualities, (i, q) => new { Individual = i, Quality = q }).OrderBy(z => z.Quality);
60      var best = Maximization ? orderedIndividuals.Last().Individual : orderedIndividuals.First().Individual;
61
62      if (!results.ContainsKey("Best Solution")) {
63        results.Add(new Result("Best Solution", typeof(ISymbolicExpressionTree)));
64      }
65      results["Best Solution"].Value = (IItem)best.SymbolicExpressionTree().Clone();
66    }
67  }
68}
Note: See TracBrowser for help on using the repository browser.