Dynamic planning of cutting steel bars

Source: Internet
Author: User
Tags truncated

1. Description of the problemA company buys long steel strips and cuts them for short steel strips, assuming the cut has no cost, and the company wants to know the best cutting solution! Let's say we know that the price of a steel bar of length i is pi (i = ...), the bar length is whole inch, and a price list is given below:
length i 1  2  3  4  5  6  7  8  9  10
Price PI 1 5 8 9 10 17 17 20 24 30
given a steel bar of length n and a price list (i =1, 2,3...N), the scheme for cutting steel bars, so that the profit is the largest, can not be cut
2. Dynamic PlanningThe dynamic programming is similar to the divide-and-conquer method, which solves the problem by solving the sub-problem and finally finding out the solution of the original question. As long as the partition method is to decompose the original problem into several independent sub-problems, the solution of the original problem is solved by sub-problem. Dynamic programming, on the contrary, is used to solve the overlapping of sub-problems, that is, different sub-problems have the same common sub-problem, in this case, the division of the law will take a lot of time to calculate the already calculated part of the overlapping sub-problem, the efficiency is greatly reduced, and dynamic planning weigh time and space factors, Put the calculated sub-problem into a table, the next time you encounter the same sub-problem, you can get the results by directly checking the table, greatly speeding up the speeddynamic programming is often used to solve optimization problems, in general, the optimal solution of a problem has multiple, dynamic programming can solve one of the optimal solution, rather than the optimal solutionwe typically design a dynamic planning problem in the following four steps:1. Describe the structural characteristics of an optimal solution. 2. Recursive definition of the value of the optimal solution3. When calculating the optimal solution, the bottom-up method is generally used to solve4. Using the computed information to construct an optimal solutionlet's consider how to calculate an optimal solution with dynamic programming.
3. Problem Analysisthere are 2^ (n-1) cutting methods for steel bars of length n, since each node with a length of 1 can be either cut or not cutthen we assume that the length n of the steel bar is cut into K bars, the maximum profit:n = i1+i2+i3+...+ik;ri = Pi1+pi2+pi3+...+pik;then the maximum profit is:rn = max (pn, R1+rn-1, r2+rn-2, ..., ri+rn-i, rn-1+r1);(p N is the profit at the time of not cutting, ri+rn-i to cut at node I, RI is the maximum profit of the steel bar of length I, Rn-i is the maximum profit for the length of n-i steel bars!)! here I am. The optimal solution of the original problem is formed by combining the optimal solution of two related sub-problems and selecting the maximal profit at all possible cutting points. we call the steel bar cutting problem to satisfy the properties of the optimal substructure: the optimal solution of the problem is composed of the optimal solution of the related sub-problem, and the sub-problem can be solved independently! we might as well simplify the above problem and get a structure that simplifies the optimal solution:rn = max (p1+rn-1, p2+rn-2,..., pn+r0);
4. Code implementationTop-down code implementation:
#include <iostream> #include <string> #include <limits.h>using namespace std; #define Kind_size 11/**  The base price for each length */int price[]={0,1,5,8,9,10,17,17,20,24,30};int dealmaxprofit (int n, int maxprofit[]);/** * Get maximum PROFIT * @param n    Steel Bar Length * @return Maximum profit */int getmaxprofit (int n) {if (N < 0 | | n > kind_size) return 0;       int maxprofit[kind_size];//record the maximum profit for each length is how many for (int i = 0; i < kind_size; ++i) {maxprofit[i] = int_min;    } Maxprofit[0] = 0;    Dealmaxprofit (n, Maxprofit); return maxprofit[n];} /** * The maximum profit for each length corresponding to n small steel bars is kept in Maxprofit * @param n Steel Strip length * @param maxprofit The maximum profit of the array * @return return the maximum length of n    Run */int dealmaxprofit (int n, int maxprofit[]) {if (n = = 0) return 0;    /** said that it had been counted before */if (maxprofit[n]!= int_min) return maxprofit[n];    /** didn't count so much. */int max = Int_min;         for (int i = 1; i < n+1; ++i) {int temp = Price[i]+dealmaxprofit (n-i, Maxprofit); if (Max < TEMP) max = temp;    } Maxprofit[n] = max; return Max;}         int main (int argc, char const *argv[]) {while (1) {int steelbarlen;        cout<< "Enter The Steel Bar Length (0-10) >";        Cin >> Steelbarlen;    cout<< "Max profit is:" <<getmaxprofit (Steelbarlen) <<endl; } return 0;}

Bottom-up code implementation:
#include <iostream> #include <limits.h> #include <string>using namespace std; #define Kind_size 11/** This is the unit price per length */int price[]={0,1,5,8,9,10,17,17,20, 30};int dealmaxprofit (int n, int maxprofit[]);/** * Get maximum PROFIT * @par    The length of the AM N steel bar * @return Maximum profit */int getmaxprofit (int n) {if (N < 0 | | n > kind_size) return 0;    int maxprofit[kind_size];    for (int i = 0; i < kind_size; ++i) {maxprofit[i] = int_min;    } Maxprofit[0] = 0;    Dealmaxprofit (n, Maxprofit); return maxprofit[n];} /** * Handle the profit of <n length * @param n length of the steel bar * @param maxprofit maximum PROFIT * @return corresponds to the maximum profit of the length n */int dealmaxpro    Fit (int n, int maxprofit[]) {if (n = = 0) return 0;        for (int i = 1; I <= n; ++i) {int max = int_min;            for (int j = 1; J <= i; ++j) {//each time the optimal substructure is obtained, then the upper layer int temp = price[j]+maxprofit[i-j];        if (Max < temp) max = temp;   } Maxprofit[i] = max; }}int Main (int argc, char const *argv[]) {while (1) {int steelbarlen;        cout<< "Enter The Steel Bar Length (0-10) >";        Cin >> Steelbarlen;    cout<< "Max profit is:" <<getmaxprofit (Steelbarlen) <<endl; } return 0;}

But the code above only solves the ultimate maximum profit, and does not work out a specific solutionwe might as well look at the structure of the optimal solution:rn = max (p1+rn-1, p2+rn-2,..., pn+r0);The optimal solution is the optimal solution of sub-problem, the steel bar is either cut at length I, or not cut, all we use an array to save the corresponding length n should be truncated from position I (not truncated case i=n) datanamely devidepos[n]=i;so we improved the algorithm above so that it could find out the truncated scheme
Top-down code implementation:
/** * This is the top-down approach  */#include <iostream> #include <string> #include <limits.h> # Include <stdio.h>using namespace std, #define Kind_size 11/** basic price per length */int price[]={ 0,1,5,8,9,10,17,17,20,24,30};int Dealmaxprofit (int n, int maxprofit[], int devidepos[]);/** * get maximum Profit  * @ param  n Steel Bar length  * @return   Maximum profit  */int getmaxprofit (int n, int devidepos[]) {      if (N < 0 | | n > kind_size)         return 0;    int maxprofit[kind_size];//record the maximum benefit per length Run is how much     for (int i = 0; i <  kind_size; ++i)     {        Maxprofit[i] = i nt_min;        Devidepos[i] = i;    }    maxprofit[0] = 0;    deal Maxprofit (n, Maxprofit, Devidepos);    return maxprofit[n];} /** * the maximum profit for each length corresponding to n small steel bars is stored in Maxprofit  * @param n         steel strip length  * @param maxprofit Save MaximumArray of profit  * @return           Returns the maximum profit of n  */int dealmaxprofit (int n, int maxprofit[], int Devidepos[]) {    if (n = = 0)         return 0;   /** indicates that the */    if (  maxprofit[n]!= int_min)          return maxprofit[n];   /** not counted so even once */     int max = int_min;    int pos = n;xia    for (int i = 1; i < n+1; ++i)     {         INT temp = Price[i]+dealmaxprofit (n-i, Maxprofit  ,devidepos);         if (max < temp)         {            max = temp;        & nbsp   pos = i;       }   }    Maxprofit[n] = max;    Devidepos[n] = P os;    return Max;} void Printcutsolution (int n, int devidepos[]) {    if (N < 0 | | n >= kind_size)     &NBSP   Return;    if (n = = Devidepos[n])     {        printf ("%s\n", "Not Devide" );        return;   }    printf ("%d steel bar devide into%d and%d", N, N- Devidepos[n], Devidepos[n]);    printcutsolution (N-devidepos[n], devidepos);} int main (int argc, char const *argv[]) {    while (1)     {          INT D evidepos[kind_size];        int steelbarlen;         cout<< "Enter the Steel Bar Length (0-10) > ";        CIN >> steelbarlen;        cout<<" Max profit is: "<<getmaxprofit (Steelbarlen, Devidepos) <<endl;        Printcutsolution ( Steelbarlen, Devidepos);   }    return 0;}

Bottom-Up Code implementation:
#include <iostream> #include <limits.h> #include <stdio.h> #include <string>using namespace std ; #define Kind_size 11/** This is the unit price per length */int price[]={0,1,5,8,9,10,17,17,20, 30};int dealmaxprofit (int n, int maxprofi t[], int devidepos[]);/** * Get maximum PROFIT * @param n length of Steel bar * @return Maximum profit */int getmaxprofit (int n, int devidepos[]) {i    F (N < 0 | | n > kind_size) return 0;    int maxprofit[kind_size];        for (int i = 0; i < kind_size; ++i) {maxprofit[i] = int_min;    Devidepos[i] = i;    } Maxprofit[0] = 0;    Dealmaxprofit (n, Maxprofit, Devidepos); return maxprofit[n];} /** * Handle the profit of <n length * @param n length of the steel bar * @param maxprofit maximum PROFIT * @return corresponds to the maximum profit of the length n */int dealmaxpro    Fit (int n, int maxprofit[], int devidepos[]) {if (n = = 0) return 0;        for (int i = 1; I <= n; ++i) {int max = int_min;        int pos=i;          for (int j = 1; J <= i; ++j) {//each time the optimal sub-structure is obtained, then the upper  int temp = Price[j]+maxprofit[i-j];                if (Max < temp) {max = temp;            pos = j;        }} Devidepos[i] = pos;    Maxprofit[i] = max;    }}void printcutsolution (int n, int devidepos[]) {if (N < 0 | | n >= kind_size) return;        if (n = = Devidepos[n]) {printf ("%s\n", "not Devide");    Return    } printf ("%d steel bar devide into%d and%d", N, N-devidepos[n], devidepos[n]); Printcutsolution (N-devidepos[n], devidepos);}        int main (int argc, char const *argv[]) {while (1) {int devidepos[kind_size];         int Steelbarlen;        cout<< "Enter The Steel Bar Length (0-10) >";        Cin >> Steelbarlen;        cout<< "Max profit is:" <<getmaxprofit (Steelbarlen, Devidepos) <<endl;    Printcutsolution (Steelbarlen, Devidepos); } return 0;}

Before I was describing the problem, assuming that the cutting rods are not cost-consuming, assuming that the condition is now changed, the steel bar every cut, the cost of C, then the best way to refuse? In fact, this problem is similar to the above problem, we only use each minus the cost of C can be, the code is implemented as follows:
#include <iostream> #include <limits.h>using namespace std, #define kind_size 11#define cost 2/** cut costs *//** per The basic price of the kind of */int price[]={0,1,5,8,9,10,17,17,20,24,30};int dealmaxprofit (int, int *); int getmaxprofit (int n) {if (n <= 0 | |    n >= kind_size) return 0;    int maxprofit[kind_size];    for (int i = 0; I <KIND_SIZE; ++i) {maxprofit[i] = int_min;    } dealmaxprofit (n, Maxprofit); return maxprofit[n];}    int dealmaxprofit (int n, int maxprofit[]) {maxprofit[0] = 0;        for (int i = 1; i < kind_size; ++i) {int max = int_min;            for (int j = 1; j < i; ++j) {int maxtemp = Price[j]+maxprofit[i-j]-cost;        if (Max < maxtemp) max = maxtemp;        } max = max>price[i]?max:price[i];    Maxprofit[i] = max; } return maxprofit[n];}         int main (int argc, char const *argv[]) {while (1) {int steelbarlen; cout<< "Enter The Steel Bar Length (0-10) > ";        Cin >> Steelbarlen;    cout<< "Max profit is:" <<getmaxprofit (Steelbarlen) <<endl; } return 0;}





Dynamic planning of cutting steel bars

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.