Skip to content
OPTIMIZATION

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:

  1. Define model model:variables using rel:mathopt:variables (or rel:mathopt:named_variables).
  2. Use mathematical operators from mathopt:Solver:Solver to define the model objective function; naming it either model:maximize or model:minimize.
  3. Use mathematical operators from mathopt:Solver:Solver to define the model constraints; naming them model:constraints:your_constraint_name.
  4. Optimize the model using rel:mathopt:optimize[model, options].
  5. 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

RelationDescription
variablesDeclare optimization model variables.
named_variablesDeclare optimization model variables with names derived from the data provided.

Optimizing Models

You can use the following relation to optimize a model

RelationDescription
optimizeOptimize 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.

RelationDescription
exportExport 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.

RelationDescription
extract_resultExtract 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.

RelationDescription
solverOptimize 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_attributesSpecify attributes of the solver used. Consult the requested solver docs for a list of available attributes.
file_formatSpecify the file format. The available file formats are "lp", "mps", "nl", and "mof".
Was this doc helpful?