Skip to content

Commit

Permalink
Improvements and corrections in Rcpp functions (#4)
Browse files Browse the repository at this point in the history
* correct input list algo in Rcpp volume.cpp

* Improvements in rounding and rotating R functions (#76)

* add seed in rotate_polytope and update Rd files

* update R rounding function to return the linear map

* fix rounding in V-poly

* overloading rotating() function and remove unused comments

* implement zonotope volume computation with cb algorithm and hpoly in MMC

* fix all bugs in new cooling_hpoly algorithm and improve test function

* all tests for zonotopes with c algortihm passed successfully

* implmentation of diameter computation of all convex bodies except Vpoly-intersection

* add Vpolyintersection diameter computation and volume approximation

* new implmentation of rounding

* add rounding in zonotope volume approximation with Hpoly in MMC

* dispatching in ratio estimation

* update ratio estimation functions

* implmemrnt dispatching for diameter computation

* improve ratio estimation code structure

* update declaration of parameters in random walks

* update parameter declaretion for both ball and billiard walks

* modify Rcpp expose for volume and sampling C++ functions

* modify Rcpp rounding function

* update rounding, sampling, volume rcpp dunctions: typedefs and includes

* modify zonotope approximation Rcpp function. minor changes to Rcpp volume and sampling functions

* fix compiler errors

* update d files

* fix compiler errors in c++ tests

* add seed in rounding and direct_sampling Rcpp functions

* add a second volume function to use fixed seed in Rcpp interface

* fix rcpp interface
  • Loading branch information
TolisChal authored and vissarion committed May 18, 2020
1 parent 1471d38 commit d3ef616
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 39 deletions.
13 changes: 7 additions & 6 deletions R-proj/src/rounding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <boost/random/uniform_int.hpp>
#include <boost/random/normal_distribution.hpp>
#include <boost/random/uniform_real_distribution.hpp>
#include "random_walks/random_walks.hpp"
#include "volume_sequence_of_balls.hpp"
#include "volume_cooling_gaussians.hpp"
#include "extractMatPoly.h"
Expand Down Expand Up @@ -91,32 +92,32 @@ Rcpp::List rounding (Rcpp::Reference P, Rcpp::Nullable<double> seed = R_NilValue
switch (type) {
case 1: {
if (cdhr) {
std::pair <std::pair<MT, VT>, NT> res = round_polytope<CDHRWalk, MT, VT>(HP, InnerBall, walkL,
round_res = round_polytope<CDHRWalk, MT, VT>(HP, InnerBall, walkL,
rng);
} else {
std::pair <std::pair<MT, VT>, NT> res = round_polytope<BilliardWalk, MT, VT>(HP, InnerBall, walkL,
round_res = round_polytope<BilliardWalk, MT, VT>(HP, InnerBall, walkL,
rng);
}
Mat = extractMatPoly(HP);
break;
}
case 2: {
if (cdhr) {
std::pair <std::pair<MT, VT>, NT> res = round_polytope<CDHRWalk, MT, VT>(VP, InnerBall, walkL,
round_res = round_polytope<CDHRWalk, MT, VT>(VP, InnerBall, walkL,
rng);
} else {
std::pair <std::pair<MT, VT>, NT> res = round_polytope<BilliardWalk, MT, VT>(VP, InnerBall, walkL,
round_res = round_polytope<BilliardWalk, MT, VT>(VP, InnerBall, walkL,
rng);
}
Mat = extractMatPoly(VP);
break;
}
case 3: {
if (cdhr) {
std::pair <std::pair<MT, VT>, NT> res = round_polytope<CDHRWalk, MT, VT>(ZP, InnerBall, walkL,
round_res = round_polytope<CDHRWalk, MT, VT>(ZP, InnerBall, walkL,
rng);
} else {
std::pair <std::pair<MT, VT>, NT> res = round_polytope<BilliardWalk, MT, VT>(ZP, InnerBall, walkL,
round_res = round_polytope<BilliardWalk, MT, VT>(ZP, InnerBall, walkL,
rng);
}
Mat = extractMatPoly(ZP);
Expand Down
1 change: 1 addition & 0 deletions R-proj/src/sample_points.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <boost/random/uniform_int.hpp>
#include <boost/random/normal_distribution.hpp>
#include <boost/random/uniform_real_distribution.hpp>
#include "random_walks/random_walks.hpp"
#include "volume_sequence_of_balls.hpp"
#include "volume_cooling_gaussians.hpp"
#include "sample_only.h"
Expand Down
12 changes: 7 additions & 5 deletions R-proj/src/volume.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include <boost/random/uniform_int.hpp>
#include <boost/random/normal_distribution.hpp>
#include <boost/random/uniform_real_distribution.hpp>
#include "random_walks/random_walks.hpp"
#include "volume_cooling_gaussians.hpp"
#include "volume_sequence_of_balls.hpp"
#include "volume_cooling_gaussians.hpp"
#include "volume_cooling_balls.hpp"
Expand Down Expand Up @@ -47,11 +49,11 @@ double generic_volume(Polytope& P, RNGType &rng, unsigned int walk_length, NT e,
NT vol;
if (CG) {
if (cdhr) {
vol = volume_gaussian_annealing<GaussianCDHRWalk>(P, rng, e, walk_length);
vol = volume_cooling_gaussians<GaussianCDHRWalk>(P, rng, e, walk_length);
} else if (rdhr) {
vol = volume_gaussian_annealing<GaussianRDHRWalk>(P, rng, e, walk_length);
vol = volume_cooling_gaussians<GaussianRDHRWalk>(P, rng, e, walk_length);
} else {
vol = volume_gaussian_annealing<GaussianBallWalk>(P, rng, e, walk_length);
vol = volume_cooling_gaussians<GaussianBallWalk>(P, rng, e, walk_length);
}
} else if (CB) {
if (cdhr) {
Expand Down Expand Up @@ -261,12 +263,12 @@ double volume (Rcpp::Reference P,
hpoly = Rcpp::as<bool>(Rcpp::as<Rcpp::List>(settings)["hpoly"]);
if (hpoly && !CB)
Rf_warning("flag 'hpoly' can be used to only in MMC of CB algorithm for zonotopes.");
} else if (ZP.num_of_generators() / ZP.dimension() < 5 ) {
} else if (ZP.num_of_generators() / ZP.dimension() < 5) {
hpoly = true;
} else {
hpoly = false;
}
if (hpoly) {
if (hpoly && CB) {
if (cdhr) {
return volume_cooling_hpoly<CDHRWalk, Hpolytope>(ZP, rng, e, walkL, win_len);
} else if (rdhr) {
Expand Down
1 change: 1 addition & 0 deletions R-proj/src/zonotope_approximation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <boost/random/uniform_int.hpp>
#include <boost/random/normal_distribution.hpp>
#include <boost/random/uniform_real_distribution.hpp>
#include "random_walks/random_walks.hpp"
#include "volume_sequence_of_balls.hpp"
#include "volume_cooling_gaussians.hpp"
#include "volume_cooling_balls.hpp"
Expand Down
22 changes: 15 additions & 7 deletions R-proj/tests/testthat/test_Hvol.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ context("H-polytopes' volume test")
library(volesti)


Hruntest <- function(P, name_string, exactvol, tol, num_of_exps, alg){
Hruntest <- function(P, name_string, exactvol, tol, num_of_exps, alg, seed){

vol = 0
for (j in 1:num_of_exps) {
if (alg == "CB") {
vol = vol + volume(P)
vol = vol + volume(P, seed = seed)
} else if (alg == "SOB") {
vol = vol + volume(P, settings = list("algorithm" = "SOB"), seed = seed)
} else {
vol = vol + volume(P, settings = list("algorithm" = "CG"))
vol = vol + volume(P, settings = list("algorithm" = "CG"), seed = seed)
}
}
vol = vol / num_of_exps
Expand All @@ -30,7 +32,7 @@ for (i in 1:2) {

if (i==1) {
algo = 'CG'
num_of_exps = 20
num_of_exps = 10
} else {
algo = 'CB'
num_of_exps = 10
Expand All @@ -39,20 +41,26 @@ for (i in 1:2) {

test_that("Volume H-cube10", {
P = gen_cube(10, 'H')
res = Hruntest(P, 'H-cube10', 1024, 0.2, num_of_exps, algo)
res = Hruntest(P, 'H-cube10', 1024, 0.2, num_of_exps, algo, 5)
expect_equal(res, 1)
})

test_that("Volume H-cross5", {
P = gen_cross(5, 'H')
res = Hruntest(P, 'H-cross10', 0.2666667, 0.2, num_of_exps, algo)
res = Hruntest(P, 'H-cross10', 0.2666667, 0.2, num_of_exps, algo, 5)
expect_equal(res, 1)
})


test_that("Volume H-prod_simplex_5_5", {
P = gen_prod_simplex(5)
res = Hruntest(P, 'H-prod_simplex_5_5', (1/prod(1:5))^2, 0.2, num_of_exps, algo)
res = Hruntest(P, 'H-prod_simplex_5_5', (1/prod(1:5))^2, 0.2, num_of_exps, algo, 5)
expect_equal(res, 1)
})

test_that("Volume H-cube10", {
P = gen_cube(10, 'H')
res = Hruntest(P, 'H-cube10', 1024, 0.2, 5, "SOB", 5)
expect_equal(res, 1)
})

Expand Down
16 changes: 8 additions & 8 deletions R-proj/tests/testthat/test_Vvol.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ context("V-polytopes' volume test")

library(volesti)

Vruntest <- function(P, name_string, exactvol, tol, num_of_exps, algorithm){
Vruntest <- function(P, name_string, exactvol, tol, num_of_exps, algorithm,seed){

vol = 0
for (j in 1:num_of_exps) {
if (algorithm == "CB") {
vol = vol + volume(P)
vol = vol + volume(P, rounding = FALSE, seed = seed)
} else {
vol = vol + volume(P, settings = list("algorithm" = "CG", "error" = 0.1))
vol = vol + volume(P, settings = list("algorithm" = "CG", "error" = 0.1), rounding = FALSE, seed = seed)
}
}
vol = vol / num_of_exps
Expand All @@ -23,21 +23,21 @@ Vruntest <- function(P, name_string, exactvol, tol, num_of_exps, algorithm){
}

cran_only = TRUE
num_of_exps = 6
num_of_exps = 2

for (i in 1:2) {

seed = 5
if (i==1) {
algo = 'CG'
tol = 0.4
tol = 0.2
} else {
algo = 'CB'
tol = 0.3
tol = 0.2
}

test_that("Volume V-simplex3", {
P = gen_simplex(3, 'V')
res = Vruntest(P, 'V-simplex3', 1/prod(1:3), tol, num_of_exps, algo)
res = Vruntest(P, 'V-simplex3', 1/prod(1:3), tol, num_of_exps, algo, seed)
expect_equal(res, 1)
})

Expand Down
16 changes: 8 additions & 8 deletions R-proj/tests/testthat/test_Zvol.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ context("Zonotopes' volume test")

library(volesti)

Zruntest <- function(P, name_string, tol, num_of_exps, algo){
Zruntest <- function(P, name_string, tol, num_of_exps, algo, seed){

exactvol = exact_vol(P)
vol = 0
for (j in 1:num_of_exps) {
if (algo == "CB") {
vol = vol + volume(P, rounding=FALSE)
vol = vol + volume(P, settings = list("hpoly" = FALSE), rounding = FALSE, seed = seed)
} else {
vol = vol + volume(P, settings = list("algorithm" = "CG", "error" = 0.1), rounding=TRUE)
vol = vol + volume(P, settings = list("algorithm" = "CG", "error" = 0.1), rounding = FALSE, seed = seed)
}
}
vol = vol / num_of_exps
Expand All @@ -24,20 +24,20 @@ Zruntest <- function(P, name_string, tol, num_of_exps, algo){
}

cran_only = TRUE
num_of_exps = 5
num_of_exps = 2

for (i in 1:2) {
if (i==1) {
algo = 'CG'
tol = 0.4
tol = 0.2
} else {
algo = 'CB'
tol = 0.3
tol = 0.2
}

test_that("Volume Zonotope_2_4", {
Z = gen_rand_zonotope(2, 4)
res = Zruntest(Z, 'Zonotope_2_4', tol, num_of_exps, algo)
Z = gen_rand_zonotope(2, 4, seed = 127)
res = Zruntest(Z, 'Zonotope_2_4', tol, num_of_exps, algo, 5)
expect_equal(res, 1)
})

Expand Down
9 changes: 5 additions & 4 deletions R-proj/tests/testthat/test_rounding.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ context("Rounding test")

library(volesti)

testRound <- function(P, exactvol, tol, name_string, num_of_exps, algo, rotation){
testRound <- function(P, exactvol, tol, name_string, num_of_exps, algo, rotation,seed){

if (rotation) {
P = rand_rotate(P)
Expand All @@ -13,9 +13,9 @@ testRound <- function(P, exactvol, tol, name_string, num_of_exps, algo, rotation
vol = 0
for (j in 1:num_of_exps) {
if (algo == "CB") {
vol = vol + listHpoly$round_value * volume(listHpoly$P)
vol = vol + listHpoly$round_value * volume(listHpoly$P, seed = seed)
} else {
vol = vol + listHpoly$round_value * volume(listHpoly$P, settings=list("algorithm"="CG", "error"=0.1))
vol = vol + listHpoly$round_value * volume(listHpoly$P, settings=list("algorithm"="CG", "error"=0.1), seed = seed)
}
}
vol = vol / num_of_exps
Expand All @@ -39,8 +39,9 @@ for (i in 1:2) {


test_that("Rounding H-skinny_cube10", {
seed=5
P = gen_skinny_cube(10)
res = testRound(P, 102400, 0.3, 'H-skinny_cube10', num_of_exps, 'CB', FALSE)
res = testRound(P, 102400, 0.3, 'H-skinny_cube10', num_of_exps, 'CB', FALSE,seed)
expect_equal(res, 1)
})

Expand Down
6 changes: 6 additions & 0 deletions include/random_walks/uniform_billiard_walk.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -384,4 +384,10 @@ private :
};








#endif // RANDOM_WALKS_UNIFORM_BILLIARD_WALK_HPP
2 changes: 1 addition & 1 deletion include/volume/rounding.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ std::pair< std::pair<MT, VT>, NT > round_polytope(Polytope &P, std::pair<Point,
shift += L_1.transpose() * e;
}
//shift += T.inverse() * e;
T.noalias() = T * L_1;
T *= L_1;
P.linear_transformIt(L_1.transpose());
InnerBall = P.ComputeInnerBall();
round_val *= L_1.determinant();
Expand Down

0 comments on commit d3ef616

Please sign in to comment.