Submission #3599444


Source Code Expand

import random
import bisect
import math

option = 0     # 0:PSO 1:CFA 2:CFArank
max_or_min = 0 # 0:minimize 1:maxmize
dimension = 1
iter=1

N = 50 #粒子の数
T = 1000 #世代数(ループの回数)

a = int(input())
b = int(input())

maximum = 10000
minimum = a - b



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

#評価関数
def criterion(x):
    z=x[0] - (a - b) 
    return z

#粒子の位置の更新を行う関数
def update_position(x, v, x_min, x_max):
#    new_x = [x[i]+ v[i] for i in range(dimension)]
    new_x = [x_min[i] if x[i]+ v[i] < x_min[i] else x_max[i] if x[i]+ v[i] > x_max[i] else x[i]+ v[i] for i in range(dimension)]
#    new_x = [random.uniform(x_min[i], x_max[i]) if x[i]+ v[i] < x_min[i] else random.uniform(x_min[i], x_max[i]) if x[i]+ v[i] > x_max[i] else x[i]+ v[i] for i in range(dimension)]
    return new_x

#粒子の速度の更新を行う関数
def update_velocity(x, v, p, g, w, ro_max=1.0, c1=1.6, c2=1.6):
    #パラメーターroはランダムに与える
    phi=c1+c2
    K=2/abs(2-phi-(phi*phi-4*phi)**0.5)
    
    #粒子速度の更新を行う
    if option!=0:
        new_v = [K * ( w * v[i] + c1 * random.uniform(0, ro_max) * (p[i] - x[i]) + c2 * random.uniform(0, ro_max) * (g[i] - x[i]) ) for i in range(dimension)]
    else:
        new_v = [w * v[i] + c1 * random.uniform(0, ro_max) * (p[i] - x[i]) + c2 * random.uniform(0, ro_max) * (g[i] - x[i]) for i in range(dimension)]

    return new_v

def main():
    w = 0.5*(1+option)
    w_best, w_worst = 1.25, 0.25
    x_min = [minimum for i in range(dimension)]
    x_max = [maximum for i in range(dimension)]
    
    #粒子位置, 速度, パーソナルベスト, グローバルベストの初期化を行う
    ps = [[random.uniform(x_min[j], x_max[j]) for j in range(dimension)] for i in range(N)]
    vs = [[0.0 for j in range(dimension)] for i in range(N)]

    personal_best_positions = ps[:] #list(ps)
    personal_best_scores = [criterion(p) for p in ps]

    if max_or_min == 1:
        best_particle = personal_best_scores.index(max(personal_best_scores))
    elif max_or_min == 0:
        best_particle = personal_best_scores.index(min(personal_best_scores))

    global_best_position = personal_best_positions[best_particle][:]

    for t in range(T):
        for n in range(N):
            x = ps[n][:]
            v = vs[n][:]
            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 = update_position(x, v, x_min, x_max)
            ps[n] = new_x[:]
            
            #粒子の速度の更新を行う
            new_v = update_velocity(new_x, v, p, global_best_position, w)
            vs[n] = new_v[:]
            
            #評価値を求め, パーソナルベストの更新を行う
            score = criterion(new_x)
            
            if max_or_min == 1:
                if score > personal_best_scores[n]:
                    personal_best_scores[n] = score
                    personal_best_positions[n] = new_x[:]
            elif max_or_min == 0:
                if score < personal_best_scores[n]:
                    personal_best_scores[n] = score
                    personal_best_positions[n] = new_x[:]
        
        #グローバルベストの更新を行う
        if max_or_min == 1:
            best_particle = personal_best_scores.index(max(personal_best_scores))
            global_best_position = personal_best_positions[best_particle][:]

        elif max_or_min == 0:
            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)
#        return min(personal_best_scores), t+1
        return global_best_position 

#--------------------------------------------------------------

if max_or_min == 1:
    best=-float("inf")
    for i in range(iter):
        best=max(best, main())

elif max_or_min == 0:
    best=float("inf")
    for i in range(iter):
#        best=min(best, main())
        best=int(main()[0])
        print(best)

Submission Info

Submission Time
Task A - 積雪深差
User Yuki_Utaai
Language Python (3.4.3)
Score 100
Code Size 4642 Byte
Status AC
Exec Time 440 ms
Memory 3572 KB

Judge Result

Set Name all
Score / Max Score 100 / 100
Status
AC × 20
Set Name Test Cases
all 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt, test_01.txt, test_02.txt, test_03.txt, test_04.txt, test_05.txt, test_06.txt, test_07.txt, test_08.txt, test_09.txt, test_10.txt, test_11.txt, test_12.txt, test_13.txt, test_14.txt, test_15.txt, test_16.txt, test_17.txt
Case Name Status Exec Time Memory
00_sample_01.txt AC 410 ms 3572 KB
00_sample_02.txt AC 397 ms 3572 KB
00_sample_03.txt AC 429 ms 3572 KB
test_01.txt AC 388 ms 3572 KB
test_02.txt AC 416 ms 3572 KB
test_03.txt AC 439 ms 3572 KB
test_04.txt AC 373 ms 3572 KB
test_05.txt AC 428 ms 3572 KB
test_06.txt AC 427 ms 3572 KB
test_07.txt AC 430 ms 3572 KB
test_08.txt AC 411 ms 3572 KB
test_09.txt AC 424 ms 3572 KB
test_10.txt AC 421 ms 3572 KB
test_11.txt AC 423 ms 3572 KB
test_12.txt AC 425 ms 3572 KB
test_13.txt AC 427 ms 3572 KB
test_14.txt AC 440 ms 3572 KB
test_15.txt AC 420 ms 3572 KB
test_16.txt AC 419 ms 3572 KB
test_17.txt AC 391 ms 3572 KB