问题
I'm building a model about airline scheduling by using genetic algorithms that generate a flight schedule on a daily basis and assigning the right aircraft too. Each chromosome represents a total of 50 flight legs a day(including the return trip that means 100 trips a day) and I have 42 legs, so obviously there will be repeated flight legs. The initial members are created randomly, and there are some constraints that must be satisfied to have a valid schedule. The problem is one of the constraints is that all flight legs must be included, but the model always fails to achieve that!! It starts by missing like 9 or 10 destinations, then it decreases by using creep mutation until it reaches 1 and gets stuck in it and never finds it! Even when it runs for 1000 generations! I tried to interference in the initial member and include them all before the beginning but that doesn't work either! so what's the matter?! how could I solve this?
The fitness code is viewed below, it's still under modifications so I'm sorry if it looks like a little dumb
def fitness (df, df2, member):
fit = []
c1, c2, c3, c4, c5 = 0, 0, 0, 0, 0
fitgene = []
for i in range (0,len(df2.index)):
df2.loc[i, ['Duration']] = 0
for i in range (0, len(member)-1):
#Range of aircraft
acindex = df2[df2['NO']== member[i][4]].index.item()
acrange = df2['Range'].iloc[acindex]
findex = df[df['NO']== member[i][0]].index.item()
distance = df['Distance'].iloc[findex]
if acrange < distance:
c1 += 1
#Duration of aircraft
acspeed = df2['Speed'].iloc[acindex]
time = (distance/acspeed) * 2
if time < 11:
df2.loc[acindex, ['Duration']] += time
for j in range (i+1, len(member)):
#same flight at the same time
if member[i][0] == member [j][0]:
if member[i][2] == member [j][2]:
c2 += 1
#same aircraft in flight
if member[i][4] == member [j][4]:
acindex = df2[df2['NO']== member[i][4]].index.item()
findex = df[df['NO']== member[i][0]].index.item()
distance = df['Distance'].iloc[findex]
acspeed = df2['Speed'].iloc[acindex]
hour = int(distance/acspeed*2)
minute = round(((distance/acspeed*2)-hour) * 60)
#turnaroud time
trt= df2['TRT'].iloc[acindex]*2
hour = hour + member[i][2]
minute = minute + trt + member[i][3]
if minute >= 60 :
while(minute >= 60):
if minute == 60:
minute=0
elif minute > 60:
minute = minute - 60
hour = hour + 1
start1 = (convert([member[i][2], member[i][3]]))
end1 = (convert([hour, int(minute)]))
hour, minute = 0, 0
acindex = df2[df2['NO']== member[j][4]].index.item()
findex = df[df['NO']== member[j][0]].index.item()
distance = df['Distance'].iloc[findex]
acspeed = df2['Speed'].iloc[acindex]
hour = int(distance/acspeed*2)
minute = round(((distance/acspeed*2)-hour) * 60)
#turnaroud time
trt= df2['TRT'].iloc[acindex]*2
hour = hour + member[j][2]
minute = minute + trt + member[j][3]
if minute >= 60 :
while(minute >= 60):
if minute == 60:
minute=0
elif minute > 60:
minute = minute - 60
hour = hour + 1
start2 = (convert([member[j][2], member[j][3]]))
end2 = (convert([hour, int(minute)]))
if (start1 <= start2 and start2 <= end1) or (start1 <= end2 and end2 <= end1) or (start2 <= start1 and start1 <= end2) or (start2 <= end1 and end1 <= end2):
c3 += 1
#all flights present in schedule
for i in range(0,len(df.index)):
flight = df['NO'].iloc[i]
test = 0
for j in range(0,len(member)):
if flight == member[j][0]:
test = 1
if test == 0:
c4 +=1
#duration
for i in range (0,len(df2.index)):
dur = df2['Duration'].iloc[i]
if dur > 11:
c5+=1
fit.append(c1)
fit.append(c2)
fit.append(c3)
fit.append(c4)
fit.append(c5)
fits = c1+c2+c3+c4+c5
return fits,fit
来源:https://stackoverflow.com/questions/59795065/there-is-a-missing-attribute-in-the-chromosome-in-genetic-algorithm