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 distance d, by swapping one of the occurrences with a candidate cell of a different integer. 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. This version evaluates candidate swaps using both the mean pairwise distance and a centrality penalty, and it uses candidate sampling to reduce computation.

Usage

swap_pairs(
  X,
  starting_dist = 3,
  stop_iter = 10,
  lambda = 0.5,
  dist_method = "euclidean",
  candidate_sample_size = 4
)

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 50.

lambda

A tuning parameter for the centrality penalty. Default is 0.1.

dist_method

The method used for distance calculation. Options are "euclidean" (default) and "manhattan".

candidate_sample_size

Maximum number of candidate cells to evaluate per swap. Default is 5.

Value

A list containing:

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

The minimum distance between pairs of occurrences of the same integer in the final design.

pairwise_distance

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

rows_incidence

A vector recording the number of rows with repeated integers for each iteration.

Examples

set.seed(123)
X <- matrix(sample(c(rep(1:10, 2), 11:50), replace = FALSE), ncol = 10)
B <- swap_pairs(
 X, 
 starting_dist = 3, 
 stop_iter = 50, 
 lambda = 0.5, 
 dist_method = "euclidean", 
 candidate_sample_size = 3
)
B$optim_design


DidierMurilloF/FielDHub documentation built on June 9, 2025, 10:58 p.m.