Skip to content

Commit

Permalink
no more sandpit; gmax stopping criterion
Browse files Browse the repository at this point in the history
  • Loading branch information
ssloy committed Apr 21, 2021
1 parent 1d805b9 commit 2fe75cf
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 18 deletions.
6 changes: 5 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,9 @@ if (STLBFGS_UNIT_TESTS)
add_subdirectory(tests)
endif()

add_subdirectory(sandpit)
add_executable(stlbfgs-helloworld helloworld.cpp)
target_link_libraries(stlbfgs-helloworld ${CMAKE_DL_LIBS} stlbfgs)
IF (NOT WIN32)
target_link_libraries(stlbfgs-helloworld m)
ENDIF()

26 changes: 26 additions & 0 deletions helloworld.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <iostream>
#include "stlbfgs.h"

int main() {
const STLBFGS::Optimizer::func_grad_eval func = [](const std::vector<double> &x, double &f, std::vector<double> &g) {
f = (x[0] - 7)*(x[0] - 7) +
(x[1] - 1)*(x[1] - 1);
g[0] = 2*(x[0] - 7);
g[1] = 2*(x[1] - 1);
};

STLBFGS::Optimizer opt(2, func);
std::vector<double> x = {10, 10};
opt.run(x);

std::cout << "Result: x=" << x[0] << ", y=" << x[1] << std::endl;

if (std::abs(x[0]-7)<1e-3 || std::abs(x[1]-1)<1e-3) {
std::cout << "Optimization succeeded" << std::endl;
return 0;
} else {
std::cout << "Optimization failed" << std::endl;
return 1;
}
}

2 changes: 0 additions & 2 deletions sandpit/CMakeLists.txt

This file was deleted.

6 changes: 0 additions & 6 deletions sandpit/helloworld/CMakeLists.txt

This file was deleted.

6 changes: 0 additions & 6 deletions sandpit/helloworld/helloworld.cpp

This file was deleted.

11 changes: 8 additions & 3 deletions stlbfgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,17 +136,22 @@ namespace STLBFGS {
break;
}

double gmax = 0.;
if (norm(g)/std::max(1., norm(x))<=gtol) {
std::cerr << "||g||/max(1,||x||) <= " << gtol << std::endl;
break;
}

double gmax_ = 0.;
#if defined(_OPENMP) && _OPENMP>=200805
#pragma omp parallel for reduction(max:gmax)
#endif
for (double gi : g)
gmax = std::max(gmax, std::abs(gi));
if (gmax <= gtol) {
gmax_ = std::max(gmax_, std::abs(gi));
if (gmax_ <= gmax) {
std::cerr << "gmax: "<< gmax << std::endl;
break;
}

if (i==maxiter-1) {
std::cerr << "reached maxiter " << std::endl;
}
Expand Down
1 change: 1 addition & 0 deletions stlbfgs.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ namespace STLBFGS {
int maxiter = 100; // Maximum number of function evaluations
double ftol = 1e-6; // The iteration stops when (f^k - f^{k+1})/max{|f^k|,|f^{k+1}|,1} <= ftol
double gtol = 1e-14; // The iteration will stop when max{|g_i|, i = 1, ..., n} <= gtol
double gmax = 1e-14; // The iteration will stop when ||g||/max(1,||x||) <= gmax

// Line search parameters: the step size must satisfy Wolfe conditions with these parameters
double mu = 1e-4; // Armijo rule (sufficient decrease) constant
Expand Down

0 comments on commit 2fe75cf

Please sign in to comment.