Skip to content

Commit

Permalink
print evaluation count in t_tutorial.cxx
Browse files Browse the repository at this point in the history
  • Loading branch information
stevengj committed Feb 16, 2025
1 parent 128b04c commit 9020c12
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion test/t_tutorial.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <iomanip>
#include <nlopt.hpp>

int count = 0; // counter for objective-function evaluations

double myvfunc(const std::vector<double> &x, std::vector<double> &grad, void *data)
{
(void)data;
Expand All @@ -27,6 +29,7 @@ double myvconstraint(const std::vector<double> &x, std::vector<double> &grad, vo
grad[0] = 3 * a * (a*x[0] + b) * (a*x[0] + b);
grad[1] = -1.0;
}
++count;
return ((a*x[0] + b) * (a*x[0] + b) * (a*x[0] + b) - x[1]);
}

Expand Down Expand Up @@ -59,7 +62,8 @@ int main(int argc, char *argv[]) {
try{
opt.optimize(x, minf);
std::cout << "found minimum at f(" << x[0] << "," << x[1] << ") = "
<< std::setprecision(10) << minf <<std::endl;
<< std::setprecision(10) << minf <<
" after " << count << " evaluations" << std::endl;
return std::fabs(minf - 0.5443310474) < 1e-3 ? EXIT_SUCCESS : EXIT_FAILURE;
}
catch(std::exception &e) {
Expand Down

0 comments on commit 9020c12

Please sign in to comment.