- Timestamp:
- 10/15/21 17:00:28 (3 years ago)
- 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 1 1 using System; 2 using System.Linq; 2 3 using System.Collections.Generic; 3 4 using HeuristicLab.Core; … … 17 18 get => template; 18 19 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(); 25 24 } 26 25 } … … 31 30 32 31 [Storable] 33 public I Dictionary<SubFunctionTreeNode, SubFunction> SubFunctions { get; private set; } = new Dictionary<SubFunctionTreeNode, SubFunction>();32 public IReadOnlyDictionary<string, SubFunction> SubFunctions { get; private set; } = new Dictionary<string, SubFunction>(); 34 33 35 34 protected InfixExpressionParser Parser { get; set; } = new InfixExpressionParser(); 35 36 36 #endregion 37 37 … … 57 57 58 58 private void GetSubFunctions(ISymbolicExpressionTree tree) { 59 int count = 1; 60 SubFunctions.Clear(); 59 var subFunctions = new Dictionary<string, SubFunction>(); 61 60 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 } 69 78 } 79 SubFunctions = subFunctions; 70 80 } 81 82 private void OnSubFunctionChanged(object sender, EventArgs e) => OnChanged(); 71 83 } 72 84 } -
branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/StructureTemplate/SubFunction.cs
r18067 r18068 18 18 private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth"; 19 19 private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength"; 20 private const string FunctionArgumentsParameterName = "Function Arguments";21 20 #endregion 22 21 … … 25 24 public IFixedValueParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter => (IFixedValueParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; 26 25 public IFixedValueParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter => (IFixedValueParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; 27 public IValueParameter<ReadOnlyItemList<StringValue>> FunctionArgumentsParameter => (IValueParameter<ReadOnlyItemList<StringValue>>)Parameters[FunctionArgumentsParameterName];28 26 #endregion 29 27 … … 44 42 } 45 43 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 52 46 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; 60 49 #endregion 61 50 … … 65 54 Parameters.Add(new FixedValueParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, new IntValue(10))); 66 55 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; 68 61 } 62 63 private void OnParameterValueChanged(object sender, EventArgs e) => Changed?.Invoke(this, EventArgs.Empty); 69 64 70 65 protected SubFunction(SubFunction original, Cloner cloner) { } -
branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Symbols/SubFunctionSymbol.cs
r18065 r18068 9 9 [Item("SubFunctionSymbol", "Symbol that represents a sub function.")] 10 10 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; 13 13 14 14 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 10 10 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { 11 11 [StorableType("05130B5F-0125-4367-A4E9-C42D1085024E")] 12 public class SubFunctionTreeNode : SymbolicExpressionTreeNode { //TODO: as TerminalNode? -> but has children in a fully builded tree12 public class SubFunctionTreeNode : SymbolicExpressionTreeNode { 13 13 14 14 #region Properties … … 17 17 public IEnumerable<string> Arguments { get; set; } = Enumerable.Empty<string>(); 18 18 19 public SubFunction SubFunction { get; set; }20 21 19 public string Name { get; set; } 22 20 #endregion … … 28 26 protected SubFunctionTreeNode(StorableConstructorFlag _) : base(_) { } 29 27 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) { } 33 29 #endregion 34 30 … … 40 36 if (string.IsNullOrEmpty(Name)) 41 37 return base.ToString(); 42 return Name;38 return $"{Name}({string.Join(",", Arguments)})"; 43 39 } 44 40
Note: See TracChangeset
for help on using the changeset viewer.