I'm enrolled in an optimization class that teaches solutions in SAS and Risk Solver (in Excel). These, unfortunately, are not free and I'm afraid that I will not have access to them following the program. Here, I'm presenting a homework problem that we had (for which I've also included the solution in SAS). What I would like to know is which packages to use to solve these types of problems in python and R, and if possible, some example code in either of those languages.

This was a 3 part problem. This is only part 1:

*An oil company produces three brands of oils: Regular, Multigrade, and Supreme. Each brand of oil is composed of one or more of four crude stocks, each having a different viscosity index. The relevant data concerning the crude stocks are:*

```
Crude Stock Viscosity Index Cost($/barrel) Supply per day (barrels)
1 20 7.10 1000
2 40 8.50 1100
3 30 7.70 1200
40 55 9.00 1100
```

*Each brand of oil must meet a minimum standard for viscosity index, and each brand thus sells at a different price. The relevant data concerning the three brands of oil are:*

```
Brand Min. Viscosity Index Selling Price ($/barrel) Daily Demand (barrels)
Regular 25 8.50 2000
Multigrade 35 9.00 1500
Supreme 50 10.00 750
```

*Determine an optimal production plan for a single day, assuming that all oil produced during this day can be either sold or stored at negligible cost.
Assume that the daily demands represent potential sales. In other words, the model should contain demand ceilings (upper limits). What is the optimal profit under these assumptions?*

Here's the SAS solution for this part of the problem:

```
PROC OPTMODEL;
VAR s1 >=0, s2 >=0, s3 >=0, s4 >=0,
m1 >=0, m2 >=0, m3 >=0, m4 >=0,
r1 >=0, r2 >=0, r3 >=0, r4 >=0;
MAX profit=(10*(s1+s2+s3+s4)+9*(m1+m2+m3+m4)+8.5*(r1+r2+r3+r4))
-(7.1*(s1+m1+r1)+8.5*(s2+m2+r2)+7.7*(s3+m3+r3)+9*(s4+m4+r4));
CON crude1supply: s1+m1+r1<=1000;
CON crude2supply: s2+m2+r2<=1100;
CON crude3supply: s3+m3+r3<=1200;
CON crude4supply: s4+m4+r4<=1100;
CON supremedemand: s1+s2+s3+s4<=750;
CON multgraddemand: m1+m2+m3+m4<=1500;
CON regulardemand: r1+r2+r3+r4<=2000;
CON v_supreme: (1/(s1+s2+s3+s4))*(20*s1+40*s2+30*s3+55*s4)>=50;
CON v_multgrad: (1/(m1+m2+m3+m4))*(20*m1+40*m2+30*m3+55*m4)>=35;
CON v_regular: (1/(r1+r2+r3+r4))*(20*r1+40*r2+30*r3+55*r4)>=25;
SOLVE;
PRINT s1 s2 s3 s4
m1 m2 m3 m4
r1 r2 r3 r4
profit;
QUIT;
```

To restate my questions: what are the best packages to use to solve these types of problems in python and/or R? Can you provide some sample code (or resources) for how to solve this problem in python and/or R?

I've looked at scipy.optimize and at Pyomo so far, but it's unclear to me that they represent the best libraries to use.

## Best Answer

Check the CRAN Task View on Optimization and Mathematical Programming. I believe that the packages linprog and quadprog will be good starting points for this kind of tasks, both have handy reference manuals on how to input equality and inequality constraints. As you problems get bigger the rneos might also prove handy.