Languages:
C
C++
Java
.NET
Python
MATLAB
R

## Cloud Guide

diet_cs.cs

### diet_cs.cs

```/* Copyright 2017, Gurobi Optimization, Inc. */

/* Solve the classic diet model, showing how to add constraints
to an existing model. */

using System;
using Gurobi;

class diet_cs
{
static void Main()
{
try {

// Nutrition guidelines, based on
// USDA Dietary Guidelines for Americans, 2005
// http://www.health.gov/DietaryGuidelines/dga2005/
string[] Categories =
new string[] { "calories", "protein", "fat", "sodium" };
int nCategories = Categories.Length;
double[] minNutrition = new double[] { 1800, 91, 0, 0 };
double[] maxNutrition = new double[] { 2200, GRB.INFINITY, 65, 1779 };

// Set of foods
string[] Foods =
new string[] { "hamburger", "chicken", "hot dog", "fries",
"macaroni", "pizza", "salad", "milk", "ice cream" };
int nFoods = Foods.Length;
double[] cost =
new double[] { 2.49, 2.89, 1.50, 1.89, 2.09, 1.99, 2.49, 0.89,
1.59 };

// Nutrition values for the foods
double[,] nutritionValues = new double[,] {
{ 410, 24, 26, 730 },   // hamburger
{ 420, 32, 10, 1190 },  // chicken
{ 560, 20, 32, 1800 },  // hot dog
{ 380, 4, 19, 270 },    // fries
{ 320, 12, 10, 930 },   // macaroni
{ 320, 15, 12, 820 },   // pizza
{ 320, 31, 12, 1230 },  // salad
{ 100, 8, 2.5, 125 },   // milk
{ 330, 8, 10, 180 }     // ice cream
};

// Model
GRBEnv env = new GRBEnv();
GRBModel model = new GRBModel(env);

model.ModelName = "diet";

// Create decision variables for the nutrition information,
// which we limit via bounds
GRBVar[] nutrition = new GRBVar[nCategories];
for (int i = 0; i < nCategories; ++i) {
nutrition[i] =
Categories[i]);
}

// Create decision variables for the foods to buy
for (int j = 0; j < nFoods; ++j) {
}

// The objective is to minimize the costs
model.ModelSense = GRB.MINIMIZE;

// Nutrition constraints
for (int i = 0; i < nCategories; ++i) {
GRBLinExpr ntot = 0.0;
for (int j = 0; j < nFoods; ++j)
}

// Solve
model.Optimize();

Console.WriteLine("\nAdding constraint: at most 6 servings of dairy");

// Solve
model.Optimize();

// Dispose of model and env
model.Dispose();
env.Dispose();

} catch (GRBException e) {
Console.WriteLine("Error code: " + e.ErrorCode + ". " +
e.Message);
}
}

private static void PrintSolution(GRBModel model, GRBVar[] buy,
GRBVar[] nutrition) {
if (model.Status == GRB.Status.OPTIMAL) {
Console.WriteLine("\nCost: " + model.ObjVal);
for (int j = 0; j < buy.Length; ++j) {