0

I have an objective function such as

enter image description here

(for simplicity I omitted the coefficients).

I want to minimize this function using intlinprog with the following constraints:

enter image description here

and

enter image description here

with all x binary. These sums result in these 4 inequalities:

enter image description here

It is clear that the constaints matrix is

enter image description here

This works well if I create this matrix manually. Now suppose I have 6 or 8 or 10 variables instead of 4 in my objective function and in the constraints (same pattern). How can I use Matlab to generate this constraints matrix for these larger problems?

1
  • Not so easy for more complex models (for an example see [link]). Commented Oct 14, 2017 at 9:47

1 Answer 1

1

I recommend writing down some other cases a bit. So it seems you want to constraint all row-sums and all column-sums:

For N=3, there are 9 vars (i'm assuming a square case here; you did not provide complete info):

x00 x01 x02
x10 x11 x12
x20 x21 x22

Now the constraint matrix looks like:

x00 x01 x02 | x10 x11 x12 | x20 x21 x22
---------------------------------------
1   1   1
              1   1   1
                            1   1   1
1             1             1
    1             1             1
        1             1             1

That's pretty regular. Not it's time to check out matlab's matrix-creation functions. Sadly i'm not much of a matlab-user, but:

the lower half of rows consist of:

  • horizontal stacking of N identity-matrices each of size N

the upper half of rows consist of:

  • block-diagonal matrix of N 1-row-vectors each of size N

the final matrix is a vertical stacking of both components

A full sparse-matrix python-example (sorry, no matlab here; but there should be nearly a 1:1 mapping), to be more clear would look like:

import numpy as np
import scipy.sparse as sp

N = 3
component_a = sp.hstack([sp.eye(N) for i in range(N)])
row_full_1 = sp.csr_matrix(np.ones(N))
component_b = sp.block_diag([row_full_1 for i in range(N)])  # matlab: blkdiag?
matrix = sp.vstack((component_b, component_a))

print(matrix.todense())

Output:

[[ 1.  1.  1.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  1.  1.  1.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  1.  1.  1.]
 [ 1.  0.  0.  1.  0.  0.  1.  0.  0.]
 [ 0.  1.  0.  0.  1.  0.  0.  1.  0.]
 [ 0.  0.  1.  0.  0.  1.  0.  0.  1.]]

Remark: depending on N, you need to think about using dense or sparse-matrices. Given N, the ratio of non-zeros in the matrix will be 1/N.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.