← All NMath Code Examples
using System;
using CenterSpace.NMath.Core;
namespace CenterSpace.NMath.Examples.CSharp
{
/// <summary>
/// Maximize 5*x0 + 3*x1
/// Subject to:
/// x0 - x1 <= 5
/// 0 <= 2*x0 + x1 <= 25
/// x0 >= 0
/// x1 <= 10
/// </summary>
public class MixedIntLinearProgrammingExample
{
static void Main( string[] args )
{
// Create the mixed integer programming problem for maximizing the
// objective function 5*x0 + 3*x1. The objective function is specified
// by a vector of coefficients.
var objectiveCoefficients = new DoubleVector( 5.0, 3.0 );
var problem = new MixedIntegerLinearProgrammingProblem( objectiveCoefficients );
// Add the constraints. The linear constraints are specified by the vector
// of the their coefficients.
// x0 -x1 <= 5
problem.AddUpperBoundConstraint( new DoubleVector( 1.0, -1.0 ), 5.0 );
// 0 <= 2*x0 + x1 <= 25
problem.AddConstraint( new DoubleVector( 2.0, 1.0 ), 0.0, 25.0 );
// Add the variable bounds. The index of the variable is specified in
// the first argument and the bound in the second.
// x0 >= 0
problem.AddLowerBound( 0, 0.0 );
// x1 <= 10
problem.AddUpperBound( 1, 10.0 );
// Finally, the first variable x0 is constrained to be integer valued.
// x0 integral
problem.AddIntegralConstraint( 0 );
// Create the linear programming problem solver object with default
// parameters and use it so solve the problem.
var solver = new DualSimplexSolver();
solver.Solve( problem );
// Write out the results.
Console.WriteLine( "Result: " + solver.Result );
Console.WriteLine( "Optimal X: " + solver.OptimalX );
Console.WriteLine( "Optimal Objective Function Value: " + solver.OptimalObjectiveFunctionValue );
Console.WriteLine();
Console.WriteLine( "Press Enter Key" );
Console.Read();
}
}
}
← All NMath Code Examples