当前位置: 首页 > news >正文

国外优质网站/长春关键词优化报价

国外优质网站,长春关键词优化报价,网站名称格式,电影网站免费建设背包问题,一般可以用动态规划解决。当涉及到的物体数目比较多,填表法所需要的存储空间很大$O(nW)$,每次都以内存不足告终。参考:https://www.geeksforgeeks.org/implementation-of-0-1-knapsack-using-branch-and-bound/1.填表法&…

背包问题,一般可以用动态规划解决。当涉及到的物体数目比较多,填表法所需要的存储空间很大$O(nW)$,每次都以内存不足告终。

参考:

https://www.geeksforgeeks.org/implementation-of-0-1-knapsack-using-branch-and-bound/

1.填表法:

defsolve_it(input_data):#Modify this code to run your optimization algorithm

#parse the input

lines = input_data.split('\n')

firstLine=lines[0].split()

item_count=int(firstLine[0])

capacity= int(firstLine[1])

items=[]for i in range(1, item_count+1):

line=lines[i]

parts=line.split()

items.append(Item(i-1, int(parts[0]), int(parts[1])))#print item information

#for i in range(0, len(items)):

#print str(items[i].index) + ',' + str(items[i].value) + ',' + str(items[i].weight) +'\n'

#a trivial greedy algorithm for filling the knapsack

#it takes items in-order until the knapsack is full

value =0

weight=0

taken= [0]*len(items) #为1则表示选择了该物体

'''for item in items:

if weight + item.weight <= capacity:

taken[item.index] = 1

value += item.value

weight += item.weight'''result= np.zeros([capacity+1, item_count+1]) #表的大小为n*W,n为物体数目,W为包的容量

#result[k][0] = 0 for all k

for i in range(0, capacity+1):

result[i][0]=0for i in range(0, item_count+1):

result[0][i]=0#填表法

for k in range(1, capacity+1):for j in range(1, item_count+1): #第j件物品其索引值为j-1

if k-items[j-1].weight >=0:

result[k][j]= max([result[k][j-1], result[k-items[j-1].weight][j-1] + items[j-1].value])else:

result[k][j]= result[k][j-1]

value=int(result[capacity][item_count])

out_to_csv(result)#根据表寻找最优解的路径

k =capacity

j=item_countwhile(result[k,j] !=0):if result[k][j] > result[k][j-1]:

k= k - items[j-1].weight

taken[j-1] = 1j= j - 1

else:

j= j - 1

#prepare the solution in the specified output format

output_data = str(value) + ' ' + str(0) + '\n'output_data+= ' '.join(map(str, taken))return output_data

填表法在物体数目较小的时候可以解决,单所需表的存储空间比较大的时候开始报错。

故选择了分支定界算法。

2. 关于python3中自定义比较函数的用法:

参考自:https://www.polarxiong.com/archives/Python3-%E6%89%BE%E5%9B%9Esort-%E4%B8%AD%E6%B6%88%E5%A4%B1%E7%9A%84cmp%E5%8F%82%E6%95%B0.html

from functools importcmp_to_key

nums= [1, 3, 2, 4]

nums.sort(key=cmp_to_key(lambda a, b: a -b))print(nums) #[1, 2, 3, 4]

2.1 我的自定义比较函数:

from functools importcmp_to_key#自定义比较函数

defmycmp(item1, item2):if(item1.value*1.0/item1.weight > item2.value*1.0/item2.weight): #value/weight大的排前边

return -1

else:return0#关于python3的自定义比较函数用法

items.sort(key=cmp_to_key(lambda a, b : mycmp(a,b)))

2.2 用到了节点类:

#节点类

classNode:def __init__(self, level, curvalue, room, bestvalue, taken, capacity): #成员变量

self.level =level

self.curvalue=curvalue

self.room=room

self.bestvalue=bestvalue

self.path=taken

self.capacity=capacitydefshow(self):print(self.level , ",", self.curvalue, ",", self.room, ",", self.bestvalue)#所求的bound值

defbound(self, items):

weight=0

value=0if self.level == -1:for i inrange(len(items)):if weight + items[i].weight <=self.capacity:

value+=items[i].value

weight+=items[i].weightelse:

value+= (self.capacity - weight) * 1.0 / items[i].weight *items[i].valuebreak

else:

value+=self.curvalue

weight+= self.capacity -self.roomfor i in range(self.level+1, len(items), 1):if weight + items[i].weight <=self.capacity:

value+=items[i].value

weight+=items[i].weightelse:

value+= (self.capacity - weight) * 1.0 / items[i].weight *items[i].valuebreak

return value

3. 深度优先的分支定界。用栈实现,未用递归。

defsolve_it(input_data):#Modify this code to run your optimization algorithm

#parse the input

lines = input_data.split('\n')

firstLine=lines[0].split()

item_count= int(firstLine[0]) #物体数目

capacity = int(firstLine[1]) #背包容量

items =[]for i in range(1, item_count+1):

line=lines[i]

parts=line.split()

items.append(Item(i-1, int(parts[0]), int(parts[1]))) #物体初始化

value=0

weight=0

taken= [0]*len(items)

empty= [0]*len(items)#关于python3的自定义比较函数用法

items.sort(key=cmp_to_key(lambdaa, b : mycmp(a,b)))#for item in items:

#print (str(item.index) + "," + str(item.value) + "," + str(item.weight))

stack= [] #深度优先用栈实现,python中list代替

u = Node(-1, 0, capacity, 0, empty, capacity)

temp=u.bound(items)

u.bestvalue=temp#print("curvalue:", u.curvalue)

#print("bound:", u.bestvalue)

stack.append(u)

max_profit=0while(len(stack) !=0):#弹出末尾的节点

t =stack.pop()

v= Node(-1, 0, capacity, 0, empty, capacity)if t.level == -1:

v.level=0if t.level == item_count-1:continue

#not choose this item

v.level = t.level + 1v.room=t.room

v.curvalue=t.curvalue

v.bestvalue=v.bound(items)

v.path= t.path.copy() #注意Python中list为引用,故不能直接赋值,而是用copy()方法

if v.bestvalue >max_profit:

stack.append(v)if v.level == item_count -1:

max_profit= v.curvalue #保留最大profit

taken = v.path #保留最优解

#choose this item

v1 = Node(-1, 0, capacity, 0, empty, capacity)

v1.level= t.level + 1v1.room= t.room -items[v1.level].weight

v1.curvalue= t.curvalue +items[v1.level].value#print("curvalue:", v1.curvalue)

#copy(), 不能直接赋值,因为都是引用

v1.path =t.path.copy()

v1.path[items[v1.level].index]= 1v1.bestvalue=t.bestvalue#print("v1.path:", v1.path)

if (v1.room >= 0) and (v1.bestvalue >max_profit):#print(taken)

#满足则加入stack

stack.append(v1)if v1.level == item_count-1:

max_profit= v1.curvalue #保留最大profit

taken = v1.path #保留最优解集

#print(taken)

value =max_profit#prepare the solution in the specified output format

output_data = str(value) + ' ' + str(0) + '\n'output_data+= ' '.join(map(str, taken))return output_data

4.总结

第一次做分支定界算法,总算解决了问题。第一遍写的实现问题百出,首先是bound的计算问题,当bound计算出错时,会发现树无法高效地剪枝(pruning)。导致程序一直运行。后来才发现是bound的计算错误。改正bug后,程序完成的时间都不到一分钟。

http://www.lbrq.cn/news/750007.html

相关文章:

  • 怎么开发聊天软件/青岛优化网站关键词
  • 京东购物网站怎么做/网址域名注册
  • 做娱乐网站彩票代理/百度搜索量查询
  • 合肥seo网站多少钱/seo页面代码优化
  • 宁波网站建设方案咨询/郑州网站定制
  • 滨湖区知名做网站价格/网站托管代运营
  • 宜宾公司做网站/qq群推广链接
  • 辽ICP备 网站建设 中企动力/湖北百度seo
  • 无锡网站优化公司/网络营销策划与创意
  • 做网站用的编程语言/怎么开设自己的网站
  • 网站开发负载测试/今日国内重大新闻
  • vps小学生/windows优化大师兑换码
  • 北京注册公司代理/seo网络营销技术
  • 宁夏自治区住房城乡建设厅网站/如何提高seo关键词排名
  • 怎么做老虎机网站的/网站建设平台哪家好
  • 网站设计的机构/精准引流推广团队
  • 搭建网页教程/谷歌广告优化师
  • 商家产品展示网站源码/品牌营销策略论文
  • 长沙网站创建/seo公司运营
  • 手工迷你饮水机/天津网站优化
  • 做网站定位/百度大搜数据多少钱一条
  • 免费制作一个自己的网站/最新的疫情最新消息
  • 网站悬浮窗/0元入驻的电商平台
  • 有用vue做企业网站的/上海专业排名优化公司
  • 温州微网站制作电话/广告公司职位
  • 北京建站设计/投稿网站
  • 做自动发卡密网站的教程/怎样申请自己的电商平台
  • 自己做网站卖机器设备/媒体软文发稿
  • 钱网站制作/做网站哪家公司比较好而且不贵
  • 怎样切换到经典编辑器wordpress/seo网站排名推广
  • 48.Seata认识、部署TC服务、微服务集成
  • http工作流程
  • 自由学习记录(85)
  • C语言指针运算题
  • OpenStack Neutron中的L2 Agent与L3 Agent:新手友好指南
  • ubuntu网络共享