Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/15/21 17:00:28 (3 years ago)
Author:
dpiringe
Message:

#3136

  • modified the StructureTemplateView to enable colorful tree nodes of type SubFunctionTreeNode
  • refactored SubFunctionTreeNode, SubFunction and StructureTemplate
Location:
branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/StructureTemplate/StructureTemplate.cs

    r18067 r18068  
    11using System;
     2using System.Linq;
    23using System.Collections.Generic;
    34using HeuristicLab.Core;
     
    1718      get => template;
    1819      set {
    19         if(template != value) {
    20           template = value;
    21           tree = Parser.Parse(template);
    22           GetSubFunctions(Tree);
    23           OnChanged();
    24         }
     20        template = value;
     21        tree = Parser.Parse(template);
     22        GetSubFunctions(Tree);
     23        OnChanged();
    2524      }
    2625    }
     
    3130
    3231    [Storable]
    33     public IDictionary<SubFunctionTreeNode, SubFunction> SubFunctions { get; private set; } = new Dictionary<SubFunctionTreeNode, SubFunction>();
     32    public IReadOnlyDictionary<string, SubFunction> SubFunctions { get; private set; } = new Dictionary<string, SubFunction>();
    3433
    3534    protected InfixExpressionParser Parser { get; set; } = new InfixExpressionParser();
     35
    3636    #endregion
    3737
     
    5757
    5858    private void GetSubFunctions(ISymbolicExpressionTree tree) {
    59       int count = 1;
    60       SubFunctions.Clear();
     59      var subFunctions = new Dictionary<string, SubFunction>();
    6160      foreach (var node in tree.IterateNodesPrefix())
    62         if (node is SubFunctionTreeNode subFunctionTreeNode) {
    63           var subFunction = new SubFunction() {
    64             Name = $"f{count++}({string.Join(",", subFunctionTreeNode.Arguments)})",
    65             FunctionArguments = subFunctionTreeNode.Arguments
    66           };
    67           subFunctionTreeNode.SubFunction = subFunction;
    68           SubFunctions.Add(subFunctionTreeNode, subFunction);
     61        if (node is SubFunctionTreeNode subFunctionTreeNode) {
     62          if (!subFunctionTreeNode.Arguments.Any())
     63            throw new ArgumentException($"The sub-function '{subFunctionTreeNode}' requires inputs (e.g. {subFunctionTreeNode.Name}(var1, var2)).");
     64
     65          if (subFunctions.TryGetValue(subFunctionTreeNode.Name, out SubFunction v)) {
     66            if(!v.Arguments.SequenceEqual(subFunctionTreeNode.Arguments))
     67              throw new ArgumentException(
     68                $"The sub-function '{v.Name}' has (at least two) different signatures " +
     69                $"({v.Name}({string.Join(",", v.Arguments)}) <> {subFunctionTreeNode.Name}({string.Join(",", subFunctionTreeNode.Arguments)})).");
     70          } else {
     71            var subFunction = new SubFunction() {
     72              Name = subFunctionTreeNode.Name,
     73              Arguments = subFunctionTreeNode.Arguments
     74            };
     75            subFunction.Changed += OnSubFunctionChanged;
     76            subFunctions.Add(subFunction.Name, subFunction);
     77          }
    6978        }
     79      SubFunctions = subFunctions;
    7080    }
     81
     82    private void OnSubFunctionChanged(object sender, EventArgs e) => OnChanged();
    7183  }
    7284}
  • branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/StructureTemplate/SubFunction.cs

    r18067 r18068  
    1818    private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth";
    1919    private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength";
    20     private const string FunctionArgumentsParameterName = "Function Arguments";
    2120    #endregion
    2221
     
    2524    public IFixedValueParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter => (IFixedValueParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName];
    2625    public IFixedValueParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter => (IFixedValueParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName];
    27     public IValueParameter<ReadOnlyItemList<StringValue>> FunctionArgumentsParameter => (IValueParameter<ReadOnlyItemList<StringValue>>)Parameters[FunctionArgumentsParameterName];
    2826    #endregion
    2927
     
    4442    }
    4543
    46     public IEnumerable<string> FunctionArguments { // TODO: gehört weg
    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.");
     44    public IEnumerable<string> Arguments { get; set; }
     45    #endregion
    5246
    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     }
     47    #region Events
     48    public event EventHandler Changed;
    6049    #endregion
    6150
     
    6554      Parameters.Add(new FixedValueParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, new IntValue(10)));
    6655      Parameters.Add(new FixedValueParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, new IntValue(30)));
    67       Parameters.Add(new ValueParameter<ReadOnlyItemList<StringValue>>(FunctionArgumentsParameterName, new ReadOnlyItemList<StringValue>()));
     56
     57      // TODO: separate events for each parameter
     58      GrammarParameter.ValueChanged += OnParameterValueChanged;
     59      MaximumSymbolicExpressionTreeDepthParameter.Value.ValueChanged += OnParameterValueChanged;
     60      MaximumSymbolicExpressionTreeLengthParameter.Value.ValueChanged += OnParameterValueChanged;
    6861    }
     62
     63    private void OnParameterValueChanged(object sender, EventArgs e) => Changed?.Invoke(this, EventArgs.Empty);
    6964
    7065    protected SubFunction(SubFunction original, Cloner cloner) { }
  • branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Symbols/SubFunctionSymbol.cs

    r18065 r18068  
    99  [Item("SubFunctionSymbol", "Symbol that represents a sub function.")]
    1010  public class SubFunctionSymbol : Symbol {
    11     public override int MinimumArity => 1;
    12     public override int MaximumArity => byte.MaxValue;
     11    public override int MinimumArity => 0;
     12    public override int MaximumArity => 1;
    1313
    1414    public SubFunctionSymbol() : base("SubFunctionSymbol", "Symbol that represents a sub function.") { }
  • branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Symbols/SubFunctionTreeNode.cs

    r18067 r18068  
    1010namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
    1111  [StorableType("05130B5F-0125-4367-A4E9-C42D1085024E")]
    12   public class SubFunctionTreeNode : SymbolicExpressionTreeNode { //TODO: as TerminalNode? -> but has children in a fully builded tree
     12  public class SubFunctionTreeNode : SymbolicExpressionTreeNode {
    1313
    1414    #region Properties
     
    1717    public IEnumerable<string> Arguments { get; set; } = Enumerable.Empty<string>();
    1818   
    19     public SubFunction SubFunction { get; set; }
    20 
    2119    public string Name { get; set; }
    2220    #endregion
     
    2826    protected SubFunctionTreeNode(StorableConstructorFlag _) : base(_) { }
    2927
    30     protected SubFunctionTreeNode(SubFunctionTreeNode original, Cloner cloner) : base(original, cloner) {
    31       this.SubFunction = original.SubFunction;
    32     }
     28    protected SubFunctionTreeNode(SubFunctionTreeNode original, Cloner cloner) : base(original, cloner) { }
    3329    #endregion
    3430
     
    4036      if (string.IsNullOrEmpty(Name))
    4137        return base.ToString();
    42       return Name;
     38      return $"{Name}({string.Join(",", Arguments)})";
    4339    }
    4440   
Note: See TracChangeset for help on using the changeset viewer.