Python uses the Backtracking Method subset tree template to solve the optimal Job Scheduling Problem example, python backtracking

Source: Internet
Author: User

Python uses the Backtracking Method subset tree template to solve the optimal Job Scheduling Problem example, python backtracking

This example describes how Python solves the optimal Job Scheduling Problem Based on the subset tree template of the Backtracking Method. We will share this with you for your reference. The details are as follows:

Problem

Given n jobs, each job has two subtasks that need to be completed on two machines respectively. Each job must be processed by machine 1 and then by machine 2.

Design an algorithm to find out the optimal scheduling for the n tasks, so that the sum of the time for Machine 2 to complete each task reaches the minimum.

Analysis:

Let's look at a specific example:

Tji machine 1 Machine 2
Job 1 2 1
Job 2 3 1
Job 3 2 3

Optimal scheduling sequence: 1 3 2

Processing time: 18

The six possible scheduling schemes for these three jobs are 1, 2, 3; 1, 3, 2; 2, 1, 3; 2, 1; 3, 1, 2; 3, 2, 1;

Their corresponding completion time and respectively are 19,18, 20,21, 19,19. Easy to see, the best scheduling scheme is 1, 3, 2, and the completion time is 18.

Take 1, 2, 3 as an example:

Job 1 is completed on machine 1 at 2 and on Machine 2 at 3
Task 2 is completed on machine 1 at 5 and machine 2 at 6
Job 3 is completed on machine 1 at 7 and on Machine 2 at 10
3 + 6 + 10 = 19

1, 3, 2

Job 1 is completed on machine 1 at 2 and on Machine 2 at 3
Job 3 is completed on machine 1 at 4, and on Machine 2 at 7
The time for job 2 to be completed on machine 1 is 7, and the time for job 2 to be completed on Machine 2 is 8

3 + 7 + 8 = 18

Uncode: (X1, X2,..., Xn), and Xi indicates the task number executed by sequence I. Therefore, a solution is an arrangement of task numbers.

Solution Space: {(X1, X2 ,..., xn) | Xi belongs to S, I = 1, 2 ,..., n}, S = {1, 2 ,..., n }. Therefore, the solution space is the full arrangement of task numbers.

To be rational, we need to apply the full arrangement template of the Backtracking Method.

However, the previous two examples are used to pave the way. Here we apply the subset tree template of the Backtracking Method.

Code

'''Optimal Job Scheduling Problem tji machine 1 Machine 2 Job 1 1 1 job 2 3 3 job 3 2 3 ''' n = 3 # number of jobs # n jobs in two time required for a machine t = [[2, 1], [3, 1], [2, 3] x = [0] * n # A solution (n-element array, xi, J) X = [] # A group of solutions best_x = [] # optimal solution (one scheduling) best_t = 0 # Machine 2 Minimum Time and # conflict Detection def conflict (k): global n, x, X, t, best_t # number of jobs in the decomposition part x [k] cannot exceed 1 if x [: k + 1]. count (x [k])> 1: return True # The sum of the time for executing each job on Machine 2 that is decomposed does not exceed best_t # total_t = sum ([sum ([y [0] for y in t] [: I + 1]) + t [I] [1] for I in range (k + 1)]) J2_t = [] s = 0 for I in range (k + 1 ): s + = t [x [I] [0] j2_t.append (s + t [x [I] [1]) total_t = sum (j2_t) if total_t> best_t> 0: return True return False # No conflict # optimal Job Scheduling Problem def dispatch (k): # Reach the k element global n, x, X, t, best_t, best_x if k = n: # elements beyond the end # print (x) # X. append (x [:]) # Save (one solution) # Calculate the sum of the completion time of Machine 2 execution of each job based on solution x j2_t = [] s = 0 for I in range (n ): s + = t [x [I] [0] j2_t.append (s + t [x [I] [1]) total_t = sum (J2_t) if best_t = 0 or total_t <best_t: best_t = total_t best_x = x [:] else: for I in range (n ): # traverse the status space of the k element. The machine number ranges from 0 ~ N-1, other tasks are handed over to the pruning Function x [k] = I if not conflict (k): # pruning dispatch (k + 1) # testing dispatch (0) print (best_x) # [0, 2, 1] print (best_t) #18

Related Article

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.