/* Buying groceries in Picat. From "New-agey interviews at the grocery startup " https://www.jasq.org/just-another-scala-quant/new-agey-interviews-at-the-grocery-startup """ So you walk into a grocery store with a grocery bag and some cash, to buy groceries for a week. Now, a couple problems - 1. your bag can hold ten pounds. 2. You have $100 3. You need about 2000 calories a day, so a weekly shopping trip is about 14,000 calories. 4. You must purchase at least 4 ounces of each grocery item. Here's your dataset - --- Calories Per Pound --- Ham, 650 cals, Lettuce, 70 cals Cheese, 1670 cals Tuna, 830 cals Bread, 1300 cals ---- ---- Price Per Pound ---- Ham, $4 Lettuce, $1.5 Cheese, $5 Tuna, $20 Bread, $1.20 ---- Take your time, and list the number of ways you can buy your groceries. """ This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ % import util. import mip. main => go. go ?=> data(Items,Calories,Price,MaxPrice,MaxCalories,MaxWeight,MinPoundPerItem), grocery(Items,Calories,Price,MaxPrice,MaxCalories,MaxWeight,MinPoundPerItem, X,TotPrice,TotCalories,TotWeight), fail, nl. go => true. grocery(Items,Calories,Price,MaxPrice,MaxCalories,MaxWeight,MinPoundPerItem, X,TotPrice,TotCalories,TotWeight) => println($grocery(Items,Calories,Price,maxPrice=MaxPrice,maxCalories=MaxCalories,maxWeight=MaxWeight,minPoundPerItem=MinPoundPerItem, X,TotPrice,TotCalories,TotWeight)), N = Items.len, % decision variables X = new_list(N), % number of pounds per item X :: MinPoundPerItem..10000.0, % scalar_product(X,Calories,TotCalories), TotCalories #= sum([X[I]*Calories[I] : I in 1..N]), TotCalories #<= MaxCalories, TotPrice #= sum([X[I]*Price[I] : I in 1..N]), TotPrice #<= MaxPrice, TotWeight #= sum(X), TotWeight #<= MaxWeight, Vars = X ++ [TotCalories,TotPrice,TotWeight], % solve($[max(TotWeight)],Vars), solve($[],Vars), println(X), println(totPrice=TotPrice), println(totWeight=TotWeight), println(totCalories=TotCalories), nl. data(Items,Calories,Price,MaxPrice,MaxCalories,MaxWeight,MinPoundPerItem) => Items = [ham,lettuce,cheese,tuna,bread], Calories = [650,70,1670,830,1300], MaxCalories = 14000, % MaxWeight = 10, MinPoundPerItem = 0.25, MaxPrice = 100, % dollar Price = [4,1.5,5,20,1.20]. % Dollar per pound