当前位置:网站首页>各种解背包问题
各种解背包问题
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] Chapter 3 and 4: window components and layout management
- 【二叉树】翻转二叉树以匹配先序遍历
- 1、 Array -- sliding window problem -- subarray with the smallest length -- fruit basket problem
- How to solve the problem that the esp8266-01s cannot connect to Huawei routers
- 实现领域驱动设计 - 使用ABP框架 - 通用准则
- Paper reading (54):deepfool: a simple and accurate method to four deep neural networks
- Leetcode: hash table 06 (ransom letter)
- 高级计网笔记(八)
- Paper reading (48):a Library of optimization algorithms for organizational design
- [sword finger offer] 46 Translate numbers into strings
猜你喜欢

【Qt】第十章:数据库

一,数组--滑动窗口问题--长度最小的子数组--水果成篮问题

QT based graphics rendering system documentation + project source code and executable exe files + system instructions

leetcode刷题:哈希表02 (两个数组的交集)

【翻译】一种减小运动伪影的新方法基于AS-LMS自适应滤波器的PPG信号

Leetcode question brushing: hash table 03 (happy number)

Counter attack and defense (1): counter sample generation in image domain

Know Chuangyu: content oriented, ai+ artificial empowerment

科技互动沙盘是凭借什么收获人气的

Stepping mode of research control motor
随机推荐
高级计网笔记(六)
leetcode刷题:哈希表05 (四数相加 II)
1、 Array -- sliding window problem -- subarray with the smallest length -- fruit basket problem
实用电路分析3
[win10 vs2019 opencv4.6 configuration reference]
如何利用好每天的时间高效复习?
【翻译】一种减小运动伪影的新方法基于AS-LMS自适应滤波器的PPG信号
程序员未来会成为非常内卷的职业么?
反直觉的三门问题,80%的人都会错?
Stepping mode of research control motor
实现领域驱动设计 - 使用ABP框架 - 存储库
An error is reported when latex uses \usepackage{hyperref}: paragraph ended before [email protected] @link was complete
高级计网笔记(三)
Landing of global organizational structure control
uniapp项目中防止用户重复提交
【Qt】第三、四章:窗口部件、布局管理
聊一聊数据库的行存与列存
Rancher2.6全新Monitoring快速入门
Dive Into Deep Learning——1、前言
Implementing Domain Driven Design - using ABP framework - repository