def run_batch(bsizes): bmode_graphics = True print "batch mode start" if bmode_graphics: pygame.init() tests_passed = True # truncate the time file timefile_name = 'times.csv' open(timefile_name, 'w').truncate() for sz in bsizes: # starting time for this trial start_time = time.time() (w, h) = (sz, sz) screen, board, b1, b2 = None, None, None, None if bmode_graphics: screen, board, b1, b2 = init((w, h), bgcolor) f=open('tests/test_' + str(sz) + '.txt', 'r');tests=eval(f.read());f.close() tcnt = 0 for t in tests: segs = t[0] isegs = t[1] intersect, s1, s2 = any_segments_intersect(segs) if intersect: if not (s1, s2) in isegs: print str((s1, s2)) + ' should not intersect, test#' + str(tcnt) + 'failed...' tests_passed = False else: if s1 or s2: print str((s1, s2)) + ' should not intersect, test#' + str(tcnt) + 'failed...' tests_passed = False tcnt += 1 if bmode_graphics: for s in segs: pygame.draw.line(board, line_color, s[0], s[1]) for iseg in isegs: pygame.draw.line(board, highlight_color, iseg[0][0], iseg[0][1]) pygame.draw.line(board, highlight_color, iseg[1][0], iseg[1][1]) screen.blit(board, (0, 0)); pygame.display.flip() # input("Press Enter to continue...") time.sleep(2) # reset the board board.fill(bgcolor); screen.blit(board, (0, 0)); pygame.display.flip() end_time = time.time() # record end time, write the difference with open(timefile_name, 'ab') as time_file: writer = csv.writer(time_file) writer.writerow([sz, end_time-start_time]) if bmode_graphics: pygame.quit() if tests_passed: print("All tests passed!") print "batch mode end"
def run_batch(bsizes): bmode_graphics = True print "batch mode start" if bmode_graphics: pygame.init() tests_passed = True for sz in bsizes: start_time = time.time() (w, h) = (sz, sz) screen, board, b1, b2 = None, None, None, None if bmode_graphics: screen, board, b1, b2 = init((w, h), bgcolor) f = open('tests/test_' + str(sz) + '.txt', 'r') tests = eval(f.read()) f.close() tcnt = 0 for t in tests: segs = t[0] isegs = t[1] intersect, s1, s2 = any_segments_intersect(segs) if intersect: if not (s1, s2) in isegs: print str((s1, s2)) + ' should not intersect, test#' + str( tcnt) + 'failed...' tests_passed = False else: if s1 or s2: print str((s1, s2)) + ' should not intersect, test#' + str( tcnt) + 'failed...' tests_passed = False tcnt += 1 if bmode_graphics: for s in segs: pygame.draw.line(board, line_color, s[0], s[1]) for iseg in isegs: pygame.draw.line(board, highlight_color, iseg[0][0], iseg[0][1]) pygame.draw.line(board, highlight_color, iseg[1][0], iseg[1][1]) screen.blit(board, (0, 0)) pygame.display.flip() # input("Press Enter to continue...") time.sleep(2) # reset the board board.fill(bgcolor) screen.blit(board, (0, 0)) pygame.display.flip() end_time = time.time() run_time = (end_time - start_time) print run_time if bmode_graphics: pygame.quit() if tests_passed: print("All tests passed!") print "batch mode end"
def run_batch(bsizes): bmode_graphics = True print "batch mode start" if bmode_graphics: pygame.init() xvals = []; yvals = [] tests_passed = True times = [] for sz in bsizes: (w, h) = (sz, sz) screen, board, b1, b2 = None, None, None, None if bmode_graphics: screen, board, b1, b2 = init((w, h), bgcolor) f=open('tests/test_' + str(sz) + '.txt', 'r');tests=eval(f.read());f.close() tcnt = 0 for t in tests: segs = t[0] isegs = t[1] t1 = time.time() #start time intersect, s1, s2 = any_segments_intersect(segs) t2 = time.time() #stop time times.append(t2-t1) if intersect: if not (s1, s2) in isegs: print str((s1, s2)) + ' should not intersect, test#' + str(tcnt) + 'failed...' tests_passed = False else: if s1 or s2: print str((s1, s2)) + ' should not intersect, test#' + str(tcnt) + 'failed...' tests_passed = False tcnt += 1 if bmode_graphics: for s in segs: pygame.draw.line(board, line_color, s[0], s[1]) for iseg in isegs: pygame.draw.line(board, highlight_color, iseg[0][0], iseg[0][1]) pygame.draw.line(board, highlight_color, iseg[1][0], iseg[1][1]) screen.blit(board, (0, 0)); pygame.display.flip() # input("Press Enter to continue...") time.sleep(2) # reset the board board.fill(bgcolor); screen.blit(board, (0, 0)); pygame.display.flip() val = numpy.mean(times) xvals.append(sz); yvals.append(val) if bmode_graphics: pygame.quit() if tests_passed: print("All tests passed!") with open('times.csv', 'wb') as f: writer = csv.writer(f) writer.writerows(zip(xvals, yvals)) print "batch mode end"
def run_batch(bsizes): bmode_graphics = True print "batch mode start" if bmode_graphics: pygame.init() tests_passed = True for sz in bsizes: (w, h) = (sz, sz) screen, board, b1, b2 = None, None, None, None if bmode_graphics: screen, board, b1, b2 = init((w, h), bgcolor) f = open("tests/test_" + str(sz) + ".txt", "r") tests = eval(f.read()) f.close() tcnt = 0 for t in tests: segs = t[0] isegs = t[1] intersect, s1, s2 = any_segments_intersect(segs) if intersect: if not (s1, s2) in isegs: print str((s1, s2)) + " should not intersect, test#" + str(tcnt) + "failed..." tests_passed = False else: if s1 or s2: print str((s1, s2)) + " should not intersect, test#" + str(tcnt) + "failed..." tests_passed = False tcnt += 1 if bmode_graphics: for s in segs: pygame.draw.line(board, line_color, s[0], s[1]) for iseg in isegs: pygame.draw.line(board, highlight_color, iseg[0][0], iseg[0][1]) pygame.draw.line(board, highlight_color, iseg[1][0], iseg[1][1]) screen.blit(board, (0, 0)) pygame.display.flip() # input("Press Enter to continue...") time.sleep(2) # reset the board board.fill(bgcolor) screen.blit(board, (0, 0)) pygame.display.flip() if bmode_graphics: pygame.quit() if tests_passed: print ("All tests passed!") print "batch mode end"
def gen_tests(bsizes): for sz in bsizes: tests = [] (w, h) = (sz, sz) for n in range(5): # number of tests segs = []; isegs = [] # generate random segments nsegs = random.randint(2, 25) for n in range(nsegs): x1 = random.randint(0, w) x2 = random.randint(0, w) # do not allow vertical segments while x1 == x2: x2 = random.randint(0, w) segs.append(((x1, random.randint(0, h)), (x2, random.randint(0, h)))) intersect, s1, s2 = any_segments_intersect(segs) if intersect: isegs.append((s1, s2)) tests.append([segs, isegs]) f=open('tests/test_' + str(sz) + '.txt', 'w');f.write(str(tests));f.close()
if reset_board: break if e.type == pygame.MOUSEBUTTONDOWN: if in_screen_bounds(e.pos, (w, h)): if startpos: pygame.draw.line(board, line_color, startpos, e.pos) segs.append((startpos, e.pos)) startpos=None; else: startpos = e.pos else: intersect = get_button(e.pos, b1, b2) if intersect == 1: print str(segs) # Run the algo intersect, s1, s2 = any_segments_intersect(segs) print intersect if intersect: pygame.draw.line(board, highlight_color, s1[0], s1[1]) pygame.draw.line(board, highlight_color, s2[0], s2[1]) elif intersect == 2: # reset reset_board = True elif e.type == pygame.MOUSEMOTION: if startpos: # erase the prev line if prevpos: pygame.draw.line(board, bgcolor, startpos, prevpos) prevpos = e.pos # draw the new line pygame.draw.line(board, movingline_color, startpos, e.pos)
def run_batch(bsizes): bmode_graphics = True print "batch mode start" if bmode_graphics: pygame.init() xvals = [] yvals = [] tests_passed = True times = [] for sz in bsizes: (w, h) = (sz, sz) screen, board, b1, b2 = None, None, None, None if bmode_graphics: screen, board, b1, b2 = init((w, h), bgcolor) f = open('tests/test_' + str(sz) + '.txt', 'r') tests = eval(f.read()) f.close() tcnt = 0 for t in tests: segs = t[0] isegs = t[1] t1 = time.time() #start time intersect, s1, s2 = any_segments_intersect(segs) t2 = time.time() #stop time times.append(t2 - t1) if intersect: if not (s1, s2) in isegs: print str((s1, s2)) + ' should not intersect, test#' + str( tcnt) + 'failed...' tests_passed = False else: if s1 or s2: print str((s1, s2)) + ' should not intersect, test#' + str( tcnt) + 'failed...' tests_passed = False tcnt += 1 if bmode_graphics: for s in segs: pygame.draw.line(board, line_color, s[0], s[1]) for iseg in isegs: pygame.draw.line(board, highlight_color, iseg[0][0], iseg[0][1]) pygame.draw.line(board, highlight_color, iseg[1][0], iseg[1][1]) screen.blit(board, (0, 0)) pygame.display.flip() # input("Press Enter to continue...") time.sleep(2) # reset the board board.fill(bgcolor) screen.blit(board, (0, 0)) pygame.display.flip() val = numpy.mean(times) xvals.append(sz) yvals.append(val) if bmode_graphics: pygame.quit() if tests_passed: print("All tests passed!") with open('times.csv', 'wb') as f: writer = csv.writer(f) writer.writerows(zip(xvals, yvals)) print "batch mode end"