Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/15/21 17:00:28 (3 years ago)
Author:
dpiringe
Message:

#3136

  • modified the StructureTemplateView to enable colorful tree nodes of type SubFunctionTreeNode
  • refactored SubFunctionTreeNode, SubFunction and StructureTemplate
File:
1 edited

Legend:

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

    r18067 r18068  
    2222    private const string ProblemDataParameterName = "ProblemData";
    2323    private const string StructureDefinitionParameterName = "Structure Definition";
    24     private const string GrammarParameterName = "Grammar";
    2524    private const string StructureTemplateParameterName = "Structure Template";
    2625    #endregion
     
    3029    public IFixedValueParameter<StringValue> StructureDefinitionParameter => (IFixedValueParameter<StringValue>)Parameters[StructureDefinitionParameterName];
    3130    public IFixedValueParameter<StructureTemplate> StructureTemplateParameter => (IFixedValueParameter<StructureTemplate>)Parameters[StructureTemplateParameterName];
    32     public IValueParameter<ISymbolicDataAnalysisGrammar> GrammarParameter => (IValueParameter<ISymbolicDataAnalysisGrammar>)Parameters[GrammarParameterName];
    3331    #endregion
    3432
     
    5149    }
    5250
    53     public ISymbolicDataAnalysisGrammar Grammar {
    54       get => GrammarParameter.Value;
    55       set => GrammarParameter.Value = value;
    56     }
    57 
    5851    IParameter IDataAnalysisProblem.ProblemDataParameter => ProblemDataParameter;
    5952    IDataAnalysisProblemData IDataAnalysisProblem.ProblemData => ProblemData;
     
    6962    public StructuredSymbolicRegressionSingleObjectiveProblem() {
    7063      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;
    7664
    7765      var structureTemplate = new StructureTemplate();
     
    8068      Parameters.Add(new ValueParameter<IRegressionProblemData>(ProblemDataParameterName, problemData));
    8169      Parameters.Add(new FixedValueParameter<StructureTemplate>(StructureTemplateParameterName, structureTemplate));
    82       Parameters.Add(new ValueParameter<ISymbolicDataAnalysisGrammar>(GrammarParameterName, grammar));
    8370
    84       //structureTemplate.Template = "f(x)*f(y)+5";
    8571    }
    8672
     
    9783
    9884    private void OnTemplateChanged(object sender, EventArgs args) {
     85      SetupStructureTemplate();
     86    }
     87
     88    private void SetupStructureTemplate() {
    9989      foreach (var e in Encoding.Encodings.ToArray())
    10090        Encoding.Remove(e);
    10191
    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));
    10496      }
    10597    }
     
    122114      else
    123115        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       } else
    131         results.Add(new Result("Tree", new ItemList<ISymbolicExpressionTree>(individuals.Select(x => (BuildTree(x))))));
    132       */
    133116    }
    134117
     
    152135        if (n.Symbol is SubFunctionSymbol) {
    153136          var subFunctionTreeNode = n as SubFunctionTreeNode;
    154           var subFunctionTree = individual.SymbolicExpressionTree(subFunctionTreeNode.SubFunction.Name);
     137          var subFunctionTree = individual.SymbolicExpressionTree(subFunctionTreeNode.Name);
    155138          var parent = n.Parent;
    156139
     
    167150    }
    168151
     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
    169185    public void Load(RegressionProblemData data) {
    170186      ProblemData = data;
     187      SetupStructureTemplate();
    171188    }
    172189  }
Note: See TracChangeset for help on using the changeset viewer.