simple linear programming example 4 – protein consumption [GLPK]

This example was found on linkedIn SlideShare Page 9:

As part of your weight training regimen, you want to
consume lean sources of protein. You want to consume
at least 300 Calories a day from at least 48
grams of protein
. One ounce of chicken provides 35 Calories
and 8.5 g of protein
. One ounce of tofu provides 20 Calories
and 2.5 g protein
. Your local supermarket charges $5 a pound
for chicken and $2.5 a pound for tofu. How much of each food
should you eat each day if you want to meet your requierments
with the lowest cost?
What is this daily cost?

c = number of lbs of chicken
t = number of lbs of tofu

optimisation equation: P(Price Paid)
P = 5 * c + 2.5 * t

subject to: (16 ounces = 1 lb)
c >= 0
t >= 0
560 * c + 320 * t >= 300 //1 lb chicken = 16 * 35 = 560 calories
//1 lb tofu = 16 * 20 = 320 calories
136 * c + 40 * t >= 48 //1 lb chicken = 16 * 8.5 = 136 g of protein
//1 lb chicken = 16 * 2.5 = 40 g of protein

Also written as:
p = 560 * c + 320 * t
q = 136 * c + 40 * t

300 <= p <= +inf
0 <= x1 < +inf

48 <= q <= +inf
0 <= x2 < +inf

//  Philipp Siedler
//  Linear Programming
//  Real Life Example 4

#include <stdio.h>
#include <stdlib.h>
#include <glpk.h>
#include <std_lib_facilities.h>

int main(void)
{
	glp_prob *lp;
	int row_index[1 + 1000];					//Row indices of each element
	int	col_index[1 + 1000];					//column indices of each element
	double value[1 + 1000];						//numerical values of corresponding elements
	double z, x1, x2, x3;
	lp = glp_create_prob();						//creates a problem object
	glp_set_prob_name(lp, "sample");			//assigns a symbolic name to the problem object
	glp_set_obj_dir(lp, GLP_MIN);				//calls the routine glp_set_obj_dir to set the
												//omptimization direction flag,
												//where GLP_MAX means maximization

	//ROWS
	glp_add_rows(lp, 2);						//adds three rows to the problem object
	//row 1
	glp_set_row_name(lp, 1, "p");				//assigns name p to first row
	glp_set_row_bnds(lp, 1, GLP_LO, 300.0, INFINITY);	//sets the type and bounds of the first row,
												//where GLP_LO means that the row has an lower bound.
												//300 <= p <= +inf	
	//row 2
	glp_set_row_name(lp, 2, "q");				//assigns name q to second row
	glp_set_row_bnds(lp, 2, GLP_LO, 48.0, INFINITY);//48 <= q <= +inf	

	//COLUMNS
	glp_add_cols(lp, 2);						//adds three columns to the problem object
	//column 1
	glp_set_col_name(lp, 1, "x1");				//assigns name x1 to first column
	glp_set_col_bnds(lp, 1, GLP_LO, 0.0, 0.0);	//sets the type and bounds to the first column,
												//where GLP_LO means that the column has an lower bound
	glp_set_obj_coef(lp, 1, 5.0);				//sets the objective coefficient for thr first column
												//P = 5 * c + 2.5 * t
	//column 2
	glp_set_col_name(lp, 2, "x2");				//assigns name x2 to first column
	glp_set_col_bnds(lp, 2, GLP_LO, 0.0, 0.0);	//sets the type and bounds to the second column
	glp_set_obj_coef(lp, 2, 2.5);				//sets the objective coefficient for thr second column

	/*
	p = 560 * c + 320 * t
	q = 136 * c + 40 * t
	*/

	row_index[1] = 1, col_index[1] = 1, value[1] = 560.0;	// a[1,1] = 560.0
	row_index[2] = 1, col_index[2] = 2, value[2] = 320.0;	// a[1,2] = 320.0
	row_index[3] = 2, col_index[3] = 1, value[3] = 136.0;	// a[2,1] = 136.0
	row_index[4] = 2, col_index[4] = 2, value[4] = 40.0;	// a[2,2] = 40.0
	
	for (int i = 1; i < 5; i++) {
		cout << value[i];
		cout << ((i % 2 == 0) ? "\n" : "\t");
	}

	glp_load_matrix(lp, 4, row_index, col_index, value);	//calls the routine glp_load_matrix
															//loads information from three arrays
															//into the problem object
	glp_simplex(lp, NULL);						//calls the routine glp_simplex
												//to solve LP problem
	z = glp_get_obj_val(lp);					//obtains a computed value of the objective function
	x1 = glp_get_col_prim(lp, 1);				//obtain computed values of structural variables (columns)
	x2 = glp_get_col_prim(lp, 2);				//obtain computed values of structural variables (columns)

	printf("\nPrize(z) = %g; chicken(x1) = %g; tofu(x2) = %g;\n", z, x1, x2); //writes out the optimal solution
	glp_delete_prob(lp);						//calls the routine glp_delete_prob, which frees all the memory
	keep_window_open();							//leave this line out and run program with ctrl + F5 to keep window open
												//that means you won't need std_lib_facilities.h
}
Output:
560     320
136     40
GLPK Simplex Optimizer, v4.63
2 rows, 2 columns, 4 non-zeros
      0: obj =   0.000000000e+00 inf =   3.480e+02 (2)
      2: obj =   2.678571429e+00 inf =   0.000e+00 (0)
*     3: obj =   2.443181818e+00 inf =   0.000e+00 (0)
OPTIMAL LP SOLUTION FOUND

Prize(z) = 2.44318; chicken(x1) = 0.159091; tofu(x2) = 0.659091;
Please enter a character to exit

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.