examples/zebra.mod
author Alpar Juttner <alpar@cs.elte.hu>
Mon, 06 Dec 2010 13:09:21 +0100
changeset 1 c445c931472f
permissions -rw-r--r--
Import glpk-4.45

- Generated files and doc/notes are removed
alpar@1
     1
/* ZEBRA, Who Owns the Zebra? */
alpar@1
     2
alpar@1
     3
/* Written in GNU MathProg by Andrew Makhorin <mao@gnu.org> */
alpar@1
     4
alpar@1
     5
########################################################################
alpar@1
     6
#  The Zebra Puzzle is a well-known logic puzzle.
alpar@1
     7
#
alpar@1
     8
#  It is often called "Einstein's Puzzle" or "Einstein's Riddle"
alpar@1
     9
#  because it is said to have been invented by Albert Einstein as a boy,
alpar@1
    10
#  with the common claim that Einstein said "only 2 percent of the
alpar@1
    11
#  world's population can solve". It is also sometimes attributed to
alpar@1
    12
#  Lewis Carroll. However, there is no known evidence for Einstein's or
alpar@1
    13
#  Carroll's authorship.
alpar@1
    14
#
alpar@1
    15
#  There are several versions of this puzzle. The version below is
alpar@1
    16
#  quoted from the first known publication in Life International
alpar@1
    17
#  magazine on December 17, 1962.
alpar@1
    18
#
alpar@1
    19
#   1. There are five houses.
alpar@1
    20
#   2. The Englishman lives in the red house.
alpar@1
    21
#   3. The Spaniard owns the dog.
alpar@1
    22
#   4. Coffee is drunk in the green house.
alpar@1
    23
#   5. The Ukrainian drinks tea.
alpar@1
    24
#   6. The green house is immediately to the right of the ivory house.
alpar@1
    25
#   7. The Old Gold smoker owns snails.
alpar@1
    26
#   8. Kools are smoked in the yellow house.
alpar@1
    27
#   9. Milk is drunk in the middle house.
alpar@1
    28
#  10. The Norwegian lives in the first house.
alpar@1
    29
#  11. The man who smokes Chesterfields lives in the house next to the
alpar@1
    30
#      man with the fox.
alpar@1
    31
#  12. Kools are smoked in the house next to the house where the horse
alpar@1
    32
#      is kept.
alpar@1
    33
#  13. The Lucky Strike smoker drinks orange juice.
alpar@1
    34
#  14. The Japanese smokes Parliaments.
alpar@1
    35
#  15. The Norwegian lives next to the blue house.
alpar@1
    36
#
alpar@1
    37
#  Now, who drinks water? Who owns the zebra?
alpar@1
    38
#
alpar@1
    39
#  In the interest of clarity, it must be added that each of the five
alpar@1
    40
#  houses is painted a different color, and their inhabitants are of
alpar@1
    41
#  different national extractions, own different pets, drink different
alpar@1
    42
#  beverages and smoke different brands of American cigarettes. One
alpar@1
    43
#  other thing: In statement 6, right means your right.
alpar@1
    44
#
alpar@1
    45
#  (From Wikipedia, the free encyclopedia.)
alpar@1
    46
########################################################################
alpar@1
    47
alpar@1
    48
set HOUSE := { 1..5 };
alpar@1
    49
alpar@1
    50
set COLOR := { "blue", "green", "ivory", "red", "yellow" };
alpar@1
    51
alpar@1
    52
set NATIONALITY := { "Englishman", "Japanese", "Norwegian", "Spaniard",
alpar@1
    53
      "Ukranian" };
alpar@1
    54
alpar@1
    55
set DRINK := { "coffee", "milk", "orange_juice", "tea", "water" };
alpar@1
    56
alpar@1
    57
set SMOKE := { "Chesterfield", "Kools", "Lucky_Strike", "Old_Gold",
alpar@1
    58
      "Parliament" };
alpar@1
    59
alpar@1
    60
set PET := { "dog", "fox", "horse", "snails", "zebra" };
alpar@1
    61
alpar@1
    62
var color{HOUSE, COLOR}, binary;
alpar@1
    63
c1{h in HOUSE}: sum{c in COLOR} color[h,c] = 1;
alpar@1
    64
c2{c in COLOR}: sum{h in HOUSE} color[h,c] = 1;
alpar@1
    65
alpar@1
    66
var nationality{HOUSE, NATIONALITY}, binary;
alpar@1
    67
n1{h in HOUSE}: sum{n in NATIONALITY} nationality[h,n] = 1;
alpar@1
    68
n2{n in NATIONALITY}: sum{h in HOUSE} nationality[h,n] = 1;
alpar@1
    69
alpar@1
    70
var drink{HOUSE, DRINK}, binary;
alpar@1
    71
d1{h in HOUSE}: sum{d in DRINK} drink[h,d] = 1;
alpar@1
    72
d2{d in DRINK}: sum{h in HOUSE} drink[h,d] = 1;
alpar@1
    73
alpar@1
    74
var smoke{HOUSE, SMOKE}, binary;
alpar@1
    75
s1{h in HOUSE}: sum{s in SMOKE} smoke[h,s] = 1;
alpar@1
    76
s2{s in SMOKE}: sum{h in HOUSE} smoke[h,s] = 1;
alpar@1
    77
alpar@1
    78
var pet{HOUSE, PET}, binary;
alpar@1
    79
p1{h in HOUSE}: sum{p in PET} pet[h,p] = 1;
alpar@1
    80
p2{p in PET}: sum{h in HOUSE} pet[h,p] = 1;
alpar@1
    81
alpar@1
    82
/* the Englishman lives in the red house */
alpar@1
    83
f2{h in HOUSE}: nationality[h,"Englishman"] = color[h,"red"];
alpar@1
    84
alpar@1
    85
/* the Spaniard owns the dog */
alpar@1
    86
f3{h in HOUSE}: nationality[h,"Spaniard"] = pet[h,"dog"];
alpar@1
    87
alpar@1
    88
/* coffee is drunk in the green house */
alpar@1
    89
f4{h in HOUSE}: drink[h,"coffee"] = color[h,"green"];
alpar@1
    90
alpar@1
    91
/* the Ukrainian drinks tea */
alpar@1
    92
f5{h in HOUSE}: nationality[h,"Ukranian"] = drink[h,"tea"];
alpar@1
    93
alpar@1
    94
/* the green house is immediately to the right of the ivory house */
alpar@1
    95
f6{h in HOUSE}:
alpar@1
    96
   color[h,"green"] = if h = 1 then 0 else color[h-1,"ivory"];
alpar@1
    97
alpar@1
    98
/* the Old Gold smoker owns snails */
alpar@1
    99
f7{h in HOUSE}: smoke[h,"Old_Gold"] = pet[h,"snails"];
alpar@1
   100
alpar@1
   101
/* Kools are smoked in the yellow house */
alpar@1
   102
f8{h in HOUSE}: smoke[h,"Kools"] = color[h,"yellow"];
alpar@1
   103
alpar@1
   104
/* milk is drunk in the middle house */
alpar@1
   105
f9: drink[3,"milk"] = 1;
alpar@1
   106
alpar@1
   107
/* the Norwegian lives in the first house */
alpar@1
   108
f10: nationality[1,"Norwegian"] = 1;
alpar@1
   109
alpar@1
   110
/* the man who smokes Chesterfields lives in the house next to the man
alpar@1
   111
   with the fox */
alpar@1
   112
f11{h in HOUSE}:
alpar@1
   113
   (1 - smoke[h,"Chesterfield"]) +
alpar@1
   114
   (if h = 1 then 0 else pet[h-1,"fox"]) +
alpar@1
   115
   (if h = 5 then 0 else pet[h+1,"fox"]) >= 1;
alpar@1
   116
alpar@1
   117
/* Kools are smoked in the house next to the house where the horse is
alpar@1
   118
   kept */
alpar@1
   119
f12{h in HOUSE}:
alpar@1
   120
   (1 - smoke[h,"Kools"]) +
alpar@1
   121
   (if h = 1 then 0 else pet[h-1,"horse"]) +
alpar@1
   122
   (if h = 5 then 0 else pet[h+1,"horse"]) >= 1;
alpar@1
   123
alpar@1
   124
/* the Lucky Strike smoker drinks orange juice */
alpar@1
   125
f13{h in HOUSE}: smoke[h,"Lucky_Strike"] = drink[h,"orange_juice"];
alpar@1
   126
alpar@1
   127
/* the Japanese smokes Parliaments */
alpar@1
   128
f14{h in HOUSE}: nationality[h,"Japanese"] = smoke[h,"Parliament"];
alpar@1
   129
alpar@1
   130
/* the Norwegian lives next to the blue house */
alpar@1
   131
f15{h in HOUSE}:
alpar@1
   132
   (1 - nationality[h,"Norwegian"]) +
alpar@1
   133
   (if h = 1 then 0 else color[h-1,"blue"]) +
alpar@1
   134
   (if h = 5 then 0 else color[h+1,"blue"]) >= 1;
alpar@1
   135
alpar@1
   136
solve;
alpar@1
   137
alpar@1
   138
printf "\n";
alpar@1
   139
printf "HOUSE  COLOR   NATIONALITY  DRINK         SMOKE         PET\n";
alpar@1
   140
for {h in HOUSE}
alpar@1
   141
{  printf "%5d", h;
alpar@1
   142
   printf{c in COLOR: color[h,c]} "  %-6s", c;
alpar@1
   143
   printf{n in NATIONALITY: nationality[h,n]} "  %-11s", n;
alpar@1
   144
   printf{d in DRINK: drink[h,d]} "  %-12s", d;
alpar@1
   145
   printf{s in SMOKE: smoke[h,s]} "  %-12s", s;
alpar@1
   146
   printf{p in PET: pet[h,p]} "  %-6s", p;
alpar@1
   147
   printf "\n";
alpar@1
   148
}
alpar@1
   149
printf "\n";
alpar@1
   150
alpar@1
   151
end;