分支限定法的具体实现问题_第1页
分支限定法的具体实现问题_第2页
分支限定法的具体实现问题_第3页
分支限定法的具体实现问题_第4页
分支限定法的具体实现问题_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

用实验三分支定界法实现0/1背包问题实验的目的1、利用点边界法实现0/1背包问题2、学习使用Java实现算法代码。实验原理分支极限是系统地搜索空间的另一种方法,与反向跟踪方法最大的区别是E节点扩展方法。每个活动节点只成为一个E节点。当一个节点成为E节点时,将创建可以从该节点移动一步到达的所有新节点。放弃结果节点中不可导出的(最佳)可能解决方案,将其馀节点添加到活动节点表中,然后选择表中的下一个E-node作为节点。从活动节点表格中移除选定节点,并且展开进程直到找到解决方案或活动表格为空。要求具体的0/1背包问题,用分界法解决。实验程序import Java . util . *;Public class bb knapsackInt容量;/背包重量int n;/项目总数intweight;/料号重量阵列intvalue;/项目价值阵列Int curWeight/当前重量Int curValue/现值intbestx;/最佳解决方案max heap max heap=new max heap();/活动节点优先队列/计算节点的上限Private int bound(int i) int cleft=capactity-curWeight;Int b=curValue/按项目单位重量值减少剩馀容量。While (i=n weighti=cleft) cleft-=weightI;b=valueI;I;/填充装满背包的剩馀容量If (i=n) b=valueI/weightI* cleft;return b;/将新活动节点添加到子集树和优先级队列private void addlive vode(int upper profit、int PP、intww、Int level、bbnode parent、boolean leftchild)、Bbnode b=new bbnode (parent,left child);Heap node=new heap node (b,upper profit,PP,ww,level);max heap . put(node);/优先级队列分支边界方法专用int bb knapsack () & nbspBBnode enode=nullint I=1;int bestp=0;int up=bound(1);While (i)!=n 1) intwt=curweightI;/查看当前展开节点的左儿子节点If (wt=capactity) If (curValue valuei bestp) bestp=curValue valueI;Addlivevode (up、curvalue value I、curweight I、I 1、enode、true);up=bound(I 1);/查看当前展开节点的右儿子节点If (up=bestp) Addlivevode (up、curvalue、curweight、I 1、e node、false);heap node node=max heap . remove max();Enode=node.liveNodeCurWeight=node.weightCurValue=fitUp=node.upperProfitI=node.level/配置当前最佳解决方案for(int j=n;j 0;J-)Bestx j=(emode.leftchild)?1 : 0;Enode=enode.parentReturn curValuePublic int knapsack (int PP,int ww,int cc,int xx)Capactity=ccn=PP . length-1;/n=pp.lengthElementq=new Elementn;int ws=0;int PS=0;for(int I=0);I n;I) Qi=新元素(i 1,PPI/wwI);PS=PPI;ws=wwI;If (ws=capactity) for(int I=1);I=n;I) xxI=1;Return PS/按单位重量值排序Arrays.sort (q,new elem comparator();value=new intn 1;weight=new intn 1;for(int I=1);I=n;I) Valuei=ppqi-1。id-1;Weighti=wwqi-1。id-1;CurWeight=0;CurValue=0;bestx=new intn 1;max heap=new max heap();/bbKnapsack调用以查找问题的最佳解决方案int maxp=bbKnapsack();for(int I=1);I=n;I) Xxqn-i。id=bestxI;Return maxppublic static void main(string arg)throws exception int capacity=0;intPP;intww;intchoice;int bestP=0;String str=最佳解决方案:;file operation f1=new file operation();Vector v1=f1.loadFile(D:/料号重量)。txt );ww=new intv1 . size()-1;for(int I=0);I v1 . size()-1;I) wwI=v 1 . get(I);cap acity=v1 . get(v1 . size()-1);file operation F2=new file operation();Vector v2=f2.loadFile(D:/料号值。txt );PP=new intv2 . size();for(int I=0);I v2 . size();I) PPI=v 2 . get(I);choice=new intPP . length;bb knap sack bb knap sack=new bb knap sack();Bestp=bbknapsack.knapsack (PP,ww,capacity,choice);Str=bestP 每个条目已装载(0未装载,1已装载):for(int I=0);I choice.lengthI) str= choiceI;F1.saveFile(D:/分支边界方法背包问题输出文件。txt ,str);import Java . parator;public class heap comparator implements comparator Public intcompare (objectobject1,objectobject2) heap node heap node 1=(heap node)object 1;heap node heap node 2=(heap node)object 2;heap node 1 . upper profit heap node 2 . upper profit(if)return 1; elsereturn 0;Public class heap nodeBBnode liveNode/实时节点Int upperProfit/最大节点值Int profit/节点的相应值Int weight/对应于节点的重量Int level/子集树中活动节点所在的层次结构序号/构造方法Public heap node (bbnode live node,int upper profit,int profit,Int weight,int level) this . live node=live node;this . upper profit=upper profit;Tfit=profitThis.weight=weightThis.level=levelimport Java . util . ArrayList;import Java . util . collections;import Java . util . list;Public class max heap/堆节点容器private List heap=new ArrayList();公共语音节点(public void put)heap . add(heap node);Public heap node removeamax(),Collections.sort (heap,new heap comparator();HeapNode maxNode=nullIf(!heap . isempty()max node=heap . get(0);heap . remove(max node);Return maxNodePublic class bbnodeBBnode paren

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论