« Previous « Start » Next »
2 LP Problem
The general formulation in TOMNET for a linear programming problem
is:
|
|
f(x) = cT x |
|
|
s/t |
xL |
≤ |
x |
≤ |
xU |
bL |
≤ |
A x |
≤ |
bU |
|
|
(1) |
where
c,
x,
xL,
xU Rn,
A Rm1 × n, and
bL,
bU Rm1. Equality constraints are defined by setting the lower bound to the upper bound.
Example problem:
|
|
f(x1,x2) = −7x1−5x2 |
|
|
s/t |
x1+2x2 |
≤ |
6 |
4x1+x2 |
≤ |
12 |
x1,x2 |
≥ |
0 |
|
|
(2) |
The following code (maintained from the solution file:
quickguide/lpQG/lpQG.sln) defines the problem in TOMNET.
Observe that there are three ways to define the linear constraints,
firstly as a dense matrix row-wise, secondly by giving the indices
for the entries, or by using the internal sparse matrix format.
using System;
using TOMNET;
namespace TOMNET
{
/// <summary>
/// Quick guide class for linear problem.
/// </summary>
static public class lpQG
{
/// <summary>
/// Creates a simple linear problem.
/// </summary>
/// <returns>
/// A simple linear problem.
/// </returns>
static public LinearProblem getLpQG()
{
//
// Number of decision variables, linear constraints and
// nonzeros in linear constraints matrix.
//
const int N = 2;
const int mLin = 2;
const int nnz = 4;
//
// Problem Name.
//
string Name = "lpQG";
//
// Linear objective function entries.
//
double[] c = new double[N] { -7, -5 };
//
// For use in bounds.
//
double Inf = double.PositiveInfinity;
//
// Lower and upper bounds for linear constraints.
//
double[] b_L = new double[mLin] { -Inf, -Inf };
double[] b_U = new double[mLin] { 6, 12 };
//
// Lower and upper bounds for decision variables as well
// as the starting point for the solver.
//
double[] x_L = new double[N] { 0, 0 };
double[] x_U = new double[N] { Inf, Inf };
double[] x_0 = new double[N] { 0, 0 };
//
// Create linear constraints.
//
//
// Method one (dense matrix with linear constraints)
// double[] A = new double[N*mLin] { 1, 2, 4, 1 };
//
// Method two (the standard sparse user format)
// double[] Av = new double[nnz] { 1, 4, 2, 1 };
// int[] Ai = new int[nnz] { 0, 1, 0, 1 };
// int[] Aj = new int[nnz] { 0, 0, 1, 1 };
// Sparse A = new Sparse(mLin, N, nnz, Ai, Aj, Av, 0);
//
// Method three (the internal representation)
double[] Apr = new double[nnz] { 1, 4, 2, 1 };
int[] Air = new int[nnz] { 0, 1, 0, 1 };
int[] Ajc = new int[N + 1] { 0, 2, 4 };
Sparse A = new Sparse(mLin, N, nnz, Air, Ajc, Apr);
//
// Create the linear problem.
//
LinearProblem Prob = new LinearProblem(c, A, b_L, b_U, x_L, x_U,
x_0, Name, double.NegativeInfinity, null, null);
//
// Return the problem.
//
return Prob;
}
/// <summary>
/// Testprogram lpQG.
/// </summary>
static void Main()
{
//
// Create a problem, solver and result instance.
//
LinearProblem Prob = lpQG.getLpQG();
SNOPT solver = new SNOPT();
Result result;
//
// Define a print file for SNOPT.
//
string printfilename = "SnoptLP.txt";
solver.Options.PrintFile = printfilename;
//
// Solve the problem.
//
solver.Solve(Prob, out result);
//
// Display the solution on the console.
//
Console.WriteLine(" * * * * * * * *");
Console.WriteLine(" * Solved an LP problem");
Console.WriteLine(" * Printfile: " + printfilename);
Console.WriteLine(" * Objective: {0,10:g3}", result.f_k);
Console.WriteLine(" * Solution");
Console.WriteLine(" x_L[i] <= x_k[i] <= x_U[i]");
for (int i = 0; i < Prob.N; i++)
{
Console.WriteLine(" {0,10:g3} <= {1,10:g3} <= {2,10:g3} ",
Prob.x_L[i], result.x_k[i], Prob.x_U[i]);
}
double[] Ax = Prob.A.Evaluate(result.x_k);
Console.WriteLine(" * Linear constraints");
Console.WriteLine(" b_L[i] <= (A*x)[i] <= b_U[i]");
for (int i = 0; i < Prob.mLin; i++)
{
Console.WriteLine(" {0,10:g3} <= {1,10:g3} <= {2,10:g3}",
Prob.A.GetLowerBound(i), Ax[i], Prob.A.GetUpperBound(i));
}
}
}
}
After opening
lpQG.sln build the solution (results in an exe
file). The executable can be run from the command prompt, which
should display the following output results:
« Previous « Start » Next »