Diet

Problem Statement

Builds and solves the classic diet problem: choosing food portions to minimize cost while meeting daily nutrition targets.

Full example scenario

A shopper wants to minimize the cost of food while meeting daily nutritional needs. Which foods, and how many portions of each, should they buy?

  • They want to eat between 1800 and 2200 calories per day.

  • At least 91 grams of protein.

  • At most 65 grams of fat.

  • At most 1779 mg of sodium.

  • Portions are non-divisible.

  • No single food may exceed 3 portions a day.

  • The foods to choose between can be found in diet.csv, with the columns food,price,calories,protein,fat,sodium.

What an optimal solution looks like

Daily cost

$5.37

Foods used

3 of 9

Total portions

4

Portions bought per food (cap: 3 per item).

Salad
2 · $1.98
Chicken
1 · $1.89
Hot dog
1 · $1.50
Not purchased: Hamburger Fries Macaroni Pizza Milk Ice cream

How close each nutrient sits to its limit.

Red marks the tightest constraints — the ones actually driving the solution.

Calories (floor 1,600)1,620 of 1,600–2,800
Protein (floor 91g)114g · 23g to spare
Fat (ceiling 100g)66g · 66% of max
Sodium (ceiling 2,600mg)2,450mg · 94% of max

Built with Modeler

The problem above is stated as a single shot: you hand over the whole problem and get a model back. Modeler takes the other route — you start with the business question you actually have, and it works with you until the problem is pinned down, the data is in, the code runs, and the answer holds up when the problem changes.

The tabs below walk the same diet problem through that route, one Modeler capability at a time.

Instead of writing the whole prompt up front, you start with the business question you actually have. Modeler searches the knowledge base, asks one clarifying question at a time — always the most important one first — and tells you which parts of the model are still missing. The options it offers come from examples in the knowledge base and from how similar problems are usually modeled, so you can answer with a number instead of prose.

I want to eat healthier without blowing my grocery budget. Can you help me figure out what to buy?

The user wants to minimize food spending subject to nutrition targets — this is a classic diet-optimization problem. Let me search the knowledge base for relevant examples and ask clarifying questions one at a time. First, let me search the knowledge base, and then ask the first clarifying question.

Querying the knowledge base.

Good, I found relevant diet-optimization examples. “Eat healthier” isn’t a number yet, so let me ask what nutrition targets the user actually wants to track before anything else.

This is a classic diet-optimization problem — minimizing food cost while meeting nutrition targets. I have found relevant examples in the knowledge base to guide the modelling. Let me ask you a few questions to build a complete specification.

Question 1: What nutrition targets do you want the plan to satisfy?

  1. A calorie range only (a target window, e.g. 1800–2200 calories)

  2. Calories plus a minimum protein target

  3. Calories plus upper limits on fat and/or sodium

  4. All of the above — calories, a protein minimum, and caps on fat and sodium

Learn More:
• Diet Optimization Examples ★★☆
• Nutritional Diet Model ★★☆

I want to eat between 1800 and 2200 calories a day, get at least 91 grams of protein, and stay under 65 grams of fat and 1779 mg of sodium.

That is option 4, with all four numeric targets supplied at once. Recording the objective and the constraint values in the specification. Still open: what foods are actually available to choose from, and whether partial portions are allowed. Asking about portion divisibility next, since it changes the variable type rather than just the data.

Recorded — calories between 1800 and 2200, at least 91g protein, at most 65g fat, at most 1779mg sodium. Two things are still missing before I can implement anything:

• Data — the foods you are choosing between, with a price and a nutrition profile per portion. A file works best here.
• Constraint — whether portions can be fractional (half a burger, say) or must be whole numbers.

Question 2: Should portions be whole numbers, or can they be fractional?

  1. Whole portions only — no half portions

  2. Fractional portions allowed — e.g. 1.5 servings

Learn More:
• Modeling Integer vs. Continuous Decisions ★★★
• Diet Optimization Examples ★★☆

Whole portions only. And I’d like to see the full nutrition breakdown of whatever you recommend, not just the cost. One more thing — please cap any single food at 3 portions a day. I don’t want a plan that’s basically just one item.

Thinking…

Modeler updates the specification after every answer, even while it is still incomplete — so you can always see what it has understood and what it is still waiting on.