Skip to content

Instantly share code, notes, and snippets.

@TRManderson
Last active March 17, 2016 12:40
Show Gist options
  • Select an option

  • Save TRManderson/b8c76a7362b5c66df7f3 to your computer and use it in GitHub Desktop.

Select an option

Save TRManderson/b8c76a7362b5c66df7f3 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Week 2 – Blending Problem \n",
"A food manufacturer refines raw oils and blends them together. The raw oils come in two categories – vegetable oils, of which there are two types; and non-­‐ vegetable oils, of which there are three types.\n",
"\n",
"The oils are refined on different production lines. In any month it is not possible to refine more than 200 tonnes of vegetable oil and more than 250 tonnes of non-­‐vegetable oils. There is no loss of mass in the refining process and the cost of refining may be ignored.\n",
"\n",
"There is a technological restriction on the “hardness” of the final product. In the units in which hardness is measured it must lie between 3 and 6. The hardness of a blended product is the weighted average of its components. The cost per tonne and hardness of the raw oils are:\n",
"\n",
"Oil | Cost | Hardness \n",
"------|------|----\n",
"Veg 1 | \\$110 | 8.8\n",
"Veg 2 | \\$120 | 6.1\n",
"Oil 1 | \\$130 | 2.0\n",
"Oil 2 | \\$110 | 4.2\n",
"Oil 3 | \\$115 | 5.0\n",
"\n",
"The final product sells for $150 per tonne. How should the food manufacturer make this product in order to maximise net profit? "
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import pulp"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Variables\n",
"veg_1 = pulp.LpVariable(\"veg_1\", 0, 200)\n",
"veg_2 = pulp.LpVariable(\"veg_2\", 0, 200)\n",
"oil_1 = pulp.LpVariable(\"oil_1\", 0, 250)\n",
"oil_2 = pulp.LpVariable(\"oil_2\", 0, 250)\n",
"oil_3 = pulp.LpVariable(\"oil_3\", 0, 250)\n",
"\n",
"lpVars = [veg_1, veg_2, oil_1, oil_2, oil_3]"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"problem = pulp.LpProblem(\"Oil Blending\", pulp.LpMaximize)\n",
"\n",
"# Objective is profit\n",
"problem += veg_1*40 + veg_2*30 + oil_1*20 + oil_2 * 40 + oil_3 * 35"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Constraints\n",
"\n",
"# Hardness\n",
"problem += 3* pulp.lpSum(lpVars) <= (8.8*veg_1 + 6.1*veg_2 + 2.0*oil_1 + 4.2*oil_2 + 5.0*oil_3) <= 6* pulp.lpSum(lpVars)\n",
"\n",
"# Quantities\n",
"problem += 0 <= veg_1 + veg_2 <= 200\n",
"problem += 0 <= oil_1 + oil_2 + oil_3 <= 250"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"Oil Blending:\n",
"MAXIMIZE\n",
"20*oil_1 + 40*oil_2 + 35*oil_3 + 40*veg_1 + 30*veg_2 + 0\n",
"SUBJECT TO\n",
"_C1: - 4 oil_1 - 1.8 oil_2 - oil_3 + 2.8 veg_1 + 0.1 veg_2 <= 0\n",
"\n",
"_C2: veg_1 + veg_2 <= 200\n",
"\n",
"_C3: oil_1 + oil_2 + oil_3 <= 250\n",
"\n",
"VARIABLES\n",
"oil_1 <= 250 Continuous\n",
"oil_2 <= 250 Continuous\n",
"oil_3 <= 250 Continuous\n",
"veg_1 <= 200 Continuous\n",
"veg_2 <= 200 Continuous"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"problem"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"'Optimal'"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pulp.LpStatus[problem.solve()]"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"veg_1: 159.25926\n",
"veg_2: 40.740741\n",
"oil_1: 0.0\n",
"oil_2: 250.0\n",
"oil_3: 0.0\n"
]
}
],
"source": [
"print(\"\\n\".join(\"{}: {}\".format(v, pulp.value(v)) for v in [\n",
" veg_1,\n",
" veg_2,\n",
" oil_1,\n",
" oil_2,\n",
" oil_3,\n",
" ]))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.9"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment