Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define a correct constraint, like outside a 2-D rectangle in Julia with JuMP

I would like to define a constraint in an optimization problem as follows: (x,y) not in {(x,y)|1.0 < x < 2.0, 3.0 < y < 4.0}.

what I tried is @constraint(model, (1.0 < x < 2.0 + 3.0 < y < 4.0)!=2), but failed. It seems that boolen operation is not allowed. such that I have no idea about it. Any advice is appreciated!

like image 725
Quinn Xie Avatar asked Oct 20 '25 04:10

Quinn Xie


1 Answers

You should avoid introducing quadratic constraints (as in the other answer) and rather introduce binary variables. This increase number of available solvers and generally linear models take shorter time to solve.

Hence you should note that !(1.0 < x < 2.0) is an equivalent of x <= 1 || x >= 2 which can be written in a linear form as:

@variable(model, bx, Bin)
const M = 1000 # number "big enough"
@constraint(model, x <= 1 + M*bx)
@constraint(model, x >=2 - M*(1-bx))

bx is here a "switcher" variable that makes either first or second constraint obligatory.

I am not sure what you want about y as you have 3.0 < y < 3.0 but basically the pattern to formulate the would be the same. Just note you cannot have a constraint such as y != 3 as solvers obviously have some numerical accuracy and you would need rather to represent this is as for an example !(3-0.01 < y < 3+0.01) (still using the same pattern as above)

like image 63
Przemyslaw Szufel Avatar answered Oct 23 '25 01:10

Przemyslaw Szufel