Working with dictionaries in @variables and @constraints

I wonder if it is possible to work with Dict in @variables and @constraints like this:
_ORDER_PRODUCTS and UNITS are Dict

@constraint(premex, PRODAMOUNTint_upper[op in _ORDER_PRODUCTS, u in UNITS, t in TIME], 

            PRODAMOUNTint[op, u, t] >= floor(u["cap"] / _PRODUCTS[op["product"]]["bagSize"]))

Sorry, you mean that you store some constants used in the constraint (or as an variable bound) in a Dict or you want the constraints and variables created to be stored in a Dict?

If it is the former, yes, you can have any constants you use in any constraints stored in whatever structure you like.

If it is the latter, also yes, but often you will need to:

my_cons_dict = Dict()
for key in constraint_keys
    my_vars_dict[key] = @constraint(my_model, ... just the equation ...)
end
my_model[:name_for_constraint_set] = my_vars_dict

In other words, you will need to create each constraint separately, add them to your Dict and then attach the Dict to the model.

Many times this is not necessary. @constraint can generate sparse containers, so if all your axis can be represented by integer sets (even if not starting in one) it will often create a container with just that positions.

Posting a MWE would make it easier to check if it is your case.

1 Like

Thank you I manage to solve that by closely look at Dict when iterating. It is not o in Dict, but (k, o) in Dict.

So I can work with Dict in my model comfortably.

Thank you again