Model
class¶
Represents a mathematical programming model.
The Model
class serves as a versatile tool for creating and managing mathematical programming models.
As a factory, the Model
class provides methods to create optimization objects, decision variables,
and constraints, allowing users to build their models step by step.
The class also offers various accessors and iterators to efficiently navigate and manipulate the
modeling objects within the model. Additionally, the Model
class manages solving operations.
Source code in pyorlib/model/model.py
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 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 |
|
Attributes¶
name
property
¶
Retrieves the name of the model.
RETURNS | DESCRIPTION |
---|---|
str
|
The name assigned to the model. |
dimensions
property
¶
Retrieves the dimensions and their sizes of the model.
RETURNS | DESCRIPTION |
---|---|
Mapping[str, int]
|
A dictionary with dimension names as keys and their sizes as values. |
constraints
property
¶
constraints: List[Element]
Retrieves the constraints defined in the model.
RETURNS | DESCRIPTION |
---|---|
List[Element]
|
A list containing the constraints. |
terms
property
¶
terms: Mapping[str, Term]
Retrieves a dictionary of individual terms used in the model.
RETURNS | DESCRIPTION |
---|---|
Mapping[str, Term]
|
A dictionary where the keys represent the names of the terms and the values represent the terms themselves. |
term_sets
property
¶
term_sets: Mapping[str, Mapping[Tuple[int, ...], Term]]
Retrieves a dictionary of term sets used in the model.
RETURNS | DESCRIPTION |
---|---|
Mapping[str, Mapping[Tuple[int, ...], Term]]
|
A dictionary where the keys represent the names of the sets and the values represent the sets themselves. Each set of terms is a dictionary with the indices of the terms as keys and the terms themselves as values. |
objective_value
property
¶
Retrieves the value of the objective function in the model.
RETURNS | DESCRIPTION |
---|---|
float | None
|
The value of the objective function, or |
objective_expr
property
¶
objective_expr: Element | None
Retrieves the expression of the objective function in the model, if available.
RETURNS | DESCRIPTION |
---|---|
Element | None
|
The objective function expression, or |
solution_status
property
¶
solution_status: SolutionStatus
Retrieves an enumeration that represents the state of the solution.
RETURNS | DESCRIPTION |
---|---|
SolutionStatus
|
An enumeration that represents the state of the solution. |
float_precision
instance-attribute
property
writable
¶
float_precision: int = float_precision
This property is used to get or set the float precision of the model.
The float_precision
is an integer number of digits, used in printing the solution and objective.
RETURNS | DESCRIPTION |
---|---|
int
|
The current float precision of the solver. |
Functions¶
__init__
¶
__init__(engine: Engine, name: str | None = None, debug: bool = False, float_precision: int = 6)
Initializes a new instance of the Model
class.
PARAMETER | DESCRIPTION |
---|---|
engine |
The engine interface to be used for solving the model.
TYPE:
|
name |
An optional name for the model. Defaults to None.
TYPE:
|
debug |
A flag indicating whether debug mode is enabled. Defaults to False.
TYPE:
|
float_precision |
The number of digits used in printing the solution and objective. Defaults to 6.
TYPE:
|
Source code in pyorlib/model/model.py
get_dimension_by_name
¶
Retrieves the size of a dimension in the model based on its name.
PARAMETER | DESCRIPTION |
---|---|
name |
The name of the dimension.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
int
|
The size of the dimension. Returns 0 if the dimension does not exist. |
Source code in pyorlib/model/model.py
get_term_by_name
¶
get_term_by_name(name: str) -> Term | None
Retrieves a term from the model based on its name.
PARAMETER | DESCRIPTION |
---|---|
name |
The name of the term.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Term | None
|
The term with the specified name. Returns |
Source code in pyorlib/model/model.py
get_term_set_by_name
¶
get_term_set_by_name(name: str) -> Mapping[Tuple[int, ...], Term] | None
Retrieves a set of terms from the model based on its name.
PARAMETER | DESCRIPTION |
---|---|
name |
The name of the set.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Mapping[Tuple[int, ...], Term] | None
|
The set of terms with the specified name. Returns |
Source code in pyorlib/model/model.py
add_dimension
¶
Adds a new dimension to the model.
PARAMETER | DESCRIPTION |
---|---|
name |
The name of the dimension to be added.
TYPE:
|
value |
The size of the new dimension.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
int
|
The dimension that was added to the model. |
Source code in pyorlib/model/model.py
add_constant
¶
Adds a new constant to the model.
PARAMETER | DESCRIPTION |
---|---|
name |
The name of the constant to be added.
TYPE:
|
value_type |
The type of the constant value.
TYPE:
|
value |
The constant value.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Constant
|
The constant that was added to the model. |
Source code in pyorlib/model/model.py
add_variable
¶
add_variable(name: str, value_type: ValueType, lower_bound: float = 0, upper_bound: float = inf) -> Variable
Adds a new variable to the model.
PARAMETER | DESCRIPTION |
---|---|
name |
The name of the variable to be added.
TYPE:
|
value_type |
The type of the variable values.
TYPE:
|
lower_bound |
The lower bound of the variable. Default is 0.
TYPE:
|
upper_bound |
The upper bound of the variable. Default is infinity.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Variable
|
The variable that was added to the model. |
Source code in pyorlib/model/model.py
add_constant_to_set
¶
add_constant_to_set(set_name: str, set_index: Tuple[int, ...], const_name: str, value_type: ValueType, value: float) -> Constant
Adds a new constant to the model within a set.
PARAMETER | DESCRIPTION |
---|---|
set_name |
The name of the set where the constant will be added.
TYPE:
|
set_index |
The position of a term within a set representing its indices.
TYPE:
|
const_name |
The name of the constant to be saved.
TYPE:
|
value_type |
The type of the constant value.
TYPE:
|
value |
The constant value.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Constant
|
The constant that was added to the model. |
Source code in pyorlib/model/model.py
add_variable_to_set
¶
add_variable_to_set(set_name: str, set_index: Tuple[int, ...], var_name: str, value_type: ValueType, lower_bound: float = 0, upper_bound: float = inf) -> Variable
Adds a new variable to the model within a set.
PARAMETER | DESCRIPTION |
---|---|
set_name |
The name of the set where the variable will be added.
TYPE:
|
set_index |
The position of a term within a set that represents its indices.
TYPE:
|
var_name |
The name of the variable to be added.
TYPE:
|
value_type |
The type of the variable values.
TYPE:
|
lower_bound |
The lower bound of the variable. Default is 0.
TYPE:
|
upper_bound |
The upper bound of the variable. Default is infinity.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Variable
|
The variable that was added to the model. |
Source code in pyorlib/model/model.py
add_constraint
¶
Adds a new constraint to the model.
PARAMETER | DESCRIPTION |
---|---|
expression |
The constraint expression
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Element
|
An object representing the constraint. |
Source code in pyorlib/model/model.py
set_objective
¶
set_objective(opt_type: OptimizationType, expression: Element) -> Element
Defines the objective function.
PARAMETER | DESCRIPTION |
---|---|
opt_type |
The type of optimization to be performed.
TYPE:
|
expression |
The objective expression.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Element
|
The objective function. |
Source code in pyorlib/model/model.py
solve
¶
Solves the optimization problem represented by the model.
RETURNS | DESCRIPTION |
---|---|
None
|
None. |
Source code in pyorlib/model/model.py
print_info
¶
Prints information about the model.
PARAMETER | DESCRIPTION |
---|---|
display_term_sets |
Whether to display information about term sets. Defaults to False.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None
|
None. |
Source code in pyorlib/model/model.py
print_solution
¶
Prints the solution of the optimization problem.
RETURNS | DESCRIPTION |
---|---|
None
|
None. |