1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HEAL.Attic;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Data;
|
---|
29 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
30 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
31 | using HeuristicLab.Optimization;
|
---|
32 | using HeuristicLab.Optimization.Operators;
|
---|
33 | using HeuristicLab.Parameters;
|
---|
34 | using HeuristicLab.Problems.Instances;
|
---|
35 | using HeuristicLab.Problems.Instances.DataAnalysis;
|
---|
36 |
|
---|
37 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
|
---|
38 | [StorableType("7464E84B-65CC-440A-91F0-9FA920D730F9")]
|
---|
39 | [Item(Name = "Structured Symbolic Regression Single Objective Problem (single-objective)", Description = "A problem with a structural definition and unfixed subfunctions.")]
|
---|
40 | [Creatable(CreatableAttribute.Categories.GeneticProgrammingProblems, Priority = 150)]
|
---|
41 | public class StructuredSymbolicRegressionSingleObjectiveProblem : SingleObjectiveBasicProblem<MultiEncoding>, IRegressionProblem, IProblemInstanceConsumer<IRegressionProblemData> {
|
---|
42 |
|
---|
43 | #region Constants
|
---|
44 | private const string ProblemDataParameterName = "ProblemData";
|
---|
45 | private const string StructureTemplateParameterName = "Structure Template";
|
---|
46 | private const string InterpreterParameterName = "Interpreter";
|
---|
47 | private const string EstimationLimitsParameterName = "EstimationLimits";
|
---|
48 | private const string BestTrainingSolutionParameterName = "Best Training Solution";
|
---|
49 | private const string ApplyLinearScalingParameterName = "Apply Linear Scaling";
|
---|
50 | private const string OptimizeParametersParameterName = "Optimize Parameters";
|
---|
51 |
|
---|
52 | private const string SymbolicExpressionTreeName = "SymbolicExpressionTree";
|
---|
53 | private const string NumericParametersEncoding = "Numeric Parameters";
|
---|
54 |
|
---|
55 | private const string StructureTemplateDescriptionText =
|
---|
56 | "Enter your expression as string in infix format into the empty input field.\n" +
|
---|
57 | "By checking the \"Apply Linear Scaling\" checkbox you can add the relevant scaling terms to your expression.\n" +
|
---|
58 | "After entering the expression click parse to build the tree.\n" +
|
---|
59 | "To edit the defined sub-functions, click on the corresponding-colored node in the tree view.\n" +
|
---|
60 | "Check the info box besides the input field for more information.";
|
---|
61 | #endregion
|
---|
62 |
|
---|
63 | #region Parameters
|
---|
64 | public IValueParameter<IRegressionProblemData> ProblemDataParameter => (IValueParameter<IRegressionProblemData>)Parameters[ProblemDataParameterName];
|
---|
65 | public IFixedValueParameter<StructureTemplate> StructureTemplateParameter => (IFixedValueParameter<StructureTemplate>)Parameters[StructureTemplateParameterName];
|
---|
66 | public IValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> InterpreterParameter => (IValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[InterpreterParameterName];
|
---|
67 | public IFixedValueParameter<DoubleLimit> EstimationLimitsParameter => (IFixedValueParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName];
|
---|
68 | public IResultParameter<ISymbolicRegressionSolution> BestTrainingSolutionParameter => (IResultParameter<ISymbolicRegressionSolution>)Parameters[BestTrainingSolutionParameterName];
|
---|
69 |
|
---|
70 | public IFixedValueParameter<BoolValue> ApplyLinearScalingParameter => (IFixedValueParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName];
|
---|
71 | public IFixedValueParameter<BoolValue> OptimizeParametersParameter => (IFixedValueParameter<BoolValue>)Parameters[OptimizeParametersParameterName];
|
---|
72 | #endregion
|
---|
73 |
|
---|
74 | #region Properties
|
---|
75 |
|
---|
76 | public IRegressionProblemData ProblemData {
|
---|
77 | get => ProblemDataParameter.Value;
|
---|
78 | set {
|
---|
79 | ProblemDataParameter.Value = value;
|
---|
80 | ProblemDataChanged?.Invoke(this, EventArgs.Empty);
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | public StructureTemplate StructureTemplate => StructureTemplateParameter.Value;
|
---|
85 |
|
---|
86 | public ISymbolicDataAnalysisExpressionTreeInterpreter Interpreter => InterpreterParameter.Value;
|
---|
87 |
|
---|
88 | IParameter IDataAnalysisProblem.ProblemDataParameter => ProblemDataParameter;
|
---|
89 | IDataAnalysisProblemData IDataAnalysisProblem.ProblemData => ProblemData;
|
---|
90 |
|
---|
91 | public DoubleLimit EstimationLimits => EstimationLimitsParameter.Value;
|
---|
92 |
|
---|
93 | public bool ApplyLinearScaling {
|
---|
94 | get => ApplyLinearScalingParameter.Value.Value;
|
---|
95 | set => ApplyLinearScalingParameter.Value.Value = value;
|
---|
96 | }
|
---|
97 |
|
---|
98 | public bool OptimizeParameters {
|
---|
99 | get => OptimizeParametersParameter.Value.Value;
|
---|
100 | set => OptimizeParametersParameter.Value.Value = value;
|
---|
101 | }
|
---|
102 |
|
---|
103 | public override bool Maximization => false;
|
---|
104 | #endregion
|
---|
105 |
|
---|
106 | #region EventHandlers
|
---|
107 | public event EventHandler ProblemDataChanged;
|
---|
108 | #endregion
|
---|
109 |
|
---|
110 | #region Constructors & Cloning
|
---|
111 | public StructuredSymbolicRegressionSingleObjectiveProblem() {
|
---|
112 | var provider = new PhysicsInstanceProvider();
|
---|
113 | var descriptor = new SheetBendingProcess();
|
---|
114 | var problemData = provider.LoadData(descriptor);
|
---|
115 | var shapeConstraintProblemData = new ShapeConstrainedRegressionProblemData(problemData);
|
---|
116 |
|
---|
117 | var structureTemplate = new StructureTemplate();
|
---|
118 |
|
---|
119 | Parameters.Add(new ValueParameter<IRegressionProblemData>(
|
---|
120 | ProblemDataParameterName,
|
---|
121 | shapeConstraintProblemData));
|
---|
122 |
|
---|
123 | Parameters.Add(new FixedValueParameter<StructureTemplate>(
|
---|
124 | StructureTemplateParameterName,
|
---|
125 | StructureTemplateDescriptionText,
|
---|
126 | structureTemplate));
|
---|
127 |
|
---|
128 | Parameters.Add(new FixedValueParameter<BoolValue>(
|
---|
129 | ApplyLinearScalingParameterName, new BoolValue(true)
|
---|
130 | ));
|
---|
131 |
|
---|
132 | Parameters.Add(new FixedValueParameter<BoolValue>(
|
---|
133 | OptimizeParametersParameterName, new BoolValue(true)
|
---|
134 | ));
|
---|
135 |
|
---|
136 | Parameters.Add(new ValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(
|
---|
137 | InterpreterParameterName,
|
---|
138 | new SymbolicDataAnalysisExpressionTreeBatchInterpreter()) { Hidden = true });
|
---|
139 | Parameters.Add(new FixedValueParameter<DoubleLimit>(
|
---|
140 | EstimationLimitsParameterName,
|
---|
141 | new DoubleLimit(double.NegativeInfinity, double.PositiveInfinity)) { Hidden = true });
|
---|
142 | Parameters.Add(new ResultParameter<ISymbolicRegressionSolution>(BestTrainingSolutionParameterName, "") { Hidden = true });
|
---|
143 |
|
---|
144 | this.EvaluatorParameter.Hidden = true;
|
---|
145 |
|
---|
146 | Operators.Add(new SymbolicDataAnalysisVariableFrequencyAnalyzer());
|
---|
147 | Operators.Add(new MinAverageMaxSymbolicExpressionTreeLengthAnalyzer());
|
---|
148 | Operators.Add(new SymbolicExpressionSymbolFrequencyAnalyzer());
|
---|
149 |
|
---|
150 | RegisterEventHandlers();
|
---|
151 |
|
---|
152 | StructureTemplate.ApplyLinearScaling = ApplyLinearScaling;
|
---|
153 | StructureTemplate.Template =
|
---|
154 | "(" +
|
---|
155 | "(210000 / (210000 + h)) * ((sigma_y * t * t) / (wR * Rt * t)) + " +
|
---|
156 | "PlasticHardening(_) - Elasticity(_)" +
|
---|
157 | ")" +
|
---|
158 | " * C(_)";
|
---|
159 | }
|
---|
160 |
|
---|
161 | public StructuredSymbolicRegressionSingleObjectiveProblem(StructuredSymbolicRegressionSingleObjectiveProblem original, Cloner cloner) : base(original, cloner) {
|
---|
162 | RegisterEventHandlers();
|
---|
163 | }
|
---|
164 |
|
---|
165 | public override IDeepCloneable Clone(Cloner cloner) =>
|
---|
166 | new StructuredSymbolicRegressionSingleObjectiveProblem(this, cloner);
|
---|
167 |
|
---|
168 | [StorableConstructor]
|
---|
169 | protected StructuredSymbolicRegressionSingleObjectiveProblem(StorableConstructorFlag _) : base(_) { }
|
---|
170 |
|
---|
171 |
|
---|
172 | [StorableHook(HookType.AfterDeserialization)]
|
---|
173 | private void AfterDeserialization() {
|
---|
174 | if (!Parameters.ContainsKey(ApplyLinearScalingParameterName)) {
|
---|
175 | Parameters.Add(new FixedValueParameter<BoolValue>(ApplyLinearScalingParameterName, new BoolValue(StructureTemplate.ApplyLinearScaling)));
|
---|
176 | }
|
---|
177 |
|
---|
178 | if (!Parameters.ContainsKey(OptimizeParametersParameterName)) {
|
---|
179 | Parameters.Add(new FixedValueParameter<BoolValue>(OptimizeParametersParameterName, new BoolValue(false)));
|
---|
180 | }
|
---|
181 |
|
---|
182 | RegisterEventHandlers();
|
---|
183 | }
|
---|
184 |
|
---|
185 | #endregion
|
---|
186 |
|
---|
187 | private void RegisterEventHandlers() {
|
---|
188 | if (StructureTemplate != null) {
|
---|
189 | StructureTemplate.Changed += OnTemplateChanged;
|
---|
190 | }
|
---|
191 |
|
---|
192 | ProblemDataParameter.ValueChanged += ProblemDataParameterValueChanged;
|
---|
193 | ApplyLinearScalingParameter.Value.ValueChanged += (o, e) => StructureTemplate.ApplyLinearScaling = ApplyLinearScaling;
|
---|
194 | }
|
---|
195 |
|
---|
196 | private void ProblemDataParameterValueChanged(object sender, EventArgs e) {
|
---|
197 | StructureTemplate.Reset();
|
---|
198 | // InfoBox for Reset?
|
---|
199 | }
|
---|
200 |
|
---|
201 | private void OnTemplateChanged(object sender, EventArgs args) {
|
---|
202 | ApplyLinearScaling = StructureTemplate.ApplyLinearScaling;
|
---|
203 | SetupEncoding();
|
---|
204 | }
|
---|
205 |
|
---|
206 | private void SetupEncoding() {
|
---|
207 | foreach (var e in Encoding.Encodings.ToArray())
|
---|
208 | Encoding.Remove(e);
|
---|
209 |
|
---|
210 |
|
---|
211 | var templateNumberTreeNodes = StructureTemplate.Tree.IterateNodesPrefix().OfType<NumberTreeNode>();
|
---|
212 | if (templateNumberTreeNodes.Any()) {
|
---|
213 | var templateParameterValues = templateNumberTreeNodes.Select(n => n.Value).ToArray();
|
---|
214 | var encoding = new RealVectorEncoding(NumericParametersEncoding, templateParameterValues.Length);
|
---|
215 |
|
---|
216 | var creator = encoding.Operators.OfType<NormalDistributedRealVectorCreator>().First();
|
---|
217 | creator.MeanParameter.Value = new RealVector(templateParameterValues);
|
---|
218 | creator.SigmaParameter.Value = new DoubleArray(templateParameterValues.Length);
|
---|
219 | encoding.SolutionCreator = creator;
|
---|
220 |
|
---|
221 |
|
---|
222 | Encoding.Add(encoding);
|
---|
223 | }
|
---|
224 |
|
---|
225 | foreach (var subFunction in StructureTemplate.SubFunctions) {
|
---|
226 | subFunction.SetupVariables(ProblemData.AllowedInputVariables);
|
---|
227 | // prevent the same encoding twice
|
---|
228 | if (Encoding.Encodings.Any(x => x.Name == subFunction.Name)) continue;
|
---|
229 |
|
---|
230 | var encoding = new SymbolicExpressionTreeEncoding(
|
---|
231 | subFunction.Name,
|
---|
232 | subFunction.Grammar,
|
---|
233 | subFunction.MaximumSymbolicExpressionTreeLength,
|
---|
234 | subFunction.MaximumSymbolicExpressionTreeDepth);
|
---|
235 | Encoding.Add(encoding);
|
---|
236 | }
|
---|
237 |
|
---|
238 | //set single point || copy crossover for numeric parameters
|
---|
239 | var multiCrossover = (IParameterizedItem)Encoding.Operators.OfType<MultiEncodingCrossover>().First();
|
---|
240 | foreach (var param in multiCrossover.Parameters.OfType<ConstrainedValueParameter<ICrossover>>()) {
|
---|
241 | if (!param.Name.Contains(NumericParametersEncoding)) continue;
|
---|
242 |
|
---|
243 | var singlePointCrossover = param.ValidValues.OfType<SinglePointCrossover>().First();
|
---|
244 | var copyCrossover = param.ValidValues.OfType<CopyCrossover>().First();
|
---|
245 |
|
---|
246 | var realvectorEncoding = (RealVectorEncoding)Encoding.Encodings.Where(e => e.Name == NumericParametersEncoding).First();
|
---|
247 | if (realvectorEncoding.Length == 1) { //single-point crossover throws if encoding length == 1
|
---|
248 | param.Value = copyCrossover;
|
---|
249 | } else
|
---|
250 | param.Value = singlePointCrossover;
|
---|
251 | }
|
---|
252 |
|
---|
253 | //adapt crossover probability for subtree crossover
|
---|
254 | foreach (var param in multiCrossover.Parameters.OfType<ConstrainedValueParameter<ICrossover>>()) {
|
---|
255 | var subtreeCrossover = param.ValidValues.OfType<SubtreeCrossover>().FirstOrDefault();
|
---|
256 | if (subtreeCrossover != null) {
|
---|
257 | subtreeCrossover.CrossoverProbability = 1.0 / Encoding.Encodings.OfType<SymbolicExpressionTreeEncoding>().Count();
|
---|
258 | param.Value = subtreeCrossover;
|
---|
259 | }
|
---|
260 | }
|
---|
261 |
|
---|
262 | //set multi manipulator as default manipulator for all symbolic expression tree encoding parts
|
---|
263 | var manipulator = (IParameterizedItem)Encoding.Operators.OfType<MultiEncodingManipulator>().First();
|
---|
264 | foreach (var param in manipulator.Parameters.OfType<ConstrainedValueParameter<IManipulator>>()) {
|
---|
265 | var m = param.ValidValues.OfType<MultiSymbolicExpressionTreeManipulator>().FirstOrDefault();
|
---|
266 | param.Value = m ?? param.ValidValues.First();
|
---|
267 | }
|
---|
268 | }
|
---|
269 |
|
---|
270 | public override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) {
|
---|
271 | base.Analyze(individuals, qualities, results, random);
|
---|
272 |
|
---|
273 | var best = GetBestIndividual(individuals, qualities).Item1;
|
---|
274 |
|
---|
275 | if (!results.ContainsKey(BestTrainingSolutionParameter.ActualName)) {
|
---|
276 | results.Add(new Result(BestTrainingSolutionParameter.ActualName, typeof(SymbolicRegressionSolution)));
|
---|
277 | }
|
---|
278 |
|
---|
279 | var tree = (ISymbolicExpressionTree)best[SymbolicExpressionTreeName];
|
---|
280 | var model = new SymbolicRegressionModel(ProblemData.TargetVariable, tree, Interpreter);
|
---|
281 | var solution = model.CreateRegressionSolution(ProblemData);
|
---|
282 |
|
---|
283 | results[BestTrainingSolutionParameter.ActualName].Value = solution;
|
---|
284 | }
|
---|
285 |
|
---|
286 |
|
---|
287 | public override double Evaluate(Individual individual, IRandom random) {
|
---|
288 | var templateTree = StructureTemplate.Tree;
|
---|
289 | if (templateTree == null)
|
---|
290 | throw new ArgumentException("No structure template defined!");
|
---|
291 |
|
---|
292 | var tree = BuildTreeFromIndividual(templateTree, individual, containsNumericParameters: StructureTemplate.ContainsNumericParameters);
|
---|
293 | individual[SymbolicExpressionTreeName] = tree;
|
---|
294 |
|
---|
295 | if (OptimizeParameters) {
|
---|
296 | var excludeNodes = GetTemplateTreeNodes(tree.Root).OfType<IVariableTreeNode>();
|
---|
297 | ParameterOptimization.OptimizeTreeParameters(ProblemData, tree, interpreter: Interpreter, excludeNodes: excludeNodes);
|
---|
298 | } else if (ApplyLinearScaling) {
|
---|
299 | LinearScaling.AdjustLinearScalingParams(ProblemData, tree, Interpreter);
|
---|
300 | }
|
---|
301 |
|
---|
302 | UpdateIndividualFromTree(tree, individual, containsNumericParameters: StructureTemplate.ContainsNumericParameters);
|
---|
303 |
|
---|
304 | //calculate NMSE
|
---|
305 | var estimatedValues = Interpreter.GetSymbolicExpressionTreeValues(tree, ProblemData.Dataset, ProblemData.TrainingIndices);
|
---|
306 | var boundedEstimatedValues = estimatedValues.LimitToRange(EstimationLimits.Lower, EstimationLimits.Upper);
|
---|
307 | var targetValues = ProblemData.TargetVariableTrainingValues;
|
---|
308 | var nmse = OnlineNormalizedMeanSquaredErrorCalculator.Calculate(targetValues, boundedEstimatedValues, out var errorState);
|
---|
309 | if (errorState != OnlineCalculatorError.None)
|
---|
310 | nmse = 1.0;
|
---|
311 |
|
---|
312 | //evaluate constraints
|
---|
313 | var constraints = Enumerable.Empty<ShapeConstraint>();
|
---|
314 | if (ProblemData is ShapeConstrainedRegressionProblemData scProbData)
|
---|
315 | constraints = scProbData.ShapeConstraints.EnabledConstraints;
|
---|
316 | if (constraints.Any()) {
|
---|
317 | var boundsEstimator = new IntervalArithBoundsEstimator();
|
---|
318 | var constraintViolations = IntervalUtil.GetConstraintViolations(constraints, boundsEstimator, ProblemData.VariableRanges, tree);
|
---|
319 |
|
---|
320 | // infinite/NaN constraints
|
---|
321 | if (constraintViolations.Any(x => double.IsNaN(x) || double.IsInfinity(x)))
|
---|
322 | nmse = 1.0;
|
---|
323 |
|
---|
324 | if (constraintViolations.Any(x => x > 0.0))
|
---|
325 | nmse = 1.0;
|
---|
326 | }
|
---|
327 |
|
---|
328 | return nmse;
|
---|
329 | }
|
---|
330 |
|
---|
331 | private static IEnumerable<ISymbolicExpressionTreeNode> GetTemplateTreeNodes(ISymbolicExpressionTreeNode rootNode) {
|
---|
332 | yield return rootNode;
|
---|
333 | foreach (var node in rootNode.Subtrees) {
|
---|
334 | if (node is SubFunctionTreeNode) {
|
---|
335 | yield return node;
|
---|
336 | continue;
|
---|
337 | }
|
---|
338 |
|
---|
339 | foreach (var subNode in GetTemplateTreeNodes(node))
|
---|
340 | yield return subNode;
|
---|
341 | }
|
---|
342 | }
|
---|
343 |
|
---|
344 | private static ISymbolicExpressionTree BuildTreeFromIndividual(ISymbolicExpressionTree template, Individual individual, bool containsNumericParameters) {
|
---|
345 | var resolvedTree = (ISymbolicExpressionTree)template.Clone();
|
---|
346 |
|
---|
347 | //set numeric parameter values
|
---|
348 | if (containsNumericParameters) {
|
---|
349 | var realVector = individual.RealVector(NumericParametersEncoding);
|
---|
350 | var numberTreeNodes = resolvedTree.IterateNodesPrefix().OfType<NumberTreeNode>().ToArray();
|
---|
351 |
|
---|
352 | if (realVector.Length != numberTreeNodes.Length)
|
---|
353 | throw new InvalidOperationException("The number of numeric parameters in the tree does not match the provided numerical values.");
|
---|
354 |
|
---|
355 | for (int i = 0; i < numberTreeNodes.Length; i++)
|
---|
356 | numberTreeNodes[i].Value = realVector[i];
|
---|
357 | }
|
---|
358 |
|
---|
359 | // build main tree
|
---|
360 | foreach (var subFunctionTreeNode in resolvedTree.IterateNodesPrefix().OfType<SubFunctionTreeNode>()) {
|
---|
361 | var subFunctionTree = individual.SymbolicExpressionTree(subFunctionTreeNode.Name);
|
---|
362 |
|
---|
363 | // extract function tree
|
---|
364 | var subTree = subFunctionTree.Root.GetSubtree(0) // StartSymbol
|
---|
365 | .GetSubtree(0); // First Symbol
|
---|
366 | subTree = (ISymbolicExpressionTreeNode)subTree.Clone();
|
---|
367 | subFunctionTreeNode.AddSubtree(subTree);
|
---|
368 | }
|
---|
369 | return resolvedTree;
|
---|
370 | }
|
---|
371 |
|
---|
372 | private static void UpdateIndividualFromTree(ISymbolicExpressionTree tree, Individual individual, bool containsNumericParameters) {
|
---|
373 | var clonedTree = (ISymbolicExpressionTree)tree.Clone();
|
---|
374 |
|
---|
375 | foreach (var subFunctionTreeNode in clonedTree.IterateNodesPrefix().OfType<SubFunctionTreeNode>()) {
|
---|
376 | var grammar = ((ISymbolicExpressionTree)individual[subFunctionTreeNode.Name]).Root.Grammar;
|
---|
377 | var functionTreeNode = subFunctionTreeNode.GetSubtree(0);
|
---|
378 | //remove function code to make numeric parameters extraction easier
|
---|
379 | subFunctionTreeNode.RemoveSubtree(0);
|
---|
380 |
|
---|
381 |
|
---|
382 | var rootNode = (SymbolicExpressionTreeTopLevelNode)new ProgramRootSymbol().CreateTreeNode();
|
---|
383 | rootNode.SetGrammar(grammar);
|
---|
384 | var startNode = (SymbolicExpressionTreeTopLevelNode)new StartSymbol().CreateTreeNode();
|
---|
385 | startNode.SetGrammar(grammar);
|
---|
386 |
|
---|
387 | rootNode.AddSubtree(startNode);
|
---|
388 | startNode.AddSubtree(functionTreeNode);
|
---|
389 | var functionTree = new SymbolicExpressionTree(rootNode);
|
---|
390 | individual[subFunctionTreeNode.Name] = functionTree;
|
---|
391 | }
|
---|
392 |
|
---|
393 | //set numeric parameter values
|
---|
394 | if (containsNumericParameters) {
|
---|
395 | var realVector = individual.RealVector(NumericParametersEncoding);
|
---|
396 | var numberTreeNodes = clonedTree.IterateNodesPrefix().OfType<NumberTreeNode>().ToArray();
|
---|
397 |
|
---|
398 | if (realVector.Length != numberTreeNodes.Length)
|
---|
399 | throw new InvalidOperationException("The number of numeric parameters in the tree does not match the provided numerical values.");
|
---|
400 |
|
---|
401 | for (int i = 0; i < numberTreeNodes.Length; i++)
|
---|
402 | realVector[i] = numberTreeNodes[i].Value;
|
---|
403 | }
|
---|
404 | }
|
---|
405 |
|
---|
406 | public void Load(IRegressionProblemData data) {
|
---|
407 | ProblemData = data;
|
---|
408 | }
|
---|
409 | }
|
---|
410 | }
|
---|