Skip to content

Quick Start

Rel’s Mathematical Optimization Library contains tools for expressing optimization models and calling external solvers.

The Mathematical Optimization Library consists of two main components. First it contains a domain specific language (DSL) that is used for overriding base Rel mathematical and logical operators. The operators from this DSL are used to express and define the optimization model’s constraints and objective functions. The second component is a user-friendly application programming interface (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 outlined below.

Define the Model

Define the model model:variables using rel:mathopt:variables (or rel:mathopt:named_variables).

Define the Objective Function

Use the mathematical operators from mathopt:Solver:Solver to define the model’s objective function — naming it either model:maximize or model:minimize.

Define the Constraints

Use the mathematical operators from mathopt:Solver:Solver to define the model’s constraints — naming them model:constraints:your_constraint_name.

Optimize the Model

Optimize the model using rel:mathopt:optimize[model, options].

Extract the Results

Extract the results using rel:mathopt:extract_result[result, model:variables].


Note that it is necessary to use @inline in many instances. It’s advisable to use @inline when you encounter arity errors.

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, +, *, , , , , =, -, /
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]
// 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]
Was this doc helpful?