Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReHMC support for spectrahedra and general convex bodies with refactored Spectrahedra classes #194

Merged
merged 69 commits into from
Jan 4, 2022
Merged
Show file tree
Hide file tree
Changes from 52 commits
Commits
Show all changes
69 commits
Select commit Hold shift + click to select a range
f2040cd
Squash commits into one
papachristoumarios Feb 26, 2021
37d54dd
Minor changes
papachristoumarios Feb 26, 2021
1bfc766
Remove merge blocks
papachristoumarios Feb 26, 2021
7b84432
Disable MKL in github actions
papachristoumarios Feb 26, 2021
619e1e3
Fix CircleCI tests
papachristoumarios Feb 26, 2021
70498e0
Fix step() method in R bindings
papachristoumarios Feb 26, 2021
eea6f69
Disactivate sdp test
papachristoumarios Feb 27, 2021
8ddb5ef
Rebuild documentation with roxygen
papachristoumarios Feb 27, 2021
a7fe93e
Rebuild documentation with roxygen
papachristoumarios Feb 27, 2021
6177f56
Add ess docs
papachristoumarios Feb 27, 2021
9ada49f
Fix ess docs
papachristoumarios Feb 27, 2021
128bcce
Fix ess docs
papachristoumarios Feb 27, 2021
7df13c2
Fix random polytope generator
papachristoumarios Mar 4, 2021
1e8eff3
Reorder class fields
papachristoumarios Mar 4, 2021
46f97b9
Remove ESS computation via stan
papachristoumarios Mar 12, 2021
eda5ff0
Add volesti implementation of ESS and test correctness
papachristoumarios Mar 12, 2021
edb06cb
Add const& to effective_sample_size
papachristoumarios Mar 12, 2021
a4f910b
Refactor and move mat_convert script
papachristoumarios Mar 15, 2021
23a1ad2
Refactoring
papachristoumarios Mar 15, 2021
20f0714
Remove removed header
papachristoumarios Mar 17, 2021
38fca74
Move R examples to separate folder
papachristoumarios Mar 17, 2021
aec692d
Add R documentation entries
papachristoumarios Mar 17, 2021
f3c0ea3
Add missing .Rd files
papachristoumarios Mar 18, 2021
d869141
Complete R documentation entries
papachristoumarios Mar 19, 2021
61a6eec
Complete R documentation entries
papachristoumarios Mar 19, 2021
c96ebb9
Complete R documentation entries
papachristoumarios Mar 19, 2021
6ac5e63
Rename R files to have ext .R and removed binary
papachristoumarios Mar 19, 2021
aa4fa38
Find libraries in OSx systems
papachristoumarios Mar 21, 2021
641cded
Merge branch 'develop' of https://github.com/GeomScale/volume_approxi…
papachristoumarios Mar 21, 2021
294b149
Move Rcpp oracle functors to R project dir
papachristoumarios Apr 3, 2021
9cf0cd7
Merge branch 'icml-2021' of github.com:papachristoumarios/volume_appr…
papachristoumarios Apr 3, 2021
3fa3572
Change .hpp extension to .h
papachristoumarios Apr 5, 2021
10637c1
Add support for OSX for cmake
papachristoumarios Apr 21, 2021
a14e418
Fixes
papachristoumarios Apr 21, 2021
ef3a0ae
Minor modifications
papachristoumarios May 17, 2021
53221d8
Add generic convex body implementation
papachristoumarios May 18, 2021
dfc725f
Add generic convex body implementation
papachristoumarios May 18, 2021
9dfe36c
add new spectrahedron class
TolisChal May 18, 2021
aeb6685
Add instances
papachristoumarios May 18, 2021
3da0517
spectrahedra implmentation, tests, and R interface
TolisChal May 24, 2021
a948bca
Minor modifications
papachristoumarios May 24, 2021
dd23163
Resolve merge conflicts
papachristoumarios May 24, 2021
915e348
Merge branch 'hmc_spectra' of github.com:TolisChal/volume_approximati…
papachristoumarios May 24, 2021
caf3d98
Merge branch 'hmc_spectra' into icml-2021
papachristoumarios May 24, 2021
491d088
Fix makefile
papachristoumarios May 24, 2021
9e43263
Add missing file
papachristoumarios Jun 19, 2021
0ee73a6
fix conflicts frm develop
TolisChal Dec 5, 2021
62c760f
update spectrahedron class
TolisChal Dec 6, 2021
0685ec8
delete unused files
TolisChal Dec 6, 2021
27a3d29
merge spectrahedron files and update examples and R interface
TolisChal Dec 10, 2021
262d590
remove unused comments
TolisChal Dec 10, 2021
410c843
Merge pull request #2 from TolisChal/spectra
papachristoumarios Dec 12, 2021
b13a939
Minor fix
papachristoumarios Dec 20, 2021
6cff201
Minor changes
papachristoumarios Dec 20, 2021
cf89f81
Linear optimization with log-concave sampling
papachristoumarios Dec 20, 2021
6cb84df
OSX Fix
papachristoumarios Dec 20, 2021
da113d0
Merge branch 'toms' of github.com:papachristoumarios/volume_approxima…
papachristoumarios Dec 20, 2021
a90b9e8
Add diagnostics
papachristoumarios Dec 21, 2021
6c3483e
Fix makefile
papachristoumarios Dec 21, 2021
1063d15
Merge branch 'toms' of github.com:papachristoumarios/volume_approxima…
papachristoumarios Dec 21, 2021
e281ff5
Add grid search to optimization
papachristoumarios Dec 21, 2021
37963b1
merge develop
TolisChal Dec 21, 2021
ea3a6ef
resolve pr comments
TolisChal Dec 21, 2021
3fd78b8
Add one more diagnostic
papachristoumarios Dec 21, 2021
6bea5df
Merge branch 'toms' of https://github.com/papachristoumarios/volume_a…
TolisChal Dec 22, 2021
50f49b0
update the c++ tests
TolisChal Dec 22, 2021
aed2fe2
Merge pull request #3 from TolisChal/spectra
papachristoumarios Dec 23, 2021
174cc73
resolve cran errors
TolisChal Dec 24, 2021
b7f7c3a
Merge pull request #4 from TolisChal/spectra
papachristoumarios Dec 26, 2021
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 R-proj/NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ importFrom("stats","cov")
importFrom("utils","read.csv")
importFrom(Rcpp,evalCpp)
importFrom(Rcpp,loadModule)
useDynLib(volesti, .registration=TRUE)
useDynLib(volesti, .registration=TRUE)
6 changes: 5 additions & 1 deletion R-proj/R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ direct_sampling <- function(body, n, seed = NULL) {
#' Gelman-Rubin and Brooks-Gelman Potential Scale Reduction Factor (PSRF) for each marginal
#'
#' @param samples A matrix that contans column-wise the sampled points from a geometric random walk.
#' @param method A string to reauest diagnostic: (i) \code{'normal'} for psrf of Gelman-Rubin and (ii) \code{'interval'} for psrf of Brooks-Gelman.
#'
#' @references \cite{Gelman, A. and Rubin, D. B.,
#' \dQuote{Inference from iterative simulation using multiple sequences,} \emph{Statistical Science,} 1992.}
Expand Down Expand Up @@ -377,6 +376,11 @@ sample_points <- function(P, n, random_walk = NULL, distribution = NULL, seed =
.Call(`_volesti_sample_points`, P, n, random_walk, distribution, seed)
}

#' @export
sample_spectra <- function(file = NULL, N = NULL, walk_length = NULL) {
.Call(`_volesti_sample_spectra`, file, N, walk_length)
}

#' Write a SDPA format file
#'
#' Outputs a spectrahedron (the matrices defining a linear matrix inequality) and a vector (the objective function)
Expand Down
2 changes: 1 addition & 1 deletion R-proj/src/ode_solve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include "volume/volume_cooling_gaussians.hpp"
#include "sampling/sampling.hpp"
#include "ode_solvers/ode_solvers.hpp"
#include "ode_solvers/oracle_functors_rcpp.hpp"
#include "oracle_functors_rcpp.h"

template <
typename Solver,
Expand Down
2 changes: 1 addition & 1 deletion R-proj/src/sample_points.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include "volume/volume_cooling_gaussians.hpp"
#include "sampling/sampling.hpp"
#include "ode_solvers/ode_solvers.hpp"
#include "ode_solvers/oracle_functors_rcpp.hpp"
#include "oracle_functors_rcpp.h"

enum random_walks {
ball_walk,
Expand Down
4 changes: 2 additions & 2 deletions R-proj/src/spectrahedron.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ void writeSdpaFormatFile(Rcpp::Nullable<Rcpp::Reference> spectrahedron = R_NilVa
typedef typename Kernel::Point Point;
typedef boost::mt19937 RNGType;
typedef LMI<NT, MT, VT> LMI;
typedef Spectrahedron<NT, MT, VT> Spectrahedron;
typedef Spectrahedron<Point> Spectrahedron;

std::vector<MT> matrices = Rcpp::as<std::vector<MT> > (Rcpp::as<Rcpp::Reference> (spectrahedron).field("matrices"));
LMI lmi(matrices);
Expand Down Expand Up @@ -91,7 +91,7 @@ Rcpp::List loadSdpaFormatFile(Rcpp::Nullable<std::string> inputFile = R_NilValue
typedef typename Kernel::Point Point;
typedef boost::mt19937 RNGType;
typedef LMI<NT, MT, VT> LMI;
typedef Spectrahedron<NT, MT, VT> Spectrahedron;
typedef Spectrahedron<Point> Spectrahedron;

Spectrahedron _spectrahedron;
Point c;
Expand Down
3 changes: 3 additions & 0 deletions R-proj/test_spectra.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
library(volesti)

uniform_points = sample_spectra('sdp_prob_2_6.txt', N=3000)
35 changes: 35 additions & 0 deletions examples/spectrahedra/Boost.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
function(GetBoost)

find_path(BOOST_DIR NAMES boost PATHS ../external/)

if (NOT BOOST_DIR)

set(BOOST_URL "https://boostorg.jfrog.io/artifactory/main/release/1.76.0/source/boost_1_76_0.tar.bz2" CACHE STRING "Boost download URL")
set(BOOST_URL_SHA256 "f0397ba6e982c4450f27bf32a2a83292aba035b827a5623a14636ea583318c41" CACHE STRING "Boost download URL SHA256 checksum")

include(FetchContent)
FetchContent_Declare(
Boost
URL ${BOOST_URL}
URL_HASH SHA256=${BOOST_URL_SHA256}
)
FetchContent_GetProperties(Boost)

if(NOT Boost_POPULATED)
message(STATUS "Fetching Boost")
FetchContent_Populate(Boost)
message(STATUS "Fetching Boost - done")
set(BOOST_DIR ${boost_SOURCE_DIR})
endif()

message(STATUS "Using downloaded Boost library at ${BOOST_DIR}")

else ()
message(STATUS "Boost Library found: ${BOOST_DIR}")

endif()

include_directories(${BOOST_DIR})
include_directories(${BOOST_DIR}/boost)

endfunction()
25 changes: 23 additions & 2 deletions examples/spectrahedra/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,30 @@
# Contributed and/or modified by Repouskos Panagiotis, as part of Google Summer of Code 2019 program.
# Licensed under GNU LGPL.3, see LICENCE file

cmake_minimum_required(VERSION 3.16)

if(COMMAND cmake_policy)
cmake_policy(SET CMP0003 NEW)
endif(COMMAND cmake_policy)


add_executable (read_write_sdpa_file read_write_sdpa_file.cpp)

include("Eigen.cmake")
GetEigen()

include("Boost.cmake")
GetBoost()

include_directories (BEFORE ../../external)
include_directories (BEFORE ../../include/generators)
include_directories (BEFORE ../../include/volume)
include_directories (BEFORE ../../include)
include_directories (BEFORE ../../include/convex_bodies)
include_directories (BEFORE ../../include/convex_bodies/spectrahedra)
include_directories (BEFORE ../../include/annealing)
include_directories (BEFORE ../../include/samplers)
include_directories (BEFORE ../../include/misc)

# Find LAPACK and BLAS
# OPENBLAS or ( ( SystemOpenblas or BLAS) and LAPACK)
Expand All @@ -26,7 +47,7 @@ IF (OPENBLAS_LIB)
message( STATUS "ARPACK_LIB found: ${ARPACK_LIB}" )

# Query gfortran to get the libgfortran path
FIND_LIBRARY(GFORTRAN_LIB NAMES libgfortran.so PATHS /usr/lib/gcc/x86_64-linux-gnu/8/)
FIND_LIBRARY(GFORTRAN_LIB NAMES libgfortran.so PATHS /usr/lib/gcc/x86_64-linux-gnu/9/)

# Find libgfortran (static preferred)
# Query gfortran to get the libgfortran path
Expand Down Expand Up @@ -61,7 +82,7 @@ IF (OPENBLAS_LIB)
TARGET_LINK_LIBRARIES(boltzmann_hmc_walk ${ARPACK_LIB} ${LAPACK_LIBRARIES} ${GFORTRAN_LIB})

add_executable (solve_sdp solve_sdp.cpp)
TARGET_LINK_LIBRARIES(solve_sdp ${ARPACK_LIB} ${LAPACK_LIBRARIES} ${GFORTRAN_LIB})
TARGET_LINK_LIBRARIES(solve_sdp ${GFORTRAN_LIB} ${LAPACK_LIBRARIES} ${ARPACK_LIB})

ELSE()
MESSAGE(STATUS "gfortran is required but it could not be found")
Expand Down
30 changes: 30 additions & 0 deletions examples/spectrahedra/Eigen.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
function(GetEigen)
find_path(EIGEN_DIR NAMES Eigen PATHS ../../external)

if (NOT EIGEN_DIR)
include(FetchContent)
FetchContent_Declare(
eigen
GIT_REPOSITORY https://gitlab.com/libeigen/eigen.git
GIT_TAG 3.4.0
)

FetchContent_GetProperties(eigen)

if(NOT eigen_POPULATED)
message(STATUS "Eigen library not found locally, downloading it.")
FetchContent_Populate(eigen)
endif()

set(EIGEN_DIR ${eigen_SOURCE_DIR})
message(STATUS "Using downloaded Eigen library at: ${EIGEN_DIR}")

else ()

message(STATUS "Eigen Library found: ${EIGEN_DIR}")

endif()

include_directories(${EIGEN_DIR})

endfunction()
4 changes: 2 additions & 2 deletions examples/spectrahedra/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ In the folder "examples", clone this repo:

```bash
git clone https://github.com/m-reuter/arpackpp
cd arpackcpp
cd arpackpp
```

It has two scripts that should easily install the libraries:
Expand Down Expand Up @@ -88,7 +88,7 @@ The contents of the file are:
1 -0 -0
```


It represents a spectrahedron in 2 dimensions, described by a linear matrix inequality with
3x3 matrices, and a linear objective function.

Expand Down
2 changes: 1 addition & 1 deletion examples/spectrahedra/boltzmann_hmc_walk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ typedef Eigen::Matrix<NT, Eigen::Dynamic, 1> VT;
typedef Eigen::Matrix <NT, Eigen::Dynamic, Eigen::Dynamic> MT;
typedef Cartesian <NT> Kernel;
typedef typename Kernel::Point Point;
typedef Spectrahedron <NT, MT, VT> SPECTRAHEDRON;
typedef Spectrahedron <Point> SPECTRAHEDRON;
typedef BoostRandomNumberGenerator<boost::mt19937, NT> RNGType;
typedef BoltzmannHMCWalk::Walk<SPECTRAHEDRON, RNGType> HmcWalk;
typedef BoltzmannHMCWalk::Walk<SPECTRAHEDRON, RNGType>::Settings HmcWalkSettings;
Expand Down
2 changes: 1 addition & 1 deletion examples/spectrahedra/read_write_sdpa_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ typedef Eigen::Matrix<NT, Eigen::Dynamic, 1> VT;
typedef Eigen::Matrix <NT, Eigen::Dynamic, Eigen::Dynamic> MT;
typedef Cartesian <NT> Kernel;
typedef typename Kernel::Point Point;
typedef Spectrahedron <NT, MT, VT> SPECTRAHEDRON;
typedef Spectrahedron <Point> SPECTRAHEDRON;


int main(int argc, char* argv[]) {
Expand Down
2 changes: 1 addition & 1 deletion examples/spectrahedra/solve_sdp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ typedef Eigen::Matrix<NT, Eigen::Dynamic, 1> VT;
typedef Eigen::Matrix <NT, Eigen::Dynamic, Eigen::Dynamic> MT;
typedef Cartesian <NT> Kernel;
typedef typename Kernel::Point Point;
typedef Spectrahedron <NT, MT, VT> SPECTRAHEDRON;
typedef Spectrahedron <Point> SPECTRAHEDRON;


int main(int argc, char* argv[]) {
Expand Down
10 changes: 5 additions & 5 deletions include/SDPAFormatManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -244,13 +244,13 @@ class SdpaFormatManager {
/// \param[in] is opened stream to input file
/// \param[out] spectrahedron
/// \param[out] objectiveFunction
template <typename Point>
void loadSDPAFormatFile(std::ifstream &is, Spectrahedron<NT, MT, VT> &spectrahedron, Point &objectiveFunction) {
template <typename Spectrahedron, typename Point>
void loadSDPAFormatFile(std::ifstream &is, Spectrahedron &spectrahedron, Point &objectiveFunction) {
std::vector<MT> matrices;
VT coeffs;
loadSDPAFormatFile(is, matrices, coeffs);
LMI<NT, MT, VT> lmi(matrices);
spectrahedron = Spectrahedron<NT, MT, VT>(lmi);
spectrahedron = Spectrahedron(lmi);
objectiveFunction = Point(coeffs);
}

Expand All @@ -260,8 +260,8 @@ class SdpaFormatManager {
/// \param[in] is opened stream to output file
/// \param[in] spectrahedron
/// \param[in] objectiveFunction
template <typename Point>
void writeSDPAFormatFile(std::ostream &os, Spectrahedron<NT, MT, VT> const & spectrahedron, Point const & objectiveFunction) {
template <typename Spectrahedron, typename Point>
void writeSDPAFormatFile(std::ostream &os, Spectrahedron const & spectrahedron, Point const & objectiveFunction) {
writeSDPAFormatFile(os, spectrahedron.getLMI().getMatrices(), objectiveFunction.getCoefficients());
}
};
Expand Down
107 changes: 107 additions & 0 deletions include/convex_bodies/convex_body.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#ifndef CONVEX_BODY_H
#define CONVEX_BODY_H

#include <limits>
#include <iostream>
#include <vector>
#include <Eigen/Eigen>
#include <functional>

template <typename Point>
class ConvexBody {
public:
typedef Point PointType;
typedef typename Point::FT NT;
typedef typename std::vector<NT>::iterator viterator;
//using RowMatrixXd = Eigen::Matrix<NT, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>;
//typedef RowMatrixXd MT;
typedef Eigen::Matrix<NT, Eigen::Dynamic, Eigen::Dynamic> MT;
typedef Eigen::Matrix<NT, Eigen::Dynamic, 1> VT;
typedef std::function<NT(const Point&)> func;
typedef std::function<Point(const Point&)> grad;
private:
unsigned int dim; //dimension
std::vector<func> gs; // convex functions defining the convex body
std::vector<grad> grad_gs; // convex function gradients
unsigned int m;
NT tol = NT(1e-4);


public:

ConvexBody() : m(0) {}

ConvexBody(std::vector<func> gs_, std::vector<grad> grad_gs_, unsigned int dim_) :
gs(gs_), grad_gs(grad_gs_), dim(dim_)
{
m = gs.size();
}

unsigned int dimension() {
return dim;
}

// Compute positive line intersection (in [0, 1]) using Binary search
// x: starting point
// v: direction (ray)
std::pair<NT, int> line_positive_intersect(Point const& x, Point const &v) const {
NT t_min = NT(1);
int constraint = -1;
NT t;
for (unsigned int i = 0; i < m; i++) {
t = binary_search(x, v, gs[i]);
if (t < t_min) {
t_min = t;
constraint = i;
}
}

return std::make_pair(t_min, constraint);
}

NT binary_search(Point const &x, Point const &v, func const& f) const {
NT t_min = NT(0);
NT t_max = NT(1);
NT t;
NT value;

while (t_max - t_min > tol) {
t = (t_max + t_min) / 2;
value = f(x + t * v);
if (value >= -tol && value <= 0) {
return t;
} else if (value < -tol) {
t_min = t;
} else {
t_max = t;
}

}

return t;
}


// Computes unit normal at point p of the boundary
Point unit_normal(Point const& p, int const& constraint) const {
Point n = grad_gs[constraint](p);
return (1 / n.length()) * n;
}

// Computes reflection of v about point p on the boundary
void compute_reflection(Point &v, Point const& p, int const& constraint) const {
Point n = unit_normal(p, constraint);
v += -2 * v.dot(n) * n;
}

// Check if point is in K
int is_in(Point const& p, NT tol=NT(0)) {
for (func g : gs) {
if (g(p) > NT(-tol)) return 0;
}
return -1;
}

};

#endif
5 changes: 4 additions & 1 deletion include/convex_bodies/hpolytope.h
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,6 @@ class HPolytope {


//First coordinate ray intersecting convex polytope

std::pair<NT,NT> line_intersect_coord(Point const& r,
unsigned int const& rand_coord,
VT& lamdas) const
Expand Down Expand Up @@ -864,6 +863,8 @@ class HPolytope {
v += -2 * v.dot(A.row(facet)) * A.row(facet);
}

void resetFlags() {}

NT log_barrier(Point &x, NT t = NT(100)) const {
int m = num_of_hyperplanes();
NT total = NT(0);
Expand Down Expand Up @@ -898,6 +899,8 @@ class HPolytope {
v += a;
}

void update_position_internal(NT&){}

template <class bfunc, class NonLinearOracle>
std::tuple<NT, Point, int> curve_intersect(
NT t_prev,
Expand Down
Loading