lecture10_P6_BB.py 1.04 KB
def kp(i, profit, weight):
        global bestset
        global maxprofit
        
        if( weight <= W and profit >maxp):
            maxp = profit
            bestset = include[:]
            # best = include는 best를 include의 reference로 만든다.
            # 한 번 동일한 값을 가진 후 그 이후는 계속 동일함.
        if(promising(i,weight,profit)):
            include[i+1]=1
            kp(i+1,profit+p[i+1], weight+w[i+1])
            include[i+1]=0
            kp(i+1,profit, weight)







def promising(i,weight,profit):
    global maxprofit
        
    if(weight >=W):
        return False
    else:
        j=i+1
        bound = profit
        totweight = weight
        
        # item이 있고 그 무게 안에서 최대의 bound를 찾고
        while((j <= n-1) and (totweight + w[j] <= W)):
            totweight += w[j]
            bound += p[j]
            j+=1
        
        # k = j
        
        if(j <= n-1):
            bound += (W-totweight)*p[k]/w[k] # 최종 bound 검색
        return bound > maxprofit