How to sum in Clingo?

3.8k Views Asked by At

I have the following dataset:

food_a(bagel, 245).
food_a(sandwich, 200).
food_a(salad,300).
food(bagel).
food(sandwich).
food(salad).

I want to satisfy the following constraint: Given a total calorie count, I want to return the food items that satisfy that. For eg. total calorie count = 500, the program should return 'bagel+sandwich' as the solution. I coded he following clingo code:

food_a(bagel, 245).
food_a(sandwich, 200).
food_a(salad,300).
food(bagel).
food(sandwich).
food(salad).
has(bagel, wheat).
has(sandwich, bread).
has(sandwich, tomatoes).
has(sandwich, onion).
has(sandwich, cheese).
%calories(food,amount):-food_a(food,amount).
%food(F):-food_a(F,C).
%limits(calories,200).

%sol(F) :- food_a(F,C1),food_a(F,C2), C1+C2<500.
%:- {food(F,C) : food_a(F,C1),food_a(F,C2)} , C1+C2 >500.

%food_diet(F) :- food(F,C), C<250.
%:- food(F1) ,food_a(F2,C2), C1+C2=445.

totals(P, S) :- S = #sum{ I : food_a(P,I)}, food(P), S<500.

The output I'm getting is in the screenshot: enter image description here

Apparently, the program is returning jsut single food items, and not considering the combinations of 2 or 3 of them at a time. Can anyone suggest the changes or steps I must follow to achieve the same.

1

There are 1 best solutions below

0
On BEST ANSWER

You are currently doing the sum for each food separately. If you had multiple values for the same food, e.g. food_a(bagel, 100) and food_a(bagel, 200) then the result would be total(bagel, 300). Essentially since you have only one food_a/2 for each food, your totals/2 definition is equivalent to

totals(P, S) :- food_a(P, S), food(P), S<500.

What you want is something like

food_a(bagel, 245).
food_a(sandwich, 200).
food_a(salad,300).
food(bagel).
food(sandwich).
food(salad).

% allow any combination of foods to be selected
{ selected(P) } :- food(P).
% sum calories on selected foods
total(S) :- S = #sum{ I : food_a(P,I), selected(P) }.
% limit total calories
:- total(S), S>=500.

#show selected/1.
#show total/1.

Which yields

> clingo how-to-sum-clingo.asp 0
clingo version 4.5.4
Reading from how-to-sum-clingo.asp
Solving...
Answer: 1
total(0)
Answer: 2
selected(sandwich) total(200)
Answer: 3
selected(bagel) total(245)
Answer: 4
selected(bagel) selected(sandwich) total(445)
Answer: 5
selected(salad) total(300)
SATISFIABLE

Models       : 5
Calls        : 1
Time         : 0.002s (Solving: 0.00s 1st Model: 0.00s Unsat: 0.00s)
CPU Time     : 0.000s