Title | AD HW11 Full - Homework#11 - Optimization |
---|---|
Course | Intro to Analytics Modeling |
Institution | Georgia Institute of Technology |
Pages | 8 |
File Size | 263.1 KB |
File Type | |
Total Downloads | 86 |
Total Views | 159 |
Homework#11 - Optimization...
11/2/2020
Assignment 15.2.ipynb - Colaboratory
Reference:- https://www.kdnuggets.com/2019/05/linear-programming-discrete-optimizationpython-pulp.html Question 15.2 In the videos, we saw the “diet problem”. (The diet problem is one of the rst large-scale optimization problems to be studied in practice. Back in the 1930’s and 40’s, the Army wanted to meet the nutritional requirements of its soldiers while minimizing the cost.) In this homework you get to solve a diet problem with real data. The data is given in the le diet.xls. 1. Formulate an optimization model (a linear program) to nd the cheapest diet that satises the maximum and minimum daily nutrition constraints, and solve it using PuLP. Turn in your code and the solution. (The optimal solution should be a diet of air-popped popcorn, poached eggs, oranges, raw iceberg lettuce, raw celery, and frozen broccoli. UGH!)
import pandas as pd import numpy as np
!pip install pulp
from pulp import *
from google.colab import files uploaded = files.upload() diet.xls diet.xls(application/vnd.ms-excel) - 25088 bytes, last modified: 11/2/2020 - 100% done Saving diet.xls to diet.xls
data = pd.read_excel("diet.xls", header = 0) # read all data
dataTable = data[0:64] # rows 0:64 (Excel calls them 1-65) is the food data table dataTable = dataTable.values.tolist() # Convert dataframe to list
minimum nutrient list and maximum nutrient list is created. nutrientNames = list(data.columns.values) minVal = data[65:66].values.tolist() maxVal = data[66:67].values.tolist()
https://colab.research.google.com/drive/1QTvSSWihEOrH10gWsNzr2-s362LCTiug#scrollTo=bgbO8Wdvtul4
1/4
11/2/2020
Assignment 15.2.ipynb - Colaboratory
You can take all the nutrition components and create separate dictionaries for them.
foods = [j[0] for j in dataTable] #list of food names cost = dict([(j[0], float(j[1])) for j in dataTable]) # cost for each food nutrients = [] for i in range(0,11): # for loop running through each nutrient: 11 times starting with 0 nutrients.append(dict([(j[0], float(j[i+3])) for j in dataTable])) # amount of nutrien
First, we create a LP problem with the method LpProblemin PuLP. Then, we need to create bunches of Python dictionary objects with the information we have from the table. The code is shown below, prob = LpProblem('Food optimization', LpMinimize) # 2 parameters: "name" and "sense" foodVars = LpVariable.dicts("Foods", foods, 0) foodVars_selected = LpVariable.dicts("food_select",foods,0,1,LpBinary) #Create binary inte
/usr/local/lib/python3.6/dist-packages/pulp/pulp.py:1198: UserWarning: Spaces are no warnings.warn("Spaces are not permitted in the name. Converted to '_'")
Objective function prob += lpSum([cost[f] * foodVars[f] for f in foods]), 'Total Cost'
Add Constraints for i in range(0,11): # for loop running through each nutrient: 11 times starting with 0 prob += lpSum([nutrients[i][j] * foodVars[j] for j in foods]) >= minVal[0][i+3], 'min prob += lpSum([nutrients[i][j] * foodVars[j] for j in foods])...