Xopt
Bases: XoptBaseModel
Object to handle a single optimization problem.
Xopt is designed for managing a single optimization problem by unifying the definition, configuration, and execution of optimization tasks. It combines the Variables, Objective, Constraints, Statics (VOCS) definition with a generator for candidate generation and an evaluator for objective function evaluations.
Parameters
vocs : VOCS VOCS object for defining the problem's variables, objectives, constraints, and statics. generator : SerializeAsAny[Generator] An object responsible for generating candidates for optimization. evaluator : SerializeAsAny[Evaluator] An object used for evaluating candidates generated by the generator. strict : bool, optional A flag indicating whether exceptions raised during evaluation should stop the optimization process. dump_file : str, optional An optional file path for dumping attributes of the xopt object and the results of evaluations. max_evaluations : int, optional An optional maximum number of evaluations to perform. If set, the optimization process will stop after reaching this limit. data : DataFrame, optional An optional DataFrame object for storing internal data related to the optimization process. serialize_torch : bool A flag indicating whether Torch (PyTorch) models should be serialized when saving them. serialize_inline : bool A flag indicating whether Torch models should be stored via binary string directly inside the main configuration file.
Methods
step() Executes one optimization cycle, generating candidates, submitting them for evaluation, waiting for evaluation results, and updating data storage. run() Runs the optimization process until the specified stopping criteria are met, such as reaching the maximum number of evaluations. evaluate(input_dict: Dict) Evaluates a candidate without storing data. evaluate_data(input_data) Evaluates a set of candidates, adding the results to the internal DataFrame. add_data(new_data) Adds new data to the internal DataFrame and the generator's data. reset_data() Resets the internal data by clearing the DataFrame. random_evaluate(n_samples=1, seed=None, kwargs) Generates random inputs using the VOCS and evaluates them, adding the data to Xopt. yaml(kwargs) Serializes the Xopt configuration to a YAML string. dump(file: str = None, kwargs) Dumps the Xopt configuration to a specified file. dict(kwargs) -> Dict Provides a custom dictionary representation of the Xopt configuration. json(**kwargs) -> str Serializes the Xopt configuration to a JSON string.
Source code in xopt/base.py
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 |
|
__init__(*args, **kwargs)
Initialize Xopt.
Parameters
args : tuple Positional arguments; a single YAML string can be passed as the only argument to initialize Xopt. kwargs : dict Keyword arguments for initializing Xopt.
Raises
ValueError If both a YAML string and keyword arguments are specified during initialization. If more than one positional argument is provided.
Notes
- If a single YAML string is provided in the
args
argument, it is deserialized into keyword arguments usingyaml.safe_load
. - When using the YAML string for initialization, no additional keyword arguments are allowed.
Source code in xopt/base.py
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 |
|
__repr__()
Return information about the Xopt object, including the YAML representation without data.
Returns
str A string representation of the Xopt object.
Source code in xopt/base.py
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 |
|
__str__()
Return a string representation of the Xopt object.
Returns
str A string representation of the Xopt object.
Source code in xopt/base.py
567 568 569 570 571 572 573 574 575 576 577 |
|
add_data(new_data)
Concatenate new data to the internal DataFrame and add it to the generator's data.
Parameters
new_data : pd.DataFrame New data to be added to the internal DataFrame.
Source code in xopt/base.py
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 |
|
dict(**kwargs)
Handle custom dictionary generation.
Parameters
**kwargs Additional keyword arguments for customizing the dictionary generation.
Returns
Dict A dictionary representation of the Xopt configuration.
Source code in xopt/base.py
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 |
|
dump(file=None, **kwargs)
Dump data to a file.
Parameters
file : str, optional The path to the file where the Xopt configuration will be dumped. **kwargs Additional keyword arguments for customizing the dump.
Raises
ValueError
If no dump file is specified via argument or in the dump_file
attribute.
Source code in xopt/base.py
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 |
|
evaluate(input_dict)
Evaluate a candidate without storing data.
Parameters
input_dict : Dict A dictionary representing the input data for candidate evaluation.
Returns
Any The result of the evaluation.
Source code in xopt/base.py
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
|
evaluate_data(input_data)
Evaluate data using the evaluator and wait for results.
This method evaluates a set of candidates and adds the results to the internal DataFrame.
Parameters
input_data : Union[pd.DataFrame, List[Dict[str, float], Dict[str, List[float], Dict[str, float]]] The input data for evaluation, which can be provided as a DataFrame, a list of dictionaries, or a single dictionary.
Returns
pd.DataFrame The results of the evaluations added to the internal DataFrame.
Source code in xopt/base.py
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 |
|
json(**kwargs)
Handle custom serialization of generators and DataFrames.
Parameters
**kwargs Additional keyword arguments for customizing serialization.
Returns
str The Xopt configuration serialized as a JSON string.
Source code in xopt/base.py
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 |
|
random_evaluate(n_samples=None, seed=None, custom_bounds=None)
Convenience method to generate random inputs using VOCs and evaluate them.
This method generates random inputs using the Variables, Objectives, Constraints, and Statics (VOCS) and evaluates them, adding the data to the Xopt object and generator.
Parameters
n_samples : int, optional The number of random samples to generate. seed : int, optional The random seed for reproducibility. custom_bounds : dict, optional Dictionary of vocs-like ranges for random sampling
Returns
pd.DataFrame The results of the evaluations added to the internal DataFrame.
Source code in xopt/base.py
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 |
|
remove_data(indices, inplace=True)
Removes data from the X.data
data storage attribute.
Parameters
indices: list of integers List of indices specifying the rows (steps) to remove from data.
boolean, optional
Whether to update data inplace. If False, returns a copy.
Returns
pd.DataFrame or None A copy of the internal DataFrame with the specified rows removed or None if inplace is True.
Source code in xopt/base.py
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 |
|
reset_data()
Reset the internal data by clearing the DataFrame.
Source code in xopt/base.py
371 372 373 374 375 376 377 |
|
run()
Run until the maximum number of evaluations is reached or the generator is done.
Source code in xopt/base.py
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
|
step()
Run one optimization cycle.
This method performs the following steps: - Determines the number of candidates to request from the generator. - Passes the candidate request to the generator. - Submits candidates to the evaluator. - Waits until all evaluations are finished - Updates data storage and generator data storage (if applicable).
Source code in xopt/base.py
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
|
yaml(**kwargs)
Serialize the Xopt configuration to a YAML string.
Parameters
**kwargs Additional keyword arguments for customizing serialization.
Returns
str The Xopt configuration serialized as a YAML string.
Source code in xopt/base.py
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 |
|