write_problem: Write problem

View source: R/write_problem.R

write_problemR Documentation

Write problem

Description

Save the mathematical formulation for a conservation planning problem to a file for mixed integer programming solvers. Note that this function requires either the Rsymphony or gurobi package to be installed.

Usage

write_problem(x, path, solver = NULL)

Arguments

x

problem() object.

path

character file path to save the problem formulation. The argument should contain a ".lp" or ⁠.mps"⁠ file extension to specify whether the problem formulation will be saved in the LP or MPS format (respectively).

solver

character name of optimization solver to write the problem to disk. Available options include: "rsymphony" , "gurobi", or NULL. Note that using the Gurobi solver is much faster, because the Rsymphony solver requires attempting to solve the problem before it can be written. Defaults to NULL, such that the best available solver is used.

Value

An invisible TRUE indicating success.

Examples

## Not run: 
# set seed for reproducibility
set.seed(500)

# load data
sim_pu_polygons <- get_sim_pu_polygons()
sim_features <- get_sim_features()

# subset data to extract first four planning units
sim_pu_polygons <- sim_pu_polygons[1:4, ]

# create minimal problem
p <-
  problem(sim_pu_polygons, sim_features, cost_column = "cost") %>%
  add_min_set_objective() %>%
  add_relative_targets(0.1) %>%
  add_binary_decisions()

# specify file path to save problem formulation
path <- file.path(tempdir(), "model.lp")
print(path)

# save problem to file
## note that either the gurobi or Rsymphony package needs to be installed
write_problem(p, path)

# print model file
cat(readLines(path), sep = "\n")

## End(Not run)


prioritizr documentation built on June 22, 2024, 10:20 a.m.