Can the string be included to represent the constraint name in non-linear constraints using @NLconstraint, akin to @constraint’s base_name functionality?
Unfortunately, no. This is a known limitation.
So, it appears that using the base_name
keyword or a similar option is not possible, and the set_name
function is not a viable solution.
Does this imply that if I need to create a non-linear constraint within the for loop, I am unable to specify distinct names? For example, if I require something like:
model = Model()
@variable(model, x[i=1:5])
for i = 1:3
@NLconstraint(model, con[i], x[i]^3 <= 1)
end
I understand that the solution could be as follows:
model = Model()
@variable(model, x[i=1:5])
@NLconstraint(model, con[i = 1:3], x[i]^3 <= 1)
However, I require the ability to invoke the @NLconstraint statement within the for loop, as demonstrated in the initial example.
So, it appears that using the
base_name
keyword or a similar option is not possible, and theset_name
function is not a viable solution.
Correct. Nonlinear constraints are not like normal @constraints
. They do not have names.
It’s always worth remembering that you can add anonymous constraints, and that you can create your own data structures to store a constraint:
Option 1
model = Model()
@variable(model, x[i=1:5])
for i = 1:3
@NLconstraint(model, x[i]^3 <= 1)
end
Option 2
model = Model()
@variable(model, x[i=1:5])
con = Any[]
for i = 1:3
c = @NLconstraint(model, x[i]^3 <= 1)
push!(con, c)
end
con
In actuality, option 2 appears to be a satisfactory solution as I can remove specific constraints, such as:
delete(model, con[2])
Although this solution results in a slightly higher number of allocations, it appears to be acceptable. Thank you, this should be sufficient.