Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/pyoptinterface/cppad_interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ ADFunDouble sparse_hessian(const ADFunDouble &f, const sparsity_pattern_t &patte

// Transform ExpressionGraph to CppAD function
ADFunDouble cppad_trace_graph_constraints(const ExpressionGraph &graph);
ADFunDouble cppad_trace_graph_objective(const ExpressionGraph &graph);
ADFunDouble cppad_trace_graph_objective(const ExpressionGraph &graph, bool aggregate = true);

struct CppADAutodiffGraph
{
Expand Down
21 changes: 14 additions & 7 deletions lib/cppad_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ ADFunDouble cppad_trace_graph_constraints(const ExpressionGraph &graph)
return f;
}

ADFunDouble cppad_trace_graph_objective(const ExpressionGraph &graph)
ADFunDouble cppad_trace_graph_objective(const ExpressionGraph &graph, bool aggregate)
{
ankerl::unordered_dense::map<ExpressionHandle, CppAD::AD<double>> seen_expressions;

Expand Down Expand Up @@ -503,15 +503,22 @@ ADFunDouble cppad_trace_graph_objective(const ExpressionGraph &graph)
y[i] = cppad_trace_expression(graph, output, x, p, seen_expressions);
}

CppAD::AD<double> y_sum = 0.0;
for (size_t i = 0; i < N_outputs; i++)
ADFunDouble f;

if (aggregate)
{
CppAD::AD<double> y_sum = 0.0;
for (size_t i = 0; i < N_outputs; i++)
{
y_sum += y[i];
}
f.Dependent(x, {y_sum});
}
else
{
y_sum += y[i];
f.Dependent(x, y);
}

ADFunDouble f;
f.Dependent(x, {y_sum});

return f;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/cppad_interface_ext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,6 @@ NB_MODULE(cppad_interface_ext, m)
.def_ro("hessian", &CppADAutodiffGraph::hessian_graph);

m.def("cppad_trace_graph_constraints", cppad_trace_graph_constraints);
m.def("cppad_trace_graph_objective", cppad_trace_graph_objective);
m.def("cppad_trace_graph_objective", cppad_trace_graph_objective, nb::arg("graph"), nb::arg("aggregate") = true);
m.def("cppad_autodiff", &cppad_autodiff);
}
4 changes: 3 additions & 1 deletion lib/knitro_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,9 @@ void KNITROModel::_add_objective_callback(ExpressionGraph *graph, const Outputs
evaluator->eval_hess(req->x, req->sigma, res->hess, true);
return 0;
};
auto trace = cppad_trace_graph_objective;
auto trace = [](const ExpressionGraph &graph) {
return cppad_trace_graph_objective(graph, false);
};
_add_callback_impl(*graph, outputs.obj_idxs, {}, trace, f, g, h);
}

Expand Down