Skip to content

Choosing a solver

How to solve on a solver other than HiGHS, pass it options, or write the model to a file. HiGHS ships with specsolve and is the default.

See which solvers this build has

Name a solver that does not exist, and the refusal lists the ones that do:

import specsolve as sps

try:
    sps.solve('examples/dispatch.yaml', {}, solver_name='cplex')
except sps.errors.SpecsolveError as exc:
    print(exc)
unknown solver 'cplex' — this build solves with gurobi, highs, xpress.

Install the solver

Every solver except HiGHS comes with the extra of the same name:

pip install "specsolve[gurobi]"

Name it in the call

solver_name= goes on solve, Model.solve and solve_over:

result = sps.solve('dispatch.yaml', sources, solver_name='gurobi')

Pass solver options

solver_options= goes to the solver verbatim, in its own vocabulary:

result = sps.solve('dispatch.yaml', sources, solver_name='gurobi', solver_options={'TimeLimit': 60})

A solver that cannot take the model

solve refuses a model the solver cannot take, such as SOS sets on HiGHS, and names the solvers that do. To ask without solving, build and check:

import specsolve as sps

model = sps.build('dispatch.yaml', sources)
model.check_sink('gurobi')

The full table is what each sink takes.

Write the model to a file

For a solver specsolve does not run, write the model. The suffix picks the format, .lp or .mps:

sps.write('dispatch.yaml', sources, 'dispatch.lp')

A written column is x0 and a row is c0 unless you ask for names. With names=True, each column and row is named by its entry and coordinate, such as p(wind,0) and balance(0), with the labels in the order of the entry's dimensions, which is by name. Use it when the tool that reads the file finds a variable by name, as JuMP's variable_by_name does:

sps.write('dispatch.yaml', sources, 'dispatch.lp', names=True)

A label keeps its letters, digits and !"#$%&'.;?@`{|}~. Every other character is written as _, because an LP reader refuses it, so north sea becomes north_sea. An entry with no dimensions is written total(). When two labels of one entry become the same name, the write is refused and the error names both labels. model.check_sink('.lp', names=True) gives the same refusal without writing a file.