CS 141 Programming Assignment #2 Set: Feb. 15, 2008 Due: March 3., 11:59am, 2008 Problem description: The goal of this assignment is to have you implement a dynamic programming algorithm for assembly-line scheduling as described in [CLRS, Ch. 15.1, pp. 324-330; see the handout], with the following specification: 1) In this problem, we have 2 assembly lines (i = 1 or 2). Each line has n stations, numbered j = 1,2,...,n. We denote the jth station on line i by s[i,j]. The jth station on line 1 performs the same function as the jth station on line 2. However, the time required at each station varies. The assembly time required at station s[i,j] is represented as a[i,j]. 2) Normally, once a chassis enters an assembly line, it passes through that line only. However, if we want an automobile to be manufactured as quickly as possible, we may let it pass through the n stations in order, but the partially-completed auto can be switched from one assembly line to another after any station. The time to transfer a chassis away from line i after having gone through station s[i,j] is t[i,j], where i=1,2 and j=1,2,...,n-1. For convenience, let t[i,0] the entry time for the chassis to enter assembly line i and t[i,n] the exit time for an completed auto to exit assembly line i. (See also Figure 15.1 in CLRS p. 325.) Note that t[i,0] and t[i,n] are denoted as e[i] and x[i] in the book. 3) The problem is to determine which stations to choose from line 1 and which to choose from line 2 in order to minimize the total time through the assembly line for an auto. The straightforward way would require Theta(2^n) time, which is infeasible when n is large. Your task is to solve this problem in Theta(n) time using the dynamic programming technique. 4) Let f[i,j] denote the fastest possible time to get a chassis from the starting point through station s[i,j]. The ultimate goal is to determine the fastest time to get a chassis all the way through the assembly line, defined as fastest = min(f[1,n] + t[1,n], f[2,n] + t[2,n]), Given that f[1,1] = t[1,0] + a[1,1] and f[2,1] = t[2,0] + a[2,1], we have the recurrences: f[1,j] = min(f[1, j-1] + a[1,j], f[2, j-1] + t[2, j-1] + a[1,j]) f[2,j] = min(f[2, j-1] + a[2,j], f[1, j-1] + t[1, j-1] + a[2,j]) for j = 2,3,...n. 5) You also need to keep track of the sequence of stations used in the fastest (optimal) path through the assembly line. To help you keep track of such an optimal solution, let us use l[i,j] to store the line number (1 or 2) of the (j-1)th station used in a fastest path through station s[i,j], for each j = 2,...,n, and l* denote the line whose nth station is used in a fastest solution. Using the algorithm given in CLRS, p. 329, write a C++ program to compute the fastest time to complete the assembly line, as well as to output an optimal sequence of stations used in the fastest assembly. Detailed specifications of the (C++) program: a) The program will receive the input from a text file in the following format: n a[1,1] a[1,2] a[1,3] a[1,4] ... a[1,n] a[2,1] a[2,2] a[2,3] a[2,4] ... a[2,n] < one blank line > t[1,0] t[1,1] t[1,2] t[1,3] ... t[1,n] t[2,0] t[2,1] t[2,2] t[2,3] ... t[2,n] where n is a positive integer indicating the total number of the stations. The rest of the numbers are real numbers. The next 2 lines are the assembly time at each station on each assembly line. The last 2 lines are the cost of transferring from one line to the other after station s[i,j], as well the entry and exit times. There is one blank line separating the set of assembly times and the set of transfer times. A single space separates numbers on each line. You may assume that there is only one set of numbers in each file (i.e., n, the a[i,j]'s, and the t[i,j]'s). A sample input file is below (corresponding to the example given in CLRS p.326 Figure 15.2). However, your program should work on any file in the format given above. 6 7 9 3 4 8 4 8 5 6 4 5 7 2 2 3 1 3 4 3 4 2 1 2 2 1 2 b) The program should print the result to an output file whose name can be prescribed at the command line. In other words, we should be able to run your program with the following command line: $ as2 where is the actual name of the input file, the will be the name of the output file, and as2 will be the name of the executable file of your program. c) The output file should give the following information for each file: 1) The fastest time computed by your program, and 2) The optimal sequence of the stations used, printed in the increasing order of the station numbers. For example, a sample output for the above input could be: 38 station 1, line 1 station 2, line 2 station 3, line 1 station 4, line 2 station 5, line 2 station 6, line 1 Other important requirements: 1. Your program should be executable under the Linux environment. Make sure you use a 'makefile' to compile and link your c++ code, and all c++ source code files are '*.cc' or '*.h' files. information about programming under linux can be found at http://www.cs.ucr.edu/~klick/tutorials.htm 2. The executable name of your program must be 'as2'. 3. Your program should have separate files for the main program, prototypes of Your functions, classes and their definitions. 4. You should turn in the following files: the C++ source files, makefile, and readme file. 5. The readme file should explain the data structures used in your program, as well as a summary of the results of your program on the four test datasets provided. 6. Do not forget to provide a makefile so that the grader only needs to type 'make' at the command line to compile your program. WITH NO FUNCTIONAL MAKEFILE, YOU WILL GET ZERO POINT. 7. Include your name, UCR ID number, login, lab section, and assignment number in each and EVERY file. 8. The readability of your program also counts. Use a proper style and add comments where appropriate. Outline of grading scheme: * Soundness, efficiency, and style of the program : 50 points * Correct output : 40 points * README file : 10 points ====================================================================== Total : 100 points