# How to evaluate a JuMP model given a candidate solution?

**URL:** <https://discourse.julialang.org/t/how-to-evaluate-a-jump-model-given-a-candidate-solution/64555>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [July 13, 2021, 9:47am UTC](https://discourse.julialang.org/t/how-to-evaluate-a-jump-model-given-a-candidate-solution/64555 "2021-07-13T09:47:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shuhua](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shuhua/32/27618_2.png) [@Shuhua](https://discourse.julialang.org/u/Shuhua)\
**Post date:** [July 13, 2021, 9:47am UTC](https://discourse.julialang.org/t/how-to-evaluate-a-jump-model-given-a-candidate-solution/64555/1 "2021-07-13T09:47:58Z")

</div>

Supposing I have built an optimization `model` in JuMP, how can I evaluate its `objective` with a candidate solution? This solution may be obtained manually or from other tools. That is, how to get `objective(x)` if I have a tentative value of `x`.

The feasibility of a solution (point) can be checked with [primal\_feasibility\_report](https://jump.dev/JuMP.jl/stable/reference/solutions/#JuMP.primal_feasibility_report), but how to get the corresponding objective value?

---

<div class="post-metadata">

**Author:** ![hdavid16](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hdavid16/32/11531_2.png) [@hdavid16](https://discourse.julialang.org/u/hdavid16)\
**Post date:** [July 13, 2021, 6:09pm UTC](https://discourse.julialang.org/t/how-to-evaluate-a-jump-model-given-a-candidate-solution/64555/2 "2021-07-13T18:09:47Z")

</div>

You can fix the variables in the model and solve it ( [Variables · JuMP](https://jump.dev/JuMP.jl/stable/manual/variables/#Create-a-fixed-variable))

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [July 13, 2021, 10:07pm UTC](https://discourse.julialang.org/t/how-to-evaluate-a-jump-model-given-a-candidate-solution/64555/3 "2021-07-13T22:07:50Z")

</div>

Another option is this

```nohighlight
julia> model = Model();

julia> @variable(model, x)
x

julia> @objective(model, Min, 2x^2 + x + 0.5)
2 x² + x + 0.5

julia> f = objective_function(model)
2 x² + x + 0.5

julia> point = Dict(x => 2.0)
Dict{VariableRef, Float64} with 1 entry:
  x => 2.0

julia> value(f, x -> point[x])
10.5

```

---

<div class="post-metadata">

**Author:** ![Shuhua](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shuhua/32/27618_2.png) [@Shuhua](https://discourse.julialang.org/u/Shuhua)\
**Post date:** [July 14, 2021, 7:52am UTC](https://discourse.julialang.org/t/how-to-evaluate-a-jump-model-given-a-candidate-solution/64555/4 "2021-07-14T07:52:03Z")

</div>

But fixing the variables changes the original model construction code, right? Supposing we are given a built `model`, can we easily evaluate its objective at a given point without modifying `model`? That is, I want something like `primal_feasibility_report`.

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [July 14, 2021, 8:15am UTC](https://discourse.julialang.org/t/how-to-evaluate-a-jump-model-given-a-candidate-solution/64555/5 "2021-07-14T08:15:15Z")

</div>

Yes, fixing will modify the bounds. You’re looking for my answer above.

---

<div class="post-metadata">

**Author:** ![Shuhua](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shuhua/32/27618_2.png) [@Shuhua](https://discourse.julialang.org/u/Shuhua)\
**Post date:** [July 14, 2021, 8:58am UTC](https://discourse.julialang.org/t/how-to-evaluate-a-jump-model-given-a-candidate-solution/64555/6 "2021-07-14T08:58:04Z")

</div>

Yes, it worked. Thanks.

I have provided some comments for your code for others who are interested.

```julia
# the model
model = Model();
@variable(model, x)
@variable(model, 0 <= y[1:3] <= 10)
@objective(model, Min, 2x^2 + x + y[1] * y[2] + y[3])

# get the objective
fobj = objective_function(model) 
# `fobj` is a JuMP expression and we can use `value` to evaluate it
# the second parameter `var_value` is a `Function` that returns the value 
# of a variable `v` when called by `var_value(v)`

# need to give the value for each variable in a container
point = Dict(x => 2.0, y[1] => 1.2, y[2] => 10.0, y[3] => -1.3)
# note that no constraints are checked; `var_value` is given as a Lambda function
value(fobj, v -> point[v])

```
