コード例 #1
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
コード例 #2
0
            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

print "Starting Generating Initial Population...."
start = time.time()
initial_population = generateInitialTripListPopulation(gift_list,INTIAL_POPULATION_SIZE)
end = time.time()
print "Total Time Taken for Creating Initial Pool : ",end-start

ordered_fitness_list = SantaUtil.sortPopulationByFitness(initial_population)
best_solution_index = ordered_fitness_list[0][0]
best_solution_cost = ordered_fitness_list[0][1]
"""
Generates the best solution from the initial population by using elitist policy and
mutation operator on the order of gifts

Params:
--------
initial_population: List containing the list of initial seed genes
ordered_fitness_list: List containing the sorted tuples of the initial_population sorted by fitness
max_iterations: The maximum number of iterations to run before convergence

Returns:
--------