Johnson Algorithm for streamline Job Scheduling

Source: Internet
Author: User
If job I and j meet the requirements, the job I and j meet the Johnson inequality. If job I and j do not meet the Johnson inequality, after the processing order of job I and j is exchanged, job I and j satisfy the Johnson inequality.

Johnson Algorithm for workflow Job Scheduling:

# Include <iostream>
# Include <algorithm>
# Include <string. h>
Using namespace STD;
Class jobtype
{
Public:
Int time; // execution time
Int index; // job number
Bool group; // group to which the job belongs. There are two machines in total. One is the first group, and the other is the second group.

Int operator <= (jobtype A) const // overload <= No.
{
Return (time <= A. time );
}

};

Bool compare (jobtype A, jobtype B) // use sort
{
Return A. time <B. Time; // ascending order. If it is changed to return A> B, it is in descending order.
}

// Execute flow Job Scheduling according to Johnson's Law
/*
Johnson Algorithm for streamline Job Scheduling:

1. Make M = {I | mi <Ni}, n = {I | mi> = ni}

2. Sort tasks in m in non-descending order of MI and tasks in N in non-ascending order of Ni

3. In M, a task connected to n is the optimal scheduling of the Johnson rule.

*/
Int flowshop (int n, int A [], int B [], int C [])
{
Jobtype * D = new jobtype [N];

// Obtain the minimum processing time of n jobs.

For (INT I = 0; I <n; I ++)
{
D [I]. Time = A [I]> B [I]? B [I]: A [I]; // execution time
D [I]. Group = A [I] <= B [I]; // the group to which the job belongs. There are two machines in total. 1 is the first group, and 0 is the second group.
D [I]. Index = I; // job ID
}

Sort (D, D + N, compare); // sort jobs in ascending order of time in D
Int J = 0, K = n-1;
For (INT I = 0; I <n; I ++)
{
If (d [I]. Group) // if it is the first group, it is put into C [] from 0, and C [] is the optimal scheduling sequence.
{
C [J ++] = d [I]. index;
}
Else
{
C [k --] = d [I]. index;
}
}

J = A [C [0]; // calculate the total time consumed in the optimal scheduling sequence
K = J + B [C [0];
For (INT I = 1; I <n; I ++)
{
J + = A [C [I];
K = j <k? K + B [C [I]: J + B [C [I]; // obtain the maximum total consumed time.
}
Delete D;
Return K;
}

Int main ()
{
// Cout <"Hello world! "<Endl;
Int A [] = {2, 5, 9, 12}; // processing time of the first machine
Int B [] = {7, 3, 10, 13}; // processing time of the second Machine
Int n = sizeof (a)/sizeof (INT); // number of jobs
Int * c = new int [N]; // optimal scheduling sequence
Int result = flowshop (n, a, B, c); // The total number of optimal scheduling times
Cout <result <Endl;
For (INT I = 0; I <n; I ++) // output the optimal scheduling sequence
{
Cout <C [I] <"";
}
Cout <Endl;
Return 0;
}

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.