使用STL处理分支限界法处理最优装载问题

本文介绍了一种用于解决货物最优装载问题的算法,通过比较三种不同的方法:朴素方法、队列方法和优先级队列方法,展示了如何在限定条件下最大化两个容器的装载效率。通过实际案例分析,对比了各种方法的时间和资源消耗。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 

View Code
  1 #include <iostream>
  2 #include <vector>
  3 #include <queue>
  4 #include <time.h> 
  5 #define MAX_SIZE 100
  6 int SIZE;
  7 using namespace std;
  8 float Object_Weight[MAX_SIZE];
  9 float SUM;
 10 class Node{
 11 public:
 12     float total_weight;    
 13     int level;
 14     Node(){
 15         total_weight = 0;
 16         level = 0;
 17         for(int i=0;i<SIZE;i++)
 18             result[i] = false;
 19     }
 20     Node(const Node& obj){ 
 21         total_weight = obj.total_weight;
 22         level = obj.level;
 23         for(int i=0;i<SIZE;i++)
 24             result[i] = obj.result[i];
 25     }
 26     Node& operator = (const Node &obj){
 27         total_weight = obj.total_weight;
 28         level = obj.level;
 29         for(int i=0;i<SIZE;i++)
 30             result[i] = obj.result[i];
 31         return *this;
 32     }
 33     void set(bool value){ 
 34         result[level-1] = value;    
 35         total_weight = getWeight();
 36     }
 37     float returnWeight(){return total_weight;}
 38     float maxEstWeight();
 39     
 40     void CopyResult(bool* re);
 41 private:
 42     float getWeight();    
 43     bool result[MAX_SIZE];
 44 };
 45 struct cmp{
 46     bool operator()(Node& obj1, Node& obj2){
 47         return obj1.total_weight<obj2.total_weight;
 48     }
 49 };
 50 void Node::CopyResult(bool* re){
 51     for(int i=0;i<SIZE;i++)
 52         re[i] = result[i];
 53 }
 54 float Node::getWeight(){
 55     float sum = 0;
 56     for(int i=0;i<level;i++)
 57     {
 58         if(result[i])
 59             sum += Object_Weight[i]; 
 60     }
 61     return sum;
 62 }
 63 
 64 float Node::maxEstWeight(){
 65     float sum = total_weight;
 66     for(int i=level;i<SIZE;i++)
 67         sum += Object_Weight[i];
 68     return sum;
 69 }
 70 void naiveMethod(float c1,float c2){
 71     float bestWeight = 0;
 72     int counter = 0;
 73     bool* bestResult = new bool(SIZE);
 74     vector<Node> Queue;
 75     Node *a = new Node();
 76     Queue.push_back(*a);
 77     while(Queue.size() != 0){
 78         Node temp(Queue[0]);
 79         Queue.erase(Queue.begin());
 80         if(temp.level != SIZE){
 81             Node left(temp);
 82             Node right(temp);
 83             left.level++;
 84             left.set(false);
 85             right.level++;
 86             right.set(true);
 87             Queue.push_back(left);
 88             Queue.push_back(right);
 89             counter += 2;
 90         }
 91         if( (bestWeight < temp.returnWeight()) && (temp.returnWeight()<=c1) ){
 92             bestWeight = temp.returnWeight();
 93             temp.CopyResult(bestResult);
 94         }
 95     }//while
 96     cout<<"c1 loading result:"<<bestWeight<<endl;
 97     for(int i=0;i<SIZE;i++)
 98         cout<< bestResult[i]<<"    ";
 99     cout<<endl;
100     cout<<"c2 loading result:"<<SUM-bestWeight<<endl;
101     for(int i=0;i<SIZE;i++)
102         cout<<! bestResult[i]<<"    ";
103     cout<<endl;
104     cout<<"Total counter:    "<<counter<<endl;
105 }
106 
107 void queueMethod(int c1, int c2){
108     float bestWeight = 0;
109     int counter = 0;
110     bool* bestResult = new bool(SIZE);
111     vector<Node> Queue;
112     Node *a = new Node();
113     Queue.push_back(*a);
114     while(Queue.size() != 0){
115         Node temp(Queue[0]);
116         Queue.erase(Queue.begin());
117         if( (temp.level != SIZE) && (bestWeight < temp.maxEstWeight() ) ){
118             Node left(temp);
119             Node right(temp);
120             left.level++;
121             left.set(false);
122             right.level++;
123             right.set(true);
124             Queue.push_back(left);
125             Queue.push_back(right);
126             counter += 2;
127         }
128         if( (bestWeight < temp.returnWeight()) && (temp.returnWeight()<=c1) ){
129             bestWeight = temp.returnWeight();
130             temp.CopyResult(bestResult);
131         }
132     }//while
133     cout<<"c1 loading result:"<<bestWeight<<endl;
134     for(int i=0;i<SIZE;i++)
135         cout<< bestResult[i]<<"    ";
136     cout<<endl;
137     cout<<"c2 loading result:"<<SUM-bestWeight<<endl;
138     for(int i=0;i<SIZE;i++)
139         cout<<! bestResult[i]<<"    ";
140     cout<<endl;
141     cout<<"Total counter:    "<<counter<<endl;
142 }
143 
144 
145 void priority_QueueMethod(int c1, int c2){
146     float bestWeight = 0;
147     int counter = 0;
148     bool* bestResult = new bool(SIZE);
149     priority_queue<Node, vector<Node>, cmp> Queue;
150     Node *a = new Node();
151     Queue.push(*a);
152     while(Queue.size() != 0){
153         Node temp(Queue.top());
154         Queue.pop();
155         if( (temp.level != SIZE) && (bestWeight < temp.maxEstWeight() ) ){
156             Node left(temp);
157             Node right(temp);
158             left.level++;
159             left.set(false);
160             right.level++;
161             right.set(true);
162             Queue.push(left);
163             Queue.push(right);
164             counter += 2;
165         }
166         if( (bestWeight < temp.returnWeight()) && (temp.returnWeight()<=c1) ){
167             bestWeight = temp.returnWeight();
168             temp.CopyResult(bestResult);
169         }
170     }//while
171     cout<<"c1 loading result:"<<bestWeight<<endl;
172     for(int i=0;i<SIZE;i++)
173         cout<< bestResult[i]<<"    ";
174     cout<<endl;
175     cout<<"c2 loading result:"<<SUM-bestWeight<<endl;
176     for(int i=0;i<SIZE;i++)
177         cout<<! bestResult[i]<<"    ";
178     cout<<endl;
179     cout<<"Total counter:    "<<counter<<endl;
180 }
181 
182 int main(){
183     float c1,c2;
184     SUM= 0;
185     cout<<"SIZE:"<<endl;
186     cin>>SIZE;
187     cout<<"WEIGHT:"<<endl;
188     for(int i=0;i<SIZE;i++){
189         cin>>Object_Weight[i];
190         SUM += Object_Weight[i];
191     }
192     cout<<"C1:"<<endl;
193     cin>>c1;
194     cout<<"C2:"<<endl;
195     cin>>c2;
196     if(c1+c2<SUM)
197     {
198         cout<<"No solution!"<<endl;
199         return EXIT_SUCCESS;
200     }
201     if(SUM<c1 || SUM<c2)
202     {
203         cout<<"Need only one ship!"<<endl;
204         return EXIT_SUCCESS;
205     }
206     time_t start ,end ;  
207     double cost;  
208     start = clock();  
209     naiveMethod(c1, c2);
210     end = clock();  
211     cost=difftime(end,start);  
212     cout<<"///////////////\nNaive method time:    "<<cost<<"\n///////////////"<<endl;
213     start = clock();  
214     queueMethod(c1,c2);
215     end = clock();  
216     cost=difftime(end,start);  
217     cout<<"///////////////\nQueue method time:    "<<cost<<"\n///////////////"<<endl;
218     start = clock();  
219     priority_QueueMethod(c1,c2);
220     end = clock();  
221     cost=difftime(end,start);  
222     cout<<"///////////////\nPriority queue method time:    "<<cost<<"\n///////////////"<<endl;
223     return EXIT_SUCCESS;
224 }

 

 

转载于:https://www.cnblogs.com/wangbiaoneu/p/OptLoading.html

#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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值