In a CADET-Process optimization, evaluating a candidate can mean applying a salt concentration, running a simulation, and calculating product purity. In v0.13.0, an evaluation pipeline does this work for the optimizer and for Problem.evaluate_batch(), which users call to sample points or generate surrogate training data.
An error during evaluation can mean four different things, and each should have its own outcome and today these cases are not kept apart:
- The setup is invalid. A required parameter is missing or the cycle time is not set, so the process fails its configuration check. Raise because changing the candidate cannot fix the setup.
- The candidate fails. The solver does not converge, no fraction meets the purity requirement, or the process does not reach cyclic stationarity within the maximum number of cycles. Return a failure for that point and let the batch finish. Only the optimizer substitutes
bad_metrics. Cache the failure if it’s repeatable. - The code has a bug. The purity function reads
process.slat, or a metric indexes a component the process does not have. Raise so the caller can fix it, and don’t cache it, so a rerun after the fix recomputes. - The failure may be transient. A solver times out on a busy machine. A retry may succeed, so such failures need their own retry and caching policy.
What happens to an exception today:
- The pipeline catches any exception raised by a step, whether a
CADETProcessErroror an ordinary Python exception from a bug (AttributeError,KeyError,IndexError, …), and puts anEvaluationFailurein place of the result: an object holding the original exception, the failed step and the error message. Steps that need that result are skipped, and other candidates continue. Failures from aCADETProcessErrorare cacheable by default and others are not; an exception can override this through arecoverableattribute. evaluate_batch()returns these failures to its caller without raising, whatever the original exception. It also wraps any exception raised while evaluating a candidate, such as one raised by another backend, in anEvaluationFailurethe same way. A sampler that hits a bug therefore records 200 failed points instead of one error.- The optimizer inspects the stored exception: it uses
bad_metricsfor aCADETProcessErrorand re-raises the original exception for anything else.
Because step 3 goes by type alone, other mistakes get scored too. An invalid setup and a solver timeout both raise CADETProcessError, so, following the linked code, both can produce fallback scores, and the timeout is cached. An objective that returns the wrong number of values is caught by the optimizer’s own wrapper, which raises CADETProcessError before step 1; running it gives the objective’s fallback score instead of an error, for a mistake in the user’s code.
The script below reproduces the typo through both entry points. It was run on f6e181ca.
from CADETProcess.optimization import OptimizationProblem
class Process:
name = "Batch elution"
salt = 100.0
def purity(process):
return process.slat / 1000 # typo: a bug, not a failed simulation
problem = OptimizationProblem("demo", use_diskcache=False)
problem.add_evaluation_object(Process())
problem.add_variable("salt", lb=0, ub=500)
problem.add_objective(purity, bad_metrics=[999.0])
print(problem.evaluate_batch([{"salt": 120.0}]))
problem.evaluate_objectives([120.0])
[{'purity': EvaluationFailure(stage='purity', reason="'Process' object has no attribute 'slat'", recoverable=True)}]
AttributeError: 'Process' object has no attribute 'slat'
Why this needs to change
- Mistakes look like results. Bugs that raise ordinary Python exceptions are exposed by the optimizer but hidden from batch callers. Errors reported as
CADETProcessError, such as an invalid setup or a wrong-length objective, can instead become fallback scores: the run finishes, and the user may conclude that the process cannot be simulated in that region instead of fixing one line. - The same error behaves differently depending on the entry point. The typo raises in the optimizer but not in
evaluate_batch(). Anyone who writes a sampler or a surrogate trainer has to know about the optimizer’s check and repeat it. - Caching makes wrong classifications stick. A failure from a
CADETProcessErroris cached, so a timeout or a wrong-length objective is reused as a bad candidate for that point instead of being evaluated again. - The contract is about to become public.
evaluate_batch()ships in v0.13.0 as the entry point for sampling and surrogate training, and the planned surrogate backend needs to know which exception to raise. Changing the behavior after the release would break callers that rely on it.
Two decisions before v0.13.0
-
What marks a failed candidate? An error that the code detecting it can identify as an expected outcome for that candidate. Options: keep
CADETProcessError, add a dedicated exception, or add a flag on the exception, which could build on therecoverableattribute the pipeline already reads for caching.# today: one type for both raise CADETProcessError("Process is not configured correctly.") # invalid setup raise CADETProcessError(return_information.error_message) # solver failed # with a dedicated exception (name open) raise CADETProcessError("Process is not configured correctly.") # now raises raise EvaluationError(return_information.error_message) # candidate failureI lean toward a dedicated exception, raised by the simulator, the fractionation code or a surrogate backend; the pipeline and
Problemonly check its type. A configuration check that cannot establish the cause keeps raisingCADETProcessError. Whether an expected failure may be cached stays a separate question: a timeout should not be cached just because it is expected. The pipeline’s cache rule has to switch with the type, since today it caches everyCADETProcessError, including a configuration check that would now raise. The cost is migration: every raise site reachable during evaluation that signals a candidate failure today must switch before release, or it starts raising. -
Where should bugs and setup errors raise? Leave it to callers, apply the rule in
Problemto both failures a backend returns and exceptions it raises, or let each backend propagate errors. I lean towardProblem, for bothevaluate()andevaluate_batch(), so samplers and optimizers behave the same across backends. Unexpected errors would then propagate to the caller; whether the other points of a parallel batch still finish is a separate detail. The cost: a surrogate must report an out-of-domain point explicitly; a bareValueErrorwould stop the batch.
Would you choose differently on either point? Is there a caller that needs bugs returned as batch results, or a backend that would struggle to identify expected candidate failures?