You are on page 1of 6

CS 2110 Fall 2012

Homework 2 Scheduling
Due: Saturday, September 29, 11:59PM In this assignment, you will be designing algorithms for scheduling. Scheduling problems are of great importance and the come up in various situations. One common abstraction of scheduling that encompasses several natural occurrences of the problem is the classical scheduling problem on identical processors. We have n tasks, each of which requires a certain amount of time to be completed. Let ti be the positive integer that denotes the amount of time required for task i (i = 0, 1, . . . , n 1). We also have m identical processors, i.e., processors that intuitively operate at the same speed. We have the following restrictions for scheduling the tasks on these processors: A processor can only execute one task at a time. A task has to be executed in full, we cannot pause it and continue it later, or move it to another processor. Once the task i starts, ti time slots will elapse until its execution nishes. The objective we want to optimize is called makespan: it is the time needed until all tasks nish. Our goal is to schedule the tasks in a way so that the makespan is minimum.

0
0.1

Instructions
Grading

Solutions will be graded on correctness, the quality of the algorithms, and style. A correct program compiles without errors or warnings, and behaves according the the requirements given here and in the comments of the code. A program with good style is clear, concise, and easy to read.

0.2

Partners

You can work in groups of at most two people for this assignment. This assignment is harder than the rst one, because you will have to design and implement non-trivial algorithms.

Scheduling

Assume that we have n = 5 tasks (think that we index them from 0). The duration of each task, i.e., the time it requires to be completed, is given as follows: t0 = 7, t1 = 5, t2 = 2, t3 = 4, t4 = 6. task 0 duration 7
CS 2110 Fall 2012

1 5
1/6

2 2

3 4

4 6
Homework 2

Also assume that we have m = 3 identical processors. A schedule is an assignment of each task i to a processor pi and starting time start i so that there is no overlap of tasks on any processor. The following table describes a schedule for our tasks on our processors: Schedule time processor 0 processor 1 processor 2 Try to see how the following information is visualized in the above table: Task 0, which requires 7 time slots, is scheduled at processor 1 with start time 6. Task 1, which requires 5 time slots, is scheduled at processor 0 with start time 0. Task 2, which requires 2 time slots, is scheduled at processor 1 with start time 4. Task 3, which requires 4 time slots, is scheduled at processor 1 with start time 0. Task 4, which requires 6 time slots, is scheduled at processor 0 with start time 5. Any processor is executing at most 1 task at any time slot and all our tasks have been scheduled. Therefore, the above constitutes a valid schedule. Two important denitions: Makespan of a schedule: The total time required for all tasks to nish. Utilization of processors (w.r.t. a schedule): This is the ratio utilization =
n total processing time i=1 ti = . makespan m makespan m

0 1 1 1 3 3

2 1 3

3 1 3

4 5 1 4 2 2

6 4 0

7 4 0

8 9 4 4 0 0

10 11 4 0 0

12

Tasks & Durations task 0 1 5 2 2 3 4 4 6

duration 7

In the above example, the makespan is equal to 12 and utilization is 7+5+2+4+6 24 = = 0.66667 . 12 3 36 The following is another example of a schedule for our tasks and processors: Schedule time 0 1 0 1 4 2 0 1 4 3 4 0 0 1 1 4 4 5 0 3 4 6 0 3 7 8 2 2 3 3 Tasks & Durations task 0 1 5 2 2 3 4 4 6

processor 0 0 processor 1 1 processor 2 4

duration 7

Now, the makespan is 9 and the utilization is 0.88889.

1.1

Some restrictions of our scheduling model

Here, we repeat some of the restrictions that our model has:


CS 2110 Fall 2012 2/6 Homework 2

1. There is at least 1 task to be scheduled, n 1. 2. Every duration ti is a positive integer, i.e., in {1, 2, 3, 4, . . . , }. 3. There is at least one processor.

1.2

Assumptions

For the purposes of our assignment, we make the following assumptions: 1. There is no idle time between tasks in a processor: as soon as a task ends and there are more tasks to be completed on it, another one starts immediately. 2. A schedule is given modulo the order of tasks within any processor. So, a schedule reduces to an assignment of each task to a processor. The order in which the tasks are executed within a processor is immaterial. Notice that the makespan of a schedule is invariant under permutations of tasks within processors.

Project Structure

This assignment mainly involves three classes: ScheduledTask: Objects of this class simply hold the assignment of a task to a processor. Schedule: This class represents a schedule for a given collection of tasks and processors. It includes the durations of the tasks and an assignment of each task to a processor. ScheduleGenerator: The purpose of this class is to hold the methods for calculating (optimal or suboptimal) schedules. An optimal schedule is one that minimizes the makespan. You will only write code inside the classes Schedule and ScheduleGenerator. The class ScheduledTask should not be changed in any way.

Class Schedule

The following have to be implemented in the class Schedule: 1. Method areValid: This method takes an array of integers t and an integer m and checks whether t, m are valid representations for task durations and # processors in our model. See Section 1.1. 2. Method isConsistent: This method has to check whether an array of ScheduledTasks is a valid representation of a schedule. No redundancy is allowed in this array. There are several things that have to be checked here. It is your work is to nd them all.
CS 2110 Fall 2012 3/6 Homework 2

Advice on how to work: We have given you a description of (a mathematical abstraction of) a schedule. We ask you here to check whether a specic data structure is an accurate representation of a schedule. Think that an adversary is trying to give you garbage schedule input in any possible way they can; you have to be able to detect it. Note: Notice the precondition. You can assume that it holds true. 3. Constructor Schedule. 4. Method getMakespan: Calculates the makespan of the schedule. 5. Method getUtilization: Calculates the utilization of the processors w.r.t. the schedule. 6. Method toString: Outputs a String representation of the schedule. This must have a precise form, which we dene with an example: # Tasks: 6 Durations: [7, 5, 2, 4, 6, 3] # Processors: 4 Schedule @0: 0, 1 Schedule @1: 2, 3, 4 Schedule @2: Schedule @3: 5 Makespan: 12 The notation Schedule @p means that we are giving the schedule at processor p. Within a processor, tasks are given in order of increasing id. Note: A change of line is given by the character represented by the escape sequence \n in Java. The string produced by the toString() method does not end with a change of line. More information is given in comments inside the code.

Class ScheduleGenerator

Implementing the class ScheduleGenerator is the hard part of this assignment and requires creativity and careful thinking. We have given almost no code, just a very simple interface you have to adhere to. The following have to be implemented: 1. Constructor ScheduleGenerator. 2. Method getOptSchedule: This method returns a schedule that minimizes the makespan. This is a well-dened problem, because there is always a minimum makespan. It is your work to nd a precise algorithm that nds an optimal schedule, but we give you the following hints that may be of help:
CS 2110 Fall 2012 4/6 Homework 2

There is no known efcient (polynomial-time) solution for the problem. Do not attempt to nd one. We simply want a brute-force search approach. You have to explore all the (reasonable) scheduling possibilities in a principled way. Your algorithm should (recursively) search the space of schedules, maintaining state along the way that records relevant information. A (private) helper method will probably be very useful, e.g. for recursion. Example of an optimal schedule: # Tasks: 5 Durations: [7, 5, 2, 4, 6] # Processors: 3 Schedule @0: 0, 2 Schedule @1: 1, 3 Schedule @2: 4 Makespan: 9 3. Method heuristicScheduling: Since it is (probably) not possible to give an efcient (polynomial-time) solution to the classical scheduling problem, it is useful to relax our optimality restriction. In this method we ask you to implement an algorithm that cannot guarantee the optimal solution, but instead nds a good suboptimal solution. We give an informal description of a greedy heuristic algorithm. The algorithm operates in stages; in each stage you pick a task and you schedule it on some processor. You need to record at every stage and for every processor what its load is, i.e., the total duration of the tasks that have already been assigned to it. Suppose that you are now in a stage, where you are processing task i: Find the processor p with the minimum load so far, and assign i to it. Even though the above paragraph gives the main idea of the greedy heuristic algorithm, there are several details to be lled in and it is your work to do this well. Try to make your implementation as efcient as you can. Note: Some people might observe that heaps could be a useful data structure here. Do not implement a heap. You can simply use arrays and get full points for this part. Example of a suboptimal schedule produced by our implementation of the greedy algorithm we described: # Tasks: 5 Durations: [7, 5, 2, 4, 6] # Processors: 3 Schedule @0: 0 Schedule @1: 1, 4 Schedule @2: 2, 3 Makespan: 11

CS 2110 Fall 2012

5/6

Homework 2

If you want, you can implement another heuristic algorithm besides the greedy one that we describe. If you do so, it has to be of comparable running time. In particular, only polynomial-time solutions are allowed. Explain your algorithm in the REAME.txt le, discuss its running time, and convince us that it is a good approach. Your solution will be subjectively graded based on how good we think it is, we can give you no numbers that have to be met in order to get full points.

Testing

We have provided a few test cases in order to give you some help debugging your program. We give you a le, called huge.txt, which contains a very large instance of the scheduling problem, which is meant to be used for testing your heuristic. You need to place the le in the project folder inside the Eclipse workspace. The optimal utilization for the instance contained in huge.txt is 1.0. Our heuristic algorithm nds a schedule with utilization 0.94. Achieving similar utilization does not ensure you full points. The overall quality of an algorithm cannot be judged based on a few instances. If you cannot obtain as good a utilization factor it does not mean you will not receive full points for a good solution.

Submission

You should compress exactly these les into a zip le that you will then submit on CMS: README.txt: This le should contain your name, your NetID, all known issues you have with your submitted code, and the names of anyone you have discussed the homework with. Also, if you want to explain something about your code that you think needs clarication, you can add a few paragraphs here. ScheduledTask.java Schedule.java ScheduleGenerator.java Do not include any les ending in .class. All .java les should compile and conform to the prototypes we gave you. We write our own classes that use your classes public methods to test your code. Even if you do not use a method we require, you should still implement it for our use.

CS 2110 Fall 2012

6/6

Homework 2

You might also like