Try our new documentation site (beta).


# Copyright 2016, Gurobi Optimization, Inc.

# Read diet model data from an Excel spreadsheet (diet.xls).
# Pass the imported data into the diet model (
# Note that this example reads an external data file (..\data\diet.xls).
# As a result, it must be run from the Gurobi examples/python directory.
# This example requires Python package 'xlrd', which isn't included
# in most Python distributions.  You can obtain it from

import os
import xlrd

book = xlrd.open_workbook(os.path.join("..", "data", "diet.xls"))

sh = book.sheet_by_name("Categories")
categories = []
minNutrition = {}
maxNutrition = {}
i = 1
while True:
        c = sh.cell_value(i, 0)
        minNutrition[c] = sh.cell_value(i,1)
        maxNutrition[c] = sh.cell_value(i,2)
        i = i + 1
    except IndexError:

sh = book.sheet_by_name("Foods")
foods = []
cost = {}
i = 1
while True:
        f = sh.cell_value(i, 0)
        cost[f] = sh.cell_value(i,1)
        i = i + 1
    except IndexError:

sh = book.sheet_by_name("Nutrition")
nutritionValues = {}
for i in range(len(foods)):
    for j in range(len(categories)):
        nutritionValues[foods[i],categories[j]] = sh.cell_value(i+1,j+1)

import dietmodel
dietmodel.solve(categories, minNutrition, maxNutrition,
                foods, cost, nutritionValues)

Try Gurobi for Free

Choose the evaluation license that fits you best, and start working with our Expert Team for technical guidance and support.

Evaluation License
Get a free, full-featured license of the Gurobi Optimizer to experience the performance, support, benchmarking and tuning services we provide as part of our product offering.
Academic License
Gurobi supports the teaching and use of optimization within academic institutions. We offer free, full-featured copies of Gurobi for use in class, and for research.
Cloud Trial

Request free trial hours, so you can see how quickly and easily a model can be solved on the cloud.