PyGurobi package documentation¶
pygurobi module¶
Module contents¶
Functions to support rapid interactive modification of Gurobi models.
For reference on Gurobi objects such as Models, Variables, and Constraints, see the Gurobi Python API.
See the PyGurobi Github page for a tutorial
-
pygurobi.
add_constraint_constant
(model, variables, constant, sense='<', con_name='')¶ Add constraint to model that says the sum of variables must be equal, less than or equal, or, greater than or equal, a constant.
- Parameters
- model: Gurobi model object
- variables: List of Gurobi variables
- constant: Any number
- sense: one of ">", "<", "=", representing greater than or equal, less than or equal, and equal, respectively.
- con_name: name of the to be added constraint
-
pygurobi.
add_constraint_variables
(model, variables1, variables2, sense='=', con_name='')¶ Add constraint to model that says the sum of a list of variables must be equal, less than or equal, or greater than or equal, the sum of another list of variables.
- Parameters
- model: Gurobi model object
- variables1: List of Gurobi variables composing the left hand side of the constraint
- variables2: List of Gurobi variables composing the right hand side of the constraint
- constant: Any number
- sense: one of ">", "<", "=", representing greater than or equal, less than or equal, and equal, respectively.
- con_name: name of the to be added constraint
-
pygurobi.
filter_constraints
(constraints, filter_values, exclude=False)¶ Return a new list of constraints that match the filter values from the given constraints list.
- Parameters
- constraints: List of Gurobi constraint objects
- filter_values: Dictionary mapping index numbers to index values. For example {0: "a", 1: "b"} returns constraints with first index value of "a" and second index value of "b".
- exclude: If True return the complement of of the constraints that match the filter values.
-
pygurobi.
filter_variables
(variables, filter_values, exclude=False)¶ Return a new list of variables that match the filter values from the given variables list.
- Parameters
- variables: List of Gurobi variable objects
- filter_values: Dictionary mapping index numbers to index values. For example {0: "a", 1: "b"} returns variables with first index value of "a" and second index value of "b".
- exclude: If True return the complement of of the constraints that match the filter values.
-
pygurobi.
get_constraints
(model, name='', approx=False, filter_values={}, exclude=False)¶ Return a list of constraints from the model selected by constraint set name.
A constraint set is composed of all constraints sharing the same string identifier before the indices: A(2,3,4) and A(1,2,3) are in the same constraint set, A; A(2,3,4) and B(2,3,4) are in constraint sets A and B, respectively
PyGurobi by default assumes that constraint set names are separated from indices by round brackets "(" and ")". For example, constraints look like env(r,t) - where "env" in the constraint set name and "r" and "t" are the index values. See the source for more details.
- Parameters
- model: Gurobi model object
- name: Name of constraint set
- filter_values: Dictionary mapping index numbers to index values. For example {0: "a", 1: "b"} returns constraints with first index value of "a" and second index value of "b".
- exclude: If True return the complement of of the constraints that match the filter values.
-
pygurobi.
get_constraints_attr
(attr, model='', name='', constraints='')¶ Return a dictionary of constraint names and their corresponding attribute value.
Specifiy either model and name parameters or supply a list of constraints
- Parameters
- attr: Gurobi constraints attribute name
- model: Gurobi model object
- name: Name of constraint set
- constraints: List of Gurobi constraint objects
-
pygurobi.
get_constraints_by_index
(index, model='', name='', constraints='')¶ Return a dictionary mapping index values to lists of constraints having that index value.
Specifiy either model and name parameters or supply a list of constraints
- Parameters
- index: Index number using Python list indexing - 0 corresponds to the first index.
- model: Gurobi model object
- name: Name of constraint set
- constraints: List of Gurobi constraint objects
-
pygurobi.
get_linexp_by_index
(index, model='', name='', variables='')¶ Return a dictionary of index values to Gurobi linear expressions corresponding to the summation of variables that match the index value for the given index number.
Specifiy either model and name parameters or supply a list of variables
- Parameters
- index: Index number using Python list indexing - 0 corresponds to the first index.
- model: Gurobi model object
- name: Name of variables set
- variables: List of Gurobi variable objects
-
pygurobi.
get_linexp_from_variables
(variables)¶ Return a linear expression from the supplied list of variables.
- Parameters
- variables: List of Gurobi variable objects
-
pygurobi.
get_variables
(model, name='', approx=False, filter_values={}, exclude=False)¶ Return a list of variables from the model selected by variable set name.
-
A variable set is composed of all variables sharing the same string identifier before the indices: A[2,3,4] and A[1,2,3] are in the same variable set, A; A[2,3,4] and B[2,3,4] are in varible sets A and B, respectively
PyGurobi by default assumes that *variable names* are separated from indices by square brackets "[" and "]", For example, variables look like x[i,j] - "x" in the variable set name, and "i" and "j" and the variable's index values. See the source code for more details.
- Parameters
- model: Gurobi model object
- name: Name of variable set
- filter_values: Dictionary mapping index numbers to index values. For example {0: "a", 1: "b"} returns variables with first index value of "a" and second index value of "b".
- exclude: If True return the complement of of the variables that match the filter values.
-
pygurobi.
get_variables_attr
(attr, model='', name='', variables='')¶ Return a dictionary of variables names and their corresponding attribute value.
Specifiy either model and name parameters or supply a list of variables
- Parameters
- attr: Gurobi variables attribute name
- model: Gurobi model object
- name: Name of variable set
- variables: List of Gurobi variables objects
-
pygurobi.
get_variables_by_index
(index, model='', name='', variables='')¶ Return a dictionary mapping index values to lists of matching variables.
Specifiy either model and name parameters or supply a list of variables
- Parameters
- index: Index number using Python list indexing - 0 corresponds to the first index.
- model: Gurobi model object
- name: Name of variable set
- variables: List of Gurobi variable objects
-
pygurobi.
get_variables_by_two_indices
(index1, index2, model='', name='', variables='')¶ Return a dictionary of variables mapping index1 values to dictionaries mapping index2 values to matching variables.
Specifiy either model and name parameters or supply a list of variables
- Parameters
- index1: Index number using Python list indexing - 0 corresponds to the first index.
- index2: Index number using Python list indexing - 0 corresponds to the first index.
- model: Gurobi model object
- name: Name of variable set
- variables: List of Gurobi variable objects
-
pygurobi.
graph_by_index
(model, variables, index, title='', y_axis='', x_axis='')¶ Display a graph of variables against the specified index.
Matplotlib must already be installed to use this. See: http://matplotlib.org/faq/installing_faq.html
- Parameters
- model: Gurobi model object
- variables: List of Gurobi variable objects
- index: Index number using Python list indexing - 0 corresponds to the first index.
- title: Graph title
- y_axis: Title for y-axis
- x_axis: Title for x-axis
-
pygurobi.
graph_by_two_indices
(model, variables, index1, index2, title='', y_axis='', x_axis='')¶ Display a graph of the variable summed over index2 against index1.
Matplotlib must already be installed to use this. See: http://matplotlib.org/faq/installing_faq.html
- Parameters
- model: Gurobi model object
- variables: List of Gurobi variable objects
- index1: Index number using Python list indexing - 0 corresponds to the first index.
- index2: Index number using Python list indexing - 0 corresponds to the first index.
- title: Graph title
- y_axis: Title for y-axis
- x_axis: Title for x-axis
-
pygurobi.
list_constraints
(model)¶ Print to screen the constraint sets in the model. Show the name of each constraint set along with the number of constraints in that set.
A constraint set is composed of all constraints sharing the same string identifier before the indices: A(2,3,4) and A(1,2,3) are in the same constraint set, A; A(2,3,4) and B(2,3,4) are in constraint sets A and B, respectively
- Parameters
- model: Gurobi model object
-
pygurobi.
list_variables
(model)¶ Print to screen the variable sets in the model. Show the name of each variable set along with the number of variables in that set.
A variable set is composed of all variables sharing the same string identifier before the indices: A[2,3,4] and A[1,2,3] are in the same variable set, A; A[2,3,4] and B[2,3,4] are in variable sets A and B, respectively
- Parameters
- model: Gurobi model object
-
pygurobi.
print_constraints
(constraints)¶ Print a list of constraints to screen
- Parameters
- constraints: List of Gurobi constraint objects
-
pygurobi.
print_constraints_attr
(attr, model='', name='', constraints='')¶ Print to screen a list of constraint attribute values given by the constraints specified in the names parameter.
Specifiy either model and name parameters or supply a list of constraints
- Parameters
- attr: Gurobi constraints attribute name
- model: Gurobi model object
- name: Name of constraint set
- constraints: List of Gurobi constraint objects
-
pygurobi.
print_dict
(dictionary)¶ Print a dictionary to screen.
- Parameters
- dictionary: Python dictionary
-
pygurobi.
print_two_indices_dict
(indices_dict)¶ Print to screen a two level nested dictionary.
- Parameters
- dictionary: Two level Python dictionary: e.g. a = {1: {2: 3}}
-
pygurobi.
print_variables
(variables)¶ Print a list of variables.
- Parameters
- variables: List of Gurobi variable objects.
-
pygurobi.
print_variables_attr
(attr, model='', name='', variables='')¶ Print to screen a dictionary of variables names and their corresponding attribute value.
Specifiy either model and name parameters or supply a list of variables
- Parameters
- attr: Gurobi variables attribute name
- model: Gurobi model object
- name: Name of variable set
- variables: List of Gurobi variables objects
-
pygurobi.
print_variables_sum_by_index
(index, model='', name='', variables='')¶ Print a dictionary of variables, summed by the specified index.
- Parameters
- index: Index number using Python list indexing - 0 corresponds to the first index.
- model: Gurobi model object
- name: Name of variable set
- variables: List of Gurobi variables objects
-
pygurobi.
read_model
(filename)¶ Read a model using gurobipy.
- Parameters
- filename: Gurobi compatible file describing an optimization model.
-
pygurobi.
remove_constraints_from_model
(model, name='', constraints='')¶ Remove the given constraints from the model.
Specifiy either model and name parameters or supply a list of constraints
- Parameters
- model: Gurobi model object
- name: Name of constraint set
- constraints: List of Gurobi constraints
-
pygurobi.
remove_variables_from_model
(model, name='', variables='')¶ Remove the given variables from the model.
Specifiy either model and name parameters or supply a list of constraints
- Parameters
- model: Gurobi model object
- name: Name of variable set
- variables: List of Gurobi variables
-
pygurobi.
reoptimize
(m)¶ Update, reset, and optimize a model.
- Parameters
- model: Gurobi model object
-
pygurobi.
set_constraints_attr
(attr, val, model='', name='', constraints='')¶ Set an attribute of a model constraint set.
Specifiy either model and name parameters or supply a list of constraints
- Parameters
- attr: Gurobi constraints attribute name
- val: Constraint attribute value
- model: Gurobi model object
- name: Name of variable set
- constraints: List of Gurobi constraints
-
pygurobi.
set_constraints_rhs_as_percent
(percent, model='', name='', constraints='')¶ Set the right hand side (rhs) of a constraint set as a percentage of its current rhs.
Specifiy either model and name parameters or supply a list of constraints
- Parameters
- percent: Number between 0 and 1
- model: Gurobi model object
- name: Name of variable set
- constraints: List of Gurobi constraints
-
pygurobi.
set_variables_attr
(attr, val, model='', name='', variables='')¶ Set an attribute of a model variable set.
Specifiy either model and name parameters or supply a list of variables
- Parameters
- attr: Gurobi variables attribute name
- val: Variable attribute value
- model: Gurobi model object
- name: Name of variable set
- variables: List of Gurobi variables
-
pygurobi.
set_variables_bounds
(lb='', ub='', model='', name='', variables='')¶ Set the lower bound and/or upper bound for a variables set.
Specifiy either model and name parameters or supply a list of variables
- Parameters
- lb: Variable set lower bound
- val: Variable set upper bound
- model: Gurobi model object
- name: Name of variable set
- variables: List of Gurobi variables
-
pygurobi.
sum_variables_by_index
(index, model='', name='', variables='')¶ Return a dictionary mapping index values to the sum of the solution values of all matching variables.
Specifiy either model and name parameters or supply a list of variables
- Parameters
- index: Index number using Python list indexing - 0 corresponds to the first index.
- model: Gurobi model object
- name: Name of variable set
- variables: List of Gurobi variables
-
pygurobi.
sum_variables_by_two_indices
(index1, index2, model='', name='', variables='')¶ Return a dictionary mapping index1 values to dictionaries of the given variables summed over index2.
- Parameters
- index1: Index number using Python list indexing - 0 corresponds to the first index.
- index2: Index number using Python list indexing - 0 corresponds to the first index.
- model: Gurobi model object
- name: Name of variable set
- variables: List of Gurobi variables
-
pygurobi.
zero_all_objective_coeffs
(model)¶ Set all variable objective coefficients in a model to zero.
- Parameters
- model: Gurobi model object