예제 #1
0
def vizualize_sample_execution(world_config_file, schedule_config_file,
                               planner_config_file, base_model_filepath,
                               schedule_filepath, strategies,
                               num_deliveries_runs, availability_percents,
                               stat_run, visualize, visualize_path):

    ## params
    params = load_params(world_config_file, schedule_config_file,
                         planner_config_file)

    ## import world
    # g = Graph()
    # g.read_graph_from_file(os.path.dirname(os.path.abspath(__file__)) + params['graph_filename'])

    g = read_graph_from_file(
        os.path.dirname(os.path.abspath(__file__)) + params['graph_filename'])

    for num_deliveries in num_deliveries_runs:
        for availability_percent in availability_percents:

            # temporal consistency parameter
            if params['availabilities'] == 'windows':
                available_time = params['budget'] * availability_percent
                num_windows = max(
                    int(
                        round(
                            float(available_time) /
                            params['availability_length'])), 1)
                ave_window_offset = float(params['budget'] -
                                          available_time) / num_windows
                mu = max(ave_window_offset, 1)
            else:
                mu = 60

            # base models, true schedules
            model_file_exists = os.path.exists(base_model_filepath +
                                               str(num_deliveries) + "_" +
                                               str(availability_percent) +
                                               "_" + str(stat_run) + ".yaml")
            schedule_file_exists = os.path.exists(schedule_filepath +
                                                  str(num_deliveries) + "_" +
                                                  str(availability_percent) +
                                                  "_" + str(stat_run) +
                                                  ".yaml")
            if model_file_exists and schedule_file_exists:
                # load pre-generated schedules/models
                base_availability_models, base_model_variances, node_requests = load_base_models_from_file(
                    base_model_filepath, num_deliveries, availability_percent,
                    stat_run)
                true_availability_models, true_schedules = load_schedules_from_file(
                    schedule_filepath, num_deliveries, availability_percent,
                    stat_run)
            else:
                # sample rooms for delivieries
                if params['node_closeness'] == 'random':
                    node_requests = random.sample(params['rooms'],
                                                  num_deliveries)
                if params['node_closeness'] == 'sequential':
                    node_requests = params['rooms'][0:num_deliveries]

                ## base availability models
                base_availability_models, base_model_variances = generate_window_base_availability_models_with_bernoulli_variance(
                    node_requests, params['start_time'], availability_percent,
                    params['budget'], params['time_interval'],
                    params['availability_length'],
                    params['availability_chance'])

                ## true availability models
                true_availability_models = sample_model_parameters(
                    node_requests, base_availability_models,
                    base_model_variances, params['sampling_method'])

                ## true schedules
                true_schedules = generate_schedule(
                    node_requests, true_availability_models, mu,
                    params['num_intervals'],
                    params['schedule_generation_method'],
                    params['temporal_consistency'])

                save_base_models_to_file(base_model_filepath,
                                         base_availability_models,
                                         base_model_variances, node_requests,
                                         num_deliveries, availability_percent,
                                         stat_run)
                save_schedules_to_file(schedule_filepath,
                                       true_availability_models,
                                       true_schedules, node_requests,
                                       num_deliveries, availability_percent,
                                       stat_run)

            ## "learned" availability models
            availability_models = base_availability_models
            model_variances = base_model_variances

            # plan and execute paths for specified strategies
            visit_traces = {}
            for strategy in strategies:
                total_profit, competitive_ratio, maintenance_competitive_ratio, path_history = plan_and_execute(
                    strategy, g, availability_models, model_variances,
                    true_schedules, node_requests, mu, params, visualize,
                    visualize_path)
                visit_traces[strategy] = path_history

            visualize_path_willow(strategies, visit_traces,
                                  true_availability_models, true_schedules,
                                  node_requests, params['maintenance_node'],
                                  params['start_time'], params['budget'],
                                  params['time_interval'])
예제 #2
0
def stat_runs(world_config_file, schedule_config_file, planner_config_file,
              base_model_filepath, schedule_filepath, output_file, strategies,
              num_deliveries_runs, availability_percents, num_stat_runs,
              visualize, visualize_path):

    if output_file == None:
        record_output = False
    else:
        record_output = True

    ## params
    params = load_params(world_config_file, schedule_config_file,
                         planner_config_file)

    ## load world
    # g = Graph()
    # g.read_graph_from_file(os.path.dirname(os.path.abspath(__file__)) + params['graph_filename'])

    # g = read_graph_from_file(os.path.dirname(os.path.abspath(__file__)) + params['graph_filename'])
    g = generate_graph(params['graph_generator_type'],
                       os.path.dirname(os.path.abspath(__file__)),
                       params['graph_filename'], params['max_rooms'],
                       params['max_traversal_cost'])

    for num_deliveries in num_deliveries_runs:
        for availability_percent in availability_percents:

            # temporal consistency parameter
            if params['availabilities'] == 'windows':
                available_time = params['budget'] * availability_percent
                num_windows = max(
                    int(
                        round(
                            float(available_time) /
                            params['availability_length'])), 1)
                ave_window_offset = float(params['budget'] -
                                          available_time) / num_windows
                mu = max(ave_window_offset, 1)
            else:
                mu = 60

            # base models, true schedules
            node_requests = []
            base_availability_models = []
            base_model_variances = []
            true_availability_models = []
            true_schedules = []
            for stat_run in range(num_stat_runs):
                model_file_exists = os.path.exists(base_model_filepath +
                                                   str(num_deliveries) + "_" +
                                                   str(availability_percent) +
                                                   "_" + str(stat_run) +
                                                   ".yaml")
                schedule_file_exists = os.path.exists(
                    schedule_filepath + str(num_deliveries) + "_" +
                    str(availability_percent) + "_" + str(stat_run) + ".yaml")
                if model_file_exists and schedule_file_exists:
                    # load pre-generated schedules/models
                    base_avails, base_variances, requests = load_base_models_from_file(
                        base_model_filepath, num_deliveries,
                        availability_percent, stat_run)
                    true_avails, schedules = load_schedules_from_file(
                        schedule_filepath, num_deliveries,
                        availability_percent, stat_run)
                    node_requests.append(requests)
                    base_availability_models.append(base_avails)
                    base_model_variances.append(base_variances)
                    true_availability_models.append(true_avails)
                    true_schedules.append(schedules)
                else:

                    if params['availabilities'] == 'windows':

                        # sample rooms for delivieries
                        if params['node_closeness'] == 'random':
                            node_requests.append(
                                random.sample(params['rooms'], num_deliveries))
                        if params['node_closeness'] == 'sequential':
                            node_requests.append(
                                params['rooms'][0:num_deliveries])

                        ## base availability models
                        avails, variances = generate_window_base_availability_models_with_bernoulli_variance(
                            node_requests[stat_run], params['start_time'],
                            availability_percent, params['budget'],
                            params['time_interval'],
                            params['availability_length'],
                            params['availability_chance'])
                        base_availability_models.append(avails)
                        base_model_variances.append(variances)

                        ## true availability models
                        sampled_avails = sample_model_parameters(
                            node_requests[stat_run], avails, variances,
                            params['sampling_method'])
                        true_availability_models.append(sampled_avails)

                        ## true schedules
                        true_schedules.append(
                            generate_schedule(
                                node_requests[stat_run], sampled_avails, mu,
                                params['num_intervals'],
                                params['schedule_generation_method']))
                        # true_schedules.append(sample_schedule_from_model(node_requests[stat_run], sampled_avails, mu, params['num_intervals'], params['temporal_consistency']))

                        save_base_models_to_file(
                            base_model_filepath,
                            base_availability_models[stat_run],
                            base_model_variances[stat_run],
                            node_requests[stat_run], num_deliveries,
                            availability_percent, stat_run)
                        save_schedules_to_file(
                            schedule_filepath,
                            true_availability_models[stat_run],
                            true_schedules[stat_run], node_requests[stat_run],
                            num_deliveries, availability_percent, stat_run)

                    elif params['availabilities'] == 'simple':

                        # sample rooms for delivieries
                        if params['node_closeness'] == 'random':
                            node_requests.append(
                                random.sample(params['rooms'], num_deliveries))
                        if params['node_closeness'] == 'sequential':
                            node_requests.append(
                                params['rooms'][0:num_deliveries])

                        ## base availability models
                        avails, variances = generate_simple_models(
                            node_requests[stat_run], params['start_time'],
                            availability_percent, params['budget'],
                            params['time_interval'],
                            params['availability_length'],
                            params['availability_chance'])
                        base_availability_models.append(avails)
                        base_model_variances.append(variances)

                        # ## true availability models
                        # sampled_avails = sample_model_parameters(node_requests[stat_run], avails, variances, params['sampling_method'])
                        # true_availability_models.append(sampled_avails)

                        ## true schedules
                        true_schedules.append(
                            generate_simple_schedules(
                                node_requests[stat_run], sampled_avails, mu,
                                params['num_intervals'],
                                params['schedule_generation_method']))
                        # true_schedules.append(sample_schedule_from_model(node_requests[stat_run], sampled_avails, mu, params['num_intervals'], params['temporal_consistency']))

                        # save_base_models_to_file(base_model_filepath, base_availability_models[stat_run], base_model_variances[stat_run], node_requests[stat_run], num_deliveries, availability_percent, stat_run)
                        # save_schedules_to_file(schedule_filepath, true_availability_models[stat_run], true_schedules[stat_run], node_requests[stat_run], num_deliveries, availability_percent, stat_run)

                    else:
                        raise ValueError(params['availabilities'])

            ## "learned" availability models
            availability_models = base_availability_models
            model_variances = base_model_variances

            # plan and execute paths for specified strategies
            for strategy in strategies:
                for stat_run in range(num_stat_runs):
                    total_profit, competitive_ratio, maintenance_competitive_ratio, path_history = plan_and_execute(
                        strategy, g, availability_models[stat_run],
                        model_variances[stat_run], true_schedules[stat_run],
                        node_requests[stat_run], mu, params, visualize,
                        visualize_path)

                    if record_output:
                        with open(output_file, 'a', newline='') as csvfile:
                            writer = csv.writer(csvfile,
                                                delimiter=',',
                                                quotechar='|',
                                                quoting=csv.QUOTE_MINIMAL)
                            writer.writerow([
                                strategy, params['budget'], num_deliveries,
                                availability_percent,
                                params['availability_chance'],
                                params['maintenance_reward'],
                                params['max_noise_amplitude'],
                                params['variance_bias'], competitive_ratio,
                                maintenance_competitive_ratio
                            ])
예제 #3
0
def stat_runs(world_config_file, schedule_config_file, planner_config_file,
              model_config_file, base_model_filepath, schedule_filepath,
              output_file, strategies, num_deliveries_runs,
              availability_percents, budgets, num_stat_runs, visualize,
              out_gif_path):

    if output_file == None:
        record_output = False
    else:
        record_output = True

    ## params
    params = load_params(world_config_file, schedule_config_file,
                         planner_config_file, model_config_file)

    ## load world
    # g = Graph()
    # g.read_graph_from_file(os.path.dirname(os.path.abspath(__file__)) + params['graph_filename'])

    # g = read_graph_from_file(os.path.dirname(os.path.abspath(__file__)) + params['graph_filename'])
    g, rooms = generate_graph(params['graph_generator_type'],
                              os.path.dirname(os.path.abspath(__file__)),
                              params['graph_filename'], params['max_rooms'],
                              params['rooms'], params['max_traversal_cost'],
                              params['distance_scaling'])
    params['rooms'] = rooms

    # for num_deliveries in num_deliveries_runs:
    num_deliveries = num_deliveries_runs[0]
    for availability_percent in availability_percents:

        for budget in budgets:

            params['budget'] = budget
            params['num_intervals'] = int(params['budget'] /
                                          params['time_interval'])
            params['longest_period'] = budget

            # temporal consistency parameter
            if params['availabilities'] == 'windows':
                # available_time = params['budget']*availability_percent
                # num_windows = max(int(round(float(available_time)/params['availability_length'])), 1)
                # ave_window_offset = float(params['budget'] - available_time)/num_windows
                # mu = max(ave_window_offset, 1)

                available_time = params['budget'] * availability_percent
                num_windows = max(
                    1,
                    int(
                        round(
                            float(available_time) /
                            params['availability_length'])))
                # new_availability_length = int(float(available_time)/num_windows)
                ave_window_offset = min(
                    float(params['budget'] - available_time) / num_windows,
                    float(params['budget'] - available_time) / 2)
                mu = int(ave_window_offset / 2)

                # mu = int(params['availability_length']/2)
            elif params['availabilities'] == 'simple':
                mu = int(params['availability_length'] / 2)
            else:
                mu = 30
            params['mu'] = mu

            # base models, true schedules
            node_requests = []
            base_availability_models = []
            base_model_variances = []
            true_availability_models = []
            true_schedules = []
            num_test_runs = 0
            for stat_run in range(num_stat_runs):
                model_file_exists = os.path.exists(base_model_filepath +
                                                   str(num_deliveries) + "_" +
                                                   str(availability_percent) +
                                                   "_" + str(stat_run) + ".p")
                schedule_file_exists = os.path.exists(
                    schedule_filepath + str(num_deliveries) + "_" +
                    str(availability_percent) + "_" + str(stat_run) + ".yaml")
                if model_file_exists and schedule_file_exists:
                    #     # load pre-generated schedules/models
                    gmms, base_variances, requests = load_base_models_from_file(
                        base_model_filepath, num_deliveries,
                        availability_percent, stat_run)
                    true_avails, schedules = load_schedules_from_file(
                        schedule_filepath, num_deliveries,
                        availability_percent, stat_run)
                    node_requests.append(requests)

                    # gmms = {}
                    # for request in node_requests[stat_run]:
                    #     x_in = list(range(int(params['start_time']), int(params['budget']), int(params['time_interval'])))
                    #     y_in = Y_in[request][:len(x_in)]
                    #     gmms[request] = build_gmm(x_in, y_in, params['start_time'], params['start_time'] + params['budget'], params['time_interval'], params, True)

                    base_availability_models.append(gmms)
                    base_model_variances.append(base_variances)
                    true_availability_models.append(true_avails)
                    true_schedules.append(schedules)
                else:

                    if params['availabilities'] == 'brayford':

                        # model
                        if params['use_gp']:
                            from gp import GP
                            gps = {}
                        # if params['use_gmm']:
                        gmms = {}
                        # mus = {}
                        mu = 0.0
                        mu_n = 0
                        node_requests.append(params['rooms'])
                        for request in node_requests[stat_run]:
                            x_in, y_in, mu_combined, mu_combined_n = load_brayford_training_data(
                                request,
                                os.path.dirname(os.path.abspath(__file__)) +
                                params['data_path'], out_gif_path)
                            if params['use_gp']:
                                gps[request] = GP(None, x_in, y_in,
                                                  params['budget'], 1,
                                                  params['noise_scaling'],
                                                  True, 'values')
                            else:
                                gmms[request] = build_gmm(
                                    x_in, y_in, params['start_time'],
                                    params['start_time'] + params['budget'],
                                    params['time_interval'], params)
                                # gmms[request].visualize(out_gif_path + "train_" + request + "_gmm_histogram_10.jpg", request)
                            # mus[request] = mu_combined/mu_combined_n
                            mu += mu_combined
                            mu_n += mu_combined_n

                            # gps[request].visualize(out_gif_path + "train_" + request + "_model_histogram_10.jpg", request)

                        if params['use_gp']:
                            base_availability_models.append(gps)
                        else:
                            base_availability_models.append(gmms)
                        base_model_variances.append({})
                        mu = mu / mu_n
                        params['mu'] = mu

                        # true schedule
                        # if params['availabilities'] == 'brayford':

                        schedules = {}
                        for request in node_requests[stat_run]:
                            X, Y = load_brayford_testing_data(
                                request,
                                os.path.dirname(os.path.abspath(__file__)) +
                                params['data_path'], stat_run, out_gif_path)
                            schedules[request] = Y[stat_run]
                            # for i in range(Y.shape[0]):
                            #     if not(i in schedules):
                            #         schedules[i] = {}
                            #     schedules[i][request] = Y[i]
                            # num_test_runs = Y.shape[0]

                            # schedules[request] = Y
                            # if params['use_gp']:
                            # from gp import GP
                            # test_gp = GP(None, x_in, y_in, params['budget'], 1, params['noise_scaling'], True, 'values')
                            # if params['use_gmm']:
                            #     test_gp = build_gmm(x_in, y_in, params['start_time'], params['start_time'] + params['budget'], params['time_interval'], params)
                            # if stat_run == 0:
                            #     test_gp.visualize(out_gif_path + "february_" + request + "_model_10.jpg", request)
                            # else:
                            # test_gp.visualize(out_gif_path + "november_" + request + "_model_histogram_10.jpg", request)
                            # schedules[request] = test_gp.threshold_sample_schedule(params['start_time'], params['budget'], params['time_interval'])

                            # # visualize:
                            # fig = plt.figure()
                            # X = np.array(list(range(params['start_time'], params['budget'], params['time_interval'])))
                            # Y = np.array(schedules[request])
                            # plt.scatter(X, Y)
                            # if stat_run == 0:
                            #     plt.title("Brayford Schedule Node " + request + ": February")
                            #     plt.savefig(out_gif_path + "february_" + request + ".jpg")
                            # else:
                            #     plt.title("Brayford Schedule Node " + request + ": November")
                            #     plt.savefig(out_gif_path + "november_" + request + ".jpg")

                        true_schedules.append(schedules)

                    elif params['availabilities'] == 'windows':

                        # sample rooms for delivieries
                        if params['node_closeness'] == 'random':
                            node_requests.append(
                                random.sample(params['rooms'], num_deliveries))
                        if params['node_closeness'] == 'sequential':
                            node_requests.append(
                                params['rooms'][0:num_deliveries])

                        ## base availability models
                        avails, variances = generate_windows_overlapping(
                            node_requests[stat_run], params['start_time'],
                            availability_percent, params['budget'],
                            params['time_interval'],
                            params['availability_length'],
                            params['availability_chance'])
                        # X_in = {}
                        # Y_in = {}
                        if params['use_gp']:
                            from gp import GP
                            gps = {}
                            for request in node_requests[stat_run]:
                                x_in = list(
                                    range(int(params['start_time']),
                                          int(params['budget']),
                                          int(params['time_interval'])))
                                y_in = copy.deepcopy(avails[request])
                                for i in range(len(y_in)):
                                    y = max(
                                        y_in[i] + random.random() *
                                        params['noise_scaling'] -
                                        params['noise_scaling'] / 2.0, 0.01)
                                    y = min(y, .99)
                                    y_in[i] = y

                                gps[request] = GP(None, x_in, y_in,
                                                  params['budget'],
                                                  params['spacing'], 0.0, True,
                                                  'values')
                                # Y_in[request] = y_in
                            base_availability_models.append(gps)
                        else:
                            gmms = {}
                            for request in node_requests[stat_run]:
                                x_in = list(
                                    range(int(params['start_time']),
                                          int(params['budget']),
                                          int(params['time_interval'])))
                                y_in = copy.deepcopy(avails[request])
                                for i in range(len(y_in)):
                                    y = max(
                                        y_in[i] + random.random() *
                                        params['noise_scaling'] -
                                        params['noise_scaling'] / 2.0, 0.01)
                                    y = min(y, .99)
                                    y_in[i] = y
                                gmms[request] = build_gmm(
                                    x_in, y_in, params['start_time'],
                                    params['start_time'] + params['budget'],
                                    params['time_interval'], params, True)
                                # Y_in[request] = y_in
                                # gmms[request].visualize(out_gif_path + "train_" + request + "_gmm_histogram_10.jpg", request)
                                # mus[request] = mu_combined/mu_combined_n
                                # mu += mu_combined
                                # mu_n += mu_combined_n
                            base_availability_models.append(gmms)
                        # else:
                        #     base_availability_models.append(avails)

                        # base_availability_models.append(avails)
                        base_model_variances.append(variances)

                        # true availability models
                        sampled_avails = sample_model_parameters(
                            node_requests[stat_run], avails, variances,
                            params['sampling_method'])
                        sampled_avails = avails

                        true_availability_models.append(avails)

                        ## true schedules
                        true_schedules.append(
                            generate_schedule(
                                node_requests[stat_run], avails, params['mu'],
                                params['num_intervals'],
                                params['schedule_generation_method'],
                                params['temporal_consistency']))
                        # true_schedules.append(sample_schedule_from_model(node_requests[stat_run], sampled_avails, mu, params['num_intervals'], params['temporal_consistency']))

                        save_base_models_to_file(
                            base_model_filepath,
                            base_availability_models[stat_run],
                            base_model_variances[stat_run],
                            node_requests[stat_run], num_deliveries,
                            availability_percent, stat_run)
                        save_schedules_to_file(
                            schedule_filepath,
                            true_availability_models[stat_run],
                            true_schedules[stat_run], node_requests[stat_run],
                            num_deliveries, availability_percent, stat_run)

                    # elif params['availabilities'] == 'simple':

                    #     # sample rooms for delivieries
                    #     if params['node_closeness'] == 'random':
                    #         node_requests.append(random.sample(params['rooms'], num_deliveries))
                    #     if params['node_closeness'] == 'sequential':
                    #         node_requests.append(params['rooms'][0:num_deliveries])

                    #     ## base availability models
                    #     avails, variances = generate_simple_models(node_requests[stat_run], params['start_time'], availability_percent, params['budget'], params['time_interval'], params['availability_length'], params['availability_chance'])
                    #     base_availability_models.append(avails)
                    #     base_model_variances.append(variances)

                    #     # ## true availability models
                    #     # sampled_avails = sample_model_parameters(node_requests[stat_run], avails, variances, params['sampling_method'])
                    #     # true_availability_models.append(sampled_avails)

                    #     ## true schedules
                    #     true_schedules.append(generate_simple_schedules(node_requests[stat_run], sampled_avails, params['mu'], params['num_intervals'], params['schedule_generation_method']))
                    #     # true_schedules.append(sample_schedule_from_model(node_requests[stat_run], sampled_avails, mu, params['num_intervals'], params['temporal_consistency']))

                    #     # save_base_models_to_file(base_model_filepath, base_availability_models[stat_run], base_model_variances[stat_run], node_requests[stat_run], num_deliveries, availability_percent, stat_run)
                    #     # save_schedules_to_file(schedule_filepath, true_availability_models[stat_run], true_schedules[stat_run], node_requests[stat_run], num_deliveries, availability_percent, stat_run)

                    else:
                        raise ValueError(params['availabilities'])

            ## "learned" availability models
            availability_models = base_availability_models
            model_variances = base_model_variances

            # plan and execute paths for specified strategies
            for strategy in strategies:

                strategy_name = strategy

                params['uncertainty_penalty'] = 0.0
                params['observation_reward'] = 0.0
                params['deliver_threshold'] = 0.0

                if strategy == 'observe_mult_visits_up_5_or_0_dt_0':
                    params['uncertainty_penalty'] = 0.5
                    params['observation_reward'] = 0.0
                    params['deliver_threshold'] = 0.0
                    strategy_name = strategy
                    strategy = 'observe_mult_visits'

                if strategy == 'observe_mult_visits_up_0_or_7_dt_0':
                    params['uncertainty_penalty'] = 0.0
                    params['observation_reward'] = 0.7
                    params['deliver_threshold'] = 0.0
                    strategy_name = strategy
                    strategy = 'observe_mult_visits'

                if strategy == 'observe_mult_visits_up_5_or_7_dt_0':
                    params['uncertainty_penalty'] = 0.5
                    params['observation_reward'] = 0.7
                    params['deliver_threshold'] = 0.0
                    strategy_name = strategy
                    strategy = 'observe_mult_visits'

                # for stat_run in range(num_stat_runs):
                for stat_run in [2]:
                    # stat_run = 0
                    # for test_run in range(num_test_runs):
                    if strategy == 'mcts':
                        total_profit, competitive_ratio, maintenance_competitive_ratio, path_history, ave_plan_time = create_policy_and_execute(
                            strategy, g, availability_models[stat_run],
                            model_variances[stat_run],
                            true_schedules[stat_run], node_requests[stat_run],
                            params['mu'], params, visualize, out_gif_path)
                    else:
                        total_profit, competitive_ratio, maintenance_competitive_ratio, path_history, ave_plan_time = plan_and_execute(
                            strategy, g, availability_models[stat_run],
                            model_variances[stat_run],
                            true_schedules[stat_run], node_requests[stat_run],
                            params['mu'], params, visualize, out_gif_path)

                    if record_output:
                        with open(output_file, 'a', newline='') as csvfile:
                            writer = csv.writer(csvfile,
                                                delimiter=',',
                                                quotechar='|',
                                                quoting=csv.QUOTE_MINIMAL)
                            writer.writerow([
                                strategy_name, params['budget'],
                                num_deliveries, availability_percent,
                                params['availability_chance'],
                                params['maintenance_reward'],
                                params['max_noise_amplitude'],
                                params['variance_bias'], competitive_ratio,
                                maintenance_competitive_ratio, ave_plan_time
                            ])
예제 #4
0
def vizualize_sample_execution(world_config_file, schedule_config_file,
                               planner_config_file, model_config_file,
                               base_model_filepath, schedule_filepath,
                               strategies, num_deliveries_runs,
                               availability_percents, stat_run, visualize,
                               out_gif_path, out_img_path):

    ## params
    params = load_params(world_config_file, schedule_config_file,
                         planner_config_file, model_config_file)

    ## import world
    # g = Graph()
    # g.read_graph_from_file(os.path.dirname(os.path.abspath(__file__)) + params['graph_filename'])

    # g = read_graph_from_file(os.path.dirname(os.path.abspath(__file__)) + params['graph_filename'])
    g, rooms = generate_graph(params['graph_generator_type'],
                              os.path.dirname(os.path.abspath(__file__)),
                              params['graph_filename'], params['max_rooms'],
                              params['rooms'], params['max_traversal_cost'],
                              params['distance_scaling'])
    params['rooms'] = rooms

    for num_deliveries in num_deliveries_runs:
        for availability_percent in availability_percents:

            # temporal consistency parameter
            if params['availabilities'] == 'windows':
                # available_time = params['budget']*availability_percent
                # num_windows = max(int(round(float(available_time)/params['availability_length'])), 1)
                # ave_window_offset = float(params['budget'] - available_time)/num_windows
                # mu = max(ave_window_offset, 1)

                available_time = params['budget'] * availability_percent
                num_windows = max(
                    1,
                    int(
                        round(
                            float(available_time) /
                            params['availability_length'])))
                # new_availability_length = int(float(available_time)/num_windows)
                ave_window_offset = min(
                    float(params['budget'] - available_time) / num_windows,
                    float(params['budget'] - available_time) / 2)
                mu = int(ave_window_offset / 2)

                # mu = int(params['availability_length']/2)
            elif params['availabilities'] == 'simple':
                mu = int(params['availability_length'] / 2)
            else:
                mu = 30
            params['mu'] = mu

            # base models, true schedules
            stat_run = 0
            model_file_exists = os.path.exists(base_model_filepath +
                                               str(num_deliveries) + "_" +
                                               str(availability_percent) +
                                               "_" + str(stat_run) + ".yaml")
            schedule_file_exists = os.path.exists(schedule_filepath +
                                                  str(num_deliveries) + "_" +
                                                  str(availability_percent) +
                                                  "_" + str(stat_run) +
                                                  ".yaml")
            if model_file_exists and schedule_file_exists:
                # load pre-generated schedules/models
                base_availability_models, base_model_variances, node_requests = load_base_models_from_file(
                    base_model_filepath, num_deliveries, availability_percent,
                    stat_run)
                true_availability_models, true_schedules = load_schedules_from_file(
                    schedule_filepath, num_deliveries, availability_percent,
                    stat_run)
                availabilities = base_availability_models
            else:

                if params['availabilities'] == 'windows':
                    # sample rooms for delivieries
                    if params['node_closeness'] == 'random':
                        node_requests = random.sample(params['rooms'],
                                                      num_deliveries)
                    if params['node_closeness'] == 'sequential':
                        node_requests = params['rooms'][0:num_deliveries]

                    ## base availability models
                    avails, base_model_variances = generate_windows_overlapping(
                        node_requests, params['start_time'],
                        availability_percent, params['budget'],
                        params['time_interval'], params['availability_length'],
                        params['availability_chance'])

                    if params['use_gp']:
                        from gp import GP
                        gps = {}
                        availabilities = {}
                        for request in node_requests:
                            x_in = list(
                                range(params['start_time'], params['budget'],
                                      params['time_interval']))
                            gps[request] = GP(None, x_in, avails[request],
                                              params['budget'],
                                              params['spacing'],
                                              params['noise_scaling'], True,
                                              'values')
                            availabilities[request] = gps[request].get_preds(
                                x_in)
                        base_availability_models = gps
                    else:
                        base_availability_models = avails
                        availabilities = avails

                    ## true availability models
                    # sampled_availability_models = sample_model_parameters(node_requests, base_availability_models, base_model_variances, params['sampling_method'])
                    true_availability_models = avails

                    ## true schedules
                    true_schedules = generate_schedule(
                        node_requests, true_availability_models, params['mu'],
                        params['num_intervals'],
                        params['schedule_generation_method'],
                        params['temporal_consistency'])

                    # save_base_models_to_file(base_model_filepath, base_availability_models, base_model_variances, node_requests, num_deliveries, availability_percent, stat_run)
                    # save_schedules_to_file(schedule_filepath, true_availability_models, true_schedules, node_requests, num_deliveries, availability_percent, stat_run)

                elif params['availabilities'] == 'simple':

                    # sample rooms for delivieries
                    if params['node_closeness'] == 'random':
                        node_requests = random.sample(params['rooms'],
                                                      num_deliveries)
                    if params['node_closeness'] == 'sequential':
                        node_requests = params['rooms'][0:num_deliveries]

                    ## base availability models
                    base_availability_models, base_model_variances = generate_simple_models(
                        node_requests, params['start_time'],
                        availability_percent, params['budget'],
                        params['time_interval'], params['availability_length'],
                        params['availability_chance'])
                    availabilities = base_availability_models

                    # ## true availability models
                    # sampled_avails = sample_model_parameters(node_requests[stat_run], avails, variances, params['sampling_method'])
                    # true_availability_models.append(sampled_avails)

                    ## true schedules
                    true_schedules = generate_simple_schedules(
                        node_requests, base_availability_models, params['mu'],
                        params['num_intervals'],
                        params['schedule_generation_method'])
                    # true_schedules.append(sample_schedule_from_model(node_requests[stat_run], sampled_avails, mu, params['num_intervals'], params['temporal_consistency']))

                    # save_base_models_to_file(base_model_filepath, base_availability_models[stat_run], base_model_variances[stat_run], node_requests[stat_run], num_deliveries, availability_percent, stat_run)
                    # save_schedules_to_file(schedule_filepath, true_availability_models[stat_run], true_schedules[stat_run], node_requests[stat_run], num_deliveries, availability_percent, stat_run)

                else:
                    raise ValueError(params['availabilities'])

            ## "learned" availability models
            availability_models = base_availability_models
            model_variances = base_model_variances

            # plan and execute paths for specified strategies
            visit_traces = {}
            for strategy in strategies:
                if strategy == 'mcts':
                    total_profit, competitive_ratio, maintenance_competitive_ratio, path_history = create_policy_and_execute(
                        strategy, g, availability_models, model_variances,
                        true_schedules, node_requests, params['mu'], params,
                        visualize, out_gif_path)
                else:
                    total_profit, competitive_ratio, maintenance_competitive_ratio, path_history = plan_and_execute(
                        strategy, g, availability_models, model_variances,
                        true_schedules, node_requests, params['mu'], params,
                        visualize, out_gif_path)
                visit_traces[strategy] = path_history

            visualize_path_willow(strategies, visit_traces, availabilities,
                                  true_schedules, node_requests,
                                  params['maintenance_node'],
                                  params['start_time'], params['budget'],
                                  params['time_interval'], out_img_path)
예제 #5
0
def sample_best_path(g, base_availability_models, base_model_variances,
                     availability_observations, requests_left_to_deliver,
                     budget, time_interval, num_intervals, curr_time,
                     curr_node, maintenance_node, mu, maintenance_reward,
                     deliver_reward, uncertainty_penalty, observation_reward,
                     deliver_threshold, num_paths, num_worlds,
                     incorporate_observation, incorporate_observation_hack,
                     variance_bias, multiple_visits, replan,
                     schedule_generation_method, use_gp):

    # generate potential solutions
    solution_set = []
    for path_index in range(num_paths):
        sample_availability_models = {}
        for request in requests_left_to_deliver:
            sample_availability_models[
                request] = sample_bernoulli_avialability_model(
                    base_availability_models[request])

        st_g = SpatioTemporalGraph(
            sample_availability_models, base_model_variances,
            availability_observations, True, mu,
            int((budget - curr_time) / time_interval), budget - curr_time,
            time_interval, maintenance_node, maintenance_reward,
            deliver_reward, uncertainty_penalty, observation_reward,
            deliver_threshold, use_gp)
        st_g.build_graph(g, curr_node, curr_time, requests_left_to_deliver,
                         availability_observations, incorporate_observation,
                         incorporate_observation_hack, variance_bias,
                         params['ensemble_method'])

        ## topological sort
        # print ("Sorting of the topological variety...")
        L = st_g.topological_sort()

        ## maximal profit path
        # print ("Calculating path...")
        # print ()
        path = st_g.calc_max_profit_path(L, requests_left_to_deliver,
                                         multiple_visits)

        solution_set.append(path)

    # generate evaluation worlds
    sim_worlds = {}
    for world_index in range(num_worlds):
        true_availability_models = {}
        for request in requests_left_to_deliver:
            true_availability_models[
                request] = sample_bernoulli_avialability_model(
                    base_availability_models[request])
        sim_worlds[world_index] = generate_schedule(
            requests_left_to_deliver, true_availability_models, mu,
            num_intervals, schedule_generation_method, incorporate_observation)

    # evaluate potential solutions
    best_path = None
    best_score = -float("inf")
    for orig_path in solution_set:

        total_score = 0.0
        ave_score = 0.0
        for world_index in range(num_worlds):

            ### plan, execution loop
            num_requests = len(requests_left_to_deliver)
            sim_requests_left_to_deliver = copy.deepcopy(
                requests_left_to_deliver)
            sim_availability_observations = copy.deepcopy(
                availability_observations)
            total_profit = 0.0
            total_maintenance_profit = 0.0
            delivery_history = []
            sim_time = curr_time
            sim_curr_node = curr_node
            path_length = 1
            path_visits = 0

            orig_path_used = False

            while (path_visits < path_length):

                if orig_path_used:
                    st_g = SpatioTemporalGraph(
                        base_availability_models, base_model_variances,
                        availability_observations, True, mu,
                        int((budget - sim_time) / time_interval),
                        budget - sim_time, time_interval, maintenance_node,
                        maintenance_reward, deliver_reward,
                        uncertainty_penalty, observation_reward,
                        deliver_threshold, use_gp)
                    st_g.build_graph(g, sim_curr_node, sim_time,
                                     sim_requests_left_to_deliver,
                                     sim_availability_observations,
                                     incorporate_observation,
                                     incorporate_observation_hack,
                                     variance_bias, params['ensemble_method'])

                    ## topological sort
                    # print ("Sorting of the topological variety...")
                    L = st_g.topological_sort()

                    ## maximal profit path
                    # print ("Calculating path...")
                    # print ()
                    path = st_g.calc_max_profit_path(
                        L, sim_requests_left_to_deliver, multiple_visits)

                else:
                    path = orig_path
                    orig_path_used = True

                ### Execute
                path_visits = 1
                for visit in path[1:]:
                    if sim_curr_node == visit:
                        dist = 1
                    else:
                        # dist = g.get_distance(sim_curr_node, visit)
                        dist = g[sim_curr_node][visit]['weight']
                    sim_time += dist
                    sim_curr_node = visit
                    path_visits += 1
                    if visit == maintenance_node:
                        total_maintenance_profit += maintenance_reward
                    if visit in sim_requests_left_to_deliver:
                        # profit = availability_models[trial][visit](sim_time)
                        # total_profit += profit
                        # delivery_history.append([visit, sim_time])
                        # sim_requests_left_to_deliver.remove(visit)
                        curr_time_index = int(sim_time / time_interval)

                        # if curr_time_index > (num_intervals - 1):
                        #     print("Curr time index exceeds num intervals: " + str(curr_time_index) + ", " + str(num_intervals))
                        #     curr_time_index = num_intervals-1
                        assert (curr_time_index <= (num_intervals - 1))
                        available = sim_worlds[world_index][request][
                            curr_time_index]
                        if bool(available):
                            sim_requests_left_to_deliver.remove(visit)
                            total_profit += deliver_reward
                            delivery_history.append([visit, sim_time])

                            if multiple_visits:
                                if replan:
                                    if incorporate_observation:
                                        sim_availability_observations[
                                            visit] = [1, sim_time]
                                    path_visits = 0
                                    path_length = 1
                                    break

                        else:
                            if replan:
                                # print ("Replanning, curr time: " + str(sim_time))
                                if incorporate_observation:
                                    sim_availability_observations[visit] = [
                                        0, sim_time
                                    ]
                                path_visits = 0
                                path_length = 1
                                break

            ratio_divisor = num_requests + (budget -
                                            num_requests) * maintenance_reward
            competitive_ratio = (float(total_profit) +
                                 total_maintenance_profit) / ratio_divisor
            total_score += competitive_ratio

        ave_score = total_score / num_worlds
        if ave_score > best_score:
            best_path = orig_path
            best_score = ave_score

    return best_path