simple linear programming example – small and large vans [GLPK]

This example was found on linkedIn SlideShare Page 3:

The West Hartford Senior Center is trying to establish a
transportation system of small and large vans. It can
spend no more than $100,000 for both sizes of vehicles
and no more than $500 per month for maintenance. The
WHSC can purchase a small van, which carries up to 7
passengers
, for $10,000 and maintain it for $100 per
month
. The large vans, which carry up to 15
passengers
, cost $20,000 each and can be maintained for
$75 per month. How many of each type of van should
they purchase if they want to maximize the number of
passengers
?

s = small van
l = large van

optimisation equation:
z = 7 * s + 15 * l

subject to:
10.000 * s + 20.000 * l <= 100.000 //initial purchase costs
100 * s + 75 * l <= 500 //monthly maintenance

also written as:
p = 10.000 * s + 20.000 * l
q = 100 * s + 75 * l

-inf < p <= 100.000 0 <= x1 < +inf
-inf < q <= 500 0 <= x2 < +inf

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

#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;
	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_MAX);				//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_UP, 0.0, 100000.0);//sets the type and bounds of the first row,
												//where GLP_UP means that the row has an upper bound.
												//-inf < p <= 100
	//row 2
	glp_set_row_name(lp, 2, "q");				//assigns name q to second row
	glp_set_row_bnds(lp, 2, GLP_UP, 0.0, 500.0);//-inf < q <= 600


	//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, 7.0);				//sets the objective coefficient for thr first column
												//z = 10 * x1 + 6 * x2 + 4 * x3
	//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, 15.0);				//sets the objective coefficient for thr second column
	
	/*
	p = 10.000 * s + 20.000 * l
	q = 100 * s + 75 * l
	*/

	row_index[1] = 1, col_index[1] = 1, value[1] = 10000.0;	// a[1,1] = 100000.0
	row_index[2] = 1, col_index[2] = 2, value[2] = 20000.0;	// a[1,2] = 200000.0
	row_index[3] = 2, col_index[3] = 1, value[3] = 100.0;	// a[2,1] = 100.0
	row_index[4] = 2, col_index[4] = 2, value[4] = 75.0;	// a[2,2] = 75.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("\nz = %g; x1 = %g; 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:
10000   20000
100     75
GLPK Simplex Optimizer, v4.63
2 rows, 2 columns, 4 non-zeros
*     0: obj =  -0.000000000e+00 inf =   0.000e+00 (2)
*     1: obj =   7.500000000e+01 inf =   0.000e+00 (0)
OPTIMAL LP SOLUTION FOUND

z = 75; x1 = 0; x2 = 5;
Please enter a character to exit

One thought on “simple linear programming example – small and large vans [GLPK]

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 )

Connecting to %s

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