~enricoschumann/neighbours

35df00a844e5eadad9edb9a8bd813cc4dd79ea0c — Enrico Schumann 1 year, 23 days ago 73650ce
Add function 'next_subset'
7 files changed, 105 insertions(+), 5 deletions(-)

M ChangeLog
M DESCRIPTION
M NAMESPACE
M NEWS
A R/next_subset.R
M man/neighbourfun.Rd
A man/next_subset.Rd
M ChangeLog => ChangeLog +6 -2
@@ 1,7 1,11 @@
2023-06-29  Enrico Schumann  <es@enricoschumann.net>

        * R/next_subset.R (next_subset): add function

2022-08-22  Enrico Schumann  <es@enricoschumann.net>

	* DESCRIPTION (Version): 0.1-2
	(Suggests): add 'quadprog', used in vignette
        * DESCRIPTION (Version): 0.1-2
        (Suggests): add 'quadprog', used in vignette

2022-08-19  Enrico Schumann  <es@enricoschumann.net>


M DESCRIPTION => DESCRIPTION +2 -2
@@ 1,8 1,8 @@
Package: neighbours
Type: Package
Title: Neighbourhood Functions for Local-Search Algorithms
Version: 0.1-2
Date: 2022-08-22
Version: 0.1-3
Date: 2023-06-29
Maintainer: Enrico Schumann <es@enricoschumann.net>
Authors@R: person(given = "Enrico", family = "Schumann",
                  role  = c("aut", "cre"),

M NAMESPACE => NAMESPACE +1 -0
@@ 2,4 2,5 @@ importFrom("stats", "runif")

export("neighbourfun",
       "neighborfun",
       "next_subset",
       "compare_vectors")

M NEWS => NEWS +13 -0
@@ 1,3 1,16 @@
v0.1-3  (2023-07-??; not yet released)

  o New function "next_subset" for computing the next
    k-of-n subset.

v0.1-2  (2022-08-22)

  o No user visible changes.

v0.1-1  (2022-08-17)

  o No user visible changes.

v0.1-0  (2022-08-16)

  o Initial release.  The package provides a function

A R/next_subset.R => R/next_subset.R +10 -0
@@ 0,0 1,10 @@
next_subset <- function(a, n, k) {
    if (a[1L] == n - k +1)
        return(NULL)
    k1 <- 1:k
    h <- min(which(a[k + 1 - k1] != n + 1 - k1))
    m1 <- a[k + 1 - h]
    h1 <- 1:h
    a[k + h1 - h] <- m1 + h1
    a
}

M man/neighbourfun.Rd => man/neighbourfun.Rd +2 -1
@@ 113,7 113,8 @@ neighborfun (min = 0, max = 1, kmin = NULL, kmax = NULL,
  Methods and Optimization in Finance}. 2nd edition. Elsevier.\cr
  \url{https://www.elsevier.com/books/numerical-methods-and-optimization-in-finance/gilli/978-0-12-815065-8}

  Schumann, E. (2019) Financial Optimisation with R (\pkg{NMOF} Manual).\cr
  Schumann, E. (2023) \emph{Financial Optimisation with R
    (\pkg{NMOF} Manual)}.\cr
  \url{http://enricoschumann.net/NMOF.htm#NMOFmanual}

}

A man/next_subset.Rd => man/next_subset.Rd +71 -0
@@ 0,0 1,71 @@
\name{next_subset}
\alias{next_subset}
\title{
  Select Next Subset
}
\description{
Select next subset of size k from a set of size n.
}
\usage{
next_subset(a, n, k)
}
\arguments{
  \item{a}{
    a numeric vector (integers)
}
  \item{n}{
    an integer: the size of the set to choose from
}
  \item{k}{
    an integer: the subset size
}
}
\details{

  Given a subset \emph{a} of size \emph{k} taken from a set
  of size \emph{n}, compute the next subset by alphabetical
  order.

  Uses algorithm NEXKSB of Nijenhuis and Wilf (1975).
}
\value{
   a numeric vector (the next subset) or \code{\link{NULL}}
   (when there is no next subset)
}
\references{
  Nijenhuis, A. and Wilf, H. S. (1975)
  \emph{Combinatorial Algorithms for Computers and Calculators}.
  Academic Press.
}
\author{
  Enrico Schumann
}
\seealso{

  \code{\link{choose}} computes the number of combinations\cr
  \code{\link{combn}} creates all combinations\cr
  \code{\link{expand.grid}}
}
\examples{
n <- 4
k <- 2
t(combn(n, k))
##      [,1] [,2]
## [1,]    1    2
## [2,]    1    3
## [3,]    1    4
## [4,]    2    3
## [5,]    2    4
## [6,]    3    4

a <- 1:k
print(a)
while (!is.null(a))
    print(a <- next_subset(a, n = n, k = k))
## [1] 1 2
## [1] 1 3
## [1] 1 4
## [1] 2 3
## [1] 2 4
## [1] 3 4
}