skcriteria.utils.lp module

Utilities for linnear programming based on PuLP.

This file contains an abstraction class to manipulate in a more OOP way the underlining PuLP model

skcriteria.utils.lp.is_solver_available(solver)[source]

Return True if the solver is available.

class skcriteria.utils.lp.Float(name, low=None, up=None, *args, **kwargs)[source]

Bases: _Var

pulp.LpVariable with pulp.LpContinuous category.

Example

This two codes are equivalent.

x = pulp.LpVariable("x", cat=pulp.LpContinuous)  # pure PuLP
x = lp.Float("x")  # skcriteria.utils.lp version
var_type = 'Continuous'
class skcriteria.utils.lp.Int(name, low=None, up=None, *args, **kwargs)[source]

Bases: _Var

pulp.LpVariable with pulp.LpInteger category.

Example

This two codes are equivalent.

x = pulp.LpVariable("x", cat=pulp.LpInteger)  # pure PuLP
x = lp.Int("x")  # skcriteria.utils.lp version
var_type = 'Integer'
class skcriteria.utils.lp.Bool(name, low=None, up=None, *args, **kwargs)[source]

Bases: _Var

pulp.LpVariable with pulp.LpBinary category.

Example

This two codes are equivalent.

x = pulp.LpVariable("x", cat=pulp.LpBinary)  # pure PuLP
x = lp.Bool("x")  # skcriteria.utils.lp version
var_type = 'Binary'
class skcriteria.utils.lp.Minimize(z, name='no-name', solver=None, **solver_kwds)[source]

Bases: _LPBase

Creates a Minimize LP problem with a way better sintax than PuLP.

Parameters
  • z (LpAffineExpression) – A linear combination of LpVariables.

  • name (str (default="no-name")) – Name of the problem.

  • solver (None, str or any pulp.LpSolver instance (default=None)) – Solver of the problem. If it’s None, the default solver is used. PULP is an alias os None.

  • solver_kwds (dict) – Dictionary of keyword arguments for the solver.

Example

# variable declaration
x0 = lp.Float("x0", low=0)
x1 = lp.Float("x1", low=0)
x2 = lp.Float("x2", low=0)

# model
model = lp.Maximize(  # or lp.Minimize
    z=250 * x0 + 130 * x1 + 350 * x2
)

# constraints
model.subject_to(
    120 * x0 + 200 * x1 + 340 * x2 <= 500,
    -20 * x0 + -40 * x1 + -15 * x2 <= -15,
    800 * x0 + 1000 * x1 + 600 * x2 <= 1000,
)

Also you can create the model and the constraints in one “line”.

model = lp.Maximize(   # or lp.Minimize
    z=250 * x0 + 130 * x1 + 350 * x2, solver=solver
).subject_to(
    120 * x0 + 200 * x1 + 340 * x2 <= 500,
    -20 * x0 + -40 * x1 + -15 * x2 <= -15,
    800 * x0 + 1000 * x1 + 600 * x2 <= 1000,
)
sense = 1
class skcriteria.utils.lp.Maximize(z, name='no-name', solver=None, **solver_kwds)[source]

Bases: _LPBase

Creates a Maximize LP problem with a way better sintax than PuLP.

Parameters
  • z (LpAffineExpression) – A linear combination of LpVariables.

  • name (str (default="no-name")) – Name of the problem.

  • solver (None, str or any pulp.LpSolver instance (default=None)) – Solver of the problem. If it’s None, the default solver is used. PULP is an alias os None.

  • solver_kwds (dict) – Dictionary of keyword arguments for the solver.

Example

# variable declaration
x0 = lp.Float("x0", low=0)
x1 = lp.Float("x1", low=0)
x2 = lp.Float("x2", low=0)

# model
model = lp.Maximize(  # or lp.Minimize
    z=250 * x0 + 130 * x1 + 350 * x2
)

# constraints
model.subject_to(
    120 * x0 + 200 * x1 + 340 * x2 <= 500,
    -20 * x0 + -40 * x1 + -15 * x2 <= -15,
    800 * x0 + 1000 * x1 + 600 * x2 <= 1000,
)

Also you can create the model and the constraints in one “line”.

model = lp.Maximize(   # or lp.Minimize
    z=250 * x0 + 130 * x1 + 350 * x2, solver=solver
).subject_to(
    120 * x0 + 200 * x1 + 340 * x2 <= 500,
    -20 * x0 + -40 * x1 + -15 * x2 <= -15,
    800 * x0 + 1000 * x1 + 600 * x2 <= 1000,
)
sense = -1