6.1 Linear Optimization

The simplest optimization problem is a purely linear problem. A linear optimization problem (see also Sec. 12.1 (Linear Optimization)) is a problem of the following form:

Minimize or maximize the objective function

j=0n1cjxj+cf

subject to the linear constraints

lkcj=0n1akjxjukc,k=0,,m1,

and the bounds

ljxxjujx,j=0,,n1.

The problem description consists of the following elements:

  • m and n — the number of constraints and variables, respectively,

  • x — the variable vector of length n,

  • c — the coefficient vector of length n

    c=[c0cn1],
  • cf — fixed term in the objective,

  • A — an m×n matrix of coefficients

    A=[a0,0a0,(n1)a(m1),0a(m1),(n1)],
  • lc and uc — the lower and upper bounds on constraints,

  • lx and ux — the lower and upper bounds on variables.

Please note that we are using 0 as the first index: x0 is the first element in variable vector x.

6.1.1 Example LO1

The following is an example of a small linear optimization problem:

(6.1)maximize3x0+1x1+5x2+1x3subject to3x0+1x1+2x2=30,2x0+1x1+3x2+1x315,2x1+3x325,

under the bounds

0x0,0x110,0x2,0x3.

Example: Linear optimization using msklpopt

A linear optimization problem such as (6.1) can be solved using the msklpopt function. The first step in solving the example is to setup the data for problem (6.1) i.e. the c, A, etc. Afterwards the problem is solved using an appropriate call to msklpopt.

Listing 6.1 Script implementing problem (6.1) using msklpopt. Click here to download.
function lo1()

c     = [3 1 5 1]';
a     = [[3 1 2 0];[2 1 3 1];[0 2 0 3]];
blc   = [30 15  -inf]';
buc   = [30 inf 25 ]';
blx   = zeros(4,1);
bux   = [inf 10 inf inf]';

[res] = msklpopt(c,a,blc,buc,blx,bux,[],'maximize');
sol   = res.sol;

% Interior-point solution.

sol.itr.xx'      % x solution.
sol.itr.sux'     % Dual variables corresponding to buc.
sol.itr.slx'     % Dual variables corresponding to blx.

% Basic solution.

sol.bas.xx'      % x solution in basic solution.

Please note that:

  • Infinite bounds are specified using -inf and inf. Moreover, using [] for bux, buc, blx or blc means there are no bounds of the corresponding type.

  • Retrieving different solution types is discussed in Sec. 7.1 (Accessing the solution).

Example: Linear optimization using mosekopt

The function msklpopt is just a wrapper around the mosekopt, which is the main interface to MOSEK and is the only choice for more complicated problems, for instance with conic constraints. We demonstrate how to solve (6.1) directly with mosekopt. The following MATLAB code demonstrate how to set up the prob structure for the example (6.1) and solve the problem using mosekopt.

Listing 6.2 Script implementing problem (6.1) using mosekopt. Click here to download.
function lo2()
clear prob;

% Specify the c vector.
prob.c  = [3 1 5 1]';

% Specify a in sparse format.
subi   = [1 1 1 2 2 2 2 3 3];
subj   = [1 2 3 1 2 3 4 2 4];
valij  = [3 1 2 2 1 3 1 2 3];

prob.a = sparse(subi,subj,valij);

% Specify lower bounds of the constraints.
prob.blc = [30 15  -inf]';

% Specify  upper bounds of the constraints.
prob.buc = [30 inf 25 ]';

% Specify lower bounds of the variables.
prob.blx = zeros(4,1);

% Specify upper bounds of the variables.
prob.bux = [inf 10 inf inf]';

% Perform the optimization.
[r,res] = mosekopt('maximize',prob); 

% Show the optimal x solution.
res.sol.bas.xx

Please note that

  • A MATLAB structure named prob containing all the relevant problem data is defined.

  • All fields of this structure are optional except prob.a which is required to be a sparse matrix. The dimension of this matrix determine the number of constraints and variables in the problem.

  • Different parts of the solution can be accessed as described in Sec. 7.1 (Accessing the solution).

Example: Linear optimization using linprog

MOSEK also provides a function linprog with a function of the same name from the MATLAB Optimization Toolbox. Consult Sec. 10.1 (Integration with MATLAB) for details.

Listing 6.3 Script implementing problem (6.1) using linprog. Click here to download.
f     = - [3 1 5 1]';				% minus because we maximize
A     = [[-2 -1 -3 -1]; [0 2 0 3]];
b     = [-15  25]';
Aeq   = [3 1 2 0];
beq   = 30;
l     = zeros(4,1);
u     = [inf 10 inf inf]';

% Example of setting options for linprog
% Get default options
opt = mskoptimset('');
% Turn on diagnostic output
opt = mskoptimset(opt,'Diagnostics','on');
% Set a MOSEK option, in this case turn basic identification off.
opt = mskoptimset(opt,'MSK_IPAR_INTPNT_BASIS','MSK_OFF');
% Modify a MOSEK parameter with double value
opt = mskoptimset(opt,'MSK_DPAR_INTPNT_TOL_INFEAS',1e-12);

[x,fval,exitflag,output,lambda] = linprog(f,A,b,Aeq,beq,l,u,opt);

x
fval
exitflag
output
lambda