def cmp_max_mutation_vs_mutation_rate():
    X, y = read_xy_data(
        '../data/ga/griewank/max_mutation_amount,mutation_rate.dat')

    b = get_regression_coef(X, y)

    x1_start = 0.1
    x1_step = 0.1
    x1_end = 1.0
    x2_start = 0.1
    x2_step = 0.1
    x2_end = 1.0

    fig = plt.figure()
    ax1 = fig.add_subplot(111, projection='3d')
    ax1.set_zlim(3, 9)

    for i, row in enumerate(X):
        ax1.scatter(row[1], row[2], y[i])

    pltx = np.arange(x1_start, x1_end + x1_step, x1_step)
    plty = np.arange(x2_start, x2_end + x2_step, x2_step)
    pltX, pltY = np.meshgrid(pltx, plty)
    F = b[0] + b[1] * pltX + b[2] * pltY + b[3] * pltX * pltX + b[
        4] * pltX * pltY + b[5] * pltY * pltY
    ax1.plot_wireframe(pltX, pltY, F)
    ax1.contour(pltX, pltY, F, zdir='z', offset=3, cmap=cm.jet)

    ax1.set_title(
        'Response Surface of Mutation Rate vs Maximum Mutation Allowed of GA on Griewank Function'
    )
    ax1.set_xlabel('Mutation Amount')
    ax1.set_ylabel('Mutation Rate')
    ax1.set_zlabel('Mean Euclidean Distance from Global Minimum')
    plt.show()
Beispiel #2
0
def cmp_cp_vs_cg():
    X, y = read_xy_data('cp,cg_ackley.dat')

    b = get_regression_coef(X, y)

    x1_start = 0.0
    x1_step = 0.05
    x1_end = 1.0
    x2_start = 0.0
    x2_step = 0.05
    x2_end = 1.0

    fig = plt.figure()
    ax1 = fig.add_subplot(111, projection='3d')
    ax1.set_zlim(0, 1)

    for i, row in enumerate(X):
        ax1.scatter(row[1],row[2],y[i])

    pltx = np.arange(x1_start, x1_end+x1_step, x1_step)
    plty = np.arange(x2_start, x2_end+x2_step, x2_step)
    pltX, pltY = np.meshgrid(pltx, plty)
    F = b[0] + b[1]*pltX + b[2]*pltY + b[3]*pltX*pltX + b[4]*pltX*pltY + b[5]*pltY*pltY
    #ax1.plot_wireframe(pltX, pltY, F)

    ax1.set_xlabel('CP')
    ax1.set_ylabel('CG')
    ax1.set_zlabel('Objective Function Value')
    plt.show()
def create_bulk_var_cmp_graphs():
    import ga_tests

    input_loc = '../tmp/'
    output_loc = '../tmp/'

    x1_start = 0.1
    x1_step = 0.1
    x1_end = 1.0
    x2_start = 0.1
    x2_step = 0.1
    x2_end = 1.0

    tests = ga_tests.tests
    functions = ga_tests.functions

    for f in functions:
        for t in tests.items():
            names = t[1]
            x1_name = names['x1']
            x2_name = names['x2']

            filename = input_loc + gen_filename(x1_name, x2_name, f)
            (X, y) = read_xy_data(filename)

            b = get_regression_coef(X, y)

            fig = plt.figure()
            ax1 = fig.add_subplot(111, projection='3d')
            #ax1.set_zlim(3, 9)

            for i, row in enumerate(X):
                ax1.scatter(row[1], row[2], y[i])

            pltx = np.arange(x1_start, x1_end + x1_step, x1_step)
            plty = np.arange(x2_start, x2_end + x2_step, x2_step)
            pltX, pltY = np.meshgrid(pltx, plty)
            F = b[0] + b[1] * pltX + b[2] * pltY + b[3] * pltX * pltX + b[
                4] * pltX * pltY + b[5] * pltY * pltY
            #ax1.plot_wireframe(pltX, pltY, F)
            ax1.contour(pltX, pltY, F, zdir='z', offset=0, cmap=cm.jet)

            ax1.set_title(
                'Response Surface of Mutation Rate vs Selection Cutoff of GA on Griewank Function'
            )
            ax1.set_xlabel('Selection Cutoff')
            ax1.set_ylabel('Mutation Rate')
            ax1.set_zlabel('Mean Euclidean Distance from Global Minimum')
            plt.show()
def plot_data(x1_info, x2_info, func, zbounds=None):
    c_i = 0
    to_plot = [0.1, 0.5, 0.9]

    x1_var = x1_info[NAME]
    x2_var = x2_info[NAME]
    x1_name = x1_var.replace('_', ' ').title()
    x2_name = x2_var.replace('_', ' ').title()

    func = func + '_function'
    func_name = func.replace('_', ' ').title()
    title = 'Response Surface of %s vs %s of %s on %s' % (x1_name, x2_name, \
                                                          algorithm_name, func_name)

    fig = plt.figure()
    ax1 = fig.add_subplot(111, projection='3d')
    if zbounds is not None:
        ax1.set_zlim(zbounds[0], zbounds[1])

    for j in to_plot:
        f = 'rosenbrock/cutoff_vs_rate_(mma_%.2f)_' % j
        filename = input_loc + f + func + '.dat'
        #filename = input_loc + gen_filename(x1_var, x2_var, func)
        (X, y) = read_xy_data(filename)

        b = get_regression_coef(X, y)

        #for i, row in enumerate(X):
        #    ax1.scatter(row[1],row[2],y[i], color=colors[c_i])

        pltx = np.arange(x1_start, x1_end + x1_step, x1_step)
        plty = np.arange(x2_start, x2_end + x2_step, x2_step)
        pltX, pltY = np.meshgrid(pltx, plty)
        F = b[0] + b[1] * pltX + b[2] * pltY + b[3] * pltX * pltX + b[
            4] * pltX * pltY + b[5] * pltY * pltY
        ax1.plot_wireframe(pltX, pltY, F, color=colors[c_i])
        #ax1.contour(pltX, pltY, F, zdir='z', offset=0, cmap=cm.jet)

        c_i += 1
        if c_i > 6:
            c_i = 0

    ax1.set_title(title)
    ax1.set_xlabel(x1_name)
    ax1.set_ylabel(x2_name)
    ax1.set_zlabel('Median Euclidean Distance from Global Minimum')
    ax1.legend(to_plot, title='Max Allowed Mutation Size', loc='lower left')
    plt.show()
Beispiel #5
0
def optimize_3d(X, y, x1_step, x2_step, x3_step):
    x1_start = min(X[:, 1])
    x1_end = max(X[:, 1])
    x2_start = min(X[:, 2])
    x2_end = max(X[:, 2])
    x3_start = min(X[:, 3])
    x3_end = max(X[:, 3])

    # get regression coefficients
    b = regression_utils.get_regression_coef(X, y)

    opt = {'disp': False}
    sol = minimize(objective_3d_function, [0.5, 0.5, 0.5], method='SLSQP', \
                   bounds=((0,1), (0,1), (0,1)), options = opt)

    return sol
Beispiel #6
0
def plot_data(ax, mma_arr, func, zbounds=None):
    c_i = 0
    to_plot = mma_arr
    algorithm_name = "Genetic Algorithm"

    x1_name = 'Selection Cutoff'
    x2_name = 'Mutation Rate'
    x1_start = 0.1
    x1_step = 0.1
    x1_end = 1.0
    x2_start = 0.1
    x2_step = 0.1
    x2_end = 1.0

    func_file = func + '_function'
    func_name = func + ' function'
    func_name = func_name.title()
    title = 'Response Surface of %s vs %s of %s on %s' % (x1_name, x2_name, \
                                                          algorithm_name, func_name)

    if zbounds is not None:
        ax.set_zlim(zbounds[0], zbounds[1])

    for j in to_plot:
        f = '%s/cutoff_vs_rate_(mma_%.2f)_' % (func, j)
        filename = '../data/ga/' + f + func_file + '.dat'
        #filename = input_loc + gen_filename(x1_var, x2_var, func)
        (X, y) = read_xy_data(filename)

        b = get_regression_coef(X, y)

        pltx = np.arange(x1_start, x1_end + x1_step, x1_step)
        plty = np.arange(x2_start, x2_end + x2_step, x2_step)
        pltX, pltY = np.meshgrid(pltx, plty)
        F = b[0] + b[1] * pltX + b[2] * pltY + b[3] * pltX * pltX + b[
            4] * pltX * pltY + b[5] * pltY * pltY
        ax.plot_wireframe(pltX, pltY, F, color=colors[c_i])

        c_i += 1
        if c_i > 6:
            c_i = 0

    ax.legend(to_plot, title='Max Allowed Mutation Size', loc='lower left')
    ax.set_title(title)
    ax.set_xlabel('Selection Cutoff')
    ax.set_ylabel('Mutation Rate')
    ax.set_zlabel('Median Euclidean Distance from Global Minimum')
Beispiel #7
0
def get_pso_two_d_accuracy(o_algorithm, o_settings, o_function, \
                           x1_start, x1_step, x1_end, \
                           x2_start, x2_step, x2_end, \
                           x1_name, x2_name, \
                           population_size=50, num_tests_per_point=10, plot=True, \
                           save_histograms=True, response_surface=True, debug=False):
    if response_surface:
        plot = True

    # turn off settings that slow us down
    o_settings = optimize_settings(o_settings)

    func_name = o_function.func_globals['__name__']
    tests = {}
    hist_num_bins = 150

    o_settings['population_size'] = population_size
    num_tests_per = num_tests_per_point
    x1_srt = x1_start
    x1_e = x1_end
    x1_stp = x1_step
    x2_srt = x2_start
    x2_e = x2_end
    x2_stp = x2_step
    num_tests = int(( (int(100*x1_e)-int(100*x1_srt))/int(100*x1_stp) + 1 )* \
                    ( (int(100*x2_e)-int(100*x2_srt))/int(100*x2_stp) + 1 ))
    X = np.ones(shape=(num_tests, 6))
    y = np.zeros(shape=(num_tests, 1))

    n = 0

    if plot:
        fig = plt.figure()
        ax1 = fig.add_subplot(111, projection='3d')
        if o_function != griewank_function.objective_function:
            ax1.set_zlim(0, 1)

    for i in np.arange(x1_srt, x1_e + x1_stp, x1_stp):
        for j in np.arange(x2_srt, x2_e + x2_stp, x2_stp):
            # set settings for this test
            o_settings[x1_name] = i
            o_settings[x2_name] = j

            # initial variables
            values = []
            euclid_distance = []
            test_name = 'cp' + '(' + str(o_settings['cp']) + ')' + ',' + \
                        'cg' + '(' + str(o_settings['cg']) + ')'

            print("Running test %s on %s" % (test_name, func_name))

            # create histogram plot if true
            if save_histograms:
                hist_fig = plt.figure()
                hist_ax = hist_fig.add_subplot(111)

            # run optimization algorithm
            for k in range(0, num_tests_per):
                algorithm = o_algorithm(o_settings, o_function)
                algorithm.run()
                # save enf values
                values.append(algorithm.get_best_x().get_fval())
                # euclidean distance
                squares = 0
                for pos in algorithm.get_best_x().pos:
                    if o_function == rosenbrock_function.objective_function:
                        squares += (pos - 1)**2
                    elif o_function == easom_function.objective_function:
                        squares += (pos - np.pi)**2
                    else:
                        squares += pos**2
                euclid_distance.append(np.sqrt(squares))

            # save histogram if true
            if save_histograms:
                hist_ax.hist(euclid_distance,
                             hist_num_bins,
                             range=(0, 9),
                             normed=True)
                hist_fig.savefig(test_name + '.png')
                plt.close(hist_fig)

            # find average and save data
            #avg = sum(values)/len(values)
            #avg = median(values)
            #avg = sum(euclid_distance)/len(euclid_distance)
            avg = np.median(euclid_distance)
            tests[test_name] = avg
            if plot:
                ax1.scatter(i, j, avg)

            X[n][1] = i
            X[n][2] = j
            X[n][3] = i * i
            X[n][4] = i * j
            X[n][5] = j * j
            y[n] = avg

            # increment test number
            n += 1

#    fname = gen_filename(x1_name, x2_name, func_name)
#    write_xy_data(X, y, fname)

    if debug:
        print("\n*** DATA ***")
        print("X")
        print(X)
        print("\ny")
        print(y)
        print("\ntests")
        print(tests)

    if response_surface:
        # get regression coefficients
        b = regression_utils.get_regression_coef(X, y)

        pltx = np.arange(x1_start, x1_end + x1_step, x1_step)
        plty = np.arange(x2_start, x2_end + x2_step, x2_step)
        pltX, pltY = np.meshgrid(pltx, plty)
        F = b[0] + b[1] * pltX + b[2] * pltY + b[3] * pltX * pltX + b[
            4] * pltX * pltY + b[5] * pltY * pltY
        ax1.plot_wireframe(pltX, pltY, F)

    if plot:
        print("\nPlotting ...")
        x1_name = x1_name[0].upper() + x1_name[1:]
        x2_name = x2_name[0].upper() + x2_name[1:]
        ax1.set_xlabel(x1_name)
        ax1.set_ylabel(x2_name)
        ax1.set_zlabel('Average Euclidean Distance from Global Minimum')
        plt.show()

    return (X, y)