[18063] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using System.Text;
|
---|
| 5 | using System.Threading.Tasks;
|
---|
| 6 | using HeuristicLab.Core;
|
---|
| 7 | using HEAL.Attic;
|
---|
| 8 | using HeuristicLab.Common;
|
---|
| 9 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 10 |
|
---|
| 11 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
| 12 | [StorableType("E3C038DB-C6AA-457D-9F65-AF16C44CCE22")]
|
---|
| 13 | [Item("StructureTemplate", "Structure Template")]
|
---|
| 14 | public class StructureTemplate : Item {
|
---|
| 15 |
|
---|
[18065] | 16 | #region Properties
|
---|
[18063] | 17 | [Storable]
|
---|
| 18 | private string template = "";
|
---|
| 19 | public string Template {
|
---|
| 20 | get => template;
|
---|
| 21 | set {
|
---|
| 22 | if(template != value) {
|
---|
| 23 | template = value;
|
---|
| 24 | tree = Parser.Parse(template);
|
---|
[18065] | 25 | GetSubFunctions(Tree);
|
---|
| 26 | OnChanged();
|
---|
[18063] | 27 | }
|
---|
| 28 | }
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | [Storable]
|
---|
| 32 | private ISymbolicExpressionTree tree;
|
---|
| 33 | public ISymbolicExpressionTree Tree => tree;
|
---|
| 34 |
|
---|
[18065] | 35 | //[Storable]
|
---|
| 36 | //private IDictionary<SubFunctionTreeNode, SubFunction> subFunctions;
|
---|
[18063] | 37 | [Storable]
|
---|
[18065] | 38 | public IDictionary<SubFunctionTreeNode, SubFunction> SubFunctions { get; private set; } = new Dictionary<SubFunctionTreeNode, SubFunction>();
|
---|
| 39 | //subFunctions == null ? new Dictionary<SubFunctionTreeNode, SubFunction>() : subFunctions;
|
---|
[18063] | 40 |
|
---|
| 41 | protected InfixExpressionParser Parser { get; set; } = new InfixExpressionParser();
|
---|
[18065] | 42 | #endregion
|
---|
[18063] | 43 |
|
---|
[18065] | 44 | #region Events
|
---|
| 45 | public event EventHandler Changed;
|
---|
| 46 |
|
---|
| 47 | private void OnChanged() => Changed?.Invoke(this, EventArgs.Empty);
|
---|
| 48 | #endregion
|
---|
[18063] | 49 |
|
---|
| 50 | #region Constructors
|
---|
[18065] | 51 | public StructureTemplate() {
|
---|
| 52 | Template = "f(x)*f(y)+5";
|
---|
| 53 | }
|
---|
[18063] | 54 |
|
---|
| 55 | [StorableConstructor]
|
---|
| 56 | protected StructureTemplate(StorableConstructorFlag _) : base(_) { }
|
---|
| 57 |
|
---|
| 58 | protected StructureTemplate(StructureTemplate original, Cloner cloner) { }
|
---|
| 59 | #endregion
|
---|
| 60 |
|
---|
| 61 | #region Cloning
|
---|
| 62 | public override IDeepCloneable Clone(Cloner cloner) =>
|
---|
| 63 | new StructureTemplate(this, cloner);
|
---|
| 64 | #endregion
|
---|
| 65 |
|
---|
[18065] | 66 | private void GetSubFunctions(ISymbolicExpressionTree tree) {
|
---|
[18063] | 67 | int count = 1;
|
---|
| 68 | foreach (var node in tree.IterateNodesPrefix())
|
---|
[18065] | 69 | if (node is SubFunctionTreeNode subFunctionTreeNode) {
|
---|
| 70 | var subFunction = new SubFunction() {
|
---|
| 71 | Name = $"f{count++}({string.Join(",", subFunctionTreeNode.FunctionArguments)})",
|
---|
| 72 | FunctionArguments = subFunctionTreeNode.FunctionArguments
|
---|
| 73 | };
|
---|
| 74 | SubFunctions.Add(subFunctionTreeNode, subFunction);
|
---|
| 75 | }
|
---|
| 76 |
|
---|
[18063] | 77 | }
|
---|
| 78 | }
|
---|
| 79 | }
|
---|