# Optimization with cost and constraints returned from a single function call

**URL:** https://discourse.julialang.org/t/optimization-with-cost-and-constraints-returned-from-a-single-function-call/40433
**Category:** Optimization (Mathematical)
**Created:** [May 29, 2020, 6:16pm UTC](https://discourse.julialang.org/t/optimization-with-cost-and-constraints-returned-from-a-single-function-call/40433 "2020-05-29T18:16:40Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![DrPapa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drpapa/32/6835_2.png) [@DrPapa](https://discourse.julialang.org/u/DrPapa)
#### Post date: [May 29, 2020, 6:16pm UTC](https://discourse.julialang.org/t/optimization-with-cost-and-constraints-returned-from-a-single-function-call/40433/1 "2020-05-29T18:16:40Z")

</div>

I need to solve a constrained non-linear optimization problem where the cost and constraints are computed via a vector-value function. E.g., instead of

```julia
J = cost(x)
c1 = constraint1(x)
c2 = constraint2(x)

```

I have, for efficiency reasons,

```julia
J,c1,c2 = myfunc(x)

```

Do any of the optimizer packages support this?

---

<div class="post-metadata">

### Author: ![frapac](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frapac/32/6879_2.png) [@frapac](https://discourse.julialang.org/u/frapac)
#### Post date: [May 29, 2020, 8:05pm UTC](https://discourse.julialang.org/t/optimization-with-cost-and-constraints-returned-from-a-single-function-call/40433/2 "2020-05-29T20:05:52Z")

</div>

If you are looking for a solver supporting the evaluation of constraints and objective in the same callback, Knitro.jl is a way-to-go (but has a commercial license).

Another way to go is to define the callbacks for your solver (`eval_f` and `eval_cons`) in a closure, where you could evaluate the constraints and the objective jointly. For instance:

```julia
function buid_callback(x0)
    J, c1, c2 = myfunc(x0)
    current_x = hash(x0)
    function eval_f(x)
        if hash(x) != current_x
            current_x = hash(x)
            J, c1, c2 = myfunc(x)
        end
        return J 
    end
    function eval_cons!(cons, x)
        if hash(x) != current_x
            current_x = hash(x)
            J, c1, c2 = myfunc(x)
        end
        cons[:] = [c1, c2]
    end
            
    return (eval_f, eval_cons)
end

```

---

<div class="post-metadata">

### Author: ![DrPapa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drpapa/32/6835_2.png) [@DrPapa](https://discourse.julialang.org/u/DrPapa)
#### Post date: [May 29, 2020, 8:15pm UTC](https://discourse.julialang.org/t/optimization-with-cost-and-constraints-returned-from-a-single-function-call/40433/3 "2020-05-29T20:15:11Z")

</div>

Thanks for the heads up on Knitro. I will look into it, but the commercial license will probably be a problem.

I’m not sure I understand your callback example. It looks like `myfunc` is getting called twice. Once for the objective and once for the constraints.

---

<div class="post-metadata">

### Author: ![frapac](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frapac/32/6879_2.png) [@frapac](https://discourse.julialang.org/u/frapac)
#### Post date: [May 29, 2020, 9:19pm UTC](https://discourse.julialang.org/t/optimization-with-cost-and-constraints-returned-from-a-single-function-call/40433/4 "2020-05-29T21:19:41Z")

</div>

I acknowledge the example is not that self-explanatory. The thing is, by using this closure, you could build you two callbacks suitable, for any optimization solvers:

```julia
eval_f, eval_cons = build_callback(x0)

```

Then, each time you are calling `eval_f` at a new point `x`, the function will look if it has already computed this point. If so, it will return the previously stored solution `J,c1, c2`. Otherwise, it will call your function `myfunc`.

So imagine that in the solver you are calling `eval_f`, then `eval_cons`, on a new point `x`. Then

- As `x` is a new point, you will call `myfunc` when you first call the function `eval_f`, and store the result in the cache inside the closure
- Then, when you call `eval_cons` at `x`, the function will recognize that it has already computed `myfunc` on `x` and hence will return directly `(c1, c2)`, without recomputing all from scratch

---

<div class="post-metadata">

### Author: ![DrPapa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drpapa/32/6835_2.png) [@DrPapa](https://discourse.julialang.org/u/DrPapa)
#### Post date: [May 29, 2020, 11:28pm UTC](https://discourse.julialang.org/t/optimization-with-cost-and-constraints-returned-from-a-single-function-call/40433/5 "2020-05-29T23:28:55Z")

</div>

Wow, thats cool. I’ve not seen this pattern before. It certainly looks like it’ll do the trick. Thanks for the extra details
