Function structure#

AMPL C API Documentation

Overview#

The AMPL C API allows programs to interact with the AMPL interpreter, access input and output, and manipulate mathematical optimization models. All API functions start with the prefix AMPL_.

The main object in the API is the AMPL struct. One instance of this struct represents an execution of the AMPL translator. You must create it before interacting with the API. The AMPL struct lets you send AMPL commands, inspect and modify models, retrieve results, and control execution.

Since the AMPL struct manages important resources, you should explicitly close it with AMPL_Close() when you are done.

AMPL Struct#

The AMPL struct wraps the AMPL execution engine. You create it using AMPL_Create() or AMPL_CreateWithEnv(). All model creation and modification happens through this struct by sending AMPL statements. You can also access the current model state through entity-related functions.

API functions for AMPL fall into four categories:

  1. Direct interaction with AMPL

  2. Model interrogation

  3. Commands and options

  4. Output and error handling

Direct Interaction with AMPL#

Model Interrogation#

Enumerating Entities:

Accessing Known Entities: If you know an entity’s AMPL name, you can access it directly. After you obtain an entity, you can inspect or modify it, and read or assign data. Accessing an entity communicates with the AMPL engine only when needed.

Entities become invalid if:

  • A dependent entity is modified, or

  • A generic AMPL command is executed

The API automatically refreshes invalid entities, but this can be costly. Prefer API functions over raw AMPL commands to avoid unnecessary invalidation.

Commands and Options#

Some common AMPL commands have direct API functions:

  • AMPL_Solve() – solves the model

  • Snapshot and export functions

  • Table read/write functions

AMPL options can be accessed and modified using:

Use dedicated API functions whenever possible instead of AMPL_Eval().

Output and Error Handling#

Output:

Errors:

  • Translator errors (e.g., syntax errors) are handled by an error callback set with AMPL_GetErrorHandler() and AMPL_SetErrorHandler().

  • API-level errors (e.g., invalid arguments) are returned by functions directly.

  • Use AMPL_CALL() to wrap API calls that return AMPL_ERRORINFO for proper error checking.

Model Entities#

Entities represent the core model components: variables, constraints, objectives, parameters, and sets. They allow programs to access model state and assign some data.

Two base abstractions exist: AMPL_ENTITY and AMPL_INSTANCE.

Entities expose methods appropriate to their type, for example:

  • Constraints: drop, restore, dual values

  • Variables: fix, unfix, assign values

Access to Instances and Values#

  • Use AMPL_EntityGetValues() or iterators to access instances.

  • Individual instance access is slower because it checks validity and syncs with the interpreter.

  • For bulk access, use AMPL_DATAFRAME for better performance.

Scalar entities mirror instance-level methods for easier access.

DataFrames and Bulk Access#

AMPL_DATAFRAME allows efficient bulk access to data: