the gnu linear programming kit, part 1_ introduction to linear optimization

13
4/17/13 The GNU Linear Programming Kit, Part 1: Introduction to linear optimization www.ibm.com/developerworks/linux/library/l-glpk1/ 1/13 Sign in (or register) English Technical topics Evaluation software Community Events The GNU Linear Programming Kit, Part 1: Introduction to linear optimization Find the best solutions to complex numeric problems Rodrigo Ceron ([email protected] ), Staff Software Engineer, IBM, Software Group Summary: The GNU Linear Programming Kit is a powerful, proven tool for solving numeric problems with multiple constraints. This article introduces GLPK, the glpsol client utility, and the GNU MathProg language to solve the problem of optimizing the operations for Giapetto's Woodcarving, Inc., a fictional toy manufacturer. Date: 08 Aug 2006 Level: Intermediate Also available in: Russian Japanese Portuguese Activity: 65860 views Comments: 5(View | Add comment Sign in) Average rating (226 votes) Rate this article Introduction "Linear programming is a tool for solving optimization problems. In 1947, George Dantzig developed an efficient method, the simplex algorithm, for solving linear programming problems. Since the development of the simplex algorithm, linear programming has been used to solve optimization problems in industries as diverse as banking, education, forestry, petroleum, and trucking. In a survey of Fortune 500 firms, 85% of the respondents said they had used linear programming." From Operations Research: Applications and Algorithms, 4th Edition, by Wayne L. Winston (Thomson, 2004); see Resources below for a link. Many tools are available to solve linear programming problems. The proprietary tools are well known, but many members of the open source community may not know about the free GLPK tool. The first in a series of three articles that show GLPK's capabilities and usage, this article briefly describes GLPK and then demonstrates and applies the GNU MathProg Language in GLPK. If you are just starting with operations research theory and want to learn how to model and solve linear problems, this article is a good guide. The GNU Linear Programming Kit The GNU Linear Programming Kit (GLPK) is a library of routines that use wellknown operations research algorithms to solve linear problems. The routines implement the simplex, branch and bound, primaldual interior point, and many other algorithms. Check the GLPK manual included with the GLPK download to find out more. (To download the GLPK, see the Resources section for a link to the GLPK page on gnu.org.) GLPK is not a program it can't be run and has no main() function. Instead, clients feed the problem data to the algorithmic routines through the GLPK API and receive results back. GLPK has a default client, the glpsol program, that interfaces with this API. Usually, a program like glpsol is called a solver rather than a client, so you'll see this nomenclature from here forward. The GNU MathProg modeling language The GNU MathProg modeling language is nice and simple for declaring linear problems. In general, a problem declaration consists of: Problem decision variables. An objective (target) function. Note that objective is a noun, not an adjective. The name is standard in operations research theory. Problem constraints. Problem parameters (data).

Upload: manifold

Post on 05-Dec-2015

61 views

Category:

Documents


11 download

DESCRIPTION

linear programming

TRANSCRIPT

Page 1: The GNU Linear Programming Kit, Part 1_ Introduction to Linear Optimization

4/17/13 The GNU Linear Programming Kit, Part 1: Introduction to linear optimization

www.ibm.com/developerworks/linux/library/l-glpk1/ 1/13

Sign in (or register)English

Technical topics Evaluation software Community Events

The GNU Linear Programming Kit, Part 1: Introduction to linearoptimizationFind the best solutions to complex numeric problems

Rodrigo Ceron ([email protected]), Staff Software Engineer, IBM, Software Group

Summary: The GNU Linear Programming Kit is a powerful, proven tool for solving numeric problems with multiple constraints. This article introduces GLPK,the glpsol client utility, and the GNU MathProg language to solve the problem of optimizing the operations for Giapetto's Woodcarving, Inc., a fictional toymanufacturer.

Date: 08 Aug 2006 Level: Intermediate Also available in: Russian Japanese Portuguese

Activity: 65860 views Comments: 5 (View | Add comment ­ Sign in)

Average rating (226 votes)Rate this article

Introduction

"Linear programming is a tool for solving optimization problems. In 1947, George Dantzig developed an efficient method, the simplex algorithm, forsolving linear programming problems. Since the development of the simplex algorithm, linear programming has been used to solve optimizationproblems in industries as diverse as banking, education, forestry, petroleum, and trucking. In a survey of Fortune 500 firms, 85% of the respondentssaid they had used linear programming."

From Operations Research: Applications and Algorithms, 4th Edition, by Wayne L. Winston (Thomson, 2004); see Resources below for a link.

Many tools are available to solve linear programming problems. The proprietary tools are well known, but many members of the open source community may notknow about the free GLPK tool.

The first in a series of three articles that show GLPK's capabilities and usage, this article briefly describes GLPK and then demonstrates and applies the GNUMathProg Language in GLPK.

If you are just starting with operations research theory and want to learn how to model and solve linear problems, this article is a good guide.

The GNU Linear Programming Kit

The GNU Linear Programming Kit (GLPK) is a library of routines that use well­known operations research algorithms to solve linear problems. The routinesimplement the simplex, branch and bound, primal­dual interior point, and many other algorithms. Check the GLPK manual included with the GLPK download tofind out more. (To download the GLPK, see the Resources section for a link to the GLPK page on gnu.org.)

GLPK is not a program ­­ it can't be run and has no main() function. Instead, clients feed the problem data to the algorithmic routines through the GLPK APIand receive results back. GLPK has a default client, the glpsol program, that interfaces with this API. Usually, a program like glpsol is called a solver rather than aclient, so you'll see this nomenclature from here forward.

The GNU MathProg modeling language

The GNU MathProg modeling language is nice and simple for declaring linear problems. In general, a problem declaration consists of:

Problem decision variables.An objective (target) function. Note that objective is a noun, not an adjective. The name is standard in operations research theory.Problem constraints.Problem parameters (data).

Page 2: The GNU Linear Programming Kit, Part 1_ Introduction to Linear Optimization

4/17/13 The GNU Linear Programming Kit, Part 1: Introduction to linear optimization

www.ibm.com/developerworks/linux/library/l-glpk1/ 2/13

Let's start with a simple two­variable example: Giapetto's Woodcarving, Inc.

Giapetto's Woodcarving Inc.

This problem is from Operations Research:

Giapetto's Woodcarving Inc. manufactures two types of wooden toys: soldiers and trains. A soldier sells for $27 and uses $10 worth of raw materials.Each soldier that is manufactured increases Giapetto's variable labor and overhead costs by $14. A train sells for $21 and uses $9 worth of rawmaterials. Each train built increases Giapetto's variable labor and overhead costs by $10. The manufacture of wooden soldiers and trains requires twotypes of skilled labor: carpentry and finishing. A soldier requires 2 hours of finishing labor and 1 hour of carpentry labor. A train requires 1 hour offinishing and 1 hour of carpentry labor. Each week, Giapetto can obtain all the needed raw material but only 100 finishing hours and 80 carpentryhours. Demand for trains is unlimited, but at most 40 soldier are bought each week. Giapetto wants to maximize weekly profits (revenues ­ costs).

To summarize the important information and assumptions about this problem:

1. There are two types of wooden toys: soldiers and trains.2. A soldier sells for $27, uses $10 worth of raw materials, and increases variable labor and overhead costs by $14.3. A train sells for $21, uses $9 worth of raw materials, and increases variable labor and overhead costs by $10.4. A soldier requires 2 hours of finishing labor and 1 hour of carpentry labor.5. A train requires 1 hour of finishing labor and 1 hour of carpentry labor.6. At most, 100 finishing hours and 80 carpentry hours are available weekly.7. The weekly demand for trains is unlimited, while, at most, 40 soldiers will be sold.

The goal is to find the numbers of soldiers and trains that will maximize the weekly profit.

Modeling

To model a linear problem, the decision variables are established first, since they will change with each iteration of the simplex algorithm and determine the value ofthe objective function and, hence, the optimal solution. In Giapetto's shop, the objective function is the profit, which is a function of the amount of soldiers andtrains produced each week. Therefore, the two decision variables in this problem are:

x1: Number of soldiers produced each weekx2: Number of trains produced each week

Once the decision variables are known, the objective function of this problem is simply the revenue minus the costs for each toy, as a function of x1 and x2.

Note that the profit depends linearly on x1 and x2 ­­ this is a linear problem.

It may seem at first glance that the profit can be maximized by simply increasing x1 and x2. Well, if life were that easy, let's start manufacturing trains and soldiersand move to the Caribbean! Unfortunately, there are restrictions that limit the decision variables that may be selected (or else the model is very likely to be wrong).

Recall the assumptions made for this problem. The first three determined the decision variables and the objective function. The fourth and sixth assumption say thatfinishing the soldiers requires time for carpentry and finishing. The limitation here is that Giapetto doesn't have infinite carpentry and finishing hours. That's aconstraint! Let's analyze it to clarify.

One soldier requires 2 hours of finishing labor, and Giapetto has at most 100 hours of finishing labor per week, so he can't produce more than 50 soldiers perweek. Similarly, the carpentry hours constraint makes it impossible to produce more than 80 soldiers weekly. Note here that the first constraint is stricter than thesecond. The first constraint is effectively a subset of the second, thus the second constraint is redundant.

The previous paragraph shows how to model optimization problems, but it's an incomplete analysis because all the necessary variables were not considered. It'snot the complete solution of the Giapetto problem. So how should the problem be approached?

Start by analyzing the limiting factors first in order to find the constraints. First, what constrains the finishing hours? Since both soldiers and trains require finishingtime, both need to be taken into account. Suppose that 10 soldiers and 20 trains were built. The amount of finishing hours needed for that would be 10 times 2hours (for soldiers) plus 20 times 1 hour (for trains), for a total of 40 hours of finishing labor. The general constraint in terms of the decision variables is:

Page 3: The GNU Linear Programming Kit, Part 1_ Introduction to Linear Optimization

4/17/13 The GNU Linear Programming Kit, Part 1: Introduction to linear optimization

www.ibm.com/developerworks/linux/library/l-glpk1/ 3/13

There are many (x1,x2) pairs that satisfy this inequality, so this does not determine the best combination for Giapetto's shop.

Now that the constraint for the finishing hours is ready, the carpentry hours constraint is found in the same way to be:

Great! There's only one more constraint for this problem. Remember the weekly demand for soldiers? According to the problem description, there can be at most40 soldiers produced each week:

The demand for trains is unlimited, so no constraint can be written for it. The model is finished and consists of the equations:

Note the last constraint. It ensures that the values of the decision variables will always be positive. The problem does not state this explicitly, but it's still important(and obvious).

Now GLPK can solve the model (since GLPK is good at solving linear optimization problems).

A little bit of theory

Let's check the problem's solution space. With two decision variables, it has two dimensions.

Figure 1. Giapetto's unbounded universe

The (x1,x2) solutions outside the first quadrant (where all values are positive) have already been discarded. Note, however, that this solution space is still infinite(that would be a situation in which I'd move to the Caribbean!)

As the constraints were written, this unlimited solution space gained boundaries. With inequality 6, above, the result is more interesting.

Page 4: The GNU Linear Programming Kit, Part 1_ Introduction to Linear Optimization

4/17/13 The GNU Linear Programming Kit, Part 1: Introduction to linear optimization

www.ibm.com/developerworks/linux/library/l-glpk1/ 4/13

Figure 2. Giapetto's universe considering the finishing constraint

The solution space contains all the possible (x1,x2) solutions in the first quadrant that satisfy the finishing hours constraint.

After inequality 7, the result set shrinks.

Figure 3. Giapetto's universe considering the finishing and carpentry constraints

Note that the solution space is smaller. This means that even fewer (x1,x2) solutions are in it. After inequality 8, the result is even smaller.

Figure 4. Giapetto's feasible region

Page 5: The GNU Linear Programming Kit, Part 1_ Introduction to Linear Optimization

4/17/13 The GNU Linear Programming Kit, Part 1: Introduction to linear optimization

www.ibm.com/developerworks/linux/library/l-glpk1/ 5/13

The solution space gets smaller still. The solution space that satisfies all the constraints is called the feasible region. Figure 4 shows the feasible region forGiapetto's shop. Any (x1,x2) pair that falls into that region is a potential solution to the problem.

The question now is: which one maximizes Giapetto's profit?

Using GLPK to solve the model

GLPK is an excellent tool to solve that question. The mathematical formulation of Giapetto's problem needs to be written with the GNU MathProg language. Thekey items to declare are:

The decision variablesThe objective functionThe constraintsThe problem data set

The following code shows how to solve Giapetto's problem with MathProg. The line numbers in this code are not part of the code itself. They have been addedonly for the sake of making references to the code.

Listing 1. First solution to Giapetto's problem: giapetto.sol

1 # 2 # Giapetto's problem 3 # 4 # This finds the optimal solution for maximizing Giapetto's profit 5 # 6 7 /* Decision variables */ 8 var x1 >=0; /* soldier */ 9 var x2 >=0; /* train */1011 /* Objective function */12 maximize z: 3*x1 + 2*x2;1314 /* Constraints */15 s.t. Finishing : 2*x1 + x2 <= 100;16 s.t. Carpentry : x1 + x2 <= 80;17 s.t. Demand : x1 <= 40;1819 end;

Lines 1 through 5 are comments. # anywhere on a line begins a comment to the end of the line. C­style comments can also be used, as shown on line 7. They evenwork in the middle of a declaration.

The first MathProg step is to declare the decision variables. Lines 8 and 9 declare x1 and x2. A decision variable declaration begins with the keyword var. Tosimplify sign constraints (check inequality 9), MathProg allows a >= 0 constraint in the decision variable declaration, as seen on lines 8 and 9. Every sentence inGNU MathProg must end with a semicolon (;). Recall that x1 represents soldier numbers and x2 represents train numbers. These variables could have been

Page 6: The GNU Linear Programming Kit, Part 1_ Introduction to Linear Optimization

4/17/13 The GNU Linear Programming Kit, Part 1: Introduction to linear optimization

www.ibm.com/developerworks/linux/library/l-glpk1/ 6/13

called soldiers and trains, but that would confuse the mathematicians in the audience.

Line 12 declares the target (objective) function. Linear problems can be either maximized or minimized. Remember, Giapetto's mathematical model is amaximization problem, so the keyword maximize is appropriate instead of the opposite keyword, minimize. The objective function is named z and equals 3x1+ 2x2. Note that:

The colon (:) character separates the name of the objective function and its definition.The asterisk (*) character denotes multiplication and, similarly, the plus (+), minus (-), and forward slash (/) characters denote addition, subtraction, anddivision as you'd expect.

Lines 15, 16, and 17 define the constraints. Although s.t. is not required at the beginning of the line to declare a constraint, it improves the readability of thecode.

The three Giapetto constraints have been labeled Finishing, Carpentry, and Demand. Each of them is declared as in the mathematical model. The symbols <=and >= express the inequalities. Don't forget the ; at the end of each declaration.

Every GNU MathProg file must end with end;, as seen on line 19.

Now, glpsol can use this file as input. But wait a minute; where's the data section of this problem? Well, this problem is so simple that the problem data is directlyincluded in the objective function and constraints declarations as the coefficients of the decision variables in the declarations. For example, in the objective function,the coefficients 3 and 1 are part of the problem's data set. When I rewrite this problem using a data set, it will become clear how it works; for now, don't worryabout it.

It's good practice to use the .mod extension for MathProg input files and redirect the solution to a file with the extension .sol. This is not a requirement ­­ you canuse any file name and extension you like. Giapetto's MathProg file for this example will be giapetto.mod, and the output will be in giapetto.sol. Now, runglpsol in your favorite console:

glpsol -m giapetto.mod -o giapetto.sol

This command line uses two glpsol options:

The -m option tells glpsol that the input is written in GNU MathProg.The -o option tells glpsol to send its output to giapetto.sol.

The solution report will be in giapetto.sol, but some information about the time and memory GLPK consumed is shown on the system's standard output:

Listing 2. Output from glpsol

ceron@curly ~ $ glpsol -m giapetto.mod -o giapetto.solReading model section from giapetto.real.mod...19 lines were readGenerating z...Generating Finishing...Generating Carpentry...Generating Demand...Model has been successfully generatedlpx_simplex: original LP has 4 rows, 2 columns, 7 non-zeroslpx_simplex: presolved LP has 2 rows, 2 columns, 4 non-zeroslpx_adv_basis: size of triangular part = 2* 0: objval = 0.000000000e+00 infeas = 0.000000000e+00 (0)* 2: objval = 1.400000000e+02 infeas = 0.000000000e+00 (0)OPTIMAL SOLUTION FOUNDTime used: 0.0 secsMemory used: 0.1M (151326 bytes)lpx_print_sol: writing LP problem solution to giapetto.sol'...

The report shows that glpsol reads the model, calls a GLPK API function to generate the objective function, then calls another API function to generate theconstraints. After the model has been generated, glpsol explains briefly how the problem was handled internally by GLPK. At the end, there's information about thesolution and the resources used by GLPK to solve it, and the solution is noted to be optimal.

Great, but what are the actual optimal values for the decision variables? They are in the giapetto.sol file:

Listing 3. The solution to Giapetto's problem: giapetto.sol

Problem: giapettoRows: 4Columns: 2Non-zeros: 7

Page 7: The GNU Linear Programming Kit, Part 1_ Introduction to Linear Optimization

4/17/13 The GNU Linear Programming Kit, Part 1: Introduction to linear optimization

www.ibm.com/developerworks/linux/library/l-glpk1/ 7/13

Status: OPTIMALObjective: z = 180 (MAXimum)

No. Row name St Activity Lower bound Upper bound Marginal------ ------------ -- ------------- ------------- ------------- ------------- 1 z B 180 2 Finishing NU 100 100 1 3 Carpentry NU 80 80 1 4 Demand B 20 40

No. Column name St Activity Lower bound Upper bound Marginal------ ------------ -- ------------- ------------- ------------- ------------- 1 x1 B 20 0 2 x2 B 60 0

Karush-Kuhn-Tucker optimality conditions:

KKT.PE: max.abs.err. = 0.00e+00 on row 0 max.rel.err. = 0.00e+00 on row 0 High quality

KKT.PB: max.abs.err. = 0.00e+00 on row 0 max.rel.err. = 0.00e+00 on row 0 High quality

KKT.DE: max.abs.err. = 0.00e+00 on column 0 max.rel.err. = 0.00e+00 on column 0 High quality

KKT.DB: max.abs.err. = 0.00e+00 on row 0 max.rel.err. = 0.00e+00 on row 0 High quality

End of output

The solution is divided into four sections:

Information about the problem and the optimal value of the objective functionPrecise information about the status of the objective function and about the constraintsPrecise information about the optimal values for the decision variablesInformation about the optimality conditions, if any

For this particular problem, we see that the solution is OPTIMAL and that Giapetto's maximum weekly profit is $180.

The Finishing constraint's status is NU (the St column). NU means that there's a non­basic variable on the upper bound for that constraint. If you know someoperation research theory, build the simplex tableau and check it out. If you don't, here's a a brief practical explanation.

Whenever a constraint reaches its upper or lower boundary, it's called a bounded constraint. A bounded constraint prevents the objective function from reachinga better value. Think of it as a volume knob, for example, that can't be turned any further. When that occurs, glpsol shows the status of the constraint as either NUor NL (for upper and lower boundary respectively), and it also shows the value of the marginal, also known as the shadow price. The marginal is the value bywhich the objective function would improve if the constraint were relaxed by one unit (if the volume knob could turn a little more). Note that the improvementdepends on whether the goal is to minimize or maximize the target function. For instance, in Giapetto's problem, which seeks maximization, the marginal value 1means that the objective function would increase by 1 if we could have one more hour of finishing labor (we know it's one more hour and not one less, becausethe finishing hours constraint is an upper boundary).

The carpentry and soldier demand constraints are similar. For the carpentry constraint, note that it's also an upper boundary. Therefore, a relaxation of one unit inthat constraint (an increment of one hour) would make the objective function's optimal value become better by the marginal value 1 and become 181.

The soldier demand, however, is not bounded, thus its state is B, and a relaxation in it will not change the objective function's optimal value.

Try relaxing the value of each bounded constraint one at a time, solve the modified problem, and see what happens to the optimal value of the objective function.Also check that changing the value of unbounded constraints won't make any difference to the solution, as expected.

Finally, glpsol's report shows the values for the decision variables: x1 = 20 and x2 = 60. This tells Giapetto that he should produce 20 soldiers and 60 trains tomaximize his weekly profit.

Giapetto's problem was very small. You may be wondering, in a problem with many more decision variables and constraints, would you have to declare eachvariable and each constraint separately? And what if you wanted to adjust the data of the problem, such as the selling price of a soldier? Do you have to makechanges everywhere this value appears? The next section discusses that.

Using model and data sections in Giapetto's problem

Page 8: The GNU Linear Programming Kit, Part 1_ Introduction to Linear Optimization

4/17/13 The GNU Linear Programming Kit, Part 1: Introduction to linear optimization

www.ibm.com/developerworks/linux/library/l-glpk1/ 8/13

MathProg models normally have a model section and a data section, sometimes in two different files. Thus, glpsol can solve a model with different data sets easily,to check what the solution would be with this new data. The following listing states Giapetto's problem in a much more elegant way:

Listing 4. Giapetto's problem with a model and a data section: giapetto2.mod

1 # 2 # Giapetto's problem 3 # 4 # This finds the optimal solution for maximizing Giapetto's profit 5 # 6 7 /* Set of toys */ 8 set TOY; 910 /* Parameters */11 param Finishing_hours i in TOY;12 param Carpentry_hours i in TOY;13 param Demand_toys i in TOY;14 param Profit_toys i in TOY;1516 /* Decision variables */17 var x i in TOY >=0;1819 /* Objective function */20 maximize z: sumi in TOY Profit_toys[i]*x[i];2122 /* Constraints */23 s.t. Fin_hours : sumi in TOY Finishing_hours[i]*x[i] <= 100;24 s.t. Carp_hours : sumi in TOY Carpentry_hours[i]*x[i] <= 80;25 s.t. Dem i in TOY : x[i] <= Demand_toys[i];262728 data;29 /* data section */3031 set TOY := soldier train;3233 param Finishing_hours:=34 soldier 235 train 1;3637 param Carpentry_hours:=38 soldier 139 train 1;4041 param Demand_toys:=42 soldier 4043 train 6.02E+23;4445 param Profit_toys:=46 soldier 347 train 2;4849 end;

Rather than two separate files, the problem is stated in a single file with a modeling section (lines 1 through 27) and a data section (lines 28 through 49).

Line 8 defines a SET. A SET is a universe of elements. For example, if I declare mathematically xi, for all i in 1;2;3;4, I'm saying that x is an array thatranges from 1 to 4, and therefore we have x1, x2, x3, x4. In this case, 1;2;3;4 is the set. So, in Giapetto's problem, there's a set called TOY. Where are theactual values of this set? In the data section of the file. Check line 31. It defines the TOY set to contain soldier and train. Wow, the set is not a numerical range.How can that be? GLPK handles this in an interesting way. You'll see how to use this in a few moments. For now, get used to the syntax for SET declarations inthe data section:

set label := value1 value2 ... valueN ;

Lines 11, 12, and 13 define the parameters of the problem. There are three: Finishing_hours, Carpentry_hours, and Demand_toys. These parametersmake up the problem's data matrix and are used to calculate the constraints, as you'll see later.

Take the Finishing_hours parameter as an example. It's defined on the TOY set, so each kind of toy in the TOY set will have a value for Finishing_hours.Remember that each soldier requires 2 hours of finishing work, and each train requires 1 hour of finishing work. Check lines 33, 34, and 35 now. There is thedefinition of the finishing hours for each kind of toy. Essentially, those lines declare that Finishing_hours[soldier]=2 and thatFinishing_hours[train]=1. Finishing_hours is, therefore, a matrix with 1 row and 2 columns.

Carpentry hours and demand parameters are declared similarly. Note that the demand for trains is unlimited, so a very large value is the upper bound on line 43.

Page 9: The GNU Linear Programming Kit, Part 1_ Introduction to Linear Optimization

4/17/13 The GNU Linear Programming Kit, Part 1: Introduction to linear optimization

www.ibm.com/developerworks/linux/library/l-glpk1/ 9/13

Does that value seem fa­mole­iar to you?

Line 17 declares a variable, x, for every i in TOY (resulting in x[soldier] and x[train]), and constrains them to be greater than or equal to zero. Once youhave sets, it's pretty easy to declare variables, isn't it?

Line 20 declares the objective (target) function as the maximization of z, which is the total profit for every kind of toy (there are two: trains and soldiers). Withsoldiers, for example, the profit is the number of soldiers times the profit per soldier.

The constraints on lines 23, 24, and 25 are declared in a similar way. Take the finishing hours constraint as an example: it's the total of the finishing hours per kindof toy, times the number of that kind of toy produced, for the two types of toys (trains and soldiers), and it must be less than or equal to 100. Similarly, the totalcarpentry hours must be less than or equal to 80.

The demand constraint is a little bit different than the previous two, because it's defined for each kind of toy, not as a total for all toy types. Therefore, we need twoof them, one for trains and one for soldiers, as you can see on line 25. Note that the index variable ( i in TOY ) comes before the :. This tells GLPK to createa constraint for each toy type in TOY, and the equation that will rule each constraint will be what comes after the :. In this case, GLPK will create

Dem[soldier] : x[soldier] <= Demand[soldier]

Dem[train] : x[train] <= Demand[train]

Solving this new model must yield the same results:

Listing 5. The solution to Giapetto's problem with a data section: giapetto2.sol

Problem: giapetto2Rows: 5Columns: 2Non-zeros: 8Status: OPTIMALObjective: z = 180 (MAXimum)

No. Row name St Activity Lower bound Upper bound Marginal------ ------------ -- ------------- ------------- ------------- ------------- 1 z B 180 2 Fin_hours NU 100 100 1 3 Carp_hours NU 80 80 1 4 Dem[soldier] B 20 40 5 Dem[train] B 60 6.02e+23

No. Column name St Activity Lower bound Upper bound Marginal------ ------------ -- ------------- ------------- ------------- ------------- 1 x[soldier] B 20 0 2 x[train] B 60 0

Karush-Kuhn-Tucker optimality conditions:

KKT.PE: max.abs.err. = 0.00e+00 on row 0 max.rel.err. = 0.00e+00 on row 0 High quality

KKT.PB: max.abs.err. = 0.00e+00 on row 0 max.rel.err. = 0.00e+00 on row 0 High quality

KKT.DE: max.abs.err. = 0.00e+00 on column 0 max.rel.err. = 0.00e+00 on column 0 High quality

KKT.DB: max.abs.err. = 0.00e+00 on row 0 max.rel.err. = 0.00e+00 on row 0 High quality

End of output

Note how the constraints and the decision variables are now named after the TOY set, which looks clean and organized. Very good. You have maximizedGiapetto's profit!

Conclusion

You've seen how to formulate a simple, two­variable linear problem. Then you saw how to use a simple MathProg program to solve it using sets, parameters,constraints, decision variables, and an objective (target) function. The program used summation over sets and a parameters data section. Finally, you learned how

Page 10: The GNU Linear Programming Kit, Part 1_ Introduction to Linear Optimization

4/17/13 The GNU Linear Programming Kit, Part 1: Introduction to linear optimization

www.ibm.com/developerworks/linux/library/l-glpk1/ 10/13

to interpret the results of a maximization problem.

The next installment in this three­article series will show you how to make the most out of a bad diet.

Download

Description Name Size Download methodSolutions to the problem solutions.zip 1KB HTTP

Information about download methods

Resources

Learn

The problems in this article are taken with permission from Operations Research: Applications and Algorithms, 4th Edition, by Wayne L. Winston(Thomson, 2004).

The online documentation for GLPK gives more information about GLPK, how to get the software, and how to join the GLPK community.

Check out the Wikipedia entry for GLPK.

Subscribe to the GLPK help mailing list or bug reports mailing list.

In the developerWorks Linux zone, find more resources for Linux developers.

Stay current with developerWorks technical events and Webcasts.

Get products and technologies

With IBM trial software, available for download directly from developerWorks, build your next development project on Linux.

Discuss

Check out developerWorks blogs and get involved in the developerWorks community.

About the author

Rodrigo Ceron Ferreira de Castro is a Staff Software Engineer at the IBM Linux Technology Center. He graduated from the State University of Campinas(UNICAMP) in 2004. He received the State Engineering Institute prize and the Engineering Council Certification of Honor when he graduated. He's givenspeeches in open source conferences in Brazil and other countries.

Close [x]

developerWorks: Sign inIBM ID:Need an IBM ID?Forgot your IBM ID?

Password:Forgot your password?

Page 11: The GNU Linear Programming Kit, Part 1_ Introduction to Linear Optimization

4/17/13 The GNU Linear Programming Kit, Part 1: Introduction to linear optimization

www.ibm.com/developerworks/linux/library/l-glpk1/ 11/13

Change your password

Keep me signed in.

By clicking Submit, you agree to the developerWorks terms of use.

Submit Cancel

The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, butyou may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that youpost.

All information submitted is secure.

Close [x]

Choose your display nameThe first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content youpost on developerWorks.

Please choose a display name between 3­31 characters. Your display name must be unique in the developerWorks community and should not be your emailaddress for privacy reasons.

Display name: (Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.

Submit Cancel

All information submitted is secure.

Average rating (226 votes)

1 star 1 star2 stars 2 stars3 stars 3 stars4 stars 4 stars5 stars 5 stars

Submit

Add comment:

Sign in or register to leave a comment.

Note: HTML elements are not supported within comments.

Notify me when a comment is added1000 characters left

Post

Total comments (5)

Page 12: The GNU Linear Programming Kit, Part 1_ Introduction to Linear Optimization

4/17/13 The GNU Linear Programming Kit, Part 1: Introduction to linear optimization

www.ibm.com/developerworks/linux/library/l-glpk1/ 12/13

@FJ2F would certainly be a viable solution, albeit not an open one. There can be compelling reasons why one might want to start with an open product. There arealso good business justifications for using a commercial one. I always recommend that people explore all sides of a technology and choose one that fits them. Thegreat thing about the open option is that it makes technology available to everyone who wants to learn about it, with no trial period.(editor)

Posted by cmw.osdude on 19 June 2012

Report abuse

you may want to try CPLEX Studio with OPL (IBM product)... it does all what you talk about... and a bit more... :­)

Posted by FJ2F on 18 June 2012

Report abuse

JoseDelaCruz, thanks for the post and for alerting us to the bracket problem. We fixed the display issue, but please let us know if there are any other errors. (Thesyntax highlighting was introduced automatically by our software.)

Tom YoungdeveloperWorks

Posted by tomyoung on 01 April 2010

Report abuse

ATTENTION: the example above misses some square brackets. I suppose that they are a way to indicate the editor it shoud add a link. So, in the declarations ofia, ja and ar don't forget to put the missing square brakets. Regards.

Posted by JoseDelaCruz on 31 March 2010

Report abuse

<pre class="jive­pre"><code class="jive­code jive­java"><font color="darkgreen">// This would be the c program that solves the problem using the api.</font> #include <stdio.h>#include <stdlib.h>#include <glpk.h> <font color="navy"><b>int</b></font> main(<font color="navy"><b>void</b></font>)<font color="navy"></font> glp_prob lp;<font color="navy"><b>int</b></font> ia[1+1000], ja[1+1000];<font color="navy"><b>double</b></font> ar[1+1000], z, x1, x2;lp = glp_create_prob();glp_set_prob_name(lp, <font color="red">"Carpinteria"</font>);glp_set_obj_dir(lp, GLP_MAX);glp_add_rows(lp, 3);glp_set_row_name(lp, 1, <font color="red">"f"</font>);glp_set_row_bnds(lp, 1, GLP_UP, 0.0, 100.0);glp_set_row_name(lp, 2, <font color="red">"c"</font>);glp_set_row_bnds(lp, 2, GLP_UP, 0.0, 80.0);glp_set_row_name(lp, 3, <font color="red">"r"</font>);glp_set_row_bnds(lp, 3, GLP_UP, 0.0, 40.0);glp_add_cols(lp, 2);glp_set_col_name(lp, 1, <font color="red">"x1"</font>);glp_set_col_bnds(lp, 1, GLP_LO, 0.0, 0.0);glp_set_obj_coef(lp, 1, 3.0);glp_set_col_name(lp, 2, <font color="red">"x2"</font>);glp_set_col_bnds(lp, 2, GLP_LO, 0.0, 0.0);glp_set_obj_coef(lp, 2, 2.0); ia[1] = 1, ja[1] = 1, ar[1] = 2.0; <font color="darkgreen">/* a[1,1] = 2 */</font>ia[2] = 1, ja[2] = 2, ar[2] = 1.0; <font color="darkgreen">/* a[1,2] = 1 */</font>ia[3] = 2, ja[3] = 1, ar[3] = 1.0; <font color="darkgreen">/* a[2,1] = 1 */</font>

Page 13: The GNU Linear Programming Kit, Part 1_ Introduction to Linear Optimization

4/17/13 The GNU Linear Programming Kit, Part 1: Introduction to linear optimization

www.ibm.com/developerworks/linux/library/l-glpk1/ 13/13

Print this page Share this page Follow developerWorks

About

Help

Contact us

Submit content

Feeds Report abuse

Terms of use

IBM privacy

IBM accessibility

Faculty

Students

Business Partners

ia[4] = 2, ja[4] = 2, ar[4] = 1.0; <font color="darkgreen">/* a[2,2] = 1 */</font>ia[5] = 3, ja[5] = 1, ar[5] = 1.0; <font color="darkgreen">/* a[3,1] = 1 */</font>ia[6] = 3, ja[6] = 2, ar[6] = 0.0; <font color="darkgreen">/* a[3,2] = 0 */</font> glp_load_matrix(lp, 6, ia, ja, ar); glp_simplex(lp, NULL );z = glp_get_obj_val(lp);x1 = glp_get_col_prim(lp, 1);x2 = glp_get_col_prim(lp, 2); printf(<font color="red">"z = %g; x1 = %g; x2 = %g; "</font>,z, x1, x2);glp_delete_prob(lp);<font color="navy"><b>return</b></font> 0;<font color="navy"></font></code>

Edited by: admin on Apr 1, 2010 10:59 AM

Posted by JoseDelaCruz on 31 March 2010

Report abuse

https://www.ibm.com/developerworks/community/report?lang=en&referingURL=http://www.ibm.com/developerworks/linux/library/l-glpk1/&mymessage=Comment:%20%26lt%3Bpre%20class%3D%22jive-pre%22%26gt%3B%26lt%3Bcode%20class%3D%22jive-code%20jive-java%22%26gt%3B%26lt%3Bfont%20color%3D%22darkgreen%22%26gt%3B%2F%2F%20This%20would%20be%20the%20c%20program%20that%20solves%20the%20problem%20using%20the%20api.%26lt%3B%2Ffont%26gt%3B%26nbsp%3B%26nbsp%3B%23include%20%26lt%3Bstdio.h%26gt%3B%23include%20%26lt%3Bstdlib.h%26gt%3B%23include%20%26lt%3Bglpk.h%26gt%3B%26nbsp%3B%26lt%3Bfont%20color%3D%22navy%22%26gt%3B%26lt%3Bb%26gt%3Bint%26lt%3B%2Fb%26gt%3B%26lt%3B%2Ffont%26gt%3B%20main(%26lt%3Bfont%20color%3D%22navy%22%26gt%3B%26lt%3Bb%26gt%3Bvoid%26lt%3B%2Fb%26gt%3B%26lt%3B%2Ffont%26gt%3B)%26lt%3Bfont%20color%3D%22navy%22%26gt%3B%7B%26lt%3B%2Ffont%26gt%3B%20glp_prob%20lp%3B%20%26lt%3Bfont%20color%3D%22navy%22%26gt%3B%26lt%3Bb%26gt%3Bint%26lt%3B%2Fb%26gt%3B%26lt%3B%2Ffont%26gt%3B%20ia%5B1%2B1000%5D%2C%20ja%5B1%2B1000%5D%3B%20%26lt%3Bfont%20color%3D%22navy%22%26gt%3B%26lt%3Bb%26gt%3Bdouble%26lt%3B%2Fb%26gt%3B%26lt%3B%2Ffont%26gt%3B%20ar%5B1%2B1000%5D%2C%20z%2C%20x1%2C%20x2%3B%20lp%20%3D%20glp_create_prob()%3B%20glp_set_prob_name(lp%2C%20%26lt%3Bfont%20color%3D%22red%22%26gt%3B%26quot%3BCarpinteria%26quot%3B%26lt%3B%2Ffont%26gt%3B)%3B%20glp_set_obj_dir(lp%2C%20GLP_MAX)%3B%20glp_add_rows(lp%2C%203)%3B%20glp_set_row_name(lp%2C%201%2C%20%26lt%3Bfont%20color%3D%22red%22%26gt%3B%26quot%3Bf%26quot%3B%26lt%3B%2Ffont%26gt%3B)%3B%20glp_set_row_bnds(lp%2C%201%2C%20GLP_UP%2C%200.0%2C%20100.0)%3B%20glp_set_row_name(lp%2C%202%2C%20%26lt%3Bfont%20color%3D%22red%22%26gt%3B%26quot%3Bc%26quot%3B%26lt%3B%2Ffont%26gt%3B)%3B%20glp_set_row_bnds(lp%2C%202%2C%20GLP_UP%2C%200.0%2C%2080.0)%3B%20glp_set_row_name(lp%2C%203%2C%20%26lt%3Bfont%20color%3D%22red%22%26gt%3B%26quot%3Br%26quot%3B%26lt%3B%2Ffont%26gt%3B)%3B%20glp_set_row_bnds(lp%2C%203%2C%20GLP_UP%2C%200.0%2C%2040.0)%3B%20glp_add_cols(lp%2C%202)%3B%20glp_set_col_name(lp%2C%201%2C%20%26lt%3Bfont%20color%3D%22red%22%26gt%3B%26quot%3Bx1%26quot%3B%26lt%3B%2Ffont%26gt%3B)%3B%20glp_set_col_bnds(lp%2C%201%2C%20GLP_LO%2C%200.0%2C%200.0)%3B%20glp_set_obj_coef(lp%2C%201%2C%203.0)%3B%20glp_set_col_name(lp%2C%202%2C%20%26lt%3Bfont%20color%3D%22red%22%26gt%3B%26quot%3Bx2%26quot%3B%26lt%3B%2Ffont%26gt%3B)%3B%20glp_set_col_bnds(lp%2C%202%2C%20GLP_LO%2C%200.0%2C%200.0)%3B%20glp_set_obj_coef(lp%2C%202%2C%202.0)%3B%26nbsp%3B%20ia%5B1%5D%20%3D%201%2C%20ja%5B1%5D%20%3D%201%2C%20ar%5B1%5D%20%3D%202.0%3B%20%26lt%3Bfont%20color%3D%22darkgreen%22%26gt%3B%2F*%20a%5B1%2C1%5D%20%3D%202%20*%2F%26lt%3B%2Ffont%26gt%3B%20ia%5B2%5D%20%3D%201%2C%20ja%5B2%5D%20%3D%202%2C%20ar%5B2%5D%20%3D%201.0%3B%20%26lt%3Bfont%20color%3D%22darkgreen%22%26gt%3B%2F*%20a%5B1%2C2%5D%20%3D%201%20*%2F%26lt%3B%2Ffont%26gt%3B%20ia%5B3%5D%20%3D%202%2C%20ja%5B3%5D%20%3D%201%2C%20ar%5B3%5D%20%3D%201.0%3B%20%26lt%3Bfont%20color%3D%22darkgreen%22%26gt%3B%2F*%20a%5B2%2C1%5D%20%3D%201%20*%2F%26lt%3B%2Ffont%26gt%3B%20ia%5B4%5D%20%3D%202%2C%20ja%5B4%5D%20%3D%202%2C%20ar%5B4%5D%20%3D%201.0%3B%20%26lt%3Bfont%20color%3D%22darkgreen%22%26gt%3B%2F*%20a%5B2%2C2%5D%20%3D%201%20*%2F%26lt%3B%2Ffont%26gt%3B%20ia%5B5%5D%20%3D%203%2C%20ja%5B5%5D%20%3D%201%2C%20ar%5B5%5D%20%3D%201.0%3B%20%26lt%3Bfont%20color%3D%22darkgreen%22%26gt%3B%2F*%20a%5B3%2C1%5D%20%3D%201%20*%2F%26lt%3B%2Ffont%26gt%3B%20ia%5B6%5D%20%3D%203%2C%20ja%5B6%5D%20%3D%202%2C%20ar%5B6%5D%20%3D%200.0%3B%20%26lt%3Bfont%20color%3D%22darkgreen%22%26gt%3B%2F*%20a%5B3%2C2%5D%20%3D%200%20*%2F%26lt%3B%2Ffont%26gt%3B%26nbsp%3B%20glp_load_matrix(lp%2C%206%2C%20ia%2C%20ja%2C%20ar)%3B%26nbsp%3B%20glp_simplex(lp%2C%20NULL%20)%3B%20z%20%3D%20glp_get_obj_val(lp)%3B%20x1%20%3D%20glp_get_col_prim(lp%2C%201)%3B%20x2%20%3D%20glp_get_col_prim(lp%2C%202)%3B%26nbsp%3B%20printf(%26lt%3Bfont%20color%3D%22red%22%26gt%3B%26quot%3Bz%20%3D%20%25g%3B%20x1%20%3D%20%25g%3B%20x2%20%3D%20%25g%3B%20%26quot%3B%26lt%3B%2Ffont%26gt%3B%2C%20z%2C%20x1%2C%20x2)%3B%09%20glp_delete_prob(lp)%3B%20%26lt%3Bfont%20color%3D%22navy%22%26gt%3B%26lt%3Bb%26gt%3Breturn%26lt%3B%2Fb%26gt%3B%26lt%3B%2Ffont%26gt%3B%200%3B%26lt%3Bfont%20color%3D%22navy%22%26gt%3B%7D%26lt%3B%2Ffont%26gt%3B%26lt%3B%2Fcode%26gt%3BEdited%20by%3A%20admin%20on%20Apr%201%2C%202010%2010%3A59%20AM%0DPosted%20by%20JoseDelaCruz%20on%2031%20March%202010%0D%0D---%20Add%20any%20comments%20below%20this%20line%20---