[12897] | 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 |
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Optimization;
|
---|
| 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 29 |
|
---|
| 30 | namespace 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 |
|
---|
[12901] | 52 | public abstract double Evaluate(ISymbolicExpressionTree vector, IRandom random);
|
---|
[12897] | 53 | public sealed override double Evaluate(Individual individual, IRandom random) {
|
---|
| 54 | return Evaluate(individual.SymbolicExpressionTree(), random);
|
---|
| 55 | }
|
---|
| 56 |
|
---|
[12901] | 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 |
|
---|
[12897] | 62 | if (!results.ContainsKey("Best Solution")) {
|
---|
| 63 | results.Add(new Result("Best Solution", typeof(ISymbolicExpressionTree)));
|
---|
| 64 | }
|
---|
[12901] | 65 | results["Best Solution"].Value = (IItem)best.SymbolicExpressionTree().Clone();
|
---|
[12897] | 66 | }
|
---|
| 67 | }
|
---|
| 68 | }
|
---|