This is the same issue as Condition in @objective - #5 by odow.
You cannot put conditions like this in the objective, because they get evaluated with JuMP VariableRef
rather than the value of the variable.
You need to use a mixed-integer reformulation.
# Instead of
model = Model()
@variable(model, x[1:2], Bin)
@show sum(x) == 0 # false! because x[1] + x[2] != 0
@objective(model, Min, sum(x) == 0 ? 0 : 1)
@show objective_value(model) # 1.0 <-- a constant!
# Use
model = Model()
@variable(model, x[1:2] >= 0, Int)
@variable(model, y, Bin)
M = 10_000 # A number bigger than optimal `sum(x)`.
@constraint(model, sum(x) - M * y <= 0)
@objective(model, Min, 1 * y)
MIP-modelling is an art, and there are a lot of tradeoffs and tractability issues you need to consider. e.g., how big is M? You’re going to run into issues if you model is large.