1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
| """ Description: 复现Informs journal on computing上的论文。 Author: TUUG Date: 2024/11/06 10:48 Version: V6.0 """
import gurobipy as gp from gurobipy import GRB import numpy as np import pandas as pd
processing_time = pd.read_csv('工件加工时间.csv',index_col=0).T relation = pd.read_csv('工件优先关系.csv',index_col=0) set_up = pd.read_csv('工件切换时间.csv',index_col=0) jobs = ['job1','job2','job3','job4','job5'] serus = ['seru1','seru2','seru3'] num_serus = len(serus) num_jobs = len(jobs) batch_info = pd.read_csv('批量大小.csv',index_col=0) batch_info['size'] = np.random.normal(loc=batch_info['mean'],scale=batch_info['std']).astype(int) batch_size = batch_info['size'].to_list()
relation = [('job1','job2')] dummy_job = ['job0']
def get_pre(h,i,job,assign_jobs_list_record): ans_list = [set_up.loc[i,job] for i in assign_jobs_list_record[h][i]] return max(ans_list)
def get_theta(h,i,job,assign_jobs_list_record): return get_pre(h,i,job,assign_jobs_list_record)+processing_time.loc[serus[i],job]
def create_master_model(gen=None,C_max_current_record=None, assign_jobs_list_record=None): master_model = gp.Model('master problem') x = master_model.addVars(serus,jobs+dummy_job,vtype=GRB.BINARY,name='x') C_max = master_model.addVar(vtype=GRB.CONTINUOUS, name="C_max") C = master_model.addVars(jobs+dummy_job,vtype=GRB.CONTINUOUS,name='C') xi = master_model.addVars(serus,vtype=GRB.CONTINUOUS,name='xi') master_model.setObjective(C_max, GRB.MINIMIZE) master_model.setParam('OutputFlag', 0)
for i in serus: master_model.addConstr(gp.quicksum(x[i,j]*processing_time.loc[i,j] for j in jobs)+xi[i] <= C_max)
for j in jobs: master_model.addConstr(gp.quicksum(x[i,j] for i in serus) == 1)
for i in serus: master_model.addConstr(x[i,'job0'] == 1) master_model.addConstr(C['job0'] == 0) if gen: print('********','增加了benders cut','*********') for h in range(gen): for i in range(len(serus)): assign_jobs = assign_jobs_list_record[h][i] if 'job0' in assign_jobs: assign_jobs.remove('job0') master_model.addConstr(C_max >= C_max_current_record[h][i] - gp.quicksum((1-x[serus[i],j])*get_theta(h,i,j,assign_jobs_list_record) for j in assign_jobs),name='cut')
master_model.optimize() master_model.write("master_model.lp") if master_model.status == GRB.OPTIMAL: print(master_model.objVal,'===主模型的解为=====') assignment = np.array([[1 if x[m, j].X > 0.1 else 0 for j in jobs+dummy_job] for m in serus]) assignment = pd.DataFrame(assignment, columns=jobs+dummy_job, index=serus) print('----------','x的取值情况','--------------------') for row in serus: for col in jobs: if x[row, col].X > 0: print(x[row, col]) print('----------','C的取值情况','--------------------') for col in jobs: if C[col].X > 0: print(C[col]) return assignment, master_model.objVal else: return None, None def get_sub_job(assignment,ind): row = assignment.loc[ind] columns_with_1 = row[row == 1].index.tolist() return columns_with_1
def solve_subproblem(ind,assignment): """求解第ind个机器上的完工时间""" i = ind x = assignment print('--------------求解第' + str(i) + '个机器上的完工时间--------------------') assign_jobs = get_sub_job(assignment,ind) sub_model = gp.Model('sub_problem') C = sub_model.addVars(jobs+dummy_job,vtype=GRB.CONTINUOUS,name='C') y = sub_model.addVars(serus,jobs+dummy_job,jobs+dummy_job,vtype=GRB.BINARY,name='y') xi = sub_model.addVars(serus,vtype=GRB.CONTINUOUS,name='xi') C_machine = sub_model.addVars(serus,vtype=GRB.CONTINUOUS,name='C_machine') sub_model.setObjective(C_machine[i],sense=GRB.MINIMIZE) M = 10086 for j in jobs: sub_model.addConstr(C_machine[i] >= C[j]*x.loc[i,j]) sub_model.addConstr(xi[i] == gp.quicksum(y[i,j,k]*set_up.loc[j,k] for j in jobs+dummy_job for k in jobs+dummy_job))
for k in jobs+dummy_job: sub_model.addConstr(gp.quicksum(y[i,j,k] for j in jobs+dummy_job) == x.loc[i,k])
for j in jobs+dummy_job: sub_model.addConstr(gp.quicksum(y[i,j,k] for k in jobs+dummy_job) == x.loc[i,j])
for j in jobs+dummy_job: for k in jobs: sub_model.addConstr(C[k] -C[j] +M*(1-y[i,j,k]) >= set_up.loc[j,k]+processing_time.loc[i,k])
for pair in relation: sub_model.addConstr(y[i,pair[0],pair[1]] == 0) sub_model.setParam('OutputFlag',0) sub_model.optimize() if sub_model.status == GRB.OPTIMAL: print('----------','y的取值情况','----------') for row in serus: for col in jobs: for k in jobs: if y[row, col, k].X > 0: print(y[row,col,k]) result = np.array([[1 if y[i,m,j].X > 0 else 0 for j in jobs] for m in jobs]) return result,sub_model.objVal,assign_jobs else: print('未求得最优解') return None, None
assignment,obj = create_master_model() gen = 1 result_obj_list = [int(obj*3)]*num_serus C_max_list = [0]*num_serus result_obj_list_record = [] assign_jobs_list_record = [] theta_record = [] LB_record = [] UB_record = [] UB_min = result_obj_list[0]
while max(result_obj_list)-obj >= 1e-6: LB_record.append(obj) UB_record.append(max(result_obj_list))
print('===============第'+str(gen)+'次循环=====================') result_list = [solve_subproblem(i,assignment=assignment) for i in serus] result_assign_list = [i[0] for i in result_list] result_obj_list = [i[1] for i in result_list] assign_jobs_list = [i[2] for i in result_list] result_obj_list_record.append(result_obj_list) assign_jobs_list_record.append(assign_jobs_list)
assignment,obj = create_master_model(gen=gen, C_max_current_record=result_obj_list_record,assign_jobs_list_record=assign_jobs_list_record) print('主问题的目标值',str(obj)) gen += 1 if gen == 100: print('迭代次数超过100,退出循环') break
print('Logic-based benders求解结束') print('目标值为:',obj)
from matplotlib import pyplot as plt import matplotlib.cm as cm
plt.rcParams['font.sans-serif']=['SimHei'] plt.rcParams['axes.unicode_minus']=False
UB_record.append(obj) LB_record.append(obj)
def update_to_historical_min(UB_record): min_value = UB_record[0] for i in range(len(UB_record)): min_value = min(min_value, UB_record[i]) UB_record[i] = min_value return UB_record
plt.figure(figsize=(10, 6)) colors = cm.viridis(np.linspace(0, 1, 7)) plt.plot(UB_record, label="子问题", color='purple',alpha=0.5, marker='s',linestyle='--', linewidth=2) plt.plot(LB_record, label="主问题", color='cornflowerblue',alpha=0.8, marker='x',linestyle='-.', linewidth=2)
plt.xlabel("迭代次数") plt.ylabel("最大完工时间")
plt.legend() plt.grid(True,alpha=0.5,linestyle='--',color='gray')
plt.show()
|