Skip to contents

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

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
#>      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#> [1,]   21   40   17   25   26    3   11   31   36     6
#> [2,]    5   33    5    8   48   29   43   23    1    45
#> [3,]   41   27   38   39    7   28   14   22   24     4
#> [4,]    4   47   18    7    2   35    6   20   12    46
#> [5,]    3   15    9   34   49   50    2   10   42     8
#> [6,]   32   16   19    9   10   13   37    1   44    30
# Swap pairs
B <- swap_pairs(X, starting_dist = 3)
B$optim_design
#>      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#> [1,]    4   40   17    8   47    5   11   18   10     7
#> [2,]    7   30   22    3   48   29   34   44   25     1
#> [3,]   32   33    6   12   13   28   37    6   21     9
#> [4,]    9    1   20    2   19   35   43   38   15    16
#> [5,]   46    5   26   27   49   23   14   45   39    31
#> [6,]   36    8   42   41   50   10   24    3    4     2
B$designs
#> [[1]]
#>      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#> [1,]   21   40   17   25   26    3   11   31   36     6
#> [2,]    5   33    5    8   48   29   43   23    1    45
#> [3,]   41   27   38   39    7   28   14   22   24     4
#> [4,]    4   47   18    7    2   35    6   20   12    46
#> [5,]    3   15    9   34   49   50    2   10   42     8
#> [6,]   32   16   19    9   10   13   37    1   44    30
#> 
#> [[2]]
#>      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#> [1,]   21   40   17   25   26    3   11    9   23     6
#> [2,]    7   33   15   36   48   29   43    8    1    45
#> [3,]   41    8   38   39   13   28   14   22   24     2
#> [4,]    4   47   18    2    9   35    6   20   12    46
#> [5,]    3    5   31    4   49   50   34   10   42     7
#> [6,]   32   16   19   27   10    5   37    1   44    30
#> 
#> [[3]]
#>      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#> [1,]   21   40   17   25   26    3   11    9   23     6
#> [2,]    7   33   15   36   48   29   43    8    4    45
#> [3,]   41    8   38   39   13   28   14   22   24     2
#> [4,]   46   47   18    2    9   35    6   20   12    10
#> [5,]   10    5   31    1   49   50   34    3   42     7
#> [6,]   32   16   19   27    4    5   37    1   44    30
#> 
#> [[4]]
#>      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#> [1,]    4   40   17    8   47    5   11   18   10     7
#> [2,]    7   30   22    3   48   29   34   44   25     1
#> [3,]   32   33    6   12   13   28   37    6   21     9
#> [4,]    9    1   20    2   19   35   43   38   15    16
#> [5,]   46    5   26   27   49   23   14   45   39    31
#> [6,]   36    8   42   41   50   10   24    3    4     2
#> 
B$distances
#> [[1]]
#>    geno Pos1 Pos2     DIST rA cA rB cB
#> 7     7   22   27 1.414214  4  4  3  5
#> 9     9   17   24 1.414214  5  3  6  4
#> 5     5    2   14 2.000000  2  1  2  3
#> 2     2   28   41 2.236068  4  5  5  7
#> 10   10   30   47 3.162278  6  5  5  8
#> 1     1   48   50 4.123106  6  8  2  9
#> 6     6   40   55 4.242641  4  7  1 10
#> 3     3    5   31 6.403124  5  1  1  6
#> 8     8   20   59 6.708204  2  4  5 10
#> 4     4    4   57 9.055385  4  1  3 10
#> 
#> [[2]]
#>    geno Pos1 Pos2     DIST rA cA rB cB
#> 4     4    4   23 3.162278  4  1  5  4
#> 10   10   30   47 3.162278  6  5  5  8
#> 1     1   48   50 4.123106  6  8  2  9
#> 5     5   11   36 4.123106  5  2  6  6
#> 6     6   40   55 4.242641  4  7  1 10
#> 9     9   28   43 4.242641  4  5  1  8
#> 2     2   22   57 6.082763  4  4  3 10
#> 8     8    9   44 6.082763  3  2  2  8
#> 3     3    5   31 6.403124  5  1  1  6
#> 7     7    2   59 9.486833  2  1  5 10
#> 
#> [[3]]
#>    geno Pos1 Pos2     DIST rA cA rB cB
#> 1     1   23   48 4.123106  5  4  6  8
#> 5     5   11   36 4.123106  5  2  6  6
#> 6     6   40   55 4.242641  4  7  1 10
#> 9     9   28   43 4.242641  4  5  1  8
#> 3     3   31   47 4.472136  1  6  5  8
#> 4     4   30   50 5.656854  6  5  2  9
#> 2     2   22   57 6.082763  4  4  3 10
#> 8     8    9   44 6.082763  3  2  2  8
#> 10   10    5   58 9.055385  5  1  4 10
#> 7     7    2   59 9.486833  2  1  5 10
#> 
#> [[4]]
#>    geno Pos1 Pos2     DIST rA cA rB cB
#> 6     6   15   45 5.000000  3  3  3  8
#> 8     8   12   19 5.385165  6  2  1  4
#> 3     3   20   48 5.656854  2  4  6  8
#> 5     5   11   31 5.656854  5  2  1  6
#> 10   10   36   49 5.830952  6  6  1  9
#> 2     2   22   60 6.324555  4  4  6 10
#> 1     1   10   56 8.246211  4  2  2 10
#> 7     7    2   55 9.055385  2  1  1 10
#> 9     9    4   57 9.055385  4  1  3 10
#> 4     4    1   54 9.433981  1  1  6  9
#>