jwood000 / RcppAlgos

Tool for Solving Problems in Combinatorics and Computational Mathematics
GNU General Public License v2.0
45 stars 5 forks source link

Repeating values for comboGeneral #35

Closed KoderKow closed 2 years ago

KoderKow commented 2 years ago

Hello!

Thanks for writing a great package! When exploring combinations I have found some unexpected results. I have all of the scenarios below.

library(RcppAlgos)

v <- c(95, 99, 106, 109, 111, 116, 121, 132, 150, 162, 163, 193, 199, 
       215, 232, 304, 307, 313, 388, 1045)

sum(v)
#> [1] 4560

## I expect this to return no values, the sum of all 20 numbers is > 2000
comboGeneral(
  v = v,
  m = length(v),
  constraintFun = "sum",
  comparisonFun = c(">=", "<="),
  limitConstraints = c(1900, 2000),
)
#>      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
#> [1,]   95   95   95   95   95   95   95   95   95    95    95    95    95    95
#>      [,15] [,16] [,17] [,18] [,19] [,20]
#> [1,]    95    95    95    95    95    95

## If m is NULL, length(v) is used or sum(freq). This returns the expected no values
comboGeneral(
  v = v,
  m = NULL,
  constraintFun = "sum",
  comparisonFun = c(">=", "<="),
  limitConstraints = c(1900, 2000)
)
#>      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
#>      [,15] [,16] [,17] [,18] [,19] [,20]

## This ends up repeating the 95 18 times for all rows. I do not expect the 95 to repeat since repitition defaults to FALSE
comboGeneral(
  v = v,
  m = length(v) - 1,
  constraintFun = "sum",
  comparisonFun = c(">=", "<="),
  limitConstraints = c(1900, 2000)
)
#>       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
#>  [1,]   95   95   95   95   95   95   95   95   95    95    95    95    95
#>  [2,]   95   95   95   95   95   95   95   95   95    95    95    95    95
#>  [3,]   95   95   95   95   95   95   95   95   95    95    95    95    95
#>  [4,]   95   95   95   95   95   95   95   95   95    95    95    95    95
#>  [5,]   95   95   95   95   95   95   95   95   95    95    95    95    95
#>  [6,]   95   95   95   95   95   95   95   95   95    95    95    95    95
#>  [7,]   95   95   95   95   95   95   95   95   95    95    95    95    95
#>  [8,]   95   95   95   95   95   95   95   95   95    95    95    95    95
#>  [9,]   95   95   95   95   95   95   95   95   95    95    95    95    95
#> [10,]   95   95   95   95   95   95   95   95   95    95    95    95    95
#> [11,]   95   95   95   95   95   95   95   95   95    95    95    95    95
#> [12,]   95   95   95   95   95   95   95   95   95    95    95    95    95
#> [13,]   95   95   95   95   95   95   95   95   95    95    95    95    95
#> [14,]   95   95   95   95   95   95   95   95   95    95    95    95    95
#> [15,]   95   95   95   95   95   95   95   95   95    95    95    95    95
#> [16,]   95   95   95   95   95   95   95   95   95    95    95    95    95
#> [17,]   95   95   95   95   95   95   95   95   95    95    95    95    95
#> [18,]   95   95   95   95   95   95   95   95   95    95    95    95    95
#> [19,]   95   95   95   95   95   95   95   95   95    95    95    95    95
#> [20,]   95   95   95   95   95   95   95   95   95    95    95    95    95
#>       [,14] [,15] [,16] [,17] [,18] [,19]
#>  [1,]    95    95    95    95    95   193
#>  [2,]    95    95    95    95    95   199
#>  [3,]    95    95    95    95    95   215
#>  [4,]    95    95    95    95    95   232
#>  [5,]    95    95    95    95    99   193
#>  [6,]    95    95    95    95    99   199
#>  [7,]    95    95    95    95    99   215
#>  [8,]    95    95    95    95    99   232
#>  [9,]    95    95    95    95   106   193
#> [10,]    95    95    95    95   106   199
#> [11,]    95    95    95    95   106   215
#> [12,]    95    95    95    95   106   232
#> [13,]    95    95    95    95   109   193
#> [14,]    95    95    95    95   109   199
#> [15,]    95    95    95    95   109   215
#> [16,]    95    95    95    95   109   232
#> [17,]    95    95    95    95   111   193
#> [18,]    95    95    95    95   111   199
#> [19,]    95    95    95    95   111   215
#> [20,]    95    95    95    95   111   232

## The dupes dont happen at 14, this is the expected and wanted response
comboGeneral(
  v = v,
  m = 14,
  constraintFun = "sum",
  comparisonFun = c(">=", "<="),
  limitConstraints = c(1900, 2000)
)
#>      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
#> [1,]   95   99  106  109  111  116  121  132  150   162   163   193   199   215
#> [2,]   95   99  106  109  111  116  121  132  150   162   163   193   199   232

## Dupes begin at 15, and at this number theres 15k+ rows. Note: There should be no result here
## Storing this in a variable to reduce the long output
d <- comboGeneral(
  v = v,
  m = 15,
  constraintFun = "sum",
  comparisonFun = c(">=", "<="),
  limitConstraints = c(1900, 2000)
)

nrow(d)
#> [1] 15504

head(d)
#>      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
#> [1,]   95   95   95   95   95   95   95   95   95    95    95    95    95   304
#> [2,]   95   95   95   95   95   95   95   95   95    95    95    95    95   307
#> [3,]   95   95   95   95   95   95   95   95   95    95    95    95    95   313
#> [4,]   95   95   95   95   95   95   95   95   95    95    95    95    99   304
#> [5,]   95   95   95   95   95   95   95   95   95    95    95    95    99   307
#> [6,]   95   95   95   95   95   95   95   95   95    95    95    95    99   313
#>      [,15]
#> [1,]   388
#> [2,]   388
#> [3,]   388
#> [4,]   388
#> [5,]   388
#> [6,]   388

sessioninfo::session_info()
#> - Session info ---------------------------------------------------------------
#>  setting  value                       
#>  version  R version 4.1.3 (2022-03-10)
#>  os       Windows 10 x64              
#>  system   x86_64, mingw32             
#>  ui       RTerm                       
#>  language (EN)                        
#>  collate  English_United States.1252  
#>  ctype    English_United States.1252  
#>  tz       UTC                         
#>  date     2022-07-19                  
#> 
#> - Packages -------------------------------------------------------------------
#>  package     * version date       lib source        
#>  backports     1.2.1   2020-12-09 [1] RSPM (R 4.1.0)
#>  cli           3.2.0   2022-02-14 [1] RSPM (R 4.1.0)
#>  crayon        1.4.1   2021-02-08 [1] RSPM (R 4.1.0)
#>  digest        0.6.28  2021-09-23 [1] RSPM (R 4.1.0)
#>  ellipsis      0.3.2   2021-04-29 [1] RSPM (R 4.1.0)
#>  evaluate      0.15    2022-02-18 [1] RSPM (R 4.1.0)
#>  fansi         0.5.0   2021-05-25 [1] RSPM (R 4.1.0)
#>  fastmap       1.1.0   2021-01-25 [1] RSPM (R 4.1.0)
#>  fs            1.5.2   2021-12-08 [1] RSPM (R 4.1.0)
#>  glue          1.6.1   2022-01-22 [1] RSPM (R 4.1.0)
#>  gmp           0.6-4   2022-02-24 [1] RSPM (R 4.1.0)
#>  highr         0.9     2021-04-16 [1] RSPM (R 4.1.0)
#>  htmltools     0.5.2   2021-08-25 [1] RSPM (R 4.1.0)
#>  knitr         1.39    2022-04-26 [1] RSPM (R 4.1.0)
#>  lifecycle     1.0.1   2021-09-24 [1] RSPM (R 4.1.0)
#>  magrittr      2.0.3   2022-03-30 [1] RSPM (R 4.1.0)
#>  pillar        1.7.0   2022-02-01 [1] RSPM (R 4.1.0)
#>  pkgconfig     2.0.3   2019-09-22 [1] RSPM (R 4.1.0)
#>  purrr         0.3.4   2020-04-17 [1] RSPM (R 4.1.0)
#>  R.cache       0.15.0  2021-04-30 [1] RSPM (R 4.1.0)
#>  R.methodsS3   1.8.1   2020-08-26 [1] RSPM (R 4.1.0)
#>  R.oo          1.24.0  2020-08-26 [1] RSPM (R 4.1.0)
#>  R.utils       2.11.0  2021-09-26 [1] RSPM (R 4.1.0)
#>  RcppAlgos   * 2.5.3   2022-03-31 [1] RSPM (R 4.1.0)
#>  reprex        2.0.1   2021-08-05 [1] RSPM (R 4.1.0)
#>  rlang         1.0.1   2022-02-03 [1] RSPM (R 4.1.0)
#>  rmarkdown     2.11    2021-09-14 [1] RSPM (R 4.1.0)
#>  rstudioapi    0.13    2020-11-12 [1] RSPM (R 4.1.0)
#>  sessioninfo   1.1.1   2018-11-05 [1] RSPM (R 4.1.0)
#>  stringi       1.7.5   2021-10-04 [1] RSPM (R 4.1.0)
#>  stringr       1.4.0   2019-02-10 [1] RSPM (R 4.1.0)
#>  styler        1.6.2   2021-09-23 [1] RSPM (R 4.1.0)
#>  tibble        3.1.6   2021-11-07 [1] RSPM (R 4.1.0)
#>  utf8          1.2.2   2021-07-24 [1] RSPM (R 4.1.0)
#>  vctrs         0.3.8   2021-04-29 [1] RSPM (R 4.1.0)
#>  withr         2.5.0   2022-03-03 [1] RSPM (R 4.1.0)
#>  xfun          0.30    2022-03-02 [1] RSPM (R 4.1.0)
#>  yaml          2.2.1   2020-02-01 [1] RSPM (R 4.1.0)
#> 
#> [1] C:/Users/kow/Documents/R/win-library/4.1
#> [2] C:/Program Files/R/R-4.1.3/library

Created on 2022-07-19 by the reprex package (v2.0.1)

I am not sure if this is a bug or if I am misusing the function. Appreciate your time!

jwood000 commented 2 years ago

Hello @KoderKow,

Yes, this is a bug. It is fixed by 7a4ed5b16ca845585dcbfdf1495a6e23fa7a551b in the features/compositions branch.

I'm almost done with all of the updates in that branch and plan on merging by the end of the month.

Thanks for reporting. I will close once the branch is merged into main.

Joseph

jwood000 commented 2 years ago

This is now fixed by #36. To get the changes simply get the development version via:

devtools::install_github("jwood000/RcppAlgos")

I will push this to CRAN once their summer break is over (Aug. 5th).

Thanks again @KoderKow for posting this.

Joseph