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()