def part9(): """ Description: Used to test the range of values for part 9 in project 2 Prints the results of changedp and changegreedy for every value in the range (9,37) """ for i in range(9, 37): print("changedp") print(changedp.changedp(DENOMINATIONS, i)) print("changegreedy") print(changegreedy.changegreedy(DENOMINATIONS, i)) print("#######################################################")
import time, timeit from changedp import changedp from changegreedy import changegreedy values = [1, 5, 10, 25, 50] print "amount, greedy coins, greedy time, dp coins, dp time" for amount in range(2010,2201,5): print amount,",", print sum(changegreedy(values,amount)),",", print timeit.timeit(stmt='changegreedy.changegreedy(%s,%s)'%(str(values),str(amount)),setup="import changegreedy",timer=time.clock,number=1000),",", print sum(changedp(values,amount)),",", print timeit.timeit(stmt='changedp.changedp(%s,%s)'%(str(values),str(amount)),setup="import changedp",timer=time.clock,number=100)*10
# Loop through all our arrays that we are to test and test them with each algo for i in range(0, len(changeArray)): output_file = open(file_name, 'a') output_file.write('\n========== Array %d Results ==========\n' % (i+1)) output_file.close() for j in range(0, len(algorithms)): print 'Testing Algorithm: ' + algorithms[j] + ' on Array ', i+1 if j == 0: result = 'still testing...' #result = changeslow(valueArray[i], changeArray[i]) print 'Minimum coins for Brute Force: ', result, '\n' output_file = open(file_name, 'a') output_file.write('%s\n' % result) output_file.close() elif j == 1: result = changegreedy(valueArray[i], changeArray[i]) print 'Minimum coins for Greedy: ', result, '\n' output_file = open(file_name, 'a') output_file.write('%s\n' % result) output_file.close() elif j == 2: result = changedp(valueArray[i], changeArray[i]) print 'Minimum coins for Dynamic Programming: ', result, '\n' output_file = open(file_name, 'a') output_file.write('%s\n' % result) output_file.close() # Pause for 1 seconds and print unless it is the last run of the loop # if (i + 1) < len(testArrays): # print 'Testing next array in 1 second...\n' # sleep(1)
from changegreedy import changegreedy from changedp import changedp TOP = 1000 for b in range(2,11): x = 1 values = [x] while x<TOP: x*=b values.append(x) print values #raw_input() incorrect = 0 for a in range(1,TOP): g = sum(changegreedy(values,a)) d = sum(changedp(values,a)) #print "amount:%d DP:%d Greedy:%d"%(a,d,g) if g>d: incorrect +=1 print "for base:%d there were %d non optimal Greedy outputs"%(b,incorrect)
def experimentalAnalysis(nMax, stepSize, start): print "Generating array..." outfile = 'arr15.txt' f = open(outfile, 'w+') #All the different arrays and sizes, starting at 4, increasing to 15 #coinArray = [1, 5, 10, 25] #coinArray = [1, 5, 10, 20, 30] #coinArray = [1, 3, 6, 12, 18, 24] #coinArray = [1, 2, 6, 12, 24, 48, 60] #coinArray = [1, 4, 8, 12, 16, 20, 24, 28] #coinArray = [1, 3, 6, 9, 12, 18, 21, 24, 27] #coinArray = [1, 2, 5, 7, 10, 12, 15, 17, 20, 22] #coinArray = [1, 2, 6, 8, 10, 12, 14, 16, 18, 20, 22] #coinArray = [1, 2, 4, 7, 10, 12, 14, 17, 20, 23, 25, 28] #coinArray = [1, 3, 5, 8, 10, 13, 15, 18, 20, 23, 25, 28, 30] #coinArray = [1, 2, 5, 8, 10, 13, 15, 18, 20, 22, 24, 26, 28, 30] coinArray = [1, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30] #generate array of amounts testArray = [] for n in range(start, nMax+1, stepSize): testArray.append(n) #Tests each algorithms f.write("-----------Analysis for Algorithm ChangeSlow-----------\n") f.write("CoinAmt\tResult\tTime\n") for a in testArray: #test each amount stored in testArray if a <= 60: #Limit for recursive algo - above run times t0 = time() #are too high result = changeslow(coinArray, a) #call algorithm t1 = time() f.write("{0} {1} {2} \n".format(a,result[1],(t1-t0))) #writes time & results else: break if (t1 - t0 > 45): #Another limit to prevent long run times break print "Algo 1 done." #Next to blocks same as previous without the limits f.write("\n-----------Analysis for Algorithm ChangeGreedy-----------\n") f.write("CoinAmt\tResult\tTime\n") for a in testArray: t0 = time() result = changegreedy(coinArray, a) t1 = time() f.write("{0} {1} {2} \n".format(a,result[1],(t1-t0))) print "Algo 2 done." f.write("\n-----------Analysis for Algorithm ChangeDP-----------\n") f.write("CoinAmt\tResult\tTime\n") for a in testArray: t0 = time() result = changedp(coinArray, a) t1 = time() f.write("{0} {1} {2} \n".format(a,result[1],(t1-t0))) f.close()
# for amount in range(2000,2201): # print amount,",", # print sum(changegreedy(vA,amount)),",", # print timeit.timeit(stmt='changegreedy.changegreedy(%s,%s)'%(str(vA),str(amount)),setup="import changegreedy",timer=time.clock,number=1000),",", # print sum(changedp(vA,amount)),",", # print timeit.timeit(stmt='changedp.changedp(%s,%s)'%(str(vA),str(amount)),setup="import changedp",timer=time.clock,number=100)*10,",", # print sum(changegreedy(vB,amount)),",", # print timeit.timeit(stmt='changegreedy.changegreedy(%s,%s)'%(str(vB),str(amount)),setup="import changegreedy",timer=time.clock,number=1000),",", # print sum(changedp(vB,amount)),",", # print timeit.timeit(stmt='changedp.changedp(%s,%s)'%(str(vB),str(amount)),setup="import changedp",timer=time.clock,number=100)*10 # wronga = 0 # wrongb = 0 # n = len(range(2000,2201)) # for amount in range(2000,2201): # if sum(changegreedy(vA,amount)) > sum(changedp(vA,amount)): wronga +=1 # if sum(changegreedy(vB,amount)) > sum(changedp(vB,amount)): wrongb +=1 # print "v1 greedy wrong: %d out of: %d for an average of:%f incorecct"%(wronga,n,float(wronga)/float(n)) # print "v2 greedy wrong: %d out of: %d for an average of:%f incorecct"%(wrongb,n,float(wrongb)/float(n)) for amount in range(2000,2201): if sum(changegreedy(vA,amount)) > sum(changedp(vA,amount)): print "wrong", else: print "-----", if sum(changegreedy(vB,amount)) > sum(changedp(vB,amount)): print "wrong" else: print "-----"