AMPL Optimization Problem: To manage its excess cash over the next 12 months, a
ID: 3773456 • Letter: A
Question
AMPL Optimization Problem:
To manage its excess cash over the next 12 months, a company may purchase 1-month, 2-month or 3-month certificates of deposit from any of several different banks. The current cash on hand and amounts invested are known, while the company must estimate the cash receipts and expenditures for each month, and the returns on the different certificates.The company’s problem is to determine the best investment strategy, subject to cash requirements. (As a practical matter, the company would use the first month of the optimal solution as a guide to its current purchases, and then re-solve with updated estimates at the beginning of the next month.)
A. Form an AMPL Model for the above problem and Construct a Data file for the following:
Suppose that the company’s estimated receipts and expenses (in thousands of dollars) over the next 12 months are as follows:
month
receipt
expense
1
3200
200
2
3600
200
3
3100
400
4
1000
800
5
1000
2100
6
1000
4500
7
1200
3300
8
1200
1800
9
1200
600
10
1500
200
11
1800
200
12
1900
200
The two banks competing for the business are estimating the following rates of return for the next
12 months
CIT:
1
2
3
NBD:
1
2
3
1
0.00433
0.01067
0.01988
1
0.00425
0.01067
0.02013
2
0.00437
0.01075
0.02
2
0.00429
0.01075
0.02025
3
0.00442
0.01083
0.02013
3
0.00433
0.01083
0.02063
4
0.00446
0.01092
0.02038
4
0.00437
0.01092
0.02088
5
0.0045
0.011
0.0205
5
0.00442
0.011
0.021
6
0.00458
0.01125
0.02088
6
0.0045
0.01125
0.02138
7
0.00467
0.01142
0.02113
7
0.00458
0.01142
0.02162
8
0.00487
0.01183
0.02187
8
0.00479
0.01183
0.02212
9
0.005
0.01217
0.02237
9
0.00492
0.01217
0.02262
10
0.005
0.01217
0.0225
10
0.00492
0.01217
0.02275
11
0.00492
0.01217
0.0225
11
0.00483
0.01233
0.02275
12
0.00483
0.01217
0.02275
12
0.00475
0.0125
0.02312
month
receipt
expense
1
3200
200
2
3600
200
3
3100
400
4
1000
800
5
1000
2100
6
1000
4500
7
1200
3300
8
1200
1800
9
1200
600
10
1500
200
11
1800
200
12
1900
200
Explanation / Answer
Consider the following problem and its corresponding LP formulation: Columbia Candies Factory produces two flavors of chewing gum: mint and cinnamon. The mint flavor gum is sold for $1 per package, while the cinnamon gum is sold for $1.5 per package. The company has limited resources and can produce only one flavor of gum at a time. The rates of production are different; the company can produce 40 package of mint gum per hour but only 30 packages of cinnamon gum per hour. Considering the result of a marketing study, the company decided that at most it can sell 900 packages of cinnamon gum and 1000 packages of mint gum. If we consider that a week has 40 hours of labor and we have no storage capacity, we need to determine how many packages of mint and cinnamon gum we should produce so that the total revenue is maximized. The corresponding LP formulation is shown below:
maximize xm + xc
s.t. 1 400 · xm + 1 300 · xc 40
xc 900
xm 1000
xm 0
xc 0
Writing AMPL code
To write your code you can use any text editor (e.g. Notepad) but given that you are programming, we recommend you to use a source code editor (e.g. crimson editor). One of the advantages of source code editors is the use of syntax highlighting, which helps you catch syntax errors. There are a considerable number of free source code editors available in the Internet. It is important that you know that the use of a text editor is not mandatory; it is just a recommendation to organize and make easier your programming tasks. In fact, you can type the code directly in the AMPL command window – in that case you may not be able to save your work. However, it is a very useful tool for debugging tasks.
From LP formulation to AMPL code:
Direct formulation:
The natural approach to program this LP formulation in AMPL is entered using syntax that is very close to the algebraic expressions of the LP formulation above. To start you need to create a .mod file, for example example1.mod, in which you should write the AMPL code. In general you can divide the code in three main parts as shown below: # PART 1 DECLARATION OF VARIABLES (variables, parameters, sets etc) var x c >= 0; var x m >= 0; # PART 2 OBJECTIVE FUNCTION: name and mathematical expression maximize revenue: x m + 1.5*x c; # PART 3 CONSTRAINTS: names and corresponding mathematical expressions subject to Aval Time: (1/40)*x m+(1/30)*x c<=40; subject to Max Mint: x m<=1000; subject to Max Cinn: x c<=900;
Comments
- The symbol # indicates the start of a comment. The remainder of the line, after the #, is ignored.
- Variables must be declared using the var command.
- All lines of code must end with a semi-colon (;).
- The objective function starts with the command maximize or minimize, followed by the name of the objective function (cost, revenue, etc), followed by a colon (:) which is finally followed by the function that should be optimized, terminated by the corresponding semicolon (;).
- Each constraint or array of constraints (constraints under the same name and different index) starts with the command subject to followed by a name, followed by a colon (:) and finally followed by the corresponding logical constraint.
- Names are unique. Variables, constraints and the objective function muat have different names.
- AMPL is case sensitive. Commands must be in lower case. This also applies to name of entities, i.e a and A are different names.
Solving the problem
Approach 1: Without scripts:
If you saved your .mod file in the folder amplcml (which contains the sw.exe file and the solvers), you will not need to refer to your .mod file using its path. Assuming that this is your case, the first step in solving your problem is to load the model: variables, objective function and constraints. To load your model you should type in your AMPL command window:
model example1.mod;
On the other hand if your .mod file is in a different location you should refer to it using its actual path. For example, if you saved your .mod file in Users then you should use the following:
model C:Usersexample1.mod;
If there is a syntactical mistake in your model, AMPL will display an error message. Debugging errors in AMPL is not always an easy task, as the error messages do not necessarily provide a lot of information. After loading your model, you should type solve; to solve your problem. Once the problem is solved you can ask for variable values. In the above example we wanted to find out the number of packages of mint gum and cinnamon gum that you should produce; you could use the command shown below to display these results: display x c, x m;
Approach 2: Using Scripts (.run file)
Sometimes we need to repeatedly solve the same problem (perhaps with slight modifications). Typing lengthy sequences of commands in the AMPL command window can become tedious. You can avoid this by using a script. Scripts capture in a file a sequence of commands that can be repeatedly used by calling the script function in the AMPL command window. Returning to the example, to make use of a script you should open a new file; assume you name it example1.run. In this file you are going to write the sequence of commands that you want to perfom. For our example you will write:
#RESET
reset;
#LOAD THE MODEL
model example1.mod;
#CHANGE THE SOLVER (optional)
option solver cplex;
#SOLVE
solve;
#SHOW RESULTS
display x c,x m;
#OTHER OPTIONS TO DISPLAY
display revenue, Aval Time, Max Mint, Max Cinn;
show revenue, Aval Time, Max Mint, Max Cinn;
After you save your .run file, you just need to refer to it in the AMPL command window in order to perform all the tasks that you asked for in the .run file.
include example1.run;
Comments0
- Since you are interested in loading your model more than once (changing coefficients, after correcting some errors, etc.), you will need to periodically reset AMPL. You cannot load your model with variables, constraints, or objective function using names which already exist – “reset” allows AMPL to forget all names.
- By default the AMPL student edition uses, as solver, MINOS 5.5, but if you want to use (e.g.) CPLEX as solver, you should define CPLEX as your solver (using option solver cplex;). You will just have to perfom this task once every time that you open the AMPL command window.
- In general you could use the command display followed by the name of the entity whose value you are interested in: objective function, variables, dual variables etc.
- To display the algebraic expression of your objective function or constraints you shoud use the command show followed by the name of the objective function, or the corresponding constraint. The command expand have a similar use.
- To execute the actions indicated in the .run file you have to type the include command before the name of the .run file. The include command does not require a semicolon at the end.
Taking advantage of AMPL
Writing a mathematical programming problem with a large number of constraints (e.g. 100, 1000 ...) would be very taxing if you had to type each constraint as in the previous example. For example, suppose that instead of having just two products, Columbia Candies Factory had a very wide line of candies, perhaps 10, 100 or even larger. AMPL lets you formulate the problem in a more general form.
Given:
n = number of flavors
t = total labor hours in a week.
pi = profit per package of gum with flavor i, i = 1, . . . , n.
ri = Number of packages of gum flavor i produced per hour, i = 1, . . . , n.
mi = maximum demand for flavor i, i = 1, . . . , n.
Variables: xi = Number of package of gum flavor i to be produced, i = 1, . . . , n.
maximize Pn i=1 pi · xi
s.t. Pn i=1 ri · xi t
0 xi mi for each i = 1..n.
In order to take advantage of this general form, we will write the model using notation that avoids explicitly writing the constraint for each different flavor. Here you are going to use two different files, a .mod file and a .dat file. In the .mod file, you describe the problem in a fairly generic form, describing the objective function and constraints in such a way that you can change the number of flavors, revenue per flavor and any other data without changing this .mod file. To achieve this goal, you create a new file named example2.mod, whose contents will be
#Declaration of parameters
param n;
param t;
param p{i in 1..n};
param r{i in 1..n};
param m{i in 1..n};
#Declaration of variables
var x {i in 1..n} >=0;
#Objective Function
maximize revenues: sum {i in 1..n} p[i]*x[i] ;
#Constraints
subject to Aval Time: sum{i in 1..n} (1/r[i])*x[i];
subject to Max Flavor {i in 1..n}: x[i]<=m[i];
After doing this you should verify that you can load your model:
reset;
model example2.mod;
If AMPL reports some errors you should correct them to guarantee that your model can be loaded. The model can still have modeling problems, but at least at this moment AMPL is able to read what you wrote. After verifying that your model is correct you should create a .dat file. This file will contain all data values arising in your model. An example of .dat file, corresponding to example2.mod, is shown next:
param n := 4;# No of Flavors
param t := 40; # Total labor hour in a week
param p := 1 1 2 1.5 3 1 4 1.5; # Revenue per package flavor i
param r := 1 40 2 30 3 50 4 20; # Production rate of package flavor i
param m := 1 1000 2 900 3 500 4 800; # Maximum demand package flavor i
Assuming this file has been named example2.dat, we can load it and after that, solve the problem:
data example2.dat;
solve;
You can also creatre a .run file and include in it the above commands: load the model, load the data, solve the problem, etc. The (example2.run) file would be something like:
reset;
model example2.mod;
data example2.dat;
solve;
display x;
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.