add_random_solver: Add a random solver

View source: R/add_random_solver.R

add_random_solverR Documentation

Add a random solver

Description

Specify that solutions should be generated using random processes. Although prioritizations should be developed using optimization routines, a portfolio of randomly generated solutions can be useful for evaluating the effectiveness of solutions.

Usage

add_random_solver(x, number_solutions = 1, verbose = TRUE)

Arguments

x

ProjectProblem object.

number_solutions

integer number of solutions desired. Defaults to 1. Note that the number of returned solutions can sometimes be less than the argument to number_solutions depending on the argument to solution_pool_method, for example if 100 solutions are requested but only 10 unique solutions exist, then only 10 solutions will be returned.

verbose

logical should information be printed while solving optimization problems?

Details

The algorithm used to randomly generate solutions depends on the the objective specified for the project prioritization problem().

For objectives which maximize benefit subject to budgetary constraints (e.g. add_max_richness_objective()):

  1. All locked in and zero-cost actions are initially selected for funding (excepting actions which are locked out).

  2. A project—and all of its associated actions—is randomly selected for funding (excepting projects associated with locked out actions, and projects which would cause the budget to be exceeded when added to the existing set of selected actions).

  3. The previous step is repeated until no more projects can be selected for funding without the total cost of the prioritized actions exceeding the budget.

For objectives which minimize cost subject to biodiversity constraints (i.e. add_min_set_objective():

  1. All locked in and zero-cost actions are initially selected for funding (excepting actions which are locked out).

  2. A project—and all of its associated actions—is randomly selected for funding (excepting projects associated with locked out actions, and projects which would cause the budget to be exceeded when added to the existing set of selected actions).

  3. The previous step is repeated until all of the persistence targets are met.

Value

ProjectProblem object with the solver added to it.

See Also

solvers.

Examples

# load data
data(sim_projects, sim_features, sim_actions)

# build problem with random solver, and generate 100 random solutions
p1 <- problem(sim_projects, sim_actions, sim_features,
             "name", "success", "name", "cost", "name") %>%
     add_max_richness_objective(budget = 200) %>%
     add_binary_decisions() %>%
     add_random_solver(number_solutions = 100)

# print problem
print(p1)

# solve problem
s1 <- solve(p1)

# print solutions
print(s1)

# plot first random solution
plot(p1, s1)

# plot histogram of the objective values for the random solutions
hist(s1$obj, xlab = "Expected richness", xlim = c(0, 2.5),
     main = "Histogram of random solutions")

# since the objective values don't tell us much about the quality of the
# solutions, we can find the optimal solution and calculate how different
# each of the random solutions is from optimality

## Not run: 
# find the optimal objective value using an exact algorithms solver
s2 <- p1 %>%
      add_default_solver() %>%
      solve()

# create new column in s1 with percent difference from optimality
s1$optimality_diff <- ((s2$obj - s1$obj) / s1$obj) * 100

# plot histogram showing the quality of the random solutions
# higher numbers indicate worse solutions
hist(s1$optimality_diff, xlab = "Difference from optimality (%)",
     main = "Histogram of random solutions", xlim = c(0, 50))

## End(Not run)

oppr documentation built on Sept. 8, 2022, 5:07 p.m.