Changeset 3532


Ignore:
Timestamp:
04/26/10 14:21:03 (12 years ago)
Author:
gkronber
Message:

Implemented linear scaling. #938 (Data types and operators for regression problems)

Location:
trunk/sources/HeuristicLab.Problems.DataAnalysis.Regression/3.3
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Regression/3.3/HeuristicLab.Problems.DataAnalysis.Regression-3.3.csproj

    r3531 r3532  
    8787    <Compile Include="Properties\AssemblyInfo.cs" />
    8888    <Compile Include="Symbolic\BestValidationSymbolicRegressionSolutionVisualizer.cs" />
     89    <Compile Include="Symbolic\SymbolicRegressionScaledMeanSquaredErrorEvaluator.cs" />
    8990    <Compile Include="Symbolic\SymbolicRegressionSolution.cs" />
    9091    <Compile Include="Symbolic\SymbolicRegressionModel.cs" />
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/BestValidationSymbolicRegressionSolutionVisualizer.cs

    r3531 r3532  
    3434using HeuristicLab.Analysis;
    3535
     36using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols;
     37
    3638namespace HeuristicLab.Problems.DataAnalysis.Regression.Symbolic {
    3739  /// <summary>
     
    4446    private const string UpperEstimationLimitParameterName = "UpperEstimationLimit";
    4547    private const string LowerEstimationLimitParameterName = "LowerEstimationLimit";
     48    private const string AlphaParameterName = "Alpha";
     49    private const string BetaParameterName = "Beta";
    4650    private const string SymbolicRegressionModelParameterName = "SymbolicRegressionModel";
    4751    private const string DataAnalysisProblemDataParameterName = "DataAnalysisProblemData";
     
    7377      get { return (ILookupParameter<ItemArray<SymbolicExpressionTree>>)Parameters[SymbolicRegressionModelParameterName]; }
    7478    }
     79    public ILookupParameter<ItemArray<DoubleValue>> AlphaParameter {
     80      get { return (ILookupParameter<ItemArray<DoubleValue>>)Parameters[AlphaParameterName]; }
     81    }
     82    public ILookupParameter<ItemArray<DoubleValue>> BetaParameter {
     83      get { return (ILookupParameter<ItemArray<DoubleValue>>)Parameters[BetaParameterName]; }
     84    }
    7585    public ILookupParameter<DataAnalysisProblemData> DataAnalysisProblemDataParameter {
    7686      get { return (ILookupParameter<DataAnalysisProblemData>)Parameters[DataAnalysisProblemDataParameterName]; }
     
    124134      Parameters.Add(new LookupParameter<DataAnalysisProblemData>(DataAnalysisProblemDataParameterName, "The symbolic regression problme data on which the best solution should be evaluated."));
    125135      Parameters.Add(new LookupParameter<ISymbolicExpressionTreeInterpreter>(SymbolicExpressionTreeInterpreterParameterName, "The interpreter that should be used to calculate the output values of symbolic expression trees."));
     136      Parameters.Add(new SubScopesLookupParameter<DoubleValue>(AlphaParameterName, "Alpha parameter for linear scaling of the estimated values."));
     137      Parameters.Add(new SubScopesLookupParameter<DoubleValue>(BetaParameterName, "Beta parameter for linear scaling ot the estimated values."));
    126138      Parameters.Add(new ValueLookupParameter<DoubleValue>(UpperEstimationLimitParameterName, "The upper limit that should be used as cut off value for the output values of symbolic expression trees."));
    127139      Parameters.Add(new ValueLookupParameter<DoubleValue>(LowerEstimationLimitParameterName, "The lower limit that should be used as cut off value for the output values of symbolic expression trees."));
     
    135147    public override IOperation Apply() {
    136148      ItemArray<SymbolicExpressionTree> expressions = SymbolicExpressionTreeParameter.ActualValue;
     149      ItemArray<DoubleValue> alphas = AlphaParameter.ActualValue;
     150      ItemArray<DoubleValue> betas = BetaParameter.ActualValue;
     151      var scaledExpressions = from i in Enumerable.Range(0, expressions.Count())
     152                              let expr = expressions[i]
     153                              let alpha = alphas[i].Value
     154                              let beta = betas[i].Value
     155                              select new { Expression = expr, Alpha = alpha, Beta = beta };
    137156      DataAnalysisProblemData problemData = DataAnalysisProblemDataParameter.ActualValue;
    138157      #region update variable frequencies
     
    156175      double upperEstimationLimit = UpperEstimationLimit.Value;
    157176      double lowerEstimationLimit = LowerEstimationLimit.Value;
    158       var currentBestExpression = (from expression in expressions
     177      var currentBestExpression = (from expression in scaledExpressions
    159178                                   let validationQuality =
    160                                      SymbolicRegressionMeanSquaredErrorEvaluator.Calculate(
    161                                        SymbolicExpressionTreeInterpreter, expression,
     179                                     SymbolicRegressionScaledMeanSquaredErrorEvaluator.CalculateWithScaling(
     180                                       SymbolicExpressionTreeInterpreter, expression.Expression,
    162181                                       lowerEstimationLimit, upperEstimationLimit,
    163182                                       problemData.Dataset, problemData.TargetVariable.Value,
    164                                        validationSamplesStart, validationSamplesEnd)
     183                                       validationSamplesStart, validationSamplesEnd,
     184                                       expression.Beta, expression.Alpha)
    165185                                   select new { Expression = expression, ValidationQuality = validationQuality })
    166186                                   .OrderBy(x => x.ValidationQuality)
     
    172192      if (bestOfRunSolution == null) {
    173193        // no best of run solution yet -> make a solution from the currentBestExpression
    174         UpdateBestOfRunSolution(problemData, currentBestExpression.Expression, SymbolicExpressionTreeInterpreter, lowerEstimationLimit, upperEstimationLimit);
     194        UpdateBestOfRunSolution(problemData, currentBestExpression.Expression.Expression, SymbolicExpressionTreeInterpreter, lowerEstimationLimit, upperEstimationLimit, currentBestExpression.Expression.Alpha, currentBestExpression.Expression.Beta);
    175195      } else {
    176196        // compare quality of current best with best of run solution
     
    178198        var bestOfRunValidationQuality = SimpleMSEEvaluator.Calculate(validationValues, estimatedValidationValues);
    179199        if (bestOfRunValidationQuality > currentBestExpression.ValidationQuality) {
    180           UpdateBestOfRunSolution(problemData, currentBestExpression.Expression, SymbolicExpressionTreeInterpreter, lowerEstimationLimit, upperEstimationLimit);
     200          UpdateBestOfRunSolution(problemData, currentBestExpression.Expression.Expression, SymbolicExpressionTreeInterpreter, lowerEstimationLimit, upperEstimationLimit, currentBestExpression.Expression.Alpha, currentBestExpression.Expression.Beta);
    181201        }
    182202      }
     
    186206
    187207    private void UpdateBestOfRunSolution(DataAnalysisProblemData problemData, SymbolicExpressionTree tree, ISymbolicExpressionTreeInterpreter interpreter,
    188       double lowerEstimationLimit, double upperEstimationLimit) {
    189       var newBestSolution = CreateDataAnalysisSolution(problemData, tree, interpreter, lowerEstimationLimit, upperEstimationLimit);
     208      double lowerEstimationLimit, double upperEstimationLimit,
     209      double alpha, double beta) {
     210      var newBestSolution = CreateDataAnalysisSolution(problemData, tree, interpreter, lowerEstimationLimit, upperEstimationLimit, alpha, beta);
    190211      if (BestValidationSolutionParameter.ActualValue == null)
    191212        BestValidationSolutionParameter.ActualValue = newBestSolution;
     
    215236    }
    216237
    217     private SymbolicRegressionSolution CreateDataAnalysisSolution(DataAnalysisProblemData problemData, SymbolicExpressionTree expression, ISymbolicExpressionTreeInterpreter interpreter,
    218       double lowerEstimationLimit, double upperEstimationLimit) {
    219       var model = new SymbolicRegressionModel(interpreter, expression, problemData.InputVariables.Select(s => s.Value));
     238    private SymbolicRegressionSolution CreateDataAnalysisSolution(DataAnalysisProblemData problemData, SymbolicExpressionTree tree, ISymbolicExpressionTreeInterpreter interpreter,
     239      double lowerEstimationLimit, double upperEstimationLimit,
     240      double alpha, double beta) {
     241      var mainBranch = tree.Root.SubTrees[0].SubTrees[0];
     242      var scaledMainBranch = MakeSum(MakeProduct(beta, mainBranch), alpha);
     243
     244      // remove the main branch before cloning to prevent cloning of sub-trees
     245      tree.Root.SubTrees[0].RemoveSubTree(0);
     246      var scaledTree = (SymbolicExpressionTree)tree.Clone();
     247      // insert main branch into the original tree again
     248      tree.Root.SubTrees[0].InsertSubTree(0, mainBranch);
     249      // insert the scaled main branch into the cloned tree
     250      scaledTree.Root.SubTrees[0].InsertSubTree(0, scaledMainBranch);
     251      // create a new solution using the scaled tree
     252      var model = new SymbolicRegressionModel(interpreter, scaledTree, problemData.InputVariables.Select(s => s.Value));
    220253      return new SymbolicRegressionSolution(problemData, model, lowerEstimationLimit, upperEstimationLimit);
     254    }
     255
     256    private SymbolicExpressionTreeNode MakeSum(SymbolicExpressionTreeNode treeNode, double alpha) {
     257      var node = (new Addition()).CreateTreeNode();
     258      var alphaConst = MakeConstant(alpha);
     259      node.AddSubTree(treeNode);
     260      node.AddSubTree(alphaConst);
     261      return node;
     262    }
     263
     264    private SymbolicExpressionTreeNode MakeProduct(double beta, SymbolicExpressionTreeNode treeNode) {
     265      var node = (new Multiplication()).CreateTreeNode();
     266      var betaConst = MakeConstant(beta);
     267      node.AddSubTree(treeNode);
     268      node.AddSubTree(betaConst);
     269      return node;
     270    }
     271
     272    private SymbolicExpressionTreeNode MakeConstant(double c) {
     273      var node = (ConstantTreeNode)(new Constant()).CreateTreeNode();
     274      node.Value = c;
     275      return node;
    221276    }
    222277  }
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/SymbolicRegressionProblem.cs

    r3528 r3532  
    111111      set { MaxExpressionDepthParameter.Value = value; }
    112112    }
     113    public IntValue MaxFunctionDefiningBranches {
     114      get { return MaxFunctionDefiningBranchesParameter.Value; }
     115      set { MaxFunctionDefiningBranchesParameter.Value = value; }
     116    }
     117    public IntValue MaxFunctionArguments {
     118      get { return MaxFunctionArgumentsParameter.Value; }
     119      set { MaxFunctionArgumentsParameter.Value = value; }
     120    }
    113121    public SymbolicExpressionTreeCreator SolutionCreator {
    114122      get { return SolutionCreatorParameter.Value; }
     
    166174      : base() {
    167175      SymbolicExpressionTreeCreator creator = new ProbabilisticTreeCreator();
    168       var evaluator = new SymbolicRegressionMeanSquaredErrorEvaluator();
     176      var evaluator = new SymbolicRegressionScaledMeanSquaredErrorEvaluator();
    169177      var grammar = new ArithmeticExpressionGrammar();
    170178      var globalGrammar = new GlobalSymbolicExpressionGrammar(grammar);
     
    190198      DataAnalysisProblemDataParameter.ValueChanged += new EventHandler(DataAnalysisProblemDataParameter_ValueChanged);
    191199      DataAnalysisProblemData.ProblemDataChanged += new EventHandler(DataAnalysisProblemData_Changed);
     200      MaxFunctionArgumentsParameter.ValueChanged += new EventHandler(ArchitectureParameter_Changed);
     201      MaxFunctionArgumentsParameter.Value.ValueChanged += new EventHandler(ArchitectureParameter_Changed);
     202      MaxFunctionDefiningBranchesParameter.ValueChanged += new EventHandler(ArchitectureParameter_Changed);
     203      MaxFunctionDefiningBranchesParameter.Value.ValueChanged += new EventHandler(ArchitectureParameter_Changed);
    192204      ParameterizeSolutionCreator();
    193205      ParameterizeEvaluator();
     
    196208      Initialize();
    197209    }
     210
    198211
    199212    [StorableConstructor]
     
    214227      UpdateGrammar();
    215228      UpdatePartitioningParameters();
     229    }
     230
     231    void ArchitectureParameter_Changed(object sender, EventArgs e) {
     232      var globalGrammar = FunctionTreeGrammar as GlobalSymbolicExpressionGrammar;
     233      globalGrammar.MaxFunctionArguments = MaxFunctionArguments.Value;
     234      globalGrammar.MaxFunctionDefinitions = MaxFunctionDefiningBranches.Value;
    216235    }
    217236
Note: See TracChangeset for help on using the changeset viewer.