def sbx_int(parents, prob, eta_c, xl, xu): k = 0 if parents.shape[0] == parents.size: parents = parents.reshape(-1, 1) child = np.full((parents.shape), np.nan) for i in range(int(parents.shape[0] / 2)): child[k] = (crossover(get_crossover("int_sbx", prob=prob, eta=eta_c, prob_per_variable=1.0), parents[k].reshape(1, -1), parents[k + 1].reshape(1, -1), xl=xl, xu=xu))[0] child[k + 1] = (crossover(get_crossover("int_sbx", prob=prob, eta=eta_c, prob_per_variable=1.0), parents[k].reshape(1, -1), parents[k + 1].reshape(1, -1), xl=xl, xu=xu))[1] k = k + 2 else: child = np.full((parents.shape), np.nan) for i in range(int(parents.shape[0] / 2)): child[k] = (crossover(get_crossover("int_sbx", prob=prob, eta=eta_c, prob_per_variable=1.0), parents[k].reshape(1, -1), parents[k + 1].reshape(1, -1), xl=xl, xu=xu))[0] child[k + 1] = (crossover(get_crossover("int_sbx", prob=prob, eta=eta_c, prob_per_variable=1.0), parents[k].reshape(1, -1), parents[k + 1].reshape(1, -1), xl=xl, xu=xu))[1] k = k + 2 return child
def show(eta_cross): a, b = np.full((5000, 1), 0.2), np.full((5000, 1), 0.8) off = crossover( get_crossover("real_sbx", prob=1.0, eta=eta_cross, prob_per_variable=1.0), a, b) plt.hist(off, range=(0, 1), bins=200, density=True, color="red") plt.show()
def example_parents(n_matings, n_var): a = np.arange(n_var)[None, :].repeat(n_matings, axis=0) b = a + n_var return a, b def show(M): plt.figure(figsize=(4, 4)) plt.imshow(M, cmap='Greys', interpolation='nearest') plt.show() n_matings, n_var = 100, 100 a, b = example_parents(n_matings, n_var) print("One Point Crossover") off = crossover(get_crossover("bin_one_point"), a, b) show((off[:n_matings] != a[0])) print("Two Point Crossover") off = crossover(get_crossover("bin_two_point"), a, b) show((off[:n_matings] != a[0])) print("K Point Crossover (k=4)") off = crossover(get_crossover("bin_k_point", n_points=4), a, b) show((off[:n_matings] != a[0])) ##END POINT
import numpy as np from pymoo.interface import crossover from pymoo.operators.crossover.parent_centric_crossover import PCX from pymoo.visualization.scatter import Scatter X = np.eye(3) X[1] = [0.9, 0.1, 0.1] n_points = 1000 a = X[[0]].repeat(n_points, axis=0) b = X[[1]].repeat(n_points, axis=0) c = X[[2]].repeat(n_points, axis=0) obj = PCX(eta=0.1, zeta=0.1, impl="elementwise") _X = crossover(obj, a, c, b, xl=-1, xu=1) sc = Scatter() sc.add(_X, facecolor=None, edgecolor="blue", alpha=0.7) sc.add(X, s=100, color="red") sc.add(X.mean(axis=0), color="green", label="Centroid") sc.show()
from pymoo.interface import crossover from pymoo.factory import get_crossover import numpy as np import matplotlib.pyplot as plt def example_parents(n_matings, n_var): a = np.arange(n_var)[None, :].repeat(n_matings, axis=0) b = a + n_var return a, b def show(M): plt.figure(figsize=(4,4)) plt.imshow(M, cmap='Greys', interpolation='nearest') plt.xlabel("Variables") plt.ylabel("Individuals") plt.show() n_matings, n_var = 10, 10 a,b = example_parents(n_matings,n_var) #print(a,b) print("One Point Crossover") print(get_crossover("bin_one_point")) off = crossover(get_crossover("bin_one_point"), a, b) print(len(off)) #show((off[:n_matings] != a[0]))