Changeset 18066 for branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression
- Timestamp:
- 10/12/21 09:41:38 (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/StructuredSymbolicRegressionSingleObjectiveProblem.cs
r18065 r18066 24 24 private const string GrammarParameterName = "Grammar"; 25 25 private const string StructureTemplateParameterName = "Structure Template"; 26 private const string MainTreeResultParameterName = "Main Tree";27 26 #endregion 28 27 … … 32 31 public IFixedValueParameter<StructureTemplate> StructureTemplateParameter => (IFixedValueParameter<StructureTemplate>)Parameters[StructureTemplateParameterName]; 33 32 public IValueParameter<ISymbolicDataAnalysisGrammar> GrammarParameter => (IValueParameter<ISymbolicDataAnalysisGrammar>)Parameters[GrammarParameterName]; 34 public IResultParameter<ISymbolicExpressionTree> MainTreeResultParameter => (IResultParameter<ISymbolicExpressionTree>)Parameters[MainTreeResultParameterName];35 33 #endregion 36 34 … … 61 59 IDataAnalysisProblemData IDataAnalysisProblem.ProblemData => ProblemData; 62 60 63 public override bool Maximization => false;61 public override bool Maximization => true; 64 62 #endregion 65 63 … … 78 76 79 77 var structureTemplate = new StructureTemplate(); 80 structureTemplate.Changed += (sender, args) => { 81 foreach (var e in Encoding.Encodings.ToArray()) 82 Encoding.Remove(e); 78 structureTemplate.Changed += OnTemplateChanged; 83 79 84 foreach(var sf in structureTemplate.SubFunctions.Values) { 85 Encoding.Add(new SymbolicExpressionTreeEncoding(sf.Name, sf.Grammar, sf.MaximumSymbolicExpressionTreeLength, sf.MaximumSymbolicExpressionTreeDepth)); 86 } 87 }; 88 89 Parameters.Add(new ValueParameter<RegressionProblemData>(ProblemDataParameterName, problemData)); 80 Parameters.Add(new ValueParameter<IRegressionProblemData>(ProblemDataParameterName, problemData)); 90 81 Parameters.Add(new FixedValueParameter<StructureTemplate>(StructureTemplateParameterName, structureTemplate)); 91 82 Parameters.Add(new ValueParameter<ISymbolicDataAnalysisGrammar>(GrammarParameterName, grammar)); 92 Parameters.Add(new ResultParameter<ISymbolicExpressionTree>(MainTreeResultParameterName, "")); 93 MainTreeResultParameter.DefaultValue = new SymbolicExpressionTree();83 84 structureTemplate.Template = "f(x)*f(y)+5"; 94 85 } 95 86 … … 105 96 #endregion 106 97 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 98 private void OnTemplateChanged(object sender, EventArgs args) { 99 foreach (var e in Encoding.Encodings.ToArray()) 100 Encoding.Remove(e); 118 101 119 var subtreeCount = parent.SubtreeCount; 120 for (int idx = 0; idx < subtreeCount; ++idx) 121 parent.RemoveSubtree(idx); 102 foreach (var sf in StructureTemplate.SubFunctions.Values) { 103 Encoding.Add(new SymbolicExpressionTreeEncoding(sf.Name, sf.Grammar, sf.MaximumSymbolicExpressionTreeLength, sf.MaximumSymbolicExpressionTreeDepth)); 104 } 105 } 122 106 123 // add new tree 124 parent.AddSubtree(subFunctionTree.Root); 125 } 107 public override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) { 108 base.Analyze(individuals, qualities, results, random); 109 110 int bestIdx = 0; 111 double bestQuality = Maximization ? double.MinValue : double.MaxValue; 112 for(int idx = 0; idx < qualities.Length; ++idx) { 113 if((Maximization && qualities[idx] > bestQuality) || 114 (!Maximization && qualities[idx] < bestQuality)) { 115 bestQuality = qualities[idx]; 116 bestIdx = idx; 126 117 } 127 118 } 128 119 129 MainTreeResultParameter.ActualValue = templateTree; 120 if (results.TryGetValue("Best Tree", out IResult result)) 121 result.Value = BuildTree(individuals[bestIdx]); 122 else 123 results.Add(new Result("Best Tree", BuildTree(individuals[bestIdx]))); 124 130 125 /* 131 foreach (var kvp in individual.Values) { 132 if(kvp.Value is SymbolicExpressionTree tree) { 133 foreach(var n in tree.IterateNodesPrefix()) { 134 if(n.Symbol is Variable v) { 135 var t = v.VariableNames; 136 } 137 } 126 if (results.TryGetValue("Tree", out IResult result)) { 127 var list = result.Value as ItemList<ISymbolicExpressionTree>; 128 list.Clear(); 129 list.AddRange(individuals.Select(x => (BuildTree(x)))); 130 } else 131 results.Add(new Result("Tree", new ItemList<ISymbolicExpressionTree>(individuals.Select(x => (BuildTree(x)))))); 132 */ 133 } 134 135 public override double Evaluate(Individual individual, IRandom random) { 136 var tree = BuildTree(individual); 137 var interpreter = new SymbolicDataAnalysisExpressionTreeInterpreter(); 138 var estimationInterval = ProblemData.VariableRanges.GetInterval(ProblemData.TargetVariable); 139 var quality = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate( 140 interpreter, tree, 141 estimationInterval.LowerBound, estimationInterval.UpperBound, 142 ProblemData, ProblemData.TrainingIndices, false); 143 144 return quality; 145 } 146 147 private ISymbolicExpressionTree BuildTree(Individual individual) { 148 var templateTree = (ISymbolicExpressionTree)StructureTemplate.Tree.Clone(); 149 150 // build main tree 151 foreach (var n in templateTree.IterateNodesPrefix()) { 152 if (n.Symbol is SubFunctionSymbol) { 153 var subFunctionTreeNode = n as SubFunctionTreeNode; 154 var subFunctionTree = individual.SymbolicExpressionTree(subFunctionTreeNode.SubFunction.Name); 155 var parent = n.Parent; 156 157 // remove SubFunctionTreeNode 158 parent.RemoveSubtree(parent.IndexOfSubtree(subFunctionTreeNode)); 159 160 // add new tree 161 var subTree = subFunctionTree.Root.GetSubtree(0) // Start 162 .GetSubtree(0); // Offset 163 parent.AddSubtree(subTree); 138 164 } 139 165 } 140 */ 141 return 0.0; 166 return templateTree; 142 167 } 143 168
Note: See TracChangeset
for help on using the changeset viewer.