Learn R Programming

windfarmGA (version 5.0.0)

trimton: Adjust the amount of turbines per windfarm

Description

Legacy repair for binary chromosomes. The GA loop encodes layouts as n unique grid IDs, so this function is not called there. It remains exported for the old 0/1 pipeline (crossover / mutation).

Usage

trimton(mut, nturb, allparks, nGrids, trimForce, seed)

Value

Returns a binary matrix with the correct amount of turbines per individual

Arguments

mut

A binary matrix with the mutated individuals

nturb

A numeric value indicating the amount of required turbines

allparks

A data.frame consisting of all individuals of the current generation

nGrids

A numeric value indicating the total amount of grid cells

trimForce

If TRUE, add or drop turbines using fitness-weighted probabilities. If FALSE, choose cells at random.

seed

Set a seed for comparability. Default is NULL

See Also

Other Genetic Algorithm Functions: crossover(), fitness(), genetic_algorithm(), init_population(), mutation(), selection(), set_crossover(), swap_mutation()

Examples

Run this code
# \donttest{
## Create a random rectangular shapefile
library(sf)
area <- sf::st_as_sf(sf::st_sfc(
  sf::st_polygon(list(cbind(
    c(0, 0, 2000, 2000, 0),
    c(0, 2000, 2000, 0, 0)
  ))),
  crs = 3035
))

## Create a uniform and unidirectional wind data.frame and plots the
## resulting wind rose
## Uniform wind speed and single wind direction
data.in <- as.data.frame(cbind(ws = 12, wd = 0))

## Calculate a Grid and an indexed data.frame with coordinates and grid cell Ids.
Grid1 <- grid_area(area = area, size = 200, prop = 1)
Grid <- Grid1[[1]]
AmountGrids <- nrow(Grid)

startsel <- init_population(Grid, 10, 20)
wind <- as.data.frame(cbind(ws = 12, wd = 0))
wind <- list(wind, probab = 100)
fit <- fitness(
  population = startsel, reference_height = 100, rotor_height = 100,
  surface_roughness = 0.3, area = area, rotor = 20,
  wind = wind, terrain = FALSE
)
allparks <- do.call("rbind", fit)
## selection() returns ID matrices; crossover()/trimton() expect 0/1.
sel <- selection(fit, Grid, 2, TRUE, 6, "FIX")
ids <- sel[[1]]
bins <- matrix(0, nrow(Grid), ncol(ids))
for (j in seq_len(ncol(ids))) {
  bins[match(ids[, j], Grid[, "ID"]), j] <- 1
}
selec6best <- list(
  data.frame(ID = Grid[, "ID"], bins),
  data.frame(ID = 1, t(sel[[2]]))
)
crossOut <- crossover(selec6best, 2, uplimit = 300, crossPart = "RAN")
mut <- mutation(a = crossOut, p = 0.3, NULL)
mut1 <- trimton(
  mut = mut, nturb = 10, allparks = allparks, nGrids = AmountGrids,
  trimForce = FALSE
)
colSums(mut)
colSums(mut1)
# }

Run the code above in your browser using DataLab