Optimization with cost and constraints returned from a single function call

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:

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


2 Likes