Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3106_AnalyticContinuedFractionsRegression/HeuristicLab.Algorithms.DataAnalysis/3.4/ContinuedFractionRegression/Problem.cs @ 17851

Last change on this file since 17851 was 17851, checked in by gkronber, 3 years ago

#3106 Create solution for best individual of each generation

File size: 46.4 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using HEAL.Attic;
5using HeuristicLab.Analysis;
6using HeuristicLab.Common;
7using HeuristicLab.Core;
8using HeuristicLab.Data;
9using HeuristicLab.Encodings.BinaryVectorEncoding;
10using HeuristicLab.Encodings.RealVectorEncoding;
11using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
12using HeuristicLab.Optimization;
13using HeuristicLab.Parameters;
14using HeuristicLab.Problems.DataAnalysis;
15using HeuristicLab.Problems.DataAnalysis.Symbolic;
16using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
17using HeuristicLab.Problems.Instances;
18
19namespace HeuristicLab.Algorithms.DataAnalysis.ContinuedFractionRegression {
20  [Item("Continued Fraction Regression (CFR)", "Attempts to find a continued fraction minimizing the regularized MSE / R^2 to given data.")]
21  [StorableType("CAC0F743-8524-436C-B81E-7C628A302DF8")]
22  [Creatable(CreatableAttribute.Categories.DataAnalysisRegression, Priority = 999)]
23  public class Problem : HeuristicLab.Optimization.SingleObjectiveBasicProblem<MultiEncoding>
24    /*, IProblemInstanceConsumer<IRegressionProblemData>, IProblemInstanceExporter<IRegressionProblemData>*/  // only if we can change the code to work with a single dataset
25    {
26    private const double epsilon = 1e-6;
27    enum Fitting { MSE, RegMSE, ElasticNet }
28    private readonly Fitting fit = Fitting.ElasticNet;
29
30    #region parameter properties
31    public IFixedValueParameter<DoubleValue> L1L2MixingParameter {
32      get { return (IFixedValueParameter<DoubleValue>)Parameters["L1L2_mixing"]; }
33    }
34    public IFixedValueParameter<DoubleValue> L1L2WeightParameter {
35      get { return (IFixedValueParameter<DoubleValue>)Parameters["L1L2_weight"]; }
36    }
37    public IFixedValueParameter<DoubleValue> PenaltyParameter {
38      get { return (IFixedValueParameter<DoubleValue>)Parameters["penalty"]; }
39    }
40    public IFixedValueParameter<IntValue> FractionDepthParameter {
41      get { return (IFixedValueParameter<IntValue>)Parameters["fraction_depth"]; }
42    }
43    public IValueParameter<Dataset> DatasetParameter {
44      get { return (IValueParameter<Dataset>)Parameters["dataset"]; }
45    }
46    public IValueParameter<Dataset> TrainingTrainingDatasetParameter {
47      get { return (IValueParameter<Dataset>)Parameters["datasetTrainingTraining"]; }
48    }
49    public IValueParameter<Dataset> TrainingTestDatasetParameter {
50      get { return (IValueParameter<Dataset>)Parameters["datasetTrainingTest"]; }
51    }
52    public IValueParameter<Dataset> Phi0_08DatasetParameter {
53      get { return (IValueParameter<Dataset>)Parameters["data_phi_0_08"]; }
54    }
55    #endregion
56
57    #region properties
58    public double L1L2Mixing {
59      get { return L1L2MixingParameter.Value.Value; }
60      set { L1L2MixingParameter.Value.Value = value; }
61    }
62    public double L1L2Weight {
63      get { return L1L2WeightParameter.Value.Value; }
64      set { L1L2WeightParameter.Value.Value = value; }
65    }
66    public double Penalty {
67      get { return PenaltyParameter.Value.Value; }
68      set { PenaltyParameter.Value.Value = value; }
69    }
70    public int FractionDepth {
71      get { return FractionDepthParameter.Value.Value; }
72      set { FractionDepthParameter.Value.Value = value; }
73    }
74    public Dataset Dataset {
75      get { return DatasetParameter.Value; }
76      set { DatasetParameter.Value = value; }
77    }
78    public Dataset TrainingTrainingDataset {
79      get { return TrainingTrainingDatasetParameter.Value; }
80      set { TrainingTrainingDatasetParameter.Value = value; }
81    }
82    public Dataset TrainingTestDataset {
83      get { return TrainingTestDatasetParameter.Value; }
84      set { TrainingTestDatasetParameter.Value = value; }
85    }
86    public Dataset Phi0_08Dataset {
87      get { return Phi0_08DatasetParameter.Value; }
88      set { Phi0_08DatasetParameter.Value = value; }
89    }
90    #endregion
91
92
93    public override bool Maximization { get { return false; } }
94
95    public IRegressionProblemData ProblemData { get; set; }
96
97    // BEWARE local fields
98    // DO NOT store or clone
99    // these variables are initialized on the first call of Evaluate.
100    // whenever a dataset parameter is changed (which has an effect on these variables)
101    // all variables must be cleared to ensure that they are recalculated on the next call of Evaluate
102    private DoubleMatrix dataMatrix; // data for training
103    private DoubleMatrix dataNelderMead; // data for optimizing coefficients
104    private DoubleMatrix dataMatrixTest; // data for testing
105    private Transformation transformation;
106    private double arithmeticMean;
107    private double SQT; //Sum of Squares Total, short: SQT
108    private void ClearLocalVariables() {
109      dataMatrix = null;
110      dataNelderMead = null;
111      dataMatrixTest = null;
112      transformation = null;
113      arithmeticMean = 0.0;
114      SQT = 0.0;
115    }
116    // END local fields
117
118
119    // cloning ctor
120    public Problem(Problem orig, Cloner cloner) : base(orig, cloner) {
121    }
122
123    public Problem() : base() {
124      Parameters.Add(new FixedValueParameter<DoubleValue>("L1L2_mixing", "TODO Description", new DoubleValue(0.2)));
125      Parameters.Add(new FixedValueParameter<DoubleValue>("L1L2_weight", "TODO Description", new DoubleValue(1)));
126      Parameters.Add(new FixedValueParameter<DoubleValue>("penalty", "TODO Description", new DoubleValue(0.01)));
127      Parameters.Add(new FixedValueParameter<IntValue>("fraction_depth", "TODO Description", new IntValue(4)));
128      Parameters.Add(new ValueParameter<Dataset>("dataset", "TODO Description", new Dataset()));
129      Parameters.Add(new ValueParameter<Dataset>("datasetTrainingTraining", "TODO Description", new Dataset()));
130      Parameters.Add(new ValueParameter<Dataset>("data_phi_0_08", "TODO Description", new Dataset()));
131      Parameters.Add(new ValueParameter<Dataset>("datasetTrainingTest", "TODO Description", new Dataset()));
132
133      foreach (var temperature in new[] { 350, 375, 400, 425, 450, 475, 500 }) {
134        foreach (var phip in new[] { "0_001", "0_01", "0_1", "1" }) {
135          // TODO: refactor
136          Parameters.Add(new ValueParameter<Dataset>("data_" + temperature + "_" + phip, "TODO Description", new Dataset()));
137        }
138      }
139
140      ClearLocalVariables();
141
142      DatasetParameter.ValueChanged += DatasetParameter_ValueChanged;
143      TrainingTrainingDatasetParameter.ValueChanged += DatasetParameter_ValueChanged;
144      TrainingTestDatasetParameter.ValueChanged += DatasetParameter_ValueChanged;
145      Phi0_08DatasetParameter.ValueChanged += DatasetParameter_ValueChanged;
146
147      UpdateEncoding();
148    }
149
150    [StorableHook(HookType.AfterDeserialization)]
151    private void AfterDeserialization() {
152      DatasetParameter.ValueChanged += DatasetParameter_ValueChanged;
153      TrainingTrainingDatasetParameter.ValueChanged += DatasetParameter_ValueChanged;
154      TrainingTestDatasetParameter.ValueChanged += DatasetParameter_ValueChanged;
155      Phi0_08DatasetParameter.ValueChanged += DatasetParameter_ValueChanged;
156
157      if (Parameters.ContainsKey("penality")) {
158        Parameters.Remove("penality");
159        Parameters.Add(new FixedValueParameter<DoubleValue>("penalty", "TODO Description", new DoubleValue(0.01)));
160      }
161    }
162
163    #region event handlers for clearing local variables whenever a dataset is changed
164    private void DatasetParameter_ValueChanged(object sender, EventArgs e) {
165      ClearLocalVariables();
166      UpdateEncoding();
167    }
168
169    private void UpdateEncoding() {
170      int fraction_depth = FractionDepth;
171      int number_variables = Dataset.Columns;
172      int vector_length = (2 * fraction_depth - 1) * number_variables; //number of variables n because n-1 coefficients + 1 constant
173
174      Encoding = new MultiEncoding()
175        .Add(new RealVectorEncoding("coeff", length: vector_length, min: -3, max: 3))
176        .Add(new BinaryVectorEncoding("binar", length: vector_length))
177      ;
178    }
179    #endregion
180
181    public override IDeepCloneable Clone(Cloner cloner) {
182      return new Problem(this, cloner);
183    }
184
185    public override double Evaluate(Individual individual, IRandom random) {
186      if (dataMatrix == null) {
187        InitializeTransformedDatasets();
188      }
189
190      double LS = 0.0; //sum of squared residuals -> SQR = LS
191      double quality = 0.0;
192
193      double[] coeff = individual.RealVector("coeff").ToArray();
194      bool[] binar = individual.BinaryVector("binar").ToArray();
195
196      LS = calculateLS(coeff, binar, dataMatrix);
197      quality = calculateQuality(coeff, binar, LS);
198
199      #region Nelder-Mead-Methode
200      double uniformPeturbation = 1.0;
201      double tolerance = 1e-3;
202      int maxEvals = 250;
203      int numSearches = 4;
204      int numPoints = dataMatrix.Rows / 5; // 20% of the training samples
205
206      double[] optimizedCoeffShort = new double[numVariablesUsed(binar)];
207      double[] optimizedCoeffLong = new double[coeff.Length];
208
209      for (int count = 0; count < numSearches; count++) {
210        int indexShort = 0;
211        for (int indexLong = 0; indexLong < coeff.Length; indexLong++) {
212          if (binar[indexLong] == true) {
213            optimizedCoeffShort[indexShort] = coeff[indexLong];
214            indexShort++;
215          }
216        }
217
218        SimplexConstant[] constants = new SimplexConstant[numVariablesUsed(binar)];
219        for (int i = 0; i < numVariablesUsed(binar); i++) {
220          constants[i] = new SimplexConstant(optimizedCoeffShort[i], uniformPeturbation);
221        }
222
223        //ObjFunctionNelderMead NelderMead = new ObjFunctionNelderMead(this, dataNelderMead, coeff, binar);
224        ObjFunctionNelderMead NelderMead = new ObjFunctionNelderMead(this, dataMatrix, numPoints, coeff, binar);
225
226        ObjectiveFunctionDelegate objFunction = new ObjectiveFunctionDelegate(NelderMead.objFunctionNelderMead);
227        RegressionResult result = NelderMeadSimplex.Regress(constants, tolerance, maxEvals, objFunction);
228
229        optimizedCoeffShort = result.Constants;
230        int shortIndex = 0;
231        for (int i = 0; i < coeff.Length; i++) {
232          if (binar[i] == true) {
233            optimizedCoeffLong[i] = optimizedCoeffShort[shortIndex];
234            shortIndex++;
235          } else {
236            optimizedCoeffLong[i] = coeff[i];
237          }
238        }
239
240        double newLS = calculateLS(optimizedCoeffLong, binar, dataMatrix);
241        double newQuality = calculateQuality(optimizedCoeffLong, binar, newLS);
242
243        if (newQuality < quality) {
244          // dont set new coeff!
245          individual["coeff"] = new RealVector(optimizedCoeffLong);
246          LS = newLS;
247          quality = newQuality;
248        }
249      }
250      #endregion
251
252      //doubleThrowInvOrNaN(quality, "quality");
253      individual["LS"] = new DoubleValue(LS); //Least Squares - Sum of Squares Residual, short: SQR
254      individual["Depth"] = new DoubleValue(calculateDepth(coeff, binar));
255      individual["CoeffNumber"] = new DoubleValue((double)numVariablesUsed(binar));
256      individual["MSE"] = new DoubleValue(LS / dataMatrix.Rows);
257      individual["MSETest"] = new DoubleValue(0.0); // only calculated for the best solution in a generation (see Analyze())
258      individual["R2"] = new DoubleValue(1.0 - LS / SQT); //R^2 = 1 - SQR/SQT
259
260      return quality;
261    }
262
263    private void InitializeTransformedDatasets() {
264      //dataMatrix = new DoubleMatrix(this.createMatrix("dataset", 1000));
265      dataMatrix = new DoubleMatrix(TrainingTrainingDataset.ToArray(TrainingTrainingDataset.VariableNames, Enumerable.Range(0, TrainingTrainingDataset.Rows)));
266      dataNelderMead = new DoubleMatrix(Phi0_08Dataset.ToArray(Phi0_08Dataset.VariableNames, Enumerable.Range(0, Phi0_08Dataset.Rows)));
267      dataMatrixTest = new DoubleMatrix(TrainingTestDataset.ToArray(TrainingTestDataset.VariableNames, Enumerable.Range(0, TrainingTestDataset.Rows)));
268      //minMaxTransformation(0);
269      //minMaxTransformation(1);
270      //log10Transformation(2); // no negativ values!
271      //minMaxTransformation(2); // Min and Max have to be different!
272
273      transformation = new Transformation(dataMatrix);
274      // vars["dataMatrix"] = dataMatrix;
275
276      transformation.useSameTransformation(dataNelderMead);
277      // vars["dataNelderMead"] = dataNelderMead;
278
279      transformation.useSameTransformation(dataMatrixTest);
280      // vars["dataMatrixTest"] = dataMatrixTest;
281
282      arithmeticMean = calculateArithmeticMean();
283      SQT = calculateSQT(arithmeticMean);
284    }
285
286    public override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) {
287      Individual bestIndividual = null;
288      double bestQuality = double.MaxValue;
289      int theBest = 0;
290
291      for (int i = 0; i < qualities.Count(); i++) {
292        if (qualities[i] < bestQuality) {
293          bestQuality = qualities[i];
294          bestIndividual = individuals[i].Copy();
295          theBest = i;
296        }
297      }
298
299      if (dataMatrix == null) {
300        InitializeTransformedDatasets();
301      }
302
303      /*
304      // Uncomment the following lines if you want to retrieve the best individual
305      var orderedIndividuals = individuals.Zip(qualities, (i, q) => new { Individual = i, Quality = q }).OrderBy(z => z.Quality);
306      var best = Maximization ? orderedIndividuals.Last().Individual : orderedIndividuals.First().Individual;
307      if (!results.ContainsKey("Best Solution")) {
308        results.Add(new Result("Best Solution", typeof(RealVector)));
309       
310      }
311      results["Best Solution"].Value = (IItem)best.RealVector("coeff").Clone();
312      //
313      */
314
315      bool[] binar = bestIndividual.BinaryVector("binar").ToArray();
316      double[] coeff = bestIndividual.RealVector("coeff").ToArray();
317
318      // calculate and set test-MSE (only) for the individual with best training-MSE
319      double bestLSTest = calculateLS(coeff, binar, dataMatrixTest);
320      double bestQualityTest = calculateQuality(coeff, binar, bestLSTest);
321      individuals[theBest]["MSETest"] = new DoubleValue(bestQualityTest);
322
323      InitDataTables(results);
324      InitScatterPlots(results);
325
326      #region set dataTables     
327      var datatable = (DataTable)results["Datatable"].Value;
328      var coefficients = (DataTable)results["Coefficients"].Value;
329      var pearsonR2 = (DataTable)results["Pearson R2"].Value;
330
331      datatable.Rows["MSE"].Values.Add(calculateRegMSE(((DoubleValue)bestIndividual["LS"]).Value, binar, 0.0));
332      datatable.Rows["RegMSE"].Values.Add(calculateRegMSE(((DoubleValue)bestIndividual["LS"]).Value, binar, Penalty));
333      datatable.Rows["ElasticNet"].Values.Add(calculateElasticNet(((DoubleValue)bestIndividual["LS"]).Value, coeff, binar));
334
335      coefficients.Rows["Depth"].Values.Add(((DoubleValue)bestIndividual["Depth"]).Value);
336      coefficients.Rows["Number"].Values.Add(((DoubleValue)bestIndividual["CoeffNumber"]).Value);
337
338      pearsonR2.Rows["R2"].Values.Add(((DoubleValue)bestIndividual["R2"]).Value);
339      #endregion
340
341      #region set scatterPlots
342      var curves0_001 = (ScatterPlot)results["Curves0_001"].Value;
343      var curves0_01 = (ScatterPlot)results["Curves0_01"].Value;
344      var curves0_1 = (ScatterPlot)results["Curves0_1"].Value;
345      var curves1 = (ScatterPlot)results["Curves1"].Value;
346      #region curves 0.001
347      curves0_001.Rows["Temp350Convergence"].Points.Clear();
348      curves0_001.Rows["Temp375Convergence"].Points.Clear();
349      curves0_001.Rows["Temp400Convergence"].Points.Clear();
350      curves0_001.Rows["Temp425Convergence"].Points.Clear();
351      curves0_001.Rows["Temp450Convergence"].Points.Clear();
352      curves0_001.Rows["Temp475Convergence"].Points.Clear();
353      curves0_001.Rows["Temp500Convergence"].Points.Clear();
354      sampleCurve(curves0_001, "Temp350Convergence", coeff, binar, 350.0, 0.001, 100, 50.0);
355      sampleCurve(curves0_001, "Temp375Convergence", coeff, binar, 375.0, 0.001, 100, 50.0);
356      sampleCurve(curves0_001, "Temp400Convergence", coeff, binar, 400.0, 0.001, 100, 50.0);
357      sampleCurve(curves0_001, "Temp425Convergence", coeff, binar, 425.0, 0.001, 100, 50.0);
358      sampleCurve(curves0_001, "Temp450Convergence", coeff, binar, 450.0, 0.001, 100, 50.0);
359      sampleCurve(curves0_001, "Temp475Convergence", coeff, binar, 475.0, 0.001, 100, 50.0);
360      sampleCurve(curves0_001, "Temp500Convergence", coeff, binar, 500.0, 0.001, 100, 50.0);
361
362      results.AddOrUpdateResult("Curves Phi' = 0.001", curves0_001);
363      #endregion
364      #region curves 0.01
365      curves0_01.Rows["Temp350Convergence"].Points.Clear();
366      curves0_01.Rows["Temp375Convergence"].Points.Clear();
367      curves0_01.Rows["Temp400Convergence"].Points.Clear();
368      curves0_01.Rows["Temp425Convergence"].Points.Clear();
369      curves0_01.Rows["Temp450Convergence"].Points.Clear();
370      curves0_01.Rows["Temp475Convergence"].Points.Clear();
371      curves0_01.Rows["Temp500Convergence"].Points.Clear();
372      sampleCurve(curves0_01, "Temp350Convergence", coeff, binar, 350.0, 0.01, 100, 60.0);
373      sampleCurve(curves0_01, "Temp375Convergence", coeff, binar, 375.0, 0.01, 100, 60.0);
374      sampleCurve(curves0_01, "Temp400Convergence", coeff, binar, 400.0, 0.01, 100, 60.0);
375      sampleCurve(curves0_01, "Temp425Convergence", coeff, binar, 425.0, 0.01, 100, 60.0);
376      sampleCurve(curves0_01, "Temp450Convergence", coeff, binar, 450.0, 0.01, 100, 60.0);
377      sampleCurve(curves0_01, "Temp475Convergence", coeff, binar, 475.0, 0.01, 100, 60.0);
378      sampleCurve(curves0_01, "Temp500Convergence", coeff, binar, 500.0, 0.01, 100, 60.0);
379
380      results.AddOrUpdateResult("Curves Phi' = 0.01", curves0_01);
381      #endregion
382      #region curves 0.1
383      curves0_1.Rows["Temp350Convergence"].Points.Clear();
384      curves0_1.Rows["Temp375Convergence"].Points.Clear();
385      curves0_1.Rows["Temp400Convergence"].Points.Clear();
386      curves0_1.Rows["Temp425Convergence"].Points.Clear();
387      curves0_1.Rows["Temp450Convergence"].Points.Clear();
388      curves0_1.Rows["Temp475Convergence"].Points.Clear();
389      curves0_1.Rows["Temp500Convergence"].Points.Clear();
390      sampleCurve(curves0_1, "Temp350Convergence", coeff, binar, 350.0, 0.1, 100, 70.0);
391      sampleCurve(curves0_1, "Temp375Convergence", coeff, binar, 375.0, 0.1, 100, 70.0);
392      sampleCurve(curves0_1, "Temp400Convergence", coeff, binar, 400.0, 0.1, 100, 70.0);
393      sampleCurve(curves0_1, "Temp425Convergence", coeff, binar, 425.0, 0.1, 100, 70.0);
394      sampleCurve(curves0_1, "Temp450Convergence", coeff, binar, 450.0, 0.1, 100, 70.0);
395      sampleCurve(curves0_1, "Temp475Convergence", coeff, binar, 475.0, 0.1, 100, 70.0);
396      sampleCurve(curves0_1, "Temp500Convergence", coeff, binar, 500.0, 0.1, 100, 70.0);
397
398      results.AddOrUpdateResult("Curves Phi' = 0.1", curves0_1);
399      #endregion
400      #region curves 1
401      curves1.Rows["Temp350Convergence"].Points.Clear();
402      curves1.Rows["Temp375Convergence"].Points.Clear();
403      curves1.Rows["Temp400Convergence"].Points.Clear();
404      curves1.Rows["Temp425Convergence"].Points.Clear();
405      curves1.Rows["Temp450Convergence"].Points.Clear();
406      curves1.Rows["Temp475Convergence"].Points.Clear();
407      curves1.Rows["Temp500Convergence"].Points.Clear();
408      sampleCurve(curves1, "Temp350Convergence", coeff, binar, 350.0, 1, 100, 80.0);
409      sampleCurve(curves1, "Temp375Convergence", coeff, binar, 375.0, 1, 100, 80.0);
410      sampleCurve(curves1, "Temp400Convergence", coeff, binar, 400.0, 1, 100, 80.0);
411      sampleCurve(curves1, "Temp425Convergence", coeff, binar, 425.0, 1, 100, 80.0);
412      sampleCurve(curves1, "Temp450Convergence", coeff, binar, 450.0, 1, 100, 80.0);
413      sampleCurve(curves1, "Temp475Convergence", coeff, binar, 475.0, 1, 100, 80.0);
414      sampleCurve(curves1, "Temp500Convergence", coeff, binar, 500.0, 1, 100, 80.0);
415
416      results.AddOrUpdateResult("Curves Phi' = 1", curves1);
417      #endregion
418      #endregion
419
420      #region create tree and regression solution
421      var allVariables = TrainingTrainingDataset.VariableNames;
422      var allowedInputVariables = allVariables.Take(allVariables.Count() - 1);
423      var targetVariable = allVariables.Last();
424
425      var tree = CreateSymbolicExpressionTree(binar, coeff, TrainingTrainingDataset.VariableNames.ToArray());
426      var model = new SymbolicRegressionModel(targetVariable, tree, new SymbolicDataAnalysisExpressionTreeLinearInterpreter());
427      var combinedData = MatrixExtensions.VertCat(dataMatrix.CloneAsMatrix(), dataMatrixTest.CloneAsMatrix());
428      var combinedDs = new Dataset(allVariables, combinedData);
429      var problemData = new RegressionProblemData(combinedDs, allowedInputVariables, targetVariable);
430      problemData.TrainingPartition.Start = 0;
431      problemData.TrainingPartition.End = dataMatrix.Rows;
432      problemData.TestPartition.Start = dataMatrix.Rows;
433      problemData.TestPartition.End = combinedDs.Rows;
434      var sol = new SymbolicRegressionSolution(model, problemData);
435      results.AddOrUpdateResult("Solution", sol);
436      #endregion
437    }
438
439    private ISymbolicExpressionTree CreateSymbolicExpressionTree(bool[] binar, double[] coeff, string[] varNames) {
440      bool firstCoeffUnequalZero = false;
441      var d = varNames.Length;
442      var expression = constSy.CreateTreeNode(); // zero
443
444      for (var i = coeff.Length - 1; i > 0; i = i - 2 * d) {
445
446        if ((linearFunctionEqualZero(coeff, binar, i - d + 1, i) == false)) {
447          firstCoeffUnequalZero = true;
448          var sum = addSy.CreateTreeNode();
449          sum.AddSubtree(createLinearExpression(coeff, binar, i - d + 1, i, varNames));
450          sum.AddSubtree(expression);
451          expression = sum;
452        }
453
454        if (firstCoeffUnequalZero == true && i > 2 * d) { // don't take the first coeffVecPart and don't take the last coeffVecPart (both are only to add)
455          if (linearFunctionEqualZero(coeff, binar, i - 2 * d + 1, i - d) == false) {
456            /* div-by-zero cannot be determined for the expression
457            if (valueNearZero(value) == true)  // no division by zero
458              return double.MaxValue;
459            else {
460            */
461              var frac = divSy.CreateTreeNode();
462              frac.AddSubtree(createLinearExpression(coeff, binar, i - 2 * d + 1, i - d, varNames));
463              frac.AddSubtree(expression);
464              expression = frac;
465            /* } */
466          } else {
467            // don't allow coeffVecParts in middle to be equal zero
468            var errorConst = (ConstantTreeNode)constSy.CreateTreeNode();
469            errorConst.Value = double.MaxValue;
470            expression = errorConst;
471          }
472        }
473      }
474
475      var startNode = startSy.CreateTreeNode();
476      var progRoot = progRootSy.CreateTreeNode();
477      progRoot.AddSubtree(startNode);
478      startNode.AddSubtree(expression);
479      return new SymbolicExpressionTree(progRoot) ;
480    }
481
482    private void InitDataTables(ResultCollection results) {
483      DataTable datatable, coefficients, pearsonR2;
484      if (!results.ContainsKey("Datatable")) {
485        datatable = new DataTable("Fitness");
486        DataRow MSE = new DataRow("MSE");
487        DataRow RegMSE = new DataRow("RegMSE");
488        DataRow ElasticNet = new DataRow("ElasticNet");
489        datatable.Rows.Add(MSE);
490        datatable.Rows.Add(RegMSE);
491        datatable.Rows.Add(ElasticNet);
492        results.Add(new Result("Datatable", datatable));
493      }
494
495
496      if (!results.ContainsKey("Coefficients")) {
497        coefficients = new DataTable("Coefficients");
498        DataRow Depth = new DataRow("Depth");
499        DataRow Number = new DataRow("Number");
500        coefficients.Rows.Add(Depth);
501        coefficients.Rows.Add(Number);
502        results.Add(new Result("Coefficients", coefficients));
503      }
504
505      if (!results.ContainsKey("Pearson R2")) {
506        pearsonR2 = new DataTable("PearsonR2");
507        DataRow R2 = new DataRow("R2");
508        pearsonR2.Rows.Add(R2);
509        results.Add(new Result("Pearson R2", pearsonR2));
510      }
511    }
512
513    private void InitScatterPlots(ResultCollection results) {
514      #region curves 0.001
515      ScatterPlot curves0_001 = new ScatterPlot("Curves0_001", "Kurven mit Phi'=0.001");
516      readCurve(curves0_001, "data_350_0_001", "Temp350", System.Drawing.Color.Blue);
517      readCurve(curves0_001, "data_375_0_001", "Temp375", System.Drawing.Color.Orange);
518      readCurve(curves0_001, "data_400_0_001", "Temp400", System.Drawing.Color.Red);
519      readCurve(curves0_001, "data_425_0_001", "Temp425", System.Drawing.Color.Green);
520      readCurve(curves0_001, "data_450_0_001", "Temp450", System.Drawing.Color.Gray);
521      readCurve(curves0_001, "data_475_0_001", "Temp475", System.Drawing.Color.Olive);
522      readCurve(curves0_001, "data_500_0_001", "Temp500", System.Drawing.Color.Gold);
523
524      var empty0_001 = new Point2D<double>[0];
525      ScatterPlotDataRow Temp350Convergence0_001 = new ScatterPlotDataRow("Temp350Convergence", "Temp350Convergence", empty0_001);
526      Temp350Convergence0_001.VisualProperties.Color = System.Drawing.Color.Blue;
527      Temp350Convergence0_001.VisualProperties.PointSize = 2;
528      curves0_001.Rows.Add(Temp350Convergence0_001);
529
530      ScatterPlotDataRow Temp375Convergence0_001 = new ScatterPlotDataRow("Temp375Convergence", "Temp375Convergence", empty0_001);
531      Temp375Convergence0_001.VisualProperties.Color = System.Drawing.Color.Orange;
532      Temp375Convergence0_001.VisualProperties.PointSize = 2;
533      curves0_001.Rows.Add(Temp375Convergence0_001);
534
535      ScatterPlotDataRow Temp400Convergence0_001 = new ScatterPlotDataRow("Temp400Convergence", "Temp400Convergence", empty0_001);
536      Temp400Convergence0_001.VisualProperties.Color = System.Drawing.Color.Red;
537      Temp400Convergence0_001.VisualProperties.PointSize = 2;
538      curves0_001.Rows.Add(Temp400Convergence0_001);
539
540      ScatterPlotDataRow Temp425Convergence0_001 = new ScatterPlotDataRow("Temp425Convergence", "Temp425Convergence", empty0_001);
541      Temp425Convergence0_001.VisualProperties.Color = System.Drawing.Color.Green;
542      Temp425Convergence0_001.VisualProperties.PointSize = 2;
543      curves0_001.Rows.Add(Temp425Convergence0_001);
544
545      ScatterPlotDataRow Temp450Convergence0_001 = new ScatterPlotDataRow("Temp450Convergence", "Temp450Convergence", empty0_001);
546      Temp450Convergence0_001.VisualProperties.Color = System.Drawing.Color.Gray;
547      Temp450Convergence0_001.VisualProperties.PointSize = 2;
548      curves0_001.Rows.Add(Temp450Convergence0_001);
549
550      ScatterPlotDataRow Temp475Convergence0_001 = new ScatterPlotDataRow("Temp475Convergence", "Temp475Convergence", empty0_001);
551      Temp475Convergence0_001.VisualProperties.Color = System.Drawing.Color.Olive;
552      Temp475Convergence0_001.VisualProperties.PointSize = 2;
553      curves0_001.Rows.Add(Temp475Convergence0_001);
554
555      ScatterPlotDataRow Temp500Convergence0_001 = new ScatterPlotDataRow("Temp500Convergence", "Temp500Convergence", empty0_001);
556      Temp500Convergence0_001.VisualProperties.Color = System.Drawing.Color.Gold;
557      Temp500Convergence0_001.VisualProperties.PointSize = 2;
558      curves0_001.Rows.Add(Temp500Convergence0_001);
559      results.AddOrUpdateResult("Curves0_001", curves0_001);
560
561      #endregion
562      #region curves 0.01
563      ScatterPlot curves0_01 = new ScatterPlot("Curves0_01", "Kurven mit Phi'=0.01");
564      readCurve(curves0_01, "data_350_0_01", "Temp350", System.Drawing.Color.Blue);
565      readCurve(curves0_01, "data_375_0_01", "Temp375", System.Drawing.Color.Orange);
566      readCurve(curves0_01, "data_400_0_01", "Temp400", System.Drawing.Color.Red);
567      readCurve(curves0_01, "data_425_0_01", "Temp425", System.Drawing.Color.Green);
568      readCurve(curves0_01, "data_450_0_01", "Temp450", System.Drawing.Color.Gray);
569      readCurve(curves0_01, "data_475_0_01", "Temp475", System.Drawing.Color.Olive);
570      readCurve(curves0_01, "data_500_0_01", "Temp500", System.Drawing.Color.Gold);
571
572      var empty0_01 = new Point2D<double>[0];
573      ScatterPlotDataRow Temp350Convergence0_01 = new ScatterPlotDataRow("Temp350Convergence", "Temp350Convergence", empty0_01);
574      Temp350Convergence0_01.VisualProperties.Color = System.Drawing.Color.Blue;
575      Temp350Convergence0_01.VisualProperties.PointSize = 2;
576      curves0_01.Rows.Add(Temp350Convergence0_01);
577
578      ScatterPlotDataRow Temp375Convergence0_01 = new ScatterPlotDataRow("Temp375Convergence", "Temp375Convergence", empty0_01);
579      Temp375Convergence0_01.VisualProperties.Color = System.Drawing.Color.Orange;
580      Temp375Convergence0_01.VisualProperties.PointSize = 2;
581      curves0_01.Rows.Add(Temp375Convergence0_01);
582
583      ScatterPlotDataRow Temp400Convergence0_01 = new ScatterPlotDataRow("Temp400Convergence", "Temp400Convergence", empty0_01);
584      Temp400Convergence0_01.VisualProperties.Color = System.Drawing.Color.Red;
585      Temp400Convergence0_01.VisualProperties.PointSize = 2;
586      curves0_01.Rows.Add(Temp400Convergence0_01);
587
588      ScatterPlotDataRow Temp425Convergence0_01 = new ScatterPlotDataRow("Temp425Convergence", "Temp425Convergence", empty0_01);
589      Temp425Convergence0_01.VisualProperties.Color = System.Drawing.Color.Green;
590      Temp425Convergence0_01.VisualProperties.PointSize = 2;
591      curves0_01.Rows.Add(Temp425Convergence0_01);
592
593      ScatterPlotDataRow Temp450Convergence0_01 = new ScatterPlotDataRow("Temp450Convergence", "Temp450Convergence", empty0_01);
594      Temp450Convergence0_01.VisualProperties.Color = System.Drawing.Color.Gray;
595      Temp450Convergence0_01.VisualProperties.PointSize = 2;
596      curves0_01.Rows.Add(Temp450Convergence0_01);
597
598      ScatterPlotDataRow Temp475Convergence0_01 = new ScatterPlotDataRow("Temp475Convergence", "Temp475Convergence", empty0_01);
599      Temp475Convergence0_01.VisualProperties.Color = System.Drawing.Color.Olive;
600      Temp475Convergence0_01.VisualProperties.PointSize = 2;
601      curves0_01.Rows.Add(Temp475Convergence0_01);
602
603      ScatterPlotDataRow Temp500Convergence0_01 = new ScatterPlotDataRow("Temp500Convergence", "Temp500Convergence", empty0_01);
604      Temp500Convergence0_01.VisualProperties.Color = System.Drawing.Color.Gold;
605      Temp500Convergence0_01.VisualProperties.PointSize = 2;
606      curves0_01.Rows.Add(Temp500Convergence0_01);
607      results.AddOrUpdateResult("Curves0_01", curves0_01);
608
609      #endregion
610      #region curves 0.1
611      ScatterPlot curves0_1 = new ScatterPlot("Curves0_1", "Kurven mit Phi'=0.1");
612      readCurve(curves0_1, "data_350_0_1", "Temp350", System.Drawing.Color.Blue);
613      readCurve(curves0_1, "data_375_0_1", "Temp375", System.Drawing.Color.Orange);
614      readCurve(curves0_1, "data_400_0_1", "Temp400", System.Drawing.Color.Red);
615      readCurve(curves0_1, "data_425_0_1", "Temp425", System.Drawing.Color.Green);
616      readCurve(curves0_1, "data_450_0_1", "Temp450", System.Drawing.Color.Gray);
617      readCurve(curves0_1, "data_475_0_1", "Temp475", System.Drawing.Color.Olive);
618      readCurve(curves0_1, "data_500_0_1", "Temp500", System.Drawing.Color.Gold);
619
620      var empty0_1 = new Point2D<double>[0];
621      ScatterPlotDataRow Temp350Convergence0_1 = new ScatterPlotDataRow("Temp350Convergence", "Temp350Convergence", empty0_1);
622      Temp350Convergence0_1.VisualProperties.Color = System.Drawing.Color.Blue;
623      Temp350Convergence0_1.VisualProperties.PointSize = 2;
624      curves0_1.Rows.Add(Temp350Convergence0_1);
625
626      ScatterPlotDataRow Temp375Convergence0_1 = new ScatterPlotDataRow("Temp375Convergence", "Temp375Convergence", empty0_1);
627      Temp375Convergence0_1.VisualProperties.Color = System.Drawing.Color.Orange;
628      Temp375Convergence0_1.VisualProperties.PointSize = 2;
629      curves0_1.Rows.Add(Temp375Convergence0_1);
630
631      ScatterPlotDataRow Temp400Convergence0_1 = new ScatterPlotDataRow("Temp400Convergence", "Temp400Convergence", empty0_1);
632      Temp400Convergence0_1.VisualProperties.Color = System.Drawing.Color.Red;
633      Temp400Convergence0_1.VisualProperties.PointSize = 2;
634      curves0_1.Rows.Add(Temp400Convergence0_1);
635
636      ScatterPlotDataRow Temp425Convergence0_1 = new ScatterPlotDataRow("Temp425Convergence", "Temp425Convergence", empty0_1);
637      Temp425Convergence0_1.VisualProperties.Color = System.Drawing.Color.Green;
638      Temp425Convergence0_1.VisualProperties.PointSize = 2;
639      curves0_1.Rows.Add(Temp425Convergence0_1);
640
641      ScatterPlotDataRow Temp450Convergence0_1 = new ScatterPlotDataRow("Temp450Convergence", "Temp450Convergence", empty0_1);
642      Temp450Convergence0_1.VisualProperties.Color = System.Drawing.Color.Gray;
643      Temp450Convergence0_1.VisualProperties.PointSize = 2;
644      curves0_1.Rows.Add(Temp450Convergence0_1);
645
646      ScatterPlotDataRow Temp475Convergence0_1 = new ScatterPlotDataRow("Temp475Convergence", "Temp475Convergence", empty0_1);
647      Temp475Convergence0_1.VisualProperties.Color = System.Drawing.Color.Olive;
648      Temp475Convergence0_1.VisualProperties.PointSize = 2;
649      curves0_1.Rows.Add(Temp475Convergence0_1);
650
651      ScatterPlotDataRow Temp500Convergence0_1 = new ScatterPlotDataRow("Temp500Convergence", "Temp500Convergence", empty0_1);
652      Temp500Convergence0_1.VisualProperties.Color = System.Drawing.Color.Gold;
653      Temp500Convergence0_1.VisualProperties.PointSize = 2;
654      curves0_1.Rows.Add(Temp500Convergence0_1);
655      results.AddOrUpdateResult("Curves0_1", curves0_1);
656      #endregion
657      #region curves 1
658      ScatterPlot curves1 = new ScatterPlot("Curves1", "Kurven mit Phi'=1");
659      readCurve(curves1, "data_350_1", "Temp350", System.Drawing.Color.Blue);
660      readCurve(curves1, "data_375_1", "Temp375", System.Drawing.Color.Orange);
661      readCurve(curves1, "data_400_1", "Temp400", System.Drawing.Color.Red);
662      readCurve(curves1, "data_425_1", "Temp425", System.Drawing.Color.Green);
663      readCurve(curves1, "data_450_1", "Temp450", System.Drawing.Color.Gray);
664      readCurve(curves1, "data_475_1", "Temp475", System.Drawing.Color.Olive);
665      readCurve(curves1, "data_500_1", "Temp500", System.Drawing.Color.Gold);
666
667      var empty1 = new Point2D<double>[0];
668      ScatterPlotDataRow Temp350Convergence1 = new ScatterPlotDataRow("Temp350Convergence", "Temp350Convergence", empty1);
669      Temp350Convergence1.VisualProperties.Color = System.Drawing.Color.Blue;
670      Temp350Convergence1.VisualProperties.PointSize = 2;
671      curves1.Rows.Add(Temp350Convergence1);
672
673      ScatterPlotDataRow Temp375Convergence1 = new ScatterPlotDataRow("Temp375Convergence", "Temp375Convergence", empty1);
674      Temp375Convergence1.VisualProperties.Color = System.Drawing.Color.Orange;
675      Temp375Convergence1.VisualProperties.PointSize = 2;
676      curves1.Rows.Add(Temp375Convergence1);
677
678      ScatterPlotDataRow Temp400Convergence1 = new ScatterPlotDataRow("Temp400Convergence", "Temp400Convergence", empty1);
679      Temp400Convergence1.VisualProperties.Color = System.Drawing.Color.Red;
680      Temp400Convergence1.VisualProperties.PointSize = 2;
681      curves1.Rows.Add(Temp400Convergence1);
682
683      ScatterPlotDataRow Temp425Convergence1 = new ScatterPlotDataRow("Temp425Convergence", "Temp425Convergence", empty1);
684      Temp425Convergence1.VisualProperties.Color = System.Drawing.Color.Green;
685      Temp425Convergence1.VisualProperties.PointSize = 2;
686      curves1.Rows.Add(Temp425Convergence1);
687
688      ScatterPlotDataRow Temp450Convergence1 = new ScatterPlotDataRow("Temp450Convergence", "Temp450Convergence", empty1);
689      Temp450Convergence1.VisualProperties.Color = System.Drawing.Color.Gray;
690      Temp450Convergence1.VisualProperties.PointSize = 2;
691      curves1.Rows.Add(Temp450Convergence1);
692
693      ScatterPlotDataRow Temp475Convergence1 = new ScatterPlotDataRow("Temp475Convergence", "Temp475Convergence", empty1);
694      Temp475Convergence1.VisualProperties.Color = System.Drawing.Color.Olive;
695      Temp475Convergence1.VisualProperties.PointSize = 2;
696      curves1.Rows.Add(Temp475Convergence1);
697
698      ScatterPlotDataRow Temp500Convergence1 = new ScatterPlotDataRow("Temp500Convergence", "Temp500Convergence", empty1);
699      Temp500Convergence1.VisualProperties.Color = System.Drawing.Color.Gold;
700      Temp500Convergence1.VisualProperties.PointSize = 2;
701      curves1.Rows.Add(Temp500Convergence1);
702      results.AddOrUpdateResult("Curves1", curves1);
703      #endregion
704    }
705
706    public override IEnumerable<Individual> GetNeighbors(Individual individual, IRandom random) {
707      // Use vars.yourVariable to access variables in the variable store i.e. yourVariable
708      // Create new vectors, based on the given one that represent small changes
709      // This method is only called from move-based algorithms (Local Search, Simulated Annealing, etc.)
710      while (true) {
711        // Algorithm will draw only a finite amount of samples
712        // Change to a for-loop to return a concrete amount of neighbors
713        var neighbor = individual.Copy();
714        // For instance, perform a single bit-flip in a binary parameter
715        //var bIndex = random.Next(neighbor.BinaryVector("b").Length);
716        //neighbor.BinaryVector("b")[bIndex] = !neighbor.BinaryVector("b")[bIndex];
717        yield return neighbor;
718      }
719    }
720    #region Import & Export
721    public void Load(IRegressionProblemData data) {
722      ProblemData = data;
723    }
724
725    public IRegressionProblemData Export() {
726      return ProblemData;
727    }
728    #endregion
729
730    // Implement further classes and methods
731
732    private int numVariablesUsed(bool[] binar) {
733      int num = 0;
734      for (int i = 0; i < binar.Count(); i++) {
735        if (binar[i] == true) // eventually we need coeff < valuenearzero too
736          num++;
737      }
738      return num;
739    }
740
741
742    /* old code
743    private double[,] createMatrix(string name, int numRows) {
744      var dataset = (HeuristicLab.Problems.DataAnalysis.Dataset)vars[name];
745
746      IEnumerable<string> variables = dataset.VariableNames;
747      // var rows = dataset.Rows;
748      var rows = Enumerable.Range(0, numRows).ToArray();
749
750      double[,] dataMatrix = dataset.ToArray(variables, rows);
751
752      return dataMatrix;
753    }
754    */
755
756
757    private double evaluateContinuedFraction(double[] coeff, bool[] binar, double[] dataPoint) {
758      double value = 0.0;
759      bool firstCoeffUnequalZero = false;
760
761      for (var i = coeff.Count() - 1; i > 0; i = i - 2 * dataPoint.Count()) {
762
763        if ((linearFunctionEqualZero(coeff, binar, i - dataPoint.Count() + 1, i) == false)) {
764          firstCoeffUnequalZero = true;
765          value = this.evaluateLinearFunction(coeff, binar, i - dataPoint.Count() + 1, i, dataPoint) + value;
766        }
767
768        if (firstCoeffUnequalZero == true && i > 2 * dataPoint.Count()) { // don't take the first coeffVecPart and don't take the last coeffVecPart (both are only to add)
769          if (linearFunctionEqualZero(coeff, binar, i - 2 * dataPoint.Count() + 1, i - dataPoint.Count()) == false) {
770            if (valueNearZero(value) == true)  // no division by zero
771              return double.MaxValue;
772            else
773              value = this.evaluateLinearFunction(coeff, binar, i - 2 * dataPoint.Count() + 1, i - dataPoint.Count(), dataPoint) / value;
774          } else return double.MaxValue; // don't allow coeffVecParts in middle to be equal zero
775        }
776      }
777
778      //doubleThrowInvOrNaN(value, "evaluateContinuedFractionValue");
779      return value;
780    }
781
782    private bool linearFunctionEqualZero(double[] coeff, bool[] binar, int start, int end) {
783      for (int i = start; i <= end; i++) {
784        if (binar[i] == true && valueNearZero(coeff[i]) == false)
785          return false;
786      }
787      return true;
788    }
789
790    private double evaluateLinearFunction(double[] coeff, bool[] binar, int start, int end, double[] dataPoint) {
791      double value = 0.0;
792
793      for (int i = start; i < end; i++) {
794        if (binar[i] == true)
795          value += dataPoint[i - start] * coeff[i];
796      }
797      if (binar[end] == true)
798        value += coeff[end];
799
800      //doubleThrowInvOrNaN(value, "evaluateLinearFunctionValue");
801      return value;
802    }
803
804    // symbols to be used in expression trees
805    private static Addition addSy = new Addition();
806    private static Division divSy = new Division();
807    private static HeuristicLab.Problems.DataAnalysis.Symbolic.Variable varSy = new Problems.DataAnalysis.Symbolic.Variable();
808    private static Constant constSy = new Constant();
809    private static StartSymbol startSy = new StartSymbol();
810    private static ProgramRootSymbol progRootSy = new ProgramRootSymbol();
811    private ISymbolicExpressionTreeNode createLinearExpression(double[] coeff, bool[] binar, int start, int end, string[] varNames) {
812      var sum = addSy.CreateTreeNode();
813
814      for (int i = start; i < end; i++) {
815        if (binar[i] == true) {
816          var varNode = (VariableTreeNode)varSy.CreateTreeNode();
817          varNode.VariableName = varNames[i - start];
818          varNode.Weight = coeff[i];
819          sum.AddSubtree(varNode);
820        }
821      }
822      if (binar[end] == true) {
823        var constNode = (ConstantTreeNode)constSy.CreateTreeNode();
824        constNode.Value = coeff[end];
825        sum.AddSubtree(constNode);
826      }
827
828      //doubleThrowInvOrNaN(value, "evaluateLinearFunctionValue");
829      return sum;
830    }
831
832    private bool valueNearZero(double value) {
833      if (Math.Abs(value) < epsilon)
834        return true;
835      else
836        return false;
837    }
838
839    private double calculateDepth(double[] coeff, bool[] binar) {
840      int coeffUnequalZero = -1;
841      double depth = 0.0;
842
843      for (int i = 0; i < coeff.Count(); i++) {
844        if (valueNearZero(coeff[i]) == false && binar[i] == true)
845          coeffUnequalZero = i;
846      }
847      if (coeffUnequalZero == -1) return 0.0; // equal zero
848      if (coeffUnequalZero < dataMatrix.Columns) return 1.0; // linear function
849      depth = (double)(coeffUnequalZero - dataMatrix.Columns + 1);
850      depth = Math.Ceiling(depth / (2 * dataMatrix.Columns)) + 1.0;
851      return depth;
852    }
853
854    private double calculateRegMSE(double quality, bool[] binar, double penalty) {
855      return (quality / dataMatrix.Rows) * (1 + penalty * numVariablesUsed(binar));
856    }
857
858    private double calculateElasticNet(double quality, double[] coeff, bool[] binar) {
859      double valueL1 = 0.0;
860      double valueL2 = 0.0;
861      double elasticNet = 0.0;
862      double L1L2weight = L1L2Weight;
863      double L1L2mixing = L1L2Mixing;
864
865      for (int i = 0; i < coeff.Count(); i++) {
866        if (binar[i] == true) {
867          valueL1 += Math.Abs(coeff[i]);
868          valueL2 += Math.Pow(coeff[i], 2);
869        }
870      }
871      elasticNet = quality + (L1L2weight * L1L2mixing * valueL1) + (L1L2weight * (1 - L1L2mixing) * valueL2);
872      return elasticNet / dataMatrix.Rows;
873    }
874
875    public double calculateLS(double[] coeff, bool[] binar, DoubleMatrix dataMatrix) {
876      double LS = 0.0;
877      double[] dataPoint = new double[dataMatrix.Columns];
878      for (var i = 0; i < dataMatrix.Rows; i++) {
879        for (var j = 0; j < dataMatrix.Columns; j++) {
880          dataPoint[j] = dataMatrix[i, j];
881        }
882        double continuedFractionValue = evaluateContinuedFraction(coeff, binar, dataPoint);
883        if (continuedFractionValue == double.MaxValue)
884          return double.MaxValue;
885        else
886          LS += Math.Pow((continuedFractionValue - dataMatrix[i, dataMatrix.Columns - 1]), 2);
887      }
888      return LS;
889    }
890
891    public double calculateQuality(double[] coeff, bool[] binar, double LS) {
892      switch (fit) {
893        case Fitting.MSE:
894          return calculateRegMSE(LS, binar, 0.0); //MSE
895        case Fitting.RegMSE:
896          return calculateRegMSE(LS, binar, Penalty); //RegMSE
897        case Fitting.ElasticNet:
898          return calculateElasticNet(LS, coeff, binar); // Elastic Net
899        default:
900          return calculateElasticNet(LS, coeff, binar);
901      }
902    }
903
904    private double calculateArithmeticMean() {
905      double arithmeticMean = 0.0;
906      for (int i = 0; i < dataMatrix.Rows; i++) {
907        arithmeticMean = arithmeticMean + dataMatrix[i, dataMatrix.Columns - 1];
908      }
909      return arithmeticMean / dataMatrix.Rows;
910    }
911
912    private double calculateSQT(double arithmeticMean) {
913      double SQT = 0.0;
914      for (int i = 0; i < dataMatrix.Rows; i++) {
915        SQT += Math.Pow((dataMatrix[i, dataMatrix.Columns - 1] - arithmeticMean), 2);
916      }
917      return SQT;
918    }
919
920    private static void doubleThrowInvOrNaN(double value, string valueName) {
921      if (double.IsInfinity(value) || double.IsNaN(value))
922        throw new InvalidProgramException(valueName + " is Infinity or NaN");
923    }
924
925
926    private void readCurve(ScatterPlot scatterPlot, string matrixName, string RowName, System.Drawing.Color color) {
927      var ds = ((IValueParameter<Dataset>)Parameters[matrixName]).Value;
928      DoubleMatrix dataMatrix = new DoubleMatrix(ds.ToArray(ds.VariableNames, Enumerable.Range(0, ds.Rows))); // TODO: performance / refactoring
929      var points = new Point2D<double>[dataMatrix.Rows];
930      for (int i = 0; i < dataMatrix.Rows; i++) {
931        points[i] = new Point2D<double>(dataMatrix[i, 1], dataMatrix[i, 3]);
932      }
933      ScatterPlotDataRow Curve = new ScatterPlotDataRow(RowName, RowName, points);
934      Curve.VisualProperties.Color = color;
935      scatterPlot.Rows.Add(Curve);
936    }
937
938    private void sampleCurve(ScatterPlot scatterPlot, string scatterPlotDataRow, double[] coeff, bool[] binar, double temp, double phip, int subdivisions, double maxStress) {
939      var points = new Point2D<double>[subdivisions + 1];
940      double phiMin = 0.0;
941      double phiMax = 0.70;
942      double step = (phiMax - phiMin) / subdivisions; // subdivisions > 0 !
943      double minStress = 0.0;
944
945      double[] dataPoint = new double[3] { temp, 0.0, phip };
946      //vars["sample0"] = new DoubleArray(dataPoint);
947      double[] dataPointNew = new double[4];
948      double x = 0.0;
949      //scatterPlot.Rows[scatterPlotDataRow].Points.Clear();
950      for (int i = 0; i <= subdivisions; i++) {
951        x = phiMin + (double)i * step;
952        dataPoint[0] = temp;
953        dataPoint[1] = x;
954        dataPoint[2] = phip;
955        //vars["sample1"] = new DoubleArray(dataPoint);
956        dataPoint = transformation.transform0(dataPoint);
957        //vars["sample2"] = new DoubleArray(dataPoint);
958        dataPointNew[0] = dataPoint[0];
959        dataPointNew[1] = dataPoint[1];
960        dataPointNew[2] = dataPoint[2];
961        dataPointNew[3] = 0.0;
962        points[i] = new Point2D<double>(x, evaluateContinuedFraction(coeff, binar, dataPointNew));
963        if (points[i].Y >= minStress && points[i].Y <= maxStress) {
964          scatterPlot.Rows[scatterPlotDataRow].Points.Add(points[i]);
965        }
966      }
967      //scatterPlot.Rows[ScatterPlotDataRow].Point2D.Add(points);
968    }
969  }
970}
971
Note: See TracBrowser for help on using the repository browser.