結果

問題 No.648  お や す み 
ユーザー Yuki_Utaai
提出日時 2018-05-28 23:10:56
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 484 ms / 2,000 ms
コード長 2,715 bytes
コンパイル時間 418 ms
コンパイル使用メモリ 82,184 KB
実行使用メモリ 78,612 KB
最終ジャッジ日時 2024-09-13 22:28:17
合計ジャッジ時間 29,255 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 84
権限があれば一括ダウンロードができます

ソースコード

diff #

import random

#--------粒 子 群 最 適 化------------------------------

#評価関数: 
def criterion(x):
    z=-abs(k-(x*(x+1))/2)
    if x<0:
      z=-10**50
    return z

#粒子の位置の更新を行う関数
def update_position(x, vx):
    new_x = x + vx
    return new_x

#粒子の速度の更新を行う関数
def update_velocity(x, vx, p, g, w=0.5, ro_max=1):
    v_max=k//3
    #パラメーターroはランダムに与える
    ro1 = random.uniform(0, ro_max)
    ro2 = random.uniform(0, ro_max)
    #粒子速度の更新を行う
    new_vx = int(w * vx + ro1 * (p - x) + ro2 * (g - x))
    if abs(new_vx)>v_max:
        new_vx=int(v_max*new_vx/abs(new_vx))
    return new_vx

def main():
    N = 1000  #粒子の数
    x_min, x_max = 0, k

    #粒子位置, 速度, パーソナルベスト, グローバルベストの初期化を行う
    ps = [random.randint(x_min, x_max) for i in range(N)]
    vs = [0.0 for i in range(N)]

    personal_best_positions = list(ps)
    personal_best_scores = [criterion(p) for p in ps]
    best_particle = personal_best_scores.index(max(personal_best_scores))
    global_best_position = personal_best_positions[best_particle]


    T = 1000  #世代数(ループの回数)
    for t in range(T):
        for n in range(N):
            x = ps[n]
            vx= vs[n]
            p = personal_best_positions[n]

            #粒子の位置の更新を行う
            new_x = update_position(x, vx)
            ps[n] = new_x

            #粒子の速度の更新を行う
            new_vx= update_velocity(new_x, vx, p, global_best_position)
            vs[n] = new_vx

            #評価値を求め, パーソナルベストの更新を行う
            score = criterion(new_x)
            if score > personal_best_scores[n]:
                personal_best_scores[n] = score
                personal_best_positions[n] = new_x

        #グローバルベストの更新を行う
        best_particle = personal_best_scores.index(max(personal_best_scores))
        global_best_position = personal_best_positions[best_particle]

        if k==int(global_best_position)*(int(global_best_position)+1)//2:
          print("YES")
          print(int(global_best_position))
          exit()


    #最適解
#    print((int(global_best_position)*(int(global_best_position)+1))//2)
#    print(max(personal_best_scores), global_best_position)
    return max(personal_best_scores), global_best_position
#--------------------------------------------------------------

k=int(input())
best=-100000000000000
xx=0
for i in range(3):
    kouho_best, kouho_x=main()
    if best>kouho_best:
        best=kouho_best
        xx=kouho_x
 
print("NO")
0