Subscribed unsubscribe Subscribe Subscribe

12A猫で学んだこと-Memoir-

...What are you learning now?

占い確定狩人生存ゲームでの村勝率の解析計算-1-(ソースコード)


import numpy as np

maxN = 50;
maxM = 15;
maxS = maxN;
undefined = -1;
baseRate = np.zeros((maxN, maxM, maxS)) - np.ones((maxN, maxM, maxS)) ;
def output(n, m, s):
    print( "people:"+ str(n), "wolves:"+str(m), "assured:" +str(s), "prob:"+ str(baseRate[n][m][s]));

def allOutput():
    for n in range(maxN):
        for m in range(maxM):
            for s in range(maxS):
                output(n,m,s);
def baseRateCalc(n, m, s):
    if baseRate[n][m][s] != undefined:
        answer = baseRate[n][m][s];
        return answer;
    elif n < m + s:
        answer = 0.0; # Actually, its value is meaningless due to invalid setting.  
    elif s >= n and n !=0:
        answer = 1.0;
    elif m == 0:
        answer = 1.0;
    elif m >= float(n)/2:
        answer = 0.0;
    else:
        sDec = max(s-1,0);
        answer = float(m)/(n-s) * baseRateCalc(n - 2, m -1, sDec) +  float(n - s -  m)/(n - s) * baseRateCalc(n- 2, m, sDec);

    baseRate[n][m][s] = answer;
    return answer;

for n in range(maxN):
    for m in range(maxM):
        for s in range(maxS):
            baseRateCalc(n, m, s);

allOutput()


maxH = maxN;
gMatrix =  np.zeros((maxN, maxM, maxS, maxN, maxN)) - np.ones((maxN, maxM, maxS, maxN, maxN)) ; 
wMatrix =  np.zeros((maxN, maxM, maxS, maxN, maxN)) - np.ones((maxN, maxM, maxS, maxN, maxN)) ; 

def debugGMatrix(n, m, s, h, H):
    print(str(m)+"W", " in"+ str(n),  "s:"+ str(s)  
          ,"h:"+ str(h),  "H:" + str(H));  

def calcwMatrix(n, m, s, h, H):
    if wMatrix[n][m][s][h][H] != undefined:
        answer = wMatrix[n][m][s][h][H];
        return answer;
    if m == 0:
        return 1.0;
    answer = 0;
    hInc = min(h+1,H);
    wolfWait = calcgMatrix(n-1, m,s-1, hInc, H);
    wolfChallenge = float(h)/ H * baseRate[n][m][s] + float(H-h)/float(H) * calcgMatrix(n,m,s, 0, n-m) 
    debugGMatrix(n,m,s,h,H);
    print("Challenge:"+str(wolfChallenge))
    print("Waiting:"+str(wolfWait))
    if wolfChallenge > wolfWait:
        print("Waiting is preferred.");
        answer = wolfWait;
    else:
        print("Challenging is preferred.")
        answer = wolfChallenge;
    wMatrix[n][m][s][h][H]=answer;
    return answer;

def calcgMatrix(n, m, s, h, H):
    if gMatrix[n][m][s][h][H] != undefined:
        answer = gMatrix[n][m][s][h][H];
        return answer;
    if m == 0:
        return 1.0;
    if (float(m)/(n+1) >= 0.5):
        return 0.0;
    if s >= n - s: 
        return 1.0;

    Kw1 = float(m)/(n-s);
    Kw2 = float(n-s-m)/(n-s) * float(m)/(n-(s+1));
    Kv  = 1.0 - (Kw1 + Kw2);
    
    answer = 0.0
    answer = answer + Kw1 * calcwMatrix(n-1, m-1,s, h, H);
    answer = answer + Kw2 * calcwMatrix(n-1, m-1,s+1,h,H);
    answer = answer + Kv * calcwMatrix(n-1, m, s+1, min(H, h+1), H); 

    debugGMatrix(n, m, s, h, H);
    print("Answer:", answer)
    gMatrix[n][m][s][h][H]=answer;
    return answer;

surveyN = 4;
surveyM = 1;
surveyS = 0;
surveyh = 0;
surveyH = surveyN - surveyM;

print(calcgMatrix(surveyN, surveyM, surveyS, surveyh, surveyH))