Changeset 18065
- Timestamp:
- 10/08/21 16:28:53 (3 years ago)
- 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 24 24 private const string GrammarParameterName = "Grammar"; 25 25 private const string StructureTemplateParameterName = "Structure Template"; 26 26 private const string MainTreeResultParameterName = "Main Tree"; 27 27 #endregion 28 28 … … 31 31 public IFixedValueParameter<StringValue> StructureDefinitionParameter => (IFixedValueParameter<StringValue>)Parameters[StructureDefinitionParameterName]; 32 32 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]; 34 35 #endregion 35 36 … … 76 77 varSym.Enabled = true; 77 78 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 78 89 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)); 81 91 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(); 86 94 } 87 95 … … 90 98 [StorableConstructor] 91 99 protected StructuredSymbolicRegressionSingleObjectiveProblem(StorableConstructorFlag _) : base(_) { } 100 #endregion 92 101 102 #region Cloning 93 103 public override IDeepCloneable Clone(Cloner cloner) => 94 104 new StructuredSymbolicRegressionSingleObjectiveProblem(this, cloner); 95 105 #endregion 96 106 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 103 118 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 /* 106 131 foreach (var kvp in individual.Values) { 107 132 if(kvp.Value is SymbolicExpressionTree tree) { … … 111 136 } 112 137 } 113 Console.WriteLine(tree);114 138 } 115 139 } 140 */ 116 141 return 0.0; 117 142 } -
branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/StructureTemplate/StructureTemplateView.cs
r18063 r18065 21 21 } 22 22 23 24 23 public StructureTemplateView() { 25 24 InitializeComponent(); 26 25 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 27 38 } 28 39 … … 34 45 35 46 var subFunctionList = new ItemList<SubFunction>(); 36 foreach (var func in Content.SubFunctions )47 foreach (var func in Content.SubFunctions.Values) 37 48 subFunctionList.Add(func); 38 49 subFunctionListView.Content = subFunctionList.AsReadOnly(); -
branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.csproj
r18063 r18065 248 248 <Compile Include="Symbols\HyperbolicTangent.cs" /> 249 249 <Compile Include="Symbols\SubFunctionSymbol.cs" /> 250 <Compile Include="Symbols\SubFunctionTreeNode.cs" /> 250 251 <Compile Include="Symbols\VariableBase.cs" /> 251 252 <Compile Include="Symbols\VariableTreeNodeBase.cs" /> -
branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Importer/InfixExpressionParser.cs
r18062 r18065 453 453 laggedVarNode.Lag = (int)Math.Round(sign * lagToken.doubleVal); 454 454 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; 455 464 } else { 456 465 // functions … … 458 467 // check number of arguments 459 468 if (funcNode.Symbol.MinimumArity > args.Length || funcNode.Symbol.MaximumArity < args.Length) { 460 throw new ArgumentException(string.Format("Symbol {0} requires between {1} and 469 throw new ArgumentException(string.Format("Symbol {0} requires between {1} and {2} arguments.", funcId, 461 470 funcNode.Symbol.MinimumArity, funcNode.Symbol.MaximumArity)); 462 471 } -
branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/StructureTemplate/StructureTemplate.cs
r18063 r18065 14 14 public class StructureTemplate : Item { 15 15 16 #region Properties 16 17 [Storable] 17 18 private string template = ""; … … 22 23 template = value; 23 24 tree = Parser.Parse(template); 24 subFunctions = GetSubFunctions(Tree); 25 GetSubFunctions(Tree); 26 OnChanged(); 25 27 } 26 28 } … … 31 33 public ISymbolicExpressionTree Tree => tree; 32 34 35 //[Storable] 36 //private IDictionary<SubFunctionTreeNode, SubFunction> subFunctions; 33 37 [Storable] 34 p rivate 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; 36 40 37 41 protected InfixExpressionParser Parser { get; set; } = new InfixExpressionParser(); 42 #endregion 38 43 44 #region Events 45 public event EventHandler Changed; 46 47 private void OnChanged() => Changed?.Invoke(this, EventArgs.Empty); 48 #endregion 39 49 40 50 #region Constructors 41 public StructureTemplate() { } 51 public StructureTemplate() { 52 Template = "f(x)*f(y)+5"; 53 } 42 54 43 55 [StorableConstructor] … … 52 64 #endregion 53 65 54 private IEnumerable<SubFunction>GetSubFunctions(ISymbolicExpressionTree tree) {66 private void GetSubFunctions(ISymbolicExpressionTree tree) { 55 67 int count = 1; 56 68 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 59 77 } 60 78 } -
branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/StructureTemplate/SubFunction.cs
r18063 r18065 18 18 private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth"; 19 19 private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength"; 20 private const string FunctionArgumentsParameterName = "Function Arguments"; 20 21 #endregion 21 22 … … 24 25 public IFixedValueParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter => (IFixedValueParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; 25 26 public IFixedValueParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter => (IFixedValueParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; 27 public IValueParameter<ReadOnlyItemList<StringValue>> FunctionArgumentsParameter => (IValueParameter<ReadOnlyItemList<StringValue>>)Parameters[FunctionArgumentsParameterName]; 26 28 #endregion 27 29 … … 41 43 set => MaximumSymbolicExpressionTreeLengthParameter.Value.Value = value; 42 44 } 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 } 43 60 #endregion 44 61 … … 48 65 Parameters.Add(new FixedValueParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, new IntValue(10))); 49 66 Parameters.Add(new FixedValueParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, new IntValue(30))); 67 Parameters.Add(new ValueParameter<ReadOnlyItemList<StringValue>>(FunctionArgumentsParameterName, new ReadOnlyItemList<StringValue>())); 50 68 } 51 69 -
branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Symbols/SubFunctionSymbol.cs
r18062 r18065 1 using HEAL.Attic; 1 using System.Collections.Generic; 2 using HEAL.Attic; 2 3 using HeuristicLab.Common; 3 4 using HeuristicLab.Core; … … 9 10 public class SubFunctionSymbol : Symbol { 10 11 public override int MinimumArity => 1; 11 public override int MaximumArity => 1;12 public override int MaximumArity => byte.MaxValue; 12 13 13 14 public SubFunctionSymbol() : base("SubFunctionSymbol", "Symbol that represents a sub function.") { } … … 20 21 public override IDeepCloneable Clone(Cloner cloner) => 21 22 new SubFunctionSymbol(this, cloner); 23 24 public override ISymbolicExpressionTreeNode CreateTreeNode() => new SubFunctionTreeNode(this); 22 25 } 23 26 }
Note: See TracChangeset
for help on using the changeset viewer.