EXPERIMENTAL: These features are experimental and should not be used in production systems.
The Mathematical Optimization Library
Rel’s Mathematical Optimization Library contains tools for expressing optimization models and calling external solvers.
Quick Start
The Mathematical Optimization Library consists of two main components. First it contains a domain specific language (or DSL ) that is used for overriding base Rel mathematical and logical operators. The operators from this DSL are used to express/ define optimization model constraints and objective functions. The second component is a user friendly application programming interface (or API) used for calling external solvers and extracting results returned by the solvers. Currently, the Mathematical Optimization Library supports linear programs (LPs), mixed-integer linear programs (MIPs), quadratic programs (QPs), and mixed-integer quadratic programs (MIQPs).
The basic workflow for working with the Mathematical Optimization Library is:
- Define model
model:variables
usingrel:mathopt:variables
(orrel:mathopt:named_variables
). - Use mathematical operators from
mathopt:Solver:Solver
to define the model objective function; naming it eithermodel:maximize
ormodel:minimize
. - Use mathematical operators from
mathopt:Solver:Solver
to define the model constraints; naming themmodel:constraints:your_constraint_name
. - Optimize the model using
rel:mathopt:optimize[model, options]
. - Extracting the results using
rel:mathopt:extract_result[result, model:variables]
.
Note that @inline
is required in many instances, but can easily be forgotten. The best practice is to use
@inline
when arity errors are encountered.
As a quick example, consider the following Rel code.
// read query
// Bring the rel:mathopt:Solver:Solve DSL into scope.
with mathopt:Solver:Solve use sum, foreach, +, *, ≼, ≽, ∧, ∨, =, -, /
@inline
module model
// Create an integer variable x with domain {1; 2}.
def variables = rel:mathopt:variables[{
:x, "integer", range[1, 2, 1]
}]
// Import x from variables.
with variables use x
// Define the objective function.
// Note that naming the objective "maximize" sets the sense of the solver to a maximization problem.
// To set the sense to minimization, name the relation "minimize."
def maximize = sum[x]
// Define the constraints.
module constraints
def artificial = foreach[d in range[1, 2, 1] : d * x[d] ≼ 20]
end
end
// Optimize the model.
// Note that the empty relation {} is used to specify that no options are used.
def result = rel:mathopt:optimize[model, {}]
// Extract and display the optimization results.
def output = rel:mathopt:extract_result[result, model:variables]
Mathematical Optimization Examples
To learn more about using the Mathematical Optimization Library, check out one of our examples:
Overview of Mathematical Optimization Library Relations
The following sections describe the functionality contained within rel:mathopt
.
Links to reference material are provided.
Defining Model Variables
Relation | Description |
---|---|
variables | Declare optimization model variables. |
named_variables | Declare optimization model variables with names derived from the data provided. |
Optimizing Models
You can use the following relation to optimize a model
Relation | Description |
---|---|
optimize | Optimize a model relation. |
Exporting Models
You can use the following relation to export a model to a desired file format. This is often useful when debugging models.
Relation | Description |
---|---|
export | Export the optimization model to a given file format as specified in a configuation relation. |
Extracting Results
You can use the following relation to extract results from the information returned by the solver.
Relation | Description |
---|---|
extract_result | Extract the data returned by optimizating the model or exporting the model. |
Model Configuration
Both optimize
and export
take an optional options
parameter. This parameter is a relation that can be used to configure the model.
The following relations can be used to configure the model.
Relation | Description |
---|---|
solver | Optimize or export the model with a specified solver. The available embedded solvers are "HiGHS" , "DAQP" , and "Pavito" . Support for "Gurobi" is currently under investigation. |
solver_attributes | Specify attributes of the solver used. Consult the requested solver docs for a list of available attributes. |
file_format | Specify the file format. The available file formats are "lp" , "mps" , "nl" , and "mof" . |