What does Bistability, Lipolysis and Menu Math conclude, in numbers, about “The Diet Problem, Solved Twice”?

Answer: Chapter 6 solves the cheapest compliant day at 8.2888 by two-phase simplex, with sodium the only tight constraint and no solution at all below an energy target of 1655.0 kcal, then builds twenty-eight distinct menus from the feasible subset of 210 candidates.

Scope of the numbers: Its parameters are illustrative or synthetic; the samples of Chapter 4 are generated; all figures are produced by the programs printed in the book; the book is not medical advice and does not diagnose. Chapter 4 reaches an area of 0.9645 for the index against 0.9244 for the waist ratio on the same synthetic sample, so the blood test adds 0.0400, with a best index cutoff of 8.705.

中文版

Data table

The numbers below are printed verbatim by the chapter's Python program and can be reproduced by running it. printed values that match the answer: 1655.0, 8.2888

cheapest day that meets every constraint8.2888
food hundred-gram units grams
eggs2.5000 250
chicken thigh3.0000 300
spinach4.0000 400
broccoli3.9293 393
olive oil0.6673 67
lite salt0.2000 20
energy 1800.0 kcal, protein 132.1 g, fat 126.4 g, carbohydrate 23.1 g
sodium 3000 mg, potassium 6609 mg, magnesium 504 mg
constraint floor or cap achieved tight
protein120 132.1 no
fat110 126.4 no
carbohydrate50 23.1 no
sodium3000 3000.0 yes
potassium3500 6609.3 no
magnesium400 503.5 no
sensitivity, cost of the cheapest day:
change cost rise
carbohydrate cap 30 g8.2888 +0.0000
carbohydrate cap 20 g8.3765 +0.0877
magnesium floor 500 mg8.2888 +0.0000
magnesium floor 600 mg8.4826 +0.1938
energy target 1500 kcal no feasible day
energy target 2100 kcal8.5286 +0.2397
lowest energy target the serving ceilings allow1655.0 kcal
the cheapest day there costs 8.1785
Show the full program
# Chapter 6. p11: The cheapest day that satisfies every constraint. This
# is a linear programme: the quantities of ten foods, in hundred-gram
# units, are chosen to minimise cost while the day's energy is held at a
# target and protein, fat, the three electrolytes and a carbohydrate
# ceiling are respected, with a serving ceiling on each food so that the
# answer is edible. The two-phase simplex method below is written out in
# full and uses Bland's rule, so it cannot cycle. Composition figures
# are rounded from published food tables; prices are illustrative.
FOODS = (
    # name, kcal, protein, fat, carb (g), Na, K, Mg (mg), cost, max units
    ("eggs",            143, 12.6,  9.5,  0.7, 142,  138,  12, 0.60, 2.5),
    ("chicken thigh",   209, 26.0, 10.9,  0.0,  86,  230,  25, 0.90, 3.0),
    ("salmon",          208, 20.4, 13.4,  0.0,  50,  363,  29, 2.40, 2.0),
    ("beef mince",      250, 26.0, 15.0,  0.0,  75,  318,  21, 1.30, 3.0),
    ("spinach",          23,  2.9,  0.4,  1.4,  79,  558,  79, 0.50, 4.0),
    ("broccoli",         34,  2.8,  0.4,  4.0,  33,  316,  21, 0.40, 4.0),
    ("avocado",         160,  2.0, 14.7,  1.8,   7,  485,  29, 0.80, 3.0),
    ("olive oil",       884,  0.0,100.0,  0.0,   2,    1,   0, 0.70, 1.0),
    ("pumpkin seeds",   559, 30.2, 49.1,  4.7,  18,  809, 592, 1.80, 0.6),
    ("lite salt",         0,  0.0,  0.0,  0.0,9700,10500,   0, 0.25, 0.2),
)
TARGET_KCAL = 1800.0
MIN_PROTEIN, MIN_FAT, MAX_CARB = 120.0, 110.0, 50.0
MIN_NA, MIN_K, MIN_MG = 3000.0, 3500.0, 400.0

def simplex(c, rows, verbose=False):
    """min c.x s.t. rows of (coeffs, sense, rhs), x >= 0. Two phases."""
    n = len(c)
    m = len(rows)
    cols = n
    extra = []            # (kind, row index)
    for i, (_, sense, _) in enumerate(rows):
        if sense == "<=":
            extra.append(("slack", i))
        elif sense == ">=":
            extra.append(("surplus", i))
            extra.append(("art", i))
        else:
            extra.append(("art", i))
    total = cols + len(extra)
    T = [[0.0] * (total + 1) for _ in range(m)]
    basis = [-1] * m
    for i, (a, _, b) in enumerate(rows):
        for j in range(n):
            T[i][j] = float(a[j])
        T[i][total] = float(b)
    for k, (kind, i) in enumerate(extra):
        j = cols + k
        T[i][j] = -1.0 if kind == "surplus" else 1.0
        if kind in ("slack", "art"):
            basis[i] = j
    art = [cols + k for k, (kind, _) in enumerate(extra) if kind == "art"]

    def pivot(obj, allowed):
        while True:
            # Bland: lowest index with a negative reduced cost
            z = [0.0] * (total + 1)
            for i in range(m):
                cb = obj[basis[i]]
                if cb:
                    for j in range(total + 1):
                        z[j] += cb * T[i][j]
            enter = -1
            for j in allowed:
                if obj[j] - z[j] < -1e-9:
                    enter = j
                    break
            if enter < 0:
                return z[total]
            best, leave = None, -1
            for i in range(m):
                if T[i][enter] > 1e-9:
                    r = T[i][total] / T[i][enter]
                    tie = (abs(r - best) <= 1e-9
                           if best is not None else False)
                    if best is None or r < best - 1e-9 or \
                            (tie and basis[i] < basis[leave]):
                        best, leave = r, i
            if leave < 0:
                raise ValueError("unbounded")
            p = T[leave][enter]
            T[leave] = [v / p for v in T[leave]]
            for i in range(m):
                if i != leave and abs(T[i][enter]) > 1e-12:
                    fv = T[i][enter]
                    T[i] = [T[i][j] - fv * T[leave][j]
                            for j in range(total + 1)]
            basis[leave] = enter

    obj1 = [0.0] * total
    for j in art:
        obj1[j] = 1.0
    pivot(obj1, list(range(total)))
    if sum(T[i][total] for i in range(m) if basis[i] in art) > 1e-7:
        raise ValueError("infeasible")
    keep = [j for j in range(total) if j not in art]
    obj2 = [0.0] * total
    for j in range(n):
        obj2[j] = float(c[j])
    val = pivot(obj2, keep)
    x = [0.0] * n
    for i in range(m):
        if basis[i] < n:
            x[basis[i]] = T[i][total]
    return val, x

def build(max_carb=MAX_CARB, min_mg=MIN_MG, target=TARGET_KCAL):
    cost = [f[8] for f in FOODS]
    rows = []
    rows.append(([f[1] for f in FOODS], "=", target))
    rows.append(([f[2] for f in FOODS], ">=", MIN_PROTEIN))
    rows.append(([f[3] for f in FOODS], ">=", MIN_FAT))
    rows.append(([f[4] for f in FOODS], "<=", max_carb))
    rows.append(([f[5] for f in FOODS], ">=", MIN_NA))
    rows.append(([f[6] for f in FOODS], ">=", MIN_K))
    rows.append(([f[7] for f in FOODS], ">=", min_mg))
    for i, f in enumerate(FOODS):
        a = [0.0] * len(FOODS)
        a[i] = 1.0
        rows.append((a, "<=", f[9]))
    return cost, rows

def totals(x):
    out = [0.0] * 7
    for xi, f in zip(x, FOODS):
        for k in range(7):
            out[k] += xi * f[k + 1]
    return out

if __name__ == "__main__":
    cost, rows = build()
    val, x = simplex(cost, rows)
    print("   cheapest day that meets every constraint: {:.4f}".format(val))
    print("   food             hundred-gram units   grams")
    for xi, f in zip(x, FOODS):
        if xi > 1e-6:
            print("   {:<17}{:<21.4f}{:.0f}".format(f[0], xi, xi * 100.0))
    t = totals(x)
    print("   energy {:.1f} kcal, protein {:.1f} g, fat {:.1f} g,"
          " carbohydrate {:.1f} g".format(t[0], t[1], t[2], t[3]))
    print("   sodium {:.0f} mg, potassium {:.0f} mg, magnesium {:.0f} mg"
          .format(t[4], t[5], t[6]))
    print("   constraint       floor or cap   achieved   tight")
    checks = (("protein", MIN_PROTEIN, t[1], "floor"),
              ("fat", MIN_FAT, t[2], "floor"),
              ("carbohydrate", MAX_CARB, t[3], "cap"),
              ("sodium", MIN_NA, t[4], "floor"),
              ("potassium", MIN_K, t[5], "floor"),
              ("magnesium", MIN_MG, t[6], "floor"))
    for name, lim, got, kind in checks:
        tight = "yes" if abs(got - lim) < 0.5 else "no"
        print("   {:<17}{:<15.0f}{:<11.1f}{}".format(name, lim, got, tight))
    print("   sensitivity, cost of the cheapest day:")
    print("   change                        cost      rise")
    for label, kw in (("carbohydrate cap 30 g", {"max_carb": 30.0}),
                      ("carbohydrate cap 20 g", {"max_carb": 20.0}),
                      ("magnesium floor 500 mg", {"min_mg": 500.0}),
                      ("magnesium floor 600 mg", {"min_mg": 600.0}),
                      ("energy target 1500 kcal", {"target": 1500.0}),
                      ("energy target 2100 kcal", {"target": 2100.0})):
        c2, r2 = build(**kw)
        try:
            v2, _ = simplex(c2, r2)
        except ValueError:
            print("   {:<30}{}".format(label, "no feasible day"))
            continue
        print("   {:<30}{:<10.4f}{:+.4f}".format(label, v2, v2 - val))
    lo, hi = 1400.0, 1800.0
    for _ in range(40):
        mid = 0.5 * (lo + hi)
        c2, r2 = build(target=mid)
        try:
            simplex(c2, r2)
            hi = mid
        except ValueError:
            lo = mid
    print("   lowest energy target the serving ceilings allow:"
          " {:.1f} kcal".format(0.5 * (lo + hi)))
    c2, r2 = build(target=0.5 * (lo + hi) + 1.0)
    v2, _ = simplex(c2, r2)
    print("   the cheapest day there costs {:.4f}".format(v2))

Single source and citation

Fan, Y. (2026). Insulin-Glucagon Bistability, Visceral Lipolysis Kinetics and Linear-Programming Diet Optimization (Version 1.0.0), Chapter 6: The Diet Problem, Solved Twice. 5D Library. https://doi.org/10.5281/zenodo.24090248

DOI: 10.5281/zenodo.24090248 · Read the monograph · 5DL-B-095 · v1.0.0 · CC-BY-NC-ND-4.0

BibTeX
@book{fan2026095,
  author    = {Fan, Yafu},
  title     = {Insulin-Glucagon Bistability, Visceral Lipolysis Kinetics and Linear-Programming Diet Optimization},
  year      = {2026},
  publisher = {5D Library},
  edition   = {1.0.0},
  doi       = {10.5281/zenodo.24090248},
  url       = {https://doi.org/10.5281/zenodo.24090248}
}

Other chapter answers in this book