Tinkering with values of variables in a solved JuMP model

I’d like to solve a JuMP model, use primal_feasibility_report to check which constraints are violated, tweak the variable values until the constraints are all strictly respected, and then compute the new objective value with the new variables. Is it possible to do so?

I can pass a Dict to primal_feasiblity_report with the new values. That’s a bit inconvenient as it seems I have to do scalar by scalar, I can’t pass a vector or a matrix at once, but it works. I see no way to recompute the objective value, though.

Does something like this work for you?

julia> using JuMP, HiGHS

julia> profit = [5, 4, 3, 7, 6];

julia> weight = [2, 3, 1, 4, 3];

julia> capacity = 8;

julia> N = length(profit);

julia> model = Model(HiGHS.Optimizer);

julia> set_silent(model);

julia> @variable(model, x[1:N], Bin);

julia> @objective(model, Max, profit' * x);

julia> @constraint(model, c, weight' * x <= capacity);

julia> optimize!(model);

julia> x_sol = value.(x)
5-element Vector{Float64}:
 0.0
 0.0
 1.0
 1.0
 1.0

julia> x_new = copy(x_sol);

julia> x_new[1] = 1.0;

julia> x_new[2] = 0.5;

julia> d = Dict(x .=> x_new)
Dict{VariableRef, Float64} with 5 entries:
  x[4] => 1.0
  x[1] => 1.0
  x[3] => 1.0
  x[2] => 0.5
  x[5] => 1.0

julia> f = v -> d[v]
#14 (generic function with 1 method)

julia> value(f, objective_function(model))
23.0

julia> value(f, c)
11.5

julia> primal_feasibility_report(f, model)
Dict{Any, Float64} with 2 entries:
  x[2] binary                                      => 0.5
  c : 2 x[1] + 3 x[2] + x[3] + 4 x[4] + 3 x[5] ≤ 8 => 3.5

If the Dict is annoying to construct in your case, maybe there are better ways to construct the variable to value function (in this case f)?