Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chriselrod committed Sep 11, 2023
1 parent 7283f38 commit 3ba28d4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
21 changes: 11 additions & 10 deletions include/Math/BoxOpt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@ template <typename F> struct IsBoxCall<BoxCall<F>> {

constexpr auto minimize(utils::Arena<> *alloc, MutPtrVector<double> x,
const auto &f) -> double {
constexpr bool constrained = IsBoxCall<decltype(f)>::value;
constexpr bool constrained =
IsBoxCall<std::remove_cvref_t<decltype(f)>>::value;
constexpr double tol = 1e-8;
constexpr double tol2 = tol * tol;
constexpr double c = 0.5;
Expand All @@ -224,6 +225,7 @@ constexpr auto minimize(utils::Arena<> *alloc, MutPtrVector<double> x,
if (dir.norm2() < tol2) break;
double t = 0.0;
for (ptrdiff_t i = 0; i < L; ++i) {
// TODO: 0 clamped dirs
t += hr.gradient()[i] * dir[i];
double xi = xcur[i] - alpha * dir[i];
if constexpr (constrained) xi = std::clamp(xi, -EXTREME, EXTREME);
Expand All @@ -241,6 +243,8 @@ constexpr auto minimize(utils::Arena<> *alloc, MutPtrVector<double> x,
}
for (;;) {
double alphanew = alpha * (1 / tau);
// TODO: write into `xcur` instead of `xtmp` by offsetting from `xnew`
//
for (ptrdiff_t i = 0; i < L; ++i) {

double xi = xcur[i] - alphanew * dir[i];
Expand All @@ -252,27 +256,24 @@ constexpr auto minimize(utils::Arena<> *alloc, MutPtrVector<double> x,
std::swap(xtmp, xnew); // we keep xtmp as new best
alpha = alphanew;
fxnew = fxtmp;
if ((fx - fxtmp) <= tol) {
dobreak = true;
break;
}
dobreak = (fx - fxtmp) <= tol;
if (dobreak) break;
}
} else {
// failure, we shrink alpha
for (;;) {
alpha *= tau;
for (ptrdiff_t i = 0; i < L; ++i) {

double xi = xcur[i] - alpha * dir[i];
if constexpr (constrained) xi = std::clamp(xi, -EXTREME, EXTREME);
xnew[i] = xi;
}
fxnew = f(xnew);
dobreak = norm2(xcur - xnew) <= tol2;
if (dobreak) break;
if ((fx - fxnew) < alpha * t) continue;
if (((fx - fxnew) <= tol) || (norm2(xcur - xnew) <= tol2)) {
dobreak = true;
break;
}
dobreak = (fx - fxnew) <= tol;
break;
}
}
fx = fxnew;
Expand Down
2 changes: 1 addition & 1 deletion include/Math/LinearAlgebra.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ constexpr auto factorize(MutSquarePtrMatrix<T> A) -> Fact<T> {
invariant(M == A.numCol());
for (ptrdiff_t k = 0; k < M; ++k) {
T Akk = A(k, k);
if constexpr (ForcePD) Akk = std::max(Akk, T(0.01));
if constexpr (ForcePD) Akk = std::max(Akk, T(0.001));
T invAkk = 1.0 / Akk;
A(k, k) = invAkk;
A(_(k + 1, M), k) *= invAkk;
Expand Down
9 changes: 6 additions & 3 deletions test/boxopt_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "Math/BoxOpt.hpp"
#include "Math/Exp.hpp"
#include <gtest/gtest.h>
#include <iostream>

constexpr auto fcore(auto u1, auto u2) {
return (2 * u1 + u2 + u1 * u2) / (u1 * u2);
Expand All @@ -26,13 +27,15 @@ TEST(BoxOptTest, BasicAssertions) {
double opt0 = poly::math::minimize(&arena, x0, box, fsoft);
double u0 = poly::math::BoxTransformVector{x0.view(), box}[0];
double u1 = poly::math::BoxTransformVector{x0.view(), box}[1];
EXPECT_LT(std::abs(3.4567718680186568 - u0), 1e-6);
EXPECT_LT(std::abs(7.799906157078232 - u1), 1e-6);
std::cout << "u0 = " << u0 << "; u1 = " << u1 << '\n';
EXPECT_LT(std::abs(3.4567 - u0), 1e-3);
EXPECT_LT(std::abs(7.8 - u1), 1e-3);
box.decreaseUpperBound(x0, 0, 3);
double opt1 = poly::math::minimize(&arena, x0, box, fsoft);
EXPECT_LT(opt0, opt1);
double u01 = poly::math::BoxTransformVector{x0.view(), box}[0];
double u11 = poly::math::BoxTransformVector{x0.view(), box}[1];
std::cout << "u01 = " << u01 << "; u11 = " << u11 << '\n';
EXPECT_EQ(u01, 3.0);
EXPECT_LT(std::abs(9.132451832031007 - u11), 1e-6);
EXPECT_LT(std::abs(9.132 - u11), 1e-3);
};

0 comments on commit 3ba28d4

Please sign in to comment.