Stopping Conditions¶
Maximum elapsed time¶
Use MaxTimeCondition to stop after a positive, finite number of wall-clock
seconds:
from xopt.stopping_conditions import MaxTimeCondition
X.stopping_condition = MaxTimeCondition(max_time=60.0)
X.run()
The timer starts when should_stop() is first called, normally at the start of
run(), and uses a monotonic clock. Time before that first check is not counted.
Conditions are checked between optimization steps, so an
in-progress evaluation is allowed to finish and the time limit may be exceeded.
The condition also works without evaluation data and can be nested inside a
CompositeCondition.
For YAML configuration:
stopping_condition:
name: MaxTimeCondition
max_time: 60.0
Only the time limit is serialized. Loading a saved configuration resets the timer; the full time budget starts at the first check.
Stopping conditions for Xopt optimization.
This module contains classes that implement various stopping criteria for optimization processes. Each stopping condition class takes an Xopt dataframe and VOCS object to determine whether optimization should stop.
CompositeCondition
¶
Bases: StoppingCondition
Combine multiple stopping conditions with AND/OR logic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
conditions
|
List[StoppingCondition]
|
List of stopping conditions to combine. |
required |
logic
|
str
|
Logic to combine conditions: "and" or "or" (default: "or"). |
required |
Source code in xopt/stopping_conditions.py
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 | |
should_stop(data, vocs)
¶
Combine conditions using specified logic.
Source code in xopt/stopping_conditions.py
372 373 374 375 376 377 378 379 380 381 382 383 384 | |
yaml(**kwargs)
¶
serialize first then dump to yaml string
Source code in xopt/pydantic.py
295 296 297 298 299 300 301 302 | |
ConvergenceCondition
¶
Bases: StoppingCondition
Stop when optimization converges (improvement is below threshold).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
objective_name
|
str
|
Name of the objective to monitor for convergence. |
required |
improvement_threshold
|
float
|
Minimum improvement required to continue optimization. |
required |
patience
|
int
|
Number of evaluations to wait without improvement before stopping. |
required |
relative
|
bool
|
Whether to use relative improvement (default: False). |
required |
Source code in xopt/stopping_conditions.py
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 | |
should_stop(data, vocs)
¶
Stop if no improvement for specified patience.
Source code in xopt/stopping_conditions.py
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 | |
yaml(**kwargs)
¶
serialize first then dump to yaml string
Source code in xopt/pydantic.py
295 296 297 298 299 300 301 302 | |
FeasibilityCondition
¶
Bases: StoppingCondition
Stop when a feasible solution is found.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
require_all_constraints
|
bool
|
Whether all constraints must be satisfied (default: True). |
required |
Source code in xopt/stopping_conditions.py
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 | |
should_stop(data, vocs)
¶
Stop when a feasible solution is found.
Source code in xopt/stopping_conditions.py
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 | |
yaml(**kwargs)
¶
serialize first then dump to yaml string
Source code in xopt/pydantic.py
295 296 297 298 299 300 301 302 | |
MaxEvaluationsCondition
¶
Bases: StoppingCondition
Stop after a maximum number of evaluations. Evaluations can be counted in different ways based on parameters. If count_valid_only is True, only evaluations without errors are counted. If use_dataframe_index is True, the dataframe index is used to count evaluations instead of the number of rows.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_evaluations
|
int
|
Maximum number of function evaluations before stopping. |
required |
count_valid_only
|
bool
|
Whether to count only valid evaluations (default: False). |
required |
use_dataframe_index
|
bool
|
Whether to use the dataframe index to count evaluations (default: False). |
required |
Source code in xopt/stopping_conditions.py
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 | |
should_stop(data, vocs)
¶
Stop if we've reached the maximum number of evaluations.
Source code in xopt/stopping_conditions.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 | |
yaml(**kwargs)
¶
serialize first then dump to yaml string
Source code in xopt/pydantic.py
295 296 297 298 299 300 301 302 | |
MaxTimeCondition
¶
Bases: StoppingCondition
Stop after a maximum elapsed wall-clock time, starting at the first check.
The timer uses a datetime clock and is checked between optimization steps; running evaluations are not interrupted. Serializing and deserializing the condition preserves the timer's start time.
Note: Set start_time to None to reset the timer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_time
|
float
|
Maximum elapsed time in seconds. Must be positive and finite. |
required |
Source code in xopt/stopping_conditions.py
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 | |
should_stop(data, vocs)
¶
Stop when elapsed time reaches the limit, regardless of data.
Source code in xopt/stopping_conditions.py
133 134 135 136 137 138 | |
yaml(**kwargs)
¶
serialize first then dump to yaml string
Source code in xopt/pydantic.py
295 296 297 298 299 300 301 302 | |
StagnationCondition
¶
Bases: StoppingCondition
Stop when the best objective value hasn't improved for a number of evaluations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
objective_name
|
str
|
Name of the objective to monitor. |
required |
patience
|
int
|
Number of evaluations without improvement before stopping. |
required |
tolerance
|
float
|
Minimum improvement considered significant (default: 1e-8). |
required |
Source code in xopt/stopping_conditions.py
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 | |
should_stop(data, vocs)
¶
Stop if no improvement in best value for specified patience.
Source code in xopt/stopping_conditions.py
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 | |
yaml(**kwargs)
¶
serialize first then dump to yaml string
Source code in xopt/pydantic.py
295 296 297 298 299 300 301 302 | |
StoppingCondition
¶
Bases: XoptBaseModel, ABC
Abstract base class for stopping conditions.
All stopping conditions must implement the should_stop method which takes an Xopt dataframe and VOCS object and returns True if optimization should stop.
Source code in xopt/stopping_conditions.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 | |
should_stop(data, vocs)
abstractmethod
¶
Determine if optimization should stop based on data and VOCS.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
The Xopt optimization data containing variables, objectives, constraints, etc. |
required |
vocs
|
VOCS
|
The VOCS object defining variables, objectives, and constraints. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if optimization should stop, False otherwise. |
Source code in xopt/stopping_conditions.py
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | |
yaml(**kwargs)
¶
serialize first then dump to yaml string
Source code in xopt/pydantic.py
295 296 297 298 299 300 301 302 | |
TargetValueCondition
¶
Bases: StoppingCondition
Stop when an objective reaches a target value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
objective_name
|
str
|
Name of the objective to monitor. |
required |
target_value
|
float
|
Target value for the objective. |
required |
tolerance
|
float
|
Tolerance for reaching the target (default: 1e-6). |
required |
Source code in xopt/stopping_conditions.py
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 | |
should_stop(data, vocs)
¶
Stop if objective reaches target value within tolerance.
Source code in xopt/stopping_conditions.py
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | |
yaml(**kwargs)
¶
serialize first then dump to yaml string
Source code in xopt/pydantic.py
295 296 297 298 299 300 301 302 | |