Pergunta

I need help with this exercise of Prolog:

% items
items (cell).
items (labial).
items (control).
items (mirror).

% Weight of each item
weight (cell 2).
weight (labial, 3).
weight (control, 5).
weight (mirror, 10).

capacity (X, Y, Z, V) :-
    weight (X C1), weight (Y, C2), weight (Z, C3), sum (C1, C2, C3, R), V> = R.

sum (X, Y, Z, K) :- K is X + Y + Z.

this program does is give me a combination of 3 items or less a given weight, eg capacity (X, Y, Z, 15).

result is, X: cell, Y: Lipstick, Z: mirror, X: control, Y: cell, Z: mirror. successively with all combinations where the sum of the 3 weight no higher input.

At the moment I am limited by the number of income variables manually, capacity (X, Y, Z, N. .......) I want that the combination with respect to number of items that are in the knowledge base, not manually enter the variables. How I can do that?

so would be ideal capacity (weight) and response.

the combination of items where the weight does not exceed

phone, lipstick, mirror. control labial phone. mirror, control, labilal .......

Sorry I do not speak English, I'm using google translator.

Foi útil?

Solução

It looks like the structure that you are looking for is a list and you should look it up ([HD:TL] where TL is a list). The solution I provide below should show how to use lists to the desired effect although my solution allows duplicates and doesn't care about the order of the list. If this is for homework, you should be able to figure out how to fix that on your own once you know a little about lists.

Basically, the first predicate handles making long lists of items, HD is an item with weight X, and it looks for a predicate to define the tail (TL) list; the second handles the end of a list (TL is [] in the previous inductive step) and empty lists (because even empty lists need love too).

items(cell).
items(labial).
items(control).
items(mirror).
weight(cell,2).
weight(labial,3).
weight(control,5).
weight(mirror,10).
capacity(W, [HD|TL]) :- items(HD),weight(HD,X),W>0,capacity(W-X,TL). 
capacity(W, []) :- W>=0.
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top