import random import bisect import math option=1 # 0:PSO 1:CFA 2:CFArank 3:hybridPSO max_or_min=1 #0:minimize 1:maxmize filewrite=0 # 1:writefile 2:benchmark dimension=2 w, h=[int(i) for i in input().split()] #--------粒 子 群 最 適 化------------------------------ """ #評価関数 def criterion(x, y, x_min, x_max, y_min, y_max): if x_min<=x<=x_max and y_min<=y<=y_max: z=-(y+47)*math.sin(abs(x/2+(y+47))**0.5) - x*math.sin(abs(x-(y+47))**0.5) else: z=[-float("inf") if max_or_min==1 else float("inf")] return z """ #評価関数: z = 1000*x+2000*y def criterion(x, y, x_min, x_max, y_min, y_max): stock_c=w-0.75*(x)-2/7*(y) stock_d=h-0.25*(x)-5/7*(y) if x_min<=x<=x_max and y_min<=y<=y_max and stock_c>=0 and stock_d>=0: z=1000*x+2000*y else: if max_or_min==1: z=-float("inf") else: z=float("inf") return z #粒子の位置の更新を行う関数 def update_position(x, y, vx, vy, x_min, x_max, y_min, y_max): if x_min<=x+vx<=x_max and y_min<=y+vy<=y_max : new_x = x + vx new_y = y + vy else: new_x = x new_y = y return new_x, new_y #粒子の速度の更新を行う関数 def update_velocity(x, y, vx, vy, p, g, w, ro_max=1, c1=2.05, c2=2.05): #パラメーターroはランダムに与える ro1 = random.uniform(0, ro_max) ro2 = random.uniform(0, ro_max) phi=c1+c2 K=2/abs(2-phi-(phi*phi-4*phi)**0.5) #粒子速度の更新を行う if option!=0: new_vx =K * ( w * vx + c1 * ro1 * (p[0] - x) + c2 * ro2 * (g[0] - x) ) new_vy =K * ( w * vy + c1 * ro1 * (p[1] - y) + c2 * ro2 * (g[1] - y) ) else: new_vx = w * vx + ro1 * (p[0] - x) + ro2 * (g[0] - x) new_vy = w * vy + ro1 * (p[1] - y) + ro2 * (g[1] - y) return new_vx, new_vy def main(): N = 1200 #粒子の数 w = 0.5 w_best, w_worst = 1.25, 0.25 x_min, x_max = 0, float("inf") y_min, y_max = 0, float("inf") #粒子位置, 速度, パーソナルベスト, グローバルベストの初期化を行う ps = [[random.uniform(x_min, w), random.uniform(y_min, h)] for i in range(N)] vs = [[0.0, 0.0] for i in range(N)] personal_best_positions = list(ps) personal_best_scores = [criterion(p[0], p[1], x_min, x_max, y_min, y_max) for p in ps] best_particle = personal_best_scores.index(max(personal_best_scores)) global_best_position = personal_best_positions[best_particle] T = 100 #世代数(ループの回数) for t in range(T): for n in range(N): x, y = ps[n][0], ps[n][1] vx, vy = vs[n][0], vs[n][1] p = personal_best_positions[n] if option>=2: best_list = sorted(personal_best_positions) mu = bisect.bisect_left( best_list, p ) + 1 w = w_best - mu * (w_best - w_worst) / (N - 1) #粒子の位置の更新を行う new_x, new_y = update_position(x, y, vx, vy, x_min, x_max, y_min, y_max ) ps[n] = [new_x, new_y] #粒子の速度の更新を行う new_vx, new_vy = update_velocity(new_x, new_y, vx, vy, p, global_best_position, w) vs[n] = [new_vx, new_vy] #評価値を求め, パーソナルベストの更新を行う score = criterion(new_x, new_y, x_min, x_max, y_min, y_max) if max_or_min == 1: if score > personal_best_scores[n]: personal_best_scores[n] = score personal_best_positions[n] = [new_x, new_y] elif max_or_min == 0: if score < personal_best_scores[n]: personal_best_scores[n] = score personal_best_positions[n] = [new_x, new_y] #グローバルベストの更新を行う if max_or_min == 1: if filewrite>=1: f.write(str(max(personal_best_scores))+"\n") best_particle = personal_best_scores.index(max(personal_best_scores)) global_best_position = personal_best_positions[best_particle] elif max_or_min == 0: if filewrite>=1: f.write(str(min(personal_best_scores))+"\n") best_particle = personal_best_scores.index(min(personal_best_scores)) global_best_position = personal_best_positions[best_particle] #最適解 if max_or_min == 1: return max(personal_best_scores) elif max_or_min == 0: return min(personal_best_scores) #-------------------------------------------------------------- if max_or_min == 1: best=-float("inf") for i in range(1): if filewrite>=2: option=i if filewrite>=1: with open('result_'+ str(option)+".txt", 'w') as f: best=max(best, main()) else: best=max(best, main()) elif max_or_min == 0: best=float("inf") for i in range(1): if filewrite>=2: option=i if filewrite>=1: with open('result_'+ str(option)+".txt", 'w') as f: best=min(best, main()) else: best=min(best, main()) print(best) if filewrite==2: with open('result_0.txt', 'r') as f1, open('result_1.txt', 'r') as f2, open('result_2.txt', 'r') as f3, open('result.txt', 'w') as g: for i in range(100): a,b,c=float(f1.readline()), float(f2.readline()), float(f3.readline()) g.write(str(a)+" "+str(b)+" "+str(c)+"\n")