Python API examples

This section lists a few examples in Python. These are the same files that can be found in the examples directory of the distribution, and show the basic usage of the Python API.

Example 1: First steps

firstexample.py

This example shows how to

  • read an AMPL model
  • reassign values to parameters
  • solve the model
  • display the objective function value

Example 2: Get and set AMPL options

optionsexample.py

This example shows how to:

  • get and set AMPL options

Example 3: Use AMPL asynchronously

asyncexample.py

This example shows how to:

  • set a callback for AMPL async operation (implementing amplpy.Runnable)
  • start and interrupt async operations

Example 4: Get and set data through DataFrames

dataframeexample.py

This example shows how to:

Example 5: Operate with multidimensional data

multidimensionalexample.py

This example shows how to:

Example 6: Assign all data to a model and solve it

dietmodel.py

This example shows how to:

  • Assign all the data necessary to generate a model instance programmatically

Example 7: Build an efficient frontier

efficientfrontier.py

This example shows how to:

  • build an efficient frontier by repeteatly solve a portfolio problem in AMPL

Example 8: Simple heuristic

trackingmodel.py

This example shows how to:

  • Do a simple heuristics for solving a QMIP problem, using the relaxed solution as a hint