結果

問題 No.453 製薬会社
ユーザー Yuki_UtaaiYuki_Utaai
提出日時 2018-03-22 20:52:34
言語 PyPy3
(7.3.15)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 3,133 bytes
コンパイル時間 501 ms
コンパイル使用メモリ 87,100 KB
実行使用メモリ 122,816 KB
最終ジャッジ日時 2023-09-07 01:49:27
合計ジャッジ時間 8,240 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 494 ms
108,792 KB
testcase_01 AC 475 ms
112,696 KB
testcase_02 AC 481 ms
112,256 KB
testcase_03 AC 484 ms
108,540 KB
testcase_04 WA -
testcase_05 AC 490 ms
108,496 KB
testcase_06 AC 486 ms
111,320 KB
testcase_07 AC 499 ms
117,792 KB
testcase_08 AC 488 ms
108,756 KB
testcase_09 AC 477 ms
108,352 KB
testcase_10 AC 486 ms
117,700 KB
testcase_11 AC 486 ms
111,368 KB
testcase_12 AC 474 ms
108,392 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import random

#評価関数: z = 1000*x+2000*y
def criterion(x, y):
    stock_c=w-0.75*(x)-2/7*(y)
    stock_d=h-0.25*(x)-5/7*(y)
    z=1000*x+2000*y
    if stock_c<0 or stock_d<0 or x<0 or y<0:
        z=-z
    return z

#粒子の位置の更新を行う関数
def update_position(x, y, vx, vy):
    stock_c=w-0.75*(x+vx)-2/7*(y+vy)
    stock_d=h-0.25*(x+vx)-5/7*(y+vy)
    if stock_c<0 or stock_d<0 or x+vx<0 or y+vy<0:
        new_x = x
        new_y = y
    else:
        new_x = x + vx
        new_y = y + vy
    return new_x, new_y

#粒子の速度の更新を行う関数
def update_velocity(x, y, vx, vy, p, g, w=0.5, ro_max=1):
    #パラメーターroはランダムに与える
    ro1 = random.uniform(0, ro_max)
    ro2 = random.uniform(0, ro_max)
    #粒子速度の更新を行う
    new_vx = w * vx + ro1 * (p["x"] - x) + ro2 * (g["x"] - x)
    new_vy = w * vy + ro1 * (p["y"] - y) + ro2 * (g["y"] - y)
    return new_vx, new_vy

def main():
    N = 1500  #粒子の数
    x_min, x_max = 0, 2*w
    y_min, y_max = 0, 2*h
    #粒子位置, 速度, パーソナルベスト, グローバルベストの初期化を行う
    ps = [{"x": random.uniform(x_min, 0.7*w), 
        "y": random.uniform(y_min, 0.7*h)} for i in range(N)]
    vs = [{"x": 0.0, "y": 0.0} for i in range(N)]

    personal_best_positions = list(ps)
    personal_best_scores = [criterion(p["x"], p["y"]) for p in ps]

#   best_particle = np.argmax(personal_best_scores)
#   best_particle = max(enumerate( personal_best_scores), key=lambda x: x[1])[0]   #argminの場合はこっち
    best_particle = personal_best_scores.index(max(personal_best_scores))
    global_best_position = personal_best_positions[best_particle]


    T = 500  #制限時間(ループの回数)
    for t in range(T):
        for n in range(N):
            x, y = ps[n]["x"], ps[n]["y"]
            vx, vy = vs[n]["x"], vs[n]["y"]
            p = personal_best_positions[n]
            #粒子の位置の更新を行う
            new_x, new_y = update_position(x, y, vx, vy)
            ps[n] = {"x": new_x, "y": new_y}
            #粒子の速度の更新を行う
            new_vx, new_vy = update_velocity(
                new_x, new_y, vx, vy, p, global_best_position)
            vs[n] = {"x": new_vx, "y": new_vy}
            #評価値を求め, パーソナルベストの更新を行う
            score = criterion(new_x, new_y)
            if score > personal_best_scores[n]:
                personal_best_scores[n] = score
                personal_best_positions[n] = {"x": new_x, "y": new_y}
        #グローバルベストの更新を行う
#        best_particle = np.argmax(personal_best_scores)
#        best_particle = max(enumerate( personal_best_scores), key=lambda x: x[1])[0]   #argminの場合はこっち
        best_particle = personal_best_scores.index(max(personal_best_scores))
        global_best_position = personal_best_positions[best_particle]

    #最適解
#    print(global_best_position)
#    print(round(new_x,5), round(new_y,5))
    print(max(personal_best_scores))


w, h=[int(i) for i in input().split()]
main()


0