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