当前位置:网站首页>各种解背包问题
各种解背包问题
2022-06-23 17:37:00 【巴川笑笑生】
montecarlo
蒙特卡洛
import numpy as np
def solve(vlist,wlist,totalWeight,totalLength):
ValListLen=np.power(2,totalLength)
choiceList=np.arange(ValListLen,dtype=np.int32)
np.random.shuffle(choiceList)
choiceListLen=ValListLen//4*3
ValList = np.zeros(choiceListLen,dtype=np.int32)
for i in range(choiceListLen):
i_tmp=choiceList[i]
value=0
weight=0
for j in range(totalLength-1,-1,-1):
j_tmp=np.power(2,j)
div=i_tmp//j_tmp;
i_tmp=i_tmp-div*j_tmp
if div:
value=value+vlist[j]
weight=weight+wlist[j]
if weight<=totalWeight:
ValList[i]=value;
return(ValList.max())
if __name__ == '__main__':
v = [4,6,12,7]
w = [1,2,3,2]
weight = 6
n = 4
result = solve(v,w,weight,n)
print(result)
backtrack
回溯
bestV=0
curW=0
curV=0
bestx=None
def solve_backtrack_asist(i,x,vlist,wlist,totalWeight,totalLength):
global bestV,curW,curV,bestx
if i>=totalLength:
if bestV<curV:
bestV=curV
bestx=x[:]
else:
if curW+wlist[i]<=totalWeight:
x[i]=1
curW+=wlist[i]
curV+=vlist[i]
solve_backtrack_asist(i+1,x,vlist,wlist,totalWeight,totalLength)
curW-=wlist[i]
curV-=vlist[i]
x[i]=0
solve_backtrack_asist(i+1,x,vlist,wlist,totalWeight,totalLength)
return bestV
def solve(vlist,wlist,totalWeight,totalLength):
x=[0 for i in range(totalLength)]
bestV=solve_backtrack_asist(0,x,vlist,wlist,totalWeight,totalLength)
return bestV
if __name__ == '__main__':
v = [4,6,12,7]
w = [1,2,3,2]
weight = 6
n = 4
bestV=solve(v,w,weight,n)
print(bestV)
branch
分支限界
import numpy as np
import queue
import math
#value
def solve(vlist,wlist,totalWeight,totalLength):
vec_len = 2**(totalLength+1) - 1#tree `s size
vec_v = np.zeros(vec_len)
vec_w = np.zeros(vec_len)
vec_w[0]=totalWeight
que = queue.Queue();
que.put(0)
best = 0
while(not que.empty()):
current = que.get()
level = int(math.log(current+1,2))
if(vec_v[current] > vec_v[best]):
best = current
left = 2*current+1#left child index
right = 2*current+2#right child index
if(left < vec_len and vec_w[current]-wlist[level] >= 0 and vec_v[current]+vlist[level] > vec_v[best] ):
vec_v[left] = int(vec_v[current]+vlist[level])
vec_w[left] = vec_w[current]-wlist[level]
que.put(left)
if(right < vec_len and sum(vlist[level+1:-1])+vec_v[current] > vec_v[best]):
vec_v[right] = vec_v[current]
vec_w[right] = vec_w[current]
que.put(right)
return int(vec_v[best])
if __name__ == '__main__':
w = [1,2,3,2]#weight
v = [4,6,12,7]
weight=6
n=4
result=solve(v,w,weight,n)
print(result)
dynamic
动态规划
import numpy as np
def solve(vlist,wlist,totalWeight,totalLength):
resArr = np.zeros((totalWeight)+1,dtype=np.int32)
for i in range(0,totalLength):
for j in range(totalWeight,0,-1):
if wlist[i] <= j:
resArr[j] = max(resArr[j],resArr[j-wlist[i]]+vlist[i])
return resArr[-1]
if __name__ == '__main__':
v = [4,6,12,7]
w = [1,2,3,2]
weight = 6
n = 4
result = solve(v,w,weight,n)
print(result)
exhoustive
穷举
import numpy as np
def solve(vlist,wlist,totalWeight,totalLength):
ValListLen=np.power(2,totalLength)
ValList = np.zeros(ValListLen,dtype=np.int32)
for i in range(ValListLen):
i_tmp=i
value=0
weight=0
for j in range(totalLength-1,-1,-1):
j_tmp=np.power(2,j)
div=i_tmp//j_tmp;
i_tmp=i_tmp-div*j_tmp
if div:
value=value+vlist[j]
weight=weight+wlist[j]
if weight<=totalWeight:
ValList[i]=value;
return(ValList.max())
if __name__ == '__main__':
v = [4,6,12,7]
w = [1,2,3,2]
weight = 6
n = 4
result = solve(v,w,weight,n)
print(result)
memorandum
记事本
import numpy as np
def solve(vlist,wlist,totalWeight,totalLength):
resArr = np.zeros((totalLength,totalWeight+1),dtype=np.int32)
for i in range(0,totalLength):
for j in range(1,totalWeight+1):
if wlist[i] <= j:
resArr[i,j] = max(resArr[i-1,j-wlist[i]]+vlist[i],resArr[i-1,j])
else:
resArr[i,j] = resArr[i-1,j]
return resArr[-1,-1]
if __name__ == '__main__':
v = [4,6,12,7]
w = [1,2,3,2]
weight = 6
n = 4
result = solve(v,w,weight,n)
print(result)
main
对比时间
import backtrack
import branch
import dynamic
import exhoustive
import memorandum
import montecarlo
import time
epoch=10000
if __name__=='__main__':
v = [4,6,12,7]
w = [1,2,3,2]
weight = 6
n = 4
time_start=time.time()
for i in range(epoch):
bestV=backtrack.solve(v,w,weight,n)
time_end=time.time()
print(bestV,time_end-time_start)
time_start=time.time()
for i in range(epoch):
bestV=branch.solve(v,w,weight,n)
time_end=time.time()
print(bestV,time_end-time_start)
time_start=time.time()
for i in range(epoch):
bestV=dynamic.solve(v,w,weight,n)
time_end=time.time()
print(bestV,time_end-time_start)
time_start=time.time()
for i in range(epoch):
bestV=exhoustive.solve(v,w,weight,n)
time_end=time.time()
print(bestV,time_end-time_start)
time_start=time.time()
for i in range(epoch):
bestV=memorandum.solve(v,w,weight,n)
time_end=time.time()
print(bestV,time_end-time_start)
time_start=time.time()
for i in range(epoch):
bestV=montecarlo.solve(v,w,weight,n)
time_end=time.time()
print(bestV,time_end-time_start)
边栏推荐
- QT based graphics rendering system documentation + project source code and executable exe files + system instructions
- Thesis reading (57):2-hydr_ Ensemble: lysine 2-hydroxyisobutyrylation identification with ensemble method (task)
- 聊一聊数据库的行存与列存
- leetcode刷题:哈希表07 (三数之和)
- 信用卡产品开发周期从23周缩短至9周,银行运维组织如何转向敏捷?
- 【Qt】第十章:数据库
- Wiley- Open Science Joint Symposium of the documentation and information center of the Chinese Academy of Sciences, lecture 2: open access journal selection and paper submission
- Paper reading (54):deepfool: a simple and accurate method to four deep neural networks
- VirtP4笔记
- 为什么要创建公开的OKR?
猜你喜欢

Paper reading (54):deepfool: a simple and accurate method to four deep neural networks

程序员未来会成为非常内卷的职业么?

Talk about row storage and column storage of database

实用电路分析3

Leetcode: hash table 06 (ransom letter)

聊一聊数据库的行存与列存

yapi安装

WIN11 系统所有快捷键说明

Paper reading (55):dynamic multi robot task allocation under uncertainty and temporary constraints

Practical circuit analysis 3
随机推荐
TT voice landing Zadig: open source co creates helm access scenario, and environmental governance can be done!
2022年在网上办理股票开户安全吗?
Rancher2.6全新Monitoring快速入门
Torch learning (I): environment configuration
可编程交换机上的近似公平排队阅读笔记
CV-图像分类
QT based graphics rendering system documentation + project source code and executable exe files + system instructions
Leetcode question brushing: hash table 05 (adding four numbers II)
Paper reading (54):deepfool: a simple and accurate method to four deep neural networks
CSDN salary increase secret script: Jenkins integrated allure test report complete tutorial
TimerTasks笔记
云原生行业应用崛起,从“可用”到“好用”有多远?
Implementing Domain Driven Design - using ABP framework - General guidelines
CV-全连接神经网络
Will programmers become very professional in the future?
Stepping mode of research control motor
测试
信用卡产品开发周期从23周缩短至9周,银行运维组织如何转向敏捷?
Paper reading (58):research and implementation of global path planning for unmanned surface vehicle based
基于FPGA的电磁超声脉冲压缩检测系统 论文+源文件