0

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.

false
  • 10,264
  • 13
  • 101
  • 209
Marztres
  • 460
  • 8
  • 15
  • 2
    can you change the title to something meaningful? – Woot4Moo May 23 '13 at 18:05
  • 2
    If you did try, then show us how you've tried. As it is now it sounds like "will you guys please do my homework for me". – Geeky Guy May 23 '13 at 18:08
  • Wholesale replacement of the original question with a new one. Classy. – Daniel Lyons May 23 '13 at 21:36
  • Is the same question but with a different example and explained the title more. – Marztres May 23 '13 at 21:38
  • We understood perfectly before, we just wanted to see how far you had gotten on your own. It's not cheating to point out a few mistakes, but a complete from-scratch solution is, and it's more work too. As Renan points out, we aren't here to do homework for people. You also just threw away work by Diego Sevilla to turn your unwieldy prose into something easily understood. Now someone will have to do that again with your new question. This is not a good way to make friends. – Daniel Lyons May 23 '13 at 21:43
  • @DanielLyons The truth is the first time I put a question here, is well aware that the idea is not to work and that's not what I want I'm just standing in this exercise because eh attempted solutions have not worked. The truth is that eh tried to solve the problem. – Marztres May 23 '13 at 21:56

1 Answers1

1

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.
  • thank you very much, I did something where the predicate called by recursion ability but he passed as a parameter the above item and all I did was an infinite loop, I thanks your help if you had used lists but did not know well how to apply. if investigare that does not result in duplicate items. – Marztres May 25 '13 at 04:40
  • 1
    Did you ever figure out how to get it to work without duplicates? – Spencer Ewall May 29 '13 at 22:24