Book Example: diet#
Description: book example autogenerated using diet.mod, diet.dat, and diet.run
Tags: ampl-only, ampl-book
Notebook author: Marcos Dominguez Velad <marcos@ampl.com>
Model author: N/A
# Install dependencies
%pip install -q amplpy
# Google Colab & Kaggle integration
from amplpy import AMPL, ampl_notebook
ampl = ampl_notebook(
modules=["coin"], # modules to install
license_uuid="default", # license to use
) # instantiate AMPL object and register magics
Example: diet#
Generated using diet.mod, diet.dat, and diet.run.
Consider the problem of choosing prepared foods to meet certain nutritional requirements.
Sets:
NUTR
: set of nutrients to considerFOOD
: set of food to consider
Parameters:
cost {FOOD}
: cost of each foodf_min {FOOD}
: minimum amount of food to buyf_max {FOOD}
: maximum amount of food to buyn_min {NUTR}
: minimum amount required of each nutrientn_max {NUTR}
: maximum amount allowed of each nutrientamt {NUTR, FOOD}
: amount of each nutrient in each food
Variables:
Buy {FOOD}
: amount of food to buy
Objective:
Total_Cost
: total cost of the diet
Constraints:
Diet {NUTR}
: ensure that the nutritional requirements are satisfied by the diet.
%%writefile diet.mod
set NUTR;
set FOOD;
param cost {FOOD} > 0;
param f_min {FOOD} >= 0;
param f_max {j in FOOD} >= f_min[j];
param n_min {NUTR} >= 0;
param n_max {i in NUTR} >= n_min[i];
param amt {NUTR,FOOD} >= 0;
var Buy {j in FOOD} >= f_min[j], <= f_max[j];
minimize Total_Cost: sum {j in FOOD} cost[j] * Buy[j];
subject to Diet {i in NUTR}:
n_min[i] <= sum {j in FOOD} amt[i,j] * Buy[j] <= n_max[i];
%%writefile diet.dat
data;
set NUTR := A B1 B2 C ;
set FOOD := BEEF CHK FISH HAM MCH MTL SPG TUR ;
param: cost f_min f_max :=
BEEF 3.19 0 100
CHK 2.59 0 100
FISH 2.29 0 100
HAM 2.89 0 100
MCH 1.89 0 100
MTL 1.99 0 100
SPG 1.99 0 100
TUR 2.49 0 100 ;
param: n_min n_max :=
A 700 10000
C 700 10000
B1 700 10000
B2 700 10000 ;
param amt (tr):
A C B1 B2 :=
BEEF 60 20 10 15
CHK 8 0 20 20
FISH 8 10 15 10
HAM 40 40 35 10
MCH 15 35 15 15
MTL 70 30 15 15
SPG 25 50 25 15
TUR 60 20 15 10 ;
%%ampl_eval
model diet.mod;
data diet.dat;
option solver cbc;
solve;
display Buy;