-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hill.py
182 lines (115 loc) · 3.54 KB
/
Hill.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import math
import numpy as np
import random
def input_board(board):
for row in range(4):
for column in range(4):
board[row][column]=int(input())
return board
def calculate_cost(queens):
return (math.factorial(queens))/(2*math.factorial(queens-2))
def heuristic_cost(tboard):
h_cost=0
#Traverse Row
for row in range(len(tboard)):
queens=0
for col in range(len(tboard)):
if tboard[row][col]==1:
queens+=1
if queens>1:
h_cost+=calculate_cost(queens)
# Traverse Column
for col in range(len(tboard)):
queens=0
for row in range(len(tboard)):
if tboard[row][col]==1:
queens+=1
if queens>1:
h_cost+=calculate_cost(queens)
# Traverse Diagonals South-West to North-East
for i in range(1,len(tboard)):
row = i
queens = 0
for j in range(i+1):
if tboard[row][j]==1:
queens+=1
row-=1
if queens>1:
h_cost+=calculate_cost(queens)
for i in range(len(tboard)):
row = 3
queens=0
for j in range(i,4-i):
if tboard[row][j]==1:
queens+=1
row-=1
if queens>1:
h_cost+=calculate_cost(queens)
# Traverse Diagonals South-East to North-West
for i in range(2,-1,-1):
row = i
queens=0
for j in range(4-i):
if tboard[row][j]==1:
queens+=1
row+=1
if queens>1:
h_cost+=calculate_cost(queens)
for i in range(len(tboard)):
row = 0
queens=0
for j in range(i, 4-i):
if tboard[row][j]==1:
queens+=1
row+=1
if queens>1:
h_cost+=calculate_cost(queens)
return h_cost
def hill_climb(board,prev_cost):
# We jump up and down in the column only
# For a single queen, we check if the jump up or down is lowest hcost
# if for that particular move h cost is minimum we take the jump
# Row is constant, only column changes
moves=[]
for col in range(len(board)):
found=False
options=[0,1,2,3]
for row in range(len(board)):
print(row,col)
if board[row][col]==1:
options.remove(row)
found=True
break
print(found)
if found:
temp_board=board.copy()
for i in range(3):
choice=random.choice(options)
options.remove(choice)
temp_board[row][col]=0
temp_board[choice][col]=1
new_cost=heuristic_cost(temp_board)
print(new_cost)
if prev_cost>=new_cost:
prev_cost=new_cost
print(prev_cost)
moves=moves+[row,col,choice,col]
if moves:
board[moves[0]][moves[1]]=0
board[moves[2]][moves[3]]=1
print(board)
moves.clear()
return board,prev_cost
if __name__=="__main__":
board = np.zeros((4,4))
board=input_board(board)
cost=heuristic_cost(board)
print("Cost of the Board is",cost)
print("Let us See")
iterations=0
while (iterations!=10 and cost!=0):
new_board, cost = hill_climb(board,cost)
print("Board No:",iterations,"Cost:",cost)
print()
print(new_board)
iterations+=1