Skip to content

Commit 0ccd791

Browse files
TolisChalvissarion
authored andcommitted
CRAN version 1.1.2 (#118)
* fix review comments on R package * update R interface in volume and sample_points * update Rcpp modules and S4 classes * replace modules with R classes * update R documentation * fix R tests * fix R bugs * add hpoly new generator, fix bugs in rounding with biw * update the Rd files * fix c++ tests and a bug in volume.coo * fix c++ test * update polytope random generators in R, update rd files * fix bugs in R interface * seperate sdp functions
1 parent 52a4f57 commit 0ccd791

File tree

83 files changed

+1134
-1036
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+1134
-1036
lines changed

R-proj/DESCRIPTION

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ Description: Provides an R interface for 'volesti' C++ package. 'volesti' comput
1111
(a.k.a. zonotopes). There are three algorithms for volume estimation as well as algorithms
1212
for sampling, rounding and rotating polytopes. Moreover, 'volesti' provides algorithms for
1313
estimating copulas useful in computational finance.
14-
Version: 1.1.1
15-
Date: 2020-06-18
14+
Version: 1.1.2
15+
Date: 2020-10-18
1616
Maintainer: Vissarion Fisikopoulos <[email protected]>
1717
Depends: Rcpp (>= 0.12.17)
1818
Imports: methods, stats
1919
LinkingTo: Rcpp, RcppEigen, BH
2020
Suggests: testthat
2121
Encoding: UTF-8
22-
RoxygenNote: 6.1.1
22+
RoxygenNote: 7.1.1
2323
BugReports: https://github.com/GeomScale/volume_approximation/issues

R-proj/NAMESPACE

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ export(compute_indicators)
44
export(copula)
55
export(direct_sampling)
66
export(exact_vol)
7-
export(file_to_polytope)
87
export(frustum_of_simplex)
98
export(gen_cross)
109
export(gen_cube)
@@ -16,13 +15,19 @@ export(gen_simplex)
1615
export(gen_skinny_cube)
1716
export(inner_ball)
1817
export(loadSdpaFormatFile)
18+
export(load_sdpa_format_file)
1919
export(readSdpaFormatFile)
2020
export(rotate_polytope)
2121
export(round_polytope)
2222
export(sample_points)
2323
export(volume)
24-
export(writeSdpaFormatFile)
24+
export(write_sdpa_format_file)
2525
export(zonotope_approximation)
26+
exportClasses(Hpolytope)
27+
exportClasses(Spectrahedron)
28+
exportClasses(Vpolytope)
29+
exportClasses(VpolytopeIntersection)
30+
exportClasses(Zonotope)
2631
exportPattern("^[[:alpha:]]+")
2732
importFrom("methods","new")
2833
importFrom("stats","cov")

R-proj/R/HpolytopeClass.R

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#' An R class to represent an H-polytope
2+
#'
3+
#' An H-polytope is a convex polytope defined by a set of linear inequalities or equivalently a \eqn{d}-dimensional H-polytope with \eqn{m} facets is defined by a \eqn{m\times d} matrix A and a \eqn{m}-dimensional vector b, s.t.: \eqn{Ax\leq b}.
4+
#'
5+
#' \describe{
6+
#' \item{A}{An \eqn{m\times d} numerical matrix.}
7+
#'
8+
#' \item{b}{An \eqn{m}-dimensional vector b.}
9+
#'
10+
#' \item{volume}{The volume of the polytope if it is known, \eqn{NaN} otherwise by default.}
11+
#'
12+
#' \item{type}{A character with default value 'Hpolytope', to declare the representation of the polytope.}
13+
#' }
14+
#'
15+
#' @examples
16+
#' A = matrix(c(-1,0,0,-1,1,1), ncol=2, nrow=3, byrow=TRUE)
17+
#' b = c(0,0,1)
18+
#' P = Hpolytope(A = A, b = b)
19+
#'
20+
#' @name Hpolytope-class
21+
#' @rdname Hpolytope-class
22+
#' @exportClass Hpolytope
23+
Hpolytope <- setClass (
24+
# Class name
25+
"Hpolytope",
26+
27+
# Defining slot type
28+
representation (
29+
A = "matrix",
30+
b = "numeric",
31+
volume = "numeric",
32+
type = "character"
33+
),
34+
35+
# Initializing slots
36+
prototype = list(
37+
A = as.matrix(0),
38+
b = as.numeric(NULL),
39+
volume = as.numeric(NaN),
40+
type = "Hpolytope"
41+
)
42+
)

R-proj/R/RcppExports.R

Lines changed: 71 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ copula <- function(r1, r2 = NULL, sigma = NULL, m = NULL, n = NULL, seed = NULL)
4646
#' \item{\code{type} }{ A string that declares the type of the body for the exact sampling: a) \code{'unit_simplex'} for the unit simplex, b) \code{'canonical_simplex'} for the canonical simplex, c) \code{'hypersphere'} for the boundary of a hypersphere centered at the origin, d) \code{'ball'} for the interior of a hypersphere centered at the origin.}
4747
#' \item{\code{dimension} }{ An integer that declares the dimension when exact sampling is enabled for a simplex or a hypersphere.}
4848
#' \item{\code{radius} }{ The radius of the \eqn{d}-dimensional hypersphere. The default value is \eqn{1}.}
49+
#' \item{\code{seed} }{ A fixed seed for the number generator.}
4950
#' }
5051
#' @param n The number of points that the function is going to sample.
51-
#' @param seed Optional. A fixed seed for the number generator.
5252
#'
5353
#' @references \cite{R.Y. Rubinstein and B. Melamed,
5454
#' \dQuote{Modern simulation and modeling} \emph{ Wiley Series in Probability and Statistics,} 1998.}
@@ -60,8 +60,8 @@ copula <- function(r1, r2 = NULL, sigma = NULL, m = NULL, n = NULL, seed = NULL)
6060
#' # 100 uniform points from the 2-d unit ball
6161
#' points = direct_sampling(n = 100, body = list("type" = "ball", "dimension" = 2))
6262
#' @export
63-
direct_sampling <- function(body, n, seed = NULL) {
64-
.Call(`_volesti_direct_sampling`, body, n, seed)
63+
direct_sampling <- function(body, n) {
64+
.Call(`_volesti_direct_sampling`, body, n)
6565
}
6666

6767
#' Compute the exact volume of (a) a zonotope (b) an arbitrary simplex in V-representation or (c) if the volume is known and declared by the input object.
@@ -83,7 +83,7 @@ direct_sampling <- function(body, n, seed = NULL) {
8383
#'
8484
#' \donttest{# compute the exact volume of a 2-d arbitrary simplex
8585
#' V = matrix(c(2,3,-1,7,0,0),ncol = 2, nrow = 3, byrow = TRUE)
86-
#' P = Vpolytope$new(V)
86+
#' P = Vpolytope(V = V)
8787
#' vol = exact_vol(P)
8888
#' }
8989
#'
@@ -142,6 +142,21 @@ inner_ball <- function(P) {
142142
.Call(`_volesti_inner_ball`, P)
143143
}
144144

145+
#' Read a SDPA format file
146+
#'
147+
#' @param input_file Name of the input file
148+
#'
149+
#' @return A list with two named items: an item "matrices" which is a list of the matrices and an vector "objFunction"
150+
#'
151+
#' @examples
152+
#' path = system.file('extdata', package = 'volesti')
153+
#' l = load_sdpa_format_file(paste0(path,'/sdpa_n2m3.txt'))
154+
#'
155+
#' @export
156+
load_sdpa_format_file <- function(input_file = NULL) {
157+
.Call(`_volesti_load_sdpa_format_file`, input_file)
158+
}
159+
145160
#' An internal Rccp function as a polytope generator
146161
#'
147162
#' @param kind_gen An integer to declare the type of the polytope.
@@ -174,13 +189,14 @@ rotating <- function(P, T = NULL, seed = NULL) {
174189
#' Internal rcpp function for the rounding of a convex polytope
175190
#'
176191
#' @param P A convex polytope (H- or V-representation or zonotope).
192+
#' @param settings A list to set the random walk and its walk length
177193
#' @param seed Optional. A fixed seed for the number generator.
178194
#'
179195
#' @keywords internal
180196
#'
181197
#' @return A numerical matrix that describes the rounded polytope, a numerical matrix of the inverse linear transofmation that is applied on the input polytope, the numerical vector the the input polytope is shifted and the determinant of the matrix of the linear transformation that is applied on the input polytope.
182-
rounding <- function(P, seed = NULL) {
183-
.Call(`_volesti_rounding`, P, seed)
198+
rounding <- function(P, settings = NULL, seed = NULL) {
199+
.Call(`_volesti_rounding`, P, settings, seed)
184200
}
185201

186202
#' Sample uniformly or normally distributed points from a convex Polytope (H-polytope, V-polytope, zonotope or intersection of two V-polytopes).
@@ -192,19 +208,19 @@ rounding <- function(P, seed = NULL) {
192208
#' @param random_walk Optional. A list that declares the random walk and some related parameters as follows:
193209
#' \itemize{
194210
#' \item{\code{walk} }{ A string to declare the random walk: i) \code{'CDHR'} for Coordinate Directions Hit-and-Run, ii) \code{'RDHR'} for Random Directions Hit-and-Run, iii) \code{'BaW'} for Ball Walk, iv) \code{'BiW'} for Billiard walk, v) \code{'BCDHR'} boundary sampling by keeping the extreme points of CDHR or vi) \code{'BRDHR'} boundary sampling by keeping the extreme points of RDHR. The default walk is \code{'BiW'} for the uniform distribution or \code{'CDHR'} for the Gaussian distribution.}
195-
#' \item{\code{walk_length} }{ The number of the steps per generated point for the random walk. The default value is \eqn{5} for \code{'BiW'} and \eqn{\lfloor 10 + d/10\rfloor} otherwise.}
211+
#' \item{\code{walk_length} }{ The number of the steps per generated point for the random walk. The default value is 1.}
196212
#' \item{\code{nburns} }{ The number of points to burn before start sampling.}
197213
#' \item{\code{starting_point} }{ A \eqn{d}-dimensional numerical vector that declares a starting point in the interior of the polytope for the random walk. The default choice is the center of the ball as that one computed by the function \code{inner_ball()}.}
198214
#' \item{\code{BaW_rad} }{ The radius for the ball walk.}
199215
#' \item{\code{L} }{ The maximum length of the billiard trajectory.}
216+
#' \item{\code{seed} }{ A fixed seed for the number generator.}
200217
#' }
201218
#' @param distribution Optional. A list that declares the target density and some related parameters as follows:
202219
#' \itemize{
203220
#' \item{\code{density} }{ A string: (a) \code{'uniform'} for the uniform distribution or b) \code{'gaussian'} for the multidimensional spherical distribution. The default target distribution is uniform.}
204221
#' \item{\code{variance} }{ The variance of the multidimensional spherical gaussian. The default value is 1.}
205222
#' \item{\code{mode} }{ A \eqn{d}-dimensional numerical vector that declares the mode of the Gaussian distribution. The default choice is the center of the as that one computed by the function \code{inner_ball()}.}
206223
#' }
207-
#' @param seed Optional. A fixed seed for the number generator.
208224
#'
209225
#' @return A \eqn{d\times n} matrix that contains, column-wise, the sampled points from the convex polytope P.
210226
#' @examples
@@ -215,52 +231,16 @@ rounding <- function(P, seed = NULL) {
215231
#' # gaussian distribution from the 2d unit simplex in H-representation with variance = 2
216232
#' A = matrix(c(-1,0,0,-1,1,1), ncol=2, nrow=3, byrow=TRUE)
217233
#' b = c(0,0,1)
218-
#' P = Hpolytope$new(A,b)
234+
#' P = Hpolytope(A = A, b = b)
219235
#' points = sample_points(P, n = 100, distribution = list("density" = "gaussian", "variance" = 2))
220236
#'
221237
#' # uniform points from the boundary of a 2-dimensional random H-polytope
222238
#' P = gen_rand_hpoly(2,20)
223239
#' points = sample_points(P, n = 100, random_walk = list("walk" = "BRDHR"))
224240
#'
225241
#' @export
226-
sample_points <- function(P, n, random_walk = NULL, distribution = NULL, seed = NULL) {
227-
.Call(`_volesti_sample_points`, P, n, random_walk, distribution, seed)
228-
}
229-
230-
#' Write a SDPA format file
231-
#'
232-
#' Outputs a spectrahedron (the matrices defining a linear matrix inequality) and a vector (the objective function)
233-
#' to a SDPA format file.
234-
#'
235-
#' @param spectrahedron A spectrahedron in n dimensions; must be an object of class Spectrahedron
236-
#' @param objectiveFunction A numerical vector of length n
237-
#' @param outputFile Name of the output file
238-
#'
239-
#' @examples
240-
#' A0 = matrix(c(-1,0,0,0,-2,1,0,1,-2), nrow=3, ncol=3, byrow = TRUE)
241-
#' A1 = matrix(c(-1,0,0,0,0,1,0,1,0), nrow=3, ncol=3, byrow = TRUE)
242-
#' A2 = matrix(c(0,0,-1,0,0,0,-1,0,0), nrow=3, ncol=3, byrow = TRUE)
243-
#' lmi = list(A0, A1, A2)
244-
#' S = Spectrahedron$new(lmi);
245-
#' objFunction = c(1,1)
246-
#' writeSdpaFormatFile(S, objFunction, "output.txt")
247-
#' @export
248-
writeSdpaFormatFile <- function(spectrahedron = NULL, objectiveFunction = NULL, outputFile = NULL) {
249-
invisible(.Call(`_volesti_writeSdpaFormatFile`, spectrahedron, objectiveFunction, outputFile))
250-
}
251-
252-
#' Read a SDPA format file
253-
#'
254-
#' @param inputFile Name of the input file
255-
#'
256-
#' @return A list with two named items: an item "matrices" which is a list of the matrices and an vector "objFunction"
257-
#'
258-
#' @examples
259-
#' path = system.file('extdata', package = 'volesti')
260-
#' l = loadSdpaFormatFile(paste0(path,'/sdpa_n2m3.txt'))
261-
#' @export
262-
loadSdpaFormatFile <- function(inputFile = NULL) {
263-
.Call(`_volesti_loadSdpaFormatFile`, inputFile)
242+
sample_points <- function(P, n, random_walk = NULL, distribution = NULL) {
243+
.Call(`_volesti_sample_points`, P, n, random_walk, distribution)
264244
}
265245

266246
#' The main function for volume approximation of a convex Polytope (H-polytope, V-polytope, zonotope or intersection of two V-polytopes)
@@ -270,15 +250,15 @@ loadSdpaFormatFile <- function(inputFile = NULL) {
270250
#' @param P A convex polytope. It is an object from class a) Hpolytope or b) Vpolytope or c) Zonotope or d) VpolytopeIntersection.
271251
#' @param settings Optional. A list that declares which algorithm, random walk and values of parameters to use, as follows:
272252
#' \itemize{
273-
#' \item{\code{algorithm} }{ A string to set the algorithm to use: a) \code{'CB'} for CB algorithm, b) \code{'SoB'} for SOB algorithm or b) \code{'CG'} for CG algorithm. The defalut algorithm for H-polytopes is \code{'CB'} when \eqn{d\leq 200} and \code{'CG'} when \eqn{d>200}. For the other representations the default algorithm is \code{'CB'}.}
253+
#' \item{\code{algorithm} }{ A string to set the algorithm to use: a) \code{'CB'} for CB algorithm, b) \code{'SoB'} for SOB algorithm or b) \code{'CG'} for CG algorithm. The defalut algorithm is \code{'CB'}.}
274254
#' \item{\code{error} }{ A numeric value to set the upper bound for the approximation error. The default value is \eqn{1} for SOB algorithm and \eqn{0.1} otherwise.}
275255
#' \item{\code{random_walk} }{ A string that declares the random walk method: a) \code{'CDHR'} for Coordinate Directions Hit-and-Run, b) \code{'RDHR'} for Random Directions Hit-and-Run, c) \code{'BaW'} for Ball Walk, or \code{'BiW'} for Billiard walk. For CB and SOB algorithms the default walk is \code{'CDHR'} for H-polytopes and \code{'BiW'} for the other representations. For CG algorithm the default walk is \code{'CDHR'} for H-polytopes and \code{'RDHR'} for the other representations.}
276256
#' \item{\code{walk_length} }{ An integer to set the number of the steps for the random walk. The default value is \eqn{\lfloor 10 + d/10\rfloor} for \code{'SOB'} and \eqn{1} otherwise.}
277257
#' \item{\code{win_len} }{ The length of the sliding window for CB or CG algorithm. The default value is \eqn{400+3d^2} for CB or \eqn{500+4d^2} for CG.}
278258
#' \item{\code{hpoly} }{ A boolean parameter to use H-polytopes in MMC of CB algorithm when the input polytope is a zonotope. The default value is \code{TRUE} when the order of the zonotope is \eqn{<5}, otherwise it is \code{FALSE}.}
259+
#' \item{\code{seed} }{ A fixed seed for the number generator.}
279260
#' }
280-
#' @param rounding Optional. A boolean parameter for rounding. The default value is \code{TRUE} for V-polytopes and \code{FALSE} otherwise.
281-
#' @param seed Optional. A fixed seed for the number generator.
261+
#' @param rounding A boolean parameter for rounding. The default value is \code{FALSE}.
282262
#'
283263
#' @references \cite{I.Z.Emiris and V. Fisikopoulos,
284264
#' \dQuote{Practical polytope volume approximation,} \emph{ACM Trans. Math. Soft.,} 2018.},
@@ -303,8 +283,47 @@ loadSdpaFormatFile <- function(inputFile = NULL) {
303283
#' vol = volume(Z, settings = list("random_walk" = "RDHR", "walk_length" = 2))
304284
#'
305285
#' @export
306-
volume <- function(P, settings = NULL, rounding = NULL, seed = NULL) {
307-
.Call(`_volesti_volume`, P, settings, rounding, seed)
286+
volume <- function(P, settings = NULL, rounding = FALSE) {
287+
.Call(`_volesti_volume`, P, settings, rounding)
288+
}
289+
290+
#' Write a SDPA format file
291+
#'
292+
#' Outputs a spectrahedron (the matrices defining a linear matrix inequality) and a vector (the objective function)
293+
#' to a SDPA format file.
294+
#'
295+
#' @param spectrahedron A spectrahedron in n dimensions; must be an object of class Spectrahedron
296+
#' @param objective_function A numerical vector of length n
297+
#' @param output_file Name of the output file
298+
#'
299+
#' @examples
300+
#' \dontrun{
301+
#' A0 = matrix(c(-1,0,0,0,-2,1,0,1,-2), nrow=3, ncol=3, byrow = TRUE)
302+
#' A1 = matrix(c(-1,0,0,0,0,1,0,1,0), nrow=3, ncol=3, byrow = TRUE)
303+
#' A2 = matrix(c(0,0,-1,0,0,0,-1,0,0), nrow=3, ncol=3, byrow = TRUE)
304+
#' lmi = list(A0, A1, A2)
305+
#' S = Spectrahedron(matrices = lmi);
306+
#' objFunction = c(1,1)
307+
#' write_sdpa_format_file(S, objFunction, "output.txt")
308+
#' }
309+
#' @export
310+
write_sdpa_format_file <- function(spectrahedron, objective_function, output_file) {
311+
invisible(.Call(`_volesti_write_sdpa_format_file`, spectrahedron, objective_function, output_file))
312+
}
313+
314+
#' Read a SDPA format file
315+
#'
316+
#' @param inputFile Name of the input file
317+
#'
318+
#' @return A list with two named items: an item "matrices" which is a list of the matrices and an vector "objFunction"
319+
#'
320+
#' @examples
321+
#' path = system.file('extdata', package = 'volesti')
322+
#' l = loadSdpaFormatFile(paste0(path,'/sdpa_n2m3.txt'))
323+
#'
324+
#' @export
325+
loadSdpaFormatFile <- function(inputFile = NULL) {
326+
.Call(`_volesti_loadSdpaFormatFile`, inputFile)
308327
}
309328

310329
#' An internal Rccp function for the over-approximation of a zonotope

R-proj/R/SpectrahedronClass.R

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#' An R class to represent a Spectrahedron
2+
#'
3+
#' A spectrahedron is a convex body defined by a linear matrix inequality of the form \eqn{A_0 + x_1 A_1 + ... + x_n A_n \preceq 0}.
4+
#' The matrices \eqn{A_i} are symmetric \eqn{m \times m} real matrices and \eqn{\preceq 0} denoted negative semidefiniteness.
5+
#'
6+
#' \describe{
7+
#' \item{matrices}{A List that contains the matrices \eqn{A_0, A_1, ..., A_n}.}
8+
#' }
9+
#'
10+
#' @examples
11+
#' A0 = matrix(c(-1,0,0,0,-2,1,0,1,-2), nrow=3, ncol=3, byrow = TRUE)
12+
#' A1 = matrix(c(-1,0,0,0,0,1,0,1,0), nrow=3, ncol=3, byrow = TRUE)
13+
#' A2 = matrix(c(0,0,-1,0,0,0,-1,0,0), nrow=3, ncol=3, byrow = TRUE)
14+
#' lmi = list(A0, A1, A2)
15+
#' S = Spectrahedron(matrices = lmi);
16+
#'
17+
#' @name Spectrahedron-class
18+
#' @rdname Spectrahedron-class
19+
#' @exportClass Spectrahedron
20+
Spectrahedron <- setClass (
21+
# Class name
22+
"Spectrahedron",
23+
24+
# Defining slot type
25+
representation (
26+
matrices = "list"
27+
),
28+
29+
# Initializing slots
30+
prototype = list(
31+
matrices = list()
32+
)
33+
)

R-proj/R/VpolytopeClass.R

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#' An R class to represent a V-polytope
2+
#'
3+
#' A V-polytope is a convex polytope defined by the set of its vertices.
4+
#'
5+
#' \describe{
6+
#' \item{V}{An \eqn{m\times d} numerical matrix that contains the vertices row-wise.}
7+
#'
8+
#' \item{volume}{The volume of the polytope if it is known, \eqn{NaN} otherwise by default.}
9+
#'
10+
#' \item{type}{A character with default value 'Vpolytope', to declare the representation of the polytope.}
11+
#' }
12+
#'
13+
#' @examples
14+
#' V = matrix(c(2,3,-1,7,0,0),ncol = 2, nrow = 3, byrow = TRUE)
15+
#' P = Vpolytope(V = V)
16+
#'
17+
#' @name Vpolytope-class
18+
#' @rdname Vpolytope-class
19+
#' @exportClass Vpolytope
20+
Vpolytope <- setClass (
21+
# Class name
22+
"Vpolytope",
23+
24+
# Defining slot type
25+
representation (
26+
V = "matrix",
27+
volume = "numeric",
28+
type = "character"
29+
),
30+
31+
# Initializing slots
32+
prototype = list(
33+
V = as.matrix(0),
34+
volume = as.numeric(NaN),
35+
type = "Vpolytope"
36+
)
37+
)

0 commit comments

Comments
 (0)