def SMSEMOA(N, maxgen, problem, encoding): D = problem.D M = problem.M lower = problem.lower upper = problem.upper pc = 1 pm = 1 / D pop = initial(N, D, M, lower, upper, problem, encoding) gen = 1 start = time.time() fig = plt.figure() while gen <= maxgen: for i in range(N): k = np.random.randint(0, len(pop)) l = np.random.randint(0, len(pop)) # keep different while k == l: l = np.random.randint(0, len(pop)) pop_parent = [pop[k], pop[l]] pop_offspring = crossMutation(pop_parent, D, lower, upper, pc, pm, problem) if checkDominance(pop_offspring[0], pop_offspring[1]): q = pop_offspring[0] else: q = pop_offspring[1] pop_combine = np.hstack([pop, q]) pop_combine = list(pop_combine) pop = Reduce(pop_combine, M) gen += 1 start1 = time.time() draw(problem, pop, M, fig) if gen < maxgen: plt.clf() end1 = time.time() print("3:%7fs" % (end1 - start1)) if (gen % 10) == 0: print("%d gen has completed!\n" % gen) plt.ioff() end = time.time() print("runtime:%2fs" % (end - start)) #plt.show() # PRINT INDICATOR score = HV(pop) print("HV indicator:%f" % score) plt.show()
def IBEA(N, maxgen, problem, encoding): start = time.time() D = problem.D M = problem.M lower = problem.lower upper = problem.upper pc = 1 pm = 1 / D kappa = 0.05 pop = initial(N, D, M, lower, upper, problem, encoding) gen = 1 plt.ion() fig = plt.figure() while gen <= maxgen: Fitness, fitness, pop = Calfitness(pop, kappa, len(pop)) pop_parent1 = TournamentSelection(pop, N) pop_parent2 = TournamentSelection(pop, N) pop_parent = pop_parent1 + pop_parent2 length = len(pop_parent) pop_offspring = crossMutation(pop_parent, D, lower, upper, pc, pm, problem, 1, 2) pop_mixed = pop + pop_offspring Fitness, fitness, pop = Calfitness(pop_mixed, kappa, len(pop_mixed)) pop = EnviromentSelection(pop_mixed, fitness, length) draw(problem, pop, M, fig) if gen < maxgen: plt.clf() if gen % 10 == 0: print("%d gen has completed" % gen) gen += 1 end = time.time() plt.ioff() print("runtime: %.2fs" % (end - start)) # PRINT INDICATOR temp = [] for i in range(N): temp.append(pop[i].obj) popObj = np.vstack(temp) score = HV(popObj) print("HV indicator:%f" % score) plt.show()
def NSGAII(N, maxgen, problem, encoding): D = problem.D M = problem.M lower = problem.lower upper = problem.upper pc = 0.9 pm = 1 / D pop = initial(N, D, M, lower, upper, problem, encoding) F1, pop_non, _ = nonDominatedSort(N, pop) pop = crowdingDistance(F1, pop_non, M) start = time.time() gen = 1 fig = plt.figure() plt.ion() while gen <= maxgen: pop_parent1 = tournamentSelection(pop, N) pop_parent2 = tournamentSelection(pop, N) pop_parent = pop_parent1 + pop_parent2 #print(np.size(pop_parent)) pop_offspring = crossMutation(pop_parent, D, lower, upper, pc, pm, problem) pop_combine = pop + pop_offspring F, pop_combine_non, _ = nonDominatedSort(len(pop_combine), pop_combine) pop_combine2 = crowdingDistance(F, pop_combine_non, M) pop = elitism(N, pop_combine2) draw(problem, pop, M, fig) if gen < maxgen: plt.clf() if (gen % 10) == 0: print("%d gen has completed!\n" % gen) gen = gen + 1 end = time.time() plt.ioff() print("runtime:%2fs" % (end - start)) #PRINT INDICATOR score = HV(pop) print("HV indicator:%f" % score) plt.show()
def SPEA2(N, maxgen, problem, encoding): start = time.time() D = problem.D M = problem.M lower = problem.lower upper = problem.upper pc = 1 pm = 1 / D k = round(np.sqrt(2 * N)) pop = initial(N, D, M, lower, upper, problem, encoding) pop_ex = [] fig = plt.figure() plt.ion() gen = 1 while True: pop_combine = pop + pop_ex pop_combine = Calfitness(pop_combine, k, M) pop_ex = EnvironmentalSelection(pop_combine, N, M) draw(problem, pop_ex, M, fig) if gen < maxgen: plt.clf() if gen > maxgen: break pop_mating = MatingSelection(pop_ex, N) #print(len(pop_mating)) pop = crossMutation(pop_mating, D, lower, upper, pc, pm, problem) #print("!!%d"%len(pop)) gen += 1 if gen % 10 == 0: print("%d gen has completed" % gen) end = time.time() plt.ioff() print("runtime: %.2fs" % (end - start)) plt.show()
def MOEAD(N,maxgen,problem,encoding,type = 1): start = time.time() D = problem.D M = problem.M lower = problem.lower upper = problem.upper pc = 1 pm = 1 / D W, N = UniformPoint(N, M) T = np.ceil(N/10) T = int(T) B = FindNeighbour(W, N, M, T) #print(W[99]) pop = initial(N, D, M, lower, upper, problem, encoding) z = Best(pop) gen = 1 plt.ion() fig = plt.figure() while gen<=maxgen: for i in range(N): k = np.random.randint(0,T) l = np.random.randint(0,T) while k==l: l = np.random.randint(0,T) pop_parent = [pop[B[i][k]],pop[B[i][l]]] pop_offspring = crossMutation(pop_parent, D, lower, upper, pc, pm, problem) if checkDominance(pop_offspring[0],pop_offspring[1]): y = pop_offspring[0] else: y = pop_offspring[1] for j in range(len(z)): if y.obj[j]<z[j]: z[j] = y.obj[j] #小心这使用的权重,之前搞错了 for j in range(T): if Tchebycheff(y,W[B[i][j]],z) < Tchebycheff(pop[B[i][j]],W[B[i][j]],z): pop[B[i][j]] = y draw(problem,pop, M, fig) # print(Tchebycheff(pop[99],W[99],z)) # print(pop[99].obj[0],pop[99].obj[1]) if gen < maxgen: plt.clf() if (gen % 10) == 0: print("%d gen has completed!\n" % gen) gen = gen + 1; end = time.time() plt.ioff() print("runtime:%2fs" % (end - start)) # for i in range(N): # print("population %f obj[0]: %f obj[1]: %f obj[2]: %f"%(i,pop[i].obj[0],pop[i].obj[1],pop[i].obj[2])) # print("x[0]:%f x[1]:%f x[2]:%f x[3]:%f x[4]:%f x[5]:%f x[6]:%f x[7]:%f x[8]:%f" # %(pop[i].x[0],pop[i].x[1],pop[i].x[2],pop[i].x[3],pop[i].x[4],pop[i].x[5],pop[i].x[6],pop[i].x[7],pop[i].x[8]) # ) # print("here") # PRINT INDICATOR score = HV(pop) print("HV indicator:%f" % score) plt.show()
def MOEADDRA(N, maxgen, problem, encoding): start = time.time() D = problem.D M = problem.M lower = problem.lower upper = problem.upper CR = 1 pm = 1 / D delta = 0.9 F = 0.5 nr = np.ceil(0.01 * N) W, N = UniformPoint(N, M) T = round(N / 10) pop = initial(N, D, M, lower, upper, problem, encoding) B = FindNeighbour(W, N, M, T) z = Best(pop) utility = [1] * N Delta = np.zeros(N) oldFunction = np.zeros(N) fig = plt.figure() plt.ion() gen = 0 #print("here!!!") while gen < maxgen: for i in range(5): I = MOEADDRAselection(utility, W, N, M) #print("here!") for i in range(len(I)): rand = np.random.rand() if (rand < delta): P = B[int(I[i])] else: P = [j for j in range(N)] k = np.random.randint(0, len(P)) l = np.random.randint(0, len(P)) y = DEandMuation(pop[int(I[i])], pop[P[k]], pop[P[l]], problem, CR, F, pm) for j in range(len(z)): if y.obj[j] < z[j]: z[j] = y.obj[j] c = 0 #flag = np.zeros(len(P)) while (c < nr and len(P) != 0): j = np.random.randint(0, len(P)) if Tchebycheff(y, W[P[j]], z) < Tchebycheff( pop[P[j]], W[P[j]], z): #print("here!") pop[P[j]] = y c = c + 1 P = np.delete(P, j) #P.remove(j) #print(c) if gen % 20 == 0: if gen != 0: for j in range(N): Delta[j] = (oldFunction[j] - Tchebycheff(pop[j], W[j], z)) / oldFunction[j] if Delta[j] > 0.001: # print("yes") utility[j] = 1 else: #print("no") utility[j] = (0.95 + 0.05 * Delta[j] / 0.001) * utility[j] for i in range(N): oldFunction[i] = Tchebycheff(pop[i], W[i], z) draw(problem, pop, M, fig) if gen < maxgen - 1: plt.clf() if ((gen + 1) % 10) == 0: print("%d gen has completed!\n" % (gen + 1)) gen = gen + 1 end = time.time() print("runtime: %.2fs" % (end - start)) plt.ioff() # PRINT INDICATOR score = HV(pop) print("HV indicator:%f" % score) plt.show()