Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411283 Posts in 69325 Topics- by 58380 Members - Latest Member: bob1029

March 29, 2024, 12:51:51 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Food optimization algorithm
Pages: [1]
Print
Author Topic: Food optimization algorithm  (Read 1313 times)
gimymblert
Level 10
*****


The archivest master, leader of all documents


View Profile
« on: November 11, 2019, 09:06:48 PM »

Let's I have a set of character, each have a consumption rate per day based on age, weight, size and activity profile, express in Calorie.

Let's say I have food, express as Calorie, but having component of nutrient such has carb, protein and fat, express as normalized unit, and serving ratio that express the minimal size of a portion relative to that unit. And each food as a cost per serving, which can change base on brand or packing size.

Given a balance of nutrient such as 50% carb, 20% protein, 30% fat. What's the best algorithm I could use, that select food according to their serving, fill the Character need in Calorie, and achieve the best balance of nutrient for the lowest price?
Logged

powly
Level 4
****



View Profile WWW
« Reply #1 on: November 12, 2019, 01:13:29 AM »

This is a classic constrained linear optimization problem (along the first ones, see Stigler diet). I’d try a barrier (or ”interior point”) method, as they’re pretty easy to implement and should work well.
Logged
gimymblert
Level 10
*****


The archivest master, leader of all documents


View Profile
« Reply #2 on: November 12, 2019, 05:30:57 AM »

Thanks for that tip, the stigler diet is exactly what I wanted, but the math (notation) is beyond me, I'm looking for code lol oh well ...
Logged

powly
Level 4
****



View Profile WWW
« Reply #3 on: November 12, 2019, 11:44:19 AM »

Here's a quick python demo; this just uses gradient descent, if your primary step is better the barrier method will also work better (Newton would be ideal, as it solves the non-constrained linear problem with a single step). Depending on the nutrition content of your foods, there might not be a good solution and the optimizer will just find some solution with minimal error. This might also blow up in some cases, lowering the step size should help, or you can add a barrier from above to each food (or the total amount, this requires a bit of thinking though).
« Last Edit: November 12, 2019, 12:02:19 PM by powly » Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic