DP34 Assembly Line Scheduling @ geeksforgeeks

Source: Internet
Author: User

A car factory has two assembly lines, each with n stations. A station is denoted by Si, j where I is either 1 or 2 and indicates the assembly line the station is on, and j indicates the number of the station. the time taken per station is denoted by ai, j. each station is dedicated to some sort of work like engine fitting, body fitting, painting and so on. so, a car chassis must pass through each of the n stations in order before exiting the factory. the parallel stations of the two assembly lines perform the same task. after it passes through station Si, j, it will continue to station Si, j + 1 unless it decides to transfer to the other line. continuing on the same line incurs no extra cost, but transferring from line I at station j-1 to station j on the other line takes time ti, j. each assembly line takes an entry time ei and exit time xi which may be different for the two lines. give an algorithm for computing the minimum time it will take to build a car chassis.

The below figure presents the problem in a clear picture:

The following information can be extracted from the problem statement to make it simpler:

    Two assembly lines, 1 and 2, each with stations from 1 to n. A car chassis must pass through all stations from 1 to n in order (in any of the two assembly lines ). i. e. it cannot jump from station I to station j if they are not at one move distance. the car chassis can move one station forward in the same line, or one station diagonally in the other line. it incurs an extra cost ti, j to move to station j from line I. no cost is incurred for movement in same line. the time taken in station j on line I is ai, j. si, j represents a station j on line I.

    Breaking the problem into smaller sub-problems:
    We can easily find the ith factorial if (I-1) th factorial is known. Can we apply the similar funda here?
    If the minimum time taken by the chassis to leave station Si, J-1 is known, the minimum time taken to leave station Si, j can be calculated quickly by combining ai, j and ti, j.

    T1 (j)Indicates the minimum time taken by the car chassis to leave station j on assembly line 1.

    T2 (j)Indicates the minimum time taken by the car chassis to leave station j on assembly line 2.

    Base cases:
    The entry time ei comes into picture only when the car chassis enters the car factory.

    Time taken to leave first station in line 1 is given:
    T1 (1) = Entry time in Line 1 + Time spent in station S1, 1
    T1 (1) = e1 + a1, 1
    Similarly, time taken to leave first station in line 2 is given:
    T2 (1) = e2 + a2, 1

    Recursive Relations:
    If we look at the problem statement, it quickly boils down to the below observations:
    The car chassis at station S1, j can come either from station S1, J-1 or station S2, J-1.

    Case #1: Its previous station is S1, J-1
    The minimum time to leave station S1, j is given:
    T1 (j) = Minimum time taken to leave station S1, J-1 + Time spent in station S1, j
    T1 (j) = T1 (J-1) + a1, j

    Case #2: Its previous station is S2. J-1
    The minimum time to leave station S1, j is given:
    T1 (j) = Minimum time taken to leave station S2, J-1 + Extra cost incurred to change the assembly line + Time spent in station S1, j
    T1 (j) = T2 (J-1) + t2, j + a1, j

    The minimum time T1 (j) is given by the minimum of the two obtained in cases #1 and #2.
    T1 (j) = min (T1 (J-1) + a1, j), (T2 (J-1) + t2, j + a1, j ))
    Similarly the minimum time to reach station S2, j is given:
    T2 (j) = min (T2 (J-1) + a2, j), (T1 (J-1) + t1, j + a2, j ))

    The total minimum time taken by the car chassis to come out of the factory is given:
    Tmin = min (Time taken to leave station Si, n + Time taken to exit the car factory)
    Tmin = min (T1 (n) + x1, T2 (n) + x2)

    Why dynamic programming?
    The above recursion exhibits overlapping sub-problems. There are two ways to reach station S1, j:

      From station S1, j-1From station S2, J-1

      So, to find the minimum time to leave station S1, j the minimum time to leave the previous two stations must be calculated (as explained in above recursion ).
      Similarly, there are two ways to reach station S2, j:

        From station S2, j-1From station S1, J-1

        Please note that the minimum times to leave stations S1, J-1 and S2, J-1 have already been calculated.

        So, we need two tables to store the partial results calculated for each station in an assembly line. The table will be filled in bottom-up fashion.

        Note:
        In this post, the word "leave" has been used in place of "reach" to avoid the confusion. since the car chassis must spend a fixed time in each station, the word leave suits better.


        The typical example of DP is pipeline scheduling. The goods arriving at an assembly point can be either from this assembly line or from another assembly line, depending on which one is more cost-effective.

        The following is an example:

        Output:

        35


        The bold line shows the path covered by the car chassis for given input values.

        Exercise:
        Extend the above algorithm to print the path covered by the car chassis in the factory.


        Package DP; public class AssemblyLineScheduling {public static void main (String [] args) {int [] [] a = {4, 5, 3, 2}, {2, 10, 1, 4 }}; int [] [] t = {0, 7, 4, 5}, {0, 9, 2, 8 }}; int [] e = {10, 12}; int [] x = {18, 7}; System. out. println (carAssemblyDP (a, t, e, x); System. out. println (carAssembly (a, t, e, x);} public static int carAssembly (int [] [] a, int [] [] t, int [] e, int [] x) {int n = a [0]. length-1; return Math. min (carAssemblyRec (a, t, e, x, n, 0) + x [0], carAssemblyRec (a, t, e, x, n, 1) + x [1]);} public static int carAssemblyRec (int [] [] a, int [] [] t, int [] e, int [] x, int n, int line) {if (n = 0) {return e [line] + a [line] [0];} int T0 = Integer. MAX_VALUE; int T1 = Integer. MAX_VALUE; if (line = 0) {// The following only applies to line0T0 = Math. min (carAssemblyRec (a, t, e, x, n-1, 0) + a [0] [n], // carAssemblyRec (a, t, e, x, n-1, 1) + t [1] [n] + a [0] [n]); //} else if (line = 1) from another line {// The following only applies to line1T1 = Math. min (carAssemblyRec (a, t, e, x, n-1, 1) + a [1] [n], // carAssemblyRec (a, t, e, x, n-1, 0) + t [0] [n] + a [1] [n]); // return Math from another line. min (T0, T1);} public static int carAssemblyDP (int [] [] a, int [] [] t, int [] e, int [] x) {int n = a [0]. length; int [] T1 = new int [n]; int [] T2 = new int [n]; t1 [0] = e [0] + a [0] [0]; T2 [0] = e [1] + a [1] [0]; for (int I = 1; I
            
             

        Let's talk about the recursion of this question. At first I wrote it like this:

        public static int carAssemblyRec(int[][] a, int[][] t, int[] e, int[] x, int n, int line){if(n == 0){return e[line] + a[line][0];}int T0 = Math.min(carAssemblyRec(a, t, e, x, n-1, 0) + a[0][n], carAssemblyRec(a, t, e, x, n-1, 1) + t[1][n] + a[0][n]);int T1 = Math.min(carAssemblyRec(a, t, e, x, n-1, 1) + a[1][n], carAssemblyRec(a, t, e, x, n-1, 0) + t[0][n] + a[1][n]);return Math.min(T0, T1);}

        The results are different from those of DP, so it took a lot of effort to find out where the problem occurred for a long time. After a single-step trace by debugger, we found that we forgot to add restrictions !!!

        The correct statement should be:

        Public static int carAssemblyRec (int [] [] a, int [] [] t, int [] e, int [] x, int n, int line) {if (n = 0) {return e [line] + a [line] [0];} int T0 = Integer. MAX_VALUE; int T1 = Integer. MAX_VALUE; if (line = 0) {// The following only applies to line0T0 = Math. min (carAssemblyRec (a, t, e, x, n-1, 0) + a [0] [n], // carAssemblyRec (a, t, e, x, n-1, 1) + t [1] [n] + a [0] [n]); //} else if (line = 1) from another line {// The following only applies to line1T1 = Math. min (carAssemblyRec (a, t, e, x, n-1, 1) + a [1] [n], // carAssemblyRec (a, t, e, x, n-1, 0) + t [0] [n] + a [1] [n]); // return Math from another line. min (T0, T1 );}



        Http://www.geeksforgeeks.org/dynamic-programming-set-34-assembly-line-scheduling/


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.