[18061] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using System.Text;
|
---|
| 5 | using System.Threading.Tasks;
|
---|
| 6 | using HeuristicLab.Core;
|
---|
| 7 | using HeuristicLab.Optimization;
|
---|
| 8 | using HEAL.Attic;
|
---|
| 9 | using HeuristicLab.Common;
|
---|
| 10 | using HeuristicLab.Problems.Instances;
|
---|
| 11 | using HeuristicLab.Parameters;
|
---|
| 12 | using HeuristicLab.Data;
|
---|
[18062] | 13 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
[18061] | 14 |
|
---|
[18063] | 15 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
|
---|
[18061] | 16 | [StorableType("7464E84B-65CC-440A-91F0-9FA920D730F9")]
|
---|
[18063] | 17 | [Item(Name = "Structured Symbolic Regression Single Objective Problem (single-objective)", Description = "A problem with a structural definition and unfixed subfunctions.")]
|
---|
| 18 | [Creatable(CreatableAttribute.Categories.GeneticProgrammingProblems, Priority = 150)]
|
---|
[18061] | 19 | public class StructuredSymbolicRegressionSingleObjectiveProblem : SingleObjectiveBasicProblem<MultiEncoding>, IRegressionProblem, IProblemInstanceConsumer<RegressionProblemData> {
|
---|
| 20 |
|
---|
| 21 | #region Constants
|
---|
| 22 | private const string ProblemDataParameterName = "ProblemData";
|
---|
| 23 | private const string StructureDefinitionParameterName = "Structure Definition";
|
---|
| 24 | private const string GrammarParameterName = "Grammar";
|
---|
[18063] | 25 | private const string StructureTemplateParameterName = "Structure Template";
|
---|
[18065] | 26 | private const string MainTreeResultParameterName = "Main Tree";
|
---|
[18061] | 27 | #endregion
|
---|
| 28 |
|
---|
| 29 | #region Parameter
|
---|
| 30 | public IValueParameter<IRegressionProblemData> ProblemDataParameter => (IValueParameter<IRegressionProblemData>)Parameters[ProblemDataParameterName];
|
---|
| 31 | public IFixedValueParameter<StringValue> StructureDefinitionParameter => (IFixedValueParameter<StringValue>)Parameters[StructureDefinitionParameterName];
|
---|
[18063] | 32 | public IFixedValueParameter<StructureTemplate> StructureTemplateParameter => (IFixedValueParameter<StructureTemplate>)Parameters[StructureTemplateParameterName];
|
---|
[18065] | 33 | public IValueParameter<ISymbolicDataAnalysisGrammar> GrammarParameter => (IValueParameter<ISymbolicDataAnalysisGrammar>)Parameters[GrammarParameterName];
|
---|
| 34 | public IResultParameter<ISymbolicExpressionTree> MainTreeResultParameter => (IResultParameter<ISymbolicExpressionTree>)Parameters[MainTreeResultParameterName];
|
---|
[18061] | 35 | #endregion
|
---|
| 36 |
|
---|
| 37 | #region Properties
|
---|
| 38 | public IRegressionProblemData ProblemData {
|
---|
| 39 | get => ProblemDataParameter.Value;
|
---|
| 40 | set {
|
---|
| 41 | ProblemDataParameter.Value = value;
|
---|
| 42 | ProblemDataChanged?.Invoke(this, EventArgs.Empty);
|
---|
| 43 | }
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | public string StructureDefinition {
|
---|
| 47 | get => StructureDefinitionParameter.Value.Value;
|
---|
| 48 | set => StructureDefinitionParameter.Value.Value = value;
|
---|
| 49 | }
|
---|
| 50 |
|
---|
[18063] | 51 | public StructureTemplate StructureTemplate {
|
---|
| 52 | get => StructureTemplateParameter.Value;
|
---|
| 53 | }
|
---|
| 54 |
|
---|
[18061] | 55 | public ISymbolicDataAnalysisGrammar Grammar {
|
---|
| 56 | get => GrammarParameter.Value;
|
---|
| 57 | set => GrammarParameter.Value = value;
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | IParameter IDataAnalysisProblem.ProblemDataParameter => ProblemDataParameter;
|
---|
| 61 | IDataAnalysisProblemData IDataAnalysisProblem.ProblemData => ProblemData;
|
---|
| 62 |
|
---|
| 63 | public override bool Maximization => false;
|
---|
| 64 | #endregion
|
---|
| 65 |
|
---|
| 66 | #region EventHandlers
|
---|
| 67 | public event EventHandler ProblemDataChanged;
|
---|
| 68 | #endregion
|
---|
| 69 |
|
---|
| 70 | #region Constructors & Cloning
|
---|
| 71 | public StructuredSymbolicRegressionSingleObjectiveProblem() {
|
---|
[18062] | 72 | var problemData = new ShapeConstrainedRegressionProblemData();
|
---|
| 73 | var grammar = new LinearScalingGrammar();
|
---|
| 74 | var varSym = (Variable)grammar.GetSymbol("Variable");
|
---|
| 75 | varSym.AllVariableNames = problemData.InputVariables.Select(x => x.Value);
|
---|
| 76 | varSym.VariableNames = problemData.InputVariables.Select(x => x.Value);
|
---|
| 77 | varSym.Enabled = true;
|
---|
| 78 |
|
---|
[18065] | 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 |
|
---|
[18062] | 89 | Parameters.Add(new ValueParameter<RegressionProblemData>(ProblemDataParameterName, problemData));
|
---|
[18065] | 90 | Parameters.Add(new FixedValueParameter<StructureTemplate>(StructureTemplateParameterName, structureTemplate));
|
---|
[18062] | 91 | Parameters.Add(new ValueParameter<ISymbolicDataAnalysisGrammar>(GrammarParameterName, grammar));
|
---|
[18065] | 92 | Parameters.Add(new ResultParameter<ISymbolicExpressionTree>(MainTreeResultParameterName, ""));
|
---|
| 93 | MainTreeResultParameter.DefaultValue = new SymbolicExpressionTree();
|
---|
[18061] | 94 | }
|
---|
| 95 |
|
---|
[18063] | 96 | public StructuredSymbolicRegressionSingleObjectiveProblem(StructuredSymbolicRegressionSingleObjectiveProblem original, Cloner cloner) { }
|
---|
[18061] | 97 |
|
---|
| 98 | [StorableConstructor]
|
---|
[18063] | 99 | protected StructuredSymbolicRegressionSingleObjectiveProblem(StorableConstructorFlag _) : base(_) { }
|
---|
[18065] | 100 | #endregion
|
---|
[18061] | 101 |
|
---|
[18065] | 102 | #region Cloning
|
---|
[18061] | 103 | public override IDeepCloneable Clone(Cloner cloner) =>
|
---|
| 104 | new StructuredSymbolicRegressionSingleObjectiveProblem(this, cloner);
|
---|
| 105 | #endregion
|
---|
| 106 |
|
---|
[18065] | 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
|
---|
[18062] | 118 |
|
---|
[18065] | 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 | /*
|
---|
[18063] | 131 | foreach (var kvp in individual.Values) {
|
---|
[18062] | 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 | }
|
---|
| 138 | }
|
---|
| 139 | }
|
---|
[18065] | 140 | */
|
---|
[18061] | 141 | return 0.0;
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | public void Load(RegressionProblemData data) {
|
---|
| 145 | ProblemData = data;
|
---|
| 146 | }
|
---|
| 147 | }
|
---|
| 148 | }
|
---|