コード例 #1
0
def generateInitialTripListPopulation(gift_list,initial_population_size):

    count = 0
    trip_list = list([])
    master_trip_population = list([])
    gift_trip_list = list([])

    while count < initial_population_size:

        random.shuffle(gift_list)
        total_weight = 0
        i = 0
        trip_list = list([])

        while i < len(gift_list):

            while i < len(gift_list) and (total_weight + gift_list[i].weight) <= SLEIGH_CAPACITY:

                gift_trip_list.append(gift_list[i])
                total_weight = total_weight + gift_list[i].weight
                i = i + 1

            trip_order = Trip(gift_trip_list,SantaUtil.tripCost(gift_trip_list))
            total_weight = 0
            gift_trip_list = list([])
            trip_list.append(trip_order)

        count = count+1
        master_trip_population.append(trip_list)

    return master_trip_population
コード例 #2
0
def generateBestSolution(initial_population,ordered_fitness_list,max_iterations):

    iteration = 0
    while iteration < max_iterations:

       iteration = iteration + 1
       gift_mutation = random.random()
      
       best_solution_index = ordered_fitness_list[0][0]
       best_solution_cost = ordered_fitness_list[0][1]
       count = 0 
       
       while count <= 10:
           
           count = count + 1
           
           if gift_mutation >= GIFT_MUTATION_PROBABILITY:
    
               gene_index = random.randint(len(initial_population)-TOP_K-1,len(initial_population)-1)
               chosen_trip_index = ordered_fitness_list[gene_index][0]
               chosen_trip_list = initial_population[chosen_trip_index]
               index,chosen_trip = SantaUtil.maximumTripCost(chosen_trip_list)
               swapped_gift_list = SantaUtil.mutateGiftList(chosen_trip.gift_list)
               temp_trip_cost = SantaUtil.tripCost(swapped_gift_list)
    
               if temp_trip_cost < chosen_trip.trip_cost:
    
                   chosen_trip.gift_list = swapped_gift_list
                   initial_population[chosen_trip_index] = chosen_trip
                   ordered_fitness_list = SantaUtil.sortPopulationByFitness(initial_population)
    
           if gift_mutation < GIFT_MUTATION_PROBABILITY:
    
               gene_index = random.randint(0,TOP_K+1)
               chosen_trip_index = ordered_fitness_list[gene_index][0]
               chosen_trip_list = initial_population[chosen_trip_index]
               index,chosen_trip = SantaUtil.maximumTripCost(chosen_trip_list)
               swapped_gift_list = SantaUtil.mutateGiftList(chosen_trip.gift_list)
               temp_trip_cost = SantaUtil.tripCost(swapped_gift_list)
    
               if temp_trip_cost < chosen_trip.trip_cost:
    
                   chosen_trip.gift_list = swapped_gift_list
                   initial_population[chosen_trip_index] = chosen_trip
                   ordered_fitness_list = SantaUtil.sortPopulationByFitness(initial_population) 
                   
    return best_solution_index,best_solution_cost