Examples#

Many Jupyter notebooks with examples are available on the AMPL Model Colaboratory and the new book Hands-On Mathematical Optimization with AMPL in Python 🐍.

You should also check out our collection of interactive Streamlit Apps and learn how easy you can build your own apps.

AMPL Model Colaboratory is a collection of AMPL models in Jupyter Notebooks that run on platforms such as Google Colab, Kaggle, Gradient, and AWS SageMaker.

Available at: ampl.com/colab.

You can use the Christmas notebook written by ChatGPT to get started:

Open In Colab Kaggle Gradient Open In SageMaker Studio Lab
BTW: you can even ask ChatGPT to write models for you! If it makes mistakes you can ask for help in our new Discourse Forum!

The repository of notebooks MO-BOOK: Hands-On Mathematical Optimization with AMPL in Python 🐍 introduces the concepts and tools of mathematical optimization with examples from a range of disciplines.

Available at: ampl.com/mo-book.

Build and share data apps quickly with Streamlit - no front-end experience necessary.

Available at: ampl.com/streamlit.

AMPL Optimization Apps on Streamlit Cloud

Example files#

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

Example 1: First steps#

first_example.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#

options_example.py

This example shows how to:

  • get and set AMPL options

Example 3: Assign all data to a model and solve it#

diet_model.py

This example shows how to:

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

Example 4: Build an efficient frontier#

efficient_frontier.py

This example shows how to:

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

Example 5: Simple heuristic#

tracking_model.py

This example shows how to:

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