当前位置:网站首页>Backtracking method to solve batch job scheduling problem
Backtracking method to solve batch job scheduling problem
2022-07-03 15:24:00 【How good】
alas , This is the beginning of a failure . however , I'm not afraid of failure ! Go to bed a little later today , Because I sleep more at noon ~ Recently, I was taught by teacher Wang Xiaodong 《 Computer algorithm design and analysis 》( The first 4 edition ) It's a hell of a torture . Won't say something elegant , It's true . Based on differential , Not only will I forget what I learned , And I thought I could never do something , Now you can understand it with a little look . How arrogant people are ! I don't say much nonsense . Put the original question first .
One 、 problem
Batch job scheduling problem requires for a given n Homework , Make the best job scheduling plan , Make it complete in the shortest time .
no way , I can't fix this form . Don't do it first . I'll put the code first .
Two 、 Code
</pre><pre name="code" class="html"> Batch job scheduling class FlowShop<pre name="code" class="java">
/** * Problem description : * Given n A collection of jobs J={J1,J2,...,Jn}. Every assignment Ji There are two tasks, respectively 2 Complete... On this machine . Each operation must be carried out by the machine first 1 Handle , Then the machine 2 Handle . Homework Ji We need machines j Of * The processing time is tji,i=1,2,...,n;j=1,2. For a certain job scheduling , set up Fji It's homework i In the machine j Last time the processing is completed . Then all operations are on the machine 2 Time and f=F2i Sum of * Schedule the completion and . * Batch job scheduling problem requires for a given n Homework , Make the best job scheduling plan , Make it complete in the shortest time . * @author noonTree * */public class FlowShop {static int Max = 200;static int M[][];// Processing time required for each operation static int[] x;// Current job schedule static int[] bestx;// Current optimal job scheduling static int[] f2;// machine 2 Processing time completed static int f1;// machine 1 Time to complete processing static int f;// Completion time and static int bestf;// The current optimal value static int n;// Number of assignments void BackTrack(int i){if(i > n){//n Secondary cycle , Let the current optimal scheduling be bestx[] For the current optimal scheduling x[]for(int j = 1;j <= n;j++)bestx[j] = x[j];// The current optimal values are completion time and bestf = f;}else{/*i To n The cycle of time *j Used to indicate which task is selected ( That is, the order of execution ) tb(0) Came in , No matter how recursive , There is j=1,2,3 These three processes , So it must be able to traverse completely ( I copied this sentence from someone else ) */for(int j = i;j <= n;j++){//f1 Equal to the i The time of the first job on the first machine is added f1 += M[x[j]][1];/*f2 There are two situations : * (1) If the last job spent more time on the second machine than the next job on machine one , Then the current job time is equal to the last job on the machine 2 Time spent plus current job * In the machine 2 Time spent ; * (2) If it's less than , Then the current job is equal to the machine 1 The completion processing time plus the current job on the machine 2 Time spent . */f2[i] = ((f2[i - 1] > f1)? f2[i - 1]:f1) + M[x[j]][2];// The total time is equal to working on the machine 2 Action time on f += f2[i];// If the total time is less than the optimal value, there is :if(f < bestf){// Exchange job scheduling at this time Swap(x,i,j);// Go back to i+1BackTrack(i + 1);// Backtracking doesn't understand !!!// Exchange job scheduling at this time Swap(x,i,j);}// When backtracking is complete , Current machine 1 The processing time is equal to the last machine 1 The completion processing time minus the current xf1 -= M[x[j]][1];f -= f2[i];}}}void Swap(int[] x, int i, int j) {int temp = x[j];x[j] = x[i];x[i] = temp;}int Flow(int[][] MM,int nn,int bbestx[]){int ub = Max;// What is it? ? Determine a maximum n = nn;M = MM;x = new int[n + 1];f2 = new int[n + 1];bestx = bbestx;bestf = ub;f1 = 0;f = 0;for(int i = 0;i <= n;i++){f2[i] = 0;x[i] = i;}BackTrack(1);return bestf;}} Test class TestFlowShop
import java.util.Scanner;
public class TestFlowShop {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.printf("\t Batch job scheduling \n");
System.out.print(" Enter the number of jobs :");
int n = input.nextInt();
System.out.println(" Enter the processing time required for each job :");
// Let array subscript from 1 Start entering data , Line zero no
int M[][] = new int[n + 1][3];
for(int i = 1;i <= n;i++){
for(int j = 1;j < 3;j++){
M[i][j] = input.nextInt();
}
}
// Current optimal scheduling
int bestx[] = new int[n + 1];
FlowShop Fls = new FlowShop();
// The current optimal value
int bestf = Fls.Flow(M,n,bestx);
System.out.print(" Output the current optimal scheduling :");
for(int i = 1;i <= n;i++){
if(i != n){
System.out.print(bestx[i] + " ");
}else{
System.out.print(bestx[i]);
}
}
System.out.println();
System.out.print(" Output the current optimal value :" + bestf);
}
}
/* What happened :
* (1) Use when variables are not initialized
* (2) It's killing me , Position of subscript , I'm sick of it . I just can't see . I think that's it . It's uncomfortable to be laughed at when you say it , Maybe this is where I don't know how to adapt . however , I won't admit defeat . In self-control ,
* I There are advantages .
* (3) I also found a problem , Why does it show up (1) What about the situation ? Because in C++ The methods of , In the method outside the class, use the method of the class to declare this class , Then default two variables with the same name , Of course, it is called by default
* It is the variable of the current method, not the variable in the class , The variable to call the class is “ Class name . Method ”. I don't understand this , So I took a detour . It's just , This is also a big lesson !!
* */
边栏推荐
- What is machine reading comprehension? What are the applications? Finally someone made it clear
- Jvm-05-object, direct memory, string constant pool
- Detailed comments on MapReduce instance code on the official website
- [Yu Yue education] scientific computing and MATLAB language reference materials of Central South University
- [transform] [practice] use pytoch's torch nn. Multiheadattention to realize self attention
- Popular understanding of gradient descent
- Redis lock Optimization Practice issued by gaobingfa
- Visual upper system design and development (Halcon WinForm) -5 camera
- qt使用QZxing生成二维码
- Custom annotation
猜你喜欢

Visual upper system design and development (Halcon WinForm) -5 camera
![[transformer] Introduction - the original author of Harvard NLP presented the annotated transformer in the form of line by line implementation in early 2018](/img/2b/b23aeab584f89be6678c0fe059d4b6.png)
[transformer] Introduction - the original author of Harvard NLP presented the annotated transformer in the form of line by line implementation in early 2018

Halcon and WinForm study section 1

Visual upper system design and development (Halcon WinForm) -1 Process node design

秒杀系统3-商品列表和商品详情

C语言刷题~Leetcode与牛客网简单题

【云原生训练营】模块八 Kubernetes 生命周期管理和服务发现
![[attention mechanism] [first vit] Detr, end to end object detection with transformers the main components of the network are CNN and transformer](/img/9b/6ca8375ef8689a80d437665909ae30.png)
[attention mechanism] [first vit] Detr, end to end object detection with transformers the main components of the network are CNN and transformer

Second kill system 3 - list of items and item details

视觉上位系统设计开发(halcon-winform)-5.相机
随机推荐
[transformer] Introduction - the original author of Harvard NLP presented the annotated transformer in the form of line by line implementation in early 2018
Visual upper system design and development (Halcon WinForm) -3 Image control
"Seven weapons" in the "treasure chest" of machine learning: Zhou Zhihua leads the publication of the new book "machine learning theory guide"
Didi off the shelf! Data security is national security
Halcon and WinForm study section 1
从 flask 服务端代码自动生成客户端代码 -- flask-native-stubs 库介绍
基础SQL教程
Custom annotation
Puppet automatic operation and maintenance troubleshooting cases
Kubernetes advanced training camp pod Foundation
Qt常用语句备忘
SQL server installation location cannot be changed
XWiki安装使用技巧
【日常训练】395. 至少有 K 个重复字符的最长子串
秒殺系統3-商品列錶和商品詳情
使用JMeter对WebService进行压力测试
Characteristics of MySQL InnoDB storage engine -- Analysis of row lock
Matlab r2011b neural network toolbox precautions
The markdown file obtains the pictures of the network and stores them locally and modifies the URL
Jvm-03-runtime data area PC, stack, local method stack