model
Model(name=None, solver=None, solver_env=None, use_var_names=False, sense=None)
The object that holds all the variables, constraints, and the objective.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
Optional[str]
|
The name of the model. Currently it is not used for much. |
None
|
solver
|
Optional[SUPPORTED_SOLVER_TYPES]
|
The solver to use. If |
None
|
solver_env
|
Optional[Dict[str, str]]
|
Gurobi only: a dictionary of parameters to set when creating the Gurobi environment. |
None
|
use_var_names
|
bool
|
Whether to pass variable names to the solver. Set to |
False
|
sense
|
Union[ObjSense, ObjSenseValue, None]
|
Either "min" or "max". Indicates whether it's a minmization or maximization problem.
Typically, this parameter can be omitted ( |
None
|
Examples:
>>> m = pf.Model()
>>> m.X = pf.Variable()
>>> m.my_constraint = m.X <= 10
>>> m
<Model vars=1 constrs=1 objective=False>
Try setting the Gurobi license:
>>> m = pf.Model(solver="gurobi", solver_env=dict(ComputeServer="myserver", ServerPassword="mypassword"))
Traceback (most recent call last):
...
RuntimeError: Could not resolve host: myserver (code 6, command POST http://myserver/api/v1/cluster/jobs)
Source code in src\pyoframe\model.py
binary_variables
property
integer_variables
property
compute_IIS()
Computes the Irreducible Infeasible Set (IIS) of the model.
Gurobi only
This method only works with the Gurobi solver. Open an issue if you'd like to see support for other solvers.
Examples:
>>> m = pf.Model(solver="gurobi")
>>> m.X = pf.Variable(lb=0, ub=2)
>>> m.Y = pf.Variable(lb=0, ub=2)
>>> m.bad_constraint = m.X >= 3
>>> m.minimize = m.X + m.Y
>>> m.optimize()
>>> m.attr.TerminationStatus
<TerminationStatusCode.INFEASIBLE: 3>
>>> m.bad_constraint.attr.IIS
Traceback (most recent call last):
...
RuntimeError: Unable to retrieve attribute 'IISConstr'
>>> m.compute_IIS()
>>> m.bad_constraint.attr.IIS
True
Source code in src\pyoframe\model.py
convert_to_fixed()
Turns a mixed integer program into a continuous one by fixing all the integer and binary variables to their solution values.
Gurobi only
This method only works with the Gurobi solver. Open an issue if you'd like to see support for other solvers.
Examples:
>>> m = pf.Model(solver="gurobi")
>>> m.X = pf.Variable(vtype=pf.VType.BINARY, lb=0)
>>> m.Y = pf.Variable(vtype=pf.VType.INTEGER, lb=0)
>>> m.Z = pf.Variable(lb=0)
>>> m.my_constraint = m.X + m.Y + m.Z <= 10
>>> m.maximize = 3 * m.X + 2 * m.Y + m.Z
>>> m.optimize()
>>> m.X.solution, m.Y.solution, m.Z.solution
(1, 9, 0.0)
>>> m.my_constraint.dual
Traceback (most recent call last):
...
RuntimeError: Unable to retrieve attribute 'Pi'
>>> m.convert_to_fixed()
>>> m.optimize()
>>> m.my_constraint.dual
1.0
Only works for Gurobi:
>>> m = pf.Model("max", solver="highs")
>>> m.convert_to_fixed()
Traceback (most recent call last):
...
NotImplementedError: Method 'convert_to_fixed' is not implemented for solver 'highs'.
Source code in src\pyoframe\model.py
dispose()
Disposes of the model and cleans up the solver environment.
When using Gurobi compute server, this cleanup will ensure your run is not marked as 'ABORTED'.
Note that once the model is disposed, it cannot be used anymore.
Examples:
>>> m = pf.Model()
>>> m.X = pf.Variable(ub=1)
>>> m.maximize = m.X
>>> m.optimize()
>>> m.X.solution
1.0
>>> m.dispose()
Source code in src\pyoframe\model.py
optimize()
write(file_path, pretty=False)
Output the model to a file.
Typical usage includes writing the solution to a .sol
file as well as writing the problem to a .lp
or .mps
file.
Set use_var_names
in your model constructor to True
if you'd like the output to contain human-readable names (useful for debugging).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path
|
Union[Path, str]
|
The path to the file to write to. |
required |
pretty
|
bool
|
Only used when writing .sol files in HiGHS. If |
False
|