<jupyter_start><jupyter_text>数据读取<jupyter_code>data_loader = DataLoader('data/Data A-Crew.csv', 'data/Data A-Flight.csv')
data_loader.dump_data()
data_loader_a1 = DataLoader('data/Data A-Crew.csv', 'data/Data A-Flight.csv')
data_loader_a1.dump_data(cropped_date=(11,18))<jupyter_output>Counting times...(0/108)
Counting times...(100/108)
## Loading C
## C loaded
## Loading AP
## AP loaded
## Loading Base
## Base loaded
## Loading F
## F loaded
## Loading FF
Loading data... (10000/11664)
## FF loaded
## Loading FF1
Loading data... (10000/11664)
## FF1 loaded
## Loading FF2
Loading data... (10000/11664)
## FF2 loaded
Counting times...(0/108)
Counting times...(100/108)<jupyter_text>模型建立与求解  第一阶段  优化目标①<jupyter_code>m=gp.Model('m1')

z=m.addVars(data_loader_a1.F,vtype=gp.GRB.BINARY,name='z')
x_ikdh=m.addVars(data_loader_a1.F,data_loader_a1.C,vtype=gp.GRB.BINARY,name='x_dh')
x_ikfo=m.addVars(data_loader_a1.F,data_loader_a1.C,vtype=gp.GRB.BINARY,name='x_fo')
x_ikcap=m.addVars(data_loader_a1.F,data_loader_a1.C,vtype=gp.GRB.BINARY,name='x_cap')

r_iksta=m.addVars(data_loader_a1.F,data_loader_a1.C,vtype=gp.GRB.BINARY,name='r_iksta')
r_jkfin=m.addVars(data_loader_a1.F,data_loader_a1.C,vtype=gp.GRB.BINARY,name='r_jkfin')
d_iksta=m.addVars(data_loader_a1.F,data_loader_a1.C,vtype=gp.GRB.BINARY,name='d_iksta')
d_jkfin=m.addVars(data_loader_a1.F,data_loader_a1.C,vtype=gp.GRB.BINARY,name='d_jkfin')

y_ijk=m.addVars(data_loader_a1.F,data_loader_a1.F,data_loader_a1.C,vtype=gp.GRB.BINARY,name='y_ijk')
v_ijk=m.addVars(data_loader_a1.F,data_loader_a1.F,data_loader_a1.C,vtype=gp.GRB.BINARY,name='v_ijk')

# a1=m.addVar(1,vtype=gp.GRB.INTEGER,name='a1')
# a2=m.addVar(2,vtype=gp.GRB.INTEGER,name='a2')

m.ModelSense=GRB.MINIMIZE


# m.setObjectiveN(expr, index=0,pririty=1)
m.setObjective(-z.sum())
# m.setObjectiveN(gp.quicksum(gp.quicksum(data_loader_a1.DCost[k]*(gp.quicksum(d_jkfin[i,k]*data_loader_a1.arrivetime[i] for i in data_loader_a1.FD[t]) 
#                                                           - gp.quicksum(d_iksta[i,k]*data_loader_a1.leavetime[i] for i in data_loader_a1.FD[t]))
#                                                           for k in data_loader_a1.C) for t in data_loader_a1.Dates ),index=1,priority=9)

# m.setObjectiveN(x_ikdh.sum(),index=2,priority=8)
# m.setObjectiveN(gp.quicksum(x_ikfo[i,k] for i in data_loader_a1.F for k in data_loader_a1.C2), index=3, priority=7)

# m.setObjectiveN(a1-a2,index=4,priority=6)

# m.setObjectiveN(-z.sum(),index = 0,weight =0.8)
# m.setObjectiveN(x_ikdh.sum(),index=1,weight=0.2)
# m.setObjectiveN(gp.quicksum(x_ikfo[i,k] for i in data_loader_a.F for k in data_loader_a.C2),index=2,weight=0.05)


M=10000
#对X的约束
m.addConstrs(x_ikfo[i,k]==0 for i in data_loader_a1.F for k in data_loader_a1.C1 )
m.addConstrs(x_ikcap[i,k]==0 for i in data_loader_a1.F for k in data_loader_a1.C3 )
m.addConstrs(x_ikcap[i,k]+x_ikfo[i,k]+x_ikdh[i,k]<=1 for i in data_loader_a1.F for k in data_loader_a1.C)

#对Z的约束
m.addConstrs(gp.quicksum(x_ikcap[i,k]+x_ikfo[i,k]+x_ikdh[i,k] for k in data_loader_a1.C)<=M*(z[i]) for i in data_loader_a1.F)
m.addConstrs(x_ikcap.sum(i,'*')== z[i] for i in data_loader_a1.F )
m.addConstrs(x_ikfo.sum(i,'*')== z[i] for i in data_loader_a1.F )
m.addConstrs(M*z[j]>=gp.quicksum(x_ikcap[j,k]+x_ikfo[j,k]+x_ikdh[j,k] for k in data_loader_a1.C) for j in data_loader_a1.F)

#对Y的约束
m.addConstrs(y_ijk[i,j,k]==0 for i,j in data_loader_a1.FF for k in data_loader_a1.C)
#m.addConstrs(gp.quicksum(y_ijk[i,j,k] for j in F)<=x_ikcap[i,k]+x_ikfo[i,k]+x_ikdh[i,k] for i in F for k in C)
#m.addConstrs(gp.quicksum(y_ijk[j,i,k] for j in F)<=x_ikcap[i,k]+x_ikfo[i,k]+x_ikdh[i,k] for i in F for k in C)

#对roaster周期的约束
# m.addConstrs(r_jkfin[j,k]== 0 for j in data_loader_a.nonF_arrive_base[0] for k in data_loader_a.C)
m.addConstrs(r_iksta[i,k]== 0 for i in data_loader_a1.nonF_leave_base[0] for k in data_loader_a1.C)

m.addConstrs(gp.quicksum(r_iksta[i,k] for i in data_loader_a1.F_leave_base[0] )<=1  for k in data_loader_a1.C)
m.addConstrs(gp.quicksum(r_jkfin[j,k] for j in data_loader_a1.F )-gp.quicksum(r_iksta[i,k] for i in data_loader_a1.F_leave_base[0] ) == 0 for k in data_loader_a1.C)

#第一问中航班对应周期的约束
m.addConstrs(1-(y_ijk.sum(i,'*',k)+r_iksta[i,k]+r_jkfin[i,k])<=M*(1-(x_ikcap[i,k]+x_ikfo[i,k]+x_ikdh[i,k])) for i in data_loader_a1.F for k in data_loader_a1.C)
m.addConstrs(y_ijk.sum(i,'*',k)+r_jkfin[i,k]-(y_ijk.sum('*',i,k)+r_iksta[i,k]) == 0 for i in data_loader_a1.F for k in data_loader_a1.C)

m.addConstrs(y_ijk.sum(i,'*',k)+r_jkfin[i,k] <= x_ikcap[i,k]+x_ikfo[i,k]+x_ikdh[i,k]  for i in data_loader_a1.F for k in data_loader_a1.C)
m.addConstrs(y_ijk.sum('*',i,k)+r_iksta[i,k] <= x_ikcap[i,k]+x_ikfo[i,k]+x_ikdh[i,k]  for i in data_loader_a1.F for k in data_loader_a1.C)

#第二问
#对V的约束
m.addConstrs(v_ijk[i,j,k]==0 for i,j in data_loader_a1.FF1 for k in data_loader_a1.C)
m.addConstrs(v_ijk[i,j,k] <= y_ijk[i,j,k] for i in data_loader_a1.F for j in data_loader_a1.F for k in data_loader_a1.C)


#对duty执勤的约束
m.addConstrs(gp.quicksum(d_iksta[i,k] for i in data_loader_a1.FD[t]) - gp.quicksum(d_jkfin[i,k] for i in data_loader_a1.FD[t]) == 0 for i in data_loader_a1.F for k in data_loader_a1.C for t in data_loader_a1.Dates)
m.addConstrs(gp.quicksum(d_iksta[i,k] for i in data_loader_a1.FD[t])   <= gp.quicksum(x_ikcap[i,k]+x_ikfo[i,k]+x_ikdh[i,k] for i in data_loader_a1.FD[t]) for i in data_loader_a1.F for k in data_loader_a1.C for t in data_loader_a1.Dates)
m.addConstrs(gp.quicksum(d_iksta[i,k] for i in data_loader_a1.FD[t])*M >= gp.quicksum(x_ikcap[i,k]+x_ikfo[i,k]+x_ikdh[i,k] for i in data_loader_a1.FD[t]) for k in data_loader_a1.C for t in data_loader_a1.Dates)

#m.addConstrs(d_iksta[i,k] for i in data_loader_a.FD[t]  <= x_ikcap[i,k]+x_ikfo[i,k]+x_ikdh[i,k] for i in data_loader_a.FD[t] for k in data_loader_a.C for t in data_loader_a.Dates)

#第二问中航班对应执勤，执勤对应周期的约束
m.addConstrs((v_ijk.sum(i,'*',k)+ r_jkfin[i,k] ==  d_jkfin[i,k]) for i in data_loader_a1.F for k in data_loader_a1.C)
m.addConstrs((v_ijk.sum('*',i,k)+ r_iksta[i,k] ==  d_iksta[i,k]) for i in data_loader_a1.F for k in data_loader_a1.C)

#m.addConstrs((d_jkfin[i,k] <= x_ikcap[i,k]+x_ikfo[i,k]+x_ikdh[i,k])  for i in data_loader_a.F for k in data_loader_a.C)
#m.addConstrs((d_iksta[i,k] <= x_ikcap[i,k]+x_ikfo[i,k]+x_ikdh[i,k])  for i in data_loader_a.F for k in data_loader_a.C)

#第二问中其余约束
m.addConstrs(gp.quicksum(d_jkfin[i,k]*data_loader_a1.arrivetime[i] for i in data_loader_a1.FD[t]) - gp.quicksum(d_iksta[i,k]*data_loader_a1.leavetime[i] for i in data_loader_a1.FD[t]) <= 720 for k in data_loader_a1.C for t in data_loader_a1.Dates )
m.addConstrs(gp.quicksum((x_ikcap[i,k]+x_ikfo[i,k])*(data_loader_a1.leavetime[i]-data_loader_a1.arrivetime[i]) for i in data_loader_a1.FD[t]) <= 600 for k in data_loader_a1.C for t in data_loader_a1.Dates )
#m.addConstrs(gp.quicksum(d_jkfin[i,k]*data_loader_a.arrivetime[i] for i in data_loader_a.FD[t]) - gp.quicksum(d_iksta[i,k]*data_loader_a.leavetime[i] for i in data_loader_a.FD[t]) >= 0 for k in data_loader_a.C for t in data_loader_a.Dates )


# m.addConstr(z.sum('*')==108)
# m.addConstr(gp.quicksum(gp.quicksum(data_loader_a1.DCost[k]*(gp.quicksum(d_jkfin[i,k]*data_loader_a1.arrivetime[i] for i in data_loader_a1.FD[t]) 
#                                                            - gp.quicksum(d_iksta[i,k]*data_loader_a1.leavetime[i] for i in data_loader_a1.FD[t]))
#                                                            for k in data_loader_a1.C) for t in data_loader_a1.Dates )==[16900000,17000000])

# m.addConstr(gp.quicksum(gp.quicksum(data_loader_a1.DCost[k]*(gp.quicksum(d_jkfin[i,k]*data_loader_a1.arrivetime[i] for i in data_loader_a1.FD[t])) 
#                     - gp.quicksum(d_iksta[i,k]*data_loader_a1.leavetime[i] for i in data_loader_a1.FD[t])
#                 for k in data_loader_a1.C) for t in data_loader_a1.Dates) <= 170000000)
#对执勤时长平衡的约束（辅助目标）
# m.addConstrs(a1>=(gp.quicksum(gp.quicksum(d_jkfin[i,k]*data_loader_a1.arrivetime[i] for i in data_loader_a1.FD[t]) for t in data_loader_a1.Dates) - gp.quicksum(gp.quicksum(d_iksta[i,k]*data_loader_a1.leavetime[i] for i in data_loader_a1.FD[t]) for t in data_loader_a1.Dates)) for k in data_loader_a1.C)
# m.addConstrs(a2<=(gp.quicksum(gp.quicksum(d_jkfin[i,k]*data_loader_a1.arrivetime[i] for i in data_loader_a1.FD[t]) for t in data_loader_a1.Dates) - gp.quicksum(gp.quicksum(d_iksta[i,k]*data_loader_a1.leavetime[i] for i in data_loader_a1.FD[t]) for t in data_loader_a1.Dates)) for k in data_loader_a1.C)
#m.addConstr(a2 >= 0)
# m.addContr(z.sum()==108)

m.update()
# m.setParam("MIPGap", )
m.optimize()<jupyter_output><empty_output><jupyter_text>联合优化目标②④⑦<jupyter_code>m=gp.Model('m1')

z=m.addVars(data_loader_a1.F,vtype=gp.GRB.BINARY,name='z')
x_ikdh=m.addVars(data_loader_a1.F,data_loader_a1.C,vtype=gp.GRB.BINARY,name='x_dh')
x_ikfo=m.addVars(data_loader_a1.F,data_loader_a1.C,vtype=gp.GRB.BINARY,name='x_fo')
x_ikcap=m.addVars(data_loader_a1.F,data_loader_a1.C,vtype=gp.GRB.BINARY,name='x_cap')

r_iksta=m.addVars(data_loader_a1.F,data_loader_a1.C,vtype=gp.GRB.BINARY,name='r_iksta')
r_jkfin=m.addVars(data_loader_a1.F,data_loader_a1.C,vtype=gp.GRB.BINARY,name='r_jkfin')
d_iksta=m.addVars(data_loader_a1.F,data_loader_a1.C,vtype=gp.GRB.BINARY,name='d_iksta')
d_jkfin=m.addVars(data_loader_a1.F,data_loader_a1.C,vtype=gp.GRB.BINARY,name='d_jkfin')

y_ijk=m.addVars(data_loader_a1.F,data_loader_a1.F,data_loader_a1.C,vtype=gp.GRB.BINARY,name='y_ijk')
v_ijk=m.addVars(data_loader_a1.F,data_loader_a1.F,data_loader_a1.C,vtype=gp.GRB.BINARY,name='v_ijk')

# a1=m.addVar(1,vtype=gp.GRB.INTEGER,name='a1')
# a2=m.addVar(2,vtype=gp.GRB.INTEGER,name='a2')

m.ModelSense=GRB.MINIMIZE


# m.setObjectiveN(expr, index=0,pririty=1)
# m.setObjectiveN(-z.sum())
m.setObjectiveN(gp.quicksum(gp.quicksum(data_loader_a1.DCost[k]*(gp.quicksum(d_jkfin[i,k]*data_loader_a1.arrivetime[i] for i in data_loader_a1.FD[t]) 
                                                          - gp.quicksum(d_iksta[i,k]*data_loader_a1.leavetime[i] for i in data_loader_a1.FD[t]))/60
                                                          for k in data_loader_a1.C) for t in data_loader_a1.Dates ),index=0,priority=8)

m.setObjectiveN(x_ikdh.sum(),index=1,priority=7)
m.setObjectiveN(gp.quicksum(x_ikfo[i,k] for i in data_loader_a1.F for k in data_loader_a1.C2), index=2, priority=6)

# m.setObjectiveN(a1-a2,index=3,priority=5)

# m.setObjectiveN(-z.sum(),index = 0,weight =0.8)
# m.setObjectiveN(x_ikdh.sum(),index=1,weight=0.2)
# m.setObjectiveN(gp.quicksum(x_ikfo[i,k] for i in data_loader_a.F for k in data_loader_a.C2),index=2,weight=0.05)


M=10000
#对X的约束
m.addConstrs(x_ikfo[i,k]==0 for i in data_loader_a1.F for k in data_loader_a1.C1 )
m.addConstrs(x_ikcap[i,k]==0 for i in data_loader_a1.F for k in data_loader_a1.C3 )
m.addConstrs(x_ikcap[i,k]+x_ikfo[i,k]+x_ikdh[i,k]<=1 for i in data_loader_a1.F for k in data_loader_a1.C)

#对Z的约束
m.addConstrs(gp.quicksum(x_ikcap[i,k]+x_ikfo[i,k]+x_ikdh[i,k] for k in data_loader_a1.C)<=M*(z[i]) for i in data_loader_a1.F)
m.addConstrs(x_ikcap.sum(i,'*')== z[i] for i in data_loader_a1.F )
m.addConstrs(x_ikfo.sum(i,'*')== z[i] for i in data_loader_a1.F )
m.addConstrs(M*z[j]>=gp.quicksum(x_ikcap[j,k]+x_ikfo[j,k]+x_ikdh[j,k] for k in data_loader_a1.C) for j in data_loader_a1.F)

#对Y的约束
m.addConstrs(y_ijk[i,j,k]==0 for i,j in data_loader_a1.FF for k in data_loader_a1.C)
#m.addConstrs(gp.quicksum(y_ijk[i,j,k] for j in F)<=x_ikcap[i,k]+x_ikfo[i,k]+x_ikdh[i,k] for i in F for k in C)
#m.addConstrs(gp.quicksum(y_ijk[j,i,k] for j in F)<=x_ikcap[i,k]+x_ikfo[i,k]+x_ikdh[i,k] for i in F for k in C)

#对roaster周期的约束
# m.addConstrs(r_jkfin[j,k]== 0 for j in data_loader_a.nonF_arrive_base[0] for k in data_loader_a.C)
m.addConstrs(r_iksta[i,k]== 0 for i in data_loader_a1.nonF_leave_base[0] for k in data_loader_a1.C)

m.addConstrs(gp.quicksum(r_iksta[i,k] for i in data_loader_a1.F_leave_base[0] )<=1  for k in data_loader_a1.C)
m.addConstrs(gp.quicksum(r_jkfin[j,k] for j in data_loader_a1.F )-gp.quicksum(r_iksta[i,k] for i in data_loader_a1.F_leave_base[0] ) == 0 for k in data_loader_a1.C)

#第一问中航班对应周期的约束
m.addConstrs(1-(y_ijk.sum(i,'*',k)+r_iksta[i,k]+r_jkfin[i,k])<=M*(1-(x_ikcap[i,k]+x_ikfo[i,k]+x_ikdh[i,k])) for i in data_loader_a1.F for k in data_loader_a1.C)
m.addConstrs(y_ijk.sum(i,'*',k)+r_jkfin[i,k]-(y_ijk.sum('*',i,k)+r_iksta[i,k]) == 0 for i in data_loader_a1.F for k in data_loader_a1.C)

m.addConstrs(y_ijk.sum(i,'*',k)+r_jkfin[i,k] <= x_ikcap[i,k]+x_ikfo[i,k]+x_ikdh[i,k]  for i in data_loader_a1.F for k in data_loader_a1.C)
m.addConstrs(y_ijk.sum('*',i,k)+r_iksta[i,k] <= x_ikcap[i,k]+x_ikfo[i,k]+x_ikdh[i,k]  for i in data_loader_a1.F for k in data_loader_a1.C)

#第二问
#对V的约束
m.addConstrs(v_ijk[i,j,k]==0 for i,j in data_loader_a1.FF1 for k in data_loader_a1.C)
m.addConstrs(v_ijk[i,j,k] <= y_ijk[i,j,k] for i in data_loader_a1.F for j in data_loader_a1.F for k in data_loader_a1.C)


#对duty执勤的约束
m.addConstrs(gp.quicksum(d_iksta[i,k] for i in data_loader_a1.FD[t]) - gp.quicksum(d_jkfin[i,k] for i in data_loader_a1.FD[t]) == 0 for i in data_loader_a1.F for k in data_loader_a1.C for t in data_loader_a1.Dates)
m.addConstrs(gp.quicksum(d_iksta[i,k] for i in data_loader_a1.FD[t])   <= gp.quicksum(x_ikcap[i,k]+x_ikfo[i,k]+x_ikdh[i,k] for i in data_loader_a1.FD[t]) for i in data_loader_a1.F for k in data_loader_a1.C for t in data_loader_a1.Dates)
m.addConstrs(gp.quicksum(d_iksta[i,k] for i in data_loader_a1.FD[t])*M >= gp.quicksum(x_ikcap[i,k]+x_ikfo[i,k]+x_ikdh[i,k] for i in data_loader_a1.FD[t]) for k in data_loader_a1.C for t in data_loader_a1.Dates)

#m.addConstrs(d_iksta[i,k] for i in data_loader_a.FD[t]  <= x_ikcap[i,k]+x_ikfo[i,k]+x_ikdh[i,k] for i in data_loader_a.FD[t] for k in data_loader_a.C for t in data_loader_a.Dates)

#第二问中航班对应执勤，执勤对应周期的约束
m.addConstrs((v_ijk.sum(i,'*',k)+ r_jkfin[i,k] ==  d_jkfin[i,k]) for i in data_loader_a1.F for k in data_loader_a1.C)
m.addConstrs((v_ijk.sum('*',i,k)+ r_iksta[i,k] ==  d_iksta[i,k]) for i in data_loader_a1.F for k in data_loader_a1.C)

#m.addConstrs((d_jkfin[i,k] <= x_ikcap[i,k]+x_ikfo[i,k]+x_ikdh[i,k])  for i in data_loader_a.F for k in data_loader_a.C)
#m.addConstrs((d_iksta[i,k] <= x_ikcap[i,k]+x_ikfo[i,k]+x_ikdh[i,k])  for i in data_loader_a.F for k in data_loader_a.C)

#第二问中其余约束
m.addConstrs(gp.quicksum(d_jkfin[i,k]*data_loader_a1.arrivetime[i] for i in data_loader_a1.FD[t]) - gp.quicksum(d_iksta[i,k]*data_loader_a1.leavetime[i] for i in data_loader_a1.FD[t]) <= 720 for k in data_loader_a1.C for t in data_loader_a1.Dates )
m.addConstrs(gp.quicksum((x_ikcap[i,k]+x_ikfo[i,k])*(data_loader_a1.leavetime[i]-data_loader_a1.arrivetime[i]) for i in data_loader_a1.FD[t]) <= 600 for k in data_loader_a1.C for t in data_loader_a1.Dates )
#m.addConstrs(gp.quicksum(d_jkfin[i,k]*data_loader_a.arrivetime[i] for i in data_loader_a.FD[t]) - gp.quicksum(d_iksta[i,k]*data_loader_a.leavetime[i] for i in data_loader_a.FD[t]) >= 0 for k in data_loader_a.C for t in data_loader_a.Dates )


# m.addConstr(z.sum('*')==108)
# m.addConstr(gp.quicksum(gp.quicksum(data_loader_a1.DCost[k]*(gp.quicksum(d_jkfin[i,k]*data_loader_a1.arrivetime[i] for i in data_loader_a1.FD[t]) 
#                                                            - gp.quicksum(d_iksta[i,k]*data_loader_a1.leavetime[i] for i in data_loader_a1.FD[t]))
#                                                            for k in data_loader_a1.C) for t in data_loader_a1.Dates )==[16900000,17000000])

# m.addConstr(gp.quicksum(gp.quicksum(data_loader_a1.DCost[k]*(gp.quicksum(d_jkfin[i,k]*data_loader_a1.arrivetime[i] for i in data_loader_a1.FD[t])) 
#                     - gp.quicksum(d_iksta[i,k]*data_loader_a1.leavetime[i] for i in data_loader_a1.FD[t])
#                 for k in data_loader_a1.C) for t in data_loader_a1.Dates) <= 170000000)
#对执勤时长平衡的约束（辅助目标）
# m.addConstrs(a1>=(gp.quicksum(gp.quicksum(d_jkfin[i,k]*data_loader_a1.arrivetime[i] for i in data_loader_a1.FD[t]) for t in data_loader_a1.Dates) - gp.quicksum(gp.quicksum(d_iksta[i,k]*data_loader_a1.leavetime[i] for i in data_loader_a1.FD[t]) for t in data_loader_a1.Dates)) for k in data_loader_a1.C)
# m.addConstrs(a2<=(gp.quicksum(gp.quicksum(d_jkfin[i,k]*data_loader_a1.arrivetime[i] for i in data_loader_a1.FD[t]) for t in data_loader_a1.Dates) - gp.quicksum(gp.quicksum(d_iksta[i,k]*data_loader_a1.leavetime[i] for i in data_loader_a1.FD[t]) for t in data_loader_a1.Dates)) for k in data_loader_a1.C)
#m.addConstr(a2 >= 0)
m.addConstr(z.sum()==108)

m.update()
# m.setParam("MIPGap", 0.0176)
m.optimize()<jupyter_output><empty_output><jupyter_text>优化目标⑤<jupyter_code>m=gp.Model('m1')

z=m.addVars(data_loader_a1.F,vtype=gp.GRB.BINARY,name='z')
x_ikdh=m.addVars(data_loader_a1.F,data_loader_a1.C,vtype=gp.GRB.BINARY,name='x_dh')
x_ikfo=m.addVars(data_loader_a1.F,data_loader_a1.C,vtype=gp.GRB.BINARY,name='x_fo')
x_ikcap=m.addVars(data_loader_a1.F,data_loader_a1.C,vtype=gp.GRB.BINARY,name='x_cap')

r_iksta=m.addVars(data_loader_a1.F,data_loader_a1.C,vtype=gp.GRB.BINARY,name='r_iksta')
r_jkfin=m.addVars(data_loader_a1.F,data_loader_a1.C,vtype=gp.GRB.BINARY,name='r_jkfin')
d_iksta=m.addVars(data_loader_a1.F,data_loader_a1.C,vtype=gp.GRB.BINARY,name='d_iksta')
d_jkfin=m.addVars(data_loader_a1.F,data_loader_a1.C,vtype=gp.GRB.BINARY,name='d_jkfin')

y_ijk=m.addVars(data_loader_a1.F,data_loader_a1.F,data_loader_a1.C,vtype=gp.GRB.BINARY,name='y_ijk')
v_ijk=m.addVars(data_loader_a1.F,data_loader_a1.F,data_loader_a1.C,vtype=gp.GRB.BINARY,name='v_ijk')

a1=m.addVar(1,vtype=gp.GRB.INTEGER,name='a1')
a2=m.addVar(2,vtype=gp.GRB.INTEGER,name='a2')

m.ModelSense=GRB.MINIMIZE


# m.setObjectiveN(expr, index=0,pririty=1)
# m.setObjectiveN(-z.sum())
# m.setObjectiveN(gp.quicksum(gp.quicksum(data_loader_a1.DCost[k]*(gp.quicksum(d_jkfin[i,k]*data_loader_a1.arrivetime[i] for i in data_loader_a1.FD[t]) 
#                                                           - gp.quicksum(d_iksta[i,k]*data_loader_a1.leavetime[i] for i in data_loader_a1.FD[t]))/60
#                                                           for k in data_loader_a1.C) for t in data_loader_a1.Dates ),index=0,priority=8)

# m.setObjectiveN(x_ikdh.sum(),index=1,priority=7)
# m.setObjectiveN(gp.quicksum(x_ikfo[i,k] for i in data_loader_a1.F for k in data_loader_a1.C2), index=2, priority=6)

m.setObjective(a1-a2)

# m.setObjectiveN(-z.sum(),index = 0,weight =0.8)
# m.setObjectiveN(x_ikdh.sum(),index=1,weight=0.2)
# m.setObjectiveN(gp.quicksum(x_ikfo[i,k] for i in data_loader_a.F for k in data_loader_a.C2),index=2,weight=0.05)


M=10000
#对X的约束
m.addConstrs(x_ikfo[i,k]==0 for i in data_loader_a1.F for k in data_loader_a1.C1 )
m.addConstrs(x_ikcap[i,k]==0 for i in data_loader_a1.F for k in data_loader_a1.C3 )
m.addConstrs(x_ikcap[i,k]+x_ikfo[i,k]+x_ikdh[i,k]<=1 for i in data_loader_a1.F for k in data_loader_a1.C)

#对Z的约束
m.addConstrs(gp.quicksum(x_ikcap[i,k]+x_ikfo[i,k]+x_ikdh[i,k] for k in data_loader_a1.C)<=M*(z[i]) for i in data_loader_a1.F)
m.addConstrs(x_ikcap.sum(i,'*')== z[i] for i in data_loader_a1.F )
m.addConstrs(x_ikfo.sum(i,'*')== z[i] for i in data_loader_a1.F )
m.addConstrs(M*z[j]>=gp.quicksum(x_ikcap[j,k]+x_ikfo[j,k]+x_ikdh[j,k] for k in data_loader_a1.C) for j in data_loader_a1.F)

#对Y的约束
m.addConstrs(y_ijk[i,j,k]==0 for i,j in data_loader_a1.FF for k in data_loader_a1.C)
#m.addConstrs(gp.quicksum(y_ijk[i,j,k] for j in F)<=x_ikcap[i,k]+x_ikfo[i,k]+x_ikdh[i,k] for i in F for k in C)
#m.addConstrs(gp.quicksum(y_ijk[j,i,k] for j in F)<=x_ikcap[i,k]+x_ikfo[i,k]+x_ikdh[i,k] for i in F for k in C)

#对roaster周期的约束
# m.addConstrs(r_jkfin[j,k]== 0 for j in data_loader_a.nonF_arrive_base[0] for k in data_loader_a.C)
m.addConstrs(r_iksta[i,k]== 0 for i in data_loader_a1.nonF_leave_base[0] for k in data_loader_a1.C)

m.addConstrs(gp.quicksum(r_iksta[i,k] for i in data_loader_a1.F_leave_base[0] )<=1  for k in data_loader_a1.C)
m.addConstrs(gp.quicksum(r_jkfin[j,k] for j in data_loader_a1.F )-gp.quicksum(r_iksta[i,k] for i in data_loader_a1.F_leave_base[0] ) == 0 for k in data_loader_a1.C)

#第一问中航班对应周期的约束
m.addConstrs(1-(y_ijk.sum(i,'*',k)+r_iksta[i,k]+r_jkfin[i,k])<=M*(1-(x_ikcap[i,k]+x_ikfo[i,k]+x_ikdh[i,k])) for i in data_loader_a1.F for k in data_loader_a1.C)
m.addConstrs(y_ijk.sum(i,'*',k)+r_jkfin[i,k]-(y_ijk.sum('*',i,k)+r_iksta[i,k]) == 0 for i in data_loader_a1.F for k in data_loader_a1.C)

m.addConstrs(y_ijk.sum(i,'*',k)+r_jkfin[i,k] <= x_ikcap[i,k]+x_ikfo[i,k]+x_ikdh[i,k]  for i in data_loader_a1.F for k in data_loader_a1.C)
m.addConstrs(y_ijk.sum('*',i,k)+r_iksta[i,k] <= x_ikcap[i,k]+x_ikfo[i,k]+x_ikdh[i,k]  for i in data_loader_a1.F for k in data_loader_a1.C)

#第二问
#对V的约束
m.addConstrs(v_ijk[i,j,k]==0 for i,j in data_loader_a1.FF1 for k in data_loader_a1.C)
m.addConstrs(v_ijk[i,j,k] <= y_ijk[i,j,k] for i in data_loader_a1.F for j in data_loader_a1.F for k in data_loader_a1.C)


#对duty执勤的约束
m.addConstrs(gp.quicksum(d_iksta[i,k] for i in data_loader_a1.FD[t]) - gp.quicksum(d_jkfin[i,k] for i in data_loader_a1.FD[t]) == 0 for i in data_loader_a1.F for k in data_loader_a1.C for t in data_loader_a1.Dates)
m.addConstrs(gp.quicksum(d_iksta[i,k] for i in data_loader_a1.FD[t])   <= gp.quicksum(x_ikcap[i,k]+x_ikfo[i,k]+x_ikdh[i,k] for i in data_loader_a1.FD[t]) for i in data_loader_a1.F for k in data_loader_a1.C for t in data_loader_a1.Dates)
m.addConstrs(gp.quicksum(d_iksta[i,k] for i in data_loader_a1.FD[t])*M >= gp.quicksum(x_ikcap[i,k]+x_ikfo[i,k]+x_ikdh[i,k] for i in data_loader_a1.FD[t]) for k in data_loader_a1.C for t in data_loader_a1.Dates)

#m.addConstrs(d_iksta[i,k] for i in data_loader_a.FD[t]  <= x_ikcap[i,k]+x_ikfo[i,k]+x_ikdh[i,k] for i in data_loader_a.FD[t] for k in data_loader_a.C for t in data_loader_a.Dates)

#第二问中航班对应执勤，执勤对应周期的约束
m.addConstrs((v_ijk.sum(i,'*',k)+ r_jkfin[i,k] ==  d_jkfin[i,k]) for i in data_loader_a1.F for k in data_loader_a1.C)
m.addConstrs((v_ijk.sum('*',i,k)+ r_iksta[i,k] ==  d_iksta[i,k]) for i in data_loader_a1.F for k in data_loader_a1.C)

#m.addConstrs((d_jkfin[i,k] <= x_ikcap[i,k]+x_ikfo[i,k]+x_ikdh[i,k])  for i in data_loader_a.F for k in data_loader_a.C)
#m.addConstrs((d_iksta[i,k] <= x_ikcap[i,k]+x_ikfo[i,k]+x_ikdh[i,k])  for i in data_loader_a.F for k in data_loader_a.C)

#第二问中其余约束
m.addConstrs(gp.quicksum(d_jkfin[i,k]*data_loader_a1.arrivetime[i] for i in data_loader_a1.FD[t]) - gp.quicksum(d_iksta[i,k]*data_loader_a1.leavetime[i] for i in data_loader_a1.FD[t]) <= 720 for k in data_loader_a1.C for t in data_loader_a1.Dates )
m.addConstrs(gp.quicksum((x_ikcap[i,k]+x_ikfo[i,k])*(data_loader_a1.leavetime[i]-data_loader_a1.arrivetime[i]) for i in data_loader_a1.FD[t]) <= 600 for k in data_loader_a1.C for t in data_loader_a1.Dates )
#m.addConstrs(gp.quicksum(d_jkfin[i,k]*data_loader_a.arrivetime[i] for i in data_loader_a.FD[t]) - gp.quicksum(d_iksta[i,k]*data_loader_a.leavetime[i] for i in data_loader_a.FD[t]) >= 0 for k in data_loader_a.C for t in data_loader_a.Dates )


# m.addConstr(z.sum('*')==108)
# m.addConstr(gp.quicksum(gp.quicksum(data_loader_a1.DCost[k]*(gp.quicksum(d_jkfin[i,k]*data_loader_a1.arrivetime[i] for i in data_loader_a1.FD[t]) 
#                                                            - gp.quicksum(d_iksta[i,k]*data_loader_a1.leavetime[i] for i in data_loader_a1.FD[t]))
#                                                            for k in data_loader_a1.C) for t in data_loader_a1.Dates )==[16900000,17000000])

# m.addConstr(gp.quicksum(gp.quicksum(data_loader_a1.DCost[k]*(gp.quicksum(d_jkfin[i,k]*data_loader_a1.arrivetime[i] for i in data_loader_a1.FD[t])) 
#                     - gp.quicksum(d_iksta[i,k]*data_loader_a1.leavetime[i] for i in data_loader_a1.FD[t])
#                 for k in data_loader_a1.C) for t in data_loader_a1.Dates) <= 170000000)
#对执勤时长平衡的约束（辅助目标）
m.addConstrs(a1>=(gp.quicksum(gp.quicksum(d_jkfin[i,k]*data_loader_a1.arrivetime[i] for i in data_loader_a1.FD[t]) for t in data_loader_a1.Dates) - gp.quicksum(gp.quicksum(d_iksta[i,k]*data_loader_a1.leavetime[i] for i in data_loader_a1.FD[t]) for t in data_loader_a1.Dates)) for k in data_loader_a1.C)
m.addConstrs(a2<=(gp.quicksum(gp.quicksum(d_jkfin[i,k]*data_loader_a1.arrivetime[i] for i in data_loader_a1.FD[t]) for t in data_loader_a1.Dates) - gp.quicksum(gp.quicksum(d_iksta[i,k]*data_loader_a1.leavetime[i] for i in data_loader_a1.FD[t]) for t in data_loader_a1.Dates)) for k in data_loader_a1.C)
#m.addConstr(a2 >= 0)
m.addConstr(z.sum()==108)
m.addConstr(gp.quicksum(gp.quicksum(data_loader_a1.DCost[k]*(gp.quicksum(d_jkfin[i,k]*data_loader_a1.arrivetime[i] for i in data_loader_a1.FD[t]) 
                                                          - gp.quicksum(d_iksta[i,k]*data_loader_a1.leavetime[i] for i in data_loader_a1.FD[t]))/60
                                                          for k in data_loader_a1.C) for t in data_loader_a1.Dates) == 282710)
m.addConstr(x_ikdh.sum()==15)
m.addConstr(gp.quicksum(x_ikfo[i,k] for i in data_loader_a1.F for k in data_loader_a1.C2)==0)

m.update()
m.setParam("MIPGap", 0.0176)
m.optimize()<jupyter_output>Using license file C:\Users\Ethan\gurobi.lic
Changed value of parameter MIPGap to 0.0176
   Prev: 0.0001  Min: 0.0  Max: inf  Default: 0.0001
Gurobi Optimizer version 9.1.2 build v9.1.2rc0 (win64)
Thread count: 8 physical cores, 16 logical processors, using up to 16 threads
Optimize a model with 723427 rows, 505874 columns and 4202577 nonzeros
Model fingerprint: 0x4a84fd8c
Variable types: 0 continuous, 505874 integer (505872 binary)
Coefficient statistics:
  Matrix range     [1e+00, 1e+05]
  Objective range  [1e+00, 1e+00]
  Bounds range     [1e+00, 2e+00]
  RHS range        [1e+00, 3e+05]
Presolve removed 678731 rows and 428550 columns
Presolve time: 4.89s
Presolved: 44696 rows, 77324 columns, 337995 nonzeros
Variable types: 0 continuous, 77324 integer (77322 binary)

Deterministic concurrent LP optimizer: primal and dual simplex
Showing first log only...


Root simplex log...

Iteration    Objective       Primal Inf.    Dual Inf.      Time
       0    0.0000000e+00   2.694125e+03   1[...]<jupyter_text>为第二阶段准备数据<jupyter_code>results_dic = {'em_no':[], 'fl_no':[], 'cls':[]}
for i in data_loader_a1.F:
    for k in data_loader_a1.C:
        if x_ikcap[i,k].x > 0.9:
            results_dic['em_no'].append(k)
            for key, value in data_loader.F_dic.items():
                if value == data_loader_a1.F_dic[i]:
                    results_dic['fl_no'].append(key)
                    break
            results_dic['cls'].append(1)
        if x_ikfo[i,k].x > 0.9:
            results_dic['em_no'].append(k)
            for key, value in data_loader.F_dic.items():
                if value == data_loader_a1.F_dic[i]:
                    results_dic['fl_no'].append(key)
                    break
            results_dic['cls'].append(2)
        if x_ikdh[i,k].x > 0.9:
            results_dic['em_no'].append(k)
            for key, value in data_loader.F_dic.items():
                if value == data_loader_a1.F_dic[i]:
                    results_dic['fl_no'].append(key)
                    break
            results_dic['cls'].append(3)
leave_temp_base = {}
arrive_time_skipping = {}
for k in data_loader_a1.C:
    for i in data_loader_a1.F:
        if r_jkfin[i,k].x > 0.9:
            leave_temp_base[k] = data_loader_a1.AP_dic[data_loader_a1.F_ap_arr_dic[i]]
            arrive_time_skipping[k] =  data_loader_a1.arrivetime[i] - (24*60*(19-1) - data_loader_a1.get_min_time())
data_loader_a2 = DataLoader('data/Data A-Crew.csv', 'data/Data A-Flight.csv')
data_loader_a2.dump_data(cropped_date=(19,25))
for k in data_loader_a2.C:
    for key, value in data_loader_a2.AP_dic.items():
        if value == leave_temp_base[k]:
            leave_temp_base[k] = key
            break
leave_temp_base_dic = {}
MinRest = 660
for k in data_loader_a2.C:
    for i in data_loader_a2.F:
        if ((data_loader_a2.F_ap_dpt_dic[i] == leave_temp_base[k]) and ((data_loader_a2.leavetime[i]+data_loader_a2.get_min_time()-data_loader_a1.get_min_time() - arrive_time_skipping[k])>=MinRest)):#不是到达的机场
            if k not in leave_temp_base_dic.keys(): 
                leave_temp_base_dic[k] = [i]
            else:
                leave_temp_base_dic[k].append(i)<jupyter_output><empty_output><jupyter_text>第二阶段  优化目标①<jupyter_code>m=gp.Model('m1')

z=m.addVars(data_loader_a2.F,vtype=gp.GRB.BINARY,name='z')
x_ikdh=m.addVars(data_loader_a2.F,data_loader_a2.C,vtype=gp.GRB.BINARY,name='x_dh')
x_ikfo=m.addVars(data_loader_a2.F,data_loader_a2.C,vtype=gp.GRB.BINARY,name='x_fo')
x_ikcap=m.addVars(data_loader_a2.F,data_loader_a2.C,vtype=gp.GRB.BINARY,name='x_cap')

r_iksta=m.addVars(data_loader_a2.F,data_loader_a2.C,vtype=gp.GRB.BINARY,name='r_iksta')
r_jkfin=m.addVars(data_loader_a2.F,data_loader_a2.C,vtype=gp.GRB.BINARY,name='r_jkfin')
d_iksta=m.addVars(data_loader_a2.F,data_loader_a2.C,vtype=gp.GRB.BINARY,name='d_iksta')
d_jkfin=m.addVars(data_loader_a2.F,data_loader_a2.C,vtype=gp.GRB.BINARY,name='d_jkfin')

y_ijk=m.addVars(data_loader_a2.F,data_loader_a2.F,data_loader_a2.C,vtype=gp.GRB.BINARY,name='y_ijk')
v_ijk=m.addVars(data_loader_a2.F,data_loader_a2.F,data_loader_a2.C,vtype=gp.GRB.BINARY,name='v_ijk')

# a1=m.addVar(1,vtype=gp.GRB.INTEGER,name='a1')
# a2=m.addVar(2,vtype=gp.GRB.INTEGER,name='a2')

m.ModelSense=GRB.MINIMIZE

m.setObjective(-z.sum())

#m.setObjective(x_ikdh.sum())
# m.setObjective(gp.quicksum(x_ikfo[i,k] for i in F for k in C2))
# m.setObjective(gp.quicksum(gp.quicksum(data_loader_a.DCost[k]*(gp.quicksum(d_jkfin[i,k]*data_loader_a.arrivetime[i] for i in data_loader_a.FD[t]) 
#                                                                - gp.quicksum(d_iksta[i,k]*data_loader_a.leavetime[i] for i in data_loader_a.FD[t]))/60
#                                                            for k in data_loader_a.C) for t in data_loader_a.Dates ))
# m.setObjective(a1-a2)

#m.setObjectiveN(-z.sum(),index = 0,weight =0.8)
#m.setObjectiveN(x_ikdh.sum(),index=1,weight=0.2)
#m.setObjectiveN(gp.quicksum(x_ikfo[i,k] for i in data_loader_a.F for k in data_loader_a.C2),index=2,weight=0.05)


M=10000
#对X的约束
m.addConstrs(x_ikfo[i,k]==0 for i in data_loader_a2.F for k in data_loader_a2.C1 )
m.addConstrs(x_ikcap[i,k]==0 for i in data_loader_a2.F for k in data_loader_a2.C3 )
m.addConstrs(x_ikcap[i,k]+x_ikfo[i,k]+x_ikdh[i,k]<=1 for i in data_loader_a2.F for k in data_loader_a2.C)

#对Z的约束
m.addConstrs(gp.quicksum(x_ikcap[i,k]+x_ikfo[i,k]+x_ikdh[i,k] for k in data_loader_a2.C)<=M*(z[i]) for i in data_loader_a2.F)
m.addConstrs(x_ikcap.sum(i,'*')== z[i] for i in data_loader_a2.F )
m.addConstrs(x_ikfo.sum(i,'*')== z[i] for i in data_loader_a2.F )
m.addConstrs(M*z[j]>=gp.quicksum(x_ikcap[j,k]+x_ikfo[j,k]+x_ikdh[j,k] for k in data_loader_a2.C) for j in data_loader_a2.F)

#对Y的约束
m.addConstrs(y_ijk[i,j,k]==0 for i,j in data_loader_a2.FF for k in data_loader_a2.C)
#m.addConstrs(gp.quicksum(y_ijk[i,j,k] for j in F)<=x_ikcap[i,k]+x_ikfo[i,k]+x_ikdh[i,k] for i in F for k in C)
#m.addConstrs(gp.quicksum(y_ijk[j,i,k] for j in F)<=x_ikcap[i,k]+x_ikfo[i,k]+x_ikdh[i,k] for i in F for k in C)

#对roaster周期的约束
m.addConstrs(r_jkfin[j,k]== 0 for j in data_loader_a2.nonF_arrive_base[0] for k in data_loader_a2.C)
# m.addConstrs(r_iksta[i,k]== 0 for i in non_leave_temp_base[k] for k in data_loader_a2.C)

m.addConstrs(gp.quicksum(r_iksta[i,k] for i in data_loader_a2.F )<=1  for k in data_loader_a2.C)
m.addConstrs(gp.quicksum(r_jkfin[j,k] for j in data_loader_a2.F )-gp.quicksum(r_iksta[i,k] for i in leave_temp_base_dic[k] ) == 0 for k in data_loader_a2.C)

#第一问中航班对应周期的约束
m.addConstrs(1-(y_ijk.sum(i,'*',k)+r_iksta[i,k]+r_jkfin[i,k])<=M*(1-(x_ikcap[i,k]+x_ikfo[i,k]+x_ikdh[i,k])) for i in data_loader_a2.F for k in data_loader_a2.C)
m.addConstrs(y_ijk.sum(i,'*',k)+r_jkfin[i,k]-(y_ijk.sum('*',i,k)+r_iksta[i,k]) == 0 for i in data_loader_a2.F for k in data_loader_a2.C)

m.addConstrs(y_ijk.sum(i,'*',k)+r_jkfin[i,k] <= x_ikcap[i,k]+x_ikfo[i,k]+x_ikdh[i,k]  for i in data_loader_a2.F for k in data_loader_a2.C)
m.addConstrs(y_ijk.sum('*',i,k)+r_iksta[i,k] <= x_ikcap[i,k]+x_ikfo[i,k]+x_ikdh[i,k]  for i in data_loader_a2.F for k in data_loader_a2.C)

#第二问
#对V的约束
m.addConstrs(v_ijk[i,j,k]==0 for i,j in data_loader_a2.FF1 for k in data_loader_a2.C)
m.addConstrs(v_ijk[i,j,k] <= y_ijk[i,j,k] for i in data_loader_a2.F for j in data_loader_a2.F for k in data_loader_a2.C)


#对duty执勤的约束
m.addConstrs(gp.quicksum(d_iksta[i,k] for i in data_loader_a2.FD[t]) - gp.quicksum(d_jkfin[i,k] for i in data_loader_a2.FD[t]) == 0 for i in data_loader_a2.F for k in data_loader_a2.C for t in data_loader_a2.Dates)
m.addConstrs(gp.quicksum(d_iksta[i,k] for i in data_loader_a2.FD[t])   <= gp.quicksum(x_ikcap[i,k]+x_ikfo[i,k]+x_ikdh[i,k] for i in data_loader_a2.FD[t]) for i in data_loader_a2.F for k in data_loader_a2.C for t in data_loader_a2.Dates)
m.addConstrs(gp.quicksum(d_iksta[i,k] for i in data_loader_a2.FD[t])*M >= gp.quicksum(x_ikcap[i,k]+x_ikfo[i,k]+x_ikdh[i,k] for i in data_loader_a2.FD[t]) for k in data_loader_a2.C for t in data_loader_a2.Dates)

#m.addConstrs(d_iksta[i,k] for i in data_loader_a.FD[t]  <= x_ikcap[i,k]+x_ikfo[i,k]+x_ikdh[i,k] for i in data_loader_a.FD[t] for k in data_loader_a.C for t in data_loader_a.Dates)

#第二问中航班对应执勤，执勤对应周期的约束
m.addConstrs((v_ijk.sum(i,'*',k)+ r_jkfin[i,k] ==  d_jkfin[i,k]) for i in data_loader_a2.F for k in data_loader_a2.C)
m.addConstrs((v_ijk.sum('*',i,k)+ r_iksta[i,k] ==  d_iksta[i,k]) for i in data_loader_a2.F for k in data_loader_a2.C)

#m.addConstrs((d_jkfin[i,k] <= x_ikcap[i,k]+x_ikfo[i,k]+x_ikdh[i,k])  for i in data_loader_a.F for k in data_loader_a.C)
#m.addConstrs((d_iksta[i,k] <= x_ikcap[i,k]+x_ikfo[i,k]+x_ikdh[i,k])  for i in data_loader_a.F for k in data_loader_a.C)

#第二问中其余约束
m.addConstrs(gp.quicksum(d_jkfin[i,k]*data_loader_a2.arrivetime[i] for i in data_loader_a2.FD[t]) - gp.quicksum(d_iksta[i,k]*data_loader_a2.leavetime[i] for i in data_loader_a2.FD[t]) <= 720 for k in data_loader_a2.C for t in data_loader_a2.Dates )
m.addConstrs(gp.quicksum((x_ikcap[i,k]+x_ikfo[i,k])*(data_loader_a2.leavetime[i]-data_loader_a2.arrivetime[i]) for i in data_loader_a2.FD[t]) <= 600 for k in data_loader_a2.C for t in data_loader_a2.Dates )
#m.addConstrs(gp.quicksum(d_jkfin[i,k]*data_loader_a.arrivetime[i] for i in data_loader_a.FD[t]) - gp.quicksum(d_iksta[i,k]*data_loader_a.leavetime[i] for i in data_loader_a.FD[t]) >= 0 for k in data_loader_a.C for t in data_loader_a.Dates )


#m.addConstr(z.sum('*')==206)
# m.addConstr(gp.quicksum(gp.quicksum(data_loader_a.DCost[k]*(gp.quicksum(d_jkfin[i,k]*data_loader_a.arrivetime[i] for i in data_loader_a.FD[t]) 
#                     - gp.quicksum(d_iksta[i,k]*data_loader_a.leavetime[i] for i in data_loader_a.FD[t]))
#                 for k in data_loader_a.C) for t in data_loader_a.Dates )==[1324573-100000,1324573+100000])
#对执勤时长平衡的约束（辅助目标）
# m.addConstrs(a1>=(gp.quicksum(gp.quicksum(d_jkfin[i,k]*data_loader_a.arrivetime[i] for i in data_loader_a.FD[t]) for t in data_loader_a.Dates) - gp.quicksum(gp.quicksum(d_iksta[i,k]*data_loader_a.leavetime[i] for i in data_loader_a.FD[t]) for t in data_loader_a.Dates)) for k in data_loader_a.C)
# m.addConstrs(a2<=(gp.quicksum(gp.quicksum(d_jkfin[i,k]*data_loader_a.arrivetime[i] for i in data_loader_a.FD[t]) for t in data_loader_a.Dates) - gp.quicksum(gp.quicksum(d_iksta[i,k]*data_loader_a.leavetime[i] for i in data_loader_a.FD[t]) for t in data_loader_a.Dates)) for k in data_loader_a.C)
         
             
m.update()
m.optimize()<jupyter_output><empty_output><jupyter_text>优化目标②<jupyter_code>m=gp.Model('m1')

z=m.addVars(data_loader_a2.F,vtype=gp.GRB.BINARY,name='z')
x_ikdh=m.addVars(data_loader_a2.F,data_loader_a2.C,vtype=gp.GRB.BINARY,name='x_dh')
x_ikfo=m.addVars(data_loader_a2.F,data_loader_a2.C,vtype=gp.GRB.BINARY,name='x_fo')
x_ikcap=m.addVars(data_loader_a2.F,data_loader_a2.C,vtype=gp.GRB.BINARY,name='x_cap')

r_iksta=m.addVars(data_loader_a2.F,data_loader_a2.C,vtype=gp.GRB.BINARY,name='r_iksta')
r_jkfin=m.addVars(data_loader_a2.F,data_loader_a2.C,vtype=gp.GRB.BINARY,name='r_jkfin')
d_iksta=m.addVars(data_loader_a2.F,data_loader_a2.C,vtype=gp.GRB.BINARY,name='d_iksta')
d_jkfin=m.addVars(data_loader_a2.F,data_loader_a2.C,vtype=gp.GRB.BINARY,name='d_jkfin')

y_ijk=m.addVars(data_loader_a2.F,data_loader_a2.F,data_loader_a2.C,vtype=gp.GRB.BINARY,name='y_ijk')
v_ijk=m.addVars(data_loader_a2.F,data_loader_a2.F,data_loader_a2.C,vtype=gp.GRB.BINARY,name='v_ijk')

# a1=m.addVar(1,vtype=gp.GRB.INTEGER,name='a1')
# a2=m.addVar(2,vtype=gp.GRB.INTEGER,name='a2')

m.ModelSense=GRB.MINIMIZE

# m.setObjective(-z.sum())
m.setObjective(gp.quicksum(gp.quicksum(data_loader_a2.DCost[k]*(gp.quicksum(d_jkfin[i,k]*data_loader_a2.arrivetime[i] for i in data_loader_a2.FD[t]) 
                                                          - gp.quicksum(d_iksta[i,k]*data_loader_a2.leavetime[i] for i in data_loader_a2.FD[t]))/60
                                                          for k in data_loader_a2.C) for t in data_loader_a2.Dates ))

# m.setObjectiveN(x_ikdh.sum(),index=1,priority=7)
# m.setObjectiveN(gp.quicksum(x_ikfo[i,k] for i in data_loader_a2.F for k in data_loader_a2.C2), index=2, priority=6)

# m.setObjective(a1-a2)


#m.setObjectiveN(-z.sum(),index = 0,weight =0.8)
#m.setObjectiveN(x_ikdh.sum(),index=1,weight=0.2)
#m.setObjectiveN(gp.quicksum(x_ikfo[i,k] for i in data_loader_a.F for k in data_loader_a.C2),index=2,weight=0.05)


M=10000
#对X的约束
m.addConstrs(x_ikfo[i,k]==0 for i in data_loader_a2.F for k in data_loader_a2.C1 )
m.addConstrs(x_ikcap[i,k]==0 for i in data_loader_a2.F for k in data_loader_a2.C3 )
m.addConstrs(x_ikcap[i,k]+x_ikfo[i,k]+x_ikdh[i,k]<=1 for i in data_loader_a2.F for k in data_loader_a2.C)

#对Z的约束
m.addConstrs(gp.quicksum(x_ikcap[i,k]+x_ikfo[i,k]+x_ikdh[i,k] for k in data_loader_a2.C)<=M*(z[i]) for i in data_loader_a2.F)
m.addConstrs(x_ikcap.sum(i,'*')== z[i] for i in data_loader_a2.F )
m.addConstrs(x_ikfo.sum(i,'*')== z[i] for i in data_loader_a2.F )
m.addConstrs(M*z[j]>=gp.quicksum(x_ikcap[j,k]+x_ikfo[j,k]+x_ikdh[j,k] for k in data_loader_a2.C) for j in data_loader_a2.F)

#对Y的约束
m.addConstrs(y_ijk[i,j,k]==0 for i,j in data_loader_a2.FF for k in data_loader_a2.C)
#m.addConstrs(gp.quicksum(y_ijk[i,j,k] for j in F)<=x_ikcap[i,k]+x_ikfo[i,k]+x_ikdh[i,k] for i in F for k in C)
#m.addConstrs(gp.quicksum(y_ijk[j,i,k] for j in F)<=x_ikcap[i,k]+x_ikfo[i,k]+x_ikdh[i,k] for i in F for k in C)

#对roaster周期的约束
m.addConstrs(r_jkfin[j,k]== 0 for j in data_loader_a2.nonF_arrive_base[0] for k in data_loader_a2.C)
# m.addConstrs(r_iksta[i,k]== 0 for i in non_leave_temp_base[k] for k in data_loader_a2.C)

m.addConstrs(gp.quicksum(r_iksta[i,k] for i in data_loader_a2.F )<=1  for k in data_loader_a2.C)
m.addConstrs(gp.quicksum(r_jkfin[j,k] for j in data_loader_a2.F )-gp.quicksum(r_iksta[i,k] for i in leave_temp_base_dic[k] ) == 0 for k in data_loader_a2.C)

#第一问中航班对应周期的约束
m.addConstrs(1-(y_ijk.sum(i,'*',k)+r_iksta[i,k]+r_jkfin[i,k])<=M*(1-(x_ikcap[i,k]+x_ikfo[i,k]+x_ikdh[i,k])) for i in data_loader_a2.F for k in data_loader_a2.C)
m.addConstrs(y_ijk.sum(i,'*',k)+r_jkfin[i,k]-(y_ijk.sum('*',i,k)+r_iksta[i,k]) == 0 for i in data_loader_a2.F for k in data_loader_a2.C)

m.addConstrs(y_ijk.sum(i,'*',k)+r_jkfin[i,k] <= x_ikcap[i,k]+x_ikfo[i,k]+x_ikdh[i,k]  for i in data_loader_a2.F for k in data_loader_a2.C)
m.addConstrs(y_ijk.sum('*',i,k)+r_iksta[i,k] <= x_ikcap[i,k]+x_ikfo[i,k]+x_ikdh[i,k]  for i in data_loader_a2.F for k in data_loader_a2.C)

#第二问
#对V的约束
m.addConstrs(v_ijk[i,j,k]==0 for i,j in data_loader_a2.FF1 for k in data_loader_a2.C)
m.addConstrs(v_ijk[i,j,k] <= y_ijk[i,j,k] for i in data_loader_a2.F for j in data_loader_a2.F for k in data_loader_a2.C)


#对duty执勤的约束
m.addConstrs(gp.quicksum(d_iksta[i,k] for i in data_loader_a2.FD[t]) - gp.quicksum(d_jkfin[i,k] for i in data_loader_a2.FD[t]) == 0 for i in data_loader_a2.F for k in data_loader_a2.C for t in data_loader_a2.Dates)
m.addConstrs(gp.quicksum(d_iksta[i,k] for i in data_loader_a2.FD[t])   <= gp.quicksum(x_ikcap[i,k]+x_ikfo[i,k]+x_ikdh[i,k] for i in data_loader_a2.FD[t]) for i in data_loader_a2.F for k in data_loader_a2.C for t in data_loader_a2.Dates)
m.addConstrs(gp.quicksum(d_iksta[i,k] for i in data_loader_a2.FD[t])*M >= gp.quicksum(x_ikcap[i,k]+x_ikfo[i,k]+x_ikdh[i,k] for i in data_loader_a2.FD[t]) for k in data_loader_a2.C for t in data_loader_a2.Dates)

#m.addConstrs(d_iksta[i,k] for i in data_loader_a.FD[t]  <= x_ikcap[i,k]+x_ikfo[i,k]+x_ikdh[i,k] for i in data_loader_a.FD[t] for k in data_loader_a.C for t in data_loader_a.Dates)

#第二问中航班对应执勤，执勤对应周期的约束
m.addConstrs((v_ijk.sum(i,'*',k)+ r_jkfin[i,k] ==  d_jkfin[i,k]) for i in data_loader_a2.F for k in data_loader_a2.C)
m.addConstrs((v_ijk.sum('*',i,k)+ r_iksta[i,k] ==  d_iksta[i,k]) for i in data_loader_a2.F for k in data_loader_a2.C)

#m.addConstrs((d_jkfin[i,k] <= x_ikcap[i,k]+x_ikfo[i,k]+x_ikdh[i,k])  for i in data_loader_a.F for k in data_loader_a.C)
#m.addConstrs((d_iksta[i,k] <= x_ikcap[i,k]+x_ikfo[i,k]+x_ikdh[i,k])  for i in data_loader_a.F for k in data_loader_a.C)

#第二问中其余约束
m.addConstrs(gp.quicksum(d_jkfin[i,k]*data_loader_a2.arrivetime[i] for i in data_loader_a2.FD[t]) - gp.quicksum(d_iksta[i,k]*data_loader_a2.leavetime[i] for i in data_loader_a2.FD[t]) <= 720 for k in data_loader_a2.C for t in data_loader_a2.Dates )
m.addConstrs(gp.quicksum((x_ikcap[i,k]+x_ikfo[i,k])*(data_loader_a2.leavetime[i]-data_loader_a2.arrivetime[i]) for i in data_loader_a2.FD[t]) <= 600 for k in data_loader_a2.C for t in data_loader_a2.Dates )
#m.addConstrs(gp.quicksum(d_jkfin[i,k]*data_loader_a.arrivetime[i] for i in data_loader_a.FD[t]) - gp.quicksum(d_iksta[i,k]*data_loader_a.leavetime[i] for i in data_loader_a.FD[t]) >= 0 for k in data_loader_a.C for t in data_loader_a.Dates )


#m.addConstr(z.sum('*')==206)
# m.addConstr(gp.quicksum(gp.quicksum(data_loader_a.DCost[k]*(gp.quicksum(d_jkfin[i,k]*data_loader_a.arrivetime[i] for i in data_loader_a.FD[t]) 
#                     - gp.quicksum(d_iksta[i,k]*data_loader_a.leavetime[i] for i in data_loader_a.FD[t]))
#                 for k in data_loader_a.C) for t in data_loader_a.Dates )==[1324573-100000,1324573+100000])
#对执勤时长平衡的约束（辅助目标）
# m.addConstrs(a1>=(gp.quicksum(gp.quicksum(d_jkfin[i,k]*data_loader_a.arrivetime[i] for i in data_loader_a.FD[t]) for t in data_loader_a.Dates) - gp.quicksum(gp.quicksum(d_iksta[i,k]*data_loader_a.leavetime[i] for i in data_loader_a.FD[t]) for t in data_loader_a.Dates)) for k in data_loader_a.C)
# m.addConstrs(a2<=(gp.quicksum(gp.quicksum(d_jkfin[i,k]*data_loader_a.arrivetime[i] for i in data_loader_a.FD[t]) for t in data_loader_a.Dates) - gp.quicksum(gp.quicksum(d_iksta[i,k]*data_loader_a.leavetime[i] for i in data_loader_a.FD[t]) for t in data_loader_a.Dates)) for k in data_loader_a.C)
m.addConstr(z.sum()==98)
# m.addConstr(gp.quicksum(gp.quicksum(data_loader_a2.DCost[k]*(gp.quicksum(d_jkfin[i,k]*data_loader_a2.arrivetime[i] for i in data_loader_a2.FD[t]) 
#                                                           - gp.quicksum(d_iksta[i,k]*data_loader_a2.leavetime[i] for i in data_loader_a2.FD[t]))/60
#                                                           for k in data_loader_a2.C) for t in data_loader_a2.Dates) == 250163)
# m.addConstr(x_ikdh.sum()==)
# m.addConstr(gp.quicksum(x_ikfo[i,k] for i in data_loader_a1.F for k in data_loader_a1.C2)==0)
             
m.update()
m.optimize()<jupyter_output><empty_output><jupyter_text>结果计算与导出<jupyter_code>count_dh = 0
count_bench = 0
for i in data_loader_a2.F:
    for k in data_loader_a2.C:
        if x_ikdh[i,k].x > 0.9:
            count_dh += 1
    for k in data_loader_a2.C2:
        if x_ikfo[i,k].x>0.9:
            count_bench += 1
print(count_dh)
print(count_bench)
for i in data_loader_a2.F:
    for k in data_loader_a2.C:
        if x_ikcap[i,k].x > 0.9:
            results_dic['em_no'].append(k)
            for key, value in data_loader.F_dic.items():
                if value == data_loader_a2.F_dic[i]:
                    results_dic['fl_no'].append(key)
                    break
            results_dic['cls'].append(1)
        if x_ikfo[i,k].x > 0.9:
            results_dic['em_no'].append(k)
            for key, value in data_loader.F_dic.items():
                if value == data_loader_a2.F_dic[i]:
                    results_dic['fl_no'].append(key)
                    break
            results_dic['cls'].append(2)
        if x_ikdh[i,k].x > 0.9:
            results_dic['em_no'].append(k)
            for key, value in data_loader.F_dic.items():
                if value == data_loader_a2.F_dic[i]:
                    results_dic['fl_no'].append(key)
                    break
            results_dic['cls'].append(3)
import pandas as pd
df = pd.DataFrame(results_dic)
df
from ResultViewer import RViewer
rv = RViewer(data_loader, data_cls='a')
rv.load_results_from_df(df)
rv.draw_ef_gantt(save='results/q2_a.png')
df_a = rv.get_results_df_a()
df_a
df_a.to_csv('results/q2_a_UnconveredFlights.csv', index=0)
df_b = rv.get_results_df_b()
df_b
df_b.to_csv('results/q2_a_CrewRosters.csv', index=0)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1594212546878814, 'fraction_numerical': 0.029958058717795086, 'mean_word_length': 4.949828360179562, 'pattern_counts': {'":': 0, '<': 114, '<?xml version=': 0, '>': 64, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 40, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '648712', 'license': 'MIT', 'n_tokens_mistral': 22597, 'n_tokens_neox': 22500, 'n_words': 3102, 'path': 'q2.ipynb', 'repo_name': 'liu-mengyang/CPIPC2021-F', 'size': '45063'}