[JuMP] Inf, -Inf constraint 제거하기

JuMP constraint에 Inf, -Inf가 포함된 경우 이를 제거하는 방법을 아래 소개 한다.

lower에 -Inf나 upper에 Inf가 있는 경우 예를 들어
@constaint(m, x <= Inf) 를 하게 되면
==> x + NaN <= Inf
라는 식으로 constraint에 NaN이 포함되어 optimize시 error가 발생한다
upper나 lower에 Inf, -Inf가 포함되는 경우 의미가 없기 때문에 에러를 발생 시키는게 아닐까 한다.
따라서 사전에 그런 조건에 있는 constraint를 제거 해주는게 좋다.

m = Model(Gurobi.Optimizer)
@variable(m, x[1:5])
@constraint(m,x .<= [1,Inf,2,Inf,3])
@constraint(m,x[2:4] .>= [1,9,-Inf])
println(m)

types = list_of_constraint_types(m)
for (expr_type, opt_type) in types
  println(expr_type, "-", opt_type)
  con_lst = JuMP.all_constraints(m,expr_type,opt_type)
  for con in con_lst
    if abs(JuMP.normalized_rhs(con)) == Inf
      JuMP.delete(m,con)
    end
  end
end
println(m)

결과

Academic license - for non-commercial use only
Feasibility
Subject to
 x[2] ≥ 1.0
 x[3] ≥ 9.0
 x[4] + NaN ≥ -Inf
 x[1] ≤ 1.0
 x[2] + NaN ≤ Inf
 x[3] ≤ 2.0
 x[4] + NaN ≤ Inf
 x[5] ≤ 3.0

GenericAffExpr{Float64,VariableRef}-MathOptInterface.GreaterThan{Float64}
GenericAffExpr{Float64,VariableRef}-MathOptInterface.LessThan{Float64}
Feasibility
Subject to
 x[2] ≥ 1.0
 x[3] ≥ 9.0
 x[1] ≤ 1.0
 x[3] ≤ 2.0
 x[5] ≤ 3.0

댓글 달기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다