Как определить значения пригодности в алгоритме geneti c? - PullRequest
0 голосов
/ 20 января 2020

Я отправил вопрос , а go о моей проблеме с моей функцией фитнеса. Это было решено, но взамен другие ограничения не будут. У меня есть 5 ограничений, для каждого нарушения я добавляю одно к счетчику. Теперь я добавляю 10 к одному из них, и это помогло решить только с4! Код работает до тех пор, пока не достигнет значения ниже десяти и застрянет в нем! Как я могу решить это? Я отправил ниже своего текущего забавы фитнеса. и мутация

Фитнес-функция

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 +=10

    #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

Мутация

def mutate(df, df2, population,generation):
    rate = 0.5
    if generation % 5 == 0 or (generation >= 60 and generation%3 == 0) or (generation >= 90 and generation%2 == 0):
        if generation >= 50:
            rate = 0.6
        if generation >= 75:
            rate = 0.7
        if generation >= 100:
            rate = 0.8
        if generation >= 150:
            rate = 0.9
        nopop = random.randint(int(0.1*len(population)),int(rate*len(population)+1))
        nogen = random.randint(int(0.1*len(population[0])),int(rate*len(population[0])+1))
        for i in range (0, nopop):
            member = random.choice(population) 
            mindex = population.index(member)
            for j in range(0, nogen):
                gene = random.choice(member) 
                gindex = member.index(gene)
                gene = initialize (df, df2, 0,23)
                member[gindex] = gene
            population[mindex] = member
        print("Mutation")
    return population

...