def mapping_opt_iterative_local_search(tg, ctg, ag, noc_rg, critical_rg, noncritical_rg, shm, iteration_num, sub_iteration, report, detailed_report, logging): if report: print("===========================================") print("STARTING MAPPING OPTIMIZATION...USING ITERATIVE LOCAL SEARCH...") best_tg = copy.deepcopy(tg) best_ag = copy.deepcopy(ag) best_ctg = copy.deepcopy(ctg) best_cost = Mapping_Functions.mapping_cost_function(tg, ag, shm, False) starting_cost = best_cost if report: print("INITIAL COST:"+str(starting_cost)) mapping_cost_file = open('Generated_Files/Internal/LocalSearchMappingCost.txt', 'w') mapping_cost_file.close() mapping_process_file = open('Generated_Files/Internal/MappingProcess.txt', 'w') mapping_process_file.close() for Iteration in range(0, iteration_num): logging.info(" ITERATION:"+str(Iteration)) random_seed = Config.mapping_random_seed random.seed(Config.mapping_random_seed) for i in range(0, Iteration): random_seed = random.randint(1, 100000) (current_tg, current_ctg, current_ag) = mapping_opt_local_search(tg, ctg, ag, noc_rg, critical_rg, noncritical_rg, shm, sub_iteration, False, detailed_report, logging, "LocalSearchMappingCost", "mapping_process_file_name", random_seed) if current_tg is not False: current_cost = Mapping_Functions.mapping_cost_function(current_tg, current_ag, shm, False) if current_cost <= best_cost: if current_cost < best_cost: if report: print("\033[32m* NOTE::\033[0mBETTER SOLUTION FOUND WITH COST: "+str(current_cost) + "\t ITERATION: "+str(Iteration)) logging.info("NOTE:: MOVED TO SOLUTION WITH COST: "+str(current_cost)+"ITERATION: "+str(Iteration)) else: logging.info("NOTE:: MOVED TO SOLUTION WITH COST: "+str(current_cost)+"ITERATION: "+str(Iteration)) best_tg = copy.deepcopy(current_tg) best_ag = copy.deepcopy(current_ag) best_ctg = copy.deepcopy(current_ctg) best_cost = current_cost del current_tg del current_ag del current_ctg Mapping_Functions.clear_mapping(tg, ctg, ag) counter = 0 schedule = True random_seed = Config.mapping_random_seed random.seed(Config.mapping_random_seed) for i in range(0, Iteration): random_seed = random.randint(1, 100000) while not Mapping_Functions.make_initial_mapping(tg, ctg, ag, shm, noc_rg, critical_rg, noncritical_rg, False, logging, random_seed): if counter == 10: # we try 10 times to find some initial solution... how ever if it fails... schedule = False break counter += 1 if schedule: Scheduling_Functions.clear_scheduling(ag) Scheduler.schedule_all(tg, ag, shm, False, logging) else: if report: print("\033[33mWARNING::\033[0m CAN NOT FIND ANOTHER FEASIBLE SOLUTION... ", "ABORTING ITERATIVE LOCAL SEARCH...") logging.info("CAN NOT FIND ANOTHER FEASIBLE SOLUTION... ABORTING ITERATIVE LOCAL SEARCH...") if report: print("-------------------------------------") print("STARTING COST: "+str(starting_cost)+"\tFINAL COST: "+str(best_cost)) print("IMPROVEMENT:"+str("{0:.2f}".format(100*(starting_cost-best_cost)/starting_cost))+" %") return best_tg, best_ctg, best_ag if report: print("-------------------------------------") print("STARTING COST:"+str(starting_cost)+"\tFINAL COST:"+str(best_cost)) print("IMPROVEMENT:"+str("{0:.2f}".format(100*(starting_cost-best_cost)/starting_cost))+" %") return best_tg, best_ctg, best_ag
def mapping(tg, ag, noc_rg, critical_rg, non_critical_rg, shm, logging, iteration=None , initial_mapping_string = None): """ Calculate different mapping algorithms Returns tg And ag after Mapping in case of success :param tg: Task Graph :param ag: Architecture Graph :param noc_rg: NoC Routing Graph :param critical_rg: NoC Routing Graph for Critical Region :param non_critical_rg: NoC Routing Graph for non-Critical Region :param shm: System Health Map! (Please note that mapper should not even have access to ful SHMU info) :param logging: logging file :return: (tg, ag) in case of failing returns (None, None) """ # to run the following heuristics (Min_Min,Max_Min), one needs to use independent # tasks... Please use: generate_random_independent_tg if Config.Mapping_Function == 'MinMin': if Config.tg.type == 'RandomIndependent': return SimpleGreedy.min_min_mapping(tg, ag, shm, logging) else: raise ValueError('WRONG TG TYPE FOR THIS MAPPING FUNCTION. SHOULD USE::RandomIndependent') elif Config.Mapping_Function == 'MaxMin': if Config.tg.type == 'RandomIndependent': return SimpleGreedy.max_min_mapping(tg, ag, shm, logging) else: raise ValueError('WRONG TG TYPE FOR THIS MAPPING FUNCTION. SHOULD USE::RandomIndependent') elif Config.Mapping_Function == 'MinExecutionTime': if Config.tg.type == 'RandomIndependent': return SimpleGreedy.min_execution_time(tg, ag, shm, logging) else: raise ValueError('WRONG TG TYPE FOR THIS MAPPING FUNCTION. SHOULD USE::RandomIndependent') elif Config.Mapping_Function == 'MinimumCompletionTime': if Config.tg.type == 'RandomIndependent': return SimpleGreedy.minimum_completion_time(tg, ag, shm, logging) else: raise ValueError('WRONG TG TYPE FOR THIS MAPPING FUNCTION. SHOULD USE::RandomIndependent') elif Config.Mapping_Function == 'NMap': return NMap.n_map(tg, ag, noc_rg, critical_rg, non_critical_rg, shm, logging) elif Config.Mapping_Function in ['LocalSearch', 'IterativeLocalSearch', 'SimulatedAnnealing']: if Config.tg.type in ['RandomDependent', 'Manual', 'FromDOTFile']: pass else: raise ValueError('WRONG TG TYPE FOR THIS MAPPING FUNCTION. SHOULD USE::RandomDependent') clustering_start_time = time.time() # clustered task graph if Config.task_clustering: ctg = copy.deepcopy(Clustering.generate_ctg(len(ag.nodes()))) if Clustering.initial_clustering(tg, ctg): # Clustered Task Graph Optimization if Config.Clustering_Optimization: (best_clustering, best_task_graph) = \ Clustering.ctg_opt_local_search(tg, ctg, Config.clustering.iterations, logging) tg = copy.deepcopy(best_task_graph) ctg = copy.deepcopy(best_clustering) del best_clustering, best_task_graph # Clustering_Test.double_check_ctg(tg, ctg) Clustering_Reports.report_ctg(ctg, "CTG_PostOpt.png") Clustering_Reports.viz_clustering_opt() else: print("CLUSTERING OPTIMIZATION TURNED OFF...") print("REMOVING EMPTY CLUSTERS...") Clustering_Functions.remove_empty_clusters(ctg) Clustering_Reports.report_ctg(ctg, "CTG_PostCleaning.png") print("\033[92mTIME::\033[0m CLUSTERING AND OPTIMIZATION TOOK: " + str(round(time.time()-clustering_start_time))+" SECONDS") else: print("Initial Clustering Failed....") raise ValueError("INITIAL CLUSTERING FAILED...") else: ctg = copy.deepcopy(Clustering.gen_transparent_clusters(tg)) mapping_start_time = time.time() # Mapping CTG on AG random_seed = Config.mapping_random_seed if Mapping_Functions.make_initial_mapping(tg, ctg, ag, shm, noc_rg, critical_rg, non_critical_rg, True, logging, random_seed, iteration): #if Config.DistanceBetweenMapping: # init_mapping_string = Mapping_Functions.mapping_into_string(tg) # print(init_mapping_string) #else: # init_mapping_string = None Mapping_Reports.report_mapping(ag, logging) # Schedule all tasks Scheduling_Functions.clear_scheduling(ag) Scheduler.schedule_all(tg, ag, shm, Config.DebugDetails, logging) Scheduling_Reports.report_mapped_tasks(ag, logging) Mapping_Functions.mapping_cost_function(tg, ag, shm, Config.DebugInfo) if Config.Mapping_Function == 'LocalSearch': mapping_cost_file = open('Generated_Files/Internal/LocalSearchMappingCost.txt', 'w') current_cost = Mapping_Functions.mapping_cost_function(tg, ag, shm, False, initial_mapping_string=initial_mapping_string) mapping_cost_file.write(str(current_cost)+"\n") mapping_cost_file.close() mapping_process_file = open('Generated_Files/Internal/MappingProcess.txt', 'w') mapping_process_file.write(Mapping_Functions.mapping_into_string(tg)+"\n") mapping_process_file.close() (best_tg, best_ctg, best_ag) = \ Local_Search.mapping_opt_local_search(tg, ctg, ag, noc_rg, critical_rg, non_critical_rg, shm, Config.LocalSearchIteration, Config.DebugInfo, Config.DebugDetails, logging, "LocalSearchMappingCost", "MappingProcess", Config.mapping_random_seed, initial_mapping_string=initial_mapping_string) tg = copy.deepcopy(best_tg) ag = copy.deepcopy(best_ag) del best_tg, best_ctg, best_ag Mapping_Reports.viz_mapping_opt('LocalSearchMappingCost', iteration) elif Config.Mapping_Function == 'IterativeLocalSearch': (best_tg, best_ctg, best_ag) = \ Local_Search.mapping_opt_iterative_local_search(tg, ctg, ag, noc_rg, critical_rg, non_critical_rg, shm, Config.IterativeLocalSearchIterations, Config.LocalSearchIteration, Config.DebugInfo, Config.DebugDetails, logging) tg = copy.deepcopy(best_tg) ag = copy.deepcopy(best_ag) del best_tg, best_ctg, best_ag Mapping_Reports.viz_mapping_opt('LocalSearchMappingCost', iteration) elif Config.Mapping_Function == 'SimulatedAnnealing': (best_tg, best_ctg, best_ag) = SimulatedAnnealing.optimize_mapping_sa(tg, ctg, ag, noc_rg, critical_rg, non_critical_rg, shm, 'SA_MappingCost', logging) Mapping_Reports.viz_mapping_opt('SA_MappingCost', iteration=None) if Config.SA_AnnealingSchedule == 'Adaptive': Mapping_Reports.viz_cost_slope() elif Config.SA_AnnealingSchedule == 'Huang': Mapping_Reports.viz_huang_race() tg = copy.deepcopy(best_tg) ag = copy.deepcopy(best_ag) del best_tg, best_ctg, best_ag # print(Mapping_Functions.mapping_into_string(TG)) print("\033[92mTIME::\033[0m MAPPING AND OPTIMIZATION TOOK: " + str(round(time.time()-mapping_start_time))+" SECONDS") Mapping_Reports.report_mapping(ag, logging) Scheduling_Functions.clear_scheduling(ag) Scheduler.schedule_all(tg, ag, shm, False, logging) Scheduling_Reports.report_mapped_tasks(ag, logging) if not Scheduling_Functions.check_if_all_deadlines_are_met(tg,ag): raise ValueError("not all critical tasks have met their deadline!") Mapping_Functions.mapping_cost_function(tg, ag, shm, True, initial_mapping_string=initial_mapping_string) return tg, ag else: Mapping_Reports.report_mapping(ag, logging) print("===========================================") raise ValueError("INITIAL MAPPING FAILED...") return None, None
def mapping_opt_iterative_local_search(tg, ctg, ag, noc_rg, critical_rg, noncritical_rg, shm, iteration_num, sub_iteration, report, detailed_report, logging): if report: print ("===========================================") print ("STARTING MAPPING OPTIMIZATION...USING ITERATIVE LOCAL SEARCH...") best_tg = copy.deepcopy(tg) best_ag = copy.deepcopy(ag) best_ctg = copy.deepcopy(ctg) best_cost = Mapping_Functions.mapping_cost_function(tg, ag, shm, False) starting_cost = best_cost if report: print ("INITIAL COST:"+str(starting_cost)) mapping_cost_file = open('Generated_Files/Internal/LocalSearchMappingCost.txt', 'w') mapping_cost_file.close() mapping_process_file = open('Generated_Files/Internal/MappingProcess.txt', 'w') mapping_process_file.close() for Iteration in range(0, iteration_num): logging.info(" ITERATION:"+str(Iteration)) random_seed = Config.mapping_random_seed random.seed(Config.mapping_random_seed) for i in range(0, Iteration): random_seed = random.randint(1, 100000) (current_tg, current_ctg, current_ag) = mapping_opt_local_search(tg, ctg, ag, noc_rg, critical_rg, noncritical_rg, shm, sub_iteration, False, detailed_report, logging, "LocalSearchMappingCost", "mapping_process_file_name", random_seed) if current_tg is not False: current_cost = Mapping_Functions.mapping_cost_function(current_tg, current_ag, shm, False) if current_cost <= best_cost: if current_cost < best_cost: if report: print ("\033[32m* NOTE::\033[0mBETTER SOLUTION FOUND WITH COST: "+str(current_cost) + "\t ITERATION: "+str(Iteration)) logging.info("NOTE:: MOVED TO SOLUTION WITH COST: "+str(current_cost)+"ITERATION: "+str(Iteration)) else: logging.info("NOTE:: MOVED TO SOLUTION WITH COST: "+str(current_cost)+"ITERATION: "+str(Iteration)) best_tg = copy.deepcopy(current_tg) best_ag = copy.deepcopy(current_ag) best_ctg = copy.deepcopy(current_ctg) best_cost = current_cost del current_tg del current_ag del current_ctg Mapping_Functions.clear_mapping(tg, ctg, ag) counter = 0 schedule = True random_seed = Config.mapping_random_seed random.seed(Config.mapping_random_seed) for i in range(0, Iteration): random_seed = random.randint(1, 100000) while not Mapping_Functions.make_initial_mapping(tg, ctg, ag, shm, noc_rg, critical_rg, noncritical_rg, False, logging, random_seed): if counter == 10: # we try 10 times to find some initial solution... how ever if it fails... schedule = False break counter += 1 if schedule: Scheduling_Functions.clear_scheduling(ag) Scheduler.schedule_all(tg, ag, shm, False, logging) else: if report: print ("\033[33mWARNING::\033[0m CAN NOT FIND ANOTHER FEASIBLE SOLUTION... ", "ABORTING ITERATIVE LOCAL SEARCH...") logging.info("CAN NOT FIND ANOTHER FEASIBLE SOLUTION... ABORTING ITERATIVE LOCAL SEARCH...") if report: print ("-------------------------------------") print ("STARTING COST: "+str(starting_cost)+"\tFINAL COST: "+str(best_cost)) print ("IMPROVEMENT:"+str("{0:.2f}".format(100*(starting_cost-best_cost)/starting_cost))+" %") return best_tg, best_ctg, best_ag if report: print ("-------------------------------------") print ("STARTING COST:"+str(starting_cost)+"\tFINAL COST:"+str(best_cost)) print ("IMPROVEMENT:"+str("{0:.2f}".format(100*(starting_cost-best_cost)/starting_cost))+" %") return best_tg, best_ctg, best_ag
def optimize_mapping_sa(tg, ctg, ag, noc_rg, critical_rg, noncritical_rg, shm, cost_data_file, logging): print("===========================================") print("STARTING MAPPING OPTIMIZATION...USING SIMULATED ANNEALING...") print("STARTING TEMPERATURE: " + str(Config.SA_InitialTemp)) print("ANNEALING SCHEDULE: " + Config.SA_AnnealingSchedule) print("TERMINATION CRITERIA: " + Config.TerminationCriteria) print("================") if type(cost_data_file) is str: mapping_cost_file = open( 'Generated_Files/Internal/' + cost_data_file + '.txt', 'a') else: raise ValueError("cost_data_file name is not string: " + str(cost_data_file)) mapping_process_file = open('Generated_Files/Internal/MappingProcess.txt', 'w') sa_temperature_file = open('Generated_Files/Internal/SATemp.txt', 'w') sa_cost_slop_file = open('Generated_Files/Internal/SACostSlope.txt', 'w') sa_huang_race_file = open('Generated_Files/Internal/SAHuangRace.txt', 'w') if Config.SA_AnnealingSchedule in ['Adaptive', 'Aart', 'Huang']: cost_monitor = deque([]) else: cost_monitor = [] if Config.DistanceBetweenMapping: init_map_string = Mapping_Functions.mapping_into_string(tg) if Config.Mapping_CostFunctionType == 'CONSTANT': Mapping_Functions.clear_mapping(tg, ctg, ag) if not Mapping_Functions.make_initial_mapping( tg, ctg, ag, shm, noc_rg, critical_rg, noncritical_rg, True, logging, Config.mapping_random_seed): raise ValueError("FEASIBLE MAPPING NOT FOUND...") else: init_map_string = None current_tg = copy.deepcopy(tg) current_ag = copy.deepcopy(ag) current_ctg = copy.deepcopy(ctg) current_cost = Mapping_Functions.mapping_cost_function( tg, ag, shm, False, initial_mapping_string=init_map_string) starting_cost = current_cost best_tg = copy.deepcopy(tg) best_ag = copy.deepcopy(ag) best_ctg = copy.deepcopy(ctg) best_cost = current_cost initial_temp = Config.SA_InitialTemp sa_temperature_file.write(str(initial_temp) + "\n") temperature = initial_temp slope = None zero_slope_counter = 0 standard_deviation = None # for Huang Annealing schedule huang_counter1 = 0 huang_counter2 = 0 huang_steady_counter = 0 iteration_num = Config.SimulatedAnnealingIteration # for i in range(0, iteration_num): # move to another solution i = 0 while True: i += 1 new_tg, new_ctg, new_ag = move_to_next_solution( i, current_tg, current_ctg, current_ag, noc_rg, shm, critical_rg, noncritical_rg, logging) Scheduling_Functions.clear_scheduling(new_ag) Scheduler.schedule_all(new_tg, new_ag, shm, False, logging) # calculate the cost of new solution new_cost = Mapping_Functions.mapping_cost_function( new_tg, new_ag, shm, False, initial_mapping_string=init_map_string) if new_cost < best_cost: best_tg = copy.deepcopy(new_tg) best_ag = copy.deepcopy(new_ag) best_ctg = copy.deepcopy(new_ctg) best_cost = new_cost print("\033[33m* NOTE::\033[0mFOUND BETTER SOLUTION WITH COST:" + "{0:.2f}".format(new_cost) + "\t ITERATION:" + str(i) + "\tIMPROVEMENT:" + "{0:.2f}".format(100 * (starting_cost - new_cost) / starting_cost) + " %") # calculate the probability P of accepting the solution prob = metropolis(current_cost, new_cost, temperature) # print("prob:", prob) # throw the coin with probability P random_seed = Config.mapping_random_seed random.seed(Config.mapping_random_seed) for j in range(0, i): random_seed = random.randint(1, 100000) random.seed(random_seed) logging.info("Throwing Dice: random_seed: " + str(random_seed) + " iteration: " + str(i)) if prob > random.random(): # accept the new solution move_accepted = True current_tg = copy.deepcopy(new_tg) current_ag = copy.deepcopy(new_ag) current_ctg = copy.deepcopy(new_ctg) current_cost = new_cost if Config.SA_ReportSolutions: if slope is not None: print( "\033[32m* NOTE::\033[0mMOVED TO SOLUTION WITH COST:", "{0:.2f}".format(current_cost), "\tprob:", "{0:.2f}".format(prob), "\tTemp:", "{0:.2f}".format(temperature), "\t Iteration:", i, "\tSLOPE:", "{0:.2f}".format(slope)) if standard_deviation is not None: print( "\033[32m* NOTE::\033[0mMOVED TO SOLUTION WITH COST:", "{0:.2f}".format(current_cost), "\tprob:", "{0:.2f}".format(prob), "\tTemp:", "{0:.2f}".format(temperature), "\t Iteration:", i, "\tSTD_DEV:", "{0:.2f}".format(standard_deviation)) else: print( "\033[32m* NOTE::\033[0mMOVED TO SOLUTION WITH COST:", "{0:.2f}".format(current_cost), "\tprob:", "{0:.2f}".format(prob), "\tTemp:", "{0:.2f}".format(temperature), "\t Iteration:", i) else: move_accepted = False # move back to initial solution pass # update Temp mapping_process_file.write( Mapping_Functions.mapping_into_string(current_tg) + "\n") sa_temperature_file.write(str(temperature) + "\n") mapping_cost_file.write(str(current_cost) + "\n") if Config.SA_AnnealingSchedule == 'Adaptive': if len(cost_monitor) > Config.CostMonitorQueSize: cost_monitor.appendleft(current_cost) cost_monitor.pop() else: cost_monitor.appendleft(current_cost) slope = calculate_slope_of_cost(cost_monitor) if slope == 0: zero_slope_counter += 1 else: zero_slope_counter = 0 sa_cost_slop_file.write(str(slope) + "\n") if Config.SA_AnnealingSchedule == 'Aart': if len(cost_monitor) == Config.CostMonitorQueSize: standard_deviation = statistics.stdev(cost_monitor) cost_monitor.clear() # print(standard_deviation) else: cost_monitor.appendleft(current_cost) # Huang's annealing schedule is very much like Aart's Schedule... how ever, Aart's schedule stays in a fixed # temperature for a fixed number of steps, however, Huang's schedule decides about number of steps dynamically if Config.SA_AnnealingSchedule == 'Huang': cost_monitor.appendleft(current_cost) if len(cost_monitor) > 1: huang_cost_mean = sum(cost_monitor) / len(cost_monitor) huang_cost_sd = statistics.stdev(cost_monitor) if move_accepted: if huang_cost_mean - Config.HuangAlpha * huang_cost_sd <= current_cost <= \ huang_cost_mean + Config.HuangAlpha * huang_cost_sd: huang_counter1 += 1 else: huang_counter2 += 1 # print(huang_counter1, huang_counter2) sa_huang_race_file.write( str(huang_counter1) + " " + str(huang_counter2) + "\n") if huang_counter1 == Config.HuangTargetValue1: standard_deviation = statistics.stdev(cost_monitor) cost_monitor.clear() huang_counter1 = 0 huang_counter2 = 0 huang_steady_counter = 0 elif huang_counter2 == Config.HuangTargetValue2: huang_counter1 = 0 huang_counter2 = 0 standard_deviation = None elif huang_steady_counter == Config.CostMonitorQueSize: standard_deviation = statistics.stdev(cost_monitor) cost_monitor.clear() huang_counter1 = 0 huang_counter2 = 0 huang_steady_counter = 0 print( "\033[36m* COOLING::\033[0m REACHED MAX STEADY STATE... PREPARING FOR COOLING..." ) else: standard_deviation = None huang_steady_counter += 1 temperature = next_temp(initial_temp, i, iteration_num, temperature, slope, standard_deviation) if Config.SA_AnnealingSchedule == 'Adaptive': if zero_slope_counter == Config.MaxSteadyState: print("NO IMPROVEMENT POSSIBLE...") break if Config.TerminationCriteria == 'IterationNum': if i == Config.SimulatedAnnealingIteration: print("REACHED MAXIMUM ITERATION NUMBER...") break elif Config.TerminationCriteria == 'StopTemp': if temperature <= Config.SA_StopTemp: print("REACHED STOP TEMPERATURE...") break mapping_cost_file.close() mapping_process_file.close() sa_temperature_file.close() sa_cost_slop_file.close() sa_huang_race_file.close() print("-------------------------------------") print("STARTING COST:" + str(starting_cost) + "\tFINAL COST:" + str(best_cost)) print("IMPROVEMENT:" + "{0:.2f}".format(100 * (starting_cost - best_cost) / starting_cost) + " %") return best_tg, best_ctg, best_ag
def optimize_mapping_sa(tg, ctg, ag, noc_rg, critical_rg, noncritical_rg, shm, cost_data_file, logging): print ("===========================================") print ("STARTING MAPPING OPTIMIZATION...USING SIMULATED ANNEALING...") print ("STARTING TEMPERATURE: "+str(Config.SA_InitialTemp)) print ("ANNEALING SCHEDULE: "+Config.SA_AnnealingSchedule) print ("TERMINATION CRITERIA: "+Config.TerminationCriteria) print ("================") if type(cost_data_file) is str: mapping_cost_file = open('Generated_Files/Internal/'+cost_data_file+'.txt', 'a') else: raise ValueError("cost_data_file name is not string: "+str(cost_data_file)) mapping_process_file = open('Generated_Files/Internal/MappingProcess.txt', 'w') sa_temperature_file = open('Generated_Files/Internal/SATemp.txt', 'w') sa_cost_slop_file = open('Generated_Files/Internal/SACostSlope.txt', 'w') sa_huang_race_file = open('Generated_Files/Internal/SAHuangRace.txt', 'w') if Config.SA_AnnealingSchedule in ['Adaptive', 'Aart', 'Huang']: cost_monitor = deque([]) else: cost_monitor = [] if Config.DistanceBetweenMapping: init_map_string = Mapping_Functions.mapping_into_string(tg) if Config.Mapping_CostFunctionType == 'CONSTANT': Mapping_Functions.clear_mapping(tg, ctg, ag) if not Mapping_Functions.make_initial_mapping(tg, ctg, ag, shm, noc_rg, critical_rg, noncritical_rg, True, logging, Config.mapping_random_seed): raise ValueError("FEASIBLE MAPPING NOT FOUND...") else: init_map_string = None current_tg = copy.deepcopy(tg) current_ag = copy.deepcopy(ag) current_ctg = copy.deepcopy(ctg) current_cost = Mapping_Functions.mapping_cost_function(tg, ag, shm, False, initial_mapping_string=init_map_string) starting_cost = current_cost best_tg = copy.deepcopy(tg) best_ag = copy.deepcopy(ag) best_ctg = copy.deepcopy(ctg) best_cost = current_cost initial_temp = Config.SA_InitialTemp sa_temperature_file.write(str(initial_temp)+"\n") temperature = initial_temp slope = None zero_slope_counter = 0 standard_deviation = None # for Huang Annealing schedule huang_counter1 = 0 huang_counter2 = 0 huang_steady_counter = 0 iteration_num = Config.SimulatedAnnealingIteration # for i in range(0, iteration_num): # move to another solution i = 0 while True: i += 1 new_tg, new_ctg, new_ag = move_to_next_solution(i, current_tg, current_ctg, current_ag, noc_rg, shm, critical_rg, noncritical_rg, logging) Scheduling_Functions.clear_scheduling(new_ag) Scheduler.schedule_all(new_tg, new_ag, shm, False, logging) # calculate the cost of new solution new_cost = Mapping_Functions.mapping_cost_function(new_tg, new_ag, shm, False, initial_mapping_string=init_map_string) if new_cost < best_cost: best_tg = copy.deepcopy(new_tg) best_ag = copy.deepcopy(new_ag) best_ctg = copy.deepcopy(new_ctg) best_cost = new_cost print ("\033[33m* NOTE::\033[0mFOUND BETTER SOLUTION WITH COST:"+"{0:.2f}".format(new_cost) + "\t ITERATION:"+str(i)+"\tIMPROVEMENT:" + "{0:.2f}".format(100*(starting_cost-new_cost)/starting_cost)+" %") # calculate the probability P of accepting the solution prob = metropolis(current_cost, new_cost, temperature) # print ("prob:", prob) # throw the coin with probability P random_seed = Config.mapping_random_seed random.seed(Config.mapping_random_seed) for j in range(0, i): random_seed = random.randint(1, 100000) random.seed(random_seed) logging.info("Throwing Dice: random_seed: "+str(random_seed)+" iteration: "+str(i)) if prob > random.random(): # accept the new solution move_accepted = True current_tg = copy.deepcopy(new_tg) current_ag = copy.deepcopy(new_ag) current_ctg = copy.deepcopy(new_ctg) current_cost = new_cost if Config.SA_ReportSolutions: if slope is not None: print ("\033[32m* NOTE::\033[0mMOVED TO SOLUTION WITH COST:", "{0:.2f}".format(current_cost), "\tprob:", "{0:.2f}".format(prob), "\tTemp:", "{0:.2f}".format(temperature), "\t Iteration:", i, "\tSLOPE:", "{0:.2f}".format(slope)) if standard_deviation is not None: print ("\033[32m* NOTE::\033[0mMOVED TO SOLUTION WITH COST:", "{0:.2f}".format(current_cost), "\tprob:", "{0:.2f}".format(prob), "\tTemp:", "{0:.2f}".format(temperature), "\t Iteration:", i, "\tSTD_DEV:", "{0:.2f}".format(standard_deviation)) else: print ("\033[32m* NOTE::\033[0mMOVED TO SOLUTION WITH COST:", "{0:.2f}".format(current_cost), "\tprob:", "{0:.2f}".format(prob), "\tTemp:", "{0:.2f}".format(temperature), "\t Iteration:", i) else: move_accepted = False # move back to initial solution pass # update Temp mapping_process_file.write(Mapping_Functions.mapping_into_string(current_tg)+"\n") sa_temperature_file.write(str(temperature)+"\n") mapping_cost_file.write(str(current_cost)+"\n") if Config.SA_AnnealingSchedule == 'Adaptive': if len(cost_monitor) > Config.CostMonitorQueSize: cost_monitor.appendleft(current_cost) cost_monitor.pop() else: cost_monitor.appendleft(current_cost) slope = calculate_slope_of_cost(cost_monitor) if slope == 0: zero_slope_counter += 1 else: zero_slope_counter = 0 sa_cost_slop_file.write(str(slope)+"\n") if Config.SA_AnnealingSchedule == 'Aart': if len(cost_monitor) == Config.CostMonitorQueSize: standard_deviation = statistics.stdev(cost_monitor) cost_monitor.clear() # print (standard_deviation) else: cost_monitor.appendleft(current_cost) # Huang's annealing schedule is very much like Aart's Schedule... how ever, Aart's schedule stays in a fixed # temperature for a fixed number of steps, however, Huang's schedule decides about number of steps dynamically if Config.SA_AnnealingSchedule == 'Huang': cost_monitor.appendleft(current_cost) if len(cost_monitor) > 1: huang_cost_mean = sum(cost_monitor)/len(cost_monitor) huang_cost_sd = statistics.stdev(cost_monitor) if move_accepted: if huang_cost_mean - Config.HuangAlpha * huang_cost_sd <= current_cost <= \ huang_cost_mean + Config.HuangAlpha * huang_cost_sd: huang_counter1 += 1 else: huang_counter2 += 1 # print (huang_counter1, huang_counter2) sa_huang_race_file.write(str(huang_counter1)+" "+str(huang_counter2)+"\n") if huang_counter1 == Config.HuangTargetValue1: standard_deviation = statistics.stdev(cost_monitor) cost_monitor.clear() huang_counter1 = 0 huang_counter2 = 0 huang_steady_counter = 0 elif huang_counter2 == Config.HuangTargetValue2: huang_counter1 = 0 huang_counter2 = 0 standard_deviation = None elif huang_steady_counter == Config.CostMonitorQueSize: standard_deviation = statistics.stdev(cost_monitor) cost_monitor.clear() huang_counter1 = 0 huang_counter2 = 0 huang_steady_counter = 0 print ("\033[36m* COOLING::\033[0m REACHED MAX STEADY STATE... PREPARING FOR COOLING...") else: standard_deviation = None huang_steady_counter += 1 temperature = next_temp(initial_temp, i, iteration_num, temperature, slope, standard_deviation) if Config.SA_AnnealingSchedule == 'Adaptive': if zero_slope_counter == Config.MaxSteadyState: print ("NO IMPROVEMENT POSSIBLE...") break if Config.TerminationCriteria == 'IterationNum': if i == Config.SimulatedAnnealingIteration: print ("REACHED MAXIMUM ITERATION NUMBER...") break elif Config.TerminationCriteria == 'StopTemp': if temperature <= Config.SA_StopTemp: print ("REACHED STOP TEMPERATURE...") break mapping_cost_file.close() mapping_process_file.close() sa_temperature_file.close() sa_cost_slop_file.close() sa_huang_race_file.close() print ("-------------------------------------") print ("STARTING COST:"+str(starting_cost)+"\tFINAL COST:"+str(best_cost)) print ("IMPROVEMENT:"+"{0:.2f}".format(100*(starting_cost-best_cost)/starting_cost)+" %") return best_tg, best_ctg, best_ag