How to Use AMPL in VSCode
This post is a guide to help you set up Visual Studio Code (VSCode) to write and run AMPL code if you cannot use the AMPL IDE on your computer.
- Install VSCode
- Install AMPL
- Install the AMPL Extensions for VSCode
- Configure the AMPL Path
- Write and Run AMPL Code
Install VSCode
Visual Studio Code is a free source code editor developed by Microsoft. You can download and install it from the official website.
Install AMPL
AMPL (A Mathematical Programming Language) is a modeling language for mathematical programming.
Download and install it from the official website.
If you are a student from Les Mines Nancy, use the link available on arche to create an account with your university email (@etud.univ-lorraine.fr
) for a free student license.
Install the AMPL Extensions for VSCode
To install the AMPL extensions in VSCode, follow these steps (see the image below):
- Click the Extensions icon on the left side of the VSCode window.
- Search for “AMPL” in the search bar.
- Install the following extensions by clicking
Install
:- AMPL by Michael Sundvick – Adds a button to run AMPL files.
- AMPL for VSCode by Johan Cho – Provides syntax highlighting and definitions on hover.
Configure the AMPL Path
To use AMPL in VSCode, configure the path to the AMPL executable:
- Open the settings in VSCode using:
Ctrl + ,
(keyboard shortcut), orFile > Preferences > Settings
, or- Click the gear icon in the bottom-left corner and select
Settings
. (1 and 2 in the image below)
- Search for “AMPL” in the settings search bar. (3 in the image below)
- Set the path to the AMPL executable in the
Ampl: Executable Path
field.
Example paths:C:\ampl\ampl.exe
(Windows)/home/YOUR_PATH/amplide.linux64/ampl.linux-intel64/ampl
(Linux)
- Optionally, check the box
Ampl: Use Relative Path
to use a relative path.
Write and Run AMPL Code
Follow these steps to write and run AMPL code (if you are a student from Les Mines Nancy):
- Download the archives “AMPL examples” (under the pdf lesson on AMPL) and “AMPL data files” (in “Solutions to the exercises” section) from arche.
- Extract the archives (right-click and select
Extract Here
) and move all the files to the same folder (in a repertory calledampl_exercises
for example). - Open the folder in VSCode:
- Use the menu:
File > Open Folder
. - Alternatively, in the terminal, navigate to the folder and type
code .
- Use the menu:
- View the folder contents in the Explorer panel on the left side of the VSCode window.
- Open the files
farmer.run
andfarmer.mod
. - Arrange the tabs to view the code side by side if needed.
- In the top-right corner of the
farmer.run
file, click the buttonAMPL: Include File
to run the file with AMPL. - A terminal will open, displaying the results of the execution.
If you are not a student from Les Mines Nancy, you can follow this steps :
- Create a folder and open it in VSCode.
-
Create a new file
prod0.mod
and write the following code (from the AMPL book):# Variables var XB; var XC; # Objective Function maximize Profit: 25 *XB + 30* XC; # Constraints subject to Time: (1/200) *XB + (1/140)* XC <= 40; subject to B_limit: 0 <= XB <= 6000; subject to C_limit: 0 <= XC <= 4000;
-
Create a new file
prod0.run
and write the following code:reset; model prod0.mod; option solver gurobi; solve; display XB, XC;
- Click the button
AMPL: Include File
in the top-right corner of theprod0.run
file to run the file with AMPL. -
A terminal will open, displaying the results of the execution :
Gurobi 12.0.0: optimal solution; objective 192000 1 simplex iteration XB = 6000 XC = 1400