Changeset 17850


Ignore:
Timestamp:
02/26/21 09:06:10 (5 months ago)
Author:
gkronber
Message:

#3106 fixed a few bugs and implemented method to create an expression tree

Location:
branches/3106_AnalyticContinuedFractionsRegression
Files:
1 deleted
3 edited

Legend:

Unmodified
Added
Removed
  • branches/3106_AnalyticContinuedFractionsRegression/HeuristicLab.Algorithms.DataAnalysis/3.4/ContinuedFractionRegression/Problem.cs

    r17849 r17850  
    99using HeuristicLab.Encodings.BinaryVectorEncoding;
    1010using HeuristicLab.Encodings.RealVectorEncoding;
     11using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    1112using HeuristicLab.Optimization;
    1213using HeuristicLab.Parameters;
    1314using HeuristicLab.Problems.DataAnalysis;
     15using HeuristicLab.Problems.DataAnalysis.Symbolic;
    1416using HeuristicLab.Problems.Instances;
    1517
     
    1820  [StorableType("CAC0F743-8524-436C-B81E-7C628A302DF8")]
    1921  [Creatable(CreatableAttribute.Categories.DataAnalysisRegression, Priority = 999)]
    20   public class Problem : HeuristicLab.Optimization.SingleObjectiveBasicProblem<MultiEncoding> 
     22  public class Problem : HeuristicLab.Optimization.SingleObjectiveBasicProblem<MultiEncoding>
    2123    /*, IProblemInstanceConsumer<IRegressionProblemData>, IProblemInstanceExporter<IRegressionProblemData>*/  // only if we can change the code to work with a single dataset
    2224    {
     
    121123      Parameters.Add(new FixedValueParameter<DoubleValue>("L1L2_mixing", "TODO Description", new DoubleValue(0.2)));
    122124      Parameters.Add(new FixedValueParameter<DoubleValue>("L1L2_weight", "TODO Description", new DoubleValue(1)));
    123       Parameters.Add(new FixedValueParameter<DoubleValue>("penality", "TODO Description", new DoubleValue(0.01)));
     125      Parameters.Add(new FixedValueParameter<DoubleValue>("penalty", "TODO Description", new DoubleValue(0.01)));
    124126      Parameters.Add(new FixedValueParameter<IntValue>("fraction_depth", "TODO Description", new IntValue(4)));
    125127      Parameters.Add(new ValueParameter<Dataset>("dataset", "TODO Description", new Dataset()));
     
    151153      TrainingTestDatasetParameter.ValueChanged += DatasetParameter_ValueChanged;
    152154      Phi0_08DatasetParameter.ValueChanged += DatasetParameter_ValueChanged;
     155
     156      if (Parameters.ContainsKey("penality")) {
     157        Parameters.Remove("penality");
     158        Parameters.Add(new FixedValueParameter<DoubleValue>("penalty", "TODO Description", new DoubleValue(0.01)));
     159      }
    153160    }
    154161
     
    176183
    177184    public override double Evaluate(Individual individual, IRandom random) {
    178       if(dataMatrix == null) {
     185      if (dataMatrix == null) {
    179186        InitializeTransformedDatasets();
    180187      }
     
    277284
    278285    public override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) {
    279       // Use vars.yourVariable to access variables in the variable store i.e. yourVariable
    280       // Write or update results given the range of vectors and resulting qualities
    281 
    282286      Individual bestIndividual = null;
    283287      double bestQuality = double.MaxValue;
     
    290294          theBest = i;
    291295        }
     296      }
     297
     298      if (dataMatrix == null) {
     299        InitializeTransformedDatasets();
    292300      }
    293301
     
    316324
    317325      #region set dataTables     
    318       var datatable = (DataTable)results["datatable"].Value;
    319       var coefficients = (DataTable)results["coefficients"].Value;
    320       var pearsonR2 = (DataTable)results["personR2"].Value;
     326      var datatable = (DataTable)results["Datatable"].Value;
     327      var coefficients = (DataTable)results["Coefficients"].Value;
     328      var pearsonR2 = (DataTable)results["Pearson R2"].Value;
    321329
    322330      datatable.Rows["MSE"].Values.Add(calculateRegMSE(((DoubleValue)bestIndividual["LS"]).Value, binar, 0.0));
     
    408416      #endregion
    409417      #endregion
     418
     419      results.AddOrUpdateResult("Model", CreateSymbolicExpressionTree(binar, coeff, TrainingTrainingDataset.VariableNames.ToArray()));
     420    }
     421
     422    private ISymbolicExpressionTree CreateSymbolicExpressionTree(bool[] binar, double[] coeff, string[] varNames) {
     423      bool firstCoeffUnequalZero = false;
     424      var d = varNames.Length;
     425      var expression = constSy.CreateTreeNode(); // zero
     426
     427      for (var i = coeff.Length - 1; i > 0; i = i - 2 * d) {
     428
     429        if ((linearFunctionEqualZero(coeff, binar, i - d + 1, i) == false)) {
     430          firstCoeffUnequalZero = true;
     431          var sum = addSy.CreateTreeNode();
     432          sum.AddSubtree(createLinearExpression(coeff, binar, i - d + 1, i, varNames));
     433          sum.AddSubtree(expression);
     434          expression = sum;
     435        }
     436
     437        if (firstCoeffUnequalZero == true && i > 2 * d) { // don't take the first coeffVecPart and don't take the last coeffVecPart (both are only to add)
     438          if (linearFunctionEqualZero(coeff, binar, i - 2 * d + 1, i - d) == false) {
     439            /* div-by-zero cannot be determined for the expression
     440            if (valueNearZero(value) == true)  // no division by zero
     441              return double.MaxValue;
     442            else {
     443            */
     444              var frac = divSy.CreateTreeNode();
     445              frac.AddSubtree(createLinearExpression(coeff, binar, i - 2 * d + 1, i - d, varNames));
     446              frac.AddSubtree(expression);
     447              expression = frac;
     448            /* } */
     449          } else {
     450            // don't allow coeffVecParts in middle to be equal zero
     451            var errorConst = (ConstantTreeNode)constSy.CreateTreeNode();
     452            errorConst.Value = double.MaxValue;
     453            expression = errorConst;
     454          }
     455        }
     456      }
     457
     458      var startNode = startSy.CreateTreeNode();
     459      var progRootNode = progRootSy.CreateTreeNode();
     460      startNode.AddSubtree(progRootNode);
     461      progRootNode.AddSubtree(expression);
     462      return new SymbolicExpressionTree(progRootNode) ;
    410463    }
    411464
     
    444497      #region curves 0.001
    445498      ScatterPlot curves0_001 = new ScatterPlot("Curves0_001", "Kurven mit Phi'=0.001");
    446       readCurve(curves0_001, "data6082_350_0_001", "Temp350", System.Drawing.Color.Blue);
    447       readCurve(curves0_001, "data6082_375_0_001", "Temp375", System.Drawing.Color.Orange);
    448       readCurve(curves0_001, "data6082_400_0_001", "Temp400", System.Drawing.Color.Red);
    449       readCurve(curves0_001, "data6082_425_0_001", "Temp425", System.Drawing.Color.Green);
    450       readCurve(curves0_001, "data6082_450_0_001", "Temp450", System.Drawing.Color.Gray);
    451       readCurve(curves0_001, "data6082_475_0_001", "Temp475", System.Drawing.Color.Olive);
    452       readCurve(curves0_001, "data6082_500_0_001", "Temp500", System.Drawing.Color.Gold);
     499      readCurve(curves0_001, "data_350_0_001", "Temp350", System.Drawing.Color.Blue);
     500      readCurve(curves0_001, "data_375_0_001", "Temp375", System.Drawing.Color.Orange);
     501      readCurve(curves0_001, "data_400_0_001", "Temp400", System.Drawing.Color.Red);
     502      readCurve(curves0_001, "data_425_0_001", "Temp425", System.Drawing.Color.Green);
     503      readCurve(curves0_001, "data_450_0_001", "Temp450", System.Drawing.Color.Gray);
     504      readCurve(curves0_001, "data_475_0_001", "Temp475", System.Drawing.Color.Olive);
     505      readCurve(curves0_001, "data_500_0_001", "Temp500", System.Drawing.Color.Gold);
    453506
    454507      var empty0_001 = new Point2D<double>[0];
     
    487540      Temp500Convergence0_001.VisualProperties.PointSize = 2;
    488541      curves0_001.Rows.Add(Temp500Convergence0_001);
    489       results.Add(new Result("Curves0_001", curves0_001));
     542      results.AddOrUpdateResult("Curves0_001", curves0_001);
    490543
    491544      #endregion
    492545      #region curves 0.01
    493546      ScatterPlot curves0_01 = new ScatterPlot("Curves0_01", "Kurven mit Phi'=0.01");
    494       readCurve(curves0_01, "data6082_350_0_01", "Temp350", System.Drawing.Color.Blue);
    495       readCurve(curves0_01, "data6082_375_0_01", "Temp375", System.Drawing.Color.Orange);
    496       readCurve(curves0_01, "data6082_400_0_01", "Temp400", System.Drawing.Color.Red);
    497       readCurve(curves0_01, "data6082_425_0_01", "Temp425", System.Drawing.Color.Green);
    498       readCurve(curves0_01, "data6082_450_0_01", "Temp450", System.Drawing.Color.Gray);
    499       readCurve(curves0_01, "data6082_475_0_01", "Temp475", System.Drawing.Color.Olive);
    500       readCurve(curves0_01, "data6082_500_0_01", "Temp500", System.Drawing.Color.Gold);
     547      readCurve(curves0_01, "data_350_0_01", "Temp350", System.Drawing.Color.Blue);
     548      readCurve(curves0_01, "data_375_0_01", "Temp375", System.Drawing.Color.Orange);
     549      readCurve(curves0_01, "data_400_0_01", "Temp400", System.Drawing.Color.Red);
     550      readCurve(curves0_01, "data_425_0_01", "Temp425", System.Drawing.Color.Green);
     551      readCurve(curves0_01, "data_450_0_01", "Temp450", System.Drawing.Color.Gray);
     552      readCurve(curves0_01, "data_475_0_01", "Temp475", System.Drawing.Color.Olive);
     553      readCurve(curves0_01, "data_500_0_01", "Temp500", System.Drawing.Color.Gold);
    501554
    502555      var empty0_01 = new Point2D<double>[0];
     
    535588      Temp500Convergence0_01.VisualProperties.PointSize = 2;
    536589      curves0_01.Rows.Add(Temp500Convergence0_01);
    537       results.Add(new Result("Curves0_01", curves0_01));
     590      results.AddOrUpdateResult("Curves0_01", curves0_01);
    538591
    539592      #endregion
    540593      #region curves 0.1
    541594      ScatterPlot curves0_1 = new ScatterPlot("Curves0_1", "Kurven mit Phi'=0.1");
    542       readCurve(curves0_1, "data6082_350_0_1", "Temp350", System.Drawing.Color.Blue);
    543       readCurve(curves0_1, "data6082_375_0_1", "Temp375", System.Drawing.Color.Orange);
    544       readCurve(curves0_1, "data6082_400_0_1", "Temp400", System.Drawing.Color.Red);
    545       readCurve(curves0_1, "data6082_425_0_1", "Temp425", System.Drawing.Color.Green);
    546       readCurve(curves0_1, "data6082_450_0_1", "Temp450", System.Drawing.Color.Gray);
    547       readCurve(curves0_1, "data6082_475_0_1", "Temp475", System.Drawing.Color.Olive);
    548       readCurve(curves0_1, "data6082_500_0_1", "Temp500", System.Drawing.Color.Gold);
     595      readCurve(curves0_1, "data_350_0_1", "Temp350", System.Drawing.Color.Blue);
     596      readCurve(curves0_1, "data_375_0_1", "Temp375", System.Drawing.Color.Orange);
     597      readCurve(curves0_1, "data_400_0_1", "Temp400", System.Drawing.Color.Red);
     598      readCurve(curves0_1, "data_425_0_1", "Temp425", System.Drawing.Color.Green);
     599      readCurve(curves0_1, "data_450_0_1", "Temp450", System.Drawing.Color.Gray);
     600      readCurve(curves0_1, "data_475_0_1", "Temp475", System.Drawing.Color.Olive);
     601      readCurve(curves0_1, "data_500_0_1", "Temp500", System.Drawing.Color.Gold);
    549602
    550603      var empty0_1 = new Point2D<double>[0];
     
    583636      Temp500Convergence0_1.VisualProperties.PointSize = 2;
    584637      curves0_1.Rows.Add(Temp500Convergence0_1);
    585       results.Add(new Result("Curves0_1", curves0_1));
     638      results.AddOrUpdateResult("Curves0_1", curves0_1);
    586639      #endregion
    587640      #region curves 1
    588641      ScatterPlot curves1 = new ScatterPlot("Curves1", "Kurven mit Phi'=1");
    589       readCurve(curves1, "data6082_350_1", "Temp350", System.Drawing.Color.Blue);
    590       readCurve(curves1, "data6082_375_1", "Temp375", System.Drawing.Color.Orange);
    591       readCurve(curves1, "data6082_400_1", "Temp400", System.Drawing.Color.Red);
    592       readCurve(curves1, "data6082_425_1", "Temp425", System.Drawing.Color.Green);
    593       readCurve(curves1, "data6082_450_1", "Temp450", System.Drawing.Color.Gray);
    594       readCurve(curves1, "data6082_475_1", "Temp475", System.Drawing.Color.Olive);
    595       readCurve(curves1, "data6082_500_1", "Temp500", System.Drawing.Color.Gold);
     642      readCurve(curves1, "data_350_1", "Temp350", System.Drawing.Color.Blue);
     643      readCurve(curves1, "data_375_1", "Temp375", System.Drawing.Color.Orange);
     644      readCurve(curves1, "data_400_1", "Temp400", System.Drawing.Color.Red);
     645      readCurve(curves1, "data_425_1", "Temp425", System.Drawing.Color.Green);
     646      readCurve(curves1, "data_450_1", "Temp450", System.Drawing.Color.Gray);
     647      readCurve(curves1, "data_475_1", "Temp475", System.Drawing.Color.Olive);
     648      readCurve(curves1, "data_500_1", "Temp500", System.Drawing.Color.Gold);
    596649
    597650      var empty1 = new Point2D<double>[0];
     
    630683      Temp500Convergence1.VisualProperties.PointSize = 2;
    631684      curves1.Rows.Add(Temp500Convergence1);
    632       results.Add(new Result("Curves1", curves1));
     685      results.AddOrUpdateResult("Curves1", curves1);
    633686      #endregion
    634687    }
     
    732785    }
    733786
     787    // symbols to be used in expression trees
     788    private static Addition addSy = new Addition();
     789    private static Division divSy = new Division();
     790    private static HeuristicLab.Problems.DataAnalysis.Symbolic.Variable varSy = new Problems.DataAnalysis.Symbolic.Variable();
     791    private static Constant constSy = new Constant();
     792    private static StartSymbol startSy = new StartSymbol();
     793    private static ProgramRootSymbol progRootSy = new ProgramRootSymbol();
     794    private ISymbolicExpressionTreeNode createLinearExpression(double[] coeff, bool[] binar, int start, int end, string[] varNames) {
     795      var sum = addSy.CreateTreeNode();
     796
     797      for (int i = start; i < end; i++) {
     798        if (binar[i] == true) {
     799          var varNode = (VariableTreeNode)varSy.CreateTreeNode();
     800          varNode.VariableName = varNames[i - start];
     801          varNode.Weight = coeff[i];
     802          sum.AddSubtree(varNode);
     803        }
     804      }
     805      if (binar[end] == true) {
     806        var constNode = (ConstantTreeNode)constSy.CreateTreeNode();
     807        constNode.Value = coeff[end];
     808        sum.AddSubtree(constNode);
     809      }
     810
     811      //doubleThrowInvOrNaN(value, "evaluateLinearFunctionValue");
     812      return sum;
     813    }
     814
    734815    private bool valueNearZero(double value) {
    735816      if (Math.Abs(value) < epsilon)
     
    755836
    756837    private double calculateRegMSE(double quality, bool[] binar, double penalty) {
    757       return (quality / dataMatrix.Rows) * (1 + penalty * (double)numVariablesUsed(binar));
     838      return (quality / dataMatrix.Rows) * (1 + penalty * numVariablesUsed(binar));
    758839    }
    759840
  • branches/3106_AnalyticContinuedFractionsRegression/HeuristicLab.Algorithms.DataAnalysis/3.4/HeuristicLab.Algorithms.DataAnalysis-3.4.csproj

    r17848 r17850  
    134134    <Compile Include="ContinuedFractionRegression\RegressionResult.cs" />
    135135    <Compile Include="ContinuedFractionRegression\SimplexConstant.cs" />
    136     <Compile Include="ContinuedFractionRegression\Tests.cs" />
    137136    <Compile Include="ContinuedFractionRegression\Transformation.cs" />
    138137    <Compile Include="ContinuedFractionRegression\Vector.cs" />
  • branches/3106_AnalyticContinuedFractionsRegression/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic.Views-3.4.csproj

    r17848 r17850  
    115115  <ItemGroup>
    116116    <Compile Include="Exporters\SymbolicSolutionExcelExporter.cs" />
    117     <Compile Include="InteractiveSymbolicExpressionTreeChart.cs" />
     117    <Compile Include="InteractiveSymbolicExpressionTreeChart.cs">
     118      <SubType>UserControl</SubType>
     119    </Compile>
    118120    <Compile Include="InteractiveSymbolicExpressionTreeChart.Designer.cs">
    119121      <DependentUpon>InteractiveSymbolicExpressionTreeChart.cs</DependentUpon>
     
    144146      <DependentUpon>GraphicalSymbolicDataAnalysisModelView.cs</DependentUpon>
    145147    </Compile>
    146     <Compile Include="Symbols\ConstantView.cs" />
     148    <Compile Include="Symbols\ConstantView.cs">
     149      <SubType>UserControl</SubType>
     150    </Compile>
    147151    <Compile Include="Symbols\ConstantView.Designer.cs">
    148152      <DependentUpon>ConstantView.cs</DependentUpon>
    149153    </Compile>
    150     <Compile Include="Symbols\LaggedVariableView.cs" />
     154    <Compile Include="Symbols\LaggedVariableView.cs">
     155      <SubType>UserControl</SubType>
     156    </Compile>
    151157    <Compile Include="Symbols\LaggedVariableView.Designer.cs">
    152158      <DependentUpon>LaggedVariableView.cs</DependentUpon>
    153159    </Compile>
    154     <Compile Include="Symbols\TimeLagView.cs" />
     160    <Compile Include="Symbols\TimeLagView.cs">
     161      <SubType>UserControl</SubType>
     162    </Compile>
    155163    <Compile Include="Symbols\TimeLagView.Designer.cs">
    156164      <DependentUpon>TimeLagView.cs</DependentUpon>
    157165    </Compile>
    158     <Compile Include="Symbols\VariableConditionView.cs" />
     166    <Compile Include="Symbols\VariableConditionView.cs">
     167      <SubType>UserControl</SubType>
     168    </Compile>
    159169    <Compile Include="Symbols\VariableConditionView.Designer.cs">
    160170      <DependentUpon>VariableConditionView.cs</DependentUpon>
    161171    </Compile>
    162     <Compile Include="Symbols\VariableView.cs" />
     172    <Compile Include="Symbols\VariableView.cs">
     173      <SubType>UserControl</SubType>
     174    </Compile>
    163175    <Compile Include="Symbols\VariableView.Designer.cs">
    164176      <DependentUpon>VariableView.cs</DependentUpon>
Note: See TracChangeset for help on using the changeset viewer.