1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Reflection;
|
---|
5 | using System.Text;
|
---|
6 | using System.Threading.Tasks;
|
---|
7 |
|
---|
8 | namespace HeuristicLab.Algorithms.DataAnalysis.ContinuedFractionRegression {
|
---|
9 | public class NumUtilsTests {
|
---|
10 | /// <summary>
|
---|
11 | /// Test to see if we can fit a parabola
|
---|
12 | /// </summary>
|
---|
13 | public static void SimplexTest1() {
|
---|
14 | Console.WriteLine("Starting SimplexTest1");
|
---|
15 | SimplexConstant[] constants = new SimplexConstant[] { new SimplexConstant(3, 1), new SimplexConstant(5, 1) };
|
---|
16 | double tolerance = 1e-6;
|
---|
17 | int maxEvals = 1000;
|
---|
18 | ObjectiveFunctionDelegate objFunction = new ObjectiveFunctionDelegate(_objFunction1);
|
---|
19 | RegressionResult result = NelderMeadSimplex.Regress(constants, tolerance, maxEvals, objFunction);
|
---|
20 | _printResult(result);
|
---|
21 | }
|
---|
22 |
|
---|
23 | private static double _objFunction1(double[] constants) {
|
---|
24 | double a = 5;
|
---|
25 | double b = 10;
|
---|
26 |
|
---|
27 | Console.Write("Called with a={0} b={1}", constants[0], constants[1]);
|
---|
28 |
|
---|
29 | // evaluate it for some selected points, with a bit of noise
|
---|
30 | double ssq = 0;
|
---|
31 | var r = new System.Random();
|
---|
32 | for (double x = -10; x < 10; x += .1) {
|
---|
33 | double yTrue = a * x * x + b * x + r.NextDouble();
|
---|
34 | double yRegress = constants[0] * x * x + constants[1] * x;
|
---|
35 | ssq += Math.Pow((yTrue - yRegress), 2);
|
---|
36 | }
|
---|
37 | Console.WriteLine(" SSQ={0}", ssq);
|
---|
38 | return ssq;
|
---|
39 | }
|
---|
40 |
|
---|
41 | /// <summary>
|
---|
42 | /// Test on the Rosenbrock function
|
---|
43 | /// </summary>
|
---|
44 | public static void SimplexTest2() {
|
---|
45 | Console.WriteLine("\n\nStarting SimplexTest2");
|
---|
46 |
|
---|
47 | // we are regressing for frequency, amplitude, and phase offset
|
---|
48 | SimplexConstant[] constants = new SimplexConstant[] { new SimplexConstant(-1.2, .1), new SimplexConstant(1, .1) };
|
---|
49 | double tolerance = 1e-10;
|
---|
50 | int maxEvals = 1000;
|
---|
51 | ObjectiveFunctionDelegate objFunction = new ObjectiveFunctionDelegate(_objFunction2);
|
---|
52 | RegressionResult result = NelderMeadSimplex.Regress(constants, tolerance, maxEvals, objFunction);
|
---|
53 | _printResult(result);
|
---|
54 | }
|
---|
55 |
|
---|
56 | private static double _objFunction2(double[] constants) {
|
---|
57 | Console.Write("Called with x1={0} x2={1} ", constants[0], constants[1]);
|
---|
58 | double err = 100 * Math.Pow((constants[1] - constants[0] * constants[0]), 2) + Math.Pow((1 - constants[0]), 2);
|
---|
59 | Console.WriteLine(" err={0}", err);
|
---|
60 |
|
---|
61 | return err;
|
---|
62 | }
|
---|
63 |
|
---|
64 | private static void _printResult(RegressionResult result) {
|
---|
65 | // a bit of reflection fun, why not...
|
---|
66 | PropertyInfo[] properties = result.GetType().GetProperties();
|
---|
67 | foreach (PropertyInfo p in properties) {
|
---|
68 | Console.WriteLine(p.Name + ": " + p.GetValue(result, null).ToString());
|
---|
69 | }
|
---|
70 | }
|
---|
71 | }
|
---|
72 | }
|
---|