VB Binary Nonlinear Programming Example

← All NMath Code Examples

 

Imports System

Imports CenterSpace.NMath.Core


Namespace CenterSpace.NMath.Examples.VisualBasic

  <summary>
  A .NET example in C# showing how to solve a nonlinear programming problem
  that has some binary and integral constraints on its solution variables.
  </summary>
  Module BinaryNonlinearProgrammingExample

    The problem is:
    min Z = x0 + 1.5*x1 + 0.5*x2 + x3^2 + x4^2
    Subject to:
    (x3 - 2)^2 - x4 <= 0,
    x3 - 2*x0 => 0,
    x3 - x4 - 4*(1-x1) <= 0,
    x3 - (1 - x0) >= 0,
    x4 - x1 >= 0,
    x3 + x4 >= 3*x2,
    x0 + x1 + x2 >= 1,
    0 <= x3 <= 4, 
    0 <= x4 <= 4,
    In addition, x0, and x1 have binary constraints, and x2
    has an integral constraint:
    x0, x1, = 0, 1
    x2 must be an integer.
    Sub Main()

      We have 5 variables, so 5 is the x, or domain, dimension. We need this
      number when creating delegate functions with lambda expressions.
      Dim XDim As Integer = 5

      Create our objective function from a delegate and use it in the mixed 
      integer problem constructor.
      Dim Z As New DoubleFunctionalDelegate(XDim, AddressOf Objective)

      Dim Problem As New MixedIntegerNonlinearProgrammingProblem(Z)

      Add the constraints to our problem.
      (x3 - 2)^2 - x4 <= 0
      Problem.AddUpperBoundConstraint(XDim, AddressOf Constraint1, 0.0)

      x3 - 2*x0 => 0
      Problem.AddLowerBoundConstraint(XDim, AddressOf Constraint2, 0.0)

      x3 - x4 - 4*(1-x1) <= 0
      or
      x3 - x4 + 4*x1 <= 4
      Problem.AddUpperBoundConstraint(XDim, AddressOf Constraint3, 0.0)

      x3 - (1 - x0) >= 0
      or
      x3 + x0 >= 1
      Problem.AddLowerBoundConstraint(XDim, AddressOf Constraint4, 0.0)

      x4 - x1 >= 0
      Problem.AddLowerBoundConstraint(XDim, AddressOf Constraint5, 0.0)

      x3 + x4 >= 3*x2
      or
      x3 + x4 - 3*x2 >= 0
      Problem.AddLowerBoundConstraint(XDim, AddressOf Constraint6, 0.0)

      x0 + x1 + x2 >= 1
      Problem.AddLowerBoundConstraint(XDim, AddressOf Constraint7, 1.0)

      Add the variable bounds.
      0 <= x3 <= 4
      problem.AddBounds(3, 0.0, 4.0)

      0 <= x4 <= 4
      problem.AddBounds(4, 0.0, 4.0)

      Add binary constraints for x0 and x1 (variable indices
      0 and 1).
      Problem.AddBinaryConstraint(0, 1)

      And the integer constraint for x2 (variable index 2).
      Problem.AddIntegralConstraint(2)

      Construct the solver and solver parameter objects.
      Dim Solver As New StochasticHillClimbingSolver()

      The solver parameters will be passed to the solver when
      we solve. Here we specify:
      
      Minimize the objective function,
      and
      Attempt to solve for at most 10 seconds (10000 milliseconds).
      If the solver fails to converge in 10 seconds, it will return
      and the Result field of the solver will have the value
      SolverInterrupted.
      Dim SolverParameters As New StochasticHillClimbingParameters
      SolverParameters.Minimize = True
      SolverParameters.TimeLimitMilliSeconds = 10000

      Solve the problem with Imports  the solver parameters and print
      out the results.
      Solver.Solve(Problem, SolverParameters)
      Console.WriteLine("Result: " & Solver.Result.ToString())
      Console.WriteLine()
      Console.WriteLine("Optimal Solution: " & Solver.OptimalX.ToString())
      Console.WriteLine("Optimal Function Value: " & Solver.OptimalObjectiveFunctionValue)

      Note that a variable with an integer constraint may not get
      an exact integer value in the solution. In this case x2, which
      has an integral constraint gets a value of about 6.48e-9
      which is very, very close to 0. In general if the
      solver finds an optimal solution to a problem with integral 
      constraints, the integrally constrained components of the 
      solution should be rounded to the closest integer - or
      cast to ints.

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

    End Sub

    Function Objective(ByVal X As DoubleVector) As Double
      Return X(0) + 1.5 * X(1) + 0.5 * X(2) + X(3) * X(3) + X(4) * X(4)
    End Function

    Function Constraint1(ByVal x As DoubleVector) As Double
      Return Math.Pow(x(3) - 2.0, 2) - x(4)
    End Function

    Function Constraint2(ByVal X As DoubleVector) As Double
      Return X(3) - 2.0 * X(0)
    End Function

    Function Constraint3(ByVal x As DoubleVector) As Double
      Return x(3) - x(4) - 4.0 * (1.0 - x(1))
    End Function

    Function Constraint4(ByVal X As DoubleVector) As Double
      Return X(3) - (1.0 - X(0))
    End Function

    Function Constraint5(ByVal x As DoubleVector) As Double
      Return x(4) - x(1)
    End Function

    Function Constraint6(ByVal X As DoubleVector) As Double
      Return X(3) + X(4) - 3.0 * X(2)
    End Function

    Function Constraint7(ByVal x As DoubleVector) As Double
      Return x(0) + x(1) + x(2)
    End Function

  End Module
End Namespace


← All NMath Code Examples
Top