Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/29/21 16:56:22 (2 years ago)
Author:
dpiringe
Message:

#3136

  • added a hidden interpreter parameter for StructuredSymbolicRegressionSingleObjectiveProblem
  • fixed a bug which crashed the application by changing ProblemData with different variables
  • fixed a bug which crashed the application by running the problem with an empty StructureTemplate
  • added a better output of exceptions of type AggregateException
  • added and resize event handler to repaint nodes of type SubFunctionTreeNode
  • code cleanup
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/StructuredSymbolicRegressionSingleObjectiveProblem.cs

    r18072 r18075  
    1212using HeuristicLab.Data;
    1313using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
     14using HeuristicLab.PluginInfrastructure;
    1415
    1516namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
     
    1718  [Item(Name = "Structured Symbolic Regression Single Objective Problem (single-objective)", Description = "A problem with a structural definition and unfixed subfunctions.")]
    1819  [Creatable(CreatableAttribute.Categories.GeneticProgrammingProblems, Priority = 150)]
    19   public class StructuredSymbolicRegressionSingleObjectiveProblem : SingleObjectiveBasicProblem<MultiEncoding>, IRegressionProblem, IProblemInstanceConsumer<RegressionProblemData> {
     20  public class StructuredSymbolicRegressionSingleObjectiveProblem : SingleObjectiveBasicProblem<MultiEncoding>, IRegressionProblem, IProblemInstanceConsumer<IRegressionProblemData> {
    2021
    2122    #region Constants
    2223    private const string ProblemDataParameterName = "ProblemData";
    23     private const string StructureDefinitionParameterName = "Structure Definition";
    2424    private const string StructureTemplateParameterName = "Structure Template";
     25    private const string InterpreterParameterName = "Interpreter";
    2526
    2627    private const string StructureTemplateDescriptionText =
     
    3334    #region Parameters
    3435    public IValueParameter<IRegressionProblemData> ProblemDataParameter => (IValueParameter<IRegressionProblemData>)Parameters[ProblemDataParameterName];
    35     public IFixedValueParameter<StringValue> StructureDefinitionParameter => (IFixedValueParameter<StringValue>)Parameters[StructureDefinitionParameterName];
    3636    public IFixedValueParameter<StructureTemplate> StructureTemplateParameter => (IFixedValueParameter<StructureTemplate>)Parameters[StructureTemplateParameterName];
     37    public IValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> InterpreterParameter => (IValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[InterpreterParameterName];
    3738    #endregion
    3839
     
    4647    }
    4748
    48     public string StructureDefinition {
    49       get => StructureDefinitionParameter.Value.Value;
    50       set => StructureDefinitionParameter.Value.Value = value;
    51     }
    52 
    53     public StructureTemplate StructureTemplate {
    54       get => StructureTemplateParameter.Value;
    55     }
    56 
    57     public ISymbolicDataAnalysisExpressionTreeInterpreter Interpreter { get; } = new SymbolicDataAnalysisExpressionTreeInterpreter();
     49    public StructureTemplate StructureTemplate => StructureTemplateParameter.Value;
     50
     51    public ISymbolicDataAnalysisExpressionTreeInterpreter Interpreter => InterpreterParameter.Value;
    5852
    5953    IParameter IDataAnalysisProblem.ProblemDataParameter => ProblemDataParameter;
     
    7468      structureTemplate.Changed += OnTemplateChanged;
    7569
    76       Parameters.Add(new ValueParameter<IRegressionProblemData>(ProblemDataParameterName, problemData));
    77       Parameters.Add(new FixedValueParameter<StructureTemplate>(StructureTemplateParameterName,
    78         StructureTemplateDescriptionText, structureTemplate));
    79 
    80 
     70      Parameters.Add(new ValueParameter<IRegressionProblemData>(
     71        ProblemDataParameterName,
     72        problemData));
     73
     74      Parameters.Add(new FixedValueParameter<StructureTemplate>(
     75        StructureTemplateParameterName,
     76        StructureTemplateDescriptionText,
     77        structureTemplate));
     78
     79      Parameters.Add(new ValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(
     80        InterpreterParameterName,
     81        new SymbolicDataAnalysisExpressionTreeInterpreter())
     82        { Hidden = true });
     83
     84      ProblemDataParameter.ValueChanged += ProblemDataParameterValueChanged;
    8185    }
    8286
     
    9397    #endregion
    9498
     99    private void ProblemDataParameterValueChanged(object sender, EventArgs e) {
     100      StructureTemplate.Reset();
     101      // InfoBox for Reset?
     102    }
     103
    95104    private void OnTemplateChanged(object sender, EventArgs args) {
    96105      SetupStructureTemplate();
     
    103112      foreach (var f in StructureTemplate.SubFunctions.Values) {
    104113        SetupVariables(f);
    105         if(!Encoding.Encodings.Any(x => x.Name == f.Name)) // to prevent the same encoding twice
    106           Encoding.Add(new SymbolicExpressionTreeEncoding(f.Name, f.Grammar, f.MaximumSymbolicExpressionTreeLength, f.MaximumSymbolicExpressionTreeDepth));
     114        if (!Encoding.Encodings.Any(x => x.Name == f.Name)) // to prevent the same encoding twice
     115          Encoding.Add(new SymbolicExpressionTreeEncoding(
     116            f.Name,
     117            f.Grammar,
     118            f.MaximumSymbolicExpressionTreeLength,
     119            f.MaximumSymbolicExpressionTreeDepth));
    107120      }
    108121    }
     
    133146        results.Add(new Result("Best Tree", tree));
    134147      }
    135        
    136148    }
    137149
     
    168180
    169181    private ISymbolicExpressionTree BuildTree(Individual individual) {
     182      if (StructureTemplate.Tree == null)
     183        throw new ArgumentException("No structure template defined!");
     184
    170185      var templateTree = (ISymbolicExpressionTree)StructureTemplate.Tree.Clone();
    171186
     
    175190          var subFunctionTreeNode = n as SubFunctionTreeNode;
    176191          var subFunctionTree = individual.SymbolicExpressionTree(subFunctionTreeNode.Name);
    177           //var parent = n.Parent;
    178 
    179           // remove SubFunctionTreeNode
    180           //parent.RemoveSubtree(parent.IndexOfSubtree(subFunctionTreeNode));
    181192
    182193          // add new tree
    183194          var subTree = subFunctionTree.Root.GetSubtree(0)  // Start
    184195                                            .GetSubtree(0); // Offset
    185           //parent.AddSubtree(subTree);
    186196          subFunctionTreeNode.AddSubtree(subTree);
    187197        }
     
    223233    }
    224234
    225     public void Load(RegressionProblemData data) {
    226       ProblemData = data;
    227       SetupStructureTemplate();
    228     }
     235    public void Load(IRegressionProblemData data) => ProblemData = data;
    229236  }
    230237}
Note: See TracChangeset for help on using the changeset viewer.