分枝限界法 - 最优装载问题

题目

 

采用分枝限界法求解最优装载问题。给出以下装载问题的求解过程和结果:

n=5,集装箱重量为w=(5,2,6,4,3),限重为 W=10。

在装载重量相同时最优装载方案是集装箱个数最少的方案。

代码

#include <stdio.h> 
#include <queue> 
using namespace std; 
#define MAXN 21      //最多的集装箱数 

//问题表示 
int n=5; 
int W=10; 
int w[]={0,5,2,6,4,3};     //集装箱重量,不计下标0的元素 

//求解结果表示 
int bestw=0;       //存放最大重量,全局变量 
int bestx[MAXN];      //存放最优解,全局变量 
int Count=1;       //搜索空间中结点数累计,全局变量 

typedef struct  { 
    int no;       //结点编号  
    int i;       //当前结点在解空间中的层次  
    int w;       //当前结点的总重量  
    int x[MAXN];      //当前结点包含的解向量  
    int ub;       //上界 
} NodeType; 

struct Cmp       //队列中关系比较函数 
{ 
    bool operator()(const NodeType &s,const NodeType &t)  
    { 
        return (s.ub<t.ub) || (s.ub==t.ub && s.x[0]>t.x[0]);    //ub越大越优先,当ub相同时x[0]越小越优先  
    } 
}; 

void bound(NodeType &e) //计算分枝结点e的上界 
{   
    int i=e.i+1;
    int r=0;       //r为剩余集装箱的重量  
    while (i<=n)
    {
        r+=w[i];
        i++;
    }  
    e.ub=e.w+r; 
}

void Loading()      //求装载问题的最优解 
{ 
    NodeType e,e1,e2;     //定义3个结点  
    priority_queue<NodeType,vector<NodeType>,Cmp > qu; //定义一个优先队列qu  
    e.no=Count++;      //设置结点编号  
    e.i=0;       //根结点置初值,其层次计为0  
    e.w=0;  
    for (int j=0; j<=n; j++)   //初始化根结点的解向量   
        e.x[j]=0;  
    bound(e);       //求根结点的上界  
    qu.push(e);      //根结点进队  
    while (!qu.empty())    //队不空循环  
    { 
        e=qu.top(); 
        qu.pop();   //出队结点e作为当前结点   
        if (e.i==n)     //e是一个叶子结点   
        { 
            if ((e.w>bestw) || (e.w==bestw && e.x[0]<bestx[0])) //比较找最优解    
            { 
                bestw=e.w;   //更新bestw     
                for (int j=0;j<=e.i;j++)      
                    bestx[j]=e.x[j]; //复制解向量e.x->bestx    
            }   
        }  
        else       //e不是叶子结点   
        { 
            if (e.w+w[e.i+1]<=W)  //检查左孩子结点    
            { 
                e1.no=Count++;  //设置结点编号     
                e1.i=e.i+1;   //建立左孩子结点     
                e1.w=e.w+w[e1.i];     
                for (int j=0; j<=e.i; j++)     
                    e1.x[j]=e.x[j]; //复制解向量e.x->e1.x     
                e1.x[e1.i]=1;   //选择集装箱i     
                e1.x[0]++;   //装入集装箱数增1     
                bound(e1);   //求左孩子结点的上界     
                qu.push(e1);   //左孩子结点进队    
            }    
            e2.no=Count++;   //设置结点编号    
            e2.i=e.i+1;    //建立右孩子结点    
            e2.w=e.w;     
            for (int j=0; j<=e.i; j++) //复制解向量e.x->e2.x     
                e2.x[j]=e.x[j];    
            e2.x[e2.i]=0;    //不选择集装箱i    
            bound(e2);    //求右孩子结点的上界    
            if (e2.ub>bestw)   //若右孩子结点可行,则进队,否则被剪枝     
                qu.push(e2);   
        }  
    } 
} 

void disparr(int x[],int len)   //输出一个解向量 
{
    for (int i=1;i<=len;i++)   
        printf("%2d",x[i]); 
} 

void dispLoading()      //输出最优解 
{ 
    printf("  X=["); 
    disparr(bestx,n);
    printf("],装入总价值为%d\n",bestw); 
} 

int main()
{ 
    Loading();  
    printf("求解结果:\n");  
    dispLoading();     //输出最优解 
} 

以下为我的微信公众号:技术经理的成长

会不定期进行更新,欢迎关注

  • 7
    点赞
  • 52
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
#include #include #include #include using namespace std; ifstream infile; ofstream outfile; class Node { friend int func(int*, int, int, int*); public: int ID; double weight;//物品的重量 }; bool comp1(Node a, Node b) //定义比较规则 { return a.weight > b.weight; } class Load; class bbnode; class Current { friend Load; friend struct Comp2; private: int upweight;//重量上界 int weight;//结点相应的重量 int level;//活结点在子集树中所处的层次 bbnode* ptr;//指向活结点在子集树中相应结点的指针 }; struct Comp2 { bool operator () (Current *x, Current *y) { return x->upweightupweight; } }; class Load { friend int func(int*, int, int, int*); public: int Max0(); private: priority_queue<Current*, vector, Comp2>H;//利用优先队列(最大堆)储存 int limit(int i); void AddLiveNode(int up, int cw, bool ch, int level); bbnode *P;//指向扩展结点的指针 int c;//背包的容量 int n;//物品的数目 int *w;//重量数组 int cw;//当前装载量 int *bestx;//最优解方案数组 }; class bbnode { friend Load; friend int func( int*, int, int, int*); bbnode* parent; bool lchild; }; //结点中有双亲指针以及左儿子标志 int Load::limit(int i) //计算结点所相应重量的上界 { int left,a; left= c - cw;//剩余容量 a = cw; //b是重量上界,初始值为已经得到的重量 while (i <= n && w[i] parent = P; b->lchild = ch; Current* N = new Current; N->upweight = up; N->weight = cw; N->level = level; N->ptr = b; H.push(N); } int Load::Max0() { int i = 1; P = 0; cw = 0; int bestw = 0; int up = limit(1); while (i != n + 1) { int wt = cw + w[i]; //检查当前扩展结点的左儿子结点 if (wt bestw) bestw =wt; AddLiveNode(up,wt, true, i + 1); } up = limit(i + 1); //检查当前扩展结点的右儿子结点 if (up >= bestw)//如果右儿子可行 { AddLiveNode(up,cw, false, i + 1); } Current* N = H.top(); //取队头元素 H.pop(); P = N->ptr; cw = N->weight; up = N->upweight; i = N->level; } bestx = new int[n + 1]; for (int j = n; j > 0; --j) { bestx[j] = P->lchild; P = P->parent; } return cw; } int func(int *w, int c, int n, int *bestx) //调用Max0函数对子集树的优先队列式进行分支限界搜索 { int W = 0; //初始化装载的总质量为0 Node* Q = new Node[n]; for (int i = 0; i < n; ++i) { Q[i].ID = i + 1; Q[i].weight = w[i+1]; W += w[i+1]; } if (W <= c)//如果足够装,全部装入 return W; sort(Q, Q + n, comp1); //首先,将各物品按照重量从大到小进行排序; Load K; K.w = new int[n + 1]; for (int j = 0; j < n; j++) K.w[j + 1] = w[Q[j].ID]; K.cw = 0; K.c = c; K.n = n; int bestp = K.Max0(); for (int k = 0; k < n; k++) { bestx[Q[k].ID] = K.bestx[k + 1]; } delete []Q; delete []K.w; delete []K.bestx; return bestp; } int main() { int*w,*Final; int c,n,i,best; infile.open("input.txt",ios::in); if(!infile) { cerr<<"open error"<>c; infile>>n; w=new int[n+1]; for(i=1;i>w[i]; infile.close(); Final = new int[n+1]; best = func( w, c, n, Final); outfile.open("output.txt",ios::out); if(!outfile) { cerr<<"open error"<<endl; exit(1); } outfile << best << endl; for (int i = 1; i <= n; ++i) { outfile<<Final[i]<<" "; } outfile.close(); return 0; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小爱玄策

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值