-
Notifications
You must be signed in to change notification settings - Fork 1
/
8-queen-hill-climbing_with_pic.py
213 lines (154 loc) · 5.48 KB
/
8-queen-hill-climbing_with_pic.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import copy
import numpy as np
import chess
import sys
from chess import svg
def exists(i, j):
# Checks if square exists within boundary
return (i >= 0 and i < 8 and j >= 0 and j < 8)
def contains(i, j, l, m, queen_pairs):
# Check if the two pair of queens have already been included in count
if ((i, j, l, m) in queen_pairs) or ((l, m, i, j) in queen_pairs):
return True
return False
def save_board_as_png(fen):
# Converts the FEN format notation to an SVG chess board
board = chess.Board(fen)
boardsvg = chess.svg.board(board=board)
filetowriteto = open("output.SVG", "w")
filetowriteto.write(boardsvg)
filetowriteto.close()
print("SVG File created successfully")
def create_board(board):
# Creates a python-chess board for the matrix board
chess_board = chess.Board()
chess_board.clear()
for i in range(8):
for j in range(8):
if board[i][j]:
chess_board.set_piece_at(chess.square(
i, j), chess.Piece(5, chess.WHITE))
return chess_board.fen()
def position_queens_row_wise(board):
"""Place a single queen on every row. If there are more than
two quueens in one row, it places them on other rows"""
for row in board:
while row.count(1) > 1:
# More than one 1s so distribute to other rows
for i in range(8):
if board[i].count(1) == 0:
j = row.index(1)
board[i][j] = 1
row[j] = 0
break
return board
def heuristic_value(board):
# Calculates the heuristic value h of the current state of board
# Number of pairs of queens attacking each other directly or indirectly
h = 0
queen_pairs = []
for i in range(8):
for j in range(8):
if board[i][j]:
# Calculate horizontal attacks
for k in range(8):
if board[i][k] == 1 and k != j and not contains(i, j, i, k, queen_pairs):
queen_pairs.append((i, j, i, k))
h += 1
# Calculate vertical attacks
for k in range(8):
if board[k][j] == 1 and i != k and not contains(i, j, k, j, queen_pairs):
queen_pairs.append((i, j, k, j))
h += 1
# Calculate / diagonal attacks
# First go up the diagonal
l, m = i-1, j+1
while exists(l, m):
if board[l][m] == 1 and not contains(i, j, l, m, queen_pairs):
queen_pairs.append((i, j, l, m))
h += 1
l, m = l-1, m+1
# Now go down the diagonal
l, m = i+1, j-1
while exists(l, m):
if board[l][m] == 1 and not contains(i, j, l, m, queen_pairs):
queen_pairs.append((i, j, l, m))
h += 1
l, m = l+1, m-1
# Calculate \ diagonal attacks
# First go up the diagonal
l, m = i-1, j-1
while exists(l, m):
if board[l][m] == 1 and not contains(i, j, l, m, queen_pairs):
queen_pairs.append((i, j, l, m))
h += 1
l, m = l-1, m-1
# Now go down the diagonal
l, m = i+1, j+1
while exists(l, m):
if board[l][m] == 1 and not contains(i, j, l, m, queen_pairs):
queen_pairs.append((i, j, l, m))
h += 1
l, m = l+1, m+1
return h
def hill_climbing(board):
# Find the least cost successor for the given board state
min_board = board
min_h = 999999
global n_side_moves, n_steps
n_steps += 1
# Check if number of side moves has reached a limit
if n_side_moves == 100:
return -1
sideway_move = False
for i in range(8):
# Find index of queen in current row
queen = board[i].index(1)
board[i][queen] = 0
for k in range(8):
# Place queen at different positions and calculate new score
if k != queen:
board[i][k] = 1
h = heuristic_value(board)
if h < min_h:
min_h = h
min_board = copy.deepcopy(board)
if h == min_h:
min_h = h
min_board = copy.deepcopy(board)
sideway_move = True
board[i][k] = 0
board[i][queen] = 1
if sideway_move:
n_side_moves += 1
if min_h == 0:
print("Number of steps required: {}".format(n_steps))
return min_board
return hill_climbing(min_board)
if __name__ == "__main__":
# 8x8 chess board
board = []
n_side_moves = 0
n_steps = 0
for i in range(8):
row = list(map(int, input().split()))
board.append(row)
print("Current position's heuristic value: ", heuristic_value(board))
board = position_queens_row_wise(board)
min_board = hill_climbing(board)
if min_board != -1:
fen = create_board(min_board)
save_board_as_png(fen)
else:
print("Could not solve")
'''
paste this in input
1 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0
0 0 0 0 1 0 0 1
0 0 0 0 0 1 0 0
1 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0
'''