Example #1
0
    def __init__(self,
                 n_points=50,
                 dimension=2,
                 function_id=1,
                 max_evaluations=10000,
                 verbose=False,
                 plot=0,
                 fig_dir=None):

        print('\nACOR: Solving F%d in %dD with population size %d...\n' %
              (function_id, dimension, n_points))

        from optproblems.cec2005 import CEC2005
        from boundary import Boundary

        self.n_points = n_points
        self.dimension = dimension
        self.function_id = function_id - 1
        self.function = CEC2005(dimension)[self.function_id].objective_function
        self.max_bounds = Boundary(dimension, self.function_id).max_bounds
        self.min_bounds = Boundary(dimension, self.function_id).min_bounds

        # Termination parameters
        self.FE = 0
        self.iteration = 0
        self.max_evaluations = max_evaluations * dimension
        self.termination_error = 1e-08
        self.should_terminate = False
        self.optimal_position = CEC2005(dimension)[
            self.function_id].get_optimal_solutions()[0].phenome
        self.optimal_fitness = self.function(self.optimal_position)
        self.best_position = np.zeros_like(self.optimal_position)
        self.best_fitness = np.inf
        self.verbose = verbose
        self.plot = plot
        self.fig_config = {
            'fig_dir': fig_dir,
            'angle': 240,
            'xlim': [self.min_bounds[0], self.max_bounds[0]],
            'ylim': [self.min_bounds[1], self.max_bounds[1]],
            'optimal': self.optimal_position
        }

        self.algo = ACOR(self.obj,
                         self.dimension,
                         min_bounds=self.min_bounds,
                         max_bounds=self.max_bounds,
                         ants_num=2,
                         archive_size=self.n_points,
                         q=1e-4,
                         xi=0.85)

        if self.plot > 0:
            error = self.best_fitness - self.optimal_fitness
            fig_name = ('F%d_%d.png' % (self.function_id + 1, self.iteration))
            self.fig_config['fig_title'] = (
                'F%d, FE=%d, error=%.2e' %
                (self.function_id + 1, self.FE, error))
            draw_quiver(self.algo, self.function, fig_name, **self.fig_config)
Example #2
0
 def find_optimal_solution(self):
     dimension = self.dimension
     function_id = self.function_id
     optimal_solutions = CEC2005(
         dimension)[function_id].get_optimal_solutions()
     test_prob = Problem(CEC2005(dimension)[function_id].objective_function)
     test_prob.batch_evaluate(optimal_solutions)
     return min(optimal_solutions, key=attrgetter('objective_values'))
Example #3
0
def draw_original_contour(function_id, **kwargs):
    dim = 2
    func = CEC2005(dim)[function_id]
    population = kwargs.get('population', [])
    angle = kwargs.get('angle', 240)
    rotate = kwargs.get('rotate', False)
    fig_name = kwargs.get('fig_name', None)
    fig_title = kwargs.get('fig_title', str(func))
    cmap = cm.coolwarm
    scatter_cmap = cm.jet(np.linspace(0.1, 0.9, len(population)))
    boundary = Boundary(dim, function_id)
    step = (boundary.max_bounds[0] - boundary.min_bounds[0]) / 100
    X = np.arange(boundary.min_bounds[0], boundary.max_bounds[0] + step, step)
    Y = np.arange(boundary.min_bounds[1], boundary.max_bounds[1] + step, step)
    (X, Y) = np.meshgrid(X, Y)
    positions = (lambda .0: continue[ [
x,
y] for (x, y) in .0 ])(zip(X.ravel(), Y.ravel()))
    solutions = (lambda .0: continue[ Individual(position) for position in .0 ])(positions)
    problem = Problem(func.objective_function)
    problem.batch_evaluate(solutions)
    Z = np.array((lambda .0: continue[ solution.objective_values for solution in .0 ])(solutions))
    vmin = min(Z)
    vmax = max(Z)
    vmin = vmin - (vmax - vmin) * 0.2
    vmax = vmax + (vmax - vmin) * 0.2
    Z = Z.reshape(X.shape)
    inch_size = 4
    fig_w = 1
    fig_h = 1
    fig = plt.figure(figsize = (fig_w * inch_size, fig_h * inch_size))
    ax = fig.add_subplot(fig_h, fig_w, 1)
    ax.set_xlim([
        boundary.min_bounds[0],
        boundary.max_bounds[0]])
    ax.set_ylim([
        boundary.min_bounds[1],
        boundary.max_bounds[1]])
    cset = ax.contourf(X, Y, Z, cmap = cmap, vmin = vmin, vmax = vmax)
    fig.colorbar(cset, aspect = 20)
    colors = iter(scatter_cmap)
    color = next(colors)
    x = np.array((lambda .0: continue[ individual.phenome[0] for individual in .0 ])(population))
    y = np.array((lambda .0: continue[ individual.phenome[1] for individual in .0 ])(population))
    ax.scatter(x, y, color = color, marker = 'o', s = 10)
    for opt in func.get_optimal_solutions():
        optimal_pos = opt.phenome
        ax.scatter(optimal_pos[0], optimal_pos[1], color = 'w', marker = 'x', s = 100)
    
    fig.tight_layout()
    st = fig.suptitle(fig_title, fontsize = 16)
    st.set_y(0.95)
    fig.subplots_adjust(top = 0.85)
    if fig_name is not None:
        plt.savefig(fig_name)
    else:
        plt.show()
        input('Press Enter to continue...')
    plt.close(fig)
Example #4
0
def draw_surface3d(function_id, **kwargs):
    dim = 2
    func = CEC2005(dim)[function_id]
    clusters = kwargs.get('clusters', [])
    angle = kwargs.get('angle', 240)
    rotate = kwargs.get('rotate', False)
    fig_name = kwargs.get('fig_name', None)
    boundary = Boundary(func)
    step = (boundary.max_bounds[0] - boundary.min_bounds[0]) / 100
    X = np.arange(boundary.min_bounds[0], boundary.max_bounds[0] + step, step)
    Y = np.arange(boundary.min_bounds[1], boundary.max_bounds[1] + step, step)
    (X, Y) = np.meshgrid(X, Y)
    positions = (lambda .0: continue[ [
x,
y] for (x, y) in .0 ])(zip(X.ravel(), Y.ravel()))
    solutions = (lambda .0: continue[ Individual(position) for position in .0 ])(positions)
    problem = Problem(func.objective_function)
    problem.batch_evaluate(solutions)
    Z = np.array((lambda .0: continue[ solution.objective_values for solution in .0 ])(solutions))
    Z = Z.reshape(X.shape)
    fig = plt.figure(figsize = plt.figaspect(0.5))
    fig.suptitle(str(func))
    ax = fig.add_subplot(1, 2, 1)
    cset = ax.contourf(X, Y, Z, cmap = cm.coolwarm)
    colors = iter(cm.rainbow(np.linspace(0, 1, len(clusters))))
    for cluster in clusters:
        color = next(colors)
        x = np.array((lambda .0: continue[ individual.phenome[0] for individual in .0 ])(cluster.population))
        y = np.array((lambda .0: continue[ individual.phenome[1] for individual in .0 ])(cluster.population))
        ax.scatter(x, y, color = color, marker = 'o', s = 10)
        x_border = (lambda .0: continue[ vertice[0] for vertice in .0 ])(cluster.border)
        x_border.append(x_border[0])
        y_border = (lambda .0: continue[ vertice[1] for vertice in .0 ])(cluster.border)
        y_border.append(y_border[0])
        ax.plot(x_border, y_border, color = color)
    
    optimal_pos = func.get_optimal_solutions()[0].phenome
    ax.scatter(optimal_pos[0], optimal_pos[1], color = 'w', marker = 'x', s = 100)
    ax = fig.add_subplot(1, 2, 2, projection = '3d')
    surf = ax.plot_surface(X, Y, Z, alpha = 0.8, zorder = -1, cmap = cm.coolwarm, linewidth = 0, edgecolors = 'k', antialiased = False)
    fig.colorbar(surf, shrink = 0.5, aspect = 5)
    cset = ax.contourf(X, Y, Z, zdir = 'z', zorder = -2, offset = np.amin(Z), cmap = cm.coolwarm)
    if rotate:
        for ang in range(angle, angle + 360, 10):
            ax.view_init(30, ang)
            plt.draw()
            plt.pause(0.001)
        
    else:
        ax.view_init(30, angle)
        plt.show()
        if fig_name is not None:
            plt.savefig(fig_name)
        input('Press Enter to continue...')
Example #5
0
 def __init__(self, dim, function_id):
     if function_id == 6:
         self.min_bounds = np.array([-300] * dim)
         self.max_bounds = np.array([600] * dim)
         self.init_min_bounds = np.array([0] * dim)
         self.init_max_bounds = np.array([600] * dim)
     #elif function_id == 8:
     #    self.min_bounds = np.array([0.5, -2])
     #    self.max_bounds = np.array([2.5, 0])
     #    self.init_min_bounds = np.array([0.5, -2])
     #    self.init_max_bounds = np.array([2.5, 0])
     elif function_id == 24:
         self.min_bounds = np.array([-5] * dim)
         self.max_bounds = np.array([10] * dim)
         self.init_min_bounds = np.array([2] * dim)
         self.init_max_bounds = np.array([5] * dim)
     else:
         self.min_bounds = np.array(CEC2005(dim)[function_id].min_bounds)
         self.max_bounds = np.array(CEC2005(dim)[function_id].max_bounds)
         self.init_min_bounds = np.array(CEC2005(dim)[function_id].min_bounds)
         self.init_max_bounds = np.array(CEC2005(dim)[function_id].max_bounds)
Example #6
0
    def __init__(self, max_evaluations, n_points, dimension, function_id,
                 **kwargs):

        self.function_id = function_id
        self.n_points = n_points
        self.dimension = dimension
        self.function = CEC2005(dimension)[function_id].objective_function
        self.max_evaluations = max_evaluations * dimension
        self.max_bounds = Boundary(dimension, function_id).max_bounds
        self.min_bounds = Boundary(dimension, function_id).min_bounds
        self.optimal_position = CEC2005(
            dimension)[function_id].get_optimal_solutions()[0].phenome
        self.optimal_fitness = self.function(self.optimal_position)

        #self.problem = Problem(self.function.objective_function, max_evaluations=max_evaluations)
        self.boundary = Boundary(dimension, function_id)
        self.verbose = kwargs.get('verbose', False)

        self.population = [
        ]  #self.init_population( self.n_points, self.dimension )
        self.algo_type = kwargs.get('algo_type', 'CMA')
        self.init_algo()

        self.iteration = 0
        self.should_terminate = False
        self.optimal_solution = self.find_optimal_solution()

        self.stats = OrderedDict([
            ('iteration', []),
            ('FEs', []),
            ('error', []),
            ('best_value', []),
            #('best_position',[])
        ])

        self.run()
        self.best_solution = min(self.population,
                                 key=attrgetter('objective_values'))
Example #7
0
def draw_cluster(function_id, clusters, k, **kwargs):
    dim = 2
    func = CEC2005(dim)[function_id]
    angle = kwargs.get('angle', 240)
    fig_name = kwargs.get('fig_name', None)
    cluster = clusters[k]
    color = cm.rainbow(np.linspace(0, 1, len(clusters)))[k]
    X = np.arange(0, 1.01, 0.01)
    Y = np.arange(0, 1.01, 0.01)
    (X, Y) = np.meshgrid(X, Y)
    positions = (lambda .0: continue[ [
x,
y] for (x, y) in .0 ])(zip(X.ravel(), Y.ravel()))
    original_positions = cluster.transform_inverse(positions)
    solutions = (lambda .0: continue[ Individual(position) for position in .0 ])(original_positions)
    problem = Problem(func.objective_function)
    problem.batch_evaluate(solutions)
    Z = np.array((lambda .0: continue[ solution.objective_values for solution in .0 ])(solutions))
    Z = Z.reshape(X.shape)
    fig = plt.figure(figsize = plt.figaspect(0.5))
    fig.suptitle(str(func))
    ax = fig.add_subplot(1, 2, 1)
    cset = ax.contourf(X, Y, Z, cmap = cm.coolwarm)
    positions = np.array((lambda .0: continue[ p.phenome for p in .0 ])(cluster.population))
    transformed_positions = cluster.transform(positions)
    x = transformed_positions.T[0]
    y = transformed_positions.T[1]
    ax.scatter(x, y, color = color, marker = 'o', s = 10)
    cord = np.array([
        [
            0,
            0],
        [
            1,
            0],
        [
            1,
            1],
        [
            0,
            1]])
    ax.plot(cord[([
        0,
        1,
        2,
        3,
        0], 0)], cord[([
        0,
        1,
        2,
        3,
        0], 1)], color = color)
    ax = fig.add_subplot(1, 2, 2, projection = '3d')
    surf = ax.plot_surface(X, Y, Z, alpha = 0.8, zorder = -1, cmap = cm.coolwarm, linewidth = 0, edgecolors = 'k', antialiased = False)
    fig.colorbar(surf, shrink = 0.5, aspect = 5)
    cset = ax.contourf(X, Y, Z, zdir = 'z', zorder = -2, offset = np.amin(Z), cmap = cm.coolwarm)
    ax.view_init(30, angle)
    plt.show()
    if fig_name is not None:
        plt.savefig(fig_name)
    input('Press Enter to continue...')
Example #8
0
def draw_arms(function_id, arms, **kwargs):

    # Parameters
    dim = 2
    function = CEC2005(dim)[function_id].objective_function

    k = len(arms)
    inch_size = 4
    fig_w = k + 1
    fig_h = 1
    fig = plt.figure(figsize = (fig_w * inch_size, fig_h * inch_size))
    angle = kwargs.get('angle', 240)
    rotate = kwargs.get('rotate', False)
    fig_name = kwargs.get('fig_name', None)
    fig_title = kwargs.get('fig_title', str(func))
    cmap = cm.coolwarm
    scatter_cmap = cm.jet(np.linspace(0.1, 0.9, k))
    boundary = Boundary(dim, function_id)


    # Get Mesh Solutions for contour
    step = (boundary.max_bounds[0] - boundary.min_bounds[0]) / 100
    X = np.arange(boundary.min_bounds[0], boundary.max_bounds[0] + step, step)
    Y = np.arange(boundary.min_bounds[1], boundary.max_bounds[1] + step, step)
    (X, Y) = np.meshgrid(X, Y)
    positions = [ [x, y] for x, y in zip(X.ravel(), Y.ravel()) ]
    Z = np.array( [ function(position) for position in positions ] )

    # Reset colormap to get rid of extreme colors
    vmin = min(Z)
    vmax = max(Z)
    vmin = vmin - (vmax - vmin) * 0.2
    vmax = vmax + (vmax - vmin) * 0.2
    Z = Z.reshape(X.shape)

    # Plot contour
    ax = fig.add_subplot(fig_h, fig_w, 1)
    ax.set_xlim([ boundary.min_bounds[0], boundary.max_bounds[0]])
    ax.set_ylim([ boundary.min_bounds[1], boundary.max_bounds[1]])
    cset = ax.contourf(X, Y, Z, cmap = cmap, vmin = vmin, vmax = vmax)
    fig.colorbar(cset, aspect = 20)


    # Plot scatter points in each arm
    colors = iter(scatter_cmap)
    for arm in arms:
        color = next(colors)
        positions = arm.get_positions()
        ax.scatter(positions[:,0], positions[:,1], color = color, marker = 'o', s = 10)

        # Plot borders on original boundary
        subspace_border = np.array([ [ 0, 0], [ 1, 0], [ 1, 1], [ 0, 1], [ 0, 0]])
        border = arm.matrix.inverse_transform( subspace_border )
        ax.plot(border[:, 0], border[:, 1], color = color)
    
    # Plot optimal solution as a big white 'X'
    optimal_pos = func.get_optimal_solutions()[0].phenome
    ax.scatter(optimal_pos[0], optimal_pos[1], color = 'w', marker = 'x', s = 100)


    # Plot from each arm's perspective
    for (i, arm) in enumerate(arms):

        color = scatter_cmap[i]
        ax = fig.add_subplot(fig_h, fig_w, i + 2)
        ax.set_xlim([ -0.01, 1.01])
        ax.set_ylim([ -0.01, 1.01])

        # Plot contour
        (X, Y) = np.meshgrid( np.arange(0, 1.01, 0.01), np.arange(0, 1.01, 0.01) )

        positions = [ [x, y] for x, y in zip(X.ravel(), Y.ravel())]
        original_positions = arm.matrix.inverse_transform(positions)

        Z = np.array( [ function(position) for position in original_positions ] )
        Z = Z.reshape(X.shape)

        cset = ax.contourf(X, Y, Z, cmap = cmap, vmin = vmin, vmax = vmax)


        # Plot scatter points in each arm
        trans_X = arm.matrix.transform( arm.get_positions() )
        ax.scatter(trans_X[(:, 0)], trans_X[(:, 1)], color = color, marker = 'o', s = 10)

        # Plot border
        cord = np.array([ [0, 0], [1, 0], [1, 1], [0, 1]])
        ax.plot(cord[[0, 1, 2, 3, 0], 0], cord[[0, 1, 2, 3, 0], 1], color = color)
    



    fig.tight_layout()
    st = fig.suptitle(fig_title, fontsize = 16)
    st.set_y(0.95)
    fig.subplots_adjust(top = 0.85)
    if fig_name is not None:
        plt.savefig(fig_name)
    else:
        plt.show()
        input('Press Enter to continue...')
    plt.close(fig)
Example #9
0
def draw_all(function_id, **kwargs):
    dim = 2
    func = CEC2005(dim)[function_id]
    clusters = kwargs.get('clusters', [])
    angle = kwargs.get('angle', 240)
    rotate = kwargs.get('rotate', False)
    fig_name = kwargs.get('fig_name', None)
    cmap = cm.coolwarm
    scatter_cmap = cm.jet(np.linspace(0.1, 0.9, len(clusters)))
    boundary = Boundary(func)
    step = (boundary.max_bounds[0] - boundary.min_bounds[0]) / 100
    X = np.arange(boundary.min_bounds[0], boundary.max_bounds[0] + step, step)
    Y = np.arange(boundary.min_bounds[1], boundary.max_bounds[1] + step, step)
    (X, Y) = np.meshgrid(X, Y)
    positions = (lambda .0: continue[ [
x,
y] for (x, y) in .0 ])(zip(X.ravel(), Y.ravel()))
    solutions = (lambda .0: continue[ Individual(position) for position in .0 ])(positions)
    problem = Problem(func.objective_function)
    problem.batch_evaluate(solutions)
    Z = np.array((lambda .0: continue[ solution.objective_values for solution in .0 ])(solutions))
    Z = Z.reshape(X.shape)
    fig_w = len(clusters) + 1
    fig_h = 2
    fig = plt.figure(figsize = plt.figaspect(float(fig_h) / fig_w))
    fig.suptitle(str(func))
    ax = fig.add_subplot(fig_h, fig_w, 1, projection = '3d')
    surf = ax.plot_surface(X, Y, Z, alpha = 0.8, zorder = -1, cmap = cmap, linewidth = 0, edgecolors = 'k', antialiased = False)
    fig.colorbar(surf, shrink = 0.5, aspect = 5)
    cset = ax.contourf(X, Y, Z, zdir = 'z', zorder = -2, offset = np.amin(Z), cmap = cmap)
    ax.view_init(30, angle)
    ax = fig.add_subplot(fig_h, fig_w, fig_w + 1)
    cset = ax.contourf(X, Y, Z, cmap = cmap)
    colors = iter(scatter_cmap)
    for cluster in clusters:
        color = next(colors)
        x = np.array((lambda .0: continue[ individual.phenome[0] for individual in .0 ])(cluster.population))
        y = np.array((lambda .0: continue[ individual.phenome[1] for individual in .0 ])(cluster.population))
        ax.scatter(x, y, color = color, marker = 'o', s = 10)
        x_border = (lambda .0: continue[ vertice[0] for vertice in .0 ])(cluster.border)
        x_border.append(x_border[0])
        y_border = (lambda .0: continue[ vertice[1] for vertice in .0 ])(cluster.border)
        y_border.append(y_border[0])
        ax.plot(x_border, y_border, color = color)
    
    optimal_pos = func.get_optimal_solutions()[0].phenome
    ax.scatter(optimal_pos[0], optimal_pos[1], color = 'w', marker = 'x', s = 100)
    for k in range(len(clusters)):
        cluster = clusters[k]
        color = scatter_cmap[k]
        X = np.arange(0, 1.01, 0.01)
        Y = np.arange(0, 1.01, 0.01)
        (X, Y) = np.meshgrid(X, Y)
        positions = (lambda .0: continue[ [
x,
y] for (x, y) in .0 ])(zip(X.ravel(), Y.ravel()))
        original_positions = cluster.transform_inverse(positions)
        solutions = (lambda .0: continue[ Individual(position) for position in .0 ])(original_positions)
        problem = Problem(func.objective_function)
        problem.batch_evaluate(solutions)
        Z = np.array((lambda .0: continue[ solution.objective_values for solution in .0 ])(solutions))
        Z = Z.reshape(X.shape)
        ax = fig.add_subplot(fig_h, fig_w, k + 2, projection = '3d')
        surf = ax.plot_surface(X, Y, Z, alpha = 0.8, zorder = -1, cmap = cmap, linewidth = 0, edgecolors = 'k', antialiased = False)
        fig.colorbar(surf, shrink = 0.5, aspect = 5)
        cset = ax.contourf(X, Y, Z, zdir = 'z', zorder = -2, offset = np.amin(Z), cmap = cmap)
        ax.view_init(30, angle)
        ax = fig.add_subplot(fig_h, fig_w, fig_w + k + 2)
        cset = ax.contourf(X, Y, Z, cmap = cmap)
        positions = np.array((lambda .0: continue[ p.phenome for p in .0 ])(cluster.population))
        transformed_positions = cluster.transform(positions)
        x = transformed_positions.T[0]
        y = transformed_positions.T[1]
        ax.scatter(x, y, color = color, marker = 'o', s = 10)
        cord = np.array([
            [
                0,
                0],
            [
                1,
                0],
            [
                1,
                1],
            [
                0,
                1]])
        ax.plot(cord[([
            0,
            1,
            2,
            3,
            0], 0)], cord[([
            0,
            1,
            2,
            3,
            0], 1)], color = color)
    
    fig.tight_layout()
    plt.show()
    if fig_name is not None:
        plt.savefig(fig_name)
    input('Press Enter to continue...')
Example #10
0
    st.set_y(0.95)
    fig.subplots_adjust(top = 0.85)
    if fig_name is not None:
        plt.savefig(fig_name)
    else:
        plt.show()
        input('Press Enter to continue...')
    plt.close(fig)


if __name__ == '__main__':
    nPoints = 40
    dim = 2
    function_id = 8
    n_clusters = 3
    func = CEC2005(dim)[function_id]
    positions = np.zeros((nPoints, dim))
    boundary = Boundary(dim, function_id)
    for d in range(dim):
        positions[(:, d)] = np.random.uniform(boundary.min_bounds[d], boundary.max_bounds[d], nPoints)
    
    population = (lambda .0: continue[ Individual(position) for position in .0 ])(positions)
    problem = Problem(func.objective_function)
    problem.batch_evaluate(population)
    population = sorted(population, key = (lambda p: p.objective_values))
    population = population[:len(population) // 2]
    X = np.array((lambda .0: continue[ individual.phenome for individual in .0 ])(population))
    labels = KMeans(n_clusters = n_clusters).fit_predict(X)
    clusters = []
    for k in range(n_clusters):
        cluster_population = []
Example #11
0
def draw_optimization(function_id, cluster_positions, matrices, **kwargs):

    assert len(cluster_positions) == len(matrices)
    import os
    from optproblems.cec2005 import CEC2005
    import matplotlib.pyplot as plt
    from matplotlib import cm
    from boundary import Boundary

    # Parameters
    dim = 2
    function = CEC2005(dim)[function_id].objective_function
    optimal_pos = CEC2005(dim)[function_id].get_optimal_solutions()[0].phenome
    boundary = Boundary(dim, function_id)

    k = len(matrices)
    inch_size = 4
    #fig_w = k + 1
    fig_w = 1.2
    fig_h = 1
    fig = plt.figure(figsize=(fig_w * inch_size, fig_h * inch_size))
    cmap = cm.coolwarm
    scatter_cmap = cm.jet(np.linspace(0.1, 0.9, k))
    angle = kwargs.get('angle', 240)
    rotate = kwargs.get('rotate', False)
    fig_name = kwargs.get('fig_name', None)
    fig_title = kwargs.get('fig_title', 'F%d' % (function_id + 1))
    fig_dir = kwargs.get('fig_dir', 'test_arms')
    if not os.path.exists(fig_dir):
        os.makedirs(fig_dir)

    print('ploting %s...' % fig_name)

    # Get Mesh Solutions for contour
    step = (boundary.max_bounds[0] - boundary.min_bounds[0]) / 100
    X = np.arange(boundary.min_bounds[0], boundary.max_bounds[0] + step, step)
    Y = np.arange(boundary.min_bounds[1], boundary.max_bounds[1] + step, step)
    (X, Y) = np.meshgrid(X, Y)
    positions = [[x, y] for x, y in zip(X.ravel(), Y.ravel())]
    Z = np.array([function(position) for position in positions])

    # Reset colormap to get rid of extreme colors
    vmin = min(Z)
    vmax = max(Z)
    vmin = vmin - (vmax - vmin) * 0.2
    vmax = vmax + (vmax - vmin) * 0.2
    Z = Z.reshape(X.shape)

    # Plot contour
    ax = fig.add_subplot(fig_h, fig_w, 1)
    #margin = (boundary.max_bounds - boundary.min_bounds)*0.1
    margin = (boundary.max_bounds - boundary.min_bounds) * 0.0
    ax.set_xlim([
        boundary.min_bounds[0] - margin[0], boundary.max_bounds[0] + margin[0]
    ])
    ax.set_ylim([
        boundary.min_bounds[1] - margin[1], boundary.max_bounds[1] + margin[1]
    ])
    cset = ax.contourf(X, Y, Z, cmap=cmap, vmin=vmin, vmax=vmax)
    fig.colorbar(cset, aspect=20)

    # Plot scatter points in each arm
    colors = iter(scatter_cmap)
    for positions, matrix in zip(cluster_positions, matrices):
        color = next(colors)
        ax.scatter(positions[:, 0],
                   positions[:, 1],
                   color=color,
                   marker='o',
                   s=10)

        # Plot borders on original boundary
        subspace_border = np.array([[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]])
        border = matrix.inverse_transform(subspace_border)
        ax.plot(border[:, 0], border[:, 1], color=color)

    # Plot optimal solution as a big white 'X'
    ax.scatter(optimal_pos[0], optimal_pos[1], color='w', marker='x', s=100)
    '''
    # Plot from each arm's perspective
    for i, (positions, matrix) in enumerate(zip(cluster_positions, matrices)):
        color = scatter_cmap[i]
        ax = fig.add_subplot(fig_h, fig_w, i + 2)
        ax.set_xlim([ -0.01, 1.01])
        ax.set_ylim([ -0.01, 1.01])
        # Plot contour
        (X, Y) = np.meshgrid( np.arange(0, 1.01, 0.01), np.arange(0, 1.01, 0.01) )
        mesh_positions = [ [x, y] for x, y in zip(X.ravel(), Y.ravel())]
        original_positions = matrix.inverse_transform(mesh_positions)
        Z = np.array( [ function(mesh_position) for mesh_position in original_positions ] )
        Z = Z.reshape(X.shape)
        cset = ax.contourf(X, Y, Z, cmap = cmap, vmin = vmin, vmax = vmax)
        # Plot scatter points in each arm
        trans_X = matrix.transform( positions )
        ax.scatter(trans_X[:, 0], trans_X[:, 1], color = color, marker = 'o', s = 10)
        # Plot border
        cord = np.array([ [0, 0], [1, 0], [1, 1], [0, 1]])
        ax.plot(cord[[0, 1, 2, 3, 0], 0], cord[[0, 1, 2, 3, 0], 1], color = color)
    '''

    fig.tight_layout()
    st = fig.suptitle(fig_title, fontsize=16)
    st.set_y(0.95)
    fig.subplots_adjust(top=0.85)
    if fig_name is not None:
        plt.savefig('%s/%s' % (fig_dir, fig_name))
    else:
        plt.show()
        input('Press Enter to continue...')
    plt.close(fig)
Example #12
0
def draw_arms(function_id, arms, **kwargs):

    import os
    from optproblems.cec2005 import CEC2005
    import matplotlib.pyplot as plt
    from matplotlib import cm
    from boundary import Boundary

    # Parameters
    dim = 2
    function = CEC2005(dim)[function_id].objective_function
    optimal_pos = CEC2005(dim)[function_id].get_optimal_solutions()[0].phenome
    boundary = Boundary(dim, function_id)
    plot_subspace = True

    inch_size = 4
    k = len(arms)
    fig_w = (k + 1.2) if plot_subspace else 1.2
    fig_h = 1
    fig = plt.figure(figsize=(fig_w * inch_size, fig_h * inch_size))
    cmap = cm.coolwarm
    scatter_cmap = cm.jet(np.linspace(0.1, 0.9, k))
    angle = kwargs.get('angle', 240)
    rotate = kwargs.get('rotate', False)
    fig_name = kwargs.get('fig_name', None)
    fig_title = kwargs.get('fig_title', 'F%d' % (function_id + 1))
    fig_dir = kwargs.get('fig_dir', 'test_arms')
    if not os.path.exists(fig_dir):
        os.makedirs(fig_dir)

    print('ploting %s...' % fig_name)

    # Plot contour
    ax = fig.add_subplot(fig_h, fig_w, 1)
    #ax.set_facecolor('black')

    # Get Mesh Solutions for contour
    step = (boundary.max_bounds[0] - boundary.min_bounds[0]) / 100
    X = np.arange(boundary.min_bounds[0], boundary.max_bounds[0] + step, step)
    Y = np.arange(boundary.min_bounds[1], boundary.max_bounds[1] + step, step)
    (X, Y) = np.meshgrid(X, Y)
    positions = [[x, y] for x, y in zip(X.ravel(), Y.ravel())]
    Z = np.array([function(position) for position in positions])

    # Reset colormap to get rid of extreme colors
    v_range = max(Z) - min(Z)
    vmin = min(Z) - v_range * 0.2
    vmax = max(Z) + v_range * 0.2
    Z = Z.reshape(X.shape)

    #margin = (boundary.max_bounds - boundary.min_bounds)*0.1
    margin = (boundary.max_bounds - boundary.min_bounds) * 0.0
    ax.set_xlim([
        boundary.min_bounds[0] - margin[0], boundary.max_bounds[0] + margin[0]
    ])
    ax.set_ylim([
        boundary.min_bounds[1] - margin[1], boundary.max_bounds[1] + margin[1]
    ])
    cset = ax.contourf(X, Y, Z, cmap=cmap, vmin=vmin, vmax=vmax)
    fig.colorbar(cset, aspect=20)

    # Plot optimal solution as a big white 'X'
    ax.scatter(optimal_pos[0], optimal_pos[1], color='w', marker='x', s=100)

    # Plot scatter points in each arm
    colors = iter(scatter_cmap)
    for arm in arms:
        arm.draw(ax, next(colors), on_subspace=False, draw_algo=False)

    if plot_subspace:
        # Plot from each arm's perspective
        for i, arm in enumerate(arms):

            color = scatter_cmap[i]
            ax_sub = fig.add_subplot(fig_h, fig_w, i + 2)
            #ax_sub.set_facecolor('black')
            ax_sub.set_xlim([-0.01, 1.01])
            ax_sub.set_ylim([-0.01, 1.01])

            # Plot contour
            (X, Y) = np.meshgrid(np.arange(0, 1.01, 0.01),
                                 np.arange(0, 1.01, 0.01))

            mesh_positions = [[x, y] for x, y in zip(X.ravel(), Y.ravel())]
            original_positions = arm.matrix.inverse_transform(mesh_positions)

            Z = np.array([
                function(mesh_position) for mesh_position in original_positions
            ])
            Z = Z.reshape(X.shape)

            #cset = ax_sub.contourf(X, Y, Z, cmap = cmap, vmin = vmin, vmax = vmax)
            cset = ax_sub.contour(X, Y, Z, cmap=cmap)

            # Plot scatter points in each arm
            arm.draw(ax_sub, color, on_subspace=True)

    fig.tight_layout()
    st = fig.suptitle(fig_title, fontsize=16)
    st.set_y(0.95)
    fig.subplots_adjust(top=0.85)
    if fig_name is not None:
        plt.savefig('%s/%s' % (fig_dir, fig_name))
    else:
        plt.show()
        input('Press Enter to continue...')
    plt.close(fig)
Example #13
0
def run(function_id, fig_dir):
    from boundary import Boundary
    dimension = 2
    n_points = 200
    function_id = function_id - 1
    obj = CEC2005(dimension)[function_id].objective_function
    min_bounds = Boundary(dimension, function_id).min_bounds
    max_bounds = Boundary(dimension, function_id).max_bounds
    '''
    positions = np.array([
                          [0.75, -1.4],
                          [0.90, -1.4],
                          [1.05, -1.4],
                          [1.75, -1.4],
                          [1.90, -1.4],
                          [2.05, -1.4],
                          [0.75, -1.6],
                          [0.90, -1.6],
                          [1.05, -1.6],
                          [1.75, -1.6],
                          [1.90, -1.6],
                          [2.05, -1.6],
                        ])
    '''
    positions = np.random.uniform(min_bounds,
                                  max_bounds,
                                  size=(n_points, dimension))
    fitnesses = np.array([obj(p) for p in positions])

    indices = fitnesses.argsort()
    selected = indices[:int(len(positions) / 2)]
    positions, fitnesses = positions[selected], fitnesses[selected]
    #mean, cov = weighted_gaussian( positions, fitnesses )

    labels = hierarchical_clustering(positions, fitnesses)
    k = max(labels) + 1

    clusters_positions, clusters_fitnesses = [], []
    for i in range(k):
        indices = np.where(labels == i)[0]
        clusters_positions.append(positions[indices])
        clusters_fitnesses.append(fitnesses[indices])

    fig_name = 'F%d_init.png' % (function_id + 1)
    print('K = %d, drawing' % k, fig_name)

    draw(clusters_positions,
         clusters_fitnesses,
         obj,
         fig_name=fig_name,
         fig_dir=fig_dir,
         xlim=[min_bounds[0], max_bounds[0]],
         ylim=[min_bounds[1], max_bounds[1]])

    labels = trim_by_MDL(positions, fitnesses, labels)
    #labels = clustering( positions, fitnesses )
    k = max(labels) + 1

    clusters_positions, clusters_fitnesses = [], []
    for i in range(k):
        #print(i)
        indices = np.where(labels == i)[0]
        clusters_positions.append(positions[indices])
        clusters_fitnesses.append(fitnesses[indices])

        #mean, cov = weighted_gaussian( positions[indices], fitnesses[indices] )
        #distances = [ manhalanobis_distance(x, mean, cov) for x in positions[indices] ]
        #idx = np.argsort(distances)
        #for i in idx:
        #    print( positions[indices][i], fitnesses[indices][i], distances[i] )

        #h = -fitnesses[indices][idx] - min(-fitnesses[indices])
        #h = h/sum(h)
        #print(h)
        #n, bins, patches = plt.hist(h, len(h)

        #print()

    fig_name = 'F%d_MDL_GMM.png' % (function_id + 1)
    print('K = %d, drawing' % k, fig_name)

    draw(clusters_positions,
         clusters_fitnesses,
         obj,
         fig_name=fig_name,
         fig_dir=fig_dir,
         xlim=[min_bounds[0], max_bounds[0]],
         ylim=[min_bounds[1], max_bounds[1]])
Example #14
0
    def __init__(self,
                 n_points=None,
                 dimension=2,
                 function_id=0,
                 use_bandit=False,
                 algo_type='CMA',
                 max_evaluations=1e4,
                 verbose=False,
                 plot=0,
                 fig_dir=None,
                 csv_file=None):

        if use_bandit:
            algo_description = 'Bandit + %s' % algo_type
        else:
            algo_description = algo_type

        if csv_file:
            self.csv_file = open(csv_file, 'w')
            self.csv_file.write(
                'iteration,FEs,error,best_fitness,best_position\n')

        if n_points is None:
            if algo_type == 'PSO':
                self.n_points = 40
            elif algo_type == 'ACOR':
                self.n_points = 50
            else:
                self.n_points = 30
        else:
            self.n_points = n_points

        print('\n%s: Solving F%d in %dD with population size %d...\n' %
              (algo_description, function_id + 1, dimension, self.n_points))

        # Parameters for optproblems
        self.dimension = dimension
        self.function_id = function_id
        self.function = CEC2005(dimension)[function_id].objective_function
        self.max_bounds = Boundary(dimension, function_id).max_bounds
        self.min_bounds = Boundary(dimension, function_id).min_bounds
        self.init_max_bounds = Boundary(dimension, function_id).init_max_bounds
        self.init_min_bounds = Boundary(dimension, function_id).init_min_bounds

        # Parameters for termination
        self.FE = 0
        self.iteration = 0
        self.max_evaluations = max_evaluations * dimension
        self.termination_error = 1e-8
        self.should_terminate = False

        # Parameters for logging
        self.optimal_position = CEC2005(
            dimension)[function_id].get_optimal_solutions()[0].phenome
        self.optimal_fitness = self.function(self.optimal_position)
        self.best_position = np.zeros_like(self.optimal_position)
        self.best_fitness = np.inf
        self.verbose = verbose
        self.plot = plot
        self.stats = OrderedDict([('iteration', []), ('FEs', []),
                                  ('error', []), ('best_fitness', []),
                                  ('best_position', [])])

        if use_bandit == True:
            self.algo = Bandit(
                self.obj,
                self.n_points,
                self.dimension,
                algo_type=algo_type,
                min_bounds=self.min_bounds,
                max_bounds=self.max_bounds,
                init_max_bounds=self.init_max_bounds,
                init_min_bounds=self.init_min_bounds,
                verbose=verbose,
                max_evaluations=self.max_evaluations,
                max_arms_num=10,
            )

        elif algo_type == 'PSO':
            self.algo = PSO(self.obj,
                            self.n_points,
                            self.dimension,
                            min_bounds=self.min_bounds,
                            max_bounds=self.max_bounds)
        elif algo_type == 'ACOR':
            self.algo = ACOR(
                self.obj,
                self.dimension,
                min_bounds=self.min_bounds,
                max_bounds=self.max_bounds,
                ants_num=2,
                archive_size=self.n_points,  # 50
                q=1e-4,  #1e-4, 0.1, 0.3, 0.5, 0.9
                xi=0.85)
        else:
            self.algo = CMA(self.obj,
                            self.n_points,
                            self.dimension,
                            min_bounds=self.min_bounds,
                            max_bounds=self.max_bounds)
Example #15
0
def testArm(plot=False):

    from optproblems.cec2005 import CEC2005
    from sklearn.cluster import KMeans
    from boundary import Boundary

    function_id = 6  # 0 ~ 24
    algo_type = 'ACOR'
    dimension = 2
    n_points = 50
    init_num_points = 100 * dimension
    n_sample = 100 * dimension
    k = 1
    function = CEC2005(dimension)[function_id].objective_function
    init_min_bounds = Boundary(dimension, function_id).init_min_bounds
    init_max_bounds = Boundary(dimension, function_id).init_max_bounds
    min_bounds = Boundary(dimension, function_id).min_bounds
    max_bounds = Boundary(dimension, function_id).max_bounds

    init_positions = np.random.uniform(init_min_bounds[0],
                                       init_max_bounds[0],
                                       size=(init_num_points, dimension))
    init_fitnesses = np.array([function(p) for p in init_positions])

    index = init_fitnesses.argsort()
    selected = index[:int(init_num_points / 2)]
    positions, fitnesses = init_positions[selected], init_fitnesses[selected]
    labels = KMeans(n_clusters=k).fit_predict(positions)

    it = 0
    should_terminate = False

    arms = []
    for i in range(k):
        indices = np.where(labels == i)[0]
        arm = Arm(function,
                  n_points,
                  positions[indices],
                  fitnesses[indices],
                  algo_type=algo_type,
                  exclude=[],
                  matrix=Matrix(positions[indices], fitnesses[indices],
                                min_bounds, max_bounds),
                  min_bounds=min_bounds,
                  max_bounds=max_bounds)
        arms.append(arm)

    if plot: draw_arms(function_id, arms, fig_name='initialize.png')

    trans_samples = np.random.uniform(0, 1, size=(n_sample, dimension))
    for i in range(k):

        indices = np.where(labels == i)[0]
        argmax = np.argmax(fitnesses[indices])
        best = positions[indices][argmax]

        opt_points = []
        exclude = []
        for j in range(k):
            if i != j:
                samples = arms[j].matrix.inverse_transform(trans_samples)
                exclude.extend(samples)
                opt_points.append(samples)
            else:
                opt_points.append(positions[indices])

        exclude = np.array(exclude)

        print('optimizing matrix of arm %d ...' % i)
        arms[i].matrix.optimize(positions[indices], fitnesses[indices],
                                exclude)

        if plot:
            draw_optimization(function_id,
                              opt_points, [arm.matrix for arm in arms],
                              fig_name='optimize_%d.png' % i)

    if plot: draw_arms(function_id, arms, fig_name='optimized.png')

    new_matrix = deepcopy(arms[0].matrix)
    translate = np.array([[1, 0, -0.5], [0, 1, 0.5], [0, 0, 1]])
    new_matrix.matrix = np.dot(new_matrix.matrix, translate)
    arms[0].transform(new_matrix)
    if plot: draw_arms(function_id, arms, fig_name='transformed.png')
    '''
Example #16
0

    plt.savefig('%s/%s' % (fig_dir, fig_name))
    plt.close(fig)



def manhalanobis_distance( x, mean, cov ):
    assert len(x) == len(mean) == len(cov) == len(cov[0])
    xm = x - mean
    inverse_cov = np.linalg.inv(cov)
    return xm.dot(inverse_cov).dot(xm.T)

function_id = 8
dimension = 2
obj = CEC2005(dimension)[function_id].objective_function
xlim = [1.5 ,3.25]
ylim = [-2, -1]
positions = np.random.uniform([xlim[0], ylim[0]], [xlim[1], ylim[1]], size=(20,2))
fitnesses = np.array([ obj(x) for x in positions ])
draw( obj, 'test.png', xlim=xlim, ylim=ylim, scatter = positions )
from cluster import weighted_gaussian
mean, cov = weighted_gaussian( positions, fitnesses )
from scipy.stats import multivariate_normal
rv = multivariate_normal(mean, cov)
from cluster import clustering
labels = clustering(positions, fitnesses)
print('mean:', mean)
print('cov:\n', cov)
print(labels)
Example #17
0
    def __init__(self,
                 n_points=12,
                 dimension=2,
                 function_id=1,
                 algo_type='CMA',
                 max_evaluations=10000,
                 verbose=False,
                 plot=0,
                 fig_dir=None):

        print(
            '\nBandit + %s: Solving F%d in %dD with population size %d...\n' %
            (algo_type, function_id, dimension, n_points))

        from optproblems.cec2005 import CEC2005
        from boundary import Boundary

        # Bandit parameters
        self.n_points = n_points
        self.dimension = dimension
        self.function_id = function_id - 1
        self.function = CEC2005(dimension)[self.function_id].objective_function
        max_bounds = Boundary(dimension, self.function_id).max_bounds
        min_bounds = Boundary(dimension, self.function_id).min_bounds
        init_max_bounds = Boundary(dimension, self.function_id).init_max_bounds
        init_min_bounds = Boundary(dimension, self.function_id).init_min_bounds

        # Termination parameters
        self.FE = 0
        self.iteration = 0
        self.max_evaluations = max_evaluations * dimension
        self.termination_error = 1e-08
        self.should_terminate = False
        self.optimal_position = CEC2005(dimension)[
            self.function_id].get_optimal_solutions()[0].phenome
        self.optimal_fitness = self.function(self.optimal_position)
        self.best_position = np.zeros_like(self.optimal_position)
        self.best_fitness = np.inf
        self.verbose = verbose
        self.plot = plot
        self.fig_config = {
            'fig_dir': fig_dir,
            'angle': 240,
            'xlim': [min_bounds[0], max_bounds[0]],
            'ylim': [min_bounds[1], max_bounds[1]],
            'optimal': self.optimal_position
        }

        self.algo = Bandit(self.obj,
                           self.n_points,
                           self.dimension,
                           algo_type=algo_type,
                           min_bounds=min_bounds,
                           max_bounds=max_bounds,
                           init_max_bounds=init_max_bounds,
                           init_min_bounds=init_min_bounds,
                           verbose=verbose,
                           max_evaluations=self.max_evaluations,
                           max_arms_num=10,
                           plot=plot,
                           fig_config=self.fig_config)

        if self.plot > 0:
            error = self.best_fitness - self.optimal_fitness
            self.fig_config['fig_title'] = (
                'F%d, FE=%d, error=%.2e' %
                (self.function_id + 1, self.FE, error))
            draw_arms(self.function_id,
                      self.algo.arms,
                      fig_name='it%d.png' % self.iteration,
                      **self.fig_config)