9.1 Solving Linear Systems Involving the Basis Matrix¶
A linear optimization problem always has an optimal solution which is also a basic solution. In an optimal basic solution there are exactly
as a matrix consisting of the columns of
or, equivalently,
and
each have a unique solution for all
MOSEK provides functions for solving the linear systems (9.1) and (9.2) for an arbitrary
In the next sections we will show how to use MOSEK to
9.1.1 Basis identification¶
To use the solutions to (9.1) and (9.2) it is important to know how the basis matrix
Internally MOSEK employs the linear optimization problem
where
The basis matrix is constructed of
If variable Task.initbasissolve
. This function initializes data structures for later use and returns the indexes of the basic variables in the array basis
. The interpretation of the basis
is as follows. If we have
then the
Moreover, the
then the
and the
For instance if
An example
Consider the linear optimization problem:
Suppose a call to Task.initbasissolve
returns an array basis
so that
basis[0] = 1,
basis[1] = 2.
Then the basis variables are
Please note the ordering of the columns in
package com.mosek.example;
import mosek.*;
public class solvebasis {
public static void main(String[] args) {
// Since the value infinity is never used, we define
// 'infinity' symbolic purposes only
double
infinity = 0;
double[] c = {1.0, 1.0};
int[] ptrb = {0, 2};
int[] ptre = {2 , 4};
int[] asub = {0, 1,
0, 1
};
double[] aval = {1.0, 1.0,
2.0, 1.0
};
mosek.boundkey[] bkc = {
mosek.boundkey.up,
mosek.boundkey.up
};
double[] blc = { -infinity,
-infinity
};
double[] buc = {2.0,
6.0
};
mosek.boundkey[] bkx = {
mosek.boundkey.lo,
mosek.boundkey.lo
};
double[] blx = {0.0,
0.0
};
double[] bux = { +infinity,
+infinity
};
int numvar = 2;
int numcon = 2;
double[] w1 = {2.0, 6.0};
double[] w2 = {1.0, 0.0};
try (Task task = new Task()) {
task.inputdata(numcon, numvar,
c,
0.0,
ptrb,
ptre,
asub,
aval,
bkc,
blc,
buc,
bkx,
blx,
bux);
task.putobjsense(mosek.objsense.maximize);
System.out.println("optimize");
try {
task.optimize();
} catch (mosek.Warning e) {
System.out.println("Mosek warning:");
System.out.println(e.toString());
}
int[] basis = new int[numcon];
task.initbasissolve(basis);
//List basis variables corresponding to columns of B
int[] varsub = {0, 1};
for (int i = 0; i < numcon; i++) {
System.out.println("Basis i:" + i + " Basis:" + basis[i]);
if (basis[varsub[i]] < numcon) {
System.out.println("Basis variable no " + i + " is xc" +
basis[i]);
} else {
int index = basis[i] - numcon;
System.out.println("Basis variable no " + i + " is x" +
index);
}
}
// solve Bx = w1
// varsub contains index of non-zeros in b.
// On return b contains the solution x and
// varsub the index of the non-zeros in x.
int nz = 2;
nz = task.solvewithbasis(false, nz, varsub, w1);
System.out.println("nz =" + nz);
System.out.println("\nSolution to Bx = w1:\n");
for (int i = 0; i < nz; i++) {
if (basis[varsub[i]] < numcon) {
System.out.println("xc" + basis[varsub[i]] + "=" + w1[varsub[i]]);
} else {
int index = basis[varsub[i]] - numcon;
System.out.println("x" + index + " = " + w1[varsub[i]]);
}
}
// Solve B^Tx = w2
nz = 2;
varsub[0] = 0;
varsub[1] = 1;
nz = task.solvewithbasis(true, nz, varsub, w2);
System.out.println("\nSolution to B^Tx = w2:\n");
for (int i = 0; i < nz; i++) {
if (basis[varsub[i]] < numcon) {
System.out.println("xc" + basis[varsub[i]] + " = " + w2[varsub[i]]);
} else {
int index = basis[varsub[i]] - numcon;
System.out.println("x" + index + " = " + w2[varsub[i]]);
}
}
} catch (mosek.Exception e)
/* Catch both Error and Warning */
{
System.out.println("An error was encountered");
System.out.println(e.getMessage());
throw e;
}
}
}
In the example above the linear system is solved using the optimal basis for (9.4) and the original right-hand side of the problem. Thus the solution to the linear system is the optimal solution to the problem. When running the example program the following output is produced.
basis[0] = 1
Basis variable no 0 is xc1.
basis[1] = 2
Basis variable no 1 is x0.
Solution to Bx = b:
x0 = 2.000000e+00
xc1 = -4.000000e+00
Solution to B^Tx = c:
x1 = -1.000000e+00
x0 = 1.000000e+00
Please note that the ordering of the basis variables is
and thus the basis is given by:
It can be verified that
is a solution to
9.1.2 Solving arbitrary linear systems¶
MOSEK can be used to solve an arbitrary (rectangular) linear system
using the Task.solvewithbasis
function without optimizing the problem as in the previous example. This is done by setting up an Task.solvewithbasis
function with the
An example
Below we demonstrate how to solve the linear system
with two inputs
package com.mosek.example;
import mosek.*;
public class solvelinear {
static public void setup(
mosek.Task task,
double[][] aval,
int[][] asub,
int[] ptrb,
int[] ptre,
int numvar,
int[] basis ) {
// Since the value infinity is never used, we define
// 'infinity' symbolic purposes only
double
infinity = 0;
mosek.stakey[] skx = new mosek.stakey [numvar];
mosek.stakey[] skc = new mosek.stakey [numvar];
for (int i = 0; i < numvar ; ++i) {
skx[i] = mosek.stakey.bas;
skc[i] = mosek.stakey.fix;
}
task.appendvars(numvar);
task.appendcons(numvar);
for (int i = 0; i < numvar ; ++i)
task.putacol(i,
asub[i],
aval[i]);
for (int i = 0 ; i < numvar ; ++i)
task.putconbound(
i,
mosek.boundkey.fx,
0.0,
0.0);
for (int i = 0 ; i < numvar ; ++i)
task.putvarbound(
i,
mosek.boundkey.fr,
-infinity,
infinity);
/* Define a basic solution by specifying
status keys for variables & constraints. */
task.deletesolution(mosek.soltype.bas);
task.putskcslice(mosek.soltype.bas, 0, numvar, skc);
task.putskxslice(mosek.soltype.bas, 0, numvar, skx);
task.initbasissolve(basis);
}
public static void main (String[] argv) {
int numcon = 2;
int numvar = 2;
double[][] aval = {
{ -1.0 },
{ 1.0, 1.0 }
};
int[][] asub = {
{ 1 },
{ 0, 1 }
};
int [] ptrb = new int[] {0, 1};
int [] ptre = new int[] {1, 3};
int[] bsub = new int[numvar];
double[] b = new double[numvar];
int[] basis = new int[numvar];
try (Task task = new Task()) {
// Directs the log task stream to the user specified
// method task_msg_obj.streamCB
task.set_Stream(
mosek.streamtype.log,
new mosek.Stream()
{ public void stream(String msg) { System.out.print(msg); }});
/* Put A matrix and factor A.
Call this function only once for a given task. */
setup(
task,
aval,
asub,
ptrb,
ptre,
numvar,
basis
);
/* now solve rhs */
b[0] = 1;
b[1] = -2;
bsub[0] = 0;
bsub[1] = 1;
int nz;
nz = task.solvewithbasis(false, 2, bsub, b);
System.out.println("\nSolution to Bx = b:\n");
/* Print solution and show correspondents
to original variables in the problem */
for (int i = 0; i < nz; ++i) {
if (basis[bsub[i]] < numcon)
System.out.println ("This should never happen");
else
System.out.println("x" + (basis[bsub[i]] - numcon) + " = " + b[bsub[i]]);
}
b[0] = 7;
bsub[0] = 0;
nz = task.solvewithbasis(false, 1, bsub, b);
System.out.println ("\nSolution to Bx = b:\n");
/* Print solution and show correspondents
to original variables in the problem */
for (int i = 0; i < nz; ++i) {
if (basis[bsub[i]] < numcon)
System.out.println("This should never happen");
else
System.out.println("x" + (basis[bsub[i]] - numcon) + " = " + b[bsub[i]] );
}
}
}
}
The most important step in the above example is the definition of the basic solution, where we define the status key for each variable. The actual values of the variables are not important and can be selected arbitrarily, so we set them to zero. All variables corresponding to columns in the linear system we want to solve are set to basic and the slack variables for the constraints, which are all non-basic, are set to their bound.
The program produces the output:
Solution to Bx = b:
x1 = 1
x0 = 3
Solution to Bx = b:
x1 = 7
x0 = 7