Changeset 17850 for branches/3106_AnalyticContinuedFractionsRegression/HeuristicLab.Algorithms.DataAnalysis
- Timestamp:
- 02/26/21 09:06:10 (4 years ago)
- Location:
- branches/3106_AnalyticContinuedFractionsRegression/HeuristicLab.Algorithms.DataAnalysis/3.4
- Files:
-
- 1 deleted
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3106_AnalyticContinuedFractionsRegression/HeuristicLab.Algorithms.DataAnalysis/3.4/ContinuedFractionRegression/Problem.cs
r17849 r17850 9 9 using HeuristicLab.Encodings.BinaryVectorEncoding; 10 10 using HeuristicLab.Encodings.RealVectorEncoding; 11 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 11 12 using HeuristicLab.Optimization; 12 13 using HeuristicLab.Parameters; 13 14 using HeuristicLab.Problems.DataAnalysis; 15 using HeuristicLab.Problems.DataAnalysis.Symbolic; 14 16 using HeuristicLab.Problems.Instances; 15 17 … … 18 20 [StorableType("CAC0F743-8524-436C-B81E-7C628A302DF8")] 19 21 [Creatable(CreatableAttribute.Categories.DataAnalysisRegression, Priority = 999)] 20 public class Problem : HeuristicLab.Optimization.SingleObjectiveBasicProblem<MultiEncoding> 22 public class Problem : HeuristicLab.Optimization.SingleObjectiveBasicProblem<MultiEncoding> 21 23 /*, IProblemInstanceConsumer<IRegressionProblemData>, IProblemInstanceExporter<IRegressionProblemData>*/ // only if we can change the code to work with a single dataset 22 24 { … … 121 123 Parameters.Add(new FixedValueParameter<DoubleValue>("L1L2_mixing", "TODO Description", new DoubleValue(0.2))); 122 124 Parameters.Add(new FixedValueParameter<DoubleValue>("L1L2_weight", "TODO Description", new DoubleValue(1))); 123 Parameters.Add(new FixedValueParameter<DoubleValue>("penal ity", "TODO Description", new DoubleValue(0.01)));125 Parameters.Add(new FixedValueParameter<DoubleValue>("penalty", "TODO Description", new DoubleValue(0.01))); 124 126 Parameters.Add(new FixedValueParameter<IntValue>("fraction_depth", "TODO Description", new IntValue(4))); 125 127 Parameters.Add(new ValueParameter<Dataset>("dataset", "TODO Description", new Dataset())); … … 151 153 TrainingTestDatasetParameter.ValueChanged += DatasetParameter_ValueChanged; 152 154 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 } 153 160 } 154 161 … … 176 183 177 184 public override double Evaluate(Individual individual, IRandom random) { 178 if (dataMatrix == null) {185 if (dataMatrix == null) { 179 186 InitializeTransformedDatasets(); 180 187 } … … 277 284 278 285 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. yourVariable280 // Write or update results given the range of vectors and resulting qualities281 282 286 Individual bestIndividual = null; 283 287 double bestQuality = double.MaxValue; … … 290 294 theBest = i; 291 295 } 296 } 297 298 if (dataMatrix == null) { 299 InitializeTransformedDatasets(); 292 300 } 293 301 … … 316 324 317 325 #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; 321 329 322 330 datatable.Rows["MSE"].Values.Add(calculateRegMSE(((DoubleValue)bestIndividual["LS"]).Value, binar, 0.0)); … … 408 416 #endregion 409 417 #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) ; 410 463 } 411 464 … … 444 497 #region curves 0.001 445 498 ScatterPlot curves0_001 = new ScatterPlot("Curves0_001", "Kurven mit Phi'=0.001"); 446 readCurve(curves0_001, "data 6082_350_0_001", "Temp350", System.Drawing.Color.Blue);447 readCurve(curves0_001, "data 6082_375_0_001", "Temp375", System.Drawing.Color.Orange);448 readCurve(curves0_001, "data 6082_400_0_001", "Temp400", System.Drawing.Color.Red);449 readCurve(curves0_001, "data 6082_425_0_001", "Temp425", System.Drawing.Color.Green);450 readCurve(curves0_001, "data 6082_450_0_001", "Temp450", System.Drawing.Color.Gray);451 readCurve(curves0_001, "data 6082_475_0_001", "Temp475", System.Drawing.Color.Olive);452 readCurve(curves0_001, "data 6082_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); 453 506 454 507 var empty0_001 = new Point2D<double>[0]; … … 487 540 Temp500Convergence0_001.VisualProperties.PointSize = 2; 488 541 curves0_001.Rows.Add(Temp500Convergence0_001); 489 results.Add (new Result("Curves0_001", curves0_001));542 results.AddOrUpdateResult("Curves0_001", curves0_001); 490 543 491 544 #endregion 492 545 #region curves 0.01 493 546 ScatterPlot curves0_01 = new ScatterPlot("Curves0_01", "Kurven mit Phi'=0.01"); 494 readCurve(curves0_01, "data 6082_350_0_01", "Temp350", System.Drawing.Color.Blue);495 readCurve(curves0_01, "data 6082_375_0_01", "Temp375", System.Drawing.Color.Orange);496 readCurve(curves0_01, "data 6082_400_0_01", "Temp400", System.Drawing.Color.Red);497 readCurve(curves0_01, "data 6082_425_0_01", "Temp425", System.Drawing.Color.Green);498 readCurve(curves0_01, "data 6082_450_0_01", "Temp450", System.Drawing.Color.Gray);499 readCurve(curves0_01, "data 6082_475_0_01", "Temp475", System.Drawing.Color.Olive);500 readCurve(curves0_01, "data 6082_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); 501 554 502 555 var empty0_01 = new Point2D<double>[0]; … … 535 588 Temp500Convergence0_01.VisualProperties.PointSize = 2; 536 589 curves0_01.Rows.Add(Temp500Convergence0_01); 537 results.Add (new Result("Curves0_01", curves0_01));590 results.AddOrUpdateResult("Curves0_01", curves0_01); 538 591 539 592 #endregion 540 593 #region curves 0.1 541 594 ScatterPlot curves0_1 = new ScatterPlot("Curves0_1", "Kurven mit Phi'=0.1"); 542 readCurve(curves0_1, "data 6082_350_0_1", "Temp350", System.Drawing.Color.Blue);543 readCurve(curves0_1, "data 6082_375_0_1", "Temp375", System.Drawing.Color.Orange);544 readCurve(curves0_1, "data 6082_400_0_1", "Temp400", System.Drawing.Color.Red);545 readCurve(curves0_1, "data 6082_425_0_1", "Temp425", System.Drawing.Color.Green);546 readCurve(curves0_1, "data 6082_450_0_1", "Temp450", System.Drawing.Color.Gray);547 readCurve(curves0_1, "data 6082_475_0_1", "Temp475", System.Drawing.Color.Olive);548 readCurve(curves0_1, "data 6082_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); 549 602 550 603 var empty0_1 = new Point2D<double>[0]; … … 583 636 Temp500Convergence0_1.VisualProperties.PointSize = 2; 584 637 curves0_1.Rows.Add(Temp500Convergence0_1); 585 results.Add (new Result("Curves0_1", curves0_1));638 results.AddOrUpdateResult("Curves0_1", curves0_1); 586 639 #endregion 587 640 #region curves 1 588 641 ScatterPlot curves1 = new ScatterPlot("Curves1", "Kurven mit Phi'=1"); 589 readCurve(curves1, "data 6082_350_1", "Temp350", System.Drawing.Color.Blue);590 readCurve(curves1, "data 6082_375_1", "Temp375", System.Drawing.Color.Orange);591 readCurve(curves1, "data 6082_400_1", "Temp400", System.Drawing.Color.Red);592 readCurve(curves1, "data 6082_425_1", "Temp425", System.Drawing.Color.Green);593 readCurve(curves1, "data 6082_450_1", "Temp450", System.Drawing.Color.Gray);594 readCurve(curves1, "data 6082_475_1", "Temp475", System.Drawing.Color.Olive);595 readCurve(curves1, "data 6082_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); 596 649 597 650 var empty1 = new Point2D<double>[0]; … … 630 683 Temp500Convergence1.VisualProperties.PointSize = 2; 631 684 curves1.Rows.Add(Temp500Convergence1); 632 results.Add (new Result("Curves1", curves1));685 results.AddOrUpdateResult("Curves1", curves1); 633 686 #endregion 634 687 } … … 732 785 } 733 786 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 734 815 private bool valueNearZero(double value) { 735 816 if (Math.Abs(value) < epsilon) … … 755 836 756 837 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)); 758 839 } 759 840 -
branches/3106_AnalyticContinuedFractionsRegression/HeuristicLab.Algorithms.DataAnalysis/3.4/HeuristicLab.Algorithms.DataAnalysis-3.4.csproj
r17848 r17850 134 134 <Compile Include="ContinuedFractionRegression\RegressionResult.cs" /> 135 135 <Compile Include="ContinuedFractionRegression\SimplexConstant.cs" /> 136 <Compile Include="ContinuedFractionRegression\Tests.cs" />137 136 <Compile Include="ContinuedFractionRegression\Transformation.cs" /> 138 137 <Compile Include="ContinuedFractionRegression\Vector.cs" />
Note: See TracChangeset
for help on using the changeset viewer.