Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/08/21 16:28:53 (3 years ago)
Author:
dpiringe
Message:

#3136

  • modified InfixExpressionParser to fully support SubFunctionSymbol
    • created a SubFunctionTreeNode to store the function arguments
  • modified StructureTemplateView to regenerate the content state
  • first implementation for the main tree build up logic
Location:
branches/3136_Structural_GP
Files:
1 added
7 edited

Legend:

Unmodified
Added
Removed
  • branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/StructuredSymbolicRegressionSingleObjectiveProblem.cs

    r18063 r18065  
    2424    private const string GrammarParameterName = "Grammar";
    2525    private const string StructureTemplateParameterName = "Structure Template";
    26 
     26    private const string MainTreeResultParameterName = "Main Tree";
    2727    #endregion
    2828
     
    3131    public IFixedValueParameter<StringValue> StructureDefinitionParameter => (IFixedValueParameter<StringValue>)Parameters[StructureDefinitionParameterName];
    3232    public IFixedValueParameter<StructureTemplate> StructureTemplateParameter => (IFixedValueParameter<StructureTemplate>)Parameters[StructureTemplateParameterName];
    33     public IValueParameter<ISymbolicDataAnalysisGrammar> GrammarParameter => (IValueParameter<ISymbolicDataAnalysisGrammar>)Parameters[GrammarParameterName]; // könnte auch weg?
     33    public IValueParameter<ISymbolicDataAnalysisGrammar> GrammarParameter => (IValueParameter<ISymbolicDataAnalysisGrammar>)Parameters[GrammarParameterName];
     34    public IResultParameter<ISymbolicExpressionTree> MainTreeResultParameter => (IResultParameter<ISymbolicExpressionTree>)Parameters[MainTreeResultParameterName];
    3435    #endregion
    3536
     
    7677      varSym.Enabled = true;
    7778
     79      var structureTemplate = new StructureTemplate();
     80      structureTemplate.Changed += (sender, args) => {
     81        foreach (var e in Encoding.Encodings.ToArray())
     82          Encoding.Remove(e);
     83
     84        foreach(var sf in structureTemplate.SubFunctions.Values) {
     85          Encoding.Add(new SymbolicExpressionTreeEncoding(sf.Name, sf.Grammar, sf.MaximumSymbolicExpressionTreeLength, sf.MaximumSymbolicExpressionTreeDepth));
     86        }
     87      };
     88
    7889      Parameters.Add(new ValueParameter<RegressionProblemData>(ProblemDataParameterName, problemData));
    79       Parameters.Add(new FixedValueParameter<StringValue>(StructureDefinitionParameterName, new StringValue("e^f(x)/F(y)")));
    80       Parameters.Add(new FixedValueParameter<StructureTemplate>(StructureTemplateParameterName, new StructureTemplate()));
     90      Parameters.Add(new FixedValueParameter<StructureTemplate>(StructureTemplateParameterName, structureTemplate));
    8191      Parameters.Add(new ValueParameter<ISymbolicDataAnalysisGrammar>(GrammarParameterName, grammar));
    82       var parser = new InfixExpressionParser();
    83       var tree = parser.Parse(StructureDefinition);
    84      
    85       GetSubFunctions(tree);
     92      Parameters.Add(new ResultParameter<ISymbolicExpressionTree>(MainTreeResultParameterName, ""));
     93      MainTreeResultParameter.DefaultValue = new SymbolicExpressionTree();
    8694    }
    8795
     
    9098    [StorableConstructor]
    9199    protected StructuredSymbolicRegressionSingleObjectiveProblem(StorableConstructorFlag _) : base(_) { }
     100    #endregion
    92101
     102    #region Cloning
    93103    public override IDeepCloneable Clone(Cloner cloner) =>
    94104      new StructuredSymbolicRegressionSingleObjectiveProblem(this, cloner);
    95105    #endregion
    96106
    97     public void GetSubFunctions(ISymbolicExpressionTree tree) {
    98       int count = 1;
    99       foreach(var node in tree.IterateNodesPrefix())
    100         if(node.Symbol is SubFunctionSymbol)
    101           Encoding.Add(new SymbolicExpressionTreeEncoding($"f{count++}", Grammar/*new LinearScalingGrammar()*/, 25, 8));
    102     }
     107    public override double Evaluate(Individual individual, IRandom random) {
     108      var templateTree = (ISymbolicExpressionTree)StructureTemplate.Tree.Clone();
     109      var subFunctionDict = StructureTemplate.SubFunctions;
     110         
     111      // build main tree
     112      foreach (var n in templateTree.IterateNodesPrefix()) {
     113        if(n is SubFunctionTreeNode subFunctionTreeNode) {
     114          if(subFunctionDict.TryGetValue(subFunctionTreeNode, out SubFunction subFunction)) {
     115            var subFunctionTree = individual.SymbolicExpressionTree(subFunction.Name);
     116            var parent = n.Parent;
     117            // remove all subtrees
    103118
    104     public override double Evaluate(Individual individual, IRandom random) {
    105       Console.WriteLine(StructureTemplate.Template);
     119            var subtreeCount = parent.SubtreeCount;
     120            for (int idx = 0; idx < subtreeCount; ++idx)
     121              parent.RemoveSubtree(idx);
     122
     123            // add new tree
     124            parent.AddSubtree(subFunctionTree.Root);
     125          }
     126        }
     127      }
     128
     129      MainTreeResultParameter.ActualValue = templateTree;
     130      /*
    106131      foreach (var kvp in individual.Values) {
    107132        if(kvp.Value is SymbolicExpressionTree tree) {
     
    111136            }
    112137          }
    113           Console.WriteLine(tree);
    114138        }
    115139      }
     140      */
    116141      return 0.0;
    117142    }
  • branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/StructureTemplate/StructureTemplateView.cs

    r18063 r18065  
    2121    }
    2222
    23 
    2423    public StructureTemplateView() {
    2524      InitializeComponent();
    2625      errorLabel.Text = "";
     26    }
     27
     28    protected override void OnContentChanged() {
     29      base.OnContentChanged();
     30      if (Content == null) return;
     31
     32      expressionInput.Text = Content.Template;
     33      symRegTreeChart.Content = Content.Tree;
     34      subFunctionListView.Content = new ItemList<SubFunction>(Content.SubFunctions.Values).AsReadOnly();
     35     
     36      errorLabel.Text = "";
     37     
    2738    }
    2839
     
    3445
    3546          var subFunctionList = new ItemList<SubFunction>();
    36           foreach (var func in Content.SubFunctions)
     47          foreach (var func in Content.SubFunctions.Values)
    3748            subFunctionList.Add(func);
    3849          subFunctionListView.Content = subFunctionList.AsReadOnly();
  • branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.csproj

    r18063 r18065  
    248248    <Compile Include="Symbols\HyperbolicTangent.cs" />
    249249    <Compile Include="Symbols\SubFunctionSymbol.cs" />
     250    <Compile Include="Symbols\SubFunctionTreeNode.cs" />
    250251    <Compile Include="Symbols\VariableBase.cs" />
    251252    <Compile Include="Symbols\VariableTreeNodeBase.cs" />
  • branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Importer/InfixExpressionParser.cs

    r18062 r18065  
    453453            laggedVarNode.Lag = (int)Math.Round(sign * lagToken.doubleVal);
    454454            laggedVarNode.Weight = 1.0;
     455          } else if (funcNode.Symbol is SubFunctionSymbol) {
     456            var subFunction = funcNode as SubFunctionTreeNode;
     457            // input arguments
     458            var args = ParseArgList(tokens);
     459            IList<string> functionArguments = new List<string>();
     460            foreach (var arg in args)
     461              if(arg is VariableTreeNode varTreeNode)
     462                functionArguments.Add(varTreeNode.VariableName);
     463            subFunction.FunctionArguments = functionArguments;
    455464          } else {
    456465            // functions
     
    458467            // check number of arguments
    459468            if (funcNode.Symbol.MinimumArity > args.Length || funcNode.Symbol.MaximumArity < args.Length) {
    460               throw new ArgumentException(string.Format("Symbol {0} requires between {1} and  {2} arguments.", funcId,
     469              throw new ArgumentException(string.Format("Symbol {0} requires between {1} and {2} arguments.", funcId,
    461470                funcNode.Symbol.MinimumArity, funcNode.Symbol.MaximumArity));
    462471            }
  • branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/StructureTemplate/StructureTemplate.cs

    r18063 r18065  
    1414  public class StructureTemplate : Item {
    1515
     16    #region Properties
    1617    [Storable]
    1718    private string template = "";
     
    2223          template = value;
    2324          tree = Parser.Parse(template);
    24           subFunctions = GetSubFunctions(Tree);
     25          GetSubFunctions(Tree);
     26          OnChanged();
    2527        }
    2628      }
     
    3133    public ISymbolicExpressionTree Tree => tree;
    3234
     35    //[Storable]
     36    //private IDictionary<SubFunctionTreeNode, SubFunction> subFunctions;
    3337    [Storable]
    34     private IEnumerable<SubFunction> subFunctions;
    35     public IEnumerable<SubFunction> SubFunctions => subFunctions;
     38    public IDictionary<SubFunctionTreeNode, SubFunction> SubFunctions { get; private set; } = new Dictionary<SubFunctionTreeNode, SubFunction>();
     39      //subFunctions == null ? new Dictionary<SubFunctionTreeNode, SubFunction>() : subFunctions;
    3640
    3741    protected InfixExpressionParser Parser { get; set; } = new InfixExpressionParser();
     42    #endregion
    3843
     44    #region Events
     45    public event EventHandler Changed;
     46   
     47    private void OnChanged() => Changed?.Invoke(this, EventArgs.Empty);
     48    #endregion
    3949
    4050    #region Constructors
    41     public StructureTemplate() { }
     51    public StructureTemplate() {
     52      Template = "f(x)*f(y)+5";
     53    }
    4254
    4355    [StorableConstructor]
     
    5264    #endregion
    5365
    54     private IEnumerable<SubFunction> GetSubFunctions(ISymbolicExpressionTree tree) {
     66    private void GetSubFunctions(ISymbolicExpressionTree tree) {
    5567      int count = 1;
    5668      foreach (var node in tree.IterateNodesPrefix())
    57         if (node.Symbol is SubFunctionSymbol)
    58           yield return new SubFunction() { Name = $"f{count++}" };
     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
    5977    }
    6078  }
  • branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/StructureTemplate/SubFunction.cs

    r18063 r18065  
    1818    private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth";
    1919    private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength";
     20    private const string FunctionArgumentsParameterName = "Function Arguments";
    2021    #endregion
    2122
     
    2425    public IFixedValueParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter => (IFixedValueParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName];
    2526    public IFixedValueParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter => (IFixedValueParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName];
     27    public IValueParameter<ReadOnlyItemList<StringValue>> FunctionArgumentsParameter => (IValueParameter<ReadOnlyItemList<StringValue>>)Parameters[FunctionArgumentsParameterName];
    2628    #endregion
    2729
     
    4143      set => MaximumSymbolicExpressionTreeLengthParameter.Value.Value = value;
    4244    }
     45
     46    public IEnumerable<string> FunctionArguments {
     47      get => FunctionArgumentsParameter.Value.Select(x => x.Value);
     48      set {
     49        var varSym = (Variable)Grammar.GetSymbol("Variable");
     50        if (varSym == null)
     51          throw new ArgumentException($"No variable symbol existent.");
     52
     53        FunctionArgumentsParameter.Value = new ItemList<StringValue>(value.Select(x => new StringValue(x))).AsReadOnly();
     54
     55        varSym.AllVariableNames = FunctionArguments;
     56        varSym.VariableNames = FunctionArguments;
     57        varSym.Enabled = true;
     58      }
     59    }
    4360    #endregion
    4461
     
    4865      Parameters.Add(new FixedValueParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, new IntValue(10)));
    4966      Parameters.Add(new FixedValueParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, new IntValue(30)));
     67      Parameters.Add(new ValueParameter<ReadOnlyItemList<StringValue>>(FunctionArgumentsParameterName, new ReadOnlyItemList<StringValue>()));
    5068    }
    5169
  • branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Symbols/SubFunctionSymbol.cs

    r18062 r18065  
    1 using HEAL.Attic;
     1using System.Collections.Generic;
     2using HEAL.Attic;
    23using HeuristicLab.Common;
    34using HeuristicLab.Core;
     
    910  public class SubFunctionSymbol : Symbol {
    1011    public override int MinimumArity => 1;
    11     public override int MaximumArity => 1;
     12    public override int MaximumArity => byte.MaxValue;
    1213
    1314    public SubFunctionSymbol() : base("SubFunctionSymbol", "Symbol that represents a sub function.") { }
     
    2021    public override IDeepCloneable Clone(Cloner cloner) =>
    2122      new SubFunctionSymbol(this, cloner);
     23
     24    public override ISymbolicExpressionTreeNode CreateTreeNode() => new SubFunctionTreeNode(this);
    2225  }
    2326}
Note: See TracChangeset for help on using the changeset viewer.