def move_to_next_solution(iteration, tg, ctg, ag, noc_rg, shm, critical_rg,
                          noncritical_rg, logging):

    random_seed = Config.mapping_random_seed
    random.seed(Config.mapping_random_seed)
    for i in range(0, iteration):
        random_seed = random.randint(1, 100000)
    random.seed(random_seed)
    logging.info("Moving to next solution: random_seed: " + str(random_seed) +
                 "    iteration: " + str(iteration))

    cluster_to_move = random.choice(ctg.nodes())
    current_node = ctg.node[cluster_to_move]['Node']
    Mapping_Functions.remove_cluster_from_node(tg, ctg, ag, noc_rg,
                                               critical_rg, noncritical_rg,
                                               cluster_to_move, current_node,
                                               logging)
    destination_node = random.choice(ag.nodes())
    if Config.EnablePartitioning:
        while ctg.node[cluster_to_move]['Criticality'] != ag.node[
                destination_node]['Region']:
            destination_node = random.choice(ag.nodes())
    try_counter = 0
    while not Mapping_Functions.add_cluster_to_node(
            tg, ctg, ag, shm, noc_rg, critical_rg, noncritical_rg,
            cluster_to_move, destination_node, logging):

        # If add_cluster_to_node fails it automatically removes all the connections...
        # we need to add the cluster to the old place...
        Mapping_Functions.add_cluster_to_node(tg, ctg, ag, shm, noc_rg,
                                              critical_rg, noncritical_rg,
                                              cluster_to_move, current_node,
                                              logging)
        try_counter += 1
        if try_counter >= 3 * len(ag.nodes()):
            print(
                "CAN NOT FIND ANY FEASIBLE SOLUTION... ABORTING LOCAL SEARCH..."
            )
            return tg, ctg, ag

        # choosing another cluster to move
        cluster_to_move = random.choice(ctg.nodes())
        current_node = ctg.node[cluster_to_move]['Node']
        Mapping_Functions.remove_cluster_from_node(tg, ctg, ag, noc_rg,
                                                   critical_rg, noncritical_rg,
                                                   cluster_to_move,
                                                   current_node, logging)
        destination_node = random.choice(ag.nodes())
        if Config.EnablePartitioning:
            while ctg.node[cluster_to_move]['Criticality'] != ag.node[
                    destination_node]['Region']:
                destination_node = random.choice(ag.nodes())
    return tg, ctg, ag
def move_to_next_solution(iteration, tg, ctg, ag, noc_rg, shm, critical_rg, noncritical_rg, logging):

    random_seed = Config.mapping_random_seed
    random.seed(Config.mapping_random_seed)
    for i in range(0, iteration):
        random_seed = random.randint(1, 100000)
    random.seed(random_seed)
    logging.info("Moving to next solution: random_seed: "+str(random_seed)+"    iteration: "+str(iteration))

    cluster_to_move = random.choice(ctg.nodes())
    current_node = ctg.node[cluster_to_move]['Node']
    Mapping_Functions.remove_cluster_from_node(tg, ctg, ag, noc_rg, critical_rg, noncritical_rg,
                                               cluster_to_move, current_node, logging)
    destination_node = random.choice(ag.nodes())
    if Config.EnablePartitioning:
        while ctg.node[cluster_to_move]['Criticality'] != ag.node[destination_node]['Region']:
            destination_node = random.choice(ag.nodes())
    try_counter = 0
    while not Mapping_Functions.add_cluster_to_node(tg, ctg, ag, shm, noc_rg, critical_rg, noncritical_rg,
                                                    cluster_to_move, destination_node, logging):

            # If add_cluster_to_node fails it automatically removes all the connections...
            # we need to add the cluster to the old place...
            Mapping_Functions.add_cluster_to_node(tg, ctg, ag, shm, noc_rg, critical_rg, noncritical_rg,
                                                  cluster_to_move, current_node, logging)
            try_counter += 1
            if try_counter >= 3*len(ag.nodes()):
                print ("CAN NOT FIND ANY FEASIBLE SOLUTION... ABORTING LOCAL SEARCH...")
                return tg, ctg, ag

            # choosing another cluster to move
            cluster_to_move = random.choice(ctg.nodes())
            current_node = ctg.node[cluster_to_move]['Node']
            Mapping_Functions.remove_cluster_from_node(tg, ctg, ag, noc_rg, critical_rg, noncritical_rg,
                                                       cluster_to_move, current_node, logging)
            destination_node = random.choice(ag.nodes())
            if Config.EnablePartitioning:
                while ctg.node[cluster_to_move]['Criticality'] != ag.node[destination_node]['Region']:
                    destination_node = random.choice(ag.nodes())
    return tg, ctg, ag
예제 #3
0
def mapping_opt_local_search(tg, ctg, ag, noc_rg, critical_rg, noncritical_rg, shm,
                             iteration_num, report, detailed_report, logging,
                             cost_data_file_name, mapping_process_file_name, random_seed,
                             initial_mapping_string=None):
    random.seed(random_seed)
    if report:
        print("===========================================")
        print("STARTING MAPPING OPTIMIZATION...USING LOCAL SEARCH...")
        print("NUMBER OF ITERATIONS: "+str(iteration_num))

    if type(cost_data_file_name) is str:
        mapping_cost_file = open('Generated_Files/Internal/'+cost_data_file_name+'.txt', 'a')
    else:
        raise ValueError("cost_data_file_name name is not string: "+str(cost_data_file_name))

    if type(mapping_process_file_name) is str:
        mapping_process_file = open('Generated_Files/Internal/'+mapping_process_file_name+'.txt', 'a')
    else:
        raise ValueError("mapping_process_file name is not string: "+str(mapping_process_file_name))

    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, initial_mapping_string=initial_mapping_string)
    starting_cost = best_cost
    for iteration in range(0, iteration_num):
        logging.info("       ITERATION:"+str(iteration))
        cluster_to_move = random.choice(list(ctg.nodes()))
        current_node = ctg.node[cluster_to_move]['Node']
        Mapping_Functions.remove_cluster_from_node(tg, ctg, ag, noc_rg, critical_rg, noncritical_rg,
                                                   cluster_to_move, current_node, logging)
        destination_node = random.choice(list(ag.nodes()))
        if Config.EnablePartitioning:
            while ctg.node[cluster_to_move]['Criticality'] != ag.node[destination_node]['Region']:
                destination_node = random.choice(list(ag.nodes()))
        # print(ctg.node[cluster_to_move]['Criticality'],AG.node[destination_node]['Region'])

        try_counter = 0
        while not Mapping_Functions.add_cluster_to_node(tg, ctg, ag, shm, noc_rg, critical_rg, noncritical_rg,
                                                        cluster_to_move, destination_node, logging):

            # If add_cluster_to_node fails it automatically removes all the connections...
            # we need to add the cluster to the old place...
            Mapping_Functions.add_cluster_to_node(tg, ctg, ag, shm, noc_rg, critical_rg, noncritical_rg,
                                                  cluster_to_move, current_node, logging)

            # choosing another cluster to move
            cluster_to_move = random.choice(list(ctg.nodes()))
            current_node = ctg.node[cluster_to_move]['Node']
            Mapping_Functions.remove_cluster_from_node(tg, ctg, ag, noc_rg, critical_rg, noncritical_rg,
                                                       cluster_to_move, current_node, logging)
            destination_node = random.choice(list(ag.nodes()))
            if Config.EnablePartitioning:
                while ctg.node[cluster_to_move]['Criticality'] != ag.node[destination_node]['Region']:
                    destination_node = random.choice(list(ag.nodes()))
            # print(ctg.node[cluster_to_move]['Criticality'],AG.node[destination_node]['Region'])

            if try_counter >= 3*len(ag.nodes()):
                if report:
                    print("CAN NOT FIND ANY FEASIBLE SOLUTION... ABORTING LOCAL SEARCH...")
                logging.info("CAN NOT FIND ANY FEASIBLE SOLUTION... ABORTING LOCAL SEARCH...")
                tg = copy.deepcopy(best_tg)
                ag = copy.deepcopy(best_ag)
                ctg = copy.deepcopy(ctg)
                if report:
                    Scheduling_Reports.report_mapped_tasks(ag, logging)
                    Mapping_Functions.mapping_cost_function(tg, ag, shm, True, initial_mapping_string=initial_mapping_string)
                return best_tg, best_ctg, best_ag
            try_counter += 1

        Scheduling_Functions.clear_scheduling(ag)
        Scheduler.schedule_all(tg, ag, shm, False, logging)

        current_cost = Mapping_Functions.mapping_cost_function(tg, ag, shm, detailed_report, initial_mapping_string= initial_mapping_string)
        mapping_process_file.write(Mapping_Functions.mapping_into_string(tg)+"\n")
        mapping_cost_file.write(str(current_cost)+"\n")
        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(tg)
            best_ag = copy.deepcopy(ag)
            best_ctg = copy.deepcopy(ctg)
            best_cost = current_cost
        else:
            tg = copy.deepcopy(best_tg)
            ag = copy.deepcopy(best_ag)
            ctg = copy.deepcopy(best_ctg)
            mapping_process_file.write(Mapping_Functions.mapping_into_string(tg)+"\n")

    Scheduling_Functions.clear_scheduling(ag)
    Scheduler.schedule_all(tg, ag, shm, False, logging)
    mapping_process_file.close()
    mapping_cost_file.close()
    if report:
        print("-------------------------------------")
        print("STARTING COST: "+str(starting_cost)+"\tFINAL COST: "+str(best_cost) +
               "\tAFTER "+str(iteration_num)+" ITERATIONS")
        print("IMPROVEMENT:"+str("{0:.2f}".format(100*(starting_cost-best_cost)/starting_cost))+" %")
    return best_tg, best_ctg, best_ag
예제 #4
0
def mapping_opt_local_search(tg, ctg, ag, noc_rg, critical_rg, noncritical_rg, shm,
                             iteration_num, report, detailed_report, logging,
                             cost_data_file_name, mapping_process_file_name, random_seed,
                             initial_mapping_string=None):
    random.seed(random_seed)
    if report:
        print ("===========================================")
        print ("STARTING MAPPING OPTIMIZATION...USING LOCAL SEARCH...")
        print ("NUMBER OF ITERATIONS: "+str(iteration_num))

    if type(cost_data_file_name) is str:
        mapping_cost_file = open('Generated_Files/Internal/'+cost_data_file_name+'.txt', 'a')
    else:
        raise ValueError("cost_data_file_name name is not string: "+str(cost_data_file_name))

    if type(mapping_process_file_name) is str:
        mapping_process_file = open('Generated_Files/Internal/'+mapping_process_file_name+'.txt', 'a')
    else:
        raise ValueError("mapping_process_file name is not string: "+str(mapping_process_file_name))

    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, initial_mapping_string=initial_mapping_string)
    starting_cost = best_cost
    for iteration in range(0, iteration_num):
        logging.info("       ITERATION:"+str(iteration))
        cluster_to_move = random.choice(ctg.nodes())
        current_node = ctg.node[cluster_to_move]['Node']
        Mapping_Functions.remove_cluster_from_node(tg, ctg, ag, noc_rg, critical_rg, noncritical_rg,
                                                   cluster_to_move, current_node, logging)
        destination_node = random.choice(ag.nodes())
        if Config.EnablePartitioning:
            while ctg.node[cluster_to_move]['Criticality'] != ag.node[destination_node]['Region']:
                destination_node = random.choice(ag.nodes())
        # print (ctg.node[cluster_to_move]['Criticality'],AG.node[destination_node]['Region'])

        try_counter = 0
        while not Mapping_Functions.add_cluster_to_node(tg, ctg, ag, shm, noc_rg, critical_rg, noncritical_rg,
                                                        cluster_to_move, destination_node, logging):

            # If add_cluster_to_node fails it automatically removes all the connections...
            # we need to add the cluster to the old place...
            Mapping_Functions.add_cluster_to_node(tg, ctg, ag, shm, noc_rg, critical_rg, noncritical_rg,
                                                  cluster_to_move, current_node, logging)

            # choosing another cluster to move
            cluster_to_move = random.choice(ctg.nodes())
            current_node = ctg.node[cluster_to_move]['Node']
            Mapping_Functions.remove_cluster_from_node(tg, ctg, ag, noc_rg, critical_rg, noncritical_rg,
                                                       cluster_to_move, current_node, logging)
            destination_node = random.choice(ag.nodes())
            if Config.EnablePartitioning:
                while ctg.node[cluster_to_move]['Criticality'] != ag.node[destination_node]['Region']:
                    destination_node = random.choice(ag.nodes())
            # print (ctg.node[cluster_to_move]['Criticality'],AG.node[destination_node]['Region'])

            if try_counter >= 3*len(ag.nodes()):
                if report:
                    print ("CAN NOT FIND ANY FEASIBLE SOLUTION... ABORTING LOCAL SEARCH...")
                logging.info("CAN NOT FIND ANY FEASIBLE SOLUTION... ABORTING LOCAL SEARCH...")
                tg = copy.deepcopy(best_tg)
                ag = copy.deepcopy(best_ag)
                ctg = copy.deepcopy(ctg)
                if report:
                    Scheduling_Reports.report_mapped_tasks(ag, logging)
                    Mapping_Functions.mapping_cost_function(tg, ag, shm, True, initial_mapping_string=initial_mapping_string)
                return best_tg, best_ctg, best_ag
            try_counter += 1

        Scheduling_Functions.clear_scheduling(ag)
        Scheduler.schedule_all(tg, ag, shm, False, logging)

        current_cost = Mapping_Functions.mapping_cost_function(tg, ag, shm, detailed_report, initial_mapping_string= initial_mapping_string)
        mapping_process_file.write(Mapping_Functions.mapping_into_string(tg)+"\n")
        mapping_cost_file.write(str(current_cost)+"\n")
        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(tg)
            best_ag = copy.deepcopy(ag)
            best_ctg = copy.deepcopy(ctg)
            best_cost = current_cost
        else:
            tg = copy.deepcopy(best_tg)
            ag = copy.deepcopy(best_ag)
            ctg = copy.deepcopy(best_ctg)
            mapping_process_file.write(Mapping_Functions.mapping_into_string(tg)+"\n")

    Scheduling_Functions.clear_scheduling(ag)
    Scheduler.schedule_all(tg, ag, shm, False, logging)
    mapping_process_file.close()
    mapping_cost_file.close()
    if report:
        print ("-------------------------------------")
        print ("STARTING COST: "+str(starting_cost)+"\tFINAL COST: "+str(best_cost) +
               "\tAFTER "+str(iteration_num)+" ITERATIONS")
        print ("IMPROVEMENT:"+str("{0:.2f}".format(100*(starting_cost-best_cost)/starting_cost))+" %")
    return best_tg, best_ctg, best_ag