C# .Net Program to Find a Function Minimum

Using Marquardt's Method

by Namir Shammas

The following program uses the Marquardt method to find the minimum of a function.

Click here to download a ZIP file containing the project files for this program.

The program prompts you to either use the predefined default input values or to enter the following:

1. The initial guesses for the optimum point for each variable

2. The gradient norm tolerance.

3. The function tolerance

4. The maximum number of iterations.

5. The Alpha factor (a large value like 1E+4 is suitable).

6. The value for C1 and C2 factors (values for C1 = 0.1 and C2 = 10 are generally adequate)

In case you choose the default input values, the program displays these values and proceeds to find the optimum point. In the case you select being prompted, the program displays the name of each input variable along with its default value. You can then either enter a new value or simply press Enter to use the default value. This approach allows you to quickly and efficiently change only a few input values if you so desire.

The program displays the following final results:

1. The coordinates of the minimum point.

2. The minimum function value.

3. The number of iterations

The current code finds the minimum for the following function:

f(x1,x2) = x1 - x2 + 2 * x1 ^ 2 + 2 * x1 * x2 + x2 ^ 2

Using, for each variable, an initial value of 0,  initial step size of 0.1, minimum step size of 1e-7, and using a function tolerance of 1e-7. Here is the sample console screen:

Here is the listing for the main module.  The module contains several test functions: 

using System.Diagnostics;
using System.Data;
using System.Collections;
using Microsoft.VisualBasic;
using System.Collections.Generic;
using System;

namespace Optim_Marquardt1
{
	sealed class Module1
	{
		
		static public void Main()
		{
			int nNumVars = 2;
			double[] fX = new double[] { 0, 0 };
			double[] fParam = new double[] { 0.5, 0.5 };
			double[] fToler = new double[] { 0.000001, 0.000001 };
			int nIter = 0;
			int nMaxIter = 1000;
			double fEpsFx = 0.0000001;
			double fAlpha = 10000.0;
			double C1 = 0.1;
			double C2 = 10;
			int i;
			object fBestF;
			string sAnswer;
			string sErrorMsg = "";
			CMarquardt1 oOpt;
			MyFxDelegate MyFx = new MyFxDelegate(Fx3);
			SayFxDelegate SayFx = new SayFxDelegate(SayFx3);
			
			oOpt = new CMarquardt1();
			
			Console.WriteLine("Marquardt\'s Method for Optimization");
			Console.WriteLine("Finding the minimum of function:");
			Console.WriteLine(SayFx());
			Console.Write("Use default input values? (Y/N) ");
			sAnswer = Console.ReadLine();
			if (sAnswer.ToUpper() == "Y")
			{
				for (i = 0; i < nNumVars; i++)
				{
					Console.WriteLine("X({0}) = {1}", i + 1, fX[i]);
					Console.WriteLine("Tolerance({0}) = {1}", i + 1, fToler[i]);
				}
				Console.WriteLine("Function tolerance = {0}", fEpsFx);
				Console.WriteLine("Maxumum cycles = {0}", nMaxIter);
				Console.WriteLine("Alpha = {0}", fAlpha);
				Console.WriteLine("C1 = {0}", C1);
				Console.WriteLine("C2 = {0}", C2);
			}
			else
			{
				for (i = 0; i < nNumVars; i++)
				{
					fX[i] = GetIndexedDblInput("X", i + 1, fX[i]);
					fToler[i] = GetIndexedDblInput("Tolerance", i + 1, fToler[i]);
				}
				fEpsFx = GetDblInput("Function tolerance", fEpsFx);
				nMaxIter = GetIntInput("Maxumum cycles", nMaxIter);
				fAlpha = GetDblInput("Alpha", fAlpha);
				C1 = GetDblInput("C1", C1);
				C2 = GetDblInput("C1", C2);
			}
			
			Console.WriteLine("******** FINAL RESULTS *************");
            fBestF = oOpt.CalcOptim(nNumVars, ref fX, ref fParam, ref fToler, fEpsFx, nMaxIter, ref fAlpha, C1, C2, ref nIter, ref sErrorMsg, MyFx);
			if (sErrorMsg.Length > 0)
			{
				Console.WriteLine("** NOTE: {0} ***", sErrorMsg);
			}
			Console.WriteLine("Optimum at");
			for (i = 0; i < nNumVars; i++)
			{
				Console.WriteLine("X({0}) = {1}", i + 1, fX[i]);
			}
			Console.WriteLine("Function value = {0}", fBestF);
			Console.WriteLine("Number of iterations = {0}", nIter);
			Console.WriteLine();
			Console.Write("Press Enter to end the program ...");
			Console.ReadLine();
		}

        static public double GetDblInput(string sPrompt, double fDefInput)
        {
            string sInput;

            Console.Write("{0}? ({1}): ", sPrompt, fDefInput);
            sInput = Console.ReadLine();
            if (sInput.Trim(null).Length > 0)
            {
                return double.Parse(sInput);
            }
            else
            {
                return fDefInput;
            }
        }

        static public int GetIntInput(string sPrompt, int nDefInput)
        {
            string sInput;

            Console.Write("{0}? ({1}): ", sPrompt, nDefInput);
            sInput = Console.ReadLine();
            if (sInput.Trim(null).Length > 0)
            {
                return int.Parse(sInput);
            }
            else
            {
                return nDefInput;
            }
        }

        static public double GetIndexedDblInput(string sPrompt, int nIndex, double fDefInput)
        {
            string sInput;

            Console.Write("{0}({1})? ({2}): ", sPrompt, nIndex, fDefInput);
            sInput = Console.ReadLine();
            if (sInput.Trim(null).Length > 0)
            {
                return double.Parse(sInput);
            }
            else
            {
                return fDefInput;
            }
        }

        static public int GetIndexedIntInput(string sPrompt, int nIndex, int nDefInput)
        {
            string sInput;

            Console.Write("{0}({1})? ({2}): ", sPrompt, nIndex, nDefInput);
            sInput = Console.ReadLine();
            if (sInput.Trim(null).Length > 0)
            {
                return int.Parse(sInput);
            }
            else
            {
                return nDefInput;
            }
        }

        static public string SayFx1()
        {
            return "F(X) = 10 + (X(1) - 2) ^ 2 + (X(2) + 5) ^ 2";
        }

        static public double Fx1(int N, ref double[] X, ref double[] fParam)
        {
            return 10 + Math.Pow(X[0] - 2, 2) + Math.Pow(X[1] + 5, 2);
        }

        static public string SayFx2()
        {
            return "F(X) = 100 * (X(1) - X(2) ^ 2) ^ 2 + (X(2) - 1) ^ 2";
        }

        static public double Fx2(int N, ref double[] X, ref double[] fParam)
        {
            return Math.Pow(100 * (X[0] - X[1] * X[1]), 2) + Math.Pow((X[1] - 1), 2);
        }

        static public string SayFx3()
        {
            return "F(X) = X(1) - X(2) + 2 * X(1) ^ 2 + 2 * X(1) * X(2) + X(2) ^ 2";
        }

        static public double Fx3(int N, ref double[] X, ref double[] fParam)
        {
            return X[0] - X[1] + 2 * X[0] * X[0] + 2 * X[0] * X[1] + X[1] * X[1];
        }
	}
}

Notice that the user-defined functions have accompanying helper functions to display the mathematical expression of the function being optimized. For example, function Fx1 has the helper function SayFx1 to list the function optimized in Fx1. Please observe the following rules::

The program uses the following class to optimize the objective function:

using System.Diagnostics;
using System.Data;
using System.Collections;
using Microsoft.VisualBasic;
using System.Collections.Generic;
using System;

namespace Optim_Marquardt1
{
	public delegate double MyFxDelegate(int nNumVars, ref double[] fX, ref double[] fParam);
	public delegate string SayFxDelegate();
	
	public class CMarquardt1
	{
		
		MyFxDelegate m_MyFx;
		
		protected double MyFxEx(int nNumVars, ref double[] fX, ref double[] fParam, ref double[] fDeltaX, double fLambda)
		{
			int i;
			double[] fXX = new double[nNumVars];
			
			for (i = 0; i < nNumVars; i++)
			{
				fXX[i] = fX[i] + fLambda * fDeltaX[i];
			}
			
			return m_MyFx(nNumVars, ref fXX, ref fParam);
		}
		
		protected double FirstDeriv(int nNumVars, ref double[] fX, ref double[] fParam, int nIdxI)
		{
			double fXt, h, Fp, Fm;
			
			fXt = fX[nIdxI];
			h = 0.01 *(1 + Math.Abs(fXt));
			fX[nIdxI] = fXt + h;
			Fp = m_MyFx(nNumVars, ref fX, ref fParam);
			fX[nIdxI] = fXt - h;
			Fm = m_MyFx(nNumVars, ref fX, ref fParam);
			fX[nIdxI] = fXt;
			return (Fp - Fm) / 2 / h;
		}
		
		protected double SecondDeriv(int nNumVars, ref double[] fX, ref double[] fParam, int nIdxI, int nIdxJ)
		{
			double fXt, fYt, fHX, fHY, F0, Fp, Fm;
			double Fpp, Fmm, Fpm, Fmp, fResult;
			
			// calculate second derivative?
			if (nIdxI == nIdxJ)
			{
				F0 = m_MyFx(nNumVars, ref fX, ref fParam);
				fXt = fX[nIdxI];
				fHX = 0.01 *(1 + Math.Abs(fXt));
				fX[nIdxI] = fXt + fHX;
				Fp = m_MyFx(nNumVars, ref fX, ref fParam);
				fX[nIdxI] = fXt - fHX;
				Fm = m_MyFx(nNumVars, ref fX, ref fParam);
				fX[nIdxI] = fXt;
				fResult = (Fp - 2 * F0 + Fm) /Math.Pow(fHX, 2);
			}
			else
			{
				fXt = fX[nIdxI];
				fYt = fX[nIdxJ];
				fHX = 0.01 *(1 + Math.Abs(fXt));
				fHY = 0.01 *(1 + Math.Abs(fYt));
				// calculate Fpp
				fX[nIdxI] = fXt + fHX;
				fX[nIdxJ] = fYt + fHY;
				Fpp = m_MyFx(nNumVars, ref fX, ref fParam);
				// calculate Fmm
				fX[nIdxI] = fXt - fHX;
				fX[nIdxJ] = fYt - fHY;
				Fmm = m_MyFx(nNumVars, ref fX, ref fParam);
				// calculate Fpm
				fX[nIdxI] = fXt + fHX;
				fX[nIdxJ] = fYt - fHY;
				Fpm = m_MyFx(nNumVars, ref fX, ref fParam);
				// calculate Fmp
				fX[nIdxI] = fXt - fHX;
				fX[nIdxJ] = fYt + fHY;
				Fmp = m_MyFx(nNumVars, ref fX, ref fParam);
				fX[nIdxI] = fXt;
				fX[nIdxJ] = fYt;
				fResult = (Fpp - Fmp - Fpm + Fmm) /(4 * fHX * fHY);
			}
			
			return fResult;
		}
		
		
		protected void GetFirstDerives(int nNumVars, ref double[] fX, ref double[] fParam, ref double[] fFirstDerivX)
		{
			
			int i;
			
			for (i = 0; i < nNumVars; i++)
			{
				fFirstDerivX[i] = FirstDeriv(nNumVars, ref fX, ref fParam, i);
			}
		}
		
		protected void GetSecondDerives(int nNumVars, ref double[] fX, ref double[] fParam, ref double[,] fSecondDerivX)
		{
			
			int i, j;
			
			for (i = 0; i < nNumVars; i++)
			{
				for (j = 0; j < nNumVars; j++)
				{
					fSecondDerivX[i, j] = SecondDeriv(nNumVars, ref fX, ref fParam, i, j);
				}
			}
		}
		
		protected bool LinSearch_DirectSearch(int nNumVars, ref double[] fX, ref double[] fParam, ref double fLambda, ref double[] fDeltaX, double fInitStep, double fMinStep)
		{
			double F1, F2;

            F1 = MyFxEx(nNumVars, ref fX, ref fParam, ref fDeltaX, fLambda);
			
			do
			{
                F2 = MyFxEx(nNumVars, ref fX, ref fParam, ref fDeltaX, fLambda + fInitStep);
				if (F2 < F1)
				{
					F1 = F2;
					fLambda += fInitStep;
				}
				else
				{
                    F2 = MyFxEx(nNumVars, ref fX, ref fParam, ref fDeltaX, fLambda - fInitStep);
					if (F2 < F1)
					{
						F1 = F2;
						fLambda -= fInitStep;
					}
					else
					{
						// reduce search step size
						fInitStep /= 10;
					}
				}
			} while (!(fInitStep < fMinStep));
			
			return true;
			
		}
		
		public double CalcOptim(int nNumVars, ref double[] fX, ref double[] fParam, ref double[] fToler, double fEpsFx, int nMaxIter, ref double fAlpha, double C1, double C2, ref int nIter, ref string sErrorMsg, MyFxDelegate MyFx)
		{
			
			const double ALPHA_MAX = 1.0E+100;
			
			int i;
			double F1;
			double F2 = 0;
			double fNorm;
			int nInnerLoopIter;
			bool bStop;
			double[] fX2 = new double[nNumVars];
			double[] g = new double[nNumVars];
			double[] G2 = new double[nNumVars];
			double[] DeltaX = new double[nNumVars];
			double[,] j = new double[nNumVars, nNumVars];
			double[,] J2 = new double[nNumVars, nNumVars];
			double[,] fIdenMat = new double[nNumVars, nNumVars];
			double[,] fIdenMat2 = new double[nNumVars, nNumVars];
			int[] nIndex = new int[nNumVars];
			
			try
			{
				m_MyFx = MyFx;
				
				// validate iterations parameters
				if (C1 <= 0 || C1 >= 1)
				{
					C1 = 0.1;
				}
				if (C2 <= 1)
				{
					C2 = 10;
				}
				
				F2 = MyFx(nNumVars, ref fX, ref fParam);
				
				nIter = 1;
				do
				{
					nIter++;
					if (nIter > nMaxIter)
					{
						sErrorMsg = "Reached maximum iterations limit";
						break;
					}
					
					// copy last Fx value
					F1 = F2;

                    GetFirstDerives(nNumVars, ref fX, ref fParam, ref g);
                    MatrixLibVb.DuplicateVector(ref g, ref G2);
					
					// test if gradient is shallow enough
					fNorm = 0;
					for (i = 0; i < nNumVars; i++)
					{
						fNorm += Math.Pow(g[i], 2);
					}
					fNorm = Math.Sqrt(fNorm);
					if (fNorm < fEpsFx)
					{
						break;
					}
					
					MatrixLibVb.DuplicateVector(ref fX, ref fX2); // copy fX
					// get matrix j
					GetSecondDerives(nNumVars, ref fX, ref fParam, ref j);
					// create identitty matrix
					MatrixLibVb.MatIdentity(ref fIdenMat);
					// initialize inner-loop counter
					nInnerLoopIter = 0;
					
					do
					{
						nInnerLoopIter++;
						if (nInnerLoopIter > nMaxIter)
						{
							return 0;
						}
						
						MatrixLibVb.DuplicateVector(ref G2, ref g);
                        MatrixLibVb.MatMultVal(ref fIdenMat, ref fAlpha, ref fIdenMat2);
                        MatrixLibVb.MatAddMat(ref j, ref fIdenMat2, ref J2);
						MatrixLibVb.MV_LUDecomp(ref J2, ref nIndex, nNumVars);
						MatrixLibVb.MV_LUBackSubst(ref J2, ref nIndex, nNumVars, ref g);
						for (i = 0; i < nNumVars; i++)
						{
							DeltaX[i] = g[i];
							fX[i] -= DeltaX[i];
						}
						
						F2 = MyFx(nNumVars, ref fX, ref fParam);
						
						if (F2 < F1)
						{
							fAlpha = C1 * fAlpha;
							bStop = true;
						}
						else
						{
							fAlpha = C2 * fAlpha;
							MatrixLibVb.DuplicateVector(ref fX2, ref fX); // restore array fX
							bStop = false;
							if (fAlpha >= ALPHA_MAX)
							{
								return F2;
							}
						}
					} while (!bStop);
					
				} while (true);
			}
			catch (Exception ex)
			{
				sErrorMsg = "Calculation error: " + ex.Message;
			}
            return F2;

		}
		
	}
	
}

BACK

Copyright (c) Namir Shammas. All rights reserved.