Skip to content

Commit 6571564

Browse files
author
Simon
committed
added 2nd assignment AI
1 parent 66ce657 commit 6571564

File tree

7 files changed

+83
-0
lines changed

7 files changed

+83
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
gtp(1,1,1). gtp(2,1,2). gtp(3,1,3). gtp(4,2,3). gtp(5,3,3). gtp(6,3,2). gtp(7,3,1). gtp(8,2,1). gblnk(2,1).
2+
tp(1,1,2). tp(2,1,3). tp(3,2,1). tp(4,2,3). tp(5,3,3). tp(6,2,2). tp(7,3,2). tp(8,1,1). blnk(3,1).
3+
4+
go:- calcH(1,[],L), sumList(L,V),write('Heuristics: '),write(V).
5+
calcH(9,X,X):-!.
6+
calcH(T,X,Y):- dist(T,D), append(X,[D],X1), T1 is T+1, calcH(T1,X1,Y).
7+
dist(T,V):-tp(T,A,B), gtp(T,C,D), V is abs(A-C) + abs(B-D).
8+
sumList([],0):-!.
9+
sumList(L,V):-L=[H|T], sumList(T,V1), V is V1+H.
10+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
gtp=[(1,1,1), (2,1,2), (3,1,3), (4,2,3), (5,3,3), (6,3,2), (7,3,1), (8,2,1)]
2+
gblnk = (2,1)
3+
tp=[(1,1,2), (2,1,3), (3,2,1), (4,2,3), (5,3,3), (6,2,2), (7,3,2), (8,1,1)]
4+
blnk = (3,1)
5+
6+
# Procedure to find the manhattan distance
7+
i,h=0,0
8+
while(i<=7):
9+
h = h + abs(tp[i][1] - gtp[i][1]) + abs(tp[i][2] - gtp[i][2])
10+
i=i+1
11+
print('Heuristics : ',h)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
queens = [(1, 2), (1, 8), (3, 6), (4, 5), (5, 3), (6, 1), (7, 4), (8, 7)]
2+
3+
count = 0
4+
for i in range(8):
5+
for j in range(8):
6+
#checking in right side
7+
if queens[j][1] > queens[i][1] :
8+
#checking horizontally
9+
if queens[i][0] == queens[j][0]:
10+
count += 1
11+
#checking diagonally
12+
elif abs(queens[j][0] - queens[i][0]) == abs(queens[j][1] - queens[i][1]):
13+
count += 1
14+
15+
print('Heuristics : ', count)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
neighbor(i,a,35). neighbor(i,b,45). neighbor(a,c,20).
2+
neighbor(a,d,30). neighbor(b,d,25). neighbor(b,e,35).
3+
neighbor(b,f,27). neighbor(c,d,30). neighbor(c,g,47).
4+
neighbor(d,g,30). neighbor(e,g,25).
5+
6+
pathLength(X,Y,L):- neighbor(X,Y,L),!.
7+
pathLength(X,Y,L):- neighbor(X,Z,L1), pathLength(Z,Y,L2), L is L1+L2.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
def pathLength(x, y):
2+
i = 0
3+
s = 0
4+
for i in range(len(neighbour)):
5+
if neighbour[i][0] == x:
6+
z = neighbour[i][1]
7+
if z == y:
8+
return neighbour[i][2]
9+
elif pathLength(z, y) != -1:
10+
return neighbour[i][2] + pathLength(z, y)
11+
else:
12+
continue
13+
return -1
14+
15+
16+
#main
17+
18+
neighbour = [('i', 'a', 35), ('i', 'b', 45), ('a', 'c', 20), ('a', 'd', 30),
19+
('b', 'd', 25), ('b', 'e', 35), ('b', 'f', 27), ('c', 'd', 30),
20+
('c', 'g', 47), ('d', 'g', 30), ('e', 'g', 25)]
21+
22+
print("input the vertices : ")
23+
x = input()
24+
y = input()
25+
print(pathLength(x, y))
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ssum(0, _, _, 0):-!.
2+
ssum(N, I, F, S) :- N1 is N-1, ssum(N1, I, F, S1), S is S1+F+(N-1) * I.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
def ssum(N,I,F):
2+
if (N==0):
3+
return 0
4+
elif (N>=1):
5+
return ssum(N-1,I,F)+F+(N-1)*I
6+
# Main
7+
t=int(input('How many times?'))
8+
for i in range(t):
9+
print('Iteration:',i+1)
10+
f=int(input('First element:'))
11+
d=int(input('Interval:'))
12+
n=int(input('n:'))
13+
print('Series sum:', ssum(n,d,f))

0 commit comments

Comments
 (0)