Sunday, April 15, 2018

C#: A Linear Regression example with the NelderMead solver from Microsoft Solver Foundation (MSF)


Given that you installed the Microsoft Solver Foundation (msdn.microsoft.com)
and referenced the MSF dll (Microsoft.Solver.Foundation.dll) in your VisualStudio Project here is a simple code to test the NonLinear solver (aka NelderMead solver)  that ships with the library.

I hope Microsoft will revive this nice library in the future (at the moment the project is abandoned :( )

// here is the code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SolverFoundation.Common;
using Microsoft.SolverFoundation.Services;
using Microsoft.SolverFoundation.Solvers;

namespace LinearRegressionMSF
{
    class Program
    {

    // We want to get a this line's coefficients back: y = 11.22*x + 77.99
   
        static double[] xData = new double[] { 0, 1, 2, 3, 4, 5, 6, 7 };
        static double[] yData = new double[] { 0 * 11.22 + 77.99, 1 * 11.22 + 77.99, 2 * 11.22 + 77.99, 3 * 11.22 + 77.99, 4 * 11.22 + 77.99, 5 * 11.22 + 77.99, 6 * 11.22 + 77.99, 7 * 11.22 + 77.99 };


        static void Main(string[] args)
        {

            LinearRegressionWithNelderMeadSolver();

            Console.ReadKey();
        }



        private static void LinearRegressionWithNelderMeadSolver()
        {
            // initial values for the two parameters
            double[] pInitial = new double[] { 1, 1 };
           
            // lower and upper limits for the two paramenters           
            double[] pLower = new double[] { -100, -100 };
            double[] pUpper = new double[] { 100, 100 };

            var solution = NelderMeadSolver.Solve(SumOfSquares, pInitial, pLower, pUpper);

            Console.WriteLine(solution.Result);
            Console.WriteLine("solution = {0}", solution.GetSolutionValue(0));
            Console.WriteLine("x = {0}", solution.GetValue(1));
            Console.WriteLine("y = {0}", solution.GetValue(2));
        }


        private static double SumOfSquares(double[] p)
        {
            double q = p[0];
            double m = p[1];

            double dSumOfSquares = 0;

            for (int i = 0; i < xData.Length; ++i)
            {
                dSumOfSquares += Math.Pow(yData[i] - m * xData[i] - q, 2);
            }

            return dSumOfSquares;
        }
    }

}

No comments:

Post a Comment

Your comment will be visible after approval.