Example #1
0
import matplotlib.pyplot as plt
import numpy as np

from util_plot import AddPlot

is_3d = True
plots = AddPlot(is_3d)
ax, data_dim = plots.returns

# Make goal as gene
gene_num = 5
chromosome_num = 10
p_arr = np.random.rand(chromosome_num, gene_num)
v_arr = np.random.rand(chromosome_num, gene_num)
pbest_arr = np.zeros((chromosome_num, gene_num))
pbest_fit_arr = np.zeros(chromosome_num)
gbest = None
gbest_path = None
gbest_fitness = 0.0

# Create points and paths
points = np.random.rand(1 + gene_num, data_dim)
paths = np.repeat([points], chromosome_num, axis=0)

# Parameters
inertia_weight = 0.7298
const_vp = 1.5
const_vg = 1.5
early_stop_fitness = 1.0

i = 0
Example #2
0
import numpy as np
import matplotlib.pyplot as plt
from util_plot import AddPlot

is_3d = True
ax, gene_num = AddPlot(is_3d).returns

chromosome_num = 10
population_shape = (chromosome_num, gene_num)

test_goal = np.random.rand(gene_num)

iteration = 0
iteration_num = 100

# Parameters
mutation_rate = 0.3
crossover_rate = 0.3
selection_ratio = 0.3

selection_num = int(chromosome_num * selection_ratio)
copy_num = chromosome_num - selection_num

best_goal = None
best_fitness = 0.0
early_stop_fitness = 0.98

# First time we generate population using Uniform Distribution
population = np.random.rand(*population_shape)

while True:
Example #3
0
import matplotlib.pyplot as plt
import numpy as np

from util_plot import AddPlot

is_3d = True
ax, data_dim = AddPlot(is_3d).returns

# Make goal as gene
gene_num = 5
chromosome_num = 10

# Create points and paths
points = np.random.rand(1 + gene_num, data_dim)
paths = np.repeat([points], chromosome_num, axis=0)

population = np.repeat([np.arange(gene_num)], chromosome_num, axis=0)
list(map(np.random.shuffle, population))

# Parameters
mutation_rate = 0.3
selection_ratio = 0.7
selection_num = int(chromosome_num * selection_ratio)
copy_num = chromosome_num - selection_num

best_fitness = 0.0
early_stop_fitness = 1.0

iteration = 0
iter_num = 100
while True:
Example #4
0
import matplotlib.pyplot as plt
import numpy as np
from sklearn import datasets
from util_plot import AddPlot

is_3d = True
ax, point_dim = AddPlot(is_3d).returns

# import some data to play with
iris = datasets.load_iris()
X = iris.data[:, :point_dim]  # we only take the first point_dim features.
y = iris.target

if is_3d:
    test_point = np.array([7, 4, 3])
else:
    test_point = np.array([7, 4])

# Set number of neighbors and (x, y) of test point.
neighbors_K = 3

distant_arr = np.zeros(len(y))

for i, (point, color_idx) in enumerate(zip(X, y)):
    # Plot all points
    ax.scatter(*point, color="C%d" % color_idx, s=50, alpha=0.1)
    distant_arr[i] = np.linalg.norm(test_point - point)

# Get neighbor points from sorted distance
min_idx = np.argsort(distant_arr)[:neighbors_K]
neighbor_points = X[min_idx]
Example #5
0
import numpy as np
import matplotlib.pyplot as plt
from util_plot import AddPlot

is_3d = True
ax, particle_dim = AddPlot(is_3d).returns

n_particles = 10
particles_shape = (n_particles, particle_dim)

test_goal = np.random.rand(particle_dim)

iteration = 0

best_goal = None
best_distant = None

while True:
    distant_list = []
    if best_goal is None:
        # First time we generate particles using Uniform Distribution
        rand_particles = np.random.rand(*particles_shape)
    else:
        # Randomly generate particles using Normal Distribution
        sigma = 1 / (4 * iteration)
        rand_particles = best_goal + sigma * np.random.randn(*particles_shape)

    ax.scatter(*rand_particles.T, s=50, alpha=0.5)
    ax.scatter(*test_goal, s=200, marker="*", alpha=1.0)

    for p in rand_particles:
Example #6
0
import numpy as np
import matplotlib.pyplot as plt
from util_plot import AddPlot

is_3d = True
ax, point_dim = AddPlot(is_3d).returns

n_cluster_points = 100
cluster_shape = (n_cluster_points, point_dim)

# Randomly generate clusters using Normal Distribution (randn)
rand_points1 = 0 + 2 * np.random.randn(*cluster_shape)
rand_points2 = 10 + 3 * np.random.randn(*cluster_shape)
if is_3d:
    rand_points3 = [20, 0, 5] + 2 * np.random.randn(*cluster_shape)
    rand_points4 = [20, 20, 10] + 1.5 * np.random.randn(*cluster_shape)
else:
    rand_points3 = [20, 0] + 2 * np.random.randn(*cluster_shape)
    rand_points4 = [20, 20] + 1.5 * np.random.randn(*cluster_shape)

all_points = (rand_points1, rand_points2, rand_points3, rand_points4)
points_num = n_cluster_points * len(all_points)
all_points = np.concatenate(all_points, axis=0)


def neighborhood_points(xs, x_centroid, dist=3):
    eligible_x = []
    for x in xs:
        distance = np.linalg.norm(x - x_centroid)
        if distance <= dist:
            eligible_x.append(x)
Example #7
0
import matplotlib.pyplot as plt
import numpy as np
from util_plot import AddPlot

p = AddPlot(is_3d=True, with_lc=True)
ax1, _ = p.returns

x_min = -4
x_max = 4
y_min = -4
y_max = 4
z_min = 0
z_max = 13

# Add step to make more smooth and more slow
x = np.arange(x_min, x_max, 0.5)
y = np.arange(y_min, y_max, 0.5)
xx, yy = np.meshgrid(x, y, sparse=True)


def reversed_ackley_function(x, y):
    z = 15 - (-20 * np.exp(-0.2 * (np.sqrt(0.5 * (x**2 + y**2)))) - np.exp(
        0.5 * (np.cos(2 * np.pi * x) + np.cos(2 * np.pi * y))) + np.e + 20)
    return z


zz = reversed_ackley_function(xx, yy)


def plot_ackley():
    ax1.set_xlim(x_min, x_max)
Example #8
0
import time

import numpy as np
import matplotlib.pyplot as plt
from util_plot import AddPlot

is_3d = True
ax, point_dim = AddPlot(is_3d).returns

n_cluster_points = 100
cluster_shape = (n_cluster_points, point_dim)
means_K = 4
early_stop_distant = 0.01

# Randomly generate clusters using Normal Distribution (randn)
rand_points1 = 0 + 2 * np.random.randn(*cluster_shape)
rand_points2 = 10 + 3 * np.random.randn(*cluster_shape)
if is_3d:
    rand_points3 = [20, 0, 5] + 2 * np.random.randn(*cluster_shape)
    rand_points4 = [30, 20, 10] + 1.5 * np.random.randn(*cluster_shape)
else:
    rand_points3 = [20, 0] + 2 * np.random.randn(*cluster_shape)
    rand_points4 = [30, 20] + 1.5 * np.random.randn(*cluster_shape)
points_num = n_cluster_points * means_K
all_points = np.concatenate(
    (rand_points1, rand_points2, rand_points3, rand_points4))

# We random choice centroids from points.
rand_indexes = np.random.choice(all_points.shape[0], means_K, replace=False)
centroids = all_points[rand_indexes]