Optimal Scheduling of Independent Tasks

Source: Internet
Author: User

Problem: Independent task optimal scheduling, also known as dual-Machine Scheduling Problem: Two processors A and B are used to process n jobs. Set the time required for the I job to be handed over to machine A for processing as a [I]. If machine B processes the job, the time required is B [I]. Currently, each job must be processed by only one machine, and each machine cannot process two jobs at the same time. Design a dynamic planning algorithm so that the two machines can process the n jobs in the shortest time (from the start of any machine to the end of the machine ). Study an example: n = 6, A = {2, 5, 7, 10, 5, 2}, B = {3, 8, 4, 11, 3, 4 }.

Analysis: When K jobs are completed, it takes X time to set machine a. The minimum time consumed by machine B must be a function of X, if f [k] [x] is set, it indicates the minimum time spent by machine B. ThenF [k] [x] = min {f [k-1] [x] + B [K], F [k-1] [x-A [k]}WhereF [k-1] [x] + B [k] indicates that the K job is handled by machine B (the time it takes for machine A to complete the K-1 job is still X ), f [k-1] [x-A [k] indicates that the K job is processed by machine A (when the K-1 job is completed, machine a spends X-A [k]).

If a single point is greater than Max (x, F [k] [x]), it indicates the total time required (that is, when machine a spends x time. The time spent by machine a X is changed, that is, x =, 2 ...... X (max) (in theory, the value of X is discrete, but for programming convenience, it is set to an integer continuous), thus forming a sequence of vertices that are less complex. The overall time is required to be the shortest. The smallest of These vertices than the sequence is.

The difficulty is that B is a function expression of A, that is, dynamic planning. It takes some time to understand the expressions, and think about it. The subsequent programming implementation is fully based on this idea. Use the enumeration examples of the first two tasks to help you understand them.

Example: The first two job examples are sufficient.

Initialize the First Job: The subscript starts with 0.

First, all possible values of the time consumed by machine A are: 0 <= x <= A [0].
If X is set to <0, F [0] [x] = ∞, max (x, ∞) = ∞. For the meaning of the method, see.
If x = 0 and f [0] [0] = 3, max () = 3. Machine A takes 0 and machine B takes 3, at this time, the time required for the two machines is 3;
When x = 1, F [0] [1] = 3, max (1, 3) = 3;
If X = 2, F [0] [2] = 0, max () = 2;

In the above point-to-point sequence, we can see that when x = 2, it takes at least 2 time to complete the first job, and machine A takes 2 time, machine B takes 0 time.

Let's look at the second job.:

First, the value range of X is: 0 <= x <= (a [0] + A [1]).
When X is <0, note f [1] [x] = ∞; this method is used for programming, because the array subscript cannot be less than 0. The actual meaning here is: X represents the time when machine a completes the first two jobs. A [1] is the time when machine a completes 2nd jobs, if x <A [1], 2nd jobs are processed by machine B, that is, the former is used in min.

X = 0, then f [1] [0] = min {f [0] [0] + B [2], f [0] [0-a [1]} = min {3 + 8, ∞} = 11, and then max () = 11;
If x = 1, F [1] [1] = min {f [0] [1] + B [2], f [0] [1-A [1]} = min {3 + 8, ∞} = 11, and then max (11) = 11;
If X = 2, F [1] [2] = min {f [0] [2] + B [2], f [0] [2-A [1]} = min {0 + 8, ∞} = 8, and then max (2, 8) = 8;
If X = 3, F [1] [3] = min {f [0] [3] + B [2], f [0] [3-A [1]} = min {0 + 8, ∞} = 8, and then max (3, 8) = 8;
If X = 4, F [1] [4] = min {f [0] [4] + B [2], f [0] [4-A [1]} = min {0 + 8, ∞} = 8, and then max (4, 8) = 8;
If X = 5, F [1] [5] = min {f [0] [5] + B [2], f [0] [5-A [1]} = min {0 + 8, 3} = 3, and then max (5, 3) = 5;
If X = 6, F [1] [6] = min {f [0] [6] + B [2], f [0] [6-A [1]} = min {0 + 8, 3} = 3, and then max (6, 3) = 6;
X = 7, then f [1] [7] = min {f [0] [7] + B [2], f [0] [7-A [1]} = min {0 + 8, 0} = 0, and then max () = 7;

In the above point-to-point sequence, we can see that when x = 5, it takes at least 5 time to complete two jobs and machine A takes 5 time, machine B takes 3 time.

Algorithm time complexity: According to the above idea, programming is implemented. The result is that the algorithm time complexity is O (n * sum), where sum is the minimum value of the sum of all elements in a and the sum of all elements in B. The time complexity of the algorithm design and experiment solution provided by Wang Xiaodong is O (M * m * n), where M is the maximum value of all elements A and B.
Comparison: sum must be much smaller than N * M. Therefore, N * sum <n * m. In contrast, O (N * m) is not limited, it seems that this idea is still outstanding ......

This is an assignment in the algorithm class last year. At that time, I had a hard time thinking about it for several days. There were many speculative methods in the middle. Because the data design of this question is coincidental, it seems that I have also obtained the correct results, however, an error can be found after any time sequence is changed. The most funny thing is that the answers provided by the TA in class are exactlyOpportunistic answers. The answer to the error is as follows. Let's take a look at the cause of the error.
T = max (T, min (tai-1 + AI, tbi-1 + bi) where Tai represents the wait time for the completion of the I task a, the same as that of the traumatic brain injury.
Initial: t = Tai = Traumatic Brain Injury = 0;
I = 1, t = max (0, min (0 + 2, 0 + 3) = 2;
I = 2, T = max (2, min (2 + 5, 0 + 8) = 7;
I = 3, T = max (7, min (7 + 7, 0 + 4) = 7;
I = 4, t = max (7, min (7 + 10, 4 + 11) = 15;
I = 5, t = max (15, min (7 + 5, 15 + 3) = 15;
I = 6, t = max (15, min (12 + 2, 15 + 4) = 15.

The following is the program code I wrote in C #. It saves all the Optimal Values of the first K tasks and records the order of the completed machines. The programming idea is completely based on the aforementioned analysis, and the program code is included with instructions. Using C ++, you can change the writing and output of two-dimensional arrays. To illustrate the problem, there are no optimizations in the Code and improvements. A picture is provided, which outputs the values of some arrays (for the sake of beauty, the output is not complete) for ease of understanding.


Using system;
Namespace zydd
{
Class Program
{
// The optimal scheduling function for independent tasks. The parameters include the number of tasks in the two groups, the optimal time, And the ordered results.
Static void dlrwzydd (INT [] A, int [] B, int N, int [] least, string [] result)
{
// First give a token.
For (INT I = 0; I <n; I ++)
{
Least [I] = 99;
}

// If the task is completed by only one machine, obtain two time points.
Int asum = 0, bsum = 0;
For (INT I = 0; I <n; I ++)
{
Asum + = A [I];
Bsum + = B [I];
}
// Add 1 as the number of columns in the array to reduce the storage space.
Int sum = 1 + math. Min (asum, bsum );

// Create an array with the same number of rows and columns. timea stores the time that machine A may use and timeb stores the time that machine B uses,
// Timemax records the time required by both, that is, the larger one. Who indicates whether the machine that completed the task is A or B.
Int [,] timea = new int [N, sum];
Int [,] timeb = new int [N, sum];
Int [,] timemax = new int [N, sum];
Char [,] Who = new char [N, sum];
Char [] temprlt = new char [N]; // temprlt records the machine sequence in which the machine completes the task and assigns values to the result

// Calculate the correlation values of the first 1st tasks and record the values in the first 0th rows of the four arrays.
For (INT I = 0; I <= A [0]; I ++)
{
Timea [0, I] = I;
If (I <A [0])
{
Timeb [0, I] = B [0];
Who [0, I] = 'B ';
}
Else
{
Timeb [0, I] = 0;
Who [0, I] = 'a ';
}
Timemax [0, I] = math. Max (timea [0, I], timeb [0, I]);
}

// Although the optimal time for completing 1st tasks can be obtained through direct comparison like below, it is necessary to calculate the above value due to the use of dynamic planning.
If (A [0] <= B [0])
{
Least [0] = A [0];
Temprlt [0] = 'a ';
}
Else
{
Least [0] = B [0];
Temprlt [0] = 'B ';
}
Result [0] = new string (temprlt );

// Calculate the nth to nth tasks and record them in the corresponding rows of the four arrays.
For (int K = 1; k <n; k ++)
{

// Tempsum records the maximum time required for machine A to complete the first K tasks, that is, the time required to complete all tasks by a, that is, all possible values of machine.
Int tempsum = 0;
For (INT temp = 0; temp <= K; temp ++)
{
Tempsum + = A [temp];
}
// Calculate all possible points (timea and timeb) and set the value to timemax.
For (INT I = 0; I <= tempsum; I ++)
{
// The time that machine a spends on completing the first K tasks is I.
Timea [K, I] = I;

// I is timea [K, I]. Is it possible if the time required for machine A to complete the first K tasks is less than the time required for machine A to complete the K tasks? Impossible, so the k-th task must be done by machine B.
If (I <A [k])
{
Timeb [K, I] = timeb [k-1, I] + B [k];
Who [K, I] = 'B ';
}
// According to the aforementioned dynamic planning method, determine the optimal time that machine B spends when machine a spends I time.
Else
{
If (timeb [k-1, I] + B [k]) <= timeb [k-1, I-A [k])
{
Timeb [K, I] = timeb [k-1, I] + B [k];
Who [K, I] = 'B ';
}
Else
{
Timeb [K, I] = timeb [k-1, I-A [k];
Who [K, I] = 'a ';
}
}
// The time spent on the two machines is the total time spent.
Timemax [K, I] = math. Max (timea [K, I], timeb [K, I]);
}

// Process the values after the array tempsum. Set the time of machine A to the maximum. In this case, machine B does not need to spend time.
For (INT I = tempsum + 1; I <asum; I ++)
{
Timea [K, I] = tempsum;
Timeb [K, I] = 0;
}

// After the k-th task is completed, select the minimum value (the optimal value) among all possible values of timemax.
Int flag = 0; // the position where the optimal value is recorded. It is also the time spent by machine.
For (INT I = 0; I <= tempsum; I ++)
{
If (timemax [K, I]> 0 & timemax [K, I] <least [k])
{
Least [k] = timemax [K, I];
Flag = I;
}
}

// Used to trace back the order of the machines used. Note that machine a spends the same time as the subscript of the array, so it can be marked.
// The trace path of the last task has been marked in, which is easy to understand.
Temprlt [k] = who [K, Flag];
For (INT I = K; I> 0 & flag> 0; I --)
{
// If machine a completes the I-th task, the time that machine a spends minus a [I] is the time that the previous I-1 task was completed.
// At the same time, you can determine the machine of the previous task in WHO, because the time consumed by machine A can be used for location marking.
If (temprlt [I] = 'A ')
{
Flag-= A [I];
Temprlt [I-1] = who [I-1, Flag];

}
If (temprlt [I] = 'B ')
{
Temprlt [I-1] = who [I-1, Flag];
}
}

Result [k] = new string (temprlt );
}
}

Static void main (string [] ARGs)
{
Const int n = 6;
Int [] A = new int [N] {2, 5, 7, 10, 5, 2 };
Int [] B = new int [N] {3, 8, 4, 11, 3, 4 };
Int [] least = new int [N]; // record the optimal time for task completion
String [] result = new string [N]; // records the order of the machines that have completed the task.

Dlrwzydd (A, B, n, least, result );
Console. writeline ();
For (INT I = 0; I <n; I ++)
{
Console. writeline ("The Machine sequence of {0} tasks before completion as required is:" + result [I] + "Time: {1}", I + 1, least [I]);
}
}
}
}

From http://hi.baidu.com/liongg/item/03938f0fcd75f58a03ce1b6d

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.