-
Notifications
You must be signed in to change notification settings - Fork 0
/
CrossWord.py
71 lines (59 loc) · 2.56 KB
/
CrossWord.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
from Algorithms.SimulatedAnnealing import *
from copy import deepcopy
import random
table = [
['a', 'p', 't'],
['m', 'l', 'b'],
['k', 'l', 'o'],
['u', 'o', 'c']
]
# table = [
# ['c', 'o', 'o'],
# ['a', 't', 'l'],
# ['l', 'm', 'b'],
# ['k', 'u', 'p']
# ]
words = ["go", "cool", "cat", "talk"]
class Problem(object):
def __init__(self, table, words):
self.table = table
self.words = words
def initial_state(self):
return table
def heuristic(self, table):
def word_score_in_table(table, word, position, index):
score_of_word = 0
if index == len(word) - 1:
return 1
elif position[0] > 0 and table[position[0] - 1][position[1]] == word[index]:
score_of_word = score_of_word + word_score_in_table(table, word, [position[0] - 1, position[1]], index + 1)
elif position[0] < len(table) - 1 and table[position[0] + 1][position[1]] == word[index]:
score_of_word = score_of_word + word_score_in_table(table, word, [position[0] + 1, position[1]], index + 1)
elif position[1] > 0 and table[position[0]][position[1] - 1] == word[index]:
score_of_word = score_of_word + word_score_in_table(table, word, [position[0], position[1] - 1], index + 1)
elif position[1] < len(table[0]) - 1 and table[position[0]][position[1] + 1] == word[index]:
score_of_word = score_of_word + word_score_in_table(table, word, [position[0], position[1] + 1], index + 1)
return score_of_word
score = 0
for word in words:
for row in table:
for col in row:
if list(word)[0] == col:
score = score + word_score_in_table(table, word, [table.index(row), row.index(col)], 1)
# print(score)
return score
def successor(self, state):
neighbors = []
rows_number = list(range(len(state)))
cols_number = list(range(len(state[0])))
for i in range(0,10):
temp_state = deepcopy(state)
random_row1 = random.choice(rows_number)
random_row2 = random.choice(rows_number)
random_col1 = random.choice(cols_number)
random_col2 = random.choice(cols_number)
temp_state[random_row1][random_col1], temp_state[random_row2][random_col2] = state[random_row2][random_col2], state[random_row1][random_col1]
neighbors.append(temp_state)
return neighbors
p = Problem(table, words)
sa = SimulatedAnnealing(p)