Description Usage Arguments Details Note Author(s) See Also Examples

Given a partition, return the “next” one; or determine whether it is the last one.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | ```
nextpart(part, check=TRUE)
islastpart(part)
firstpart(n)
nextdiffpart(part, check=TRUE)
islastdiffpart(part)
firstdiffpart(n)
nextrestrictedpart(part, check=TRUE)
islastrestrictedpart(part)
firstrestrictedpart(n, m, include.zero=TRUE)
nextblockpart(part, f, n=sum(part), include.fewer=FALSE, check=TRUE)
islastblockpart(part, f, n=NULL , include.fewer=FALSE)
firstblockpart( f, n=NULL , include.fewer=FALSE)
nextcomposition(comp, restricted, include.zero=TRUE, check=TRUE)
islastcomposition(comp, restricted, include.zero=TRUE)
firstcomposition(n, m=NULL , include.zero=TRUE)
``` |

`part,comp` |
A partition or composition |

`check` |
Boolean, with default |

`f, n, include.fewer, m, include.zero` |
Other arguments as per the vectorized version |

`restricted` |
In function |

These functions are intended to enumerate partitions one at a time, eliminating the need to store a huge matrix. This is useful for optimization over large domains and makes it possible to investigate larger partitions than is possible with the vectorized codes.

The idea is to use a `first...()`

function to generate the first
partition, then iterate using a `next...()`

function, stopping when
the `islast...()`

function returns `TRUE`

.

An example is given below, in which the “scrabble” problem is
solved; note the small size of the sample space. More examples are
given in the `tests/aab.R`

file.

Functions `nextpart()`

and `nextdiffpart()`

require a vector
of the right length: they require and return a partition padded with
zeros. Functions `nextrestrictedpart()`

and
`nextblockpart()`

work with partitions of the specified length.
Function `nextcomposition()`

truncates any zeros at the end of
the composition. This behaviour is inherited from the `C`

code.

In functions `nextcomposition()`

and `firstcomposition()`

,
argument `include.zero`

is ignored if `restricted`

is
`FALSE`

.

I must say that the performance of these functions is terrible;
they are much much slower than their vectorized equivalents. The
magnitude of the difference is much larger than I expected. Heigh
ho. Frankly you would better off working directly in `C`

.

Robin K. S. Hankin

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | ```
# Do the optimization in scrabble vignette, one partition at a time:
# (but with a smaller letter bag)
scrabble <- c(a=9 , b=2 , c=2 , d=4 , e=12 , f=2 , g=3)
f <- function(a){prod(choose(scrabble,a))/choose(sum(scrabble),7)}
bestsofar <- 0
a <- firstblockpart(scrabble,7)
while(!islastpart(a)){
jj <- f(a)
if(jj>bestsofar){
bestsofar <- jj
bestpart <- a
}
a <- nextblockpart(a,scrabble)
}
``` |

Embedding an R snippet on your website

Add the following code to your website.

For more information on customizing the embed code, read Embedding Snippets.