« Previous « Start » Next »
3 QP Problem
The general formulation in TOMNET for a quadratic programming
problem is:
|
|
|
|
|
s/t |
xL |
≤ |
x |
≤ |
xU |
bL |
≤ |
A x |
≤ |
bU |
|
|
(3) |
where
c,
x,
xL,
xU Rn,
F Rn
× n,
A Rm1 × n, and
bL,
bU
Rm1. Equality constraints are defined by setting
the lower bound equal to the upper bound, i.e. for constraint
i:
bL(
i) =
bU(
i). Fixed variables are handled the same way.
Example problem:
|
|
f(x)=4x12+1x1x2+4x22+3x1−4x2 |
|
|
s/t |
x1+x2 ≤ 5 |
x1−x2 = 0 |
x1 ≥ 0 |
x2 ≥ 0 |
|
|
(4) |
The following code (maintained from the solution file:
quickguide/qpQG/qpQG.sln) defines the problem in TOMNET.
using System;
using TOMNET;
namespace TOMNET
{
/// <summary>
/// Quick guide class for quadratic problem.
/// </summary>
static public class qpQG
{
/// <summary>
/// Creates a simple quadratic problem.
/// </summary>
/// <returns>A simple quadratic problem.</returns>
static public QuadraticProblem getQpQG()
{
//
// Name of the problem.
//
string Name = "QP Example";
//
// Linear and quadratic part of the objective function
// 1/2 * x' * F * x + c' * x
//
double[] F = new double[4] { 8, 1, 1, 8 };
double[] c = new double[2] { 3, -4 };
//
// For use in bounds.
//
double Inf = double.PositiveInfinity;
//
// linear constraints and lower and upper bounds.
//
double[] A = new double[4] { 1, 1, 1, -1 };
double[] b_L = new double[2] { -Inf, 0 };
double[] b_U = new double[2] { 5, 0 };
//
// Lower and upper bounds for decision variables as well
// as the starting point for the solver.
//
double[] x_L = new double[2] { 0, 0 };
double[] x_U = new double[2] { Inf, Inf };
double[] x_0 = new double[2] { 0, 1 };
//
// Create and return the quadratic problem
//
return QuadraticProblem.qpAssign(F, c, A, b_L, b_U, x_L, x_U, x_0,
Name);
}
/// <summary>
/// Testprogram for qpQG.
/// </summary>
static void Main()
{
//
// Create a problem, solver and result instance.
//
QuadraticProblem Prob = qpQG.getQpQG();
SNOPT solver = new SNOPT();
Result result;
//
// Define a print file for SNOPT and a problem Name
//
Prob.Name += " (solved with SNOPT)";
string printfilename = "SnoptQP.txt";
solver.Options.PrintFile = printfilename;
//
// Solve the problem
//
solver.Solve(Prob, out result);
//
// Get the solution results
//
double[] solution = result.x_k;
double objective = result.f_k;
//
// Display the solution on the console.
//
Console.WriteLine(" * * * * * * * *");
Console.WriteLine(" * Solved a QP problem");
Console.WriteLine(" * Printfile: " + printfilename);
Console.WriteLine(" * Objective: {0}", objective);
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], solution[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));
}
}
}
}
The solution file
qpQG.sln can be compiled to build an exe
file. The executable can be run from the command prompt, which
should display the following output results:
« Previous « Start » Next »