/*********************************************************** diet.pi from Constraint Solving and Planning with Picat, Springer by Neng-Fa Zhou, Hakan Kjellerstrand, and Jonathan Fruhman ***********************************************************/ import mip. main => data(Prices,Limits,{Calories,Chocolate,Sugar,Fat}), Len = length(Prices), Xs = new_array(Len), Xs :: 0..10, scalar_product(Calories,Xs, #>=,Limits[1]), % 500 scalar_product(Chocolate,Xs,#>=,Limits[2]), % 6 scalar_product(Sugar,Xs, #>=,Limits[3]), % 10 scalar_product(Fat,Xs, #>=,Limits[4]), % 8 scalar_product(Prices,Xs,XSum), % to minimize solve(\$[min(XSum)],Xs), writeln(Xs). % plain scalar product scalar_product(A,Xs,Product) => Product #= sum([A[I]*Xs[I] : I in 1..A.length]). scalar_product(A,Xs,Rel,Product) => scalar_product(A,Xs,P), call(Rel,P,Product). data(Prices,Limits,Nutrition) => % prices in cents for each product Prices = {50,20,30,80}, % required amount for each nutrition type Limits = {500,6,10,8}, % nutrition for each product Nutrition = {{400,200,150,500}, % calories { 3, 2, 0, 0}, % chocolate { 2, 2, 4, 4}, % sugar { 2, 4, 1, 5}}. % fat