Solvers¶
Solvers are thin wrappers that put the numerical backends, such as Gurobi and HiGHS, behind a common interface.
Solver
¶
Bases: object
Abstract base class for optimization solvers.
This class defines the common interface for all solver backends in FOMTools, including MIP solvers (Gurobi, HiGHS) and SDP solvers (Clarabel). Subclasses must implement the abstract methods to interface with their specific solver.
Attributes:
| Name | Type | Description |
|---|---|---|
vector_var_map |
dict
|
Mapping from LinearExpression objects to their corresponding solver variables. |
Source code in fomtools/solvers/solver.py
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 | |
__init__
¶
__init__(solver_params=None)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
solver_params
|
dict
|
Dictionary of solver-specific parameters to configure. Keys are parameter names and values are the settings. |
None
|
Source code in fomtools/solvers/solver.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | |
add_constr
¶
add_constr(constr)
Add a single constraint to the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
constr
|
Constraint
|
The constraint to add to the solver model. |
required |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
Must be implemented by subclasses. |
Source code in fomtools/solvers/solver.py
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | |
add_param_stack_var
¶
add_param_stack_var(param_stack_var, param_list)
Create a stacked variable from a list of parameter variables.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
param_stack_var
|
LinearExpression
|
The new variable to register as the stacked result. |
required |
param_list
|
list[LinearExpression]
|
List of existing variables to stack together. |
required |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
Must be implemented by subclasses. |
Source code in fomtools/solvers/solver.py
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | |
add_var
¶
add_var(iterate, lb=-inf, ub=inf)
Add a decision variable to the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
iterate
|
LinearExpression
|
The variable to register with the solver. |
required |
lb
|
float
|
Lower bound for the variable. |
-np.inf
|
ub
|
float
|
Upper bound for the variable. |
np.inf
|
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
Must be implemented by subclasses. |
Source code in fomtools/solvers/solver.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | |
constraints_to_solver
¶
constraints_to_solver(constraints)
Add multiple constraints to the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
constraints
|
list[Constraint]
|
List of constraints to add to the solver model. |
required |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
Must be implemented by subclasses. |
Source code in fomtools/solvers/solver.py
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | |
copy
¶
copy() -> Solver
Create an independent copy of this solver.
The copy can be modified without affecting the original.
Returns:
| Type | Description |
|---|---|
Solver
|
A new solver instance with copied state. |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
Must be implemented by subclasses. |
Source code in fomtools/solvers/solver.py
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 | |
optimize
¶
optimize()
Solve the optimization model.
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
Must be implemented by subclasses. |
Source code in fomtools/solvers/solver.py
152 153 154 155 156 157 158 159 160 161 | |
set_param
¶
set_param(param, value)
Set an individual solver parameter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
param
|
str
|
Name of the solver parameter. |
required |
value
|
int or float
|
Value to assign to the parameter. |
required |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
Must be implemented by subclasses. |
Source code in fomtools/solvers/solver.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | |
set_solver_params
¶
set_solver_params(solver_params)
Apply a dictionary of solver parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
solver_params
|
dict
|
Dictionary mapping parameter names to their values.
Each key-value pair is passed to |
required |
Source code in fomtools/solvers/solver.py
51 52 53 54 55 56 57 58 59 60 61 62 | |
verbosity_params
classmethod
¶
verbosity_params(params: dict | None) -> dict | None
The verbosity option from params, if it names this backend's option.
Used to pass only the output setting of the main solver's options on to a secondary solver of the same backend (e.g. the OBBT LP).
Source code in fomtools/solvers/solver.py
39 40 41 42 43 44 45 46 47 48 49 | |
GurobiSolver
¶
Bases: MIPSolver
A wrapper around the Gurobi Model that bridges LinearExpression
and Constraint objects to solver-ready variables, expressions, and
constraints.
Attributes:
| Name | Type | Description |
|---|---|---|
model |
Model
|
The underlying Gurobi model instance. |
... |
|
Source code in fomtools/solvers/gurobi.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 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 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 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 | |
__init__
¶
__init__(solver_params=None)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
solver_params
|
Gurobi parameters; they override |
None
|
Source code in fomtools/solvers/gurobi.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | |
add_constr
¶
add_constr(constraint) -> None
Add a single constraint to the Gurobi model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
constraint
|
A compatible Gurobi constraint expression. Typical forms include
comparisons like |
required |
Returns:
| Type | Description |
|---|---|
None
|
|
Source code in fomtools/solvers/gurobi.py
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 | |
add_param_stack_var
¶
add_param_stack_var(param_stack_var: LinearExpression, param_list: list[LinearExpression]) -> None
Create a stacked Gurobi MVar by horizontally concatenating
existing parameter variables.
Registers param_stack_var in self.vector_var_map as the
horizontal stack of the variables associated with each element of
param_list. This is useful for building block-structured vectors
from previously created components.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
param_stack_var
|
LinearExpression
|
LinearExpression object to be registered. |
required |
param_list
|
list[LinearExpression]
|
List of LinearExpression objects whose associated Gurobi variables are to be stacked.
Each entry must already exist in |
required |
Returns:
| Type | Description |
|---|---|
None
|
|
Raises:
| Type | Description |
|---|---|
KeyError
|
If any element of |
Source code in fomtools/solvers/gurobi.py
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 | |
add_sum_squares_constr
¶
add_sum_squares_constr(expr, radius_sq, sense) -> None
Add a sum-of-squares (quadratic) constraint to the Gurobi model.
Encodes sum_i (a_i^T x + b_i)^2 op radius_sq where op is given by
sense. The squared two-norm is built as gp_expr @ gp_expr (a dot product),
the same construct used by set_two_norm_objective. The const term b is
included via expression_to_solver.
The >= / == senses are nonconvex quadratic constraints; NonConvex=2 is
set for them so Gurobi accepts the model across versions (the convex <= sense
does not need it).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expr
|
LinearExpression
|
The expression whose squared two-norm is constrained. |
required |
radius_sq
|
float
|
The nonnegative scalar right-hand side (e.g. |
required |
sense
|
str {"<=", ">=", "=="}
|
The relational operator. |
required |
Source code in fomtools/solvers/gurobi.py
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 444 445 446 447 448 449 450 451 452 453 | |
add_var
¶
add_var(var: LinearExpression, lb: float = -inf, ub: float = inf) -> Var
Create and register a Gurobi matrix variable corresponding to the
given LinearExpression.
This method wraps model.addVar to create one or more decision variables
whose shape matches that of the provided LinearExpression. It also records
the mapping between the LinearExpression object and its associated Gurobi
variable for subsequent reference. The created variable(s) have the specified
bounds and types.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
var
|
LinearExpression
|
LinearExpression object to be registered. |
required |
lb
|
float
|
Lower bound for all the elements of the matrix variable. |
-inf
|
ub
|
float
|
Upper bound for all the elements of the matrix variable. |
inf
|
Returns:
| Type | Description |
|---|---|
None
|
|
Source code in fomtools/solvers/gurobi.py
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | |
constraints_to_solver
¶
constraints_to_solver(constraints: List[Constraint])
Add multiple constraints to the Gurobi model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
constraints
|
List[Constraint]
|
A list of constraint expressions, typically of the form |
required |
Returns:
| Type | Description |
|---|---|
None
|
|
Source code in fomtools/solvers/gurobi.py
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 | |
copy
¶
copy() -> GurobiSolver
Create an independent copy of this Gurobi solver.
Returns a new GurobiSolver with a copied model and rebuilt vector_var_map pointing to variables in the copied model.
Returns:
| Type | Description |
|---|---|
GurobiSolver
|
A new solver instance with copied state. |
Source code in fomtools/solvers/gurobi.py
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 | |
expression_to_solver
¶
expression_to_solver(expression: LinearExpression) -> MLinExpr
Convert a LinearExpression into a Gurobi MLinExpr.
Given a LinearExpression object with a decomposition of the form
expression.decomposition_dict = {key_1: matrix_1, ..., key_n: matrix_n},
this method constructs the equivalent solver-side representation by multiplying each
coefficient matrix_i with its corresponding Gurobi variable and
summing the results:
The resulting object is a Gurobi linear expression with the same dimensionality as
the input expression, and can be directly used in model constraints or
objective functions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expression
|
LinearExpression
|
The LinearExpression object to be converted into its corresponding Gurobi representation. |
required |
Returns:
| Type | Description |
|---|---|
MLinExpr
|
The Gurobi linear expression corresponding to the input |
Source code in fomtools/solvers/gurobi.py
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 | |
get_relaxed_variables
¶
get_relaxed_variables(expression: LinearExpression) -> Tuple[MVar, ndarray]
Reconstruct solver-side variables from a LinearExpression object
using the relaxed Gurobi model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expression
|
LinearExpression
|
A |
required |
Returns:
| Type | Description |
|---|---|
Tuple[MVar, ndarray]
|
A single Gurobi matrix variable formed by horizontally stacking the
individual |
Source code in fomtools/solvers/gurobi.py
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 | |
get_solution_value
¶
get_solution_value(expression: LinearExpression) -> ndarray
Compute the solution value for the given LinearExpression
object.
For each leaf expression in the decomposition of the given
LinearExpression object, this method retrieves the corresponding
solver variables from the model and computes the dot product between
the coefficient matrix and the vector of variable values associated with
that leaf.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expression
|
LinearExpression
|
A |
required |
Returns:
| Type | Description |
|---|---|
ndarray[float]
|
A 1-D array of shape |
Source code in fomtools/solvers/gurobi.py
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 | |
has_solution
¶
has_solution() -> bool
Whether the last solve produced a feasible solution.
Source code in fomtools/solvers/gurobi.py
153 154 155 | |
is_optimal
¶
is_optimal() -> bool
Check whether the underlying Gurobi model has reached an optimal solution.
Source code in fomtools/solvers/gurobi.py
555 556 557 558 559 | |
leaf_expr_to_solver
¶
leaf_expr_to_solver(expression: LinearExpression) -> LinearExpression
Resolve a leaf LinearExpression into its solver-side Gurobi variable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expression
|
LinearExpression
|
A leaf |
required |
Returns:
| Type | Description |
|---|---|
Var or MVar
|
The Gurobi variable corresponding to the leaf |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
Source code in fomtools/solvers/gurobi.py
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 | |
optimize
¶
optimize()
Optimize the underlying Gurobi model.
Notes
The algorithm used depends on the model objective and on the parameter settings
Source code in fomtools/solvers/gurobi.py
482 483 484 485 486 487 488 489 490 491 | |
relax_model
¶
relax_model()
Build and solve the continuous relaxation of the current model.
This method creates a Gurobi model in which all integrality
constraints are relaxed to continuous domains and optimizes
it. It then clears any cached relaxed_model property
so that future accesses re-expose the freshly computed
object.
Source code in fomtools/solvers/gurobi.py
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | |
relaxed_is_optimal
¶
relaxed_is_optimal() -> bool
Whether the last relax_model solve ended optimal.
Source code in fomtools/solvers/gurobi.py
157 158 159 | |
set_objective
¶
set_objective(expression: LinearExpression, sense: str = None) -> None
Set the optimization objective for the underlying Gurobi model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expression
|
LinearExpression
|
A |
required |
sense
|
str
|
Objective sense specified as a string. |
None
|
Returns:
| Type | Description |
|---|---|
None
|
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If provided objective |
ValueError
|
If provided expression is anything other than a |
Source code in fomtools/solvers/gurobi.py
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 | |
set_param
¶
set_param(param: str, val)
Configure a solver parameter, passed verbatim to Gurobi.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
param
|
str
|
The native Gurobi parameter name to set (e.g. |
required |
val
|
The value to assign to the specified parameter. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If Gurobi rejects the parameter name or value. |
Source code in fomtools/solvers/gurobi.py
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | |
set_starts
¶
set_starts(starts: list) -> None
Provide MIP starts, one {leaf: value} map each. Gurobi tries every start
and keeps the best feasible one; leaves a start omits are completed by Gurobi.
Source code in fomtools/solvers/gurobi.py
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | |
set_two_norm_objective
¶
set_two_norm_objective(expr_list, lb_list, ub_list) -> None
Set a quadratic two-norm objective for the underlying Gurobi model.
The objective is to maximize sum_i ||expr_i||_2^2 = sum_i (expr_i @ expr_i).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expr_list
|
list[LinearExpression]
|
List of expressions whose squared norms are summed in the objective. |
required |
lb_list
|
list[ndarray]
|
Lower bounds for each expression (currently unused but passed for interface consistency). |
required |
ub_list
|
list[ndarray]
|
Upper bounds for each expression (currently unused but passed for interface consistency). |
required |
Returns:
| Type | Description |
|---|---|
None
|
|
Source code in fomtools/solvers/gurobi.py
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 | |
update_bounds
¶
update_bounds(expression: LinearExpression, lb: float | ndarray, ub: float | ndarray)
Update the lower and upper bounds of the solver variables corresponding
to the given LinearExpression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expression
|
LinearExpression
|
A |
required |
lb
|
float | ndarray
|
New lower bound(s). |
required |
ub
|
float | ndarray
|
New upper bound(s). |
required |
Source code in fomtools/solvers/gurobi.py
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 | |
HiGHSSolver
¶
Bases: MIPSolver
Source code in fomtools/solvers/highs.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 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 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 | |
add_constr
¶
add_constr(constraint: Constraint | highs_linear_expression)
Add a single constraint to the HiGHS model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
constraint
|
Constraint | highs_linear_expression
|
A compatible HiGHS constraint expression. Typical forms include
comparisons like |
required |
Returns:
| Type | Description |
|---|---|
None
|
|
Source code in fomtools/solvers/highs.py
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 | |
add_var
¶
add_var(var: LinearExpression, lb: float = -inf, ub: float = inf)
Create and register a list of HiGHS variables corresponding to the
given LinearExpression.
This method creates a list of HiGHS variables using hp.Highs().addIntegral
or hp.Highs().addVariable to create one or more decision variables whose
shape matches that of the provided LinearExpression. It also records
the mapping between the LinearExpression object and its associated list of
HiGHS variables for subsequent reference. The created variable(s) have the specified
bounds and types.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
var
|
LinearExpression
|
LinearExpression object to be registered. |
required |
lb
|
float
|
Lower bound for all the elements of the matrix variable. |
-inf
|
ub
|
float
|
Upper bound for all the elements of the matrix variable. |
inf
|
Returns:
| Type | Description |
|---|---|
None
|
|
Source code in fomtools/solvers/highs.py
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | |
constraints_to_solver
¶
constraints_to_solver(constraints: List[Constraint]) -> None
Add multiple constraints to the HiGHS model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
constraints
|
List[Constraint]
|
A list of constraint expressions, typically of the form |
required |
Returns:
| Type | Description |
|---|---|
None
|
|
Source code in fomtools/solvers/highs.py
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | |
copy
¶
copy() -> HiGHSSolver
Create an independent copy of this HiGHS solver.
Returns a new HiGHSSolver with a copied model and rebuilt vector_var_map pointing to variables in the copied model.
Returns:
| Type | Description |
|---|---|
HiGHSSolver
|
A new solver instance with copied state. |
Source code in fomtools/solvers/highs.py
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 | |
expression_to_solver
¶
expression_to_solver(expression: LinearExpression) -> ndarray[highs_linear_expression]
Convert a LinearExpression into a list of HiGHS highs_linear_expression.
Given a LinearExpression object with a decomposition of the form
expression.decomposition_dict = {key_1: matrix_1, ..., key_n: matrix_n},
this method constructs the equivalent solver-side representation by multiplying each
coefficient matrix_i with its corresponding HiGHS variable and
summing the results:
The resulting object is a NumPy array of HiGHS linear expressions with the same
dimensionality as the input expression, the elements of which can be
used in model constraints or objective functions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expression
|
LinearExpression
|
The LinearExpression object to be converted into its corresponding HiGHS form. |
required |
Returns:
| Type | Description |
|---|---|
ndarray[highs_linear_expression]
|
An array of HiGHS linear expressions corresponding to the input |
Source code in fomtools/solvers/highs.py
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | |
get_relaxed_variables
¶
get_relaxed_variables(expression: LinearExpression) -> Tuple[list[highs_var], ndarray]
Reconstruct solver-side variables from a LinearExpression object
using the relaxed HiGHS model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expression
|
LinearExpression
|
A |
required |
Returns:
| Type | Description |
|---|---|
Tuple[list[highs_var], ndarray]
|
A list of HiGHS variables formed by stacking the individual
|
Source code in fomtools/solvers/highs.py
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 | |
get_solution_value
¶
get_solution_value(expression: LinearExpression) -> ndarray
Compute the solution value for the given LinearExpression
object.
For each leaf expression in the decomposition of the given
LinearExpression object, this method retrieves the corresponding
solver variables from the model and computes the dot product between
the coefficient matrix and the vector of variable values associated with
that leaf.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expression
|
LinearExpression
|
A |
required |
Returns:
| Type | Description |
|---|---|
ndarray[float]
|
A 1-D array of shape |
Source code in fomtools/solvers/highs.py
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 | |
has_solution
¶
has_solution() -> bool
Whether the last solve produced a feasible solution.
Source code in fomtools/solvers/highs.py
110 111 112 | |
is_optimal
¶
is_optimal() -> bool
Check whether the underlying HiGHS model has reached an optimal solution.
Source code in fomtools/solvers/highs.py
395 396 397 398 399 | |
optimize
¶
optimize()
Optimize the underlying HiGHS model.
Notes
The algorithm used depends on the model objective and on the parameter settings
Source code in fomtools/solvers/highs.py
321 322 323 324 325 326 327 328 329 330 | |
relax_model
¶
relax_model()
Build and solve the continuous relaxation of the current model.
This method creates a HiGHS model in which all integrality
constraints are relaxed to continuous domains and optimizes
it. It then clears any cached relaxed_model property
so that future accesses re-expose the freshly computed
object.
Source code in fomtools/solvers/highs.py
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | |
relaxed_is_optimal
¶
relaxed_is_optimal() -> bool
Whether the last relax_model solve ended optimal.
Source code in fomtools/solvers/highs.py
114 115 116 | |
set_objective
¶
set_objective(expression: LinearExpression, sense: str = None) -> None
Set the optimization objective for the underlying HiGHS model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expression
|
LinearExpression
|
A |
required |
sense
|
str
|
Objective sense specified as a string. |
None
|
Returns:
| Type | Description |
|---|---|
None
|
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If provided objective |
ValueError
|
If provided expression is anything other than a |
Source code in fomtools/solvers/highs.py
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 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 | |
set_param
¶
set_param(param: str, val)
Configure a solver option, passed verbatim to HiGHS.
The option name must be a native HiGHS option (e.g. time_limit,
mip_rel_gap, output_flag); no name translation is performed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
param
|
str
|
The native HiGHS option name to set. |
required |
val
|
The value to assign to the specified option. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If HiGHS rejects the option name or value. |
Source code in fomtools/solvers/highs.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | |
update_bounds
¶
update_bounds(expression: LinearExpression, lb: float | ndarray, ub: float | ndarray)
Update the lower and upper bounds of the solver variables corresponding
to the given LinearExpression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expression
|
LinearExpression
|
A lb: float or np.ndarray New lower bound(s). ub: float or np.ndarray New upper bound(s). |
required |
Source code in fomtools/solvers/highs.py
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 | |
ClarabelSolver
¶
Bases: SDPSolver
Solver wrapper for the Clarabel conic optimization solver.
This class provides an interface to solve semidefinite programs (SDPs) using the Clarabel solver. It converts the SDP problem data (equality constraints, inequality constraints, and PSD cone constraints) into Clarabel's native format and invokes the solver.
The SDP is formulated as: maximize tr(C @ X) subject to tr(Aeq[i] @ X) = beq[i] for all i tr(Aineq[j] @ X) <= bineq[j] for all j X[cone_indices] >> 0 for each PSD cone
Attributes:
| Name | Type | Description |
|---|---|---|
q |
ndarray
|
Vectorized objective matrix. |
Aeq |
csc_matrix
|
Stacked equality constraint matrices (vectorized). |
beq |
ndarray
|
Equality constraint right-hand sides. |
Aineq |
csc_matrix
|
Stacked inequality constraint matrices (vectorized). |
bineq |
ndarray
|
Inequality constraint right-hand sides. |
M |
csc_matrix
|
PSD cone selection matrices. |
cone_dims |
dict
|
Cone dimensions with keys 'zero', 'nonneg', 'psd'. |
Source code in fomtools/solvers/clarabel.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | |
__init__
¶
__init__(solver_params=None)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
solver_params
|
dict
|
Dictionary of native Clarabel settings to configure. Stored
and applied to the |
None
|
Source code in fomtools/solvers/clarabel.py
44 45 46 47 48 49 50 51 52 53 54 | |
setup_clarabel_data
¶
setup_clarabel_data(mat_problem_dim, C, Aeq_mats, beq_consts, Aineq_mats, bineq_consts, psd_cone_indices)
Convert SDP problem data to Clarabel's native format.
This method transforms the matrix-form SDP specification into the vectorized conic form expected by Clarabel. Symmetric matrices are vectorized using column-major upper triangular format with appropriate scaling for off-diagonal elements.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mat_problem_dim
|
int
|
Dimension of the symmetric matrix variable X. |
required |
C
|
csc_matrix
|
Objective matrix for maximizing tr(C @ X). |
required |
Aeq_mats
|
list[csc_matrix]
|
List of equality constraint matrices A_i for tr(A_i @ X) = b_i. |
required |
beq_consts
|
list[float]
|
Right-hand sides for equality constraints. |
required |
Aineq_mats
|
list[csc_matrix]
|
List of inequality constraint matrices for tr(A_j @ X) <= b_j. |
required |
bineq_consts
|
list[float]
|
Right-hand sides for inequality constraints. |
required |
psd_cone_indices
|
list[set]
|
List of index sets defining PSD cone constraints on submatrices. |
required |
Source code in fomtools/solvers/clarabel.py
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | |
solve
¶
solve(**solve_kwargs)
Solve the SDP and return its accepted dual upper bound.
Assembles the conic problem from the prepared data and invokes
the Clarabel solver. The problem is formulated as a minimization
in Clarabel's standard form, so the returned value is negated
to give a dual upper bound for the maximization problem.
Solved is accepted; AlmostSolved is accepted with a warning, since
the dual objective then holds only to Clarabel's reduced tolerances. Any
other termination (limits, infeasibility, numerical errors) raises.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**solve_kwargs
|
Native Clarabel settings applied verbatim to the
|
{}
|
Returns:
| Type | Description |
|---|---|
float
|
The finite dual objective at the requested numerical tolerances. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If Clarabel rejects a settings name or value. |
Source code in fomtools/solvers/clarabel.py
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | |
CuClarabelSolver
¶
Bases: ClarabelSolver
GPU-accelerated solver wrapper for Clarabel using CUDA.
This class extends ClarabelSolver to leverage GPU acceleration via CuPy and Julia's CUDA-enabled Clarabel implementation. It transfers problem data to GPU memory and solves the SDP using cuDSS for sparse linear algebra.
Requirements
- CuPy with CUDA support
- Julia with Clarabel.jl, CUDA.jl packages
- juliacall Python package
Notes
Problem setup uses the parent class's setup_clarabel_data method.
Only the solve method differs, utilizing GPU kernels.
Source code in fomtools/solvers/cuclarabel.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | |
__init__
¶
__init__(solver_params=None)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
solver_params
|
dict
|
Dictionary of solver-specific parameters to configure. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If CUDA dependencies (cupy) are not available. |
Source code in fomtools/solvers/cuclarabel.py
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | |
solve
¶
solve(**solve_kwargs)
Solve the SDP on GPU and return the objective value.
Transfers the problem data to GPU memory using CuPy and invokes Julia's CUDA-enabled Clarabel solver via juliacall. Uses cuDSS for the direct linear system solves.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**solve_kwargs
|
Accepted for API parity, but native Clarabel settings are
not yet wired into the Julia GPU path. Passing any option
raises |
{}
|
Returns:
| Type | Description |
|---|---|
float
|
The optimal objective value of the SDP maximization problem. |
Notes
Requires CUDA-capable GPU and properly configured Julia environment with CUDA.jl and Clarabel.jl packages.
Source code in fomtools/solvers/cuclarabel.py
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | |