def test_with_twitter_data():
    max_tweets = 100
    data = read_twitter_data("../tweets.csv",max_tweets) #threshold on maximum tweets, > 500 gets slower due to there is a lot of words.
                                                 #That should be improved somehow in order to visualize data in a better way.
    print "finish reading data"
    adj =  build_distance(data)
    print "finish building distance"
    matrix,labels = build_adjacency_matrix(adj)
    k = 10
    clusters = single_link_clustering(matrix,100)
    colors = build_colors(clusters)
    plot_helper.plot(matrix,None, colors) 
Ejemplo n.º 2
0
def train():
    plot_scores = []
    plot_mean_scores = []
    total_score = 0
    record = 0
    agent = Agent()
    game = SnakeGameAI()
    while True:
        # get old state
        state_old = agent.get_state(game)

        # get move
        final_move = agent.get_action(state_old)

        # perform move and get new state
        reward, done, score = game.play_step(final_move)
        state_new = agent.get_state(game)

        # train short memory
        agent.train_short_memory(state_old, final_move, reward, state_new,
                                 done)

        # remember
        agent.remember(state_old, final_move, reward, state_new, done)

        if done:
            # train long memory, plot result
            game.reset()
            agent.n_games += 1
            agent.train_long_memory()

            if score > record:
                record = score
                agent.model.save()

            print('Game', agent.n_games, 'Score', score, 'Record:', record)

            plot_scores.append(score)
            total_score += score
            mean_score = total_score / agent.n_games
            plot_mean_scores.append(mean_score)
            plot(plot_scores, plot_mean_scores)
    print("===Using saved trials")

    xstar_firsts_lst = read_saved_data_from_file(
        "xstar_firsts_lst_ci_{}e{}.txt".format(scenario_name, inflation))
    xstar_totals_lst = read_saved_data_from_file(
        "xstar_totals_lst_ci_{}e{}.txt".format(scenario_name, inflation))
    eager_xstar_firsts_lst = read_saved_data_from_file(
        "eager_xstar_firsts_lst_ci_{}e{}.txt".format(scenario_name, inflation))
    eager_xstar_totals_lst = read_saved_data_from_file(
        "eager_xstar_totals_lst_ci_{}e{}.txt".format(scenario_name, inflation))

#plot_helper.plot_ci(kNumRobotsList, xstar_firsts_lst, 3, "X* First Search")
#plot_helper.plot_ci(kNumRobotsList, xstar_totals_lst, 3, "X* Full Search")
#plot_helper.plot_ci(kNumRobotsList, mstar_totals_lst, 3, "M* First/Full Search")
plot_helper.plot(
    kNumRobotsList,
    get_percent_different(eager_xstar_firsts_lst, xstar_firsts_lst), 2,
    "First Solution Improvement")
plot_helper.plot(
    kNumRobotsList,
    get_percent_different(eager_xstar_totals_lst, xstar_totals_lst), 2,
    "Opt. Solution Improvement")


def to_percent(y, position):
    # Ignore the passed in position. This has the effect of scaling the default
    # tick locations.
    s = str(100 * y)

    # The percent symbol needs escaping in latex
    if matplotlib.rcParams['text.usetex'] is True:
        return s + r'$\%$'
Ejemplo n.º 4
0
                                                 scenario_name, inflation),
        nwastar_totals_lst)
else:
    print("===Using saved trials")
    xstar_firsts_lst = read_saved_data_from_file(
        "xstar_firsts_lst_{}_{}e{}.txt".format(experiment_name, scenario_name,
                                               inflation))
    xstar_totals_lst = read_saved_data_from_file(
        "xstar_totals_lst_{}_{}e{}.txt".format(experiment_name, scenario_name,
                                               inflation))
    nwastar_firsts_lst = read_saved_data_from_file(
        "nwastar_firsts_lst_{}_{}e{}.txt".format(experiment_name,
                                                 scenario_name, inflation))
    nwastar_totals_lst = read_saved_data_from_file(
        "nwastar_totals_lst_{}_{}e{}.txt".format(experiment_name,
                                                 scenario_name, inflation))

plot_helper.plot(kAgentCount, xstar_firsts_lst, 4, "X* First Search")
plot_helper.plot(kAgentCount, xstar_totals_lst, 4, "X* Full Search")
plot_helper.plot(kAgentCount, nwastar_firsts_lst, 4, "NWA* First Search")
plot_helper.plot(kAgentCount, nwastar_totals_lst, 4, "NWA* Full Search")
plt.ylim(bottom=0)
plt.xticks(kAgentCount)
plt.xlabel("Agent Count")
plt.ylabel("Number of Expansions")
#plt.yscale('log')
plt.legend(loc=2)
plt.savefig("{}_{}_{}.png".format(experiment_name, scenario_name, inflation),
            bbox_inches='tight')
plt.show()
xstar_totals_lst = []

if kRunNewTrials:
    print("===Running new trials")
    for inflation in kInflationValues:
        seed = int(inflation * 100 * 53 + 17)
        print("Inflation:", inflation)
        print("X*")
        xstar_first_exps, xstar_total_exps = run_xstar(num_robots, inflation)
        xstar_firsts_lst.append(xstar_first_exps)
        xstar_totals_lst.append(xstar_total_exps)

    save_data_to_file("xstar_firsts_lst_{}_{}e{}.txt".format(experiment_name, scenario_name, inflation), xstar_firsts_lst)
    save_data_to_file("xstar_totals_lst_{}_{}e{}.txt".format(experiment_name, scenario_name, inflation), xstar_totals_lst)
else:
    inflation = kInflationValues[-1]
    print("===Using saved trials")
    xstar_firsts_lst = read_saved_data_from_file("xstar_firsts_lst_{}_{}e{}.txt".format(experiment_name, scenario_name, kInflationValues[-1]))
    xstar_totals_lst = read_saved_data_from_file("xstar_totals_lst_{}_{}e{}.txt".format(experiment_name, scenario_name, kInflationValues[-1]))

plot_helper.plot(kInflationValues, xstar_firsts_lst, 2, "X* First Solution")
plot_helper.plot(kInflationValues, xstar_totals_lst, 2, "X* Full Solution")
plt.ylim(bottom=0)
plt.xticks(kInflationValues)
plt.xlabel("Heuristic Inflation")
plt.ylabel("State Expansions")
#plt.yscale('log')
plot_helper.legend(loc=1)
plot_helper.save_fig("xstar_heuristic_inflation")
plt.show()
Ejemplo n.º 6
0
xstar_firsts_lst = []
xstar_totals_lst = []

if kRunNewTrials:
    print("===Running new trials")
    for num_robots in kAgentCount:
        seed = int(num_robots * 100 * 53 + 17)
        print("Agent Count:", num_robots)
        print("X*")
        xstar_first_exps, xstar_total_exps = run_xstar(num_robots)
        xstar_firsts_lst.append(xstar_first_exps)
        xstar_totals_lst.append(xstar_total_exps)

    save_data_to_file("xstar_firsts_lst_{}_{}e{}.txt".format(experiment_name, scenario_name, inflation), xstar_firsts_lst)
    save_data_to_file("xstar_totals_lst_{}_{}e{}.txt".format(experiment_name, scenario_name, inflation), xstar_totals_lst)
else:
    print("===Using saved trials")
    xstar_firsts_lst = read_saved_data_from_file("xstar_firsts_lst_{}_{}e{}.txt".format(experiment_name, scenario_name, inflation))
    xstar_totals_lst = read_saved_data_from_file("xstar_totals_lst_{}_{}e{}.txt".format(experiment_name, scenario_name, inflation))

plot_helper.plot(kAgentCount, xstar_firsts_lst, 2, "X* First Solution")
plot_helper.plot(kAgentCount, xstar_totals_lst, 2, "X* Opt. Solution")
plt.ylim(bottom=5)
plt.xticks(kAgentCount)
plt.xlabel("Agent Count")
plt.ylabel("Number of Expansions")
plt.yscale('log')
plot_helper.legend(loc=2)
plot_helper.save_fig("xstar_agent_sweep")
plt.show()
xstar_firsts_lst = []
xstar_totals_lst = []

if kRunNewTrials:
    print("===Running new trials")
    for radius in kRadiusValues:
        seed = int(radius * 100 * 53 + 17)
        print("radius:", radius)
        print("X*")
        xstar_first_exps, xstar_total_exps = run_xstar(num_robots, radius)
        xstar_firsts_lst.append(xstar_first_exps)
        xstar_totals_lst.append(xstar_total_exps)

    save_data_to_file("xstar_firsts_lst_{}_{}e{}.txt".format(experiment_name, scenario_name, inflation), xstar_firsts_lst)
    save_data_to_file("xstar_totals_lst_{}_{}e{}.txt".format(experiment_name, scenario_name, inflation), xstar_totals_lst)
else:
    print("===Using saved trials")
    xstar_firsts_lst = read_saved_data_from_file("xstar_firsts_lst_{}_{}e{}.txt".format(experiment_name, scenario_name, inflation))
    xstar_totals_lst = read_saved_data_from_file("xstar_totals_lst_{}_{}e{}.txt".format(experiment_name, scenario_name, inflation))

plot_helper.plot(kRadiusValues, xstar_firsts_lst, 2, "X* First Solution")
plot_helper.plot(kRadiusValues, xstar_totals_lst, 2, "X* Opt. Solution")
plt.ylim(bottom=0)
plt.xticks(kRadiusValues)
plt.xlabel("Radius Values")
plt.ylabel("State Expansions")
#plt.yscale('log')
plot_helper.legend(loc=4)
plot_helper.save_fig("xstar_radius_sweep")
plt.show()
    save_data_to_file(
        "final_percentage_iterations_{}.txt".format(experiment_name,
                                                    scenario_name, inflation),
        final_percentage_iterations)
else:
    print("===Using saved trials")
    final_precentage_solutions = read_saved_data_from_file(
        "xstar_soltion_quality_{}.txt".format(experiment_name, scenario_name,
                                              inflation))
    final_percentage_iterations = read_saved_data_from_file(
        "final_percentage_iterations_{}.txt".format(experiment_name,
                                                    scenario_name, inflation))

xs = list(range(1, len(final_precentage_solutions) + 1))

plot_helper.plot(xs, final_percentage_iterations, 2, None)


def to_percent(y, position):
    # Ignore the passed in position. This has the effect of scaling the default
    # tick locations.
    s = str(100 * y)

    # The percent symbol needs escaping in latex
    if matplotlib.rcParams['text.usetex'] is True:
        return s + r'$\%$'
    else:
        return s + '%'


plt.ylim(bottom=0)
    save_data_to_file(
        "xstar_totals_lst_{}_{}e{}.txt".format(experiment_name, scenario_name,
                                               inflation), xstar_totals_lst)
    save_data_to_file(
        "astar_totals_lst_{}_{}e{}.txt".format(experiment_name, scenario_name,
                                               inflation), astar_totals_lst)
else:
    print("===Using saved trials")
    xstar_firsts_lst = read_saved_data_from_file(
        "xstar_firsts_lst_{}_{}e{}.txt".format(experiment_name, scenario_name,
                                               inflation))
    xstar_totals_lst = read_saved_data_from_file(
        "xstar_totals_lst_{}_{}e{}.txt".format(experiment_name, scenario_name,
                                               inflation))
    astar_totals_lst = read_saved_data_from_file(
        "astar_totals_lst_{}_{}e{}.txt".format(experiment_name, scenario_name,
                                               inflation))

plot_helper.plot(kAgentCount, xstar_firsts_lst, 3, "X* First Search")
plot_helper.plot(kAgentCount, xstar_totals_lst, 3, "X* Full Search")
plot_helper.plot(kAgentCount, astar_totals_lst, 3, "A* Full Search")
plt.ylim(bottom=0)
plt.xticks(kAgentCount)
plt.xlabel("Agent Count")
plt.ylabel("Number of Expansions")
#plt.yscale('log')
plt.legend(loc=2)
plt.savefig("{}_{}_{}.png".format(experiment_name, scenario_name, inflation),
            bbox_inches='tight')
plt.show()