コード例 #1
0
def get_scenario(duration):
    """ Call to scenario_builder for generating a new scenario and receive the file where the new scenario is saved """

    logger = logging.getLogger(__name__)

    # Create a TCP/IP socket
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    # Connect the socket to the port where the server is listening
    server_address = (HOST, PORT)
    logger.info('Connecting to %s port %s' % server_address)
    sock.connect(server_address)

    # The protocol is implemented by sending the message lenght before sending the message itself
    CONFIG = settings.update_CONFIG()

    js_obj = {
        "duration": duration,
        "clusters_amount": str(CONFIG['clusters_amount']),
        'area_dimens': CONFIG['area_dimens']
    }
    message = json.dumps(js_obj)
    data_size = len(message)
    sock.sendall(str(data_size))  # Send data size

    # wait for ACK
    data = sock.recv(BUFFER_SIZE)

    if data == 'ACK':
        sock.sendall(message)  # send the actual message

        # wait for response size
        data_size = sock.recv(BUFFER_SIZE)
        data = sock.recv(int(data_size))

    else:
        pass

    logger.info('Closing socket to %s port %s' % server_address)

    sock.shutdown(SHUT_RDWR)
    sock.close()

    return data
コード例 #2
0
def listen_requests():
    # Create a TCP/IP socket
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    # Bind the server to the port
    server_address = ('localhost', 10000
                      )  # This follows the syntax (host, port)
    print 'starting up on %s port %s' % server_address
    sock.bind(server_address)

    # Listen for incoming connections
    sock.listen(1)

    # Wait for a connection
    print 'waiting for a connection'

    connection, client_address = sock.accept()
    print 'connection from', client_address

    # Receive the data
    while True:
        data_size = connection.recv(BUFFER_SIZE)

        if data_size:
            break

    connection.sendall('ACK')

    data = connection.recv(int(data_size))
    print 'received %s ' % data

    json_parsed = json.loads(data)
    duration = int(json_parsed["duration"])
    clusters_amount = int(json_parsed['clusters_amount'])
    area_dimens = [
        int(json_parsed["area_dimens"][0]),
        int(json_parsed["area_dimens"][1])
    ]

    # call to generate scenario
    settings.DURATION = duration
    settings.AREA_DIMENS = area_dimens
    settings.CLUSTERS_AMOUNT = clusters_amount
    CONFIG = settings.update_CONFIG()

    folder_path, clusters, nodes_amount = create_n_scenarios.generate_scenarios(
        CONFIG)

    # generate json-like object
    js_obj = {
        "scenario": folder_path,
        "clusters": clusters,
        "nodes_amount": nodes_amount
    }
    message = json.dumps(js_obj)

    data_size = len(message)

    connection.sendall(str(data_size))

    time.sleep(10)

    connection.sendall(message)

    # Clean up the connection
    print 'Closing server connection'
    connection.shutdown(SHUT_RDWR)
    connection.close()
コード例 #3
0
def launch_fourcorners_and_char(LAUNCH_CONFIG):

    logger = logging.getLogger(__name__)

    # Configuration flags
    # -------------------
    pso_flag = LAUNCH_CONFIG['pso']
    lawn_mower_flag = LAUNCH_CONFIG['lawn']
    exception_flag = LAUNCH_CONFIG['exception']
    pso_iterations = LAUNCH_CONFIG['pso_iter']
    lawn_iterations = LAUNCH_CONFIG['lawn_iter']
    first_dr_pos = LAUNCH_CONFIG['first_drones_pos']

    t_start = time.time()

    # Generate lawn mower trajectories for defining the duration
    #-----------------------------------------------------------
    logger.info(
        'Calculating the duration of the simulation from lawn_mower algo')

    CONFIG = settings.update_CONFIG()
    traj_per_drone = lawn_mower.gen_lawnmower_traj(CONFIG)
    settings.lm_trajectories = traj_per_drone

    duration = len(traj_per_drone[0])
    logger.info('Duration: ' + str(duration))

    # Generate scenario
    #------------------
    # call to scenario_builder through socket
    logger.info(
        'Using socket to get the scenario_builder generating the scenario')

    message = client_socket.get_scenario(duration)

    js_obj = json.loads(message)
    folder_path = js_obj['scenario']
    clusters = js_obj['clusters']
    settings.NODES_AMOUNT = js_obj['nodes_amount']

    folder = folder_path[-13:]
    relative_folder = './scenarios/' + folder + '/'
    command = ['mkdir', relative_folder]
    subprocess.call(command)

    # save new scenario from /scenario_builder folder to /scenario folder
    logger.info('Moving the new scenario to /scenarios folder')

    command = ['find', folder_path, '-maxdepth', '1', '-type', 'f']
    popen_proc = Popen(command, stdout=PIPE)
    files_to_copy = popen_proc.stdout.read()
    files_to_copy = files_to_copy.split('\n')
    del files_to_copy[-1]

    for file_i in files_to_copy:
        command = ['cp', file_i, relative_folder]
        subprocess.call(command)

    # get .wml filename
    logger.info('Finding the main .wml file')

    command = ['ls', relative_folder]
    popen_proc = Popen(command, stdout=PIPE)
    filename_l = popen_proc.stdout.read()

    # get '.wml' file name
    filename_l = filename_l.split('\n')
    for file_i in filename_l:
        if file_i.endswith('.wml'):
            wml_filename = file_i
        else:
            pass

    wml_full_path = relative_folder + wml_filename

    # save the scenario path in settings
    logger.info('Saving scenario path in settings file')
    settings.INPUT_FILE = wml_full_path

    CONFIG = settings.update_CONFIG()

    if CONFIG['f_duration'] == 0:
        settings.DURATION = duration
    else:
        # Leave the duration specified on the settings file
        pass

    settings.CLUSTERS = clusters

    #  SIM_CORE calls
    #-----------------

    timestamp_main = timing.timestamp_gen()
    logger.info('Creating output folder')

    main_output_folder = './outputs/' + str(timestamp_main)
    command = ['mkdir', main_output_folder]
    subprocess.call(command)

    # Generate pso parameters
    #------------------------
    logger.info('Generating sets of pso parameters values')
    pso_vals = pso.generate_pso_params()

    for pso_mode_i in pso_vals:

        mode = pso_mode_i[0]
        lb_vals = pso_mode_i[1]
        nb_vals = pso_mode_i[2]

        for val_i in range(len(lb_vals)):

            # inertia weight has not to be set
            settings.beta = (0, lb_vals[val_i])  # local best weight
            settings.gamma = (0, nb_vals[val_i])  # global best weight
            logger.info('Parameter set: mode=' + str(mode) + '\t lb=' +
                        str(settings.beta) + '\t nb=' + str(settings.gamma))

            #  SIM_CORE calls
            #-----------------
            logger.info('Creating output folder')

            charac_folder = main_output_folder + '/' + str(mode) + '_' + str(
                lb_vals[val_i]) + '_' + str(nb_vals[val_i])
            command = ['mkdir', charac_folder]
            subprocess.call(command)

            settings.MAIN_OUTPUT_FOLDER = charac_folder

            # create pso params file
            CONFIG = settings.update_CONFIG()

            report_file = file_helper.create_pso_vals(timestamp_main,
                                                      charac_folder)
            report_file.write('PSO values:\n')
            report_file.write('mode: ' + mode + '\n')
            report_file.write('inertia final val: ' +
                              str(1 - lb_vals[val_i] - nb_vals[val_i]) + '\n')
            report_file.write('local best final val: ' + str(lb_vals[val_i]) +
                              '\n')
            report_file.write('global best final val: ' + str(nb_vals[val_i]) +
                              '\n')

            report_file.write('inertia only time: [0,' +
                              str(CONFIG['pso_params'][3]) + ']\n')
            report_file.write('lb time: [' + str(CONFIG['pso_params'][3]) +
                              ',' + str(CONFIG['pso_params'][5]) + ']\n')
            report_file.write('nb time: [' + str(CONFIG['pso_params'][5]) +
                              ',' + str(CONFIG['duration']) + ']\n')

            # PSO
            #-----
            if pso_flag == 1:
                pso_folder = charac_folder + '/pso'
                command = ['mkdir', pso_folder]
                subprocess.call(command)

                settings.ALGORITHM = 'pso'
                CONFIG = settings.update_CONFIG()

                for i in range(pso_iterations):
                    for first_pos in first_dr_pos:

                        if first_pos == 'corner_bottom_left':
                            pos_label = '_bl'
                        elif first_pos == 'corner_bottom_right':
                            pos_label = '_br'
                        elif first_pos == 'corner_top_left':
                            pos_label = '_tl'
                        elif first_pos == 'corner_top_right':
                            pos_label = '_tr'
                        else:
                            pass

                        sub_timestamp = timing.timestamp_gen()
                        sub_folder = pso_folder + '/' + str(
                            sub_timestamp) + pos_label
                        command = ['mkdir', sub_folder]
                        subprocess.call(command)

                        settings.INIT_DISTRIB = first_pos
                        settings.OUTPUT_FOLDER = sub_folder
                        settings.CURRENT_ITERATION = i
                        logger.info(str(settings.ALGORITHM)+' algorithm: iteration '+str(i)\
                            +'\n\t initial drones positions: '+pos_label)

                        if exception_flag == 1:
                            try:
                                logger.info('Calling sim_core')
                                sim_core.simulator()
                            except (BaseException, KeyboardInterrupt):
                                logger.debug('pso iteration: '+str(i)+' -- Exception caught:' \
                                     +'\n\tfilename: '+sys.exc_info()[2].tb_frame.f_code.co_filename \
                                     +'\n\tlineno: '+str(sys.exc_info()[2].tb_lineno) \
                                     +'\n\tname: '+sys.exc_info()[2].tb_frame.f_code.co_name \
                                     +'\n\ttype: '+sys.exc_info()[0].__name__ \
                                     +'\n\tmessage: '+sys.exc_info()[1].message)

                        else:
                            logger.info('Calling sim_core')
                            sim_core.simulator()

                #  Create averaged charts and maps
                #-----------------------------------
                logger.info('Generating average charts for: ' +
                            settings.ALGORITHM)

                settings.TIMESTAMP = timestamp_main  # averaged charts will be generated with main timestamp
                CONFIG = settings.update_CONFIG()
                avg_charts.plot_all(settings.FILES, CONFIG, report_file)

                logger.info('Average charts generated for: ' +
                            settings.ALGORITHM)

            # Clean state after pso simulation
            settings.FILES = []
            report_file.close()

            logger.info('PSO sims terminated')

    # Lawn mower
    #------------
    if lawn_mower_flag == 1:
        lawn_sim_folder = main_output_folder + '/x_lawn_sim'
        command = ['mkdir', lawn_sim_folder]
        subprocess.call(command)

        settings.MAIN_OUTPUT_FOLDER = lawn_sim_folder

        lawn_folder = lawn_sim_folder + '/lawn'
        command = ['mkdir', lawn_folder]
        subprocess.call(command)

        settings.ALGORITHM = 'lawn_mower'
        CONFIG = settings.update_CONFIG()

        report_file = file_helper.create_pso_vals(timestamp_main,
                                                  lawn_sim_folder)
        report_file.write('Lawn_mower values:\n')
        report_file.write('entire sweep duration: ' + str(CONFIG['duration']) +
                          '\n')

        for i in range(
                lawn_iterations
        ):  #range(iterations) will be used in the loop when there is random variables in the lawn mower algo
            for first_pos in first_dr_pos:

                if first_pos == 'corner_bottom_left':
                    pos_label = '_bl'
                elif first_pos == 'corner_bottom_right':
                    pos_label = '_br'
                elif first_pos == 'corner_top_left':
                    pos_label = '_tl'
                elif first_pos == 'corner_top_right':
                    pos_label = '_tr'
                else:
                    pass

                sub_timestamp = timing.timestamp_gen()
                sub_folder = lawn_folder + '/' + str(sub_timestamp) + pos_label
                command = ['mkdir', sub_folder]
                subprocess.call(command)

                settings.INIT_DISTRIB = first_pos
                settings.OUTPUT_FOLDER = sub_folder
                settings.CURRENT_ITERATION = i
                logger.info(str(settings.ALGORITHM)+' algorithm: iteration '+str(i)\
                    +'\n\t initial drones positions: '+pos_label)

                if exception_flag == 1:
                    try:
                        logger.info('Calling sim_core')
                        sim_core.simulator()
                    except (BaseException, KeyboardInterrupt):
                        logger.debug('lawn_mower iteration: '+str(i)+' -- Exception caught:' \
                             +'\n\tfilename: '+sys.exc_info()[2].tb_frame.f_code.co_filename \
                             +'\n\tlineno: '+str(sys.exc_info()[2].tb_lineno) \
                             +'\n\tname: '+sys.exc_info()[2].tb_frame.f_code.co_name \
                             +'\n\ttype: '+sys.exc_info()[0].__name__ \
                             +'\n\tmessage: '+sys.exc_info()[1].message)

                else:
                    logger.info('Calling sim_core')
                    sim_core.simulator()

        #  Create averaged charts and maps
        #-----------------------------------
        logger.info('Generating average charts for: ' + settings.ALGORITHM)

        settings.TIMESTAMP = timestamp_main  # averaged charts will be generated with main timestamp
        CONFIG = settings.update_CONFIG()
        avg_charts.plot_all(settings.FILES, CONFIG, report_file)

        logger.info('Average charts generated for: ' + settings.ALGORITHM)

    # Close files
    #-------------
    logger.info('All simulations terminated')

    report_file.close()

    t_end = time.time()
    sim_duration = (t_end - t_start)  # simulation total duration
    mins = int(sim_duration / 60)  # (minutes)
    secs = int(sim_duration % 60)

    logger.info('Simulation duration: ' + str(mins) + ' mins ' + str(secs) +
                ' secs \n')

    # Clean state after pso simulation
    # Necessary when running the characterization for avoiding last lawn_mower being plot with the next pso
    settings.FILES = []

    # create a single report with all the report files
    #-------------------------------------------------
    logger.info('Creating general report file')

    # get the folders corresponding to the simulation
    all_files_l = os.listdir(main_output_folder)
    all_sorted_files = sorted(all_files_l)

    # create the general report file
    gen_report_filename = main_output_folder + '/' + str(
        timestamp_main) + '_gen_report'
    command = ['touch', gen_report_filename]
    subprocess.call(command)

    gen_report_file = open(gen_report_filename, "w+")

    for file_i in all_sorted_files:
        # create full path to file
        file_i_path = main_output_folder + '/' + file_i

        # if is a folder we look for the report file of that folder
        if os.path.isdir(file_i_path):

            val_filename = file_i_path + '/' + str(
                timestamp_main) + '_pso_vals'
            val_file = open(val_filename, "r")

            for line in val_file:
                gen_report_file.write(line)

            val_file.close()
            gen_report_file.write(
                '\n\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n'
            )

    gen_report_file.close()

    logger.info('General report created')
    logger.info('Everything terminated')
コード例 #4
0
ファイル: sim_core.py プロジェクト: jchanger/dpsou_mobmod
def simulator():

    logger = logging.getLogger(__name__)

    #    INPUTS VALUES
    ########################

    settings.TIMESTAMP = timing.timestamp_gen()  # modifies only local CONFIG
    CONFIG = settings.update_CONFIG()

    #    Input checking
    ###########################

    # Checking that inputs values have proper values. If the inputs are not correct values
    # for the simulation, the program exits with 'sys.exit()'
    logger.info('Checking input values')
    inputs.check_algo_name(CONFIG['algorithm'])
    inputs.check_drones_amount(CONFIG['drones_amount'])
    inputs.check_initial_pos_mode(CONFIG['init_distrib'])

    #   Start counting time
    ##########################

    # Time variable for controlling simulation duration
    t_sim_START = time.time()

    #   Files creation
    #####################
    logger.info('Creating simulation files')
    param_file, output_file = file_helper.create_files(CONFIG)
    output.param_file_write(CONFIG, param_file)

    out_filename = output_file.name  # get the name for the chart at the end of the module
    settings.FILES.append(
        out_filename)  # appends to the list of '_values' files in settings

    #   Objects creation
    ###############################
    logger.info('Creating simulation objects: drones, nodes and sim_network')

    # Networking object/server creation
    #----------------------------------
    sim_network = networking.Network(CONFIG)

    # Scenario loading process
    #-------------------------
    nodes = initialize.create_scenario(CONFIG, sim_network)

    # Drones objects generation
    #--------------------------
    drones = initialize.create_drones(CONFIG, sim_network)

    # if lawn mower,  trajectories are generated and set within 'initialize' module

    logger.info('Starting simulation main loop')

    #     Sim. Loop
    #---------------------

    # The for loop runs until the simulation time has finished
    for t_current in range(0, int(CONFIG['duration'])):

        # Printing simulation progress (each 10 seconds for avoiding excessive messages)
        if t_current % 10 == 0:
            logger.info('Current time = ' + str(t_current) +
                        '; Progress = %d%% ' %
                        (100 * t_current / int(CONFIG['duration'])))

        # Communications
        #---------------
        #logger.debug('Drones communicate')
        for drone_i in drones:
            drone_i.communicate()

        #logger.debug('Nodes communicate')
        for node_i in nodes:
            node_i.communicate()

        # Algorithm call for calculating the next position
        #-------------------------------------------------
        #logger.debug('Drones calculate next destination')
        for drone_i in drones:
            drone_i.calc_next_destination()

        # Logging drones information to output file
        #------------------------------------------
        #logger.debug('Drones logging to file')
        for drone_i in drones:
            drone_i.drone_logging(t_current, output_file, CONFIG)

        # Move to next position
        #----------------------
        #logger.debug('Drones move')
        for drone_i in drones:
            drone_i.move()

        #logger.debug('Nodes move')
        for node_i in nodes:
            node_i.move()

        # Update drones and nodes time
        #-----------------------------
        #logger.debug('Drones update time')
        for drone_i in drones:
            drone_i.update_time()

        #logger.debug('Nodes update time')
        for node_i in nodes:
            node_i.update_time()

    #   Closing section
    ##########################

    logger.info('Main iteration loop finished')

    # Calculate simulation time
    t_sim_TOTAL = timing.get_sim_duration(t_sim_START)

    # Write the simulation time on the parameters file
    output.param_file_write(None, param_file, t_sim_TOTAL)

    # Final message print on screen
    output.param_screen(None, t_sim_TOTAL)

    file_helper.close_files(param_file, output_file)

    #   Plotting section
    ######################

    if CONFIG['individual_charts'] == 1:
        logger.info('Generating individual graphics')

        # Create charts after files are closed
        drone_charts.plot_all(out_filename, CONFIG)

        logger.info('Sim_core iteration terminated')
コード例 #5
0
ファイル: launcher_one.py プロジェクト: jchanger/dpsou_mobmod
def launcher_local_scen(LAUNCH_CONFIG, input_file_local):

    logger = logging.getLogger(__name__)

    # Configuration flags
    # -------------------
    pso_flag = LAUNCH_CONFIG['pso']
    lawn_mower_flag = LAUNCH_CONFIG['lawn']
    pso_iterations = LAUNCH_CONFIG['pso_iter']
    lawn_iterations = LAUNCH_CONFIG['lawn_iter']

    logger.info('Reading local scenario from: ' + input_file_local)
    settings.INPUT_FILE = input_file_local

    t_start = time.time()

    # Generate lawn mower trajectories (run this wit debug to know the duration of an entire sweep with lawn_mower,
    # then generate the scenario manually, copy it to ./scenarios folder within this project, copy the path to settings
    # and also the new duration and run an entire simulation for getting the results)
    #---------------------------------
    logger.info(
        'Calculating the duration of the simulation from lawn_mower algo')

    CONFIG = settings.update_CONFIG()
    traj_per_drone = lawn_mower.gen_lawnmower_traj(CONFIG)
    settings.lm_trajectories = traj_per_drone

    duration = len(traj_per_drone[0])
    logger.info('Duration: ' + str(duration))

    if CONFIG['f_duration'] == 0:
        settings.DURATION = duration
    else:
        # Leave the duration specified on the settings file
        pass

    #  SIM_CORE calls
    #-----------------

    timestamp_main = timing.timestamp_gen()

    main_output_folder = './outputs/' + str(timestamp_main)
    command = ['mkdir', main_output_folder]
    subprocess.call(command)

    settings.MAIN_OUTPUT_FOLDER = main_output_folder

    # PSO
    #-----
    if pso_flag == 1:
        pso_folder = main_output_folder + '/pso'
        command = ['mkdir', pso_folder]
        subprocess.call(command)

        settings.ALGORITHM = 'pso'

        for i in range(pso_iterations):
            sub_timestamp = timing.timestamp_gen()
            sub_folder = pso_folder + '/' + str(sub_timestamp)
            command = ['mkdir', sub_folder]
            subprocess.call(command)

            settings.OUTPUT_FOLDER = sub_folder
            settings.CURRENT_ITERATION = i
            logger.info(
                str(settings.ALGORITHM) + ' algorithm: iteration ' + str(i))

            logger.info('Calling sim_core')
            sim_core.simulator()

        #  Create averaged charts and maps
        #-----------------------------------
        logger.info('Generating average charts for: ' + settings.ALGORITHM)
        settings.TIMESTAMP = timestamp_main  # averaged charts will be generated with main timestamp
        CONFIG = settings.update_CONFIG()
        avg_charts.plot_all(settings.FILES, CONFIG)
        logger.info('Average charts generated for: ' + settings.ALGORITHM)

    # Clean state after pso simulation
    settings.FILES = []

    # Lawn mower
    #------------
    if lawn_mower_flag == 1:
        lawn_folder = main_output_folder + '/lawn'
        command = ['mkdir', lawn_folder]
        subprocess.call(command)

        settings.ALGORITHM = 'lawn_mower'

        for i in range(
                lawn_iterations
        ):  #range(iterations) will be used in the loop when there is random variables in the lawn mower algo
            sub_timestamp = timing.timestamp_gen()
            sub_folder = lawn_folder + '/' + str(sub_timestamp)
            command = ['mkdir', sub_folder]
            subprocess.call(command)

            settings.OUTPUT_FOLDER = sub_folder
            settings.CURRENT_ITERATION = i
            logger.info(
                str(settings.ALGORITHM) + ' algorithm: iteration ' + str(i))

            logger.info('Calling sim_core')
            sim_core.simulator()

        #  Create averaged charts and maps
        #-----------------------------------
        logger.info('Generating average charts for: ' + settings.ALGORITHM)
        settings.TIMESTAMP = timestamp_main  # averaged charts will be generated with main timestamp
        CONFIG = settings.update_CONFIG()
        avg_charts.plot_all(settings.FILES, CONFIG)
        logger.info('Average charts generated for: ' + settings.ALGORITHM)

    # Close files
    #-------------
    logger.info('All simulations terminated')

    t_end = time.time()
    sim_duration = (t_end - t_start)  # simulation total duration
    mins = int(sim_duration / 60)  # (minutes)
    secs = int(sim_duration % 60)

    logger.info('Simulation duration: ' + str(mins) + ' mins ' + str(secs) +
                ' secs \n')
コード例 #6
0
def launch_fourcorners(LAUNCH_CONFIG):

    logger = logging.getLogger(__name__)
    
    # Configuration flags
    # -------------------
    pso_flag = LAUNCH_CONFIG['pso']
    lawn_mower_flag = LAUNCH_CONFIG['lawn']
    exception_flag = LAUNCH_CONFIG['exception']
    pso_iterations = LAUNCH_CONFIG['pso_iter']
    lawn_iterations = LAUNCH_CONFIG['lawn_iter']
    first_dr_pos = LAUNCH_CONFIG['first_drones_pos']

    t_start = time.time()
    
    # Generate lawn mower trajectories for defining the duration
    #-----------------------------------------------------------
    logger.info('Calculating the duration of the simulation from lawn_mower algo')
    
    CONFIG = settings.update_CONFIG()
    traj_per_drone = lawn_mower.gen_lawnmower_traj(CONFIG)
    settings.lm_trajectories = traj_per_drone
    
    duration = len(traj_per_drone[0])
    logger.info('Duration: '+str(duration))
    
    # Generate scenario
    #------------------
    # call to scenario_builder through socket
    logger.info('Using socket to get the scenario_builder generating the scenario')
    
    message = client_socket.get_scenario(duration)
    
    js_obj = json.loads(message)
    rec_folder_path = js_obj['scenario']
    clusters = js_obj['clusters']
    settings.NODES_AMOUNT = js_obj['nodes_amount']
    
    folder_name = rec_folder_path.split('/')[-1]
    folder_path = app_settings.MAIN_APP_PATH+'/auav/src/scenarios/'+folder_name+'/'
    command = ['mkdir',folder_path]
    subprocess.call(command)
    
    # save new scenario from /scenario_builder folder to /scenario folder
    logger.info('Moving the new scenario to /scenarios folder')
    
    command = ['find',rec_folder_path,'-maxdepth','1','-type','f']
    popen_proc = Popen(command,stdout=PIPE)
    files_to_copy = popen_proc.stdout.read()
    files_to_copy = files_to_copy.split('\n')
    del files_to_copy[-1]
    
    for file_i in files_to_copy:
        command = ['cp', file_i, folder_path]
        subprocess.call(command)
    
    # get .wml filename
    logger.info('Finding the main .wml file')
    
    command = ['ls', folder_path]
    popen_proc = Popen(command,stdout=PIPE)
    filename_l = popen_proc.stdout.read()
    
    # get '.wml' file name
    filename_l = filename_l.split('\n')
    for file_i in filename_l:
        if file_i.endswith('.wml'):
            wml_filename = file_i
            break
        else:
            pass
    
    wml_full_path = folder_path+wml_filename
    
    # save the scenario path in settings
    logger.info('Saving scenario path in settings file')
    settings.INPUT_FILE = wml_full_path
    
    CONFIG = settings.update_CONFIG()
    
    if CONFIG['f_duration'] == 0:
        settings.DURATION = duration
    else:
        # Leave the duration specified on the settings file
        pass
    
    settings.CLUSTERS = clusters
    
    #  SIM_CORE calls
    #-----------------
    
    timestamp_main = timing.timestamp_gen()
    logger.info('Creating output folder')

    main_output_folder = app_settings.MAIN_APP_PATH+'/auav/src/outputs/'+str(timestamp_main)
    command = ['mkdir', main_output_folder]
    subprocess.call(command)
    
    settings.MAIN_OUTPUT_FOLDER = main_output_folder
    
    # report file for storing the simulation data
    report_file = file_helper.create_pso_vals(timestamp_main,main_output_folder)
    report_file.write('PSO values:\n')
    report_file.write('inertia final val: '+str(1-settings.beta[1]-settings.gamma[1])+'\n')
    report_file.write('local best final val: '+str(settings.beta[1])+'\n')
    report_file.write('global best final val: '+str(settings.gamma[1])+'\n')
     
    report_file.write('inertia only time: [0,'+str(CONFIG['pso_params'][3])+']\n')
    report_file.write('lb time: ['+str(CONFIG['pso_params'][3])+','+str(CONFIG['pso_params'][5])+']\n')
    report_file.write('nb time: ['+str(CONFIG['pso_params'][5])+','+str(CONFIG['duration'])+']\n')
    
    
    # PSO
    #-----
    if pso_flag == 1:
        pso_folder = main_output_folder+'/pso'
        command = ['mkdir', pso_folder]
        subprocess.call(command)
        
        settings.ALGORITHM = 'pso'
        CONFIG = settings.update_CONFIG()
                
        for i in range(pso_iterations):   
            for first_pos in first_dr_pos:
                
                if first_pos == 'corner_bottom_left':
                    pos_label = '_bl'
                elif first_pos == 'corner_bottom_right':
                    pos_label = '_br'
                elif first_pos == 'corner_top_left':
                    pos_label = '_tl'
                elif first_pos == 'corner_top_right':
                    pos_label = '_tr'
                else:
                    pass
            
                sub_timestamp = timing.timestamp_gen()
                sub_folder = pso_folder+'/'+str(sub_timestamp)+pos_label
                command = ['mkdir', sub_folder]
                subprocess.call(command)
                
                settings.INIT_DISTRIB = first_pos
                settings.OUTPUT_FOLDER = sub_folder
                settings.CURRENT_ITERATION = i
                logger.info(str(settings.ALGORITHM)+' algorithm: iteration '+str(i)\
                            +'\n\t initial drones positions: '+pos_label)
    
                
                if exception_flag == 1:
                    try:
                        logger.info('Calling sim_core')
                        sim_core.simulator()
                    except (BaseException,KeyboardInterrupt):
                        logger.debug('pso iteration: '+str(i)+' -- Exception caught:' \
                                     +'\n\tfilename: '+sys.exc_info()[2].tb_frame.f_code.co_filename \
                                     +'\n\tlineno: '+str(sys.exc_info()[2].tb_lineno) \
                                     +'\n\tname: '+sys.exc_info()[2].tb_frame.f_code.co_name \
                                     +'\n\ttype: '+sys.exc_info()[0].__name__ \
                                     +'\n\tmessage: '+sys.exc_info()[1].message)
    
                else:
                    logger.info('Calling sim_core')
                    sim_core.simulator()

        
        #  Create averaged charts and maps
        #----------------------------------
        logger.info('Generating average charts for: '+settings.ALGORITHM)

        settings.TIMESTAMP =  timestamp_main # averaged charts will be generated with main timestamp
        CONFIG = settings.update_CONFIG()
        avg_charts.plot_all(settings.FILES, CONFIG, report_file)
        
        logger.info('Average charts generated for: '+settings.ALGORITHM)
    
    # Clean state after pso simulation
    settings.FILES = []
    report_file.close()
    
    # Lawn mower
    #------------
    if lawn_mower_flag == 1:
        lawn_folder = main_output_folder+'/lawn'
        command = ['mkdir', lawn_folder]
        subprocess.call(command)
        
        settings.ALGORITHM = 'lawn_mower'
        CONFIG = settings.update_CONFIG()
        
        report_file = file_helper.create_pso_vals(timestamp_main,lawn_folder)
        report_file.write('Lawn_mower values:\n')       
        report_file.write('entire sweep duration: '+str(CONFIG['duration'])+'\n')
        
        for i in range(lawn_iterations):
            for first_pos in first_dr_pos:           
            
                if first_pos == 'corner_bottom_left':
                    pos_label = '_bl'
                elif first_pos == 'corner_bottom_right':
                    pos_label = '_br'
                elif first_pos == 'corner_top_left':
                    pos_label = '_tl'
                elif first_pos == 'corner_top_right':
                    pos_label = '_tr'
                else:
                    pass
            
               
                sub_timestamp = timing.timestamp_gen()
                sub_folder = lawn_folder+'/'+str(sub_timestamp)+pos_label
                command = ['mkdir', sub_folder]
                subprocess.call(command)
                
                settings.INIT_DISTRIB = first_pos
                settings.OUTPUT_FOLDER = sub_folder
                settings.CURRENT_ITERATION = i
                logger.info(str(settings.ALGORITHM)+' algorithm: iteration '+str(i)\
                            +'\n\t initial drones positions: '+pos_label)
    
                if exception_flag == 1:
                    try:
                        logger.info('Calling sim_core')
                        sim_core.simulator()
                    except (BaseException,KeyboardInterrupt):
                        logger.debug('lawn_mower iteration: '+str(i)+' -- Exception caught:' \
                                     +'\n\tfilename: '+sys.exc_info()[2].tb_frame.f_code.co_filename \
                                     +'\n\tlineno: '+str(sys.exc_info()[2].tb_lineno) \
                                     +'\n\tname: '+sys.exc_info()[2].tb_frame.f_code.co_name \
                                     +'\n\ttype: '+sys.exc_info()[0].__name__ \
                                     +'\n\tmessage: '+sys.exc_info()[1].message)
    
                else:
                    logger.info('Calling sim_core')
                    sim_core.simulator()

        
        #  Create averaged charts and maps
        #-----------------------------------
        logger.info('Generating average charts for: '+settings.ALGORITHM)
        
        settings.TIMESTAMP =  timestamp_main # averaged charts will be generated with main timestamp
        CONFIG = settings.update_CONFIG()
        avg_charts.plot_all(settings.FILES, CONFIG, report_file)
        
        logger.info('Average charts generated for: '+settings.ALGORITHM)
    
    # Close files
    #-------------
    logger.info('All simulations terminated')
    
    report_file.close()

    t_end = time.time()
    sim_duration = (t_end - t_start) # simulation total duration 
    mins = int(sim_duration/60) # (minutes)
    secs = int(sim_duration%60)
    
    logger.info('Simulation duration: '+str(mins)+' mins '+str(secs)+' secs \n')

    # create a single report with all the report files
    #-------------------------------------------------
    logger.info('Creating general report file')
    
    # create the general report file
    # as the gen_report will be as the pso_vals file we only copy the file and change its name   
    command = ['find',main_output_folder,'-maxdepth','1','-type','f']
    popen_proc = Popen(command,stdout=PIPE)
    files_to_find = popen_proc.stdout.read()
    files_to_find = files_to_find.split('\n')
    del files_to_find[-1]
    
    for file_i in files_to_find:
        if file_i.endswith('_pso_vals'):
            gen_report_filename = file_i.strip('_pso_vals')+'_gen_report'
            command = ['cp', file_i, gen_report_filename]
            subprocess.call(command)
            break
        else:
            pass

    gen_report_file = open(gen_report_filename, "a") 
    gen_report_file.write('\n\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n')
    gen_report_file.close()
    
    logger.info('General report created')
    logger.info('Everything terminated')
コード例 #7
0
ファイル: launcher_two.py プロジェクト: jchanger/dpsou_mobmod
def launcher_create_scen(LAUNCH_CONFIG):

    logger = logging.getLogger(__name__)
    
    # Configuration flags
    # -------------------
    pso_flag = LAUNCH_CONFIG['pso']
    lawn_mower_flag = LAUNCH_CONFIG['lawn']
    exception_flag = LAUNCH_CONFIG['exception']
    pso_iterations = LAUNCH_CONFIG['pso_iter']
    lawn_iterations = LAUNCH_CONFIG['lawn_iter']
    
    t_start = time.time()
    
    # Generate lawn mower trajectories for defining the duration
    #-----------------------------------------------------------
    logger.info('Calculating the duration of the simulation from lawn_mower algo')
    
    CONFIG = settings.update_CONFIG()
    traj_per_drone = lawn_mower.gen_lawnmower_traj(CONFIG)
    settings.lm_trajectories = traj_per_drone
    
    duration = len(traj_per_drone[0])
    logger.info('Duration: '+str(duration))
    
    # Generate scenario
    #------------------
    # call to scenario_builder through socket
    logger.info('Using socket to get the scenario_builder generating the scenario')
    
    message = client_socket.get_scenario(duration)
    
    js_obj = json.loads(message)
    folder_path = js_obj['scenario']
    clusters = js_obj['clusters']
    settings.NODES_AMOUNT = js_obj['nodes_amount']
    
    folder = folder_path[-13:]
    relative_folder = './scenarios/'+folder+'/'
    command = ['mkdir',relative_folder]
    subprocess.call(command)
    
    # save new scenario from /scenario_builder folder to /scenario folder
    logger.info('Moving the new scenario to /scenarios folder')
    
    command = ['find',folder_path,'-maxdepth','1','-type','f']
    popen_proc = Popen(command,stdout=PIPE)
    files_to_copy = popen_proc.stdout.read()
    files_to_copy = files_to_copy.split('\n')
    del files_to_copy[-1]
    
    for file_i in files_to_copy:
        command = ['cp', file_i, relative_folder]
        subprocess.call(command)
    
    # get .wml filename
    logger.info('Finding the main .wml file')
    
    command = ['ls', relative_folder]
    popen_proc = Popen(command,stdout=PIPE)
    filename_l = popen_proc.stdout.read()
    
    # get '.wml' file name
    filename_l = filename_l.split('\n')
    for file_i in filename_l:
        if file_i.endswith('.wml'):
            wml_filename = file_i
        else:
            pass
    
    wml_full_path = relative_folder+wml_filename
    
    # save the scenario path in settings
    logger.info('Saving scenario path in settings file')
    settings.INPUT_FILE = wml_full_path
    
    CONFIG = settings.update_CONFIG()

    if CONFIG['f_duration'] == 0:
        settings.DURATION = duration
    else:
        # Leave the duration specified on the settings file
        pass
    
    settings.CLUSTERS = clusters
       
    #  SIM_CORE calls
    #-----------------
    
    timestamp_main = timing.timestamp_gen()
    logger.info('Creating output folder')
    
    main_output_folder = './outputs/'+str(timestamp_main)
    command = ['mkdir', main_output_folder]
    subprocess.call(command)
    
    settings.MAIN_OUTPUT_FOLDER = main_output_folder
    
    
    # PSO
    #-----
    if pso_flag == 1:
        
        
        pso_folder = main_output_folder+'/pso'
        command = ['mkdir', pso_folder]
        subprocess.call(command)
        
        settings.ALGORITHM = 'pso'
        
        for i in range(pso_iterations):   
            sub_timestamp = timing.timestamp_gen()
            sub_folder = pso_folder+'/'+str(sub_timestamp)
            command = ['mkdir', sub_folder]
            subprocess.call(command)
            
            settings.OUTPUT_FOLDER = sub_folder
            settings.CURRENT_ITERATION = i
            logger.info(str(settings.ALGORITHM)+' algorithm: iteration '+str(i))

            
            if exception_flag == 1:
                try:
                    logger.info('Calling sim_core')
                    sim_core.simulator()
                except (BaseException,KeyboardInterrupt):
                    logger.debug('pso iteration: '+str(i)+' -- Exception caught:' \
                                 +'\n\tfilename: '+sys.exc_info()[2].tb_frame.f_code.co_filename \
                                 +'\n\tlineno: '+str(sys.exc_info()[2].tb_lineno) \
                                 +'\n\tname: '+sys.exc_info()[2].tb_frame.f_code.co_name \
                                 +'\n\ttype: '+sys.exc_info()[0].__name__ \
                                 +'\n\tmessage: '+sys.exc_info()[1].message)

            else:
                logger.info('Calling sim_core')
                sim_core.simulator()

        
        #  Create averaged charts and maps
        #----------------------------------
        logger.info('Generating average charts for: '+settings.ALGORITHM)

        settings.TIMESTAMP =  timestamp_main # averaged charts will be generated with main timestamp
        CONFIG = settings.update_CONFIG()
        avg_charts.plot_all(settings.FILES, CONFIG)
        
        logger.info('Average charts generated for: '+settings.ALGORITHM)
    
    # Clean state after pso simulation
    settings.FILES = []
    
    # Lawn mower
    #------------
    if lawn_mower_flag == 1:
        lawn_folder = main_output_folder+'/lawn'
        command = ['mkdir', lawn_folder]
        subprocess.call(command)
        
        settings.ALGORITHM = 'lawn_mower'
        
        for i in range(lawn_iterations):   
            sub_timestamp = timing.timestamp_gen()
            sub_folder = lawn_folder+'/'+str(sub_timestamp)
            command = ['mkdir', sub_folder]
            subprocess.call(command)
            
            settings.OUTPUT_FOLDER = sub_folder
            settings.CURRENT_ITERATION = i
            logger.info(str(settings.ALGORITHM)+' algorithm: iteration '+str(i))

            if exception_flag == 1:
                try:
                    logger.info('Calling sim_core')
                    sim_core.simulator()
                except (BaseException,KeyboardInterrupt):
                    logger.debug('lawn_mower iteration: '+str(i)+' -- Exception caught:' \
                                 +'\n\tfilename: '+sys.exc_info()[2].tb_frame.f_code.co_filename \
                                 +'\n\tlineno: '+str(sys.exc_info()[2].tb_lineno) \
                                 +'\n\tname: '+sys.exc_info()[2].tb_frame.f_code.co_name \
                                 +'\n\ttype: '+sys.exc_info()[0].__name__ \
                                 +'\n\tmessage: '+sys.exc_info()[1].message)

            else:
                logger.info('Calling sim_core')
                sim_core.simulator()

        
        #  Create averaged charts and maps
        #-----------------------------------
        logger.info('Generating average charts for: '+settings.ALGORITHM)
        settings.TIMESTAMP =  timestamp_main # averaged charts will be generated with main timestamp
        CONFIG = settings.update_CONFIG()
        avg_charts.plot_all(settings.FILES, CONFIG)
        logger.info('Average charts generated for: '+settings.ALGORITHM)
    
    # Close files
    #-------------
    logger.info('All simulations terminated')
    
    t_end = time.time()
    sim_duration = (t_end - t_start) # simulation total duration 
    mins = int(sim_duration/60) # (minutes)
    secs = int(sim_duration%60)
    
    logger.info('Simulation duration: '+str(mins)+' mins '+str(secs)+' secs \n')
コード例 #8
0
def generate_scenarios(CONFIG):
    """ generate the scenario wml file with all the clusters 
    """

    CONFIG = settings.update_CONFIG()
    duration = CONFIG["duration"]
    clusters_amount = CONFIG["clusters_amount"]

    # Table for reading the scenarios in an ordered manner
    scen_list, ORIGINS = read_scenarios.get_clusters(clusters_amount)

    if CONFIG['maintain_nodes'] == 1:
        read_scenarios.redistribute_cluster_nodes(scen_list, CONFIG)

    #-----------------------------------------------------------------------------
    # Main procedure
    #-----------------------------------------------------------------------------

    # Generates timestamp for adding to the filename
    settings.TIMESTAMP = timing.timestamp_gen()  # modifies only local CONFIG
    CONFIG = settings.update_CONFIG()

    # the main scenario directory is created in the BM workspace, it will contain sub_scenario folders
    bm_dir_name = app_settings.BM_PATH + 'bin/built_scenarios/' + str(
        CONFIG['timestamp']) + '/'
    command = ['mkdir', bm_dir_name]
    subprocess.call(command)

    # update config with timestamp and files paths
    settings.OUTPUT_FOLDER = bm_dir_name
    CONFIG = settings.update_CONFIG()

    for index, scen_i in enumerate(scen_list):
        if index == 0:
            print ""

        print "Generating cluster " + str(index)
        create_sub_scenario(bm_dir_name, index, duration, ORIGINS[index],
                            *scen_i)

    # parse files to join several sub-scenarios
    print '\nMerging clusters into the scenario...'
    merge_scenarios.merge_sub_scenarios(bm_dir_name, duration, ORIGINS)

    # generate a general 'params' file for the merged scenario
    command = ['find', bm_dir_name, '-name', '*.params']
    popen_proc = Popen(command, stdout=PIPE)
    params_filenames = popen_proc.stdout.read()
    params_filenames = params_filenames.split('\n')
    del params_filenames[-1]  # remove the last empty string
    params_filenames.sort()

    general_param_file = open(
        bm_dir_name + str(CONFIG['timestamp']) + '.params', 'w')

    for filename_i in params_filenames:
        file_i = open(filename_i, 'r')

        for line in file_i:
            general_param_file.write(line)

        general_param_file.write('\n\n')
        file_i.close()

    general_param_file.close()

    # Copy files to python workspace
    command = ['mv', bm_dir_name, app_settings.SCENARIO_PATH]
    subprocess.call(command)

    # returns the path of the output folder
    out_folder_path = app_settings.SCENARIO_PATH + '/' + str(
        CONFIG['timestamp'])

    # organize return information
    clusters = []
    nodes_amount = 0

    for i, scen_i in enumerate(scen_list):
        aux_origin = [ORIGINS[i][0], ORIGINS[i][1]]
        aux_dimension = [scen_i[1], scen_i[2]]
        cluster_i = [aux_origin, aux_dimension]
        clusters.append(cluster_i)

        nodes_amount = nodes_amount + scen_i[3]

    # plot scenario figure
    print "Plotting scenario..."
    plotting.create_scenario_figure(CONFIG['timestamp'], CONFIG)

    print 'Scenario generation finished'

    return out_folder_path, clusters, nodes_amount