Newsgroups: comp.lang.prolog
Path: cantaloupe.srv.cs.cmu.edu!rochester!udel!gatech!howland.reston.ans.net!math.ohio-state.edu!jussieu.fr!pasteur.fr!news2.EUnet.fr!news.fnet.fr!ilog!puget
From: puget@ilog.ilog.fr (Jean-Francois Puget)
Subject: Re: Zebra puzzle
Message-ID: <1995Mar4.084059.3895@ilog.fr>
To: Elliot Smith <E.Smith-MSCAI94@cs.bham.ac.uk>
Sender: news@ilog.fr
Nntp-Posting-Host: laumiere
Organization: ILOG S.A., France
References:  <3j1ve4$gal@percy.cs.bham.ac.uk>
Date: Sat, 4 Mar 95 08:40:59 GMT
Lines: 81

In article <3j1ve4$gal@percy.cs.bham.ac.uk>, Elliot Smith <E.Smith-MSCAI94@cs.bham.ac.uk> writes:
|> Can anyone give me an idea of where to find code for solving the 
|> zebra puzzle, or for solving general 'logic puzzles'?  
|> 
|> Personal replies preferred: E.Smith-MSCAI94@cs.bham.ac.uk
|> 
|> Thanks.
|> Elliot Smith

Constraint (Logic) Programming (CLP) is a very nice way to solve such problems.

I give below the Ilog Solver code needed to solve the zebra problem. 
Ilog Solver is a C++ contraint programming library, that implements Constraint
Logic Programming concepts. If you want to know more about this library, 
some scientific papers are available on our web server: 
http://www.ilog.fr 

select Solver in the product section, then select papers.

More general information about CLP is discussed in the
comp.constraints newsgroup.

// -------------------------------------------------------------- -*- C++ -*-
// File: zebra.cc
// --------------------------------------------------------------------------
 
#include <iostream.h>
#include <ilsolver/ctint.h>

void NextTo (const IlcIntExp v1, const IlcIntExp v2) {
    // v2 - v1 is either -1 or 1
    IlcTell(IlcAbs(v2-v1) == 1);
}

main (){
    IlcInit();

    IlcIntVar england (0,4), spain(0,4), japan(0,4), italy(0,4), norway(0,4);
    IlcIntVar dog(0,4), zebra(0,4), fox(0,4), snails(0,4), horse(0,4);
    IlcIntVar juice(0,4), water(0,4), tea(0,4), coffee(0,4), milk(0,4);
    IlcIntVar painter(0,4), diplomat(0,4), violonist(0,4), doctor(0,4), sculptor(0,4);
    IlcIntVar green(0,4), red(0,4), yellow(0,4), blue(0,4), white(0,4);

    IlcIntExpArray  allvars(25,
			england, spain, japan, italy, norway, 
			dog, zebra, fox, snails, horse, 
			juice, water, tea, coffee, milk, 
			painter, diplomat, violonist, doctor, sculptor, 
			green, red, yellow, blue, white);

    IlcTell(IlcAllDiff(allvars.extract(0, 5)));  // england, spain, japan, italy, norway
    IlcTell(IlcAllDiff(allvars.extract(5, 5)));  // dog, zebra, fox, snails, horse
    IlcTell(IlcAllDiff(allvars.extract(10, 5))); // juice, water, tea, coffee, milk
    IlcTell(IlcAllDiff(allvars.extract(15, 5))); // painter, diplomat, violonist, doctor, sculptor
    IlcTell(IlcAllDiff(allvars.extract(20, 5))); // green, red, yellow, blue, white

    IlcTell(red ==       england);
    IlcTell(spain ==     dog);
    IlcTell(japan ==     painter);
    IlcTell(italy ==     tea);
    IlcTell(green ==     coffee);
    IlcTell(sculptor ==  snails);
    IlcTell(diplomat ==  yellow);
    IlcTell(violonist == juice);
    IlcTell(milk ==      2);
    IlcTell(norway ==    0);
    IlcTell(green ==    white + 1);
    NextTo(norway,  blue);
    NextTo(fox,     doctor);
    NextTo(horse,   diplomat);
    IlcSolve (IlcGenerate(allvars));
    cout << "Zebra is in house: " << zebra.getValue() << endl;
    IlcEnd();
    return 0;
}

-- 
  Jean-Francois Puget		 net : puget@ilog.fr
  ILOG S.A.                      url : http://www.ilog.fr
  2 Avenue Gallieni - BP 85	 tel : +33 1 46 63 66 66
  F-94253 Gentilly Cedex FRANCE	 fax : +33 1 46 63 15 82
