人工智能homework
Ships Puzzle - Solution
The Puzzle:
There are 5 ships in a port:
1. The Greek ship leaves at six and carries coffee.
2. The Ship in the middle has a black exterior.
3. The English ship leaves at nine.
4. The French ship with blue exterior is to the left of a ship that carries coffee.
5. To the right of the ship carrying cocoa is a ship going to Marseille.
6. The Brazilian ship is heading for Manila.
7. Next to the ship carrying rice is a ship with a green exterior.
8. A ship going to Genoa leaves at five.
9. The Spanish ship leaves at seven and is to the right of the ship going to Marseille.
10. The ship with a red exterior goes to Hamburg.
11. Next to the ship leaving at seven is a ship with a white exterior.
12. The ship on the border carries corn.
13. The ship with a black exterior leaves at eight.
14. The ship carrying corn is anchored next to the ship carrying rice.
15. The ship to Hamburg leaves at six.
Which ship goes to Port Said? Which ship carries tea?
1. The Greek ship leaves at six and carries coffee.
2. The Ship in the middle has a black exterior.
3. The English ship leaves at nine.
4. The French ship with blue exterior is to the left of a ship that carries coffee.
5. To the right of the ship carrying cocoa is a ship going to Marseille.
6. The Brazilian ship is heading for Manila.
7. Next to the ship carrying rice is a ship with a green exterior.
8. A ship going to Genoa leaves at five.
9. The Spanish ship leaves at seven and is to the right of the ship going to Marseille.
10. The ship with a red exterior goes to Hamburg.
11. Next to the ship leaving at seven is a ship with a white exterior.
12. The ship on the border carries corn.
13. The ship with a black exterior leaves at eight.
14. The ship carrying corn is anchored next to the ship carrying rice.
15. The ship to Hamburg leaves at six.
Which ship goes to Port Said? Which ship carries tea?
''' @Time : 2020/10/21 16:37 @Author : laolao @FileName: 1.py ''' from kanren import * from kanren.core import lall def example_1(): from kanren import run,eq,membero,var,conde from kanren import Relation,facts parent = Relation() facts(parent,("Homer","Bart"),("Homer","Lisa"),("Abe","Homer")) x = var() print(run(1,x,parent(x,"Bart"))) print(run(2,x,parent("Homer",x))) y = var() print(run(1,x,parent(x,y),parent(y,"Bart"))) def example_2(): ''' 查找素数 :return: ''' from kanren import isvar,run,membero from kanren.core import success,fail,goaleval,condeseq,eq,var from sympy.ntheory.generate import prime,isprime import itertools as it def prime_check(x): if isvar(x): return condeseq([(eq,x,p)] for p in map(prime,it.count(1))) else: return success if isprime(x) else fail x = var() print((set(run(0, x, (membero, x, (12, 14, 15, 19, 20, 21, 22, 23, 29, 30, 41, 44, 52, 62, 65, 85)), (prime_check, x))))) print((run(10,x,prime_check(x)))) def example_3(): def left(q,p,list): return membero((q,p),zip(list,list[1:])) def next(q,p,list): return conde([left(q,p,list)],[left(p,q,list)]) houses =var() rules_zebraproblem = lall( (eq, (var(), var(), var(), var(), var()), houses), # (membero, ('Englishman', var(), var(), var(), 'red'), houses), (membero, ('Swede', var(), var(), 'dog', var()), houses), (membero, ('Dane', var(), 'tea', var(), var()), houses), (left, (var(), var(), var(), var(), 'green'), (var(), var(), var(), var(), 'white'), houses), (membero, (var(), var(), 'coffee', var(), 'green'), houses), (membero, (var(), 'Pall Mall', var(), 'birds', var()), houses), (membero, (var(), 'Dunhill', var(), var(), 'yellow'), houses), (eq, (var(), var(), (var(), var(), 'milk', var(), var()), var(), var()), houses), (eq, (('Norwegian', var(), var(), var(), var()), var(), var(), var(), var()), houses), (next, (var(), 'Blend', var(), var(), var()),(var(), var(), var(), 'cats', var()), houses), (next, (var(), 'Dunhill', var(), var(), var()),(var(), var(), var(), 'horse', var()), houses), (membero, (var(), 'Blue Master', 'beer', var(), var()), houses), (membero, ('German', 'Prince', var(), var(), var()), houses), (next, ('Norwegian', var(), var(), var(), var()), (var(), var(), var(), var(), 'blue'), houses), (next, (var(), 'Blend', var(), var(), var()), (var(), var(), 'water', var(), var()), houses), (membero, (var(), var(), var(), 'zebra', var()), houses) ) solutions = run(0, houses, rules_zebraproblem) print(solutions) output_zebra = [house for house in solutions[0] if 'zebra' in house][0][0] print('\n' + output_zebra + ' owns zebra.') def Homework(): def left(q, p, list): return membero((q, p), zip(list, list[1:])) def right(q, p, list): return membero((p, q), zip(list, list[1:])) def next(q, p, list): return conde([left(q, p, list)], [left(p, q, list)]) ship = var() rules_zebraproblem = lall( (eq, (var(), var(), var(), var(), var()), ship), # 国家,时间,货物,烟囱颜色,目的地 # 希腊船六点离开,带着咖啡。 (membero, ('Greek', '6','coffee',var(),var()), ship), # 中间的船有一个黑色的烟囱。 (eq, (var(), var(), (var(), var(),var(), 'black', var()), var(), var()), ship), # 英国船在九点离开。 (membero, ('English', '9', var(), var(), var()), ship), # 带蓝色烟囱的法国船在一艘载有咖啡的船的左边。 (left, ('French', var(), var(), 'blue', var()),(var(), var(), 'coffee', var(), var()), ship), # 携带可可的船的右边是前往马赛的船。 (left, (var(), var(), var(),var(), 'Marseilles'), (var(), var(), 'cocoa', var(), var()), ship), # 巴西船正驶向马尼拉。 (membero, ('Brazilian', var(), var(), var(),'Manila'), ship), # 运送大米的船旁边是一个带有绿色烟囱的船。 (next, (var(), var(), 'rice', var(), var()),(var(), var(), var(), 'green', var()), ship), # 去热那亚的船离开五点。 (membero, (var(), '5', var(), var(), 'Genoa'), ship), # 这艘西班牙船在七点离开,在去马赛的船的右边。 (right, ('Spanish', '7', var(), var(), var()),(var(), var(), var(), var(), 'Marseilles'), ship), # 带着红色烟囱的船驶往汉堡。 (membero, (var(), var(), var(),'red', 'Hamburg'), ship), # 七点出发的船旁边是一艘白色的烟囱。 (next, (var(), '7', var(), var(), var()), (var(), var(), var(), 'white', var()), ship), # 边界上的船运载玉米。 # (eq, ((var(), var(), var(), 'corn', var()), var(), var(), var(), var()), ship), (eq, (var(), var(), var(), var(), (var(), var(), var(), 'corn', var())), ship), # 那个有黑烟囱的船在八点钟离开。 (membero, (var(), '8', var(), 'black', var(), ship), # 运载玉米的船靠在运送大米的船旁边。 (next, (var(), var(), 'corn', var(), var()),(var(), var(), 'rice', var(),var())), ship), # 去汉堡的船六点离开。 (membero, (var(), '6', var(),var(), 'Hamburg'), ship) ) solutions = run(0, ship, rules_zebraproblem) print(solutions) print(ship) # output_zebra = [s for s in solutions[0] if '塞得港' in ship][0][0] # print('\n' + output_zebra + ' owns 塞得港.') # output_zebra = [s for s in solutions[0] if '茶' in ship][0][0] # print('\n' + output_zebra + ' owns 茶.') if __name__ == '__main__': Homework()