Evaluation and algorithms combination

Alfonso Jiménez Vílchez

2026-12-03

Evaluation and algorithms combination

FSinR evaluation measures and search algorithms are designed following a common interface so that they are easier to use: every search algorithm is a function of the dataset, the name of the class and an evaluation measure.

In this example two evaluation measures, MDLC and Gain Ratio, are used with the Sequential Forward Selection algorithm. MDLC works with continuous features, while Gain Ratio needs discrete ones, so it is used with a discretized copy of the dataset (see discretizeFeatures).

library(FSinR)

irisDiscrete <- discretizeFeatures(iris, 'Species')

sequentialForwardSelection()(iris, 'Species', MDLC())
#> $bestFeatures
#>      Sepal.Length Sepal.Width Petal.Length Petal.Width
#> [1,]            0           0            1           0
#> 
#> $bestFitness
#> [1] -192.1499
sequentialForwardSelection()(irisDiscrete, 'Species', gainRatio())
#> $bestFeatures
#>      Sepal.Length Sepal.Width Petal.Length Petal.Width
#> [1,]            0           0            0           1
#> 
#> $bestFitness
#> [1] 0.8713692

The attributes of each measure tell which kind of data it needs and whether it has to be maximized or minimized:

attr(MDLC(), 'target')
#> [1] "minimize"
attr(MDLC(), 'needsDataToBeContinuous')
#> [1] TRUE
attr(gainRatio(), 'target')
#> [1] "maximize"
attr(gainRatio(), 'needsDataToBeDiscrete')
#> [1] TRUE

As these functions share the same interface, a list of measures can be used in a loop. Each measure is paired with the dataset it needs:

measures <- list(
  list(measure = MDLC(), data = iris),
  list(measure = gainRatio(), data = irisDiscrete),
  list(measure = symmetricalUncertain(), data = irisDiscrete)
)
for (m in measures) {
  result <- sequentialForwardSelection()(m$data, 'Species', m$measure)
  print(attr(m$measure, 'name'))
  print(result$bestFeatures)
}
#> [1] "MDLC"
#>      Sepal.Length Sepal.Width Petal.Length Petal.Width
#> [1,]            0           0            1           0
#> [1] "Gain Ratio"
#>      Sepal.Length Sepal.Width Petal.Length Petal.Width
#> [1,]            0           0            0           1
#> [1] "Symmetrical Uncertain"
#>      Sepal.Length Sepal.Width Petal.Length Petal.Width
#> [1,]            0           0            0           1

Search algorithms also share the same interface, so they can be combined as well. In this example Sequential Forward Selection, Las Vegas, the Genetic Algorithm and Tabu Search are run with all the previous evaluation measures. Some of the algorithms are stochastic, so a seed is set to obtain reproducible results. The results are collected in a table:

algorithms <- list(
  sequentialForwardSelection(),
  LasVegas(),
  geneticAlgorithm(popSize = 10, maxiter = 10),
  tabu(iter = 10)
)

runAll <- function(algorithms, measures) {
  rows <- list()
  for (algorithm in algorithms) {
    for (m in measures) {
      set.seed(1)
      result <- algorithm(m$data, 'Species', m$measure)
      selected <- colnames(result$bestFeatures)[result$bestFeatures[1, ] == 1]
      rows[[length(rows) + 1]] <- data.frame(
        Algorithm = attr(algorithm, 'name'),
        Measure = attr(m$measure, 'name'),
        Features = paste(selected, collapse = ', '),
        Value = round(as.numeric(result$bestFitness), 4)
      )
    }
  }
  do.call(rbind, rows)
}

knitr::kable(runAll(algorithms, measures))
Algorithm Measure Features Value
Sequential Forward Selection MDLC Petal.Length -192.1499
Sequential Forward Selection Gain Ratio Petal.Width 0.8714
Sequential Forward Selection Symmetrical Uncertain Petal.Width 0.8705
Las Vegas MDLC Petal.Length -192.1499
Las Vegas Gain Ratio Petal.Width 0.8714
Las Vegas Symmetrical Uncertain Petal.Width 0.8705
Genetic Algorithm MDLC Petal.Length -192.1499
Genetic Algorithm Gain Ratio Petal.Width 0.8714
Genetic Algorithm Symmetrical Uncertain Petal.Width 0.8705
Tabu Search MDLC Petal.Length -192.1499
Tabu Search Gain Ratio Petal.Width 0.8714
Tabu Search Symmetrical Uncertain Petal.Width 0.8705

Wrapper evaluation measures can also be combined. In the example a knn model is used, since the iris problem is a classification problem. The FSinR package is able to detect automatically depending on the metric whether the objective of the problem is to maximize or minimize. To tune the model, the resampling method is established as a 5-fold cross validation, the dataset is centered and scaled, the accuracy is used as a metric, and a grid of the k parameter is performed.

The wrapper evaluation measure is created and added to the list of measures, and the algorithms are run again with all of them:

resamplingParams <- list(method = "cv", number = 5)
fittingParams <- list(preProc = c("center", "scale"), metric = "Accuracy", tuneGrid = expand.grid(k = seq(1, 15, by = 2)))

wra <- wrapperEvaluator("knn", resamplingParams, fittingParams)
attr(wra, 'target')
#> [1] "maximize"

measures[[length(measures) + 1]] <- list(measure = wra, data = iris)

knitr::kable(runAll(algorithms[1:2], measures))
Algorithm Measure Features Value
Sequential Forward Selection MDLC Petal.Length -192.1499
Sequential Forward Selection Gain Ratio Petal.Width 0.8714
Sequential Forward Selection Symmetrical Uncertain Petal.Width 0.8705
Sequential Forward Selection knn Wrapper Sepal.Length, Petal.Length, Petal.Width 0.9733
Las Vegas MDLC Petal.Length -192.1499
Las Vegas Gain Ratio Petal.Width 0.8714
Las Vegas Symmetrical Uncertain Petal.Width 0.8705
Las Vegas knn Wrapper Sepal.Length, Petal.Width 0.9667

The wrapper is much slower than the filter measures, as it trains a model for each subset of features evaluated. That is why only the first two algorithms are used in this last example.