-2

How would I specify a constraint so that the boolean 2-dimensional decision variable matrix has the following criteria:

  • all the non-zero columns have to be identical

If the matrix was a numpy array, we would be able to use np.all, but I don't see an analogous cvxpy function.

import numpy as np
import cvxpy as cp
import cvxopt 

util = np.array([[0.7, 0.95, 0.3, 3], [2, 1.05, 2.2, 5], [4, 1, 3, 6]])

dec_vars = cp.Variable(len(util), boolean = True)
zero_cat_vars = cp.Variable(util.shape[0], boolean = True)

# example of output
dev_vars_smp_output = np.array([[0, 0, 0, 0], [1, 0, 0, 1], [1, 0, 0, 1]])

zero_cat_constr = cp.sum(dec_vars, axis=1) >= 1 * zero_cat_vars

dec_vars_excl_zeros = dec_vars[:,~cp.all(dec_vars==0, axis=1)]
col_equal_constr = cp.sum(dec_vars_excl_zeros, axis=1) == dec_vars_excl_zeros.shape[1] * zero_cat_vars

matsuo_basho
  • 2,833
  • 8
  • 26
  • 47

1 Answers1

1

Mathematical programming has more to do with mathematics than programming. (The term "programming" in math programming actually means "planning" and has nothing to do with computer programming). Often a programming analogy is not very useful.

The mathematical device you are looking for is

 x(i,j) = v(i) ⋅ δ(j)
 δ(j) ∈ {0,1}
 v(i), x(i,j) ≥ 0

v is the (possibly nonzero) vector and δ indicates whether we use v or a zero vector. x is the final matrix. This nonlinear (and nonconvex) formulation can be linearized as:

 x(i,j) ≤ v(i)
 x(i,j) ≤ M ⋅ δ(j)
 x(i,j) ≥ v(i) − M ⋅ (1−δ(j))
 δ(j) ∈ {0,1}
 v(i), x(i,j) ≥ 0

Here M is an upper bound on v(j) (and x(i,j)). This approach can be generalized to allow for negative v(j) and x(i,j). I'll leave it to you to transcribe this into Python/CVXPY code.

Erwin Kalvelagen
  • 15,677
  • 2
  • 14
  • 39