Using AMPL Studio


Db connection
AMPL commands
Script & Tracing
AMPL and Studio



 A Step-By-Step Walk Through Example


Now you know the basics of AMPL studio. Now we will go through the steps involved in solving a simple real world problem of National Insurance Associate’s (NIA) investment problem using AMPL studio. Before we open the AMPL studio the problem needs to be analysed and translated into mathematical notation, and then into an AMPL model. The following steps go into detail.


A Simple Real World Problem

National Insurance Associates carries an investment portfolio of stocks, bonds and other investment alternatives. Currently £200,000 of funds is available and must be considered for new investment opportunities.  The four stock options National is considering and the relevant financial data are as follows:







Price per Share





Annual rate of return





Risk measure per £ invested





Table:  Financial Data


The risk measure indicates the relative uncertainty associated with the stock in terms of it realising the projected annual return: higher values indicate greater risk.


National’s top management has stipulated the following investment guidelines


1.     The annual rate of return for the portfolio must be 9% 

2.     No one stock can account for more than 50% of the total sterling investment

They request you to find the investment decisions.

Formulating the Problem into Mathematical Form

In this problem we need to find the number of stocks A, B, C and D need to be bought with the provided guidelines and with minimum risk.

Now this problem needs to be presented in the mathematical form. This will involve three steps

(1)   Formulate an LP that minimises risk


(2)   Identifying the Decision Variables

The decision that National faces is to decide how much of each stock to buy.


Let     x1 be the number of shares of stock A bought

                             x2 be the number of shares of stock B bought

                             x3 be the number of shares of stock C bought

                             x4 be the number of shares of stock D bought


(3)   Determine the values of these four variables in order to minimise  

National’s risk


Identify the Objective Function

In our example we wish to minimise risk.  We risk £0.10 on each pound invested in stock A, similarly for stock B the risk is 0.07 per pound, for stock C it is 0.05, and for stock D the corresponding risk is 0.08. 

Thus if we buy x1 shares of stock A, we have a risk exposure of 0.10*100*x1 since each share costs £100.  Similarly, if we buy x2 shares of stock B we risk 0.07*50*x2, while for stocks C and D the risk measures are 0.05*80*x2 and 0.10*40*x2.  Therefore this leads to the following quantity that we wish to minimise

          Risk =0.10*100 x1  + 0.07*50 x2 + 0.05*80 x1  + 0.10*40 x2


Identifying the Constraints

The first constraint concerns the budget.  That is we can’t invest more than the money we have available. This leads to the following constraint

          100* x1 + 50* x2 + 80* x3 + 40* x4  200000


The second constraint concerns the rate of return of the portfolio and is as follows

          100*0.12* x1 + 50*0.08* x2 + 80*0.06* x3 + 40*0.10* x4  200000*.09


Finally, the cash investment in any one stock cannot exceed 50% of the total investment

          100*x1           100000 

          50*x2   100000 

          80*x3   100000 

          40*x4   100000  and x1  0, x2  0,  x3  0 , x4  0 


Translating the Mathematical Problem into AMPL Model

AMPL is mainly an algebraic language.  That means it follows the algebraic syntax used in the mathematical representation of the problems. AMPL’s main keyword declarations are set, param, var and maximize/minimize

Since AMPL deal with plain text files the above problem can be rewrite as the following AMPL model as follows. Where x1, x2, x3 and x4 are replaced with the most suitable variable names StockA, StockB, StockC and StockD.


Risk = 10*StockA + 3.5*StockB + 4*StockC + 4*StockD



StockA  1000 

            StockB  2000 

            StockC  1250 

      StockD  2500


Subject to

                   100*StockA + 50*StockB + 80*StockC + 40*StockD  200000

            12*StockA + 4*StockB + 4.8*StockC + 4*StockD  18000



Using AMPL Studio to Solve the Problem.

Now the AMPL model is ready for the problem. Now you open the AMPL studio.


Create Workspace

In order to create a new workspace for NIA’s problem create a new workspace with the following steps.


Step 1:        Choose New Workspace from the File menu.




Step 2:        Write workspace name as NIA and choose your appropriate folder (C:\) by clicking the ellipsis (…) button where you want to create your workspace.



Click OK to create the workspace at your chosen folder.



Create a Project

Step 1:                 Having created a workspace, we now define a new project by selecting Insert New Project from the Project menu.




Step 2:                 Enter the Project name as “StepByStep1” and choose your preferred folder by clicking the ellipsis (…) button.


                             Check the R Add templates checkbox and write the Model name as “StepByStep1.mod” and the Data instance as “StepByStep1.dat”.




Clicking OK will create a new project with the model and data template files within the created workspace.







Create an AMPL Model file



Double clicking on the model file will open the model template file. The lines with # at the beginning are comment lines. The AMPL key words will be in blue and the numbers in red.


Step 1:        Now write your AMPL model in this window.



Step 2:        To check the syntax of your model choose Build Model menu from Build menu.


                   Build Menu



If any syntax errors occurred then the appropriate error messages will be displayed in the Console Window. In the above model displays the following syntax error.


Step 3:        Double click on the error line (line 30) will display the following screen.



Step 4: The line has two errors.

1.     Risk = should be replaced by Risk :

2.     Semicolon is missing at the end of line.



Step 5: Make these corrections and compiling it again will show the following.




Solve and Display Results

Step 1:        In order to solve the model you need to select the solver. By default you will receive FortMP solver with your AMPL studio distribution. FortMP is a powerful solver and capable to handle this simple problem.




Step 2:        Now you can run this problem by choosing the Solve Problem menu from the Build Menu.



Immediately the problem will be solved and the results will be displayed in the Editing Area.




Enhance to Data Separated project


Creating Data and Model Files

The following is the investment problem exploiting structure.





Solve and Display Results







Contents Overview Example Db connection AMPL commands Script & Tracing AMPL and Studio Viewlet


Copyright (c) 2012. Datumatic Ltd. Registration No. 04988675. UK.