1

I have some troubles to understand how to implement the following MIQP (Mixed Integer Quadratic Programming) with linear constraints in Matlab calling Gurobi.

Let me explain in a schematic way my setting.


(1) x is the unknown and it is a column vector with size 225x1.


(2) The objective function (which should be minimised wrto x) looks like

enter image description here

which can be rewritten as

enter image description here

I have a Matlab script computing alpha, Q,c (Q,c sparse) when some_known_parameters1 are given:

function [alpha, Q,c]=matrix_objective_function(some_known_parameters1)

%...

end

(3) The constraints are linear in x, include equalities and inequalities, and are written in the form enter image description here

I have a Matlab script computing Aeq,beq,Aineq,bineq (Aeq,Aineq sparse) when some_known_parameters2 is given:

function [Aeq,beq,Aineq,bineq]=constraints(some_known_parameters2)

%...

end

(4) Some components of x are restricted to be in {0,1}. I have a Matlab script producing a string of letters B (binary), C (continous) when some_known_parameters3 is given:

function type=binary_continuous(some_known_parameters3)

%...

end

Now, I need to put together (1)-(4) using Gurobi. I am struggling to understand how. I found this example but it looks very cryptic to me. Below I report some lines I have attempted to write, but they are incomplete and I would like your help to complete them.

clear 
rng default

%Define some_known_parameters1, 
 some_known_parameters2,some_known_parameters3 [...]

%1) generate alpha,Q,c,Aeq,beq,Aineq,bineq,type with Q,c,Aeq, Aineq sparse
[alpha, Q,c]=matrix_objective_function(some_known_parameters1)
[Aeq,beq,Aineq,bineq]=constraints(some_known_parameters2)
type=binary_continuous(some_known_parameters3)



%2) Set up Gurobi
clear model;
model.A=[Aineq; Aeq];
model.rhs=full([bineq(:); beq(:)]); 
model.sense=[repmat('<', size(Aineq,1),1); repmat('=', size(Aeq,1),1)];
model.Q=Q; %not sure?
model.alpha=alpha; %not sure?
model.c=c; %not sure?
model.vtype=type;
result=gurobi(model); %how do I get just the objective function here without the minimiser?

Questions:

(1) I'm not sure about

model.Q=Q; 
model.alpha=alpha; 
model.c=c;

I'm just trying to set the matrices of the objective function using the letters provided here but it gives me error. The example here seems to me doing

model.Q=Q; 
model.obj=c; 

But then how do I set alpha? Is it ignoring it because it does not change the set of solutions?

(2) How do I get as output stored in a matrix just the minimum value of the objective function without the corresponding x?

TEX
  • 2,249
  • 20
  • 43
  • 1
    You should not expect to be able to give gurobi some black-box objective. This is something for NLP territory (where some form of differentiation is happening inside; e.g. Ipopt/Bonmin). Gurobi needs this objective in it's own native form. What form that is depends on your lib/wrapper. In low-level form usually something like `0.5 * x'Qx + q'x` with Q psd (convex QP; which is probably the only one gurobi supports; ignoring SOCP generalizations). If that's giving you headaches, look for some more high-level wrapper. Gurobi's Python-API for example supports `expr = QuadExpr(x*x + y+y)`. – sascha Nov 13 '18 at 19:49
  • @sascha thanks: my problem is very basic I guess: (a) I think my objective function can be rewritten as `Q+x'Hx` (I have added this to my question); (b) I still don't understand how to complete steps 3) and 4) above. – TEX Nov 13 '18 at 19:55
  • 1
    This is all explained in the [docs](http://www.gurobi.com/documentation/8.0/refman/matlab_the_model_argument.html) for this low-level view. Bring your objective in [this form](http://www.gurobi.com/documentation/8.0/refman/matlab_api_overview.html#matlab:problem) and set `obj = some_vec`, `objcon = some_vec` and `Q = some_matrix`. Then (4) is just a string it seems like `BBC` (binary, binary, continuous). The bounds are vectors `lb` and `ub`. – sascha Nov 13 '18 at 20:02
  • @sascha regarding (4), I have `225` unknowns: is there a way to write the variable type in a short way, without writing it one by one? Also, what is the type to use for a variable constrained to be in `{-1,1}`? – TEX Nov 13 '18 at 21:03
  • 1
    vtypes is string. lb ub are vectors. 3 vars between (-1,1), then (1,3) and (2,3) will be lb=[-1,1,2] and ub=[1,3,3]. – sascha Nov 14 '18 at 05:35
  • thanks: I did not mean continously varying between - 1 and 1 but taking values 1 or - 1. – TEX Nov 14 '18 at 07:40
  • 1
    That's a modelling thing. I recommend grabbing some integer-programming book. You can use one binary variable and replace all occurences with the term `x = 1-2*binVar`. x will be in {-1,1} then. Yes, this is annoying in low-level form (but there is no way out without wrappers / lib-support). But nobody should do prototyping on this level imho. – sascha Nov 14 '18 at 08:15
  • @sascha Thanks: I've mostly solved all my doubts, but I still have 2 points to clear up (see my question, final part). Thanks in case you can help. – TEX Nov 14 '18 at 13:09

1 Answers1

2

(1) You're right, there's no need to pass the constant alpha since it doesn't affect the optimal solution. Gurobi's MATLAB API only accepts sparse matrices. Furthermore model.obj is always the c vector in the problem statement:

model.Q = sparse(Q); 
model.obj = c;

(2) To get the optimal objective value, you first need to pass your model to gurobi and solve it. Then you can access it via the objval attribute:

results = gurobi(model);
val = results.objval + alpha
joni
  • 6,840
  • 2
  • 13
  • 20