Problem Description:
There are a batch of n containers to be loaded with 2 load capacity of C1 and C2 respectively, of which the weight of container I is WI, and can not exceed.
Algorithm idea:
Optimal loading scheme: Fill the first steamer as full as possible, then load the remaining cargo on the second ship.
Algorithm Description:
Template <class type>class loading{ friend type maxloading (Type [],type,int); Private: void Backtrack (int i); int n; Type * W,C,CW,BESTW;}; Template <class type>void loading<type>::backtrack (int i) { if (i>n) { if (CW>BESTW) BESTW = CW; return; } if (Cw+w[i] <= c) { CW + = W[i]; Backtrack (i+1); CW-= W[i]; } Backtrack (i+1);} Template <class type>type maxloading (Type w[],type c,int N) { loading<type> X; X.W = W; X.C = C; X.N = n; X.BESTW = 0; X.CW = 0; X.backtrack (1); return X.BESTW;}
Upper bound function:
An upper bound function is introduced to cut out subtrees that do not contain the optimal solution:
Template <class type>class loading{ friend type maxloading (Type [],type,int); Private: void Backtrack (int i); int n; Type * W, C, CW, BESTW, r;//remaining container weight};template <class type>void loading<type>::backtrack (int i) { if (i>n) { if (CW>BESTW) BESTW = CW; return; } r-=w[i];//calculate the weight of the remaining container if (Cw+w[i] <= c) { cw + = W[i ]; Backtrack (i+1); CW-= W[i]; } Backtrack (i+1); r+=w[i];//if the optimal solution is not available, then the current container is canceled, indicating that the remaining capacity is added to the current container weight}template <class type>type maxloading (Type w[],type c,int N ) { loading<type> X; X.W = W; X.C = C; X.N = n; X.BESTW = 0; X.CW = 0; X.R = 0; for (int i=1;i<=n;i++)//Calculate the total remaining container weight X.R + = W[i]; X.backtrack (1); return X.BESTW;}
Structure Optimal Solution:
In order to construct the optimal solution, the record of the optimal solution must be saved in the algorithm. So it takes two member array x, BESTX, one for recording the current selection, and one for recording the optimal record.
The improved algorithm is described as follows:
Template <class type>class loading{friend type maxloading (Type [],type,int); Private:void Backtrack (int i); int N, * x, * BESTX; Type * W, C, CW, BESTW, r;//remaining container weight};template <class type>void LOADING&L T Type>::backtrack (int i) {if (i>n) {if (CW>BESTW) {for (j=1;j<=n;j++) BESTX[J] = X[j]; BESTW = CW; } return; r-=w[i];//calculates the weight of the remaining container if (Cw+w[i] <= c) {x[i] = 1; CW + = W[i]; Backtrack (i+1); CW-= W[i]; } if (Cw+r > Bestw) {x[i] = 0; Backtrack (i+1); r+=w[i];//if the optimal solution is not available, then the current container is canceled, indicating that the remaining capacity is added to the current container weight}template <class type>type maxloading (Type w[],type c,int N) {loading<type> X; X.W = W; X.C = C; X.N = n; X.BESTX = BESTX; X.BESTW = 0; X.CW = 0; X.R = 0; for (int i=1;i<=n;i++)//Calculate the total remaining container weight X.R + = W[i]; X.backtrack (1); delete []x,x; return X.BESTW;}
Iterative Backtracking methods:
Using the information contained in array x, the above method can be represented as a non-recursive form. Eliminates O (n) recursive stack space.
template <class type>type maxloading (Type w[],type c,int n,int bestx[]) {//iterative backtracking, returning the optimal load and its corresponding solution, initializing the root node int i = 1; int *x = new Int[n+1]; Type BESTW = 0, cw = 0, r = 0; for (int j=1;j<=n;j++) R+=W[J]; while (true) {while (i<=n && cw+w[i]<=c) {R-= W[i]; CW +=w[i]; X[i] = 1; i++; } if (I>n) {for (int j=1;j<=n;j++) bestx[j] = X[j]; BESTW = CW; } else {R-= W[i]; X[i] = 0; i++; } while (Cw+w[i] <= bestw) {i--; while (i>0 &&!x[i]) {r+=w[i]; i--; } if (i = = 0) {delete[] x; return BESTW; } X[i] = 0; CW-= W[i]; i++; } }}
Backtracking-Loading problems