port of PYSWIP package.

This commit is contained in:
Vítor Santos Costa
2010-06-01 00:33:32 +01:00
parent 0645040602
commit e94104306c
27 changed files with 2502 additions and 0 deletions

View File

@@ -0,0 +1,18 @@
% This example is adapted from http://eclipse.crosscoreop.com/examples/puzzle1.pl.txt
:- use_module(library('clpfd')).
solve(Board) :-
Board = [NW,N,NE,W,E,SW,S,SE],
domains(Board,0..12),
sum(Board, #=, 12),
NW + N + NE #= 5,
NE + E + SE #= 5,
NW + W + SW #= 5,
SW + S + SE #= 5,
label(Board).
domains([],_).
domains([Pos|Board],D) :-
Pos in D,
domains(Board,D).

View File

@@ -0,0 +1,34 @@
# -*- coding: utf-8 -*-
# This example is adapted from http://eclipse.crosscoreop.com/examples/puzzle1.pl.txt
# "Twelve draught pieces are arranged in a square frame with four on
# each side. Try placing them so there are 5 on each side. (Kordemsky)
#
# "Maybe this problem is not described very well but I wanted to stick
# with the original text from Kordemsky. The problem may be stated in
# terms of guards on the wall of a square fort. If a guard stands on a
# side wall then he may only watch that particular wall whereas a guard
# at a corner may watch two walls. If twelve guards are positioned such
# that there are two on each side wall and one at each corner then there
# are four guards watching each wall. How can they be rearranged such
# that there are five watching each wall?"
from pyswip.prolog import Prolog
def main():
prolog = Prolog()
prolog.consult("puzzle1.pl")
for soln in prolog.query("solve(B)."):
#B = eval(soln["B"])
B = soln["B"]
# [NW,N,NE,W,E,SW,S,SE]
print "%d %d %d" % tuple(B[:3])
print "%d %d" % tuple(B[3:5])
print "%d %d %d" % tuple(B[5:])
cont = raw_input("Press 'n' to finish: ")
if cont.lower() == "n": break
if __name__ == "__main__":
main()