-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathautograder.py
701 lines (611 loc) · 26 KB
/
autograder.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
from constraints import *
from backtracking import bt_search, GacEnforce
from csp import Variable, CSP
from csp_problems import nQueens, sudokuCSP
from sudoku import b1, b5, b6
import argparse
legalQs = ["q1", "q2", "q3", "q4", "q5"]
tested = [False]*5
gradeMessage = ""
grades = [0, 0, 0, 0, 0]
outof = [4, 5, 7, 2, 4]
tests = ["Q1", "Q2", "Q3", "Q4", "Q5"]
titles = ["Q1. Table Constraint for nQueens (4 points)",
"Q2. Forward Checking implementation (5 points)",
"Q3. GacEnforce and GAC implementation (5 points)",
"Q4. AllDiff for Sudoku (2 points)",
"Q5. NValues Constraint implementation (4 points)"]
def print_title(i):
l = max([len(t) for t in titles])
print "-"*l
print titles[i]
print "-"*l
def print_sep(c='-'):
l = max([len(t) for t in titles])
print c*l
def print_soln(s):
for (var, val) in s:
print "{} = {} ".format(var.name(), val),
def question_1():
print_title(0)
tested[0] = True
ntests = 3
fails = [False]*ntests
#test1 constraint.check()
q2 = Variable("Q2", [1, 2, 3, 4, 5])
q5 = Variable("Q5", [1, 2, 3, 4, 5])
c = QueensTableConstraint("Q2/Q5", q2, q5, 2, 5)
q2.setValue(2)
for val in q5.domain():
q5.setValue(val)
if c.check():
if val in [2,5]:
print "Queens table constraint check routine failed"
print "Q2={}, Q5={} not detected as falsifying constraint".format(q2.getValue(), q5.getValue())
fails[0] = True
else:
if val in [1,3,4]:
print "Queens table constraint check routine failed"
print "Q2={}, Q5={} not detected as satisfying constraint".format(q2.getValue(), q5.getValue())
fails[0] = True
if fails[0]:
print "Fail Q1 test 1"
else:
print "Pass Q1 test 1"
print_sep()
#test2 constraint.hasSupport()
q2.reset()
q3 = Variable("Q3", [1, 2, 3, 4, 5])
q2.pruneValue(1, None, None)
q2.pruneValue(4, None, None)
q2.pruneValue(5, None, None)
c = QueensTableConstraint("Q2/Q5", q2, q3, 2, 3)
for val in q3.domain():
if c.hasSupport(q3, val):
if val not in [1, 4, 5]:
print "Queens table constraint hasSupport routine failed"
print "Q2 current domain = {}, Q3 = {} detected to have support (doesn't)".format(q2.curDomain(), val)
fails[1] = True
else:
if val not in [2, 3]:
print "Queens table constraint hasSupport routine failed"
print "Q2 current domain = {}, Q3 = {} detected to not have support (does)".format(q2.curDomain(), val)
fails[1] = True
if fails[1]:
print "Fail Q1 test 2"
else:
print "Pass Q1 test 2"
print_sep()
#test3 within backtracking search
csp = nQueens(8, True)
solutions, num_nodes = bt_search('BT', csp, 'fixed', True, False)
if num_nodes != 1965:
print "Queens table constraint not working correctly. BT should explore 1965 nodes."
print "With your implementation it explores {}".format(num_nodes)
fails[2] = True
if len(solutions) != 92:
print "Queens table constraint not working correctly. BT should return 92 solutions"
print "With your implementation it returns {}".format(len(solutions))
fails[2] = True
if fails[2]:
print "Fail Q1 test 3"
else:
print "Pass Q1 test 3"
if any(fails):
grades[0] = 0
else:
grades[0] = outof[0]
def question_2():
print_title(1)
tested[1] = True
fails = [False, False]
#test 1. Find one solution
csp = nQueens(8, False)
solutions, num_nodes = bt_search('FC', csp, 'fixed', False, False)
errors = csp.check(solutions)
if len(errors) > 0:
fails[0] = True
print "Fail Q2 test 1: invalid solution(s) returned by FC"
for err in errors:
print_soln(err[0])
print "\n", err[1]
if len(solutions) != 1:
fails[0] = True
print "Fail Q2 test 1: FC failed to return only one solution"
print " returned: "
for s in solutions:
print_soln(s)
print ""
ok=True
for v in csp.variables():
if set(v.curDomain()) != set(v.domain()):
fails[0] = True
print "Fail Q2 test 1: FC failed to restore domains of variables"
if not fails[0]:
print "Pass Q2 test 1"
print_sep()
csp = nQueens(8, False)
solutions, num_nodes = bt_search('FC', csp, 'fixed', True, False)
errors=csp.check(solutions)
if len(errors) > 0:
fails[1] = True
print "Fail Q2 test 2: invalid solution(s) returned by FC"
for err in errors:
print_soln(err[0])
print "\n", err[1]
if len(solutions) != 92:
fails[1] = True
print "Fail Q2 test 2: FC failed to return 92 solutions"
print " returned {} solutions".format(len(solutions))
ok=True
for v in csp.variables():
if set(v.curDomain()) != set(v.domain()):
fails[1] = True
print "Fail Q2 test 2: FC failed to restore domains of variables"
if not fails[1]:
print "Pass Q2 test 1"
print_sep()
if sum(fails) == 2:
grades[1] = 0
elif sum(fails) == 1:
grades[1] = 3
elif sum(fails) == 0:
grades[1] = outof[1]
def question_3():
print_title(2)
tested[2] = True
fails = [False, False, False, False, False, False, False, False]
v1 = Variable('V1', [1, 2])
v2 = Variable('V2', [1, 2])
v3 = Variable('V3', [1, 2, 3, 4, 5])
v4 = Variable('V4', [1, 2, 3, 4, 5])
v5 = Variable('V5', [1, 2, 3, 4, 5])
v6 = Variable('V6', [1, 2, 3, 4, 5, 6, 7, 8, 9])
v7 = Variable('V7', [1, 2, 3, 4, 5, 6, 7, 8, 9])
v8 = Variable('V8', [1, 2, 3, 4, 5, 6, 7, 8, 9])
v9 = Variable('V9', [1, 2, 3, 4, 5, 6, 7, 8, 9])
vars = [v1, v2, v3, v4, v5, v6, v7, v8, v9]
ac = AllDiffConstraint('test9', vars)
testcsp = CSP('test', vars, [ac])
GacEnforce([ac], testcsp, None, None)
#v1.pruneValue(1, None, None)
test1 = " v1 = Variable('V1', [1, 2])\n\
v2 = Variable('V2', [1, 2])\n\
v3 = Variable('V3', [1, 2, 3, 4, 5])\n\
v4 = Variable('V4', [1, 2, 3, 4, 5])\n\
v5 = Variable('V5', [1, 2, 3, 4, 5])\n\
v6 = Variable('V6', [1, 2, 3, 4, 5, 6, 7, 8, 9])\n\
v7 = Variable('V7', [1, 2, 3, 4, 5, 6, 7, 8, 9])\n\
v8 = Variable('V8', [1, 2, 3, 4, 5, 6, 7, 8, 9])\n\
v9 = Variable('V9', [1, 2, 3, 4, 5, 6, 7, 8, 9])\n\
vars = [v1, v2, v3, v4, v5, v6, v7, v8, v9]\n\
ac = AllDiffConstraint('test9', vars)\n\
testcsp = CSP('test', vars, [ac])\n\
GacEnforce([ac], testcsp, None, None)"
soln_doms = [ set([1,2]), set([1,2]), set([3,4,5]), set([3,4,5]), set([3,4,5]),
set([6, 7, 8, 9]), set([6, 7, 8, 9]), set([6, 7, 8, 9]), set([6, 7, 8, 9]) ]
for i, v in enumerate(vars):
if set(v.curDomain()) != soln_doms[i]:
fails[0] = True
print "Error: {}.curDomain() == {}".format(v.name(), v.curDomain())
print "Correct curDomin should be == {}".format(list(soln_doms[i]))
if fails[0]:
print "\nFail Q3 test 1\nErrors were generated on the following code:"
print test1
else:
print "Pass Q3 test 1"
print_sep()
v1 = Variable('V1', [1, 2])
v2 = Variable('V2', [1, 2])
v3 = Variable('V3', [1, 2, 3, 4, 5])
v4 = Variable('V4', [1, 2, 3, 4, 5])
v5 = Variable('V5', [1, 2, 3, 4, 5])
v6 = Variable('V6', [1, 3, 4, 5])
v7 = Variable('V7', [1, 3, 4, 5])
ac1 = AllDiffConstraint('1', [v1,v2,v3])
ac2 = AllDiffConstraint('1', [v1,v2,v4])
ac3 = AllDiffConstraint('1', [v1,v2,v5])
ac4 = AllDiffConstraint('1', [v3,v4,v5,v6])
ac5 = AllDiffConstraint('1', [v3,v4,v5,v7])
vars = [v1, v2, v3, v4, v5, v6, v7]
cnstrs = [ac1,ac2,ac3,ac4,ac5]
testcsp = CSP('test2', vars, cnstrs)
GacEnforce(cnstrs, testcsp, None, None)
test2 = " v1 = Variable('V1', [1, 2])\n\
v2 = Variable('V2', [1, 2])\n\
v3 = Variable('V3', [1, 2, 3, 4, 5])\n\
v4 = Variable('V4', [1, 2, 3, 4, 5])\n\
v5 = Variable('V5', [1, 2, 3, 4, 5])\n\
v6 = Variable('V6', [1, 3, 4, 5])\n\
v7 = Variable('V7', [1, 3, 4, 5])\n\
ac1 = AllDiffConstraint('1', [v1,v2,v3])\n\
ac2 = AllDiffConstraint('1', [v1,v2,v4])\n\
ac3 = AllDiffConstraint('1', [v1,v2,v5])\n\
ac4 = AllDiffConstraint('1', [v3,v4,v5,v6])\n\
ac5 = AllDiffConstraint('1', [v3,v4,v5,v7])\n\
vars = [v1, v2, v3, v4, v5, v6, v7]\n\
cnstrs = [ac1,ac2,ac3,ac4,ac5]\n\
testcsp = CSP('test2', vars, cnstrs)\n\
GacEnforce(cnstrs, testcsp, None, None)"
soln_doms = [ set([1,2]), set([1,2]), set([3, 4, 5]), set([3,4,5]), set([3,4,5]),
set([1]), set([1]) ]
#v1.pruneValue(1, None, None)
for i, v in enumerate(vars):
if set(v.curDomain()) != soln_doms[i]:
fails[1] = True
print "Error: {}.curDomain() == {}".format(v.name(), v.curDomain())
print "Correct curDomin should be == {}".format(list(soln_doms[i]))
if fails[1]:
print "\nFail Q3 test 2\nErrors were generated on the following code:"
print test2
else:
print "Pass Q3 test 2"
print_sep()
csp = sudokuCSP(b1, 'neq')
GacEnforce(csp.constraints(), csp, None, None)
vars=csp.variables()
vars.sort(key=lambda var: var.name())
soln_doms = [ set([3]), set([1]), set([2]), set([5]), set([9]),
set([8]), set([7]), set([6]), set([4]), set([9]), set([4]),
set([6]), set([7]), set([3]), set([1]), set([2]), set([5]),
set([8]), set([8]), set([7]), set([5]), set([4]), set([2]),
set([6]), set([9]), set([1]), set([3]), set([5]), set([6]),
set([7]), set([8]), set([4]), set([2]), set([3]), set([9]),
set([1]), set([4]), set([8]), set([1]), set([3]), set([6]),
set([9]), set([5]), set([7]), set([2]), set([2]), set([9]),
set([3]), set([1]), set([7]), set([5]), set([4]), set([8]),
set([6]), set([1]), set([3]), set([8]), set([2]), set([5]),
set([7]), set([6]), set([4]), set([9]), set([6]), set([5]),
set([4]), set([9]), set([1]), set([3]), set([8]), set([2]),
set([7]), set([7]), set([2]), set([9]), set([6]), set([8]),
set([4]), set([1]), set([3]), set([5])]
#vars[0].pruneValue(3, None, None)
for i, v in enumerate(vars):
if set(v.curDomain()) != soln_doms[i]:
fails[2] = True
print "Error: {}.curDomain() == {}".format(v.name(), v.curDomain())
print "Correct curDomin should be == {}".format(list(soln_doms[i]))
if fails[2]:
print "\nFail Q3 test 3\nErrors were generated on the following code:"
print "python2.7 sudoku.py -e -m neq 1"
else:
print "Pass Q3 test 3"
print_sep()
csp = sudokuCSP(b5, 'neq')
GacEnforce(csp.constraints(), csp, None, None)
vars=csp.variables()
vars.sort(key=lambda var: var.name())
soln_doms = [ set([2, 4, 5, 8]), set([6]), set([2, 4, 5, 8]),
set([1]), set([2, 5, 8]), set([3, 8]), set([7, 9]), set([3, 5, 7,
8]), set([3, 7, 8, 9]), set([1, 2, 5, 8]), set([1, 2, 5, 8]),
set([7]), set([2, 3, 9]), set([2, 5, 6, 8]), set([3, 6, 8]),
set([1, 6, 9]), set([3, 5, 6, 8]), set([4]), set([1, 5, 8]),
set([9]), set([3]), set([4]), set([7]), set([6, 8]), set([1, 6]),
set([5, 6, 8]), set([2]), set([2, 4, 8]), set([2, 4, 7, 8]),
set([1]), set([6]), set([3]), set([9]), set([2, 4, 7]), set([2, 4,
7]), set([5]), set([5, 9]), set([5, 7]), set([5, 6, 9]), set([8]),
set([4]), set([2]), set([1, 6, 7, 9]), set([3, 6, 7]), set([1, 3,
6, 7, 9]), set([3]), set([2, 4]), set([2, 4, 6, 9]), set([5]),
set([1]), set([7]), set([8]), set([2, 4, 6]), set([6, 9]),
set([6]), set([2, 4, 5, 8]), set([2, 4, 5, 8]), set([2, 7]),
set([9]), set([4, 8]), set([3]), set([1]), set([7, 8]), set([7]),
set([1, 2, 3, 4, 8]), set([2, 4, 8, 9]), set([2, 3]), set([2, 6,
8]), set([1, 3, 4, 6, 8]), set([5]), set([2, 4, 6, 8]), set([6,
8]), set([1, 2, 4, 8]), set([1, 2, 3, 4, 8]), set([2, 4, 8]),
set([2, 3, 7]), set([2, 6, 8]), set([5]), set([2, 4, 6, 7]),
set([9]), set([6, 7, 8]) ]
#vars[0].pruneValue(2, None, None)
for i, v in enumerate(vars):
if set(v.curDomain()) != soln_doms[i]:
fails[3] = True
print "Error: {}.curDomain() == {}".format(v.name(), v.curDomain())
print "Correct curDomin should be == {}".format(list(soln_doms[i]))
if fails[3]:
print "\nFail Q3 test 4\nErrors were generated on the following code:"
print "python2.7 sudoku.py -e -m neq 5"
else:
print "Pass Q3 test 4"
print_sep()
v1 = Variable('V1', [1, 2])
v2 = Variable('V2', [1, 2])
v3 = Variable('V3', [1, 2, 3, 4, 5])
v4 = Variable('V4', [1, 2, 3, 4, 5])
v5 = Variable('V5', [1, 2, 3, 4, 5])
v6 = Variable('V6', [1, 3, 4, 5])
v7 = Variable('V7', [1, 3, 4, 5])
ac1 = AllDiffConstraint('1', [v1,v2,v3])
ac2 = AllDiffConstraint('1', [v1,v2,v4])
ac3 = AllDiffConstraint('1', [v1,v2,v5])
ac4 = AllDiffConstraint('1', [v3,v4,v5,v6])
ac5 = AllDiffConstraint('1', [v3,v4,v5,v7])
neq = NeqConstraint('2', [v6,v7])
vars = [v1, v2, v3, v4, v5, v6, v7]
cnstrs = [ac1,ac2,ac3,ac4,ac5,neq]
testcsp = CSP('test2', vars, cnstrs)
val = GacEnforce(cnstrs, testcsp, None, None)
test5 = " v1 = Variable('V1', [1, 2])\n\
v2 = Variable('V2', [1, 2])\n\
v3 = Variable('V3', [1, 2, 3, 4, 5])\n\
v4 = Variable('V4', [1, 2, 3, 4, 5])\n\
v5 = Variable('V5', [1, 2, 3, 4, 5])\n\
v6 = Variable('V6', [1, 3, 4, 5])\n\
v7 = Variable('V7', [1, 3, 4, 5])\n\
ac1 = AllDiffConstraint('1', [v1,v2,v3])\n\
ac2 = AllDiffConstraint('1', [v1,v2,v4])\n\
ac3 = AllDiffConstraint('1', [v1,v2,v5])\n\
ac4 = AllDiffConstraint('1', [v3,v4,v5,v6])\n\
ac5 = AllDiffConstraint('1', [v3,v4,v5,v7])\n\
neq = NeqConstraint('2', [v6,v7])\n\
vars = [v1, v2, v3, v4, v5, v6, v7]\n\
cnstrs = [ac1,ac2,ac3,ac4,ac5]\n\
testcsp = CSP('test2', vars, cnstrs)\n\
val = GacEnforce(cnstrs, testcsp, None, None)"
#val = 'fo'
if val != "DWO":
fails[4] = True
print "Error: GacEnforce failed to return \"DWO\" returned {} instead".format(val)
if fails[4]:
print "\nFail Q3 test 5\nErrors were generated on the following code:"
print test5
else:
print "Pass Q3 test 5"
print_sep()
csp = nQueens(8, False)
solutions, num_nodes = bt_search('GAC', csp, 'fixed', False, False)
errors = csp.check(solutions)
if len(errors) > 0:
fails[5] = True
print "Fail Q3 test 6: invalid solution(s) returned by GAC"
for err in errors:
print_soln(err[0])
print "\n", err[1]
if len(solutions) != 1:
fails[5] = True
print "Fail Q3 test 6: GAC failed to return only one solution"
print " returned: "
for s in solutions:
print_soln(s)
print ""
ok=True
for v in csp.variables():
if set(v.curDomain()) != set(v.domain()):
fails[5] = True
print "Fail Q3 test 6: GAC failed to restore domains of variables"
if not fails[5]:
print "Pass Q3 test 6"
print_sep()
csp = nQueens(8, False)
solutions, num_nodes = bt_search('GAC', csp, 'fixed', True, False)
errors=csp.check(solutions)
if len(errors) > 0:
fails[6] = True
print "Fail Q3 test 7: invalid solution(s) returned by GAC"
for err in errors:
print_soln(err[0])
print "\n", err[1]
if len(solutions) != 92:
fails[6] = True
print "Fail Q3 test 7: GAC failed to return 92 solutions"
print " returned {} solutions".format(len(solutions))
ok=True
for v in csp.variables():
if set(v.curDomain()) != set(v.domain()):
fails[6] = True
print "Fail Q3 test 7: GAC failed to restore domains of variables"
if not fails[7]:
print "Pass Q3 test 7"
print_sep()
grades[2] = 0
if sum(fails[:4]) == 0:
grades[2] += 3
if not fails[4]:
grades[2] +=1
if grades[2] >= 3:
if sum([fails[5], fails[6]]) == 0:
grades[2] += 3
def question_4():
print_title(3)
tested[3] = True
fails = [False, False]
if not tested[2]:
print_sep('=')
print "Q4 depends on Q3, running Q3 tests"
question_3()
print_sep('=')
if grades[2] == 0:
grades[3] = 0
print "Q3 failed, cannot mark Q4"
return
csp = sudokuCSP(b5, 'alldiff')
GacEnforce(csp.constraints(), csp, None, None)
vars=csp.variables()
vars.sort(key=lambda var: var.name())
soln_doms = [ set([2]), set([6]), set([4]), set([1]), set([5]),
set([8]), set([9]), set([7]), set([3]), set([1]), set([8]),
set([7]), set([9]), set([2]), set([3]), set([6]), set([5]),
set([4]), set([5]), set([9]), set([3]), set([4]), set([7]),
set([6]), set([1]), set([8]), set([2]), set([8]), set([7]),
set([1]), set([6]), set([3]), set([9]), set([4]), set([2]),
set([5]), set([9]), set([5]), set([6]), set([8]), set([4]),
set([2]), set([7]), set([3]), set([1]), set([3]), set([4]),
set([2]), set([5]), set([1]), set([7]), set([8]), set([6]),
set([9]), set([6]), set([2]), set([5]), set([7]), set([9]),
set([4]), set([3]), set([1]), set([8]), set([7]), set([3]),
set([9]), set([2]), set([8]), set([1]), set([5]), set([4]),
set([6]), set([4]), set([1]), set([8]), set([3]), set([6]),
set([5]), set([2]), set([9]), set([7]) ]
#vars[0].pruneValue(3, None, None)
for i, v in enumerate(vars):
if set(v.curDomain()) != soln_doms[i]:
fails[0] = True
print "Error: {}.curDomain() == {}".format(v.name(), v.curDomain())
print "Correct curDomin should be == {}".format(list(soln_doms[i]))
if fails[0]:
print "\nFail Q4 test 1\nErrors were generated on the following code:"
print "python2.7 sudoku.py -e -m alldiff 5"
else:
print "Pass Q4 test 1"
print_sep()
csp = sudokuCSP(b6, 'alldiff')
GacEnforce(csp.constraints(), csp, None, None)
vars=csp.variables()
vars.sort(key=lambda var: var.name())
soln_doms = [set([7]), set([9]), set([2]), set([1]), set([6]),
set([5]), set([4, 8]), set([4, 8]),
set([3]), set([3]), set([4, 8]), set([4, 8]), set([9]),
set([2]), set([7]), set([5]),
set([6]), set([1]), set([6]), set([5]),
set([1]), set([8]), set([4]), set([3]),
set([9]), set([2]), set([7]), set([4, 8]),
set([3, 4, 7, 8]), set([6]), set([4, 7]),
set([1]), set([9]), set([3, 4, 8]),
set([5]), set([2]), set([9]), set([3, 4, 7]),
set([4, 5, 7]), set([2]), set([5, 7]),
set([8]), set([1, 3, 4]), set([1, 3, 4]),
set([6]), set([1]), set([2]), set([4, 5, 8]),
set([4, 5]), set([3]), set([6]),
set([7]), set([4, 8, 9]), set([4, 9]),
set([4, 5]), set([1]), set([3]), set([5, 6, 7]),
set([8]), set([2]), set([4, 6]),
set([4, 7, 9]), set([4, 9]), set([2, 5]), set([6]),
set([9]), set([3, 5, 7]), set([5, 7]),
set([4]), set([1, 2, 3]), set([1, 3, 7]), set([8]),
set([2, 4, 8]), set([4, 7, 8]), set([4, 7, 8]), set([3, 6, 7]),
set([9]), set([1]), set([2, 3, 4, 6]),
set([3, 4, 7]), set([5])]
#vars[0].pruneValue(2, None, None)
for i, v in enumerate(vars):
if set(v.curDomain()) != soln_doms[i]:
fails[1] = True
print "Error: {}.curDomain() == {}".format(v.name(), v.curDomain())
print "Correct curDomin should be == {}".format(list(soln_doms[i]))
if fails[1]:
print "\nFail Q4 test 2\nErrors were generated on the following code:"
print "python2.7 sudoku.py -e -m alldiff 6"
else:
print "Pass Q4 test 1"
print_sep()
if any(fails):
grades[3] = 0
else:
grades[3] = outof[3]
def question_5():
print_title(4)
fails = [False]*2
test1= " v1 = Variable('V1', [1, 2])\n\
v2 = Variable('V2', [1, 2])\n\
v3 = Variable('V3', [1, 2, 3, 4, 5])\n\
v4 = Variable('V4', [1, 2, 3, 4, 5])\n\
v5 = Variable('V5', [1, 2, 3, 4, 5])\n\
v6 = Variable('V6', [1, 2, 3, 4, 5, 6, 7, 8, 9])\n\
v7 = Variable('V7', [1, 2, 3, 4, 5, 6, 7, 8, 9])\n\
v8 = Variable('V8', [1, 2, 3, 4, 5, 6, 7, 8, 9])\n\
v9 = Variable('V9', [1, 2, 3, 4, 5, 6, 7, 8, 9])\n\
vars = [v1, v2, v3, v4, v5, v6, v7, v8, v9]\n\
nv9 = NValuesConstraint('9', vars, 9, 4, 5)\n\
testcsp = CSP('test', vars, [nv9])\n\
GacEnforce([nv9], testcsp, None, None)"
v1 = Variable('V1', [1, 2])
v2 = Variable('V2', [1, 2])
v3 = Variable('V3', [1, 2, 3, 4, 5])
v4 = Variable('V4', [1, 2, 3, 4, 5])
v5 = Variable('V5', [1, 2, 3, 4, 5])
v6 = Variable('V6', [1, 2, 3, 4, 5, 6, 7, 8, 9])
v7 = Variable('V7', [1, 2, 3, 4, 5, 6, 7, 8, 9])
v8 = Variable('V8', [1, 2, 3, 4, 5, 6, 7, 8, 9])
v9 = Variable('V9', [1, 2, 3, 4, 5, 6, 7, 8, 9])
vars = [v1, v2, v3, v4, v5, v6, v7, v8, v9]
nv9 = NValuesConstraint('9', vars, 9, 4, 5)
testcsp = CSP('test', vars, [nv9])
GacEnforce([nv9], testcsp, None, None)
soln_doms = [set([1, 2]), set([1, 2]), set([1, 2, 3, 4, 5]),
set([1, 2, 3, 4, 5]), set([1, 2, 3, 4, 5]), set([9]),
set([9]), set([9]), set([9])]
for i, v in enumerate(vars):
if set(v.curDomain()) != soln_doms[i]:
fails[0] = True
print "Error: {}.curDomain() == {}".format(v.name(), v.curDomain())
print "Correct curDomin should be == {}".format(list(soln_doms[i]))
if fails[0]:
print "\nFail Q5 test 1\nErrors were generated on the following code:"
print test1
else:
print "Pass Q5 test 1"
print_sep()
test2 = " v1 = Variable('V1', [1, 2])\n\
v2 = Variable('V2', [1, 2])\n\
v3 = Variable('V3', [1, 2, 3, 4, 5])\n\
v4 = Variable('V4', [1, 2, 3, 4, 5])\n\
v5 = Variable('V5', [1, 2, 3, 4, 5])\n\
v6 = Variable('V6', [1, 2, 3, 4, 5, 6, 7, 8, 9])\n\
v7 = Variable('V7', [1, 2, 3, 4, 5, 6, 7, 8, 9])\n\
v8 = Variable('V8', [1, 2, 3, 4, 5, 6, 7, 8, 9])\n\
v9 = Variable('V9', [1, 2, 3, 4, 5, 6, 7, 8, 9])\n\
vars = [v1, v2, v3, v4, v5, v6, v7, v8, v9]\n\
nv9 = NValuesConstraint('9', vars, 9, 4, 5)\n\
nv1 = NValuesConstraint('1', vars, 1, 5, 5)\n\
testcsp = CSP('test', vars, [nv1, nv9])\n\
GacEnforce([nv1, nv9], testcsp, None, None)"
v1 = Variable('V1', [1, 2])
v2 = Variable('V2', [1, 2])
v3 = Variable('V3', [1, 2, 3, 4, 5])
v4 = Variable('V4', [1, 2, 3, 4, 5])
v5 = Variable('V5', [1, 2, 3, 4, 5])
v6 = Variable('V6', [1, 2, 3, 4, 5, 6, 7, 8, 9])
v7 = Variable('V7', [1, 2, 3, 4, 5, 6, 7, 8, 9])
v8 = Variable('V8', [1, 2, 3, 4, 5, 6, 7, 8, 9])
v9 = Variable('V9', [1, 2, 3, 4, 5, 6, 7, 8, 9])
vars = [v1, v2, v3, v4, v5, v6, v7, v8, v9]
nv9 = NValuesConstraint('9', vars, 9, 4, 5)
nv1 = NValuesConstraint('1', vars, 1, 5, 5)
testcsp = CSP('test', vars, [nv1, nv9])
GacEnforce([nv1, nv9], testcsp, None, None)
soln_doms = [set([1]), set([1]), set([1]), set([1]), set([1]),
set([9]), set([9]), set([9]), set([9])]
#vars[0].pruneValue(1, None, None)
for i, v in enumerate(vars):
if set(v.curDomain()) != soln_doms[i]:
fails[1] = True
print "Error: {}.curDomain() == {}".format(v.name(), v.curDomain())
print "Correct curDomin should be == {}".format(list(soln_doms[i]))
if fails[1]:
print "\nFail Q5 test 2\nErrors were generated on the following code:"
print test3
else:
print "Pass Q5 test 2"
print_sep()
if not any(fails):
grades[4] = outof[4]
def outputGrades():
print_sep('=')
for i in range(len(grades)):
print "Q{} mark = {}/{}".format(i+1, grades[i], outof[i])
print "-"*30
print "Total Mark = {}/{}".format(sum(grades), sum(outof))
print "The mark given by the autograder is not your final mark. More tests might be run"
print "You are not done yet. You must also submit your assignment"
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Autograder for Assignment 3')
parser.add_argument("-q", "--question", help="The question (1-5) to mark")
args = parser.parse_args()
if args.question:
if args.question not in legalQs:
print "Error: autograder only knows how to evaluate one of {}".format(legalQs)
exit(1)
if args.question == legalQs[0]:
question_1()
if args.question == legalQs[1]:
question_2()
if args.question == legalQs[2]:
question_3()
if args.question == legalQs[3]:
question_4()
if args.question == legalQs[4]:
question_5()
else:
question_1()
question_2()
question_3()
question_4()
question_5()
outputGrades()