Evaluator¶
Evaluator
¶
Evaluator(
env: list[VectorEnv] | list[VecEnv] | list[Env] | Env,
log_dir: Path | str | PathLike | None = "logs",
log_subdir: str = "eval",
log_level: int = INFO,
*,
colorize_logs: bool = False,
)
Evaluates a policy on registered Property instances.
The evaluator repeatedly rolls out a policy on one or more (vectorized) environments, feeds each
resulting trajectory to every registered property, and updates the property's statistical
confidence interval. Evaluation continues until all properties have converged (when
stop_on_convergence=True) or a resource limit (episode_limit / time_limit) is reached.
Typical usage:
evaluator = Evaluator(env=envs)
evaluator.register_property(my_property)
results = evaluator.eval(agent=agent, stop_on_convergence=True)
Attributes:
| Name | Type | Description |
|---|---|---|
envs |
list[Any]
|
The (list of) vectorized environments used for evaluation. |
time_passed |
float
|
Wall-clock seconds spent in the most recent evaluation. |
log_dir |
Path | None
|
Directory of the current run, or |
properties |
list[Property]
|
List of registered |
Create an evaluator over one or more environments.
The environment(s) may be a single Gymnasium environment, a single vectorized environment,
or a list of vectorized environments (one per worker thread). Both Gymnasium
(gym.vector.VectorEnv) and Stable-Baselines3 (VecEnv) vectorized environments are
supported. A non-vectorized environment is automatically wrapped in an AsyncVectorEnv.
Use create_eval_envs to build environments in the expected
format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env
|
list[VectorEnv] | list[VecEnv] | list[Env] | Env
|
The evaluation environment(s). A single (vectorized) environment, or a list of
vectorized environments — one per thread used during |
required |
log_dir
|
Path | str | PathLike | None
|
Base directory for run logs. Each |
'logs'
|
log_subdir
|
str
|
Prefix for the per-run subdirectory (e.g. |
'eval'
|
log_level
|
int
|
Logging verbosity, as a |
INFO
|
colorize_logs
|
bool
|
Whether to colorize console log output. |
False
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the environment is not a vectorized Gymnasium or Stable-Baselines3
environment, or uses an unsupported autoreset mode (only |
Source code in pydsmc/evaluator.py
num_envs
¶
register_property
¶
register_property(property_: Property) -> None
Register a property to be evaluated on the next eval call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
property_
|
Property
|
The property to evaluate. It is assigned a fresh property ID on registration. |
required |
Source code in pydsmc/evaluator.py
register_properties
¶
register_properties(properties: Iterable[Property]) -> None
Register several properties at once.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
properties
|
Iterable[Property]
|
An iterable of properties to register. |
required |
eval
¶
eval(
agent: Any = None,
predict_fn: Callable | None = None,
episode_limit: int | None = None,
time_limit: float | None = None,
num_initial_episodes: int = 100,
num_episodes_per_policy_run: int = 50,
save_every_n_episodes: int | None = None,
*,
stop_on_convergence: bool = True,
save_full_results: bool = False,
save_full_trajectory: bool = False,
num_threads: int | None = None,
extra_log_info: Any = None,
reset_hidden_states: tuple[ndarray, ...] | None = None,
**predict_kwargs: Any,
) -> list[Property]
Evaluate the policy on all registered properties until convergence or a resource limit.
Provide the policy either as an agent exposing a predict(obs, ...) method (e.g. any
Stable-Baselines3 model) or directly as a predict_fn callable. Extra keyword arguments are
forwarded to the policy (e.g. deterministic=True).
At least one stopping criterion must be active: episode_limit, time_limit, or
stop_on_convergence=True together with at least one property that has an epsilon.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent
|
Any
|
An object with a |
None
|
predict_fn
|
Callable | None
|
A callable |
None
|
episode_limit
|
int | None
|
Maximum number of episodes to sample before stopping. |
None
|
time_limit
|
float | None
|
Maximum wall-clock time in minutes before stopping. |
None
|
num_initial_episodes
|
int
|
Number of episodes sampled in the first policy run. |
100
|
num_episodes_per_policy_run
|
int
|
Number of episodes sampled per subsequent policy run (i.e. between convergence checks). |
50
|
save_every_n_episodes
|
int | None
|
Persist intermediate results every this many episodes. |
None
|
stop_on_convergence
|
bool
|
Whether to stop once all properties with an |
True
|
save_full_results
|
bool
|
Whether to store every individual sample. |
False
|
save_full_trajectory
|
bool
|
Whether to store full trajectories to disk. Slow and disk-heavy; not recommended. |
False
|
num_threads
|
int | None
|
Number of worker threads. Defaults to the number of environments. Must not exceed the number of environments. |
None
|
extra_log_info
|
Any
|
Arbitrary extra metadata stored alongside the run settings. |
None
|
**predict_kwargs
|
Any
|
Extra keyword arguments forwarded to |
{}
|
Returns:
| Type | Description |
|---|---|
list[Property]
|
The list of registered |
Raises:
| Type | Description |
|---|---|
ValueError
|
If arguments are invalid (e.g. no stopping criterion, no registered
properties, |
TypeError
|
If neither a callable |
Source code in pydsmc/evaluator.py
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 | |
clear_properties
¶
set_log_dir
¶
Set the base directory for run logs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
log_dir
|
Path | str | PathLike | None
|
New base log directory, or |
'logs'
|