This article mainly introduces the Python-based subset tree template to solve the best job scheduling problem, simply explains the job scheduling problem and gives the specific steps of Python using the backtracking subset tree template to achieve the best job scheduling problem, and the related operation skills, the friends who need can refer to the following
In this paper, we describe the best job scheduling problem in Python based on the backtracking subset tree template. Share to everyone for your reference, as follows:
Problem
Given n jobs, each job has two subtasks that need to be completed on two machines, respectively. Each job must first be handled by machine 1 and then treated by machine 2.
Try to design an algorithm to find out the best scheduling of these n tasks, so that their machine 2 complete the sum of the time of the work to achieve a minimum.
Analysis:
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 6 possible scheduling schemes for these 3 jobs are 1,2,3;1,3,2;2,1,3;2,3,1;3,1,2;3,2,1;
The corresponding completion time and the difference are 19,18,20,21,19,19. Easy to see, the best scheduling scheme is 1,3,2, and its completion time is 18.
Take the example of:
Job 1 is completed on machine 1 at 2 and the time on Machine 2 is 3
Job 2 is completed on machine 1 at 5 and the time on machine 2 is 6
Job 3 is completed on machine 1 at 7 and the time on machine 2 is 10
3+6+10 = 19
1,3,2
Job 1 is completed on machine 1 at 2 and the time on Machine 2 is 3
Job 3 is completed on machine 1 at 4 and the time on machine 2 is 7
Job 2 is completed on machine 1 at 7 and the time on machine 2 is 8
3+7+8 = 18
Encode: (x1,x2,..., Xn), Xi denotes the task number executed by order I. So, a solution is a permutation of the task number.
Solution space: {(x1,x2,..., Xn) | Xi belongs to s,i=1,2,..., n},s={1,2,..., n}. So, the solution space is the whole arrangement of the task number.
To be reasonable, we should apply the full-array template of backtracking.
However, with the first two examples to pave the way, here is a subset tree template with backtracking.
Code
"Best Job scheduling problem Tji machine 1 Machine 2 jobs 1 2 1 Jobs 2 3 1 Jobs 3 2 3" ' n = 3 # jobs # N jobs are required on two machines at a time t = [[2,1], [3, 1], [2,3]]x = [0]*n # a solution (n-ary array, xi∈j) x = [] # A set of solutions best_x = [] # Best solution (one scheduler) best_t = 0 # machine 2 min Time and # Collision Detection def conflict (k): G Lobal n, x, X, T, best_t # Part of the solution's job number X[k] cannot exceed 1 if X[:k+1].count (X[k]) > 1:return True # Part of the solution of the Machine 2 perform each job completion time and not more than be st_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 RE Turn False # no conflict # best Job scheduling Problem def dispatch (k): # reaches K element global n, x, X, T, best_t, best_x if k = = N: # exceeds the last element #print (x) #X. Append (x[:]) # Save (a solution) # calculates the sum of each job completion time according to the solution X calculation Machine 2 = [] 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 state space of the K element, machine number 0~n-1, other things to the pruning function x[k] = i if not conflict (k): # Pruning Dispatch (k+1) # Test Dispatch (0) print (best_x) # [0, 2, 1]print (best_t) # 18