Module Highs

OCaml bindings for the HiGHS optimization solver.

A functional interface: an optimization problem is a value of type model built from immutable records. solve and write are the only operations that talk to HiGHS; every other function in this module is a pure OCaml constructor.

Quick example: a diet problem

  let m =
    Highs.model
      ~sense:Highs.Minimize
      ~vars:[|
        Highs.continuous ~name:"bread" ~cost:0.5 ~lower:0.0 ();
        Highs.continuous ~name:"milk"  ~cost:0.3 ~lower:0.0 ();
        Highs.continuous ~name:"meat"  ~cost:0.7 ~lower:0.0 ();
      |]
      ~constraints:[|
        Highs.geq ~name:"protein"
          ~terms:[(4.0, 0); (8.0, 1); (20.0, 2)] ~rhs:50.0 ();
        Highs.geq ~name:"calcium"
          ~terms:[(2.0, 0); (12.0, 1); (3.0, 2)] ~rhs:30.0 ();
      |]
      ()
  in
  let sol = Highs.solve m in
  match sol.status with
  | Highs.Optimal ->
    Printf.printf "cost = %.4f\n" sol.objective;
    Array.iter (Printf.printf "  %f\n") sol.values
  | s -> Printf.printf "no optimum: %s\n" (Highs.status_to_string s)

Objective sense

type sense =
  1. | Minimize
  2. | Maximize

Variables

type var_kind =
  1. | Continuous
    (*

    any real in lower, upper

    *)
  2. | Integer
    (*

    any integer in lower, upper

    *)
  3. | Binary
    (*

    0 or 1 (lower and upper ignored)

    *)
  4. | Semi_continuous
    (*

    0, or any real in lower, upper

    *)
  5. | Semi_integer
    (*

    0, or any integer in lower, upper

    *)

What values a variable can take.

type var = {
  1. name : string;
    (*

    Passed to HiGHS and used by write. Empty means unnamed, and HiGHS makes up a name when writing. If names are duplicated or contain spaces, HiGHS replaces all variable names with c0, c1, ... in the file.

    *)
  2. kind : var_kind;
  3. lower : float;
    (*

    use neg_infinity for no lower bound

    *)
  4. upper : float;
    (*

    use infinity for no upper bound

    *)
  5. cost : float;
    (*

    coefficient in the objective

    *)
}

Variable constructors

val var : ?name:string -> ?kind:var_kind -> ?lower:float -> ?upper:float -> ?cost:float -> unit -> var

General constructor. Defaults: name = "", kind = Continuous, lower = 0.0, upper = infinity, cost = 0.0.

val continuous : ?name:string -> ?lower:float -> ?upper:float -> ?cost:float -> unit -> var
val integer : ?name:string -> ?lower:float -> ?upper:float -> ?cost:float -> unit -> var
val binary : ?name:string -> ?cost:float -> unit -> var

Constraints

A term is a pair (coefficient, variable_index). Read (4.0, 0) as "4.0 times variable 0". The index is a position in the model's vars array. Each variable may appear at most once per constraint.

type term = float * int
type constr = {
  1. name : string;
    (*

    as for var: empty means unnamed

    *)
  2. lower : float;
  3. upper : float;
  4. terms : term list;
}

Constraint constructors

val eq : ?name:string -> terms:term list -> rhs:float -> unit -> constr

eq ~terms ~rhs () encodes sum(terms) = rhs.

val leq : ?name:string -> terms:term list -> rhs:float -> unit -> constr

leq ~terms ~rhs () encodes sum(terms) <= rhs.

val geq : ?name:string -> terms:term list -> rhs:float -> unit -> constr

geq ~terms ~rhs () encodes sum(terms) >= rhs.

val range : ?name:string -> terms:term list -> lower:float -> upper:float -> unit -> constr

range ~terms ~lower ~upper () encodes lower <= sum(terms) <= upper.

Models

type model = {
  1. name : string;
  2. sense : sense;
  3. offset : float;
    (*

    constant added to the objective

    *)
  4. vars : var array;
  5. constraints : constr array;
}
val model : ?name:string -> ?sense:sense -> ?offset:float -> vars:var array -> constraints:constr array -> unit -> model

Assemble a model. Defaults: name = "", sense = Minimize, offset = 0.0.

Solver options

type solver =
  1. | Simplex
  2. | Ipm
  3. | Pdlp
  4. | Auto

LP algorithm to use.

type toggle =
  1. | On
  2. | Off
  3. | Auto

For yes/no/let-HiGHS-decide options.

type option_value =
  1. | Bool of bool
  2. | Int of int
  3. | Float of float
  4. | String of string

For the extra option escape hatch.

type options = {
  1. time_limit : float option;
    (*

    seconds

    *)
  2. mip_gap : float option;
    (*

    relative MIP gap tolerance

    *)
  3. threads : int option;
  4. output : bool;
    (*

    HiGHS's own log to stdout

    *)
  5. solver : solver;
  6. presolve : toggle;
  7. parallel : toggle;
  8. extra : (string * option_value) list;
    (*

    Any HiGHS option not covered above, keyed by name.

    *)
}
val default_options : options

Sensible defaults: no time limit, mip_gap left to HiGHS's default, all threads, output = false, everything else on Auto, no extras.

Status of a completed solve

type status =
  1. | Optimal
  2. | Infeasible
  3. | Unbounded
  4. | Unbounded_or_infeasible
  5. | Time_limit
  6. | Iteration_limit
  7. | Model_error
  8. | Interrupted
  9. | Not_solved
  10. | Other of string
    (*

    A HiGHS model-status we don't map explicitly (rare).

    *)

HiGHS's model status. HiGHS's load, presolve, solve and postsolve errors all map to Model_error; an empty model (no variables) is Not_solved.

val status_to_string : status -> string
val pp_status : Stdlib.Format.formatter -> status -> unit

Solution

When HiGHS has no primal solution (for example, the model is infeasible, or a MIP hit its time limit before finding a feasible point), values and row_values are filled with nan. Likewise duals and row_duals when it has no dual solution.

type solution = {
  1. status : status;
  2. objective : float;
  3. values : float array;
    (*

    one entry per variable

    *)
  4. duals : float array;
    (*

    dual (reduced cost) per variable

    *)
  5. row_values : float array;
    (*

    one entry per constraint

    *)
  6. row_duals : float array;
    (*

    one entry per constraint

    *)
  7. simplex_iterations : int64;
  8. mip_nodes : int64;
    (*

    Branch-and-bound nodes. HiGHS reports -1 for a model without integer or semi-continuous variables.

    *)
}

Errors

exception Solver_error of string

Raised when an option is rejected, a model cannot be loaded, or a file cannot be written. The message names the offending option key, constraint, variable index or path when there is one.

The core operation

val solve : ?options:options -> model -> solution

Solve a model. Allocates a HiGHS instance, runs the solver, and frees the instance before returning (also when an exception is raised).

  • raises Solver_error

    if an option is rejected or the model cannot be loaded (for example, a term refers to a variable index outside the model, or repeats a variable within one constraint). Infeasible and unbounded models, and failures inside the solver, are not exceptions; they are reported through status.

File I/O

val write : model -> string -> unit

Write the model to a file in LP format (path ending in .lp) or MPS format (path ending in .mps). Names from the model are used where given. Prints nothing.

  • raises Solver_error

    if the model cannot be loaded, the extension is neither .lp nor .mps, or the file cannot be written.

Version

module Version : sig ... end