Diet Problem

My diet requires that all the food I eat come from one of the four “basic food groups” (chocolate cake, ice cream, soda, and cheesecake). At present, the following four foods are available for consumption: brownies, chocolate ice cream, cola, and pineapple cheesecake. Each brownie costs 50 cents, each scoop of chocolate ice cream costs 20 cents, each bottle of cola costs 30 cents, and each piece of pineapple cheesecake costs 80 cents. Each day, I must ingest at least 500 calories, 6 oz of chocolate, 10 oz of sugar, and 8 oz of fat. The nutritional content per unit of each food is shown in the following table. Formulate a linear programming model that can be used to satisfy my daily nutritional requirements at minimum costs.


In [ ]:
from gurobipy import *

try:

    # Create a new model
    m = Model("diet_problem")

    # Create variables
    x1 = m.addVar(name="brownie")
    x2 = m.addVar(name="icecream")
    x3 = m.addVar(name="cola")
    x4 = m.addVar(name="pineapple")
    
    # Integrate new variables
    m.update()

    # Set objective
    m.setObjective(50 * x1 + 20 * x2 + 30 * x3 + 80 * x4, GRB.MINIMIZE)
    
    # Add constraint
    m.addConstr(400 * x1 + 200 * x2 + 150 * x3 + 500 * x4 >= 500, "c0")
    m.addConstr(3 * x1 + 2 * x2 >= 6, "c1")
    m.addConstr(2 *x1 + 2 * x2 + 4 *x3 + 4 * x4 >= 10, "c2")
    m.addConstr(2 * x1 + 4 *x2 + x3 + 5 *x4 >= 8, "c3")

    m.optimize()
    
    
    for v in m.getVars():
        print (v.varName, v.x)

    print ('Obj:', m.objVal)
except GurobiError:
    print ('Error reported')