Changeset 18068 for branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression
- Timestamp:
- 10/15/21 17:00:28 (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/StructuredSymbolicRegressionSingleObjectiveProblem.cs
r18067 r18068 22 22 private const string ProblemDataParameterName = "ProblemData"; 23 23 private const string StructureDefinitionParameterName = "Structure Definition"; 24 private const string GrammarParameterName = "Grammar";25 24 private const string StructureTemplateParameterName = "Structure Template"; 26 25 #endregion … … 30 29 public IFixedValueParameter<StringValue> StructureDefinitionParameter => (IFixedValueParameter<StringValue>)Parameters[StructureDefinitionParameterName]; 31 30 public IFixedValueParameter<StructureTemplate> StructureTemplateParameter => (IFixedValueParameter<StructureTemplate>)Parameters[StructureTemplateParameterName]; 32 public IValueParameter<ISymbolicDataAnalysisGrammar> GrammarParameter => (IValueParameter<ISymbolicDataAnalysisGrammar>)Parameters[GrammarParameterName];33 31 #endregion 34 32 … … 51 49 } 52 50 53 public ISymbolicDataAnalysisGrammar Grammar {54 get => GrammarParameter.Value;55 set => GrammarParameter.Value = value;56 }57 58 51 IParameter IDataAnalysisProblem.ProblemDataParameter => ProblemDataParameter; 59 52 IDataAnalysisProblemData IDataAnalysisProblem.ProblemData => ProblemData; … … 69 62 public StructuredSymbolicRegressionSingleObjectiveProblem() { 70 63 var problemData = new ShapeConstrainedRegressionProblemData(); 71 var grammar = new LinearScalingGrammar();72 var varSym = (Variable)grammar.GetSymbol("Variable");73 varSym.AllVariableNames = problemData.InputVariables.Select(x => x.Value);74 varSym.VariableNames = problemData.InputVariables.Select(x => x.Value);75 varSym.Enabled = true;76 64 77 65 var structureTemplate = new StructureTemplate(); … … 80 68 Parameters.Add(new ValueParameter<IRegressionProblemData>(ProblemDataParameterName, problemData)); 81 69 Parameters.Add(new FixedValueParameter<StructureTemplate>(StructureTemplateParameterName, structureTemplate)); 82 Parameters.Add(new ValueParameter<ISymbolicDataAnalysisGrammar>(GrammarParameterName, grammar));83 70 84 //structureTemplate.Template = "f(x)*f(y)+5";85 71 } 86 72 … … 97 83 98 84 private void OnTemplateChanged(object sender, EventArgs args) { 85 SetupStructureTemplate(); 86 } 87 88 private void SetupStructureTemplate() { 99 89 foreach (var e in Encoding.Encodings.ToArray()) 100 90 Encoding.Remove(e); 101 91 102 foreach (var sf in StructureTemplate.SubFunctions.Values) { 103 Encoding.Add(new SymbolicExpressionTreeEncoding(sf.Name, sf.Grammar, sf.MaximumSymbolicExpressionTreeLength, sf.MaximumSymbolicExpressionTreeDepth)); 92 foreach (var f in StructureTemplate.SubFunctions.Values) { 93 SetupVariables(f); 94 if(!Encoding.Encodings.Any(x => x.Name == f.Name)) // to prevent the same encoding twice 95 Encoding.Add(new SymbolicExpressionTreeEncoding(f.Name, f.Grammar, f.MaximumSymbolicExpressionTreeLength, f.MaximumSymbolicExpressionTreeDepth)); 104 96 } 105 97 } … … 122 114 else 123 115 results.Add(new Result("Best Tree", BuildTree(individuals[bestIdx]))); 124 125 /*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 } else131 results.Add(new Result("Tree", new ItemList<ISymbolicExpressionTree>(individuals.Select(x => (BuildTree(x))))));132 */133 116 } 134 117 … … 152 135 if (n.Symbol is SubFunctionSymbol) { 153 136 var subFunctionTreeNode = n as SubFunctionTreeNode; 154 var subFunctionTree = individual.SymbolicExpressionTree(subFunctionTreeNode. SubFunction.Name);137 var subFunctionTree = individual.SymbolicExpressionTree(subFunctionTreeNode.Name); 155 138 var parent = n.Parent; 156 139 … … 167 150 } 168 151 152 private void SetupVariables(SubFunction subFunction) { 153 var varSym = (Variable)subFunction.Grammar.GetSymbol("Variable"); 154 if (varSym == null) { 155 varSym = new Variable(); 156 subFunction.Grammar.AddSymbol(varSym); 157 } 158 159 var allVariables = ProblemData.InputVariables.Select(x => x.Value); 160 var allInputs = allVariables.Where(x => x != ProblemData.TargetVariable); 161 162 // set all variables 163 varSym.AllVariableNames = allVariables; 164 165 // set all allowed variables 166 if (subFunction.Arguments.Contains("_")) { 167 varSym.VariableNames = allInputs; 168 } else { 169 var vars = new List<string>(); 170 var exceptions = new List<Exception>(); 171 foreach (var arg in subFunction.Arguments) { 172 if (allInputs.Contains(arg)) 173 vars.Add(arg); 174 else 175 exceptions.Add(new ArgumentException($"The argument '{arg}' for sub-function '{subFunction.Name}' is not a valid variable.")); 176 } 177 if (exceptions.Any()) 178 throw new AggregateException(exceptions); 179 varSym.VariableNames = vars; 180 } 181 182 varSym.Enabled = true; 183 } 184 169 185 public void Load(RegressionProblemData data) { 170 186 ProblemData = data; 187 SetupStructureTemplate(); 171 188 } 172 189 }
Note: See TracChangeset
for help on using the changeset viewer.