def mutate(num_objectives, X_pool, X_hi, X_lo, mui, pop_size, seed): # The method "mutate" requires the following parameters: # X_pool : The population from the previous generation # X_hi : Max. Values of Xi # X_lo : Min. Values of Xi # pop_size : Population Size # seed_gen : For random number gen random.seed(seed) num_params = len(X_hi) X_mut = [] for i in range(0, pop_size, 1): xm = [] rn = random.random() rnmut = random.uniform(0, 1) if rnmut < 0.1: for j in range(0, num_params, 1): val = X_pool[i][j] + (X_hi[j] - X_lo[j]) * delta_calculator( rn, mui) if val > X_hi[j]: val = X_hi[j] elif val < X_lo[j]: val = X_lo[j] xm.append(val) x_ev = xm for k in range(0, num_objectives, 1): xm.append(evaluate(k, x_ev)) X_mut.append(xm) else: x_nil = X_pool[i] for k in range(0, num_objectives, 1): x_nil.append(0) x_nil[num_params + k] = evaluate(k, x_nil) X_mut.append(x_nil) return X_mut
def initialize(X_hi, X_lo, pop_size, num_objectives): X_init = [[0 for x in range(len(X_hi) + num_objectives)] for y in range(pop_size)] for i in range(0, pop_size, 1): for j in range(0, len(X_hi), 1): rn = random.uniform(0, 1) X_init[i][j] = round(X_lo[j] + rn * (X_hi[j] - X_lo[j]), 4) for k in range(0, num_objectives, 1): X_init[i][len(X_hi) + k] = evaluate(k, X_init[i][:len(X_hi)]) return X_init
def selection(num_obj, X_trial, X_target, pop_size, num_params): X_sel = [[0 for x in range(num_params)] for y in range(pop_size)] for i in range(0, pop_size, 1): trial_wins = 0 trial_F_val = [] target_F_val = [] for j in range(0, num_obj, 1): trial_F_val.append(evaluate(j, X_trial[i])) target_F_val.append(X_target[i][num_params + j]) if trial_F_val[j] < target_F_val[j]: trial_wins = 1 if trial_wins == 1: X_sel[i] = X_trial[i][:num_params] + trial_F_val else: X_sel[i] = X_target[i][:num_params] + target_F_val return X_sel
def genetic_operator(num_objectives, X_pool, X_hi, X_lo, cxi, mui, pop_size, num_params, seed): mutated = 0 crossed = 0 p = 1 random.seed(seed) X_daughters = [] for i in range(0, pop_size, 1): # Performing Crossover with 90% probability cxp = random.uniform(0, 1) rn = random.sample(range(0, pop_size - 1), 3) # List of 3 random numbers if cxp < 0.9: xd1 = [] xd2 = [] for j in range(0, num_params, 1): rn_beta = random.random() beta = beta_calculator(rn_beta, cxi) xd1_val = (0.5 * ((1 - beta) * X_pool[rn[0]][j] + (1 + beta) * X_pool[rn[1]][j])) if xd1_val > X_hi[j]: xd1_val = X_hi[j] elif xd1_val < X_lo[j]: xd1_val = X_lo[j] xd2_val = (0.5 * ((1 + beta) * X_pool[rn[0]][j] + (1 - beta) * X_pool[rn[1]][j])) if xd2_val > X_hi[j]: xd2_val = X_hi[j] elif xd2_val < X_lo[j]: xd2_val = X_lo[j] xd1.append(xd1_val) xd2.append(xd2_val) mup = random.uniform(0, 1) if mup < 0.1: xd1[j] = X_lo[j] + random.uniform(0, 1) * (X_hi[j] - X_lo[j]) xd2[j] = X_lo[j] + random.uniform(0, 1) * (X_hi[j] - X_lo[j]) xev1 = xd1 xev2 = xd2 for k in range(0, num_objectives, 1): xd1.append(evaluate(k, xev1)) xd2.append(evaluate(k, xev2)) crossed = 1 mutated = 0 else: XM = [] XP = X_pool[rn[2]] for j in range(0, num_params, 1): rn = random.random() val = XP[j] + delta_calculator(rn, mui) if val > X_hi[j]: val = X_hi[j] elif val < X_lo[j]: val = X_lo[j] XM.append(val) x_ev = XM for k in range(0, num_objectives, 1): XM.append(evaluate(k, x_ev)) mutated = 1 crossed = 0 if crossed: X_daughters.append(xd1) X_daughters.append(xd2) crossed = 0 elif mutated: X_daughters.append(XM) mutated = 0 return X_daughters