Parameters in a JuMP model

Welcome! Once you create the model, the values of p[1] and p[2] are linked to that model (as far as I know). Thus, changing these values afterwards without updating the model will have no effect. You could do it in a loop, e.g.,

P = [[1 2], [2 4]]
for p in P
    model = Model(HiGHS.Optimizer);
    @variable(model, x1);
    @variable(model, x2);
    @constraint(model, c1, x1 == p[1]);
    @constraint(model, c2, x2 == p[2]);
    @objective(model, Min, x1+x2);
    optimize!(model)
    println("objective: ", objective_value(model))
    print(model)
end

or with a function taking your parameters as argument, e.g.,

function build_model(p)
    model = Model(HiGHS.Optimizer);
    @variable(model, x1);
    @variable(model, x2);
    @constraint(model, c1, x1 == p[1]);
    @constraint(model, c2, x2 == p[2]);
    @objective(model, Min, x1+x2);
    return model
end

P = [[1 2], [2 4]]
for p in P
    model = build_model(p)
    optimize!(model)
    println("objective: ", objective_value(model))
end

There is probably a more efficient way (with respect to performance) to do that, but I do not know.

2 Likes