HighsOCaml 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.
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)type var = {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.
kind : var_kind;lower : float;use neg_infinity for no lower bound
upper : float;use infinity for no upper bound
cost : float;coefficient in the objective
*)}val var :
?name:string ->
?kind:var_kind ->
?lower:float ->
?upper:float ->
?cost:float ->
unit ->
varGeneral constructor. Defaults: name = "", kind = Continuous, lower = 0.0, upper = infinity, cost = 0.0.
val continuous :
?name:string ->
?lower:float ->
?upper:float ->
?cost:float ->
unit ->
varval integer :
?name:string ->
?lower:float ->
?upper:float ->
?cost:float ->
unit ->
varval binary : ?name:string -> ?cost:float -> unit -> varA 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.
eq ~terms ~rhs () encodes sum(terms) = rhs.
leq ~terms ~rhs () encodes sum(terms) <= rhs.
geq ~terms ~rhs () encodes sum(terms) >= rhs.
range ~terms ~lower ~upper () encodes lower <= sum(terms) <= upper.
val model :
?name:string ->
?sense:sense ->
?offset:float ->
vars:var array ->
constraints:constr array ->
unit ->
modelAssemble a model. Defaults: name = "", sense = Minimize, offset = 0.0.
For the extra option escape hatch.
type options = {time_limit : float option;seconds
*)mip_gap : float option;relative MIP gap tolerance
*)threads : int option;output : bool;HiGHS's own log to stdout
*)solver : solver;presolve : toggle;parallel : toggle;extra : (string * option_value) list;Any HiGHS option not covered above, keyed by name.
*)}val default_options : optionsSensible defaults: no time limit, mip_gap left to HiGHS's default, all threads, output = false, everything else on Auto, no extras.
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 -> stringval pp_status : Stdlib.Format.formatter -> status -> unitWhen 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 = {status : status;objective : float;values : float array;one entry per variable
*)duals : float array;dual (reduced cost) per variable
*)row_values : float array;one entry per constraint
*)row_duals : float array;one entry per constraint
*)simplex_iterations : int64;mip_nodes : int64;Branch-and-bound nodes. HiGHS reports -1 for a model without integer or semi-continuous variables.
}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.
Solve a model. Allocates a HiGHS instance, runs the solver, and frees the instance before returning (also when an exception is raised).
val write : model -> string -> unitWrite 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.
module Version : sig ... end