Solver Wrapper
HighsCustom
Bases: Highs
set_objective_without_solving
This method is implemented is the same was as the minimize() or maximize() methods of the Highs class, only that it does not call the solve() method.
That is, you can use it to set the objective value, without also running the solver.
obj
cannot be a single variable, but a linear expression.
Source code in flowpaths/utils/solverwrapper.py
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 / integer variable and a positive continuous / integer variable
- Piecewise constant constraints
- 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 continuous 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 continuous 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
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 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 |
|