[18190] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
| 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
[18146] | 23 | using System.Collections.Generic;
|
---|
[18068] | 24 | using System.Linq;
|
---|
[18063] | 25 | using HEAL.Attic;
|
---|
| 26 | using HeuristicLab.Common;
|
---|
[18146] | 27 | using HeuristicLab.Core;
|
---|
[18063] | 28 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 29 |
|
---|
| 30 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
| 31 | [StorableType("E3C038DB-C6AA-457D-9F65-AF16C44CCE22")]
|
---|
| 32 | [Item("StructureTemplate", "Structure Template")]
|
---|
| 33 | public class StructureTemplate : Item {
|
---|
| 34 |
|
---|
[18065] | 35 | #region Properties
|
---|
[18063] | 36 | [Storable]
|
---|
[18075] | 37 | private string template;
|
---|
[18063] | 38 | public string Template {
|
---|
[18146] | 39 | get => template;
|
---|
[18063] | 40 | set {
|
---|
[18187] | 41 | if (value == template) return;
|
---|
| 42 |
|
---|
[18199] | 43 | var parsedTree = Parser.Parse(value);
|
---|
| 44 | //assignment must be done after successfully parsing the tree
|
---|
[18068] | 45 | template = value;
|
---|
[18199] | 46 |
|
---|
[18187] | 47 | if (applyLinearScaling)
|
---|
[18191] | 48 | parsedTree = LinearScaling.AddLinearScalingTerms(parsedTree);
|
---|
[18187] | 49 | Tree = parsedTree;
|
---|
[18068] | 50 | OnChanged();
|
---|
[18146] | 51 | }
|
---|
[18063] | 52 | }
|
---|
| 53 |
|
---|
[18188] | 54 | private ISymbolicExpressionTree _oldTree;
|
---|
| 55 | [Storable(OldName = "treeWithoutLinearScaling")]
|
---|
| 56 | private ISymbolicExpressionTree _OldTreeW {
|
---|
| 57 | set => _oldTree = value;
|
---|
| 58 | }
|
---|
| 59 |
|
---|
[18063] | 60 | [Storable]
|
---|
[18187] | 61 | private ISymbolicExpressionTree tree;
|
---|
[18069] | 62 | public ISymbolicExpressionTree Tree {
|
---|
[18187] | 63 | get => tree;
|
---|
[18069] | 64 | private set {
|
---|
[18194] | 65 | containsNumericParameters = null;
|
---|
[18164] | 66 |
|
---|
[18199] | 67 | var newFunctions = CreateSubFunctions(value);
|
---|
[18187] | 68 | var oldFunctions = subFunctions?.Intersect(newFunctions)
|
---|
[18164] | 69 | ?? Enumerable.Empty<SubFunction>();
|
---|
| 70 | // adds new functions and keeps the old ones (if they match)
|
---|
[18195] | 71 | var functionsToAdd = newFunctions.Except(oldFunctions);
|
---|
| 72 | subFunctions = functionsToAdd.Concat(oldFunctions).ToList();
|
---|
| 73 | RegisterSubFunctionEventHandlers(functionsToAdd);
|
---|
[18199] | 74 |
|
---|
| 75 | tree = value;
|
---|
[18069] | 76 | }
|
---|
| 77 | }
|
---|
[18063] | 78 |
|
---|
[18194] | 79 | private bool? containsNumericParameters;
|
---|
| 80 | public bool ContainsNumericParameters {
|
---|
| 81 | get {
|
---|
| 82 | if (!containsNumericParameters.HasValue)
|
---|
| 83 | containsNumericParameters = Tree.IterateNodesPrefix().OfType<NumberTreeNode>().Any();
|
---|
| 84 |
|
---|
| 85 | return containsNumericParameters.Value;
|
---|
| 86 | }
|
---|
| 87 | }
|
---|
| 88 |
|
---|
[18063] | 89 | [Storable]
|
---|
[18151] | 90 | private IList<SubFunction> subFunctions = new List<SubFunction>();
|
---|
| 91 | public IEnumerable<SubFunction> SubFunctions => subFunctions;
|
---|
[18063] | 92 |
|
---|
[18069] | 93 | [Storable]
|
---|
[18075] | 94 | private bool applyLinearScaling;
|
---|
[18069] | 95 | public bool ApplyLinearScaling {
|
---|
| 96 | get => applyLinearScaling;
|
---|
| 97 | set {
|
---|
[18187] | 98 | if (value == applyLinearScaling) return;
|
---|
| 99 |
|
---|
[18069] | 100 | applyLinearScaling = value;
|
---|
[18191] | 101 | if (applyLinearScaling) LinearScaling.AddLinearScalingTerms(Tree);
|
---|
| 102 | else LinearScaling.RemoveLinearScalingTerms(Tree);
|
---|
[18187] | 103 |
|
---|
[18069] | 104 | OnChanged();
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 |
|
---|
[18063] | 108 | protected InfixExpressionParser Parser { get; set; } = new InfixExpressionParser();
|
---|
[18065] | 109 | #endregion
|
---|
[18063] | 110 |
|
---|
[18065] | 111 | #region Events
|
---|
| 112 | public event EventHandler Changed;
|
---|
[18146] | 113 |
|
---|
[18065] | 114 | private void OnChanged() => Changed?.Invoke(this, EventArgs.Empty);
|
---|
| 115 | #endregion
|
---|
[18063] | 116 |
|
---|
| 117 | #region Constructors
|
---|
[18075] | 118 | public StructureTemplate() {
|
---|
| 119 | Reset();
|
---|
| 120 | }
|
---|
[18063] | 121 |
|
---|
| 122 | [StorableConstructor]
|
---|
| 123 | protected StructureTemplate(StorableConstructorFlag _) : base(_) { }
|
---|
| 124 |
|
---|
[18074] | 125 | protected StructureTemplate(StructureTemplate original, Cloner cloner) : base(original, cloner) {
|
---|
[18187] | 126 | this.tree = cloner.Clone(original.tree);
|
---|
| 127 | this.template = original.Template;
|
---|
| 128 | this.applyLinearScaling = original.ApplyLinearScaling;
|
---|
[18151] | 129 | this.subFunctions = original.subFunctions.Select(cloner.Clone).ToList();
|
---|
[18195] | 130 | RegisterSubFunctionEventHandlers(SubFunctions);
|
---|
[18074] | 131 | }
|
---|
[18151] | 132 |
|
---|
| 133 |
|
---|
| 134 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 135 | private void AfterDeserialization() {
|
---|
[18188] | 136 | if (Tree == null && _oldTree != null) {
|
---|
[18191] | 137 | if (ApplyLinearScaling) _oldTree = LinearScaling.AddLinearScalingTerms(_oldTree);
|
---|
[18188] | 138 | Tree = _oldTree;
|
---|
| 139 | _oldTree = null;
|
---|
| 140 | }
|
---|
| 141 |
|
---|
[18195] | 142 | RegisterSubFunctionEventHandlers(SubFunctions);
|
---|
[18151] | 143 | }
|
---|
[18063] | 144 | #endregion
|
---|
| 145 |
|
---|
| 146 | #region Cloning
|
---|
| 147 | public override IDeepCloneable Clone(Cloner cloner) =>
|
---|
| 148 | new StructureTemplate(this, cloner);
|
---|
| 149 | #endregion
|
---|
| 150 |
|
---|
[18075] | 151 | public void Reset() {
|
---|
[18151] | 152 | subFunctions = new List<SubFunction>();
|
---|
[18187] | 153 | tree = null;
|
---|
[18199] | 154 | containsNumericParameters = null;
|
---|
[18152] | 155 | Template = "f(_)";
|
---|
[18075] | 156 | }
|
---|
[18072] | 157 |
|
---|
[18195] | 158 | private static IList<SubFunction> CreateSubFunctions(ISymbolicExpressionTree tree) {
|
---|
[18151] | 159 | var subFunctions = new List<SubFunction>();
|
---|
[18195] | 160 | foreach (var node in tree.IterateNodesPrefix())
|
---|
[18068] | 161 | if (node is SubFunctionTreeNode subFunctionTreeNode) {
|
---|
| 162 | if (!subFunctionTreeNode.Arguments.Any())
|
---|
| 163 | throw new ArgumentException($"The sub-function '{subFunctionTreeNode}' requires inputs (e.g. {subFunctionTreeNode.Name}(var1, var2)).");
|
---|
| 164 |
|
---|
[18151] | 165 | var existingSubFunction = subFunctions.Where(x => x.Name == subFunctionTreeNode.Name).FirstOrDefault();
|
---|
| 166 | if (existingSubFunction != null) {
|
---|
[18187] | 167 | // an existing subFunction must have the same signature
|
---|
| 168 | if (!existingSubFunction.Arguments.SequenceEqual(subFunctionTreeNode.Arguments))
|
---|
[18068] | 169 | throw new ArgumentException(
|
---|
[18151] | 170 | $"The sub-function '{existingSubFunction.Name}' has (at least two) different signatures " +
|
---|
| 171 | $"({existingSubFunction.Name}({string.Join(",", existingSubFunction.Arguments)}) <> " +
|
---|
| 172 | $"{subFunctionTreeNode.Name}({string.Join(",", subFunctionTreeNode.Arguments)})).");
|
---|
[18068] | 173 | } else {
|
---|
| 174 | var subFunction = new SubFunction() {
|
---|
| 175 | Name = subFunctionTreeNode.Name,
|
---|
| 176 | Arguments = subFunctionTreeNode.Arguments
|
---|
| 177 | };
|
---|
[18151] | 178 | subFunctions.Add(subFunction);
|
---|
[18068] | 179 | }
|
---|
[18065] | 180 | }
|
---|
[18069] | 181 | return subFunctions;
|
---|
[18063] | 182 | }
|
---|
[18068] | 183 |
|
---|
[18195] | 184 | private void RegisterSubFunctionEventHandlers(IEnumerable<SubFunction> subFunctions) {
|
---|
| 185 | foreach (var sf in subFunctions) {
|
---|
| 186 | sf.Changed += (o, e) => OnChanged();
|
---|
[18151] | 187 | }
|
---|
[18199] | 188 | }
|
---|
[18063] | 189 | }
|
---|
| 190 | }
|
---|