Knap[Snack] is a tool that helps you create personalised, cost-effective meal plans. It takes into account macronutrients (proteins, carbohydrates, fats), micronutrients (vitamins, minerals), and hydration to ensure a balanced diet tailored to your age, gender, and health goals.
Under the hood, Knap[Snack] uses an optimisation algorithm called mixed-integer linear programming (MILP). Here's the maths:
The objective is to minimise the total cost of the diet:
where:
-
$n$ is the number of available food items -
$c_i$ is the cost per serving of food item$i$ -
$x_i$ is the number of servings of food item$i$
The optimisation is subject to the following constraints:
For each nutrient
For nutrients with established upper limits, the total amount must not exceed the maximum allowable intake:
where:
-
$m$ is the number of nutrients -
$p$ is the number of micronutrients with upper limits -
$a_{ij}$ is the amount of nutrient$j$ in one serving of food item$i$ -
$b_j$ is the minimum required amount of nutrient$j$ (RDA value) -
$u_j$ is the maximum allowable amount of nutrient$j$ (UL value)
Once Knap[Snack] calculates your daily caloric target, you specify how you want to split that into protein, carbs, and fats. These values then form the lower limits for those nutrients:
where:
-
$C$ is your daily caloric goal -
$r_p, r_c$ and$r_f$ are the proportion of calories from protein, carbs, and fats respectively -
$a_{i,p}, a_{i,c}$ and$a_{i,f}$ are grams of protein, carbs, and fats in food item$i$ -
$w_p, w_c$ and$w_f$ are the Atwater energy factors for protein (4 kcal/g), carbs (4 kcal/g), and fats (9 kcal/g)
Additionally, in accordance with evidence-based recommendations, fibre has been given a lower limit of 14 grams per 1000 calories:
And saturated fats have been capped at 10% of your daily caloric goal:
Unlike micronutrients, macronutrients don't have well-defined toxicity thresholds, so they typically don't require strict upper limits. However, without any upper bounds, the optimiser might converge on solutions where macronutrient values are significantly higher than their intended targets, disrupting your desired macronutrient ratios and resulting in excessive overall intake. At the same time, enforcing zero deviation from each macronutrient target may make it impossible for the optimiser to find a feasible solution. To balance these competing needs, Knap[Snack] allows controlled deviations above your macronutrient targets:
where
This creates a dual-objective optimisation problem:
- minimising the total cost of the diet, and
- minimising deviations from your target macronutrient values.
Knap[Snack] solves this using a systematic grid search approach that explores combinations of allowable deviations, prioritising solutions that stay closest to your nutritional targets while still finding feasible, cost-effective meal plans.
Each food has a maximum practical serving size
Foods marked as 'Discrete Servings' will not be recommended in fractional amounts, e.g., 1.5 eggs →
Foods marked as 'Must Include' will be guaranteed to appear in your meal plan →
Visualise the repo structure here.
