def benchmark(benchmarkfile,
              om=None,
              om_2=None,
              em=None,
              filename=None,
              fileformat="pdf"):
    plt = pp.setup()
    f = fh.load_pkl(benchmarkfile)
    bm = plot_benchmark(f)
    if om_2 is None: data = [bm, fh.load_pkl(om), fh.load_pkl(em)]
    else: data = [bm, fh.load_pkl(om), fh.load_pkl(om_2), fh.load_pkl(em)]
    plt.boxplot(data)
    plt.ylabel("Fitness $F_g$")
    labels = ["Random", "PR", "Evolved"]
    ax = plt.gca()
    ax.set_xticklabels(labels)
    plt = pp.adjust(plt)

    # Save or show
    if filename is not None:
        folder = "figures/benchmark/"
        if not os.path.exists(os.path.dirname(folder)):
            os.makedirs(os.path.dirname(folder))
        filename_raw = os.path.splitext(os.path.basename(filename))[0]
        plt.savefig(folder + "%s.%s" % (filename_raw, fileformat))
        plt.close()
    else:
        plt.show()
        plt.close()
Example #2
0
def main(args):
    parser = argparse.ArgumentParser(
        description='Simulate a task to gather the data for optimization')
    parser.add_argument('folder', type=str, help="(str) folder", default=None)
    parser.add_argument('-format', type=str, default="pdf")
    args = parser.parse_args(args)

    # Load evolution API
    e = evolution.evolution()

    # Initalize plot
    plt = pp.setup()

    # Initialize baseline limit in x axis
    lim = 0

    # Plot all evolution files found in the given folder together
    ## Extract all evolution files in the indicated folder
    filelist = sorted([
        f for f in os.listdir(args.folder)
        if f.startswith('evolution_') and f.endswith('pkl')
    ])

    # filelist = [f for f in os.listdir(args.folder) if
    # 				f.startswith('evolution_') and f.endswith('pkl')]
    li = []
    ## For each file in the list, add the evolution to the plot
    for file in filelist:
        ## Load the file
        e.load(args.folder + file)

        ## Plot it
        print(file)
        plt, l = plot_evolution(plt, e, label=str(file))
        li += l
        ## Fix lim according to the highest number of generations
        if len(e.stats) > lim:
            lim = len(e.stats)

    # Axis labels and dimensions
    plt.xlim(0, lim)
    plt.xlabel('Generation')
    plt.ylabel('Fitness')
    plt.legend([li[0], li[-1]], ['Model-based runs', 'Standard runs'])
    plt = pp.adjust(plt)

    # Save figure
    fname = os.path.dirname(args.folder)
    folder = "figures/evolution/"
    if not os.path.exists(os.path.dirname(folder)):
        os.makedirs(os.path.dirname(folder))
    filename_raw = os.path.splitext(os.path.basename(fname))[0]
    plt.savefig(folder + "evolution_%s.%s" % (filename_raw, args.format))
    plt.close()
Example #3
0
def main(args):
    # Parse arguments

    ## Load parser
    parser = argparse.ArgumentParser(
        description='Simulate a task to gather the data for optimization')

    ## Main arguments
    parser.add_argument('controller',
                        type=str,
                        help="(str) Training data folder")
    parser.add_argument('folder_training',
                        type=str,
                        help="(str) Training data folder")
    parser.add_argument('-format',
                        type=str,
                        default="pdf",
                        help="(str) Training data folder")

    ## Parse
    args = parser.parse_args(args)

    # Load data
    data = evaluate_model_values(args.folder_training)

    # Set folder
    folder = "figures/model/"

    # Plot the difference to the last estimate to check the convergence
    plt = pp.setup()
    for d in data:

        # Only plot the ones that showed transitions
        if d[-1] > 0:
            plt.plot(d - (d[-1]))  # Residual

    plt.xlabel("Simulation number")
    plt.ylabel("Difference to final estimate")
    plt = pp.adjust(plt)
    plt.ylim((-1, 1))

    # Create a directory if it doesn't exist
    if not os.path.exists(os.path.dirname(folder)):
        os.makedirs(os.path.dirname(folder))
    vname = os.path.basename(os.path.dirname(args.folder_training))

    # Save the figure
    plt.savefig(folder + "model_%s_%s.%s" %
                (args.controller, vname, args.format))
    plt.close()
Example #4
0
def plot_realtimefactor(filename, tl, figurename=None):
    plt = pp.setup(h=8)
    t, t_batch = load(filename)
    batchsize = t_batch.shape[1]
    alpha = 0.2
    tmean = t.mean(axis=1) / tl
    tmean_batch = t_batch.mean(axis=1) / tl
    tmean_batch_adj = t_batch.mean(axis=1) / tl / batchsize
    plt.plot(range(1, t.shape[0] + 1),
             1 / tmean,
             linestyle='solid',
             color='blue',
             label="Single")
    plt.plot(range(1, t_batch.shape[0] + 1),
             1 / tmean_batch,
             linestyle='dotted',
             color='orange',
             label="Batch (%i), individual" % batchsize)
    plt.plot(range(1, t_batch.shape[0] + 1),
             1 / tmean_batch_adj,
             linestyle='dashed',
             color='red',
             label="Batch (%i), cumulative" % batchsize)
    plt.fill_between(range(1,
                           len(tmean) + 1),
                     1 / (t.min(axis=1) / tl),
                     1 / (t.max(axis=1) / tl),
                     color='blue',
                     alpha=alpha)
    plt.fill_between(range(1,
                           len(tmean) + 1),
                     1 / (t_batch.min(axis=1) / tl),
                     1 / (t_batch.max(axis=1) / tl),
                     color='orange',
                     alpha=alpha)
    plt.fill_between(range(1,
                           len(tmean) + 1),
                     1 / (t_batch.min(axis=1) / tl / batchsize),
                     1 / (t_batch.max(axis=1) / tl / batchsize),
                     color='red',
                     alpha=alpha)
    plt.xlabel("Number of robots")
    plt.ylabel("Real time factor")
    plt.legend(loc="upper right")
    plt.xlim([0, 50])
    plt = pp.adjust(plt)
    plt.savefig(figurename) if figurename is not None else plt.show()
    plt.close()
Example #5
0
def plot_evaluationtime(filename, figurename=None):
    plt = pp.setup(h=8)
    t, t_batch = load(filename)
    tmean = t.mean(axis=1)
    batchsize = t_batch.shape[1]
    alpha = 0.2
    tmean_batch = t_batch.mean(axis=1)
    plt.plot(range(1, t.shape[0] + 1),
             tmean,
             linestyle='solid',
             color='blue',
             label="Single")
    plt.plot(range(1, t_batch.shape[0] + 1),
             tmean_batch / batchsize,
             linestyle='dotted',
             color='orange',
             label="Batch (%i), individual" % batchsize)
    plt.plot(range(1, t_batch.shape[0] + 1),
             tmean_batch,
             linestyle='dashed',
             color='red',
             label="Batch (%i), cumulative" % batchsize)
    plt.fill_between(range(1,
                           len(tmean) + 1),
                     t.min(axis=1),
                     t.max(axis=1),
                     color='blue',
                     alpha=alpha)
    plt.fill_between(range(1,
                           len(tmean) + 1),
                     t_batch.min(axis=1) / batchsize,
                     t_batch.max(axis=1) / batchsize,
                     color='orange',
                     alpha=alpha)
    plt.fill_between(range(1,
                           len(tmean) + 1),
                     t_batch.min(axis=1),
                     t_batch.max(axis=1),
                     color='red',
                     alpha=alpha)
    plt.xlabel("Number of robots")
    plt.ylabel("Wall-clock time [s]")
    plt.legend(loc="upper left")
    plt.xlim([0, 50])
    plt = pp.adjust(plt)
    plt.savefig(figurename) if figurename is not None else plt.show()
    plt.close()
Example #6
0
def main(args):
    # Input argument parser
    parser = argparse.ArgumentParser(
        description='Simulate a task to gather the data for optimization')
    parser.add_argument('controller',
                        type=str,
                        help="(str) path to logs folder")
    parser.add_argument('-format', type=str, default="pdf")
    args = parser.parse_args(args)

    pf = "data/%s/" % args.controller
    folders = ["onlinelearning_detach_false_shared_false_iter_10_boost_1/"\
    ,"onlinelearning_detach_false_shared_true_iter_10_boost_1/"]
    plt = pp.setup()
    li = []
    for i, f in enumerate(folders):
        tv, fv, f_mean, f_std, nlogs = process_logs(pf + f, "log")

        if i == 0: c = "red"
        else: c = "blue"

        # Plot
        l = plt.plot(tv[0], fv, alpha=0.2, color="gray")
        l2 = plt.plot(tv[0], f_mean, color=c)
        li += l2
        plt.fill_between(tv[0],
                         f_mean - f_std,
                         f_mean + f_std,
                         alpha=0.2,
                         color=c)

    # Axis
    # plt.legend()
    plt.legend([li[0], li[-1]], ['Local model', 'Shared local model'])
    plt = pp.adjust(plt)
    plt.xlabel("Time [s]")
    plt.ylabel("Fitness [-]")

    # # Save or show
    fname = "fitness_logs_%s.%s" % (args.controller, args.format)
    folder = "figures/onlinelearning/"
    if not os.path.exists(os.path.dirname(folder)):
        os.makedirs(os.path.dirname(folder))
    filename_raw = os.path.splitext(os.path.basename(fname))[0]
    plt.savefig(folder + "onlinelearning_%s.%s" % (filename_raw, args.format))
    plt.close()
Example #7
0
def main(args):
    # Input argument parser
    parser = argparse.ArgumentParser(
        description='Simulate a task to gather the data for optimization')
    parser.add_argument('controller', type=str, help="(str) controller")
    parser.add_argument('folder', type=str, help="(str) path to logs folder")
    parser.add_argument('folder2', type=str, help="(str) path to logs folder")
    parser.add_argument('-format', type=str, default="pdf")
    args = parser.parse_args(args)

    tv, fv, f_mean, f_std = process_logs(args.folder, "sample_log")
    tv_e, fv_e, f_mean_e, f_std_e = process_logs(args.folder2, "evo_log")

    # Plot
    plt = pp.setup()
    plt.plot(tv[0], f_mean, color="blue")
    plt.fill_between(tv[0],
                     f_mean - f_std,
                     f_mean + f_std,
                     alpha=0.2,
                     color="blue")
    plt.plot(tv_e[0], f_mean_e, color="red")
    plt.fill_between(tv_e[0],
                     f_mean_e - f_std_e,
                     f_mean_e + f_std_e,
                     alpha=0.2,
                     color="red")
    plt = pp.adjust(plt)
    plt.xlabel("Time [s]")
    plt.ylabel("Fitness [-]")

    # Save or show
    fname = "fitness_logs_%s.%s" % (args.controller, args.format)
    if fname is not None:
        folder = "figures/fitness_logs/"
        if not os.path.exists(os.path.dirname(folder)):
            os.makedirs(os.path.dirname(folder))
        filename_raw = os.path.splitext(os.path.basename(fname))[0]
        plt.savefig(folder + "%s.%s" % (filename_raw, args.format))
        plt.close()
    else:
        plt.show()
        plt.close()
def main(args):
    ####################################################################
    # Initialize

    # Argument parser
    parser = argparse.ArgumentParser(
        description='Simulate a task to gather the data for optimization')
    parser.add_argument('controller', type=str, help="(str) Controller to use")
    parser.add_argument('folder', type=str, help="(str) Folder to use")
    parser.add_argument('-format',
                        type=str,
                        default="pdf",
                        help="(str) Save figure format")
    parser.add_argument('-plot',
                        action='store_true',
                        help="(bool) Animate flag to true")
    parser.add_argument('-verbose',
                        action='store_true',
                        help="(bool) Animate flag to true")
    args = parser.parse_args(args)

    # Load parameters
    fitness, controller, agent, pr_states, pr_actions = \
     parameters.get(args.controller)
    ####################################################################

    ####################################################################
    # Load optimization files
    files_train = [f for f in os.listdir(args.folder) \
     if f.startswith("optimization") and f.endswith('.npz')]

    # Unpack last file
    data = np.load(args.folder + files_train[-1])
    H0 = data["H0"].astype(float)
    H1 = data["H1"].astype(float)
    # Fix rounding errors
    H0[H0 < 0.01] = 0.00000
    H1[H1 < 0.01] = 0.00000
    E = matop.normalize_rows(data["E"])
    policy = data["policy"]
    des = data["des"]
    alpha = data["alpha"]
    ####################################################################

    ####################################################################
    # if -plot
    # Plot and display relevant results

    if args.plot:

        # Calculate parameters
        ## Calculate Google matrices
        G0 = np.diag(alpha).dot(H0) + np.diag(1 - alpha).dot(E)
        G1 = np.diag(alpha).dot(H1) + np.diag(1 - alpha).dot(E)

        ## PageRank scores
        prH0 = matop.pagerank(H0)
        prE = matop.pagerank(E)
        pr0 = matop.pagerank(G0)
        pr1 = matop.pagerank(G1)

        ## Initialize pagerank optimizer for evaluation
        ## Using dummy inputs, since init not needed
        p = propt.pagerank_evolve(des, np.array([H0, H1]), E)

        ## Get original fitness and new fitness
        f0 = p.pagerank_fitness(pr0, des)
        f1 = p.pagerank_fitness(pr1, des)

        # Make a folder to store the figures
        folder = "figures/pagerank"
        if not os.path.exists(os.path.dirname(folder)):
            os.makedirs(os.path.dirname(folder))

        #Now let's plot some figures
        import math
        xint = range(0, math.ceil(pr1[0].size), 2)

        # Figure: Plot pagerank H and E
        plt = pp.setup()
        plt.bar(np.array(range(prH0[0].size)),
                prH0[0],
                alpha=0.5,
                label="$PR^\pi$, $\mathbf{H^\pi}$ only")
        plt.bar(np.array(range(prE[0].size)),
                prE[0],
                alpha=0.5,
                label="$PR^\pi$, $\mathbf{E}$ only")
        plt = pp.adjust(plt)
        plt.xlabel("State")
        plt.ylabel("PageRank [-]")
        matplotlib.pyplot.xticks(xint)
        plt.legend()
        plt.savefig("%s/pagerank_original_%s.%s" \
         %(folder,controller,args.format))
        plt.close()

        # Figure: Diff plot of pagerank values
        plt = pp.setup()
        c = ["blue", "green"]
        color_list = list(map(lambda x: c[1] if x > 0.01 else c[0], des))
        if controller == "forage":
            plt.bar(range(pr1[0].size), (pr1[0] - pr0[0]) * 1000,
                    label="$PR^\pi-PR^{\pi^\star}$",
                    color=color_list)
            plt.ylabel("$\Delta$ PageRank (" r"$\times$" r"1000) [-]")
        else:
            plt.bar(range(pr1[0].size), (pr1[0] - pr0[0]),
                    label="$PR^\pi-PR^{\pi^\star}$",
                    color=color_list)
            plt.ylabel("$\Delta$ PageRank [-]")
        plt = pp.adjust(plt)
        plt.xlabel("State [-]")
        matplotlib.pyplot.xticks(xint)

        # Custom legend
        custom_lines = [
            matplotlib.lines.Line2D([0], [0], color="blue", lw=20),
            matplotlib.lines.Line2D([0], [0], color="green", lw=20)
        ]
        plt.legend(custom_lines, ['Transitional', 'Desired'])
        plt.savefig("%s/pagerank_diff_%s.%s" %
                    (folder, controller, args.format))
        plt.close()
        return
    ####################################################################

    ####################################################################
    # if -verbose
    # Display relevant results to terminal
    if args.verbose:
        print("\n------- MODEL -------\n")
        print("\nH0 matrix:\n", H0)
        print("\nH1 matrix:\n", H1)
        print("\nE matrix:\n", E)
        print("\nalpha vector:\n", alpha)
        print("\n------- POLICY -------\n", policy)
        # print("\n------- STATS -------\n")
        # print("Original fitness =", f0[0])
        # print("New fitness =", f1[0])

    # Check conditions on last file
    e = 0.00000001
    H0[H0 > e] = 1
    H1[H1 > e] = 1
    E[E > e] = 1
    H0 = H0.astype(int)
    H1 = H1.astype(int)
    E = E.astype(int)
    c = verification.verification(H0, H1, E, policy, des)
    c.verify()
def main(args):
    ####################################################################
    # Initialize
    # Input arguments
    parser = argparse.ArgumentParser(
        description='Simulate a task to gather the data for optimization')
    parser.add_argument('folder_train',
                        type=str,
                        help="(str) Training data folder",
                        default=None)
    parser.add_argument('folder_test',
                        type=str,
                        help="(str) Validation data folder",
                        default=None)
    parser.add_argument('savefolder',
                        type=str,
                        help="(str) Save folder",
                        default=None)
    parser.add_argument('-id',
                        type=int,
                        help="Model ID (for save/load)",
                        default=np.random.randint(0, 10000))
    parser.add_argument('-train',
                        action='store_true',
                        help="(bool) Train flag to true")
    parser.add_argument(
        '-validate',
        action='store_true',
        help="(bool) Validate flag to true (checks all models)")
    parser.add_argument(
        '-evaluate',
        action='store_true',
        help="(bool) Evaluate flag to true (checks last model only)")
    parser.add_argument(
        '-plot',
        action='store_true',
        help="(bool) Plot all validation sets (checks last model only)")
    parser.add_argument('-layer_size',
                        type=int,
                        help="Nodes in hidden layers",
                        default=100)
    parser.add_argument('-layers',
                        type=int,
                        help="Number of hiddent layers",
                        default=3)
    parser.add_argument('-lr',
                        type=float,
                        help="Number of hiddent layers",
                        default=1e-6)

    args = parser.parse_args(args)

    # Load files
    folder_train = args.folder_train
    folder_test = args.folder_test
    save_folder = args.savefolder

    # Make the save_folder if it does not exist
    if not os.path.exists(os.path.dirname(save_folder)):
        os.makedirs(os.path.dirname(save_folder))

    files_train = [f for f in os.listdir(folder_train) if f.endswith('.npz')]
    files_test = [
        f for f in os.listdir(folder_test + "/") if f.endswith('.npz')
    ]

    # Initialize desired states extractor
    dse = desired_states_extractor.desired_states_extractor()
    ####################################################################

    ####################################################################
    # if -train
    # Else try to load pre-trained sets in a file called "models.pkl"
    if args.train:
        nets = []
        i = 0
        for filename in tqdm(sorted(files_train)):
            model = dse.train(folder_train + filename,
                              layer_size=args.layer_size,
                              layers=args.layers,
                              lr=args.lr)
            print(model[0].network)
            nets.append(copy.deepcopy(model))
            i += 1
        fh.save_pkl(nets, "%s/models.pkl" % (save_folder))
    else:
        nets = fh.load_pkl("%s/models.pkl" % (save_folder))
    ####################################################################

    ####################################################################
    # If -validate
    # Crosscheck all models against all validation files
    if args.validate:
        v = []
        for model in tqdm(nets):
            e = []
            for filename in sorted(files_test):
                _, s, f = dse.extract_states(folder_test + "/" + filename,
                                             load_pkl=True)
                _, corr, _ = dse.evaluate_model(model[0], s, f)
                e.append(corr)
            v.append(e)
            print(np.mean(e))  # Display progress

        # Save to file
        vname = os.path.basename(os.path.dirname(folder_test))
        fh.save_pkl(
            v, "%s/validation_%s_id%i.pkl" % (save_folder, vname, args.id))
    ####################################################################

    ####################################################################
    # If -evaluate
    # Crosscheck the correlation of the last model against validation set
    # This is mainly for debugging purposes on the last model
    if args.evaluate:
        # Get the most recent network
        model = nets[-1]

        # Evaluate the correlation for the most recent network
        # to the validation dataset
        e = []
        y_pred = []
        for i, filename in enumerate(sorted(files_test)):
            t, s, f = dse.extract_states(args.folder_test + "/" + filename,
                                         load_pkl=True)
            _, corr, y_pred_i = dse.evaluate_model(model[0], s, f)
            e.append(corr)
            if args.plot:
                fname = "nn_test_%s_%i.%s" % (os.path.dirname(save_folder), i,
                                              "pdf")
                folder = "figures/nn/"
                if not os.path.exists(os.path.dirname(folder)):
                    os.makedirs(os.path.dirname(folder))
                filename_raw = os.path.splitext(os.path.basename(fname))[0]
                plt = pp.setup()
                plt.plot(t, f, color="blue", label="Real")
                plt.plot(t, y_pred_i, color="red", label="Predicted")
                plt.ylabel("Fitness")
                plt.xlabel("Time [s]")
                plt.legend(loc="upper left", ncol=2)
                plt = pp.adjust(plt)
                plt.savefig(folder + "%s.%s" % (filename_raw, "pdf"))
                plt.close()

        # Display some data
        if args.plot is False:
            print(np.mean(e))  # Mean error
            print(model[0].optimizer)  # Optimizer parameters
            print(model[0].network)  # Network parameters
Example #10
0
def main(args):
    # Input argument parser
    parser = argparse.ArgumentParser(description='Simulate a task to gather \
						 the data for optimization')
    parser.add_argument('controller',
                        type=str,
                        default=None,
                        help="(str) Controller")
    parser.add_argument('-format',
                        type=str,
                        default="pdf",
                        help="(str) Figure file format, default pdf")
    args = parser.parse_args(args)

    # Default character init
    df = "data/%s/validation_%s_" % (args.controller, args.controller)

    filenames = []
    name = []
    if args.controller == "aggregation":
        filenames.append(df + "aggregation_1.pkl")
        filenames.append(df + "aggregation_2.pkl")
        filenames.append(df + "aggregation_3.pkl")
        # Plot names
        name.append("VS 1")
        name.append("VS 2")
        name.append("VS 3")
    elif args.controller == "pfsm_exploration":
        # Files where it evaluates against itself
        filenames.append(df + "pfsm_exploration_1.pkl")
        filenames.append(df + "pfsm_exploration_2.pkl")
        filenames.append(df + "pfsm_exploration_3.pkl")
        # Files where it evaluates against different dynamics
        filenames.append(df + "pfsm_exploration_mod_1.pkl")
        filenames.append(df + "pfsm_exploration_mod_2.pkl")
        filenames.append(df + "pfsm_exploration_mod_3.pkl")
        name.append("VS 1 (B1)")
        name.append("VS 2 (B1)")
        name.append("VS 3 (B1)")
        name.append("VS 1 (B2)")
        name.append("VS 2 (B2)")
        name.append("VS 3 (B2)")
    elif args.controller == "pfsm_exploration_mod":
        # Files where it evaluates against itself
        filenames.append(df + "pfsm_exploration_mod_1.pkl")
        filenames.append(df + "pfsm_exploration_mod_2.pkl")
        filenames.append(df + "pfsm_exploration_mod_3.pkl")
        # Files where it evaluates against different dynamics
        filenames.append(df + "pfsm_exploration_1.pkl")
        filenames.append(df + "pfsm_exploration_2.pkl")
        filenames.append(df + "pfsm_exploration_3.pkl")
        name.append("VS 1 (B2)")
        name.append("VS 2 (B2)")
        name.append("VS 3 (B2)")
        name.append("VS 1 (B1)")
        name.append("VS 2 (B1)")
        name.append("VS 3 (B1)")
    elif args.controller == "forage":
        filenames.append(df + "forage_1.pkl")
        filenames.append(df + "forage_2.pkl")
        filenames.append(df + "forage_3.pkl")
        name.append("VS 1")
        name.append("VS 2")
        name.append("VS 3")
    else:
        print("Not a valid mode!!!!")

    def process(file):
        m = []  # Mean
        s = []  # Standard deviation
        for e in file:
            m.append(np.nanmean(e))
            s.append(np.nanstd(e))
        return m, s

    # Load and process the data in each file
    data = []
    for f in filenames:
        data.append(process(fh.load_pkl(f)))

    # Plot
    color = ["blue", "red", "green", "black", "magenta", "lime"]
    plt = pp.setup()
    for i, d in enumerate(data):

        # Plot line
        plt.plot(d[0], label=name[i], color=color[i])

        # Error margin
        plt.fill_between(range(len(d[0])),
                         np.array(d[0]) - np.array(d[1]),
                         np.array(d[0]) + np.array(d[1]),
                         alpha=0.2,
                         color=color[i])

    # Plot deets
    plt.xlabel("Simulation")
    plt.ylabel("Correlation")
    plt.legend()
    plt = pp.adjust(plt)

    # Save
    fname = "nn_correlation_%s.%s" % (args.controller, args.format)
    folder = "figures/nn/"
    if not os.path.exists(os.path.dirname(folder)):
        os.makedirs(os.path.dirname(folder))
    filename_raw = os.path.splitext(os.path.basename(fname))[0]
    plt.savefig(folder + "%s.%s" % (filename_raw, args.format))
    plt.close()