[12897] | 1 | #region License Information
|
---|
| 2 |
|
---|
| 3 | /* HeuristicLab
|
---|
[17434] | 4 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[12897] | 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 |
|
---|
[12904] | 24 | using System;
|
---|
[12897] | 25 | using System.Linq;
|
---|
| 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Core;
|
---|
[12921] | 28 | using HeuristicLab.Data;
|
---|
[12897] | 29 | using HeuristicLab.Optimization;
|
---|
[17434] | 30 | using HEAL.Attic;
|
---|
[12897] | 31 |
|
---|
| 32 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
|
---|
[17434] | 33 | [StorableType("A1B9F4C8-5E29-493C-A483-2AC68453BC63")]
|
---|
[12897] | 34 | public abstract class SymbolicExpressionTreeProblem : SingleObjectiveBasicProblem<SymbolicExpressionTreeEncoding> {
|
---|
| 35 |
|
---|
| 36 | // persistence
|
---|
| 37 | [StorableConstructor]
|
---|
[17434] | 38 | protected SymbolicExpressionTreeProblem(StorableConstructorFlag _) : base(_) { }
|
---|
[12897] | 39 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 40 | private void AfterDeserialization() { }
|
---|
| 41 |
|
---|
| 42 |
|
---|
| 43 | // cloning
|
---|
| 44 | protected SymbolicExpressionTreeProblem(SymbolicExpressionTreeProblem original, Cloner cloner)
|
---|
| 45 | : base(original, cloner) {
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | protected SymbolicExpressionTreeProblem() : base() { }
|
---|
| 49 |
|
---|
| 50 | public virtual bool IsBetter(double quality, double bestQuality) {
|
---|
| 51 | return (Maximization && quality > bestQuality || !Maximization && quality < bestQuality);
|
---|
| 52 | }
|
---|
| 53 |
|
---|
[12904] | 54 | public abstract double Evaluate(ISymbolicExpressionTree tree, IRandom random);
|
---|
[12897] | 55 | public sealed override double Evaluate(Individual individual, IRandom random) {
|
---|
| 56 | return Evaluate(individual.SymbolicExpressionTree(), random);
|
---|
| 57 | }
|
---|
| 58 |
|
---|
[12921] | 59 | public virtual void Analyze(ISymbolicExpressionTree[] trees, double[] qualities, ResultCollection results,
|
---|
| 60 | IRandom random) {
|
---|
| 61 | if (!results.ContainsKey("Best Solution Quality")) {
|
---|
| 62 | results.Add(new Result("Best Solution Quality", typeof(DoubleValue)));
|
---|
| 63 | }
|
---|
[12897] | 64 | if (!results.ContainsKey("Best Solution")) {
|
---|
| 65 | results.Add(new Result("Best Solution", typeof(ISymbolicExpressionTree)));
|
---|
| 66 | }
|
---|
[12921] | 67 |
|
---|
| 68 | var bestQuality = Maximization ? qualities.Max() : qualities.Min();
|
---|
| 69 |
|
---|
| 70 | if (results["Best Solution Quality"].Value == null ||
|
---|
| 71 | IsBetter(bestQuality, ((DoubleValue)results["Best Solution Quality"].Value).Value)) {
|
---|
| 72 | var bestIdx = Array.IndexOf(qualities, bestQuality);
|
---|
| 73 | var bestClone = (IItem)trees[bestIdx].Clone();
|
---|
| 74 | results["Best Solution"].Value = bestClone;
|
---|
| 75 | results["Best Solution Quality"].Value = new DoubleValue(bestQuality);
|
---|
| 76 | }
|
---|
[12897] | 77 | }
|
---|
[12921] | 78 |
|
---|
[12904] | 79 | public sealed override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) {
|
---|
| 80 | Analyze(individuals.Select(ind => ind.SymbolicExpressionTree()).ToArray(), qualities, results, random);
|
---|
| 81 | }
|
---|
[12897] | 82 | }
|
---|
| 83 | }
|
---|