BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Audron
Obsidian | Level 7

This is more of a show and tell rather than a question.

 

For those that are using the SAS University Edition that don't have access to PROC OPTMODEL, I created an example of how to solve for an Optimal Mix of products that a company would need to sell.

 

In this example we have demand for 1,200 barrels of crude oil, but we can supply only 1,000. How much of each type of fuel should I produce given the different demand for the given types of fuels.

 

 

PROC IML;
	names={'87' '89' '91' '93' 'Diesel'};
	names2={'Supply' '87' '89' '91' '93' 'Diesel'};
	/*number of variables */
	n=ncol(A);
	/*Maximize, Print Solver Info*/
	cntl = {-1,1};
	/* Objective Function - Pofit Values of each segments for one oil plant*/
	obj = {70 50 30 80 100}; /*Profit = 87[70][X1] + 89[50][X1] + 91[30][X1] + 93[80][X1] + Diesel[100][X1]*/
	/* coefficients of the constraint equation*/
	A =  {
		  1 1 1 1 1  /*Turn on ALL the segments to later constrain Total Supply*/
		 ,1 0 0 0 0  /*Turn on 87 to later constrain 87 Demand*/
		 ,0 1 0 0 0  /*Turn on 89 to later constrain 89 Demand*/
		 ,0 0 1 0 0  /*Turn on 91 to later constrain 91 Demand*/
		 ,0 0 0 1 0  /*Turn on 93 to later constrain 93 Demand*/
		 ,0 0 0 0 1  /*Turn on Diesel to later constrain Diesel Demand*/
					}; 

	/*Constraint Values*/
	con = {
			1000 /*Total Supply*/
		    ,100 /*87 Demand*/
		    ,200 /*89 Demand*/
			,600 /*91 Demand*/
			,200 /*93 Demand*/
			,100 /*Diesel Demand*/
			};

	/*Constraint Operators: 'L' for <=, 'G' for >=, 'E' for =*/
	ops = {
			 'L' /*87[1] + 89[1] + 91[1] + 93 [1] + Diesel[1] <= 1000*/
			,'L' /*87[1] + 89[0] + 91[0] + 93 [0] + Diesel[0] <= 100*/
			,'L' /*87[0] + 89[1] + 91[0] + 93 [0] + Diesel[0] <= 200*/
			,'L' /*87[0] + 89[0] + 91[1] + 93 [0] + Diesel[0] <= 600*/
			,'L' /*87[0] + 89[0] + 91[0] + 93 [1] + Diesel[0] <= 200*/
			,'L' /*87[0] + 89[0] + 91[0] + 93 [0] + Diesel[1] <= 100*/
			};

	call lpsolve(rc, value, x, dual, redcost,obj, A, con, cntl, ops);

	/*Print Solver Information to Results, can do an ODS option to print to a dataset*/
		lhs = A*x;
		Constraints =  con || lhs ;
		print Constraints[r=names2
		                  c={"Demand" "Optimal"}
		                  L= "Optimal Mix"];
		print obj[L={"Profit Per Segment"} c=names];
		print value[L='Maximum Profit'];
QUIT;


Audron_0-1605869643559.png

 

The output shows that since 91 octane is the least profitable, it displaced 200 to make sure that demand is met for the other fuel types.

 

Note: I do not work for the oil industry or did any research on the topic, so please forgive any misleading assumptions on how an oil company makes decision of production of different fuels.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Audron
Obsidian | Level 7

I couldn't find the button in the first post, so I'll repost it here so that I can click solve. I'll work on adding the description on solving for LP Problems.

 

 

PROC IML;
	names={'87' '89' '91' '93' 'Diesel'};
	names2={'Supply' '87' '89' '91' '93' 'Diesel'};
	/*number of variables */
	n=ncol(A);
	/*Maximize, Print Solver Info*/
	cntl = {-1,1};
	/* Objective Function - Pofit Values of each segments for one oil plant*/
	obj = {70 50 30 80 100}; /*Profit = 87[70][X1] + 89[50][X1] + 91[30][X1] + 93[80][X1] + Diesel[100][X1]*/
	/* coefficients of the constraint equation*/
	A =  {
		  1 1 1 1 1  /*Turn on ALL the segments to later constrain Total Supply*/
		 ,1 0 0 0 0  /*Turn on 87 to later constrain 87 Demand*/
		 ,0 1 0 0 0  /*Turn on 89 to later constrain 89 Demand*/
		 ,0 0 1 0 0  /*Turn on 91 to later constrain 91 Demand*/
		 ,0 0 0 1 0  /*Turn on 93 to later constrain 93 Demand*/
		 ,0 0 0 0 1  /*Turn on Diesel to later constrain Diesel Demand*/
					}; 

	/*Constraint Values*/
	con = {
			1000 /*Total Supply*/
		    ,100 /*87 Demand*/
		    ,200 /*89 Demand*/
			,600 /*91 Demand*/
			,200 /*93 Demand*/
			,100 /*Diesel Demand*/
			};

	/*Constraint Operators: 'L' for <=, 'G' for >=, 'E' for =*/
	ops = {
			 'L' /*87[1] + 89[1] + 91[1] + 93 [1] + Diesel[1] <= 1000*/
			,'L' /*87[1] + 89[0] + 91[0] + 93 [0] + Diesel[0] <= 100*/
			,'L' /*87[0] + 89[1] + 91[0] + 93 [0] + Diesel[0] <= 200*/
			,'L' /*87[0] + 89[0] + 91[1] + 93 [0] + Diesel[0] <= 600*/
			,'L' /*87[0] + 89[0] + 91[0] + 93 [1] + Diesel[0] <= 200*/
			,'L' /*87[0] + 89[0] + 91[0] + 93 [0] + Diesel[1] <= 100*/
			};

	call lpsolve(rc, value, x, dual, redcost,obj, A, con, cntl, ops);

	/*Print Solver Information to Results, can do an ODS option to print to a dataset*/
		lhs = A*x;
		Constraints =  con || lhs ;
		print Constraints[r=names2
		                  c={"Demand" "Optimal"}
		                  L= "Optimal Mix"];
		print obj[L={"Profit Per Segment"} c=names];
		print value[L='Maximum Profit'];
QUIT;

 

 

View solution in original post

2 REPLIES 2
Rick_SAS
SAS Super FREQ

Thanks for this example of how to use LPSOLVE. Your code is very well organized and clear.

 

Since this is a solution and because "Solved Questions" rank higher for internet searches, I suggest you mark this question "Solved" (choose your own post) so that when others search for "LPSOLVE EXAMPLE SAS IML" they will find your program.

 

For people who are not familiar with LP problems, maybe you could add the problem description.

Audron
Obsidian | Level 7

I couldn't find the button in the first post, so I'll repost it here so that I can click solve. I'll work on adding the description on solving for LP Problems.

 

 

PROC IML;
	names={'87' '89' '91' '93' 'Diesel'};
	names2={'Supply' '87' '89' '91' '93' 'Diesel'};
	/*number of variables */
	n=ncol(A);
	/*Maximize, Print Solver Info*/
	cntl = {-1,1};
	/* Objective Function - Pofit Values of each segments for one oil plant*/
	obj = {70 50 30 80 100}; /*Profit = 87[70][X1] + 89[50][X1] + 91[30][X1] + 93[80][X1] + Diesel[100][X1]*/
	/* coefficients of the constraint equation*/
	A =  {
		  1 1 1 1 1  /*Turn on ALL the segments to later constrain Total Supply*/
		 ,1 0 0 0 0  /*Turn on 87 to later constrain 87 Demand*/
		 ,0 1 0 0 0  /*Turn on 89 to later constrain 89 Demand*/
		 ,0 0 1 0 0  /*Turn on 91 to later constrain 91 Demand*/
		 ,0 0 0 1 0  /*Turn on 93 to later constrain 93 Demand*/
		 ,0 0 0 0 1  /*Turn on Diesel to later constrain Diesel Demand*/
					}; 

	/*Constraint Values*/
	con = {
			1000 /*Total Supply*/
		    ,100 /*87 Demand*/
		    ,200 /*89 Demand*/
			,600 /*91 Demand*/
			,200 /*93 Demand*/
			,100 /*Diesel Demand*/
			};

	/*Constraint Operators: 'L' for <=, 'G' for >=, 'E' for =*/
	ops = {
			 'L' /*87[1] + 89[1] + 91[1] + 93 [1] + Diesel[1] <= 1000*/
			,'L' /*87[1] + 89[0] + 91[0] + 93 [0] + Diesel[0] <= 100*/
			,'L' /*87[0] + 89[1] + 91[0] + 93 [0] + Diesel[0] <= 200*/
			,'L' /*87[0] + 89[0] + 91[1] + 93 [0] + Diesel[0] <= 600*/
			,'L' /*87[0] + 89[0] + 91[0] + 93 [1] + Diesel[0] <= 200*/
			,'L' /*87[0] + 89[0] + 91[0] + 93 [0] + Diesel[1] <= 100*/
			};

	call lpsolve(rc, value, x, dual, redcost,obj, A, con, cntl, ops);

	/*Print Solver Information to Results, can do an ODS option to print to a dataset*/
		lhs = A*x;
		Constraints =  con || lhs ;
		print Constraints[r=names2
		                  c={"Demand" "Optimal"}
		                  L= "Optimal Mix"];
		print obj[L={"Profit Per Segment"} c=names];
		print value[L='Maximum Profit'];
QUIT;

 

 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Multiple Linear Regression in SAS

Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.

Find more tutorials on the SAS Users YouTube channel.

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 2 replies
  • 851 views
  • 7 likes
  • 2 in conversation