swap_pairs: Swap pairs in a matrix of integers

View source: R/utils_swap_functions.R

swap_pairsR Documentation

Swap pairs in a matrix of integers

Description

Modifies the input matrix X to ensure that the distance between any two occurrences of the same integer is at least a dist d, by swapping one of the occurrences with a random occurrence of a different integer that is at least d away. The function starts with starting_dist = 3 and increases it by 1 until the algorithm no longer converges or stop_iter iterations have been performed.

Usage

swap_pairs(X, starting_dist = 3, stop_iter = 50)

Arguments

X

A matrix of integers.

starting_dist

The minimum starting distance to enforce between pairs of occurrences of the same integer. Default is 3.

stop_iter

The maximum number of iterations to perform. Default is 100.

Value

A list containing the following elements:

optim_design

The modified matrix.

designs

A list of all intermediate designs, starting from the input matrix.

distances

A list of all pair distances for each intermediate design.

min_distance

An integer indicating the minimum distance between pairs of occurrences of the same integer.

pairwise_distance

A data frame with the pairwise distances for the final design.

Author(s)

Jean-Marc Montpetit [aut], Didier Murillo [aut]

Examples

# Create a matrix X with the numbers 1 to 10 are twice and 11 to 50 are once.
# The matrix has 6 rows and 10 columns
set.seed(123)
X <- matrix(sample(c(rep(1:10, 2), 11:50), replace = FALSE), ncol = 10)
X
# Swap pairs
B <- swap_pairs(X, starting_dist = 3)
B$optim_design
B$designs
B$distances



FielDHub documentation built on Oct. 20, 2023, 1:07 a.m.