0

I need your help in solving the following problem:

There're 3 girls (Ann,Susan,Alice) who need to choose what color shoes and dress to wear. There're 3 possible colors for shoes and dresses: white, blue and green.

Main conditions:

  • Ann hates white.
  • Susan wears shoes and dress of same color.
  • Alice has white shoes.
  • Alice and Ann's shoes and dress have different colors.

My code satisfies only 2 conditions; I'm kinda having hard times meeting conditions on the same color for Susan and whereas other girls need to have different color garments.

Here's what I come up with:

PREDICATES
   girl(symbol)
   shoes(symbol,symbol)
   skirt(symbol,symbol)
   hates(symbol,symbol)
   will_wear(symbol, symbol, symbol)


CLAUSES
   will_wear(X,Y,Z):-
      girl(X),
      shoes(X,Y),
      skirt(X,Z),
      not(hates(X,Y)),
      not(hates(X,Z)).

   girl(ann).
   girl(susan).
   girl(alice).

   hates(ann,white).

   skirt(_,white).
   skirt(_,blue).
   skirt(_,green).

   shoes(alice,white).
   shoes(_,blue).
   shoes(_,green).

GOAL
   will_wear(Name,Shoes,Dress).

Code above works fine, but gives too many solutions. Plus, I couldn't come up with any logical solution for the condition for Susan to wear shoes and dress of same color.

Thanks.

false
  • 10,264
  • 13
  • 101
  • 209
Luke
  • 37
  • 9
  • 1
    Is this homework? It's ok if it is, just let us know and tell us what you've tried so far.... – Matthew Oct 06 '11 at 16:34
  • It actually is. The code above is the thing I came up with myself. Teacher gave us only conditions. I think main problem is the logic of solution. I've already been thinking on this for 2 hours. Kinda makes me wanna laugh at myself for that :) – Luke Oct 06 '11 at 16:37
  • Don't feel bad. I spent way more than 2 hours on my first 'simple' prolog assignment. Keep at it. It's a different way of thinking. I'd love to help you, but all that knowledge has left my brain years ago... :) – i_am_jorf Oct 06 '11 at 16:45
  • Could you indent your will_wear clause? – Ehtesh Choudhury Oct 06 '11 at 16:54

2 Answers2

1

Off the top of my head, I'm thinking something along these lines:

only_wears(Girl,Color):-
    shoes(Girl, Color),
    skirt(Girl, Color).

different_shoes(F, S):-
    shoes(F,F_color),
    shoes(S,S_color),
    not(equals(F_color,S_color)).

different_skirts(F, S):-
    skirt(F,F_color),
    skirt(S,S_color),
    not(equals(F_color,S_color)).

I do wonder if there's a way to pass clauses to other clauses, because different_shoes and different_skirts are identical in structure.

You would initialize it like so:

only_wears(ann, white).
different_shoes(alice, ann).
different_skirt(alice, ann).
Ehtesh Choudhury
  • 7,452
  • 5
  • 42
  • 48
1

If I understand the conditions correctly, they're not what Shurane answered.

This will make sure that a girl wears a dress and shoes with the same colour:

same_color(Girl) :-
    shoes(Girl, Color),
    dress(Girl, Color).

I'll leave the different colour one as an exercise, but hint that to say two things are not the same you say A \= B. Please leave a comment if you have a hard time with different_color - and tell me what you've tried.

configurator
  • 40,828
  • 14
  • 81
  • 115
  • I found a solution for similar problem online. Like I said logic there is a bit different. Thank you all for your help. Will try to develop my version of solution though :) – Luke Oct 06 '11 at 18:05
  • idea is that for similar colors we use correspond clause correspond(X,Y,Z):-girl(X),shoes(Y),dress(Z), X=susan,Y=green,Y<>Z. And that should be customized for each of the girls. Then there's solution clause: solution(X1,Y1,Z1,X2,Y2,Z2,X3,Y3,Z3):- X1=susan,correspond(X1,Y1,Z1), X2=alice, correspond(X2,Y2,Z2), X3=ann, correspond(X3,Y3,Z3), Y1<>Y2, Y2<>Y3, Y1<>Y3, Z1<>Z2, Z2<>Z3, Z1<>Z3. – Luke Oct 06 '11 at 18:07
  • Isn't that the same as `only_wears` in my code? Also, `dress()` ought to be `skirt()`. – Ehtesh Choudhury Oct 06 '11 at 18:07
  • I think what configurator meant was that clause's logic has to be bind to "color" attribute – Luke Oct 06 '11 at 18:10
  • Ooh, I see. Although, that still seems a lot like `only_wears(Girl, _)` – Ehtesh Choudhury Oct 06 '11 at 18:40
  • @Shurane: Yes, it _does_ the same thing. But it doesn't _mean_ the same thing. The important thing in software development in general and Prolog in particular is that the meaning are all correct. Everything else follows from that. – configurator Oct 06 '11 at 21:35