% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/select.diversity.R
\name{select.diversity}
\alias{select.diversity}
\title{Selection of Entries from Clusters/Groups on the basis of Optimized Diversity}
\usage{
select.diversity(
  data,
  names,
  group,
  alloc,
  qualitative,
  always.selected = NULL,
  div.index = c("richness", "shannon", "simpson", "mcintosh"),
  shannon.base = exp(1),
  div.fun = NULL,
  metric = c("mean", "pooled"),
  search = c("random", "greedy"),
  local.search = c("best.improvement", "first.improvement"),
  n.iter = 1000,
  max.iter = 30
)
}
\arguments{
\item{data}{The data as a data frame object. The data frame should possess
one row per individual and columns with the individual names and multiple
trait/character data.}

\item{names}{Name of column with the accession names as a character string.}

\item{group}{Name of column with the accession group/cluster names as a
character string.}

\item{alloc}{A named numeric vector specifying the number of entries to be
selected. Names should correspond to the levels of the "\code{"group"}
column, and values indicate the number of elements to be selected from each
level.}

\item{qualitative}{Name of columns with the qualitative traits as a character
vector.}

\item{always.selected}{Names of accessions to be always included in the core
set as a character vector.}

\item{div.index}{The diversity index to be used to estimate within
cluster/group diversity.}

\item{shannon.base}{The logarithm base to be used for estimation of Shannon
diversity index. Default is \code{exp(1)}.}

\item{div.fun}{A function to estimate diversity index from a factor vector of
qualitative trait data.}

\item{metric}{The metric to be computed from the diversity index. Either
\code{"pooled"} or \code{"mean"}.}

\item{search}{Character string specifying the search strategy used to find
the subset with the highest diversity score. Either \code{"random"}
(default) or \code{"greedy"} (See \strong{Details}).}

\item{local.search}{Character string specifying the local search strategy
used in the 1-opt improvement phase of the greedy search (\code{search =
"greedy"}). Either \code{"best.improvement"} (default) or
\code{"first.improvement"}. Ignored when \code{search = "random"}.}

\item{n.iter}{Integer specifying the number of random candidate subsets
generated per group to optimze the diversity for random search
(\code{search = "random"}).}

\item{max.iter}{The maximum number of 1-opt passes for greedy search
(\code{search = "greedy"}).}
}
\value{
A named list where each element contains the selected entry
  identifiers for a cluster/group.
}
\description{
Select entries from cluster/groups in the entire collection which form a
subset with the highest trait diversity according to a either pooled or mean
diversity index estimate. \loadmathjax
}
\details{
To identify subsets with highest diversity estimates, the following
strategies are available. These strategies are similar to the "Maximization"
or M strategy of \insertCite{schoen_conservation_1993;textual}{SampleCore}.

 \subsection{Random search / Monte Carlo Method}{For each cluster/group,
 multiple candidate subsets are sampled randomly and the subset with the
 highest trait diversity according to either pooled or mean diversity index
 estimate is retained. The quality of the solution improves with increasing
 \code{n.iter} but is not guaranteed to find the global optimum
 \insertCite{anatoly_zhigljavsky_stochastic_2008}{SampleCore}.
 }

 \subsection{Greedy search with 1-opt}{This method builds a solution
 incrementally by adding the accession that maximises the diversity score at
 each step, starting from the \code{always.selected} accessions (or a single
 randomly drawn accession when there are no accessions specified in
 \code{always.selected}) present in the particular cluster/group
 \insertCite{nemhauser_analysis_1978,fisher_analysis_1978,cormen_introduction_2022}{SampleCore}.
 The 'greedy' solution is then refined by a 1-opt local search controlled by
 \code{local.search} and \code{max.iter}
 \insertCite{lin_computer_1965}{SampleCore}. Greedy search is deterministic
 given a fixed \code{always.selected} set; when there are no accessions
 specified in \code{always.selected} present in the particular cluster/group
 results may vary across runs due to the random initialisation.

 \code{local.search = "best.improvement"} scans all possible single swaps
 in each pass and applies the one yielding the greatest improvement before
 restarting. his guarantees the steepest ascent at each pass but requires
 evaluating all \mjseqn{k \times (n - k)} swap pairs per pass, where
 \mjseqn{k} is the number of swappable accessions and \mjseqn{n - k} is the
 size of the candidate pool
 \insertCite{papadimitriou_combinatorial_1998}{SampleCore}.

 \code{local.search = "first.improvement"} applies the first swap that
 improves the score and immediately restarts the search. This typically
 requires fewer score evaluations per pass and converges faster, but may
 find a different local optimum than \code{"best.improvement"}
 \insertCite{papadimitriou_combinatorial_1998}{SampleCore}.

 Both strategies terminate when no improving swap exists (local optimum)
 or when \code{max.iter} passes have been completed.
 }

Entries listed as \code{always.selected} are mandatorily included in the
selection. Warnings are issued if requested allocation is smaller than the
number of always-selected entries in a cluster/group and/or when the
cluster/group does not contain enough remaining entries to fulfill the
allocation.
}
\examples{

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Prepare example data
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

library(cluster)
library(ggplot2)

data(cassava_EC_gp)

set.seed(123)
cassava_EC_gp <- cassava_EC_gp[sample(1:nrow(cassava_EC_gp), 500), ]

data <- cbind(genotypes = rownames(cassava_EC_gp), cassava_EC_gp)

quant <- c("NMSR", "TTRN", "TFWSR", "TTRW", "TFWSS", "TTSW", "TTPW", "AVPW",
           "ARSR", "SRDM")
qual <- c("CUAL", "LNGS", "PTLC", "DSTA", "LFRT", "LBTEF", "CBTR", "NMLB",
          "ANGB", "CUAL9M", "LVC9M", "TNPR9M", "PL9M", "STRP", "STRC",
          "PSTR")

# Convert qualitative data columns to factor
data[, qual] <- lapply(data[, qual], as.factor)

# Convert quantitative data columns to qualitative scores
quant_to_score5 <- function(x) {

  brks <- unique( quantile(x,
                           probs = seq(0, 1, 0.2),
                           na.rm = TRUE))
  cut(x, breaks = brks,
      include.lowest = TRUE,
      labels = seq_len(length(brks) - 1))
}

data[, quant] <- lapply(data[, quant], quant_to_score5)

traits <- c(quant, qual)

# Prepare inputs
counts <- c(I = 31, II = 31, III = 18, IV = 35, V = 40, VI = 17)

mand_accns <-
  c("TMe-2018", "TMe-801", "TMe-3191", "TMe-1830", "TMe-1790")

# Get distance matrix - Only for visualization

# Convert qualitative data columns to factor
cassava_EC_gp[, qual] <- lapply(cassava_EC_gp[, qual], as.factor)

# Standardise quantitative data column
cassava_EC_gp[, quant] <- lapply(cassava_EC_gp[, quant], function(x) {
  scale(x)[, 1]
})

gp_vec <- setNames(as.character(data[, "Cluster"]), data[, "genotypes"])

# Get the Gower's distance matrix
dist_matrix <- daisy(x = cassava_EC_gp[, c(qual, quant)],
                     metric = "gower")

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Custom Diversity functions
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

div_fun_brillouin <- function(x) {
  n <- tabulate(x)
  n <- n[n > 0]
  N <- sum(n)
  if (N <= 1) {
    return(0)
  }
  (lgamma(N + 1) - sum(lgamma(n + 1)))/N
}

div_fun_margalef <- function(x) {
  tab <- tabulate(x)
  tab <- tab[tab > 0]
  S <- length(tab)
  N <- length(x)
  if (N <= 1) {
    return(0)
  }
  (S - 1)/log(N)
}

\donttest{
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  # Random search
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  # Mean richness
  randomsel_mean_richness <-
    select.diversity(data = data, names = "genotypes", group = "Cluster",
                     alloc = counts, qualitative = traits,
                     always.selected = mand_accns, div.index = "richness",
                     metric = "mean", search = "random", local.search = NULL,
                     n.iter = 50)
  randomsel_mean_richness

  plot_dist(d = dist_matrix, method = "isomds",
            gp = gp_vec,
            highlight =  unlist(randomsel_mean_richness,
                                use.names = FALSE)) +
    labs(title = "Random search", subtitle = "Mean richness")

  # Pooled richness
  randomsel_sum_richness <-
    select.diversity(data = data, names = "genotypes", group = "Cluster",
                     alloc = counts, qualitative = traits,
                     always.selected = mand_accns, div.index = "richness",
                     metric = "pooled", search = "random", local.search = NULL,
                     n.iter = 50)
  randomsel_sum_richness

  plot_dist(d = dist_matrix, method = "isomds",
            gp = gp_vec,
            highlight =  unlist(randomsel_sum_richness,
                                use.names = FALSE)) +
    labs(title = "Random search", subtitle = "Pooled richness")

  # Mean Shannon-Weaver diversity index
  randomsel_mean_shannon <-
    select.diversity(data = data, names = "genotypes", group = "Cluster",
                     alloc = counts, qualitative = traits,
                     always.selected = mand_accns, div.index = "shannon",
                     metric = "mean", search = "random", local.search = NULL,
                     n.iter = 50)
  randomsel_mean_shannon

  plot_dist(d = dist_matrix, method = "isomds",
            gp = gp_vec,
            highlight =  unlist(randomsel_mean_shannon,
                                use.names = FALSE)) +
    labs(title = "Random search",
         subtitle = "Mean Shannon-Weaver diversity index")

  # Pooled Shannon-Weaver diversity index
  randomsel_sum_shannon <-
    select.diversity(data = data, names = "genotypes", group = "Cluster",
                     alloc = counts, qualitative = traits,
                     always.selected = mand_accns, div.index = "shannon",
                     metric = "pooled", search = "random", local.search = NULL,
                     n.iter = 50)
  randomsel_sum_shannon

  plot_dist(d = dist_matrix, method = "isomds",
            gp = gp_vec,
            highlight =  unlist(randomsel_sum_shannon,
                                use.names = FALSE)) +
    labs(title = "Random search",
         subtitle = "Pooled Shannon-Weaver diversity index")

  # Mean Gini-Simpson diversity index
  randomsel_mean_simpson <-
    select.diversity(data = data, names = "genotypes", group = "Cluster",
                     alloc = counts, qualitative = traits,
                     always.selected = mand_accns, div.index = "simpson",
                     metric = "mean", search = "random", local.search = NULL,
                     n.iter = 50)
  randomsel_mean_simpson

  plot_dist(d = dist_matrix, method = "isomds",
            gp = gp_vec,
            highlight =  unlist(randomsel_mean_simpson,
                                use.names = FALSE)) +
    labs(title = "Random search",
         subtitle = "Mean Gini-Simpson diversity index")

  # Pooled Gini-Simpson diversity index
  randomsel_sum_simpson <-
    select.diversity(data = data, names = "genotypes", group = "Cluster",
                     alloc = counts, qualitative = traits,
                     always.selected = mand_accns, div.index = "simpson",
                     metric = "pooled", search = "random", local.search = NULL,
                     n.iter = 50)
  randomsel_sum_simpson

  plot_dist(d = dist_matrix, method = "isomds",
            gp = gp_vec,
            highlight =  unlist(randomsel_sum_simpson,
                                use.names = FALSE)) +
    labs(title = "Random search",
         subtitle = "Pooled Gini-Simpson diversity index")

  # Mean McIntosh diversity index
  randomsel_mean_mcintosh <-
    select.diversity(data = data, names = "genotypes", group = "Cluster",
                     alloc = counts, qualitative = traits,
                     always.selected = mand_accns, div.index = "mcintosh",
                     metric = "pooled", search = "random", local.search = NULL,
                     n.iter = 50)
  randomsel_mean_mcintosh

  plot_dist(d = dist_matrix, method = "isomds",
            gp = gp_vec,
            highlight =  unlist(randomsel_mean_mcintosh,
                                use.names = FALSE)) +
    labs(title = "Random search",
         subtitle = "Mean McIntosh diversity index")

  # Pooled McIntosh diversity index
  randomsel_sum_mcintosh <-
    select.diversity(data = data, names = "genotypes", group = "Cluster",
                     alloc = counts, qualitative = traits,
                     always.selected = mand_accns, div.index = "mcintosh",
                     metric = "pooled", search = "random", local.search = NULL,
                     n.iter = 50)
  randomsel_sum_mcintosh

  plot_dist(d = dist_matrix, method = "isomds",
            gp = gp_vec,
            highlight =  unlist(randomsel_sum_mcintosh,
                                use.names = FALSE)) +
    labs(title = "Random search",
         subtitle = "Pooled McIntosh diversity index")

  # Mean Brillouin diversity index
  randomsel_mean_brillouin <-
    select.diversity(data = data, names = "genotypes", group = "Cluster",
                     alloc = counts, qualitative = traits,
                     always.selected = mand_accns, div.fun = div_fun_brillouin,
                     metric = "mean", search = "random", local.search = NULL,
                     n.iter = 50)
  randomsel_mean_brillouin

  plot_dist(d = dist_matrix, method = "isomds",
            gp = gp_vec,
            highlight =  unlist(randomsel_mean_brillouin,
                                use.names = FALSE)) +
    labs(title = "Random search",
         subtitle = "Mean Brillouin diversity index")

  # Pooled Brillouin diversity index
  randomsel_sum_brillouin <-
    select.diversity(data = data, names = "genotypes", group = "Cluster",
                     alloc = counts, qualitative = traits,
                     always.selected = mand_accns, div.fun = div_fun_brillouin,
                     metric = "pooled", search = "random", local.search = NULL,
                     n.iter = 50)
  randomsel_sum_brillouin

  plot_dist(d = dist_matrix, method = "isomds",
            gp = gp_vec,
            highlight =  unlist(randomsel_sum_brillouin,
                                use.names = FALSE)) +
    labs(title = "Random search",
         subtitle = "Pooled Brillouin diversity index")


  # Mean Margalef's richness index
  randomsel_mean_margalef <-
    select.diversity(data = data, names = "genotypes", group = "Cluster",
                     alloc = counts, qualitative = traits,
                     always.selected = mand_accns, div.fun = div_fun_margalef,
                     metric = "mean", search = "random", local.search = NULL,
                     n.iter = 50)
  randomsel_mean_margalef

  plot_dist(d = dist_matrix, method = "isomds",
            gp = gp_vec,
            highlight =  unlist(randomsel_mean_margalef,
                                use.names = FALSE)) +
    labs(title = "Random search",
         subtitle = "Mean Margalef's diversity index")

  # Pooled Margalef's richness index
  randomsel_sum_margalef <-
    select.diversity(data = data, names = "genotypes", group = "Cluster",
                     alloc = counts, qualitative = traits,
                     always.selected = mand_accns, div.fun = div_fun_margalef,
                     metric = "pooled", search = "random", local.search = NULL,
                     n.iter = 50)
  randomsel_sum_margalef

  plot_dist(d = dist_matrix, method = "isomds",
            gp = gp_vec,
            highlight =  unlist(randomsel_sum_margalef,
                                use.names = FALSE)) +
    labs(title = "Random search",
         subtitle = "Pooled Margalef's diversity index")

  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  # Greedy search with 1-opt best improvement
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  # Mean richness
  greedysel_best_mean_richness <-
    select.diversity(data = data, names = "genotypes", group = "Cluster",
                     alloc = counts, qualitative = traits,
                     always.selected = mand_accns, div.index = "richness",
                     metric = "mean", search = "greedy",
                     local.search = "best.improvement",max.iter = 3)
  greedysel_best_mean_richness

  plot_dist(d = dist_matrix, method = "isomds",
            gp = gp_vec,
            highlight =  unlist(greedysel_best_mean_richness,
                                use.names = FALSE)) +
    labs(title = "Greed search | 1-opt best improvement",
         subtitle = "Mean richness")

  # Pooled richness
  greedysel_best_sum_richness <-
    select.diversity(data = data, names = "genotypes", group = "Cluster",
                     alloc = counts, qualitative = traits,
                     always.selected = mand_accns, div.index = "richness",
                     metric = "pooled", search = "greedy",
                     local.search = "best.improvement",max.iter = 3)
  greedysel_best_sum_richness

  plot_dist(d = dist_matrix, method = "isomds",
            gp = gp_vec,
            highlight =  unlist(greedysel_best_sum_richness,
                                use.names = FALSE)) +
    labs(title = "Greed search | 1-opt best improvement",
         subtitle = "Pooled richness")

  # Mean Shannon-Weaver diversity index
  greedysel_best_mean_shannon <-
    select.diversity(data = data, names = "genotypes", group = "Cluster",
                     alloc = counts, qualitative = traits,
                     always.selected = mand_accns, div.index = "shannon",
                     metric = "mean", search = "greedy",
                     local.search = "best.improvement",max.iter = 3)
  greedysel_best_mean_shannon

  plot_dist(d = dist_matrix, method = "isomds",
            gp = gp_vec,
            highlight =  unlist(greedysel_best_mean_shannon,
                                use.names = FALSE)) +
    labs(title = "Greed search | 1-opt best improvement",
         subtitle = "Mean Shannon-Weaver diversity index")

  # Pooled Shannon-Weaver diversity index
  greedysel_best_sum_shannon <-
    select.diversity(data = data, names = "genotypes", group = "Cluster",
                     alloc = counts, qualitative = traits,
                     always.selected = mand_accns, div.index = "shannon",
                     metric = "pooled", search = "greedy",
                     local.search = "best.improvement",max.iter = 3)
  greedysel_best_sum_shannon

  plot_dist(d = dist_matrix, method = "isomds",
            gp = gp_vec,
            highlight =  unlist(greedysel_best_sum_shannon,
                                use.names = FALSE)) +
    labs(title = "Greed search | 1-opt best improvement",
         subtitle = "Pooled Shannon-Weaver diversity index")

  # Mean Gini-Simpson diversity index
  greedysel_best_mean_simpson <-
    select.diversity(data = data, names = "genotypes", group = "Cluster",
                     alloc = counts, qualitative = traits,
                     always.selected = mand_accns, div.index = "simpson",
                     metric = "mean", search = "greedy",
                     local.search = "best.improvement",max.iter = 3)
  greedysel_best_mean_simpson

  plot_dist(d = dist_matrix, method = "isomds",
            gp = gp_vec,
            highlight =  unlist(greedysel_best_mean_simpson,
                                use.names = FALSE)) +
    labs(title = "Greed search | 1-opt best improvement",
         subtitle = "Mean Gini-Simpson diversity index")

  # Pooled Gini-Simpson diversity index
  greedysel_best_sum_simpson <-
    select.diversity(data = data, names = "genotypes", group = "Cluster",
                     alloc = counts, qualitative = traits,
                     always.selected = mand_accns, div.index = "simpson",
                     metric = "pooled", search = "greedy",
                     local.search = "best.improvement",max.iter = 3)
  greedysel_best_sum_simpson

  plot_dist(d = dist_matrix, method = "isomds",
            gp = gp_vec,
            highlight =  unlist(greedysel_best_sum_simpson,
                                use.names = FALSE)) +
    labs(title = "Greed search | 1-opt best improvement",
         subtitle = "Pooled Gini-Simpson diversity index")

  # Mean McIntosh diversity index
  greedysel_best_mean_mcintosh <-
    select.diversity(data = data, names = "genotypes", group = "Cluster",
                     alloc = counts, qualitative = traits,
                     always.selected = mand_accns, div.index = "mcintosh",
                     metric = "pooled", search = "greedy",
                     local.search = "best.improvement",max.iter = 3)
  greedysel_best_mean_mcintosh

  plot_dist(d = dist_matrix, method = "isomds",
            gp = gp_vec,
            highlight =  unlist(greedysel_best_mean_mcintosh,
                                use.names = FALSE)) +
    labs(title = "Greed search | 1-opt best improvement",
         subtitle = "Mean McIntosh diversity index")

  # Pooled McIntosh diversity index
  greedysel_best_sum_mcintosh <-
    select.diversity(data = data, names = "genotypes", group = "Cluster",
                     alloc = counts, qualitative = traits,
                     always.selected = mand_accns, div.index = "mcintosh",
                     metric = "pooled", search = "greedy",
                     local.search = "best.improvement",max.iter = 3)
  greedysel_best_sum_mcintosh

  plot_dist(d = dist_matrix, method = "isomds",
            gp = gp_vec,
            highlight =  unlist(greedysel_best_sum_mcintosh,
                                use.names = FALSE)) +
    labs(title = "Greed search | 1-opt best improvement",
         subtitle = "Pooled McIntosh diversity index")

  # Mean Brillouin diversity index
  greedysel_best_mean_brillouin <-
    select.diversity(data = data, names = "genotypes", group = "Cluster",
                     alloc = counts, qualitative = traits,
                     always.selected = mand_accns,
                     div.fun = div_fun_brillouin,
                     metric = "mean", search = "greedy",
                     local.search = "best.improvement",max.iter = 3)
  greedysel_best_mean_brillouin

  plot_dist(d = dist_matrix, method = "isomds",
            gp = gp_vec,
            highlight =  unlist(greedysel_best_mean_brillouin,
                                use.names = FALSE)) +
    labs(title = "Greed search | 1-opt best improvement",
         subtitle = "Mean Brillouin diversity index")

  # Pooled Brillouin diversity index
  greedysel_best_sum_brillouin <-
    select.diversity(data = data, names = "genotypes", group = "Cluster",
                     alloc = counts, qualitative = traits,
                     always.selected = mand_accns,
                     div.fun = div_fun_brillouin,
                     metric = "pooled", search = "greedy",
                     local.search = "best.improvement",max.iter = 3)
  greedysel_best_sum_brillouin

  plot_dist(d = dist_matrix, method = "isomds",
            gp = gp_vec,
            highlight =  unlist(greedysel_best_sum_brillouin,
                                use.names = FALSE)) +
    labs(title = "Greed search | 1-opt best improvement",
         subtitle = "Pooled Brillouin diversity index")

  # Mean Margalef's richness index
  greedysel_best_mean_margalef <-
    select.diversity(data = data, names = "genotypes", group = "Cluster",
                     alloc = counts, qualitative = traits,
                     always.selected = mand_accns,
                     div.fun = div_fun_margalef,
                     metric = "mean", search = "greedy",
                     local.search = "best.improvement",max.iter = 3)
  greedysel_best_mean_margalef

  plot_dist(d = dist_matrix, method = "isomds",
            gp = gp_vec,
            highlight =  unlist(greedysel_best_mean_margalef,
                                use.names = FALSE)) +
    labs(title = "Greed search | 1-opt best improvement",
         subtitle = "Mean Margalef's diversity index")

  # Pooled Margalef's richness index
  greedysel_best_sum_margalef <-
    select.diversity(data = data, names = "genotypes", group = "Cluster",
                     alloc = counts, qualitative = traits,
                     always.selected = mand_accns,
                     div.fun = div_fun_margalef,
                     metric = "pooled", search = "greedy",
                     local.search = "best.improvement",max.iter = 3)
  greedysel_best_sum_margalef

  plot_dist(d = dist_matrix, method = "isomds",
            gp = gp_vec,
            highlight =  unlist(greedysel_best_sum_margalef,
                                use.names = FALSE)) +
    labs(title = "Greed search | 1-opt best improvement",
         subtitle = "Pooled Margalef's diversity index")

  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  # Greedy search with 1-opt first improvement
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  # Mean richness
  greedysel_first_mean_richness <-
    select.diversity(data = data, names = "genotypes", group = "Cluster",
                     alloc = counts, qualitative = traits,
                     always.selected = mand_accns, div.index = "richness",
                     metric = "mean", search = "greedy",
                     local.search = "first.improvement",max.iter = 3)
  greedysel_first_mean_richness

  plot_dist(d = dist_matrix, method = "isomds",
            gp = gp_vec,
            highlight =  unlist(greedysel_first_mean_richness,
                                use.names = FALSE)) +
    labs(title = "Greed search | 1-opt first improvement",
         subtitle = "Mean richness")

  # Pooled richness
  greedysel_first_sum_richness <-
    select.diversity(data = data, names = "genotypes", group = "Cluster",
                     alloc = counts, qualitative = traits,
                     always.selected = mand_accns, div.index = "richness",
                     metric = "pooled", search = "greedy",
                     local.search = "first.improvement",max.iter = 3)
  greedysel_first_sum_richness

  plot_dist(d = dist_matrix, method = "isomds",
            gp = gp_vec,
            highlight =  unlist(greedysel_first_sum_richness,
                                use.names = FALSE)) +
    labs(title = "Greed search | 1-opt first improvement",
         subtitle = "Pooled richness")

  # Mean Shannon-Weaver diversity index
  greedysel_first_mean_shannon <-
    select.diversity(data = data, names = "genotypes", group = "Cluster",
                     alloc = counts, qualitative = traits,
                     always.selected = mand_accns, div.index = "shannon",
                     metric = "mean", search = "greedy",
                     local.search = "first.improvement",max.iter = 3)
  greedysel_first_mean_shannon

  plot_dist(d = dist_matrix, method = "isomds",
            gp = gp_vec,
            highlight =  unlist(greedysel_first_mean_shannon,
                                use.names = FALSE)) +
    labs(title = "Greed search | 1-opt first improvement",
         subtitle = "Mean Shannon-Weaver diversity index")

  # Pooled Shannon-Weaver diversity index
  greedysel_first_sum_shannon <-
    select.diversity(data = data, names = "genotypes", group = "Cluster",
                     alloc = counts, qualitative = traits,
                     always.selected = mand_accns, div.index = "shannon",
                     metric = "pooled", search = "greedy",
                     local.search = "first.improvement",max.iter = 3)
  greedysel_first_sum_shannon

  plot_dist(d = dist_matrix, method = "isomds",
            gp = gp_vec,
            highlight =  unlist(greedysel_first_sum_shannon,
                                use.names = FALSE)) +
    labs(title = "Greed search | 1-opt first improvement",
         subtitle = "Pooled Shannon-Weaver diversity index")

  # Mean Gini-Simpson diversity index
  greedysel_first_mean_simpson <-
    select.diversity(data = data, names = "genotypes", group = "Cluster",
                     alloc = counts, qualitative = traits,
                     always.selected = mand_accns, div.index = "simpson",
                     metric = "mean", search = "greedy",
                     local.search = "first.improvement",max.iter = 3)
  greedysel_first_mean_simpson

  plot_dist(d = dist_matrix, method = "isomds",
            gp = gp_vec,
            highlight =  unlist(greedysel_first_mean_simpson,
                                use.names = FALSE)) +
    labs(title = "Greed search | 1-opt first improvement",
         subtitle = "Mean Gini-Simpson diversity index")

  # Pooled Gini-Simpson diversity index
  greedysel_first_sum_simpson <-
    select.diversity(data = data, names = "genotypes", group = "Cluster",
                     alloc = counts, qualitative = traits,
                     always.selected = mand_accns, div.index = "simpson",
                     metric = "pooled", search = "greedy",
                     local.search = "first.improvement",max.iter = 3)
  greedysel_first_sum_simpson

  plot_dist(d = dist_matrix, method = "isomds",
            gp = gp_vec,
            highlight =  unlist(greedysel_first_sum_simpson,
                                use.names = FALSE)) +
    labs(title = "Greed search | 1-opt first improvement",
         subtitle = "Pooled Gini-Simpson diversity index")

  # Mean McIntosh diversity index
  greedysel_first_mean_mcintosh <-
    select.diversity(data = data, names = "genotypes", group = "Cluster",
                     alloc = counts, qualitative = traits,
                     always.selected = mand_accns, div.index = "mcintosh",
                     metric = "pooled", search = "greedy",
                     local.search = "first.improvement",max.iter = 3)
  greedysel_first_mean_mcintosh

  plot_dist(d = dist_matrix, method = "isomds",
            gp = gp_vec,
            highlight =  unlist(greedysel_first_mean_mcintosh,
                                use.names = FALSE)) +
    labs(title = "Greed search | 1-opt first improvement",
         subtitle = "Mean McIntosh diversity index")

  # Pooled McIntosh diversity index
  greedysel_first_sum_mcintosh <-
    select.diversity(data = data, names = "genotypes", group = "Cluster",
                     alloc = counts, qualitative = traits,
                     always.selected = mand_accns, div.index = "mcintosh",
                     metric = "pooled", search = "greedy",
                     local.search = "first.improvement",max.iter = 3)
  greedysel_first_sum_mcintosh

  plot_dist(d = dist_matrix, method = "isomds",
            gp = gp_vec,
            highlight =  unlist(greedysel_first_sum_mcintosh,
                                use.names = FALSE)) +
    labs(title = "Greed search | 1-opt first improvement",
         subtitle = "Pooled McIntosh diversity index")

  # Mean Brillouin diversity index
  greedysel_first_mean_brillouin <-
    select.diversity(data = data, names = "genotypes", group = "Cluster",
                     alloc = counts, qualitative = traits,
                     always.selected = mand_accns,
                     div.fun = div_fun_brillouin,
                     metric = "mean", search = "greedy",
                     local.search = "first.improvement",max.iter = 3)
  greedysel_first_mean_brillouin

  plot_dist(d = dist_matrix, method = "isomds",
            gp = gp_vec,
            highlight =  unlist(greedysel_first_mean_brillouin,
                                use.names = FALSE)) +
    labs(title = "Greed search | 1-opt first improvement",
         subtitle = "Mean Brillouin diversity index")

  # Pooled Brillouin diversity index
  greedysel_first_sum_brillouin <-
    select.diversity(data = data, names = "genotypes", group = "Cluster",
                     alloc = counts, qualitative = traits,
                     always.selected = mand_accns,
                     div.fun = div_fun_brillouin,
                     metric = "pooled", search = "greedy",
                     local.search = "first.improvement",max.iter = 3)
  greedysel_first_sum_brillouin

  plot_dist(d = dist_matrix, method = "isomds",
            gp = gp_vec,
            highlight =  unlist(greedysel_first_sum_brillouin,
                                use.names = FALSE)) +
    labs(title = "Greed search | 1-opt first improvement",
         subtitle = "Pooled Brillouin diversity index")

  # Mean Margalef's richness index
  greedysel_first_mean_margalef <-
    select.diversity(data = data, names = "genotypes", group = "Cluster",
                     alloc = counts, qualitative = traits,
                     always.selected = mand_accns,
                     div.fun = div_fun_margalef,
                     metric = "mean", search = "greedy",
                     local.search = "first.improvement",max.iter = 3)
  greedysel_first_mean_margalef

  plot_dist(d = dist_matrix, method = "isomds",
            gp = gp_vec,
            highlight =  unlist(greedysel_first_mean_margalef,
                                use.names = FALSE)) +
    labs(title = "Greed search | 1-opt first improvement",
         subtitle = "Mean Margalef's richness index")

  # Pooled Margalef's richness index
  greedysel_first_sum_margalef <-
    select.diversity(data = data, names = "genotypes", group = "Cluster",
                     alloc = counts, qualitative = traits,
                     always.selected = mand_accns,
                     div.fun = div_fun_margalef,
                     metric = "pooled", search = "greedy",
                     local.search = "first.improvement",max.iter = 3)
  greedysel_first_sum_margalef

  plot_dist(d = dist_matrix, method = "isomds",
            gp = gp_vec,
            highlight =  unlist(greedysel_first_sum_margalef,
                                use.names = FALSE)) +
    labs(title = "Greed search | 1-opt first improvement",
         subtitle = "Pooled Margalef's richness index")
}

}
\references{
\insertAllCited
}
\seealso{
\code{\link[SampleCore]{select.random}},
  \code{\link[SampleCore]{select.distance}}
}
