Docsity

AMPL Modeling for Linear Programming: Farmer Jones Problem - Prof. B. Krishnamoorthy, Study notes of Linear Algebra

Toggle sidebar
bg1
Math364 - March 3, 2005 1
Introduction to AMPL
Details of how to download and start up the student version of AMPL are given in http://www.ampl.com.
Download and install AMPL in your PC (if you do not have a PC, you can submit your models on-line at the
above web page, but this procedure is rather inconvenient). To solve problems using AMPL, you will also have
to download a solver (such as CPLEX). For Windows, the solver is included in the zip file that you download
(amplcml.zip). For Unix/Linux, you need to download cplex separately (details are given in the web page too).
Cplex is the preferred solver.
The first chapter from the AMPL book is available as a PDF document from the above web page. Go through
the first chapter in detail. You can also borrow the book from me for additional reference.
AMPL Basics
You need to create a model file (for example FarmerJones.mod) and a data file (FarmerJones.dat). The model
file declares the parameters, the variables, objective function, and the constraints in a symbolic fashion. All the
numbers (actual data) are provided in the data file. Note the following.
•Every declaration (of a parameter, variables, objective function or a constraint) ends with a ;(semi-colon).
This is true for both the model and the data file.
•You can specify values for a parameter in the data file only if it is already declared.
•Before solving problems, you need to specify the solver. It is a good practice to start each AMPL session
by typing option solver cplex; at the ampl: prompt.
Example: The Farmer Jones problem
Model file: FarmerJones.mod
# AMPL model file for the Farmer Jones problem
#
# The LP is
# max Z = 30 x1 + 100 x2 (total revenue)
# s.t x1 + x2 <= 7 (land available)
# 4 x1 + 10 x2 <= 40 (labor hrs)
# 10 x1 >= 30 (min corn)
# x1, x2 >= 0 (non-negativity)
set Crops; # corn, wheat
param Yield {j in Crops}; # yield per acre
param Labor {j in Crops}; # labor hrs per acre
param Price {j in Crops}; # selling price per bushel
param Min_Crop {j in Crops}; # min levels of corn and wheat (bushels)
param Max_Acres; # total land available (acres)
param Max_Hours; # total labor hrs available
var x {j in Crops} >= 0; # x[corn] = acres of corn, x[wheat] = acres of wheat
maximize total_revenue: sum {j in Crops} Price[j]*Yield[j]*x[j];
subject to land_constraint: sum {j in Crops} x[j] <= Max_Acres;
subject to labor_constraint: sum {j in Crops} Labor[j]*x[j] <= Max_Hours;
subject to min_crop_constraint {j in Crops}: Yield[j]*x[j] >= Min_Crop[j];
pf2

Partial preview of the text

Download AMPL Modeling for Linear Programming: Farmer Jones Problem - Prof. B. Krishnamoorthy and more Study notes Linear Algebra in PDF only on Docsity!

Math364 - March 3, 2005 1

Introduction to AMPL

Details of how to download and start up the student version of AMPL are given in http://www.ampl.com. Download and install AMPL in your PC (if you do not have a PC, you can submit your models on-line at the above web page, but this procedure is rather inconvenient). To solve problems using AMPL, you will also have to download a solver (such as CPLEX). For Windows, the solver is included in the zip file that you download (amplcml.zip). For Unix/Linux, you need to download cplex separately (details are given in the web page too). Cplex is the preferred solver.

The first chapter from the AMPL book is available as a PDF document from the above web page. Go through the first chapter in detail. You can also borrow the book from me for additional reference.

AMPL Basics

You need to create a model file (for example FarmerJones.mod) and a data file (FarmerJones.dat). The model file declares the parameters, the variables, objective function, and the constraints in a symbolic fashion. All the numbers (actual data) are provided in the data file. Note the following.

  • Every declaration (of a parameter, variables, objective function or a constraint) ends with a ; (semi-colon). This is true for both the model and the data file.
  • You can specify values for a parameter in the data file only if it is already declared.
  • Before solving problems, you need to specify the solver. It is a good practice to start each AMPL session by typing option solver cplex; at the ampl: prompt.

Example: The Farmer Jones problem

Model file: FarmerJones.mod

AMPL model file for the Farmer Jones problem

The LP is

max Z = 30 x1 + 100 x2 (total revenue)

s.t x1 + x2 <= 7 (land available)

4 x1 + 10 x2 <= 40 (labor hrs)

10 x1 >= 30 (min corn)

x1, x2 >= 0 (non-negativity)

set Crops; # corn, wheat param Yield {j in Crops}; # yield per acre param Labor {j in Crops}; # labor hrs per acre param Price {j in Crops}; # selling price per bushel param Min_Crop {j in Crops}; # min levels of corn and wheat (bushels) param Max_Acres; # total land available (acres) param Max_Hours; # total labor hrs available

var x {j in Crops} >= 0; # x[corn] = acres of corn, x[wheat] = acres of wheat

maximize total_revenue: sum {j in Crops} Price[j]Yield[j]x[j];

subject to land_constraint: sum {j in Crops} x[j] <= Max_Acres; subject to labor_constraint: sum {j in Crops} Labor[j]x[j] <= Max_Hours; subject to min_crop_constraint {j in Crops}: Yield[j]x[j] >= Min_Crop[j];

Math364 - March 3, 2005 2

Data file: FarmerJones.dat

set Crops := corn wheat;

param Yield := corn 10 wheat 25;

param Labor := corn 4 wheat 10;

param Price := corn 3 wheat 4;

param Min_Crop := corn 30 wheat 0; # No minimum level specified for wheat

param Max_Acres := 7; param Max_Hours := 40;

Output from AMPL session

sw: ampl ampl: option solver cplex; ampl: model FarmerJones.mod; data FarmerJones.dat; ampl: expand land_constraint; subject to land_constraint: x[’corn’] + x[’wheat’] <= 7;

ampl: expand min_crop_constraint; subject to min_crop_constraint[’corn’]: 10*x[’corn’] >= 30;

subject to min_crop_constraint[’wheat’]: 25*x[’wheat’] >= 0;

ampl: solve; CPLEX 8.0.0: optimal solution; objective 370 1 dual simplex iterations (1 in phase I) ampl: display x; x [*] := corn 3 wheat 2. ;

ampl: display total_revenue; total_revenue = 370

ampl: