How can I formulate these constraints for the optimization math programming (MIP) - constraints

I am struggling with some constraints that have ranged variables and several decision variables.
The constraints are
`if a[i] <= t <= a[i]+b[i], then M[t][i] == 1,
else if t < a[i] or t > a[i]+b[i], then M[t][i] == 0.
decision variables (int a[i], b[i])
(boolean M[t][i])
Examples of ranges of the variables
0 <= a[i] <= 50
0 <= b[i] <= 10
0 <= t <= 100`
I'd like to linearize these formulations, like as Big-M method or etc...
Plz, let me know the way to overcome it.
Thank you.
Linearization by using the Big-M method or etc...
such as...
M*t + (M-1)*t <= M*(a[i]) + (M-1)*a[i]

Related

How to specify variable bounds in a clean way?

/*#
requires 0 <= lb < N_LOG_BLOCKS && 0 <= lp < N_PAGE ;
requires ( 1 <= h_clean_counter + l_clean_counter <= N_PHY_BLOCKS );
requires 0 <= h_act_block_index_p < N_PHY_BLOCKS && 0 <= h_act_page_p < N_PAGE;
requires 0 <= l_act_block_index_p < N_PHY_BLOCKS && 0 <= l_act_page_p < N_PAGE;
requires 0 <= l_to_p[lb][lp] < N_PHY_BLOCKS * N_PAGE || l_to_p[lb][lp] == -1;
requires -2147483648 <= d <= 2147283647 ;
requires 0 <= chance_index_p < LRU_SIZE;
requires 0 <= index_2_physical[h_act_block_index_p] < N_PHY_BLOCKS ;
requires \forall integer i; 0 <= i < N_PHY_BLOCKS ==> 0 <= index_2_physical[i] < N_PHY_BLOCKS;
requires 0 <= l_array_counter < N_PHY_BLOCKS/2;
requires l_clean_counter == low_array_counter;
requires h_clean_counter == high_array_counter;
...
*/
My code has tons of variables with some intended bound. As a result, I need to put all these bound constraints in all "requires" and "ensures" regions of all functions. I wonder whether there is a smarter way for me to specify the variable bound, preferably in a global manner.
If these bounds are indeed the same for all functions and you have global variables or the formals are consistently named across all functions, I'd say that this is something MetAcsl can do for you, with something like (for your first requires)
/*# meta \prop,
\name(lb_bounds),
\context(\precond),
\targets(\ALL),
0 <= lb < N_LOG_BLOCKS;
*/
[NB: I haven't tried to type-check it, it may contain typos]
Basically, this tells MetAcsl to insert a pre-condition (because of the \precond context) to \ALL functions saying 0<= lb < N_LOG_BLOCKS.
You can find more information about the annotations that can be generated by MetAcsl on its gitlab repository.

How to define a JuMP expression conditional on index value

I am trying to define a #NLexpression in JuMP that has different specifications at different indices. In the below example, I want the expression y[j,k] to be defined as 1/(x[j] - x[k]) when j != k and take some other value when j == k. I can simulate this behavior by defining an auxiliary variable z and adding constraints conditional on index values. Is there a similar way to define expression conditional on index values?
using JuMP, Ipopt
model = JuMP.Model(with_optimizer(Ipopt.Optimizer))
#variable(model, 0 <= x[1:2])
#NLexpression(model, y[j=1:2,k=1:2], 1/(x[j] - x[k])) # <- problematic line
#variable(model, z[1:2,1:2])
for j=1:2, k=1:2
if j == k
#constraint(model, z[j,k] == 1)
else
#NLconstraint(model, z[j,k] == 1/(p[j] - p[k]))
end
end
display(model)
You're not obligated to use JuMP's macros to create containers for expressions. You should be able to conditionally create the expressions as follows:
model = JuMP.Model()
#variable(model, 0 <= x[1:2])
y = Dict() # Or Array, if you prefer.
for j=1:2, k=1:2
if j == k
y[j,k] = 1
else
y[j,k] = #NLexpression(model, 1/(p[j] - p[k]))
end
end

cvxpy contrained normalization equations (abs)

I am working in an optimization problem (A*v = b) where I would like to rank a set of alternatives X = {x1,x2,x3,x4}. However, I have the following normalization constraint: |v[i] - v[j]| <= 1, which can be in the form -1 <= v[i] - v[j] <= 1.
My code is as follows:
import cvxpy as cp
n = len(X) #set of alternatives
v = cp.Variable(n)
objective = cp.Minimize(cp.sum_squares(A*v - b))
constraints = [0 <= v]
#Normalization condition -1 <= v[i] - v[j] <= 1
for i in range(n):
for j in range(n):
constraints = [-1 <= v[i]-v[j], 1 >= v[i]-v[j]]
prob = cp.Problem(objective, constraints)
# The optimal objective value is returned by `prob.solve()`.
result = prob.solve()
# The optimal value for v is stored in `v.value`.
va2 = v.value
Which outputs:
[-0.15 0.45 -0.35 0.05]
Result, which is not close to what should be and even have negative values. I think, my code for the normalization contraint most probably is wrong.
You are not appending your constraints, instead you are overwriting them each time. Instead of this line
constraints = [-1 <= v[i]-v[j], 1 >= v[i]-v[j]]
You should have
constraints += [-1 <= v[i]-v[j], 1 >= v[i]-v[j]]
For cleanliness you may want to change this
for i in range(n):
for j in range(n):
To only consider each pair once:
for i in range(n):
for j in range(i+1, n):

Correct way for excluding constraints based on index

Assume the following constraint:
#constraint(model, [i=1:5,j=1:5], a[i] <= b[j])
How do I exclude constraint where i == j from the above? This is simple to do in AMPL but I can't find any similar syntax in JuMP.
#constraint(model, [i=1:5, j=1:5; i != j], a[i] <= b[j])

Minimize the maximum variable

I have a Mixed Integer Programming problem. The objective function is a minimization of the maximum variable value in the a vector. The variable is has an upper bound of 5. The problem is like this:
m = Model(solver = GLPKSolverMIP())
#objective(m, Min, max(x[i] for i=1:12))
#variable(m, 0 <= x[i] <= 5, Int)
#constraint(m, sum(x[i] for i=1:12) == 12)
status = solve(m)
The max variable is not part of the julia JuMP syntax. So I modified the problem to
t=1
while t<=5 && (status == :NotSolved || status == :Infeasible)
m = Model(solver = GLPKSolverMIP())
i = 1:12
#objective(m, Min, max(x[i] for i=1:12))
#variable(m, 0 <= x[i] <= t, Int)
#constraint(m, sum(x[i] for i=1:12) == 12)
status = solve(m)
t += 1
end
This solution does the job by solving the problem iterative for starting with a upper bound for the variable at 1 and then increase by one until the solutoin is feasible. Is this really the best way to do this?
The question wants to minimize a maximum, this maximum can be held in an auxiliary variable and then we will minimize it. To do so, add constraints to force the new variable to actually be an upper bound on x. In code it is:
using GLPKMathProgInterface
using JuMP
m = Model(solver = GLPKSolverMIP())
#variable(m, 0 <= x[i=1:3] <= 5, Int) # define variables
#variable(m, 0 <= t <= 12) # define auxiliary variable
#constraint(m, t .>= x) # constrain t to be the max
#constraint(m, sum(x[i] for i=1:3) == 12) # the meat of the constraints
#objective(m, Min, t) # we wish to minimize the max
status = solve(m)
Now we can inspect the solution:
julia> getValue(t)
4.0
julia> getValue(x)
3-element Array{Float64,1}:
4.0
4.0
4.0
The actual problem the poster wanted to solve is probably more complex that this, but it can be solved by a variation on this framework.

Resources