-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubmit.py
More file actions
225 lines (215 loc) · 8.1 KB
/
Copy pathsubmit.py
File metadata and controls
225 lines (215 loc) · 8.1 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
import copy
import time
# Node expansion counters
BTCounter = 0
FCCounter = 0
CPCounter = 0
def NodeCounter(count):
'''Increments the number of nodes by 1'''
return count + 1
def BTHelper(L, M, moveI, markCount, marks, domain):
'''
The main recursion function for the backtracking search
:param L: Length of the ruler given. | int Eg: L = 6
:param M: Number of marks on the ruler. | int Eg: M = 4
:param moveI: The ith mark assignment on the ruler. | int Eg: 4
:param markCount: Number of marks on the ruler at a given time. | int Eg: 4
:param marks: List containing the mark positions on the ruler.
:param domain: Range of values that a mark can take. Initially [0,1,...,L]
:return: Boolean stating whether a solution exists for the given L, M
'''
global BTCounter
# The recursion end condition
if markCount == M:
return True
# Return false if domain becomes empty
if domain == set():
return False
# For each value in domain
for nextI in domain:
# Check if the value taken from domain is a valid assignment
dValid, diffList = checkDomain(L, marks, nextI)
# Increment the nodes expanded count
BTCounter = NodeCounter(BTCounter)
# If the assignment is valid
if dValid == True:
# Add the current value to the marks list
marks.append(nextI)
# Nothing is removed from the domain for pure BT
distSet = set()
tempDomain = domain - distSet
# Recursively check for valid assignments to marks
if BTHelper(L, M, nextI, markCount + 1, marks, tempDomain) == True:
return True
# If the assignments do not give a solution, remove the last mark added (Backtrack)
else:
del marks[-1]
return False
def BT(L, M):
"*** YOUR CODE HERE ***"
# Initialize the ith move, marks-list, domain and markCount
moveI = 0
markCount = 1
marks = []
marks.append(0)
domain = set()
# Single assignment case
if M == 1:
return 1, marks
for i in range(1, L + 1):
domain.add(i)
# Call the helper to find a solution for given L, M
if BTHelper(L, M, moveI, markCount, marks, domain) == True:
# If solution exists, call BT again with decreased L and same M
tempLength , tempMarks = BT(marks[-1] - 1, M)
# If there is no solution for a given L, return the previous L for which solution exists
if tempLength == -1:
return L, marks
return tempMarks[-1], tempMarks
# Return no solution if above all fails
return -1, []
def FCHelper(L, M, moveI, markCount, marks, domain):
'''
The main recursion function for the backtracking search
:param L: Length of the ruler given. | int Eg: L = 6
:param M: Number of marks on the ruler. | int Eg: M = 4
:param moveI: The ith mark assignment on the ruler. | int Eg: 4
:param markCount: Number of marks on the ruler at a given time. | int Eg: 4
:param marks: List containing the mark positions on the ruler.
:param domain: Range of values that a mark can take. Initially [0,1,...,L]
:return: Boolean stating whether a solution exists for the given L, M
'''
global FCCounter
if markCount == M:
return True
if domain == set():
return False
for nextI in domain:
dValid, diffList = checkDomain(L, marks, nextI)
FCCounter = NodeCounter(FCCounter)
if dValid == True:
marks.append(nextI)
distSet = {nextI}
# Remove the current assignment from domain
tempDomain = domain - distSet
if FCHelper(L, M, nextI, markCount + 1, marks, tempDomain) == True:
return True
else:
del marks[-1]
return False
def FC(L, M):
"*** YOUR CODE HERE ***"
moveI = 0
markCount = 1
marks = []
marks.append(0)
domain = set()
if M == 1:
return 1, marks
for i in range(1, L + 1):
domain.add(i)
if FCHelper(L, M, moveI, markCount, marks, domain) == True:
tempLength , tempMarks = FC(marks[-1] - 1, M)
if tempLength == -1:
return L, marks
return tempMarks[-1], tempMarks
return -1, []
def checkDomain(L, marks, nextI):
'''
Checks if a given marking sequence is valid
Computes distinct lengths from the marks and checks if the assignment violates the constraint
:param L: Length of the ruler | int
:param marks: List containing mark positions
:param nextI: The new mark that has to be assigned. | int
:return: A boolean stating whether the mark list is valid and list of distinct differences
'''
diffList = set()
isValid = True
if nextI > L:
return False, diffList
tempMarks = copy.deepcopy(marks)
tempMarks.append(nextI)
for i in range(len(tempMarks)):
for j in range(i + 1, len(tempMarks)):
temp = abs(tempMarks[j] - tempMarks[i])
if temp in diffList:
return False, diffList
else:
diffList.add(temp)
return isValid, diffList
def CPHelper(L, M, moveI, markCount, marks, domain):
'''
The main recursion function for the backtracking search
:param L: Length of the ruler given. | int Eg: L = 6
:param M: Number of marks on the ruler. | int Eg: M = 4
:param moveI: The ith mark assignment on the ruler. | int Eg: 4
:param markCount: Number of marks on the ruler at a given time. | int Eg: 4
:param marks: List containing the mark positions on the ruler.
:param domain: Range of values that a mark can take. Initially [0,1,...,L]
:return: Boolean stating whether a solution exists for the given L, M
'''
global CPCounter
if markCount == M:
return True
if domain == set():
return False
for nextI in domain:
dValid, diffList = checkDomain(L, marks, nextI)
CPCounter = NodeCounter(CPCounter)
if dValid == True:
marks.append(nextI)
distSet = set()
# Find all future illegal values by adding distinct lengths to current assignments
for m in marks:
for j in diffList:
distSet.add(m + j)
# Remove the above values from the domain
tempDomain = domain - distSet
# Call the recursion with the reduced domain
if CPHelper(L, M, nextI, markCount + 1, marks, tempDomain) == True:
return True
else:
del marks[-1]
return False
def CP(L, M):
"*** YOUR CODE HERE ***"
moveI = 0
markCount = 1
marks = []
marks.append(0)
domain = set()
if M == 1:
return 1, marks
for i in range(1, L + 1):
domain.add(i)
if CPHelper(L, M, moveI, markCount, marks, domain) == True:
m1 = sorted(marks)
tempLength, tempMarks = CP(m1[-1] - 1, M)
if tempLength == -1:
return L, sorted(marks)
return tempMarks[-1], sorted(tempMarks)
return -1, []
# Uncomment this to test for nodes expanded and time taken
# def main():
# L,M = 30,7
# print("----------------------------------------------------------")
# a = time.time()
# print("Solution by BT+CP: ", CP(L,M))
# print ("Nodes Expanded: ", CPCounter)
# b = time.time()
# print ("Time taken: ", round(b - a, 2), "s")
# print("----------------------------------------------------------")
# a = time.time()
# print("Solution by BT+FC: ", FC(L,M))
# print ("Nodes Expanded: ", FCCounter)
# b = time.time()
# print ("Time Taken: ", round(b - a, 2), "s")
# print("----------------------------------------------------------")
# a = time.time()
# print("Solution by BT: ", BT(L,M))
# print ("Nodes Expanded: ", BTCounter)
# b = time.time()
# print ("Time taken: ", round(b - a, 2), "s")
# print("----------------------------------------------------------")
#
# main()