ampl_sweep
Sweep a scalar parameter across a list of values, re-solve the AMPL model for each, and collect objective and custom expression results in a single call.
Instructions
Set a scalar parameter to each value, re-solve, and collect results.
Server-side so a twenty-point study is one call rather than forty round
trips; AMPL re-presolves only what changed. Each run goes through the
same shared core as ampl_solve (_solve_once), so it gets the same
output-failure-marker scan, the same "solved AND has an objective"
requirement for ok, and the same stable objective_name/objective_value
shape - not a re-implementation that would silently reopen the holes
Task 8 closed. objective, if given, is activated with an objective <name>; statement before the first solve (exactly like ampl_solve's own
objective=) so every run actually optimizes that objective, not merely
reports its value computed at some other objective's optimum; a bogus
name is caught before any solve runs, the same way ampl_solve catches
it. Omit objective to use whichever objective AMPL reports first,
which on a model with more than one declared objective may not be the
one actually active.
A value that fails to assign (wrong parameter name, wrong type, a value
outside a declared domain) is recorded as its own failed run (ok
False, with a message) and the sweep continues through the remaining
values rather than aborting. collect names extra AMPL expressions
evaluated with ampl.get_value after each solve; each is guard-checked
exactly like a statement passed to ampl_eval, so collect cannot become
another route to shell, and none may reuse a name this tool already
uses for a run's own fields (value, ok, solve_result, ...) - doing
so would silently overwrite that field instead of adding a new one. The
overall ok is false if any run failed.
The parameter is left at whatever the LAST swept value was; this tool
never restores the value it had before the call. A later ampl_solve()
on the same session solves against that last value, not the model's
original state - re-set the parameter first (e.g. via ampl_set_data)
if that is not what is wanted.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| param | Yes | ||
| solver | No | ||
| values | Yes | ||
| collect | No | ||
| session | No | ||
| log_tail | No | ||
| objective | No |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||