C# Nonlinear Programming Example

← All NMath Code Examples

 

using System;

using CenterSpace.NMath.Core;


namespace CenterSpace.NMath.Examples.CSharp
{
  class NonlinearProgrammingExample
  {
    static void Main( string[] args )
    {
      DoubleFunctional objective = new ObjectiveFunction();
      var problem = new NonlinearProgrammingProblem( objective );
    
      // Dimensionality of the objective and constraint function domains
      int xDimension = 2;

      // Add constraint x0*x1 - x0 -x1 <= -1.5
      problem.AddUpperBoundConstraint( xDimension, ( DoubleVector x ) => x[0] * x[1] - x[0] - x[1], -1.5 );

      // Add constraint x0*x1 >= -10
      problem.AddLowerBoundConstraint( xDimension, ( DoubleVector x ) => x[0] * x[1], -10.0 );


      // Set some options on the solver by creating an Options object and setting
      // some properties, then use it to construct the solver instance.
      var solverOptions = new ActiveSetLineSearchSQP.Options();
      solverOptions.StepDirectionTolerance = 1e-8;
      solverOptions.FunctionChangeTolerance = 1e-6;

      // Since our constraints are nearly linear (one quadratic term) well
      // use the simpler constant step size.
      solverOptions.StepSizeCalculator = new ConstantSQPStepSize( 1 );

      // Create the solver with the above options and use it to solve the problem.
      var solver = new ActiveSetLineSearchSQP( solverOptions );
      // Initial solution guess
      var x0 = new DoubleVector( -1.0, 1.0 );
      bool success = solver.Solve( problem, x0 );

      Console.WriteLine();

      Console.WriteLine( success ? "Solver was successful" : "Solver encountered a problem." );
      Console.WriteLine( "Termination status = " + solver.SolverTerminationStatus );
      Console.WriteLine( "X = " + solver.OptimalX );
      Console.WriteLine( "f(x) = " + solver.OptimalObjectiveFunctionValue );
      Console.WriteLine( "Iterations = " + solver.Iterations );

      Console.WriteLine();
      Console.WriteLine( "Press Enter Key" );
      Console.Read();
    }

    /// <summary>
    /// f(x) = exp(x0)*(4*x0^2 + 2*x1^2 + 4*x0*x1 + 2*x1 + 1)
    /// </summary>
    class ObjectiveFunction : DoubleFunctional
    {
      /// <summary>
      /// Constructor. Must initialize the base class with dimension
      /// of the domain, 2 in this case.
      /// </summary>
      public ObjectiveFunction()
        : base( 2 )
      {
        ;
      }

      /// <summary>
      /// Evaluate the objective function at a point x.
      /// </summary>
      /// <param name="x">The point at which to evaluate the function.</param>
      /// <returns>The value of the objective function.</returns>
      public override double Evaluate( DoubleVector x )
      {
        double x0 = x[0];
        double x1 = x[1];
        return Math.Exp( x0 ) * ( 4 * x0 * x0 + 2 * x1 * x1 + 4 * x0 * x1 + 2 * x1 + 1 );
      }

      /// <summary>
      /// Returns the gradient of the objective function at a point x.
      /// </summary>
      /// <param name="x">The point at which to evaluate the gradient.</param>
      /// <param name="grad">Vector into which to place the gradient values.</param>
      public override void Gradient( DoubleVector x, DoubleVector grad )
      {
        double x0 = x[0];
        double x1 = x[1];
        double ex0 = Math.Exp( x0 );
        grad[0] = ex0 * ( 4 * x0 * x0 + 2 * x1 * x1 + 4 * x0 * x1 + 2 * x1 + 1 ) + ex0 * ( 8 * x0 + 4 * x1 );
        grad[1] = ex0 * ( 4 * x0 + 4 * x1 + 2 );
      }
    }
  }
}

← All NMath Code Examples
Top