Beispiel #1
0
def plotEvolutionaryOptimization(directory, axs, plot_all_rollouts=False):

    #################################
    # Load and plot learning curve
    (update_at_samples, costs_all, eval_at_samples,
     costs_eval) = loadLearningCurve(directory)
    ax = (None if axs == None else axs[1])
    plotLearningCurve(eval_at_samples, costs_eval, ax, costs_all)
    #y_limits = [0,1.2*max(learning_curve)];
    plotUpdateLines(update_at_samples, ax)

    #################################
    # Load and plot exploration curve
    (covar_at_samples, sqrt_max_eigvals) = loadExplorationCurve(directory)
    ax = (None if axs == None else axs[0])
    plotExplorationCurve(covar_at_samples, sqrt_max_eigvals, ax)
    plotUpdateLines(update_at_samples, ax)

    n_updates = loadNumberOfUpdates(directory)
    ax = (None if axs == None else axs[2])
    if (ax != None):
        #################################
        # Visualize the update in parameter space
        for update in range(n_updates):
            cur_directory = '%s/update%05d' % (directory, update + 1)
            plotUpdateSummaryFromDirectory(cur_directory, ax, False)

        cur_directory = '%s/update%05d' % (directory, n_updates)
        plotUpdateSummaryFromDirectory(cur_directory, ax, True)
        ax.set_title('Search space')

    if ((axs != None) and (len(axs) > 3)):
        ax = axs[3]
        rollouts_python_script = directory + '/plotRollouts.py'
        if (os.path.isfile(rollouts_python_script)):
            lib_path = os.path.abspath(directory)
            sys.path.append(lib_path)
            from plotRollouts import plotRollouts

            for update in range(n_updates):
                cur_directory = '%s/update%05d' % (directory, update + 1)
                filename = "/cost_vars_eval.txt"
                if plot_all_rollouts:
                    filename = "/cost_vars.txt"
                cost_vars = np.loadtxt(cur_directory + filename)
                rollout_lines = plotRollouts(cost_vars, ax)
                color_val = (1.0 * update / n_updates)
                #cur_color = [1.0-0.9*color_val,0.1+0.9*color_val,0.1]
                cur_color = [
                    0.0 - 0.0 * color_val, 0.0 + 1.0 * color_val,
                    0.0 - 0.0 * color_val
                ]
                #print str(update)+" "+str(n_updates)+" "+str(cur_color)
                plt.setp(rollout_lines, color=cur_color)
                if (update == 0):
                    plt.setp(rollout_lines, color='r', linewidth=2)
def plotEvolutionaryOptimization(directory,axs,plot_all_rollouts=False):
      
    #################################
    # Load and plot learning curve
    (update_at_samples, costs_all, eval_at_samples, costs_eval) = loadLearningCurve(directory)
    ax = (None if axs==None else axs[1])
    plotLearningCurve(eval_at_samples,costs_eval,ax,costs_all)
    #y_limits = [0,1.2*max(learning_curve)];
    plotUpdateLines(update_at_samples,ax)
    
    
    #################################
    # Load and plot exploration curve
    (covar_at_samples, sqrt_max_eigvals) = loadExplorationCurve(directory)
    ax = (None if axs==None else axs[0])
    plotExplorationCurve(covar_at_samples,sqrt_max_eigvals,ax)
    plotUpdateLines(update_at_samples,ax)

    n_updates = loadNumberOfUpdates(directory)
    ax = (None if axs==None else axs[2])
    if (ax!=None):
        #################################
        # Visualize the update in parameter space 
        for update in range(n_updates):
            cur_directory = '%s/update%05d' % (directory, update+1)
            plotUpdateSummaryFromDirectory(cur_directory,ax,False)
            
        cur_directory = '%s/update%05d' % (directory, n_updates)
        plotUpdateSummaryFromDirectory(cur_directory,ax,True)
        ax.set_title('Search space')
        
    if ( (axs!=None) and (len(axs)>3) ):
        ax = axs[3];
        rollouts_python_script = directory+'/plotRollouts.py'
        if (os.path.isfile(rollouts_python_script)):
            lib_path = os.path.abspath(directory)
            sys.path.append(lib_path)
            from plotRollouts import plotRollouts
        
            for update in range(n_updates):
                cur_directory = '%s/update%05d' % (directory, update+1)
                filename = "/cost_vars_eval.txt"
                if plot_all_rollouts:
                    filename = "/cost_vars.txt"
                cost_vars = np.loadtxt(cur_directory+filename)
                rollout_lines = plotRollouts(cost_vars,ax)
                color_val = (1.0*update/n_updates)
                #cur_color = [1.0-0.9*color_val,0.1+0.9*color_val,0.1]
                cur_color = [0.0-0.0*color_val, 0.0+1.0*color_val, 0.0-0.0*color_val]
                #print str(update)+" "+str(n_updates)+" "+str(cur_color) 
                plt.setp(rollout_lines,color=cur_color)
                if (update==0):
                    plt.setp(rollout_lines,color='r',linewidth=2)
def plotEvolutionaryOptimization(n_updates,directory,axs=None,plot_all_rollouts=False):

    if (n_updates==0):
      n_updates += 1;
      
    #################################
    # Read data relevant to learning and exploration curve
    costs = []
    cost_evals = []
    n_samples_per_update = [0]
    covars_per_update = [];
    for update in range(n_updates):
      
        cur_directory = '%s/update%05d' % (directory, update+1)
        
        # Load evaluation cost
        try:
            cur_cost_eval = np.loadtxt(cur_directory+"/cost_eval.txt")
            cost_evals.append(np.atleast_1d(cur_cost_eval)[0])
        except IOError:
            cur_cost_eval = []
            
            
        # Load costs
        cur_costs = np.loadtxt(cur_directory+"/costs.txt")
        costs.extend(cur_costs)
        
        n_samples_per_update.append(len(costs))
        
        # Load covar matrix
        covar = np.loadtxt(cur_directory+"/distribution_covar.txt")
        covars_per_update.append(covar)

    # Load final covar matrix
    covar = np.loadtxt(cur_directory+"/distribution_new_covar.txt")
    covars_per_update.append(covar)
        
    #################################
    # Plot the exploration magnitude
    ax = (None if axs==None else axs[0])
    max_eigval_per_update = plotExplorationCurve(n_samples_per_update,covars_per_update,ax)
    
    #################################
    # Plot the learning curve
    ax = (None if axs==None else axs[1])
    update_centers, mean_costs = plotLearningCurve(n_samples_per_update,costs,cost_evals,ax)

    ax = (None if axs==None else axs[2])
    if (ax!=None):
        #################################
        # Visualize the update in parameter space 
        for update in range(n_updates):
            cur_directory = '%s/update%05d' % (directory, update+1)
            plotUpdateSummaryFromDirectory(cur_directory,ax,False)
            
        cur_directory = '%s/update%05d' % (directory, n_updates)
        plotUpdateSummaryFromDirectory(cur_directory,ax,True)
        ax.set_title('Search space')
        
    if ( (axs!=None) and (len(axs)>3) ):
        ax = axs[3];
        for update in range(n_updates):
            cur_directory = '%s/update%05d' % (directory, update+1)
            filename = "/cost_vars_eval.txt"
            if plot_all_rollouts:
                filename = "/cost_vars.txt"
            cost_vars = np.loadtxt(cur_directory+filename)
            rollout_lines = plotRollouts(cost_vars,ax)
            color_val = (1.0*update/n_updates)
            #cur_color = [1.0-0.9*color_val,0.1+0.9*color_val,0.1]
            cur_color = [0.0-0.0*color_val, 0.0+1.0*color_val, 0.0-0.0*color_val]
            #print str(update)+" "+str(n_updates)+" "+str(cur_color) 
            plt.setp(rollout_lines,color=cur_color)
            if (update==0):
                plt.setp(rollout_lines,color='r',linewidth=2)
                

    return (update_centers, mean_costs, n_samples_per_update, max_eigval_per_update)
def plotEvolutionaryOptimization(n_updates,
                                 directory,
                                 axs=None,
                                 plot_all_rollouts=False):

    if (n_updates == 0):
        n_updates += 1

    #################################
    # Read data relevant to learning and exploration curve
    costs = []
    cost_evals = []
    n_samples_per_update = [0]
    covars_per_update = []
    for update in range(n_updates):

        cur_directory = '%s/update%05d' % (directory, update + 1)

        # Load evaluation cost
        try:
            cur_cost_eval = np.loadtxt(cur_directory + "/cost_eval.txt")
            cost_evals.append(np.atleast_1d(cur_cost_eval)[0])
        except IOError:
            cur_cost_eval = []

        # Load costs
        cur_costs = np.loadtxt(cur_directory + "/costs.txt")
        costs.extend(cur_costs)

        n_samples_per_update.append(len(costs))

        # Load covar matrix
        covar = np.loadtxt(cur_directory + "/distribution_covar.txt")
        covars_per_update.append(covar)

    # Load final covar matrix
    covar = np.loadtxt(cur_directory + "/distribution_new_covar.txt")
    covars_per_update.append(covar)

    #################################
    # Plot the exploration magnitude
    ax = (None if axs == None else axs[0])
    max_eigval_per_update = plotExplorationCurve(n_samples_per_update,
                                                 covars_per_update, ax)

    #################################
    # Plot the learning curve
    ax = (None if axs == None else axs[1])
    update_centers, mean_costs = plotLearningCurve(n_samples_per_update, costs,
                                                   cost_evals, ax)

    ax = (None if axs == None else axs[2])
    if (ax != None):
        #################################
        # Visualize the update in parameter space
        for update in range(n_updates):
            cur_directory = '%s/update%05d' % (directory, update + 1)
            plotUpdateSummaryFromDirectory(cur_directory, ax, False)

        cur_directory = '%s/update%05d' % (directory, n_updates)
        plotUpdateSummaryFromDirectory(cur_directory, ax, True)
        ax.set_title('Search space')

    if ((axs != None) and (len(axs) > 3)):
        ax = axs[3]
        for update in range(n_updates):
            cur_directory = '%s/update%05d' % (directory, update + 1)
            filename = "/cost_vars_eval.txt"
            if plot_all_rollouts:
                filename = "/cost_vars.txt"
            cost_vars = np.loadtxt(cur_directory + filename)
            rollout_lines = plotRollouts(cost_vars, ax)
            color_val = (1.0 * update / n_updates)
            #cur_color = [1.0-0.9*color_val,0.1+0.9*color_val,0.1]
            cur_color = [
                0.0 - 0.0 * color_val, 0.0 + 1.0 * color_val,
                0.0 - 0.0 * color_val
            ]
            #print str(update)+" "+str(n_updates)+" "+str(cur_color)
            plt.setp(rollout_lines, color=cur_color)
            if (update == 0):
                plt.setp(rollout_lines, color='r', linewidth=2)

    return (update_centers, mean_costs, n_samples_per_update,
            max_eigval_per_update)