Skip to content

Commit

Permalink
Position Nudging after Position Update (GeomScale#308)
Browse files Browse the repository at this point in the history
* Position Nudging after Position Update

* Complexity improvements and Polytope Normalization

* HPolytope Normalization Flag

* Polytope normalization within Walk Constructor

* Alias HPolytope Normalization for Nudging inside Gaussian HMC

* Polytope Normalization in ComputeInner Ball Fixed

* Polytope Normalization Style change

* Nudge in function within Gaussian HMC, and restore Hpoly file

* More efficient Nudge in Process
  • Loading branch information
vgnecula authored and TolisChal committed Jun 25, 2024
1 parent 50e2c56 commit a77af2a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
2 changes: 1 addition & 1 deletion include/convex_bodies/hpolytope.h
Original file line number Diff line number Diff line change
Expand Up @@ -927,4 +927,4 @@ class HPolytope {
}
};

#endif
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ struct Walk
typedef typename Polytope::PointType Point;
typedef typename Point::FT NT;
typedef typename Polytope::VT VT;
typedef typename Polytope::MT MT;

template <typename GenericPolytope>
Walk(GenericPolytope &P, Point const& p, NT const& a_i, RandomNumberGenerator &rng)
Expand Down Expand Up @@ -80,7 +81,7 @@ struct Walk
<
typename GenericPolytope
>
inline void apply(GenericPolytope& P,
inline void apply(GenericPolytope const& P,
Point& p,
NT const& a_i,
unsigned int const& walk_length,
Expand All @@ -89,6 +90,9 @@ struct Walk
unsigned int n = P.dimension();
NT T;

GenericPolytope P_normalized = P;
P_normalized.normalize();

for (auto j=0u; j<walk_length; ++j)
{
T = rng.sample_urdist() * _Len;
Expand All @@ -105,6 +109,7 @@ struct Walk
_lambda_prev = pbpair.first;
T -= _lambda_prev;
update_position(_p, _v, _lambda_prev, _omega);
nudge_in(P_normalized, _p);
P.compute_reflection(_v, _p, pbpair.second);
it++;
}
Expand All @@ -120,7 +125,7 @@ struct Walk
<
typename GenericPolytope
>
inline void get_starting_point(GenericPolytope& P,
inline void get_starting_point(GenericPolytope const& P,
Point const& center,
Point &q,
unsigned int const& walk_length,
Expand All @@ -141,7 +146,7 @@ struct Walk
<
typename GenericPolytope
>
inline void parameters_burnin(GenericPolytope& P,
inline void parameters_burnin(GenericPolytope const& P,
Point const& center,
unsigned int const& num_points,
unsigned int const& walk_length,
Expand Down Expand Up @@ -191,7 +196,7 @@ private :
<
typename GenericPolytope
>
inline void initialize(GenericPolytope& P,
inline void initialize(GenericPolytope const& P,
Point const& p,
NT const& a_i,
RandomNumberGenerator &rng)
Expand All @@ -204,6 +209,9 @@ private :
NT T = rng.sample_urdist() * _Len;
int it = 0;

GenericPolytope P_normalized = P;
P_normalized.normalize();

while (it <= _rho)
{
auto pbpair
Expand All @@ -218,12 +226,50 @@ private :
}
_lambda_prev = pbpair.first;
update_position(_p, _v, _lambda_prev, _omega);
nudge_in(P_normalized, _p);
T -= _lambda_prev;
P.compute_reflection(_v, _p, pbpair.second);
it++;
}
}

template
<
typename GenericPolytope
>
inline void nudge_in(GenericPolytope& P, Point& p, NT tol=NT(0))
{
MT A = P.get_mat();
VT b = P.get_vec();
int m = A.rows();

VT b_Ax = b - A * p.getCoefficients();
const NT* b_Ax_data = b_Ax.data();

NT dist;

for (int i = 0; i < m; i++) {

dist = *b_Ax_data;

if (dist < NT(-tol)){
//Nudging correction
NT eps = -1e-7;

NT eps_1 = -dist;
//A.row is already normalized, no need to do it again
VT A_i = A.row(i);
NT eps_2 = eps_1 + eps;

//Nudge the point inside with respect to the normal its vector
Point shift(A_i);
shift.operator*=(eps_2);
p.operator+=(shift);
}
b_Ax_data++;
}
}

inline void update_position(Point &p, Point &v, NT const& T, NT const& omega)
{
NT next_p, next_v;
Expand Down Expand Up @@ -274,4 +320,3 @@ private :


#endif // RANDOM_WALKS_GAUSSIAN_HMC_WALK_HPP

0 comments on commit a77af2a

Please sign in to comment.