class Problem8: def __init__(self): self.time_out = FileWriter("Problem8Output.csv") def __delete__(self): self.time_out.close() def run(self): slow_modifier = 3 greedy_modifier = 100000 dp_modifier = 100 # V_slow = [1, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30] V_slow = [] V_greedy = [] V_dp = [] self.time_out.write(", ") for i in range(10, 510, 10): self.time_out.write("%d, " % i) V_slow.append(self.get_V_of_length_for_modifier(i, slow_modifier)) V_greedy.append(self.get_V_of_length_for_modifier(i, greedy_modifier)) V_dp.append(self.get_V_of_length_for_modifier(i, dp_modifier)) self.time_out.write_line("") A_slow = 10 * slow_modifier A_greedy = 10 * greedy_modifier A_dp = 10 * dp_modifier self.time_out.write("CHANGESLOW, ") for V in V_slow: start_slow = time.clock() (denominations_slow, total_slow) = changeslow(V, A_slow) duration_slow = time.clock() - start_slow self.time_out.write("%f, " % duration_slow) self.time_out.write("\nCHANGEGREEDY, ") for V in V_greedy: start_greedy = time.clock() (denominations_greedy, total_greedy) = changegreedy(V, A_greedy) duration_greedy = time.clock() - start_greedy self.time_out.write("%f, " % duration_greedy) self.time_out.write("\nCHANGEDP, ") for V in V_dp: start_dp = time.clock() (denominations_dp, total_dp) = changedp(V, A_dp) duration_dp = time.clock() - start_dp self.time_out.write("%f, " % duration_dp) self.time_out.write_line("") self.time_out.close() def get_V_of_length_for_modifier(self, length, modifier): V = [1] for i in range(1, length): V.append(V[i - 1] + i * modifier) return V
from algorithm4.algorithm4 import dynamic_programming import time # The values of n to test each algorithm for each step a1_list_lengths = [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000] a2_list_lengths = [1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000] a3_list_lengths = [1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000] a4_list_lengths = [100000, 200000, 300000, 400000, 500000, 600000, 700000, 800000, 900000, 1000000] random_list_gen = RandomListGen() out = FileWriter('Output/ExperimentalData.txt') out.write_line(" , 1, 2, 3, 4, 5, 6, 7, 8, 9, 10") out.write("Enumeration, ") # For each possible list length of algorithm1 for i in range(0, len(a1_list_lengths)): a1_random_lists = random_list_gen.get_lists(100, a1_list_lengths[i]) start = time.clock() # For each of the random lists of the current size for random_list in a1_random_lists: enumeration(random_list) elapsed = time.clock() - start out.write("%f, " % elapsed) out.write_line('') out.write("Better Enumeration, ") for i in range(0, len(a2_list_lengths)): a2_random_lists = random_list_gen.get_lists(100, a2_list_lengths[i])
fileReader = FileReader(file_name + ".txt") data = fileReader.read_data() fileReader.close() fileWriter = FileWriter(file_name + "change.txt") for i in data: V, A = i fileWriter.write_line("for problem:") fileWriter.write_result((V, A)) fileWriter.write_line("results are: \n") if A <= 30: result = changeslow(V, A) fileWriter.write("changeslow:\n") fileWriter.write_result(result) else: fileWriter.write_line("changeslow cannot run on problems of size %d in a timely manner" % A) result = changegreedy(V, A) fileWriter.write("changegreedy:\n") fileWriter.write_result(result) result = changedp(V, A) fileWriter.write("changedp:\n") fileWriter.write_result(result) fileWriter.write_line("----------------------") fileWriter.close()