3
3
from csp import *
4
4
import random
5
5
6
-
7
6
random .seed ("aima-python" )
8
7
9
8
@@ -174,7 +173,7 @@ def test_csp_conflicted_vars():
174
173
def test_revise ():
175
174
neighbors = parse_neighbors ('A: B; B: ' )
176
175
domains = {'A' : [0 ], 'B' : [4 ]}
177
- constraints = lambda X , x , Y , y : x % 2 == 0 and (x + y ) == 4
176
+ constraints = lambda X , x , Y , y : x % 2 == 0 and (x + y ) == 4
178
177
179
178
csp = CSP (variables = None , domains = domains , neighbors = neighbors , constraints = constraints )
180
179
csp .support_pruning ()
@@ -196,24 +195,24 @@ def test_revise():
196
195
def test_AC3 ():
197
196
neighbors = parse_neighbors ('A: B; B: ' )
198
197
domains = {'A' : [0 , 1 , 2 , 3 , 4 ], 'B' : [0 , 1 , 2 , 3 , 4 ]}
199
- constraints = lambda X , x , Y , y : x % 2 == 0 and (x + y ) == 4 and y % 2 != 0
198
+ constraints = lambda X , x , Y , y : x % 2 == 0 and (x + y ) == 4 and y % 2 != 0
200
199
removals = []
201
200
202
201
csp = CSP (variables = None , domains = domains , neighbors = neighbors , constraints = constraints )
203
202
204
203
assert AC3 (csp , removals = removals ) is False
205
204
206
- constraints = lambda X , x , Y , y : (x % 2 ) == 0 and (x + y ) == 4
205
+ constraints = lambda X , x , Y , y : (x % 2 ) == 0 and (x + y ) == 4
207
206
removals = []
208
207
csp = CSP (variables = None , domains = domains , neighbors = neighbors , constraints = constraints )
209
208
210
209
assert AC3 (csp , removals = removals ) is True
211
210
assert (removals == [('A' , 1 ), ('A' , 3 ), ('B' , 1 ), ('B' , 3 )] or
212
211
removals == [('B' , 1 ), ('B' , 3 ), ('A' , 1 ), ('A' , 3 )])
213
-
214
- domains = {'A' : [ 2 , 4 ], 'B' : [ 3 , 5 ]}
215
- constraints = lambda X , x , Y , y : int (x ) > int (y )
216
- removals = []
212
+
213
+ domains = {'A' : [2 , 4 ], 'B' : [3 , 5 ]}
214
+ constraints = lambda X , x , Y , y : int (x ) > int (y )
215
+ removals = []
217
216
csp = CSP (variables = None , domains = domains , neighbors = neighbors , constraints = constraints )
218
217
219
218
assert AC3 (csp , removals = removals )
@@ -247,7 +246,7 @@ def test_num_legal_values():
247
246
def test_mrv ():
248
247
neighbors = parse_neighbors ('A: B; B: C; C: ' )
249
248
domains = {'A' : [0 , 1 , 2 , 3 , 4 ], 'B' : [4 ], 'C' : [0 , 1 , 2 , 3 , 4 ]}
250
- constraints = lambda X , x , Y , y : x % 2 == 0 and (x + y ) == 4
249
+ constraints = lambda X , x , Y , y : x % 2 == 0 and (x + y ) == 4
251
250
csp = CSP (variables = None , domains = domains , neighbors = neighbors , constraints = constraints )
252
251
assignment = {'A' : 0 }
253
252
@@ -269,13 +268,13 @@ def test_mrv():
269
268
def test_unordered_domain_values ():
270
269
map_coloring_test = MapColoringCSP (list ('123' ), 'A: B C; B: C; C: ' )
271
270
assignment = None
272
- assert unordered_domain_values ('A' , assignment , map_coloring_test ) == ['1' , '2' , '3' ]
271
+ assert unordered_domain_values ('A' , assignment , map_coloring_test ) == ['1' , '2' , '3' ]
273
272
274
273
275
274
def test_lcv ():
276
275
neighbors = parse_neighbors ('A: B; B: C; C: ' )
277
276
domains = {'A' : [0 , 1 , 2 , 3 , 4 ], 'B' : [0 , 1 , 2 , 3 , 4 , 5 ], 'C' : [0 , 1 , 2 , 3 , 4 ]}
278
- constraints = lambda X , x , Y , y : x % 2 == 0 and (x + y ) == 4
277
+ constraints = lambda X , x , Y , y : x % 2 == 0 and (x + y ) == 4
279
278
csp = CSP (variables = None , domains = domains , neighbors = neighbors , constraints = constraints )
280
279
assignment = {'A' : 0 }
281
280
@@ -347,7 +346,7 @@ def test_min_conflicts():
347
346
assert min_conflicts (france )
348
347
349
348
tests = [(usa , None )] * 3
350
- assert failure_test (min_conflicts , tests ) >= 1 / 3
349
+ assert failure_test (min_conflicts , tests ) >= 1 / 3
351
350
352
351
australia_impossible = MapColoringCSP (list ('RG' ), 'SA: WA NT Q NSW V; NT: WA Q; NSW: Q V; T: ' )
353
352
assert min_conflicts (australia_impossible , 1000 ) is None
@@ -419,9 +418,9 @@ def test_parse_neighbours():
419
418
420
419
def test_topological_sort ():
421
420
root = 'NT'
422
- Sort , Parents = topological_sort (australia ,root )
421
+ Sort , Parents = topological_sort (australia , root )
423
422
424
- assert Sort == ['NT' ,'SA' ,'Q' ,'NSW' ,'V' ,'WA' ]
423
+ assert Sort == ['NT' , 'SA' , 'Q' , 'NSW' , 'V' , 'WA' ]
425
424
assert Parents ['NT' ] == None
426
425
assert Parents ['SA' ] == 'NT'
427
426
assert Parents ['Q' ] == 'SA'
@@ -432,10 +431,11 @@ def test_topological_sort():
432
431
433
432
def test_tree_csp_solver ():
434
433
australia_small = MapColoringCSP (list ('RB' ),
435
- 'NT: WA Q; NSW: Q V' )
434
+ 'NT: WA Q; NSW: Q V' )
436
435
tcs = tree_csp_solver (australia_small )
437
436
assert (tcs ['NT' ] == 'R' and tcs ['WA' ] == 'B' and tcs ['Q' ] == 'B' and tcs ['NSW' ] == 'R' and tcs ['V' ] == 'B' ) or \
438
437
(tcs ['NT' ] == 'B' and tcs ['WA' ] == 'R' and tcs ['Q' ] == 'R' and tcs ['NSW' ] == 'B' and tcs ['V' ] == 'R' )
439
438
439
+
440
440
if __name__ == "__main__" :
441
441
pytest .main ()
0 commit comments