Changeset 18075


Ignore:
Timestamp:
10/29/21 16:56:22 (8 months 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
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

    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}
  • branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/StructureTemplate/StructureTemplateView.cs

    r18073 r18075  
    22using System.Collections.Generic;
    33using System.Drawing;
     4using System.Linq;
    45using System.Text;
    56using System.Windows.Forms;
     
    2526      InitializeComponent();
    2627      infoLabel.Text = "";
     28      this.Resize += StructureTemplateViewResize;
    2729      treeChart.SymbolicExpressionTreeNodeClicked += SymbolicExpressionTreeNodeClicked;
    2830     
     31    }
     32
     33    private void StructureTemplateViewResize(object sender, EventArgs e) {
     34      PaintTree();
    2935    }
    3036
     
    8389          infoLabel.Text = "Template structure successfully parsed.";
    8490          infoLabel.ForeColor = Color.DarkGreen;
     91        } catch (AggregateException ex) {
     92          infoLabel.Text = string.Join("\n", ex.InnerExceptions.Select(x => x.Message));
     93          infoLabel.ForeColor = Color.DarkRed;
    8594        } catch (Exception ex) {
    8695          infoLabel.Text = ex.Message;
  • branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/StructureTemplate/StructureTemplate.cs

    r18074 r18075  
    1414    #region Properties
    1515    [Storable]
    16     private string template = "";
     16    private string template;
    1717    public string Template {
    1818      get => template;
     
    3838
    3939    [Storable]
    40     public IReadOnlyDictionary<string, SubFunction> SubFunctions { get; private set; } = new Dictionary<string, SubFunction>();
     40    public IReadOnlyDictionary<string, SubFunction> SubFunctions { get; private set; }
    4141
    4242    [Storable]
    43     private bool applyLinearScaling = false;
     43    private bool applyLinearScaling;
    4444    public bool ApplyLinearScaling {
    4545      get => applyLinearScaling;
     
    6161
    6262    #region Constructors
    63     public StructureTemplate() { }
     63    public StructureTemplate() {
     64      Reset();
     65    }
    6466
    6567    [StorableConstructor]
     
    7981    #endregion
    8082
    81    
     83    public void Reset() {
     84      SubFunctions = new Dictionary<string, SubFunction>();
     85      treeWithoutLinearScaling = null;
     86      treeWithLinearScaling = null;
     87      template = "";
     88      applyLinearScaling = false;
     89      OnChanged();
     90    }
    8291
    8392    private Dictionary<string, SubFunction> GetSubFunctions() {
Note: See TracChangeset for help on using the changeset viewer.