Changeset 18066
- Timestamp:
- 10/12/21 09:41:38 (3 years ago)
- Location:
- branches/3136_Structural_GP
- Files:
-
- 3 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 -
branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/StructureTemplate/StructureTemplate.cs
r18065 r18066 1 1 using System; 2 2 using System.Collections.Generic; 3 using System.Linq;4 using System.Text;5 using System.Threading.Tasks;6 3 using HeuristicLab.Core; 7 4 using HEAL.Attic; … … 33 30 public ISymbolicExpressionTree Tree => tree; 34 31 35 //[Storable]36 //private IDictionary<SubFunctionTreeNode, SubFunction> subFunctions;37 32 [Storable] 38 33 public IDictionary<SubFunctionTreeNode, SubFunction> SubFunctions { get; private set; } = new Dictionary<SubFunctionTreeNode, SubFunction>(); 39 //subFunctions == null ? new Dictionary<SubFunctionTreeNode, SubFunction>() : subFunctions;40 34 41 35 protected InfixExpressionParser Parser { get; set; } = new InfixExpressionParser(); … … 49 43 50 44 #region Constructors 51 public StructureTemplate() { 52 Template = "f(x)*f(y)+5"; 53 } 45 public StructureTemplate() { } 54 46 55 47 [StorableConstructor] … … 66 58 private void GetSubFunctions(ISymbolicExpressionTree tree) { 67 59 int count = 1; 60 SubFunctions.Clear(); 68 61 foreach (var node in tree.IterateNodesPrefix()) 69 62 if (node is SubFunctionTreeNode subFunctionTreeNode) { … … 72 65 FunctionArguments = subFunctionTreeNode.FunctionArguments 73 66 }; 67 subFunctionTreeNode.SubFunction = subFunction; 74 68 SubFunctions.Add(subFunctionTreeNode, subFunction); 75 69 } 76 77 70 } 78 71 } -
branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Symbols/SubFunctionTreeNode.cs
r18065 r18066 16 16 17 17 public IEnumerable<string> FunctionArguments { get; set; } = Enumerable.Empty<string>(); 18 19 public SubFunction SubFunction { get; set; } 18 20 #endregion 19 21 … … 24 26 protected SubFunctionTreeNode(StorableConstructorFlag _) : base(_) { } 25 27 26 protected SubFunctionTreeNode(SubFunctionTreeNode original, Cloner cloner) : base(original, cloner) { } 28 protected SubFunctionTreeNode(SubFunctionTreeNode original, Cloner cloner) : base(original, cloner) { 29 this.SubFunction = original.SubFunction; 30 } 27 31 #endregion 28 32
Note: See TracChangeset
for help on using the changeset viewer.