├── LICENSE ├── README.md ├── main.py ├── requirements.txt └── 测试用例.txt /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Robin 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Simulated-Annealing-Algorithm-for-JSP 2 | 3 | **Abstract**:Job Shop Scheduling Problem (JSP) is a popular optimization problem in computer science and operational research. It focuses on assigning jobs to finite resources at particular times. Simulated Annealing Algorithm (SSA) is a general stochastic global search optimization algorithm based on Monte Carlo iterative solution strategy. Its starting point is based on the similarity between physical solid material annealing processing and general combinatorial optimization problem. Under a certain initial temperature, combined with the probability and decreased temperature, the global optimal solution of the objective function will be found in the solution space. In this paper, I use this algorithm on JSP with ten instances and got the scheduling orders. 4 | 5 | **The flow chart:** 6 | 7 | ![image](https://user-images.githubusercontent.com/60317828/126891466-9d0cdaac-2f46-4aa6-98be-e47c747cc46f.png) 8 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | #------------------------------- 2 | # Simulated Annealing Algorithm for Job Shop Scheduling Problem 3 | # Created by WZQ 4 | # 2021/7/25 5 | #------------------------------- 6 | 7 | import random 8 | import math 9 | import time 10 | 11 | temperature = 5000 12 | DLATA = 0.99 13 | EPS=10e-4 14 | INLOOP = 3000 15 | 16 | def inititial(): 17 | ''' 18 | 输入数据,初始化 19 | cost[i][j]=t:第i个元件在第j个机器上加工的时间为t 20 | manage[j][k]=i:第j台机器上加工第k个原件为i 21 | 0<=i<=m-1,0<=j<=n-1 22 | ''' 23 | m,n = map(int,input().split()) 24 | cost = [[] for i in range(m)] 25 | for i in range(m): 26 | line = input() 27 | line = ' '.join(line.split()).split(" ") 28 | for j in range(round(len(line)/2)): 29 | cost[i].append(eval(line[2*j+1])) 30 | manage = [[i for i in range(m)] for j in range(n)] 31 | return m,n,cost,manage 32 | 33 | def randomExChange(m,n,manage): 34 | ''' 35 | 产生新解,随机交换某两个零件在某个机器上的的加工顺序 36 | ''' 37 | x = random.randint(0,m-1) 38 | y = random.randint(0,m-1) 39 | for j in range(n): 40 | temp = manage[j][x] 41 | manage[j][x] = manage[j][y] 42 | manage[j][y] = temp 43 | return manage 44 | 45 | def output(num,order): 46 | ''' 47 | 输出最优时间,输出最优调度方案 48 | ''' 49 | print("最优加工时间为:{}".format(num)) 50 | print("顺序为{}".format(order)) 51 | return 0 52 | 53 | def evaluate(cost,manage,n,m): 54 | ''' 55 | 评估函数,返回加工时间 56 | ''' 57 | time= [[0 for i in range(m)] for j in range(n)] #定义时间二维表 58 | a=0 59 | for i in range(m): 60 | a += cost[manage[0][i]][0] #初始化第一行的时间表 61 | time[0][i]=a 62 | for j in range(1,n): 63 | for i in range(m): 64 | position = manage[j-1].index(manage[j][i]) 65 | if(i==0): 66 | bigger = time[j-1][position] 67 | else: 68 | bigger = max(time[j-1][position],time[j][i-1]) 69 | time[j][i]=bigger+cost[manage[j][i]][j] 70 | return time[n-1][m-1] 71 | 72 | def main(): 73 | ''' 74 | 模拟退火算法主函数 75 | ''' 76 | Pnum,Mnum,cost,manage = inititial() 77 | t = evaluate(cost,manage,Mnum,Pnum) 78 | 79 | best = t 80 | T = float(temperature) 81 | startTime=time.perf_counter() 82 | 83 | #外循环迭代,终止于温度小于阈值 84 | while(T>EPS): 85 | #内循环 86 | T = T * DLATA 87 | for iter in range(INLOOP): 88 | temporary = manage 89 | temporary = randomExChange(Pnum,Mnum,temporary) 90 | new_t = float(evaluate(cost,temporary,Mnum,Pnum)) 91 | if(new_t