Solver Wrapper
SolverWrapper
A wrapper class for the both the HiGHS (highspy) and Gurobi (gurobipy) solvers.
This supports the following functionalities:
- Adding:
- Variables
- Constraints
- Product Constraints, encoding the product of a binary variable and a positive continuous / integer variable
- Setting the objective
- Optimizing, and getting the model status
- Writing the model to a file
- Getting variable names and values
- Getting the objective value
Source code in flowpaths/utils/solverwrapper.py
add_binary_continuous_product_constraint
add_binary_continuous_product_constraint(binary_var, continuous_var, product_var, lb, ub, name: str)
Description
This function adds constraints to model the equality: binary_var
* continuous_var
= product_var
.
Assumptions
binary_var
\(\in [0,1]\)- lb ≤
continuous_var
≤ ub
Note
This works correctly also if continuous_var
is an integer variable.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
binary_var
|
variable
|
The binary variable. |
required |
continuous_var
|
variable
|
The continuous variable (can also be integer). |
required |
product_var
|
variable
|
The variable that should be equal to the product of the binary and continous variables. |
required |
lb
|
float
|
The lower bound of the continuous variable. |
required |
ub
|
float
|
The upper bound of the continuous variable. |
required |
name
|
str
|
The name of the constraint. |
required |
Source code in flowpaths/utils/solverwrapper.py
add_integer_continuous_product_constraint
add_integer_continuous_product_constraint(integer_var, continuous_var, product_var, lb, ub, name: str)
This function adds constraints to model the equality
integer_var * continuous_var = product_var
Assumptions
lb <= product_var <= ub
Note
This works correctly also if continuous_var
is an integer variable.
Parameters
binary_var : Variable The binary variable. continuous_var : Variable The continuous variable (can also be integer). product_var : Variable The variable that should be equal to the product of the binary and continous variables. lb, ub : float The lower and upper bounds of the continuous variable. name : str The name of the constraint
Source code in flowpaths/utils/solverwrapper.py
add_piecewise_constant_constraint
Enforces that variable y
equals a constant from constants
depending on the range that x
falls into.
For each piece i
if x in [ranges[i][0], ranges[i][1]] then y = constants[i].
Assumptions
- The ranges must be non-overlapping. Otherwise, if x belongs to more ranges, the solver will choose one arbitrarily.
- The value of x must be within the union of the ranges. Otherwise the solver will not find a feasible solution.
This is modeled by: - introducing binary variables z[i] with sum(z) = 1, - for each piece i: x >= L_i - M(1 - z[i]) x <= U_i + M(1 - z[i]) y <= constant[i] + M(1 - z[i]) y >= constant[i] - M(1 - z[i])
Parameters
x: The continuous variable (created earlier) whose value determines the segment. y: The continuous variable whose value equals the corresponding constant. ranges: List of tuples [(L0, U0), (L1, U1), …] constants: List of constants [c0, c1, …] for each segment. name_prefix: A prefix for naming the added variables and constraints.
Returns
y: The created piecewise output variable.
Source code in flowpaths/utils/solverwrapper.py
get_variable_values
Retrieve the values of variables whose names start with a given prefix.
This method extracts variable values from the solver, filters them based on a specified prefix, and returns them in a dictionary with appropriate indexing.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name_prefix
|
str
|
The prefix of the variable names to filter. |
required |
index_types
|
list
|
A list of types corresponding to the indices of the variables. Each type in the list is used to cast the string indices to the appropriate type. If empty, then it is assumed that the variable has no index, and does exact matching with the variable name. |
required |
binary_values
|
bool
|
If True, ensures that the variable values (rounded) are binary (0 or 1). Defaults to False. |
False
|
Returns:
Name | Type | Description |
---|---|---|
values |
dict
|
A dictionary where the keys are the indices of the variables (as tuples or single values) and the values are the corresponding variable values. If index_types is empty, then the unique key is 0 and the value is the variable value. |
Raises:
Type | Description |
---|---|
Exception
|
If the length of |
Exception
|
If |
Source code in flowpaths/utils/solverwrapper.py
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 |
|