Why am I getting an overflow error with simulated annealing?

32 Views Asked by At

I have a simulated annealing code that randomly adds and removes stores, amount other operators. The code runs for 50000 iterations however, when I try to run 100000 iterations, I get this error: RuntimeWarning: overflow encountered in scalar divide acceptance_probability = min(1, safe_exp(delta_cost / temperature))

Here is the code where the error occurs:

max_iterations = 75000
temperature = 10
cooling_rate = 0.99
mutation_rate = 0.3

stores_copy = closest_id_store.copy()
original_stores = closest_id_store.copy()
best_stores = closest_id_store
best_cost = calculate_cost(stores_copy)

node_id = nodes['NODE ID'].tolist()
for i in range(max_iterations):
    # select a random store
    random_store_index = random.randint(0, len(stores_copy) - 1)
    store_id = stores_copy[random_store_index]

    # select a random new location (node)
    random_node_index = random.randint(0, len(node_id) - 1)
    new_store_location_id = node_id[random_node_index]

    # decide whether to perform mutation operation
    perform_mutation = np.random.rand() < mutation_rate

    if perform_mutation:
        # 50% chance to either add a new location or remove an existing one
        if np.random.rand() < 0.25:
            # add a new location
            stores_copy.append(new_store_location_id)
        elif len(stores_copy) > 1:  # make sure there's more than one store to remove
            # remove an existing location
            stores_copy.pop(random_store_index)
    else:
        # update the store's location in stores_copy
        stores_copy[random_store_index] = new_store_location_id

    # Check if the number of original stores is less than 10 and add missing original stores up to 10
    if len(stores_copy) < 10:
        missing_stores = list(set(original_stores) - set(stores_copy))
        num_missing_stores = min(10 - len(stores_copy), len(missing_stores))
        stores_copy.extend(missing_stores[:num_missing_stores])

    # Limit the maximum number of stores to 15
    if len(stores_copy) > 15:
        num_extra_stores = len(stores_copy) - 15
        random.shuffle(stores_copy)
        stores_copy = stores_copy[:-num_extra_stores]

    # calculate the cost of the new solution
    current_cost = calculate_cost(stores_copy)
    # if the new solution is better, accept it
    if current_cost < best_cost:
        best_cost = current_cost
        best_stores = stores_copy.copy()
    else:
        # if the new solution is worse, accept it with a certain probability
        delta_cost = current_cost - best_cost
        acceptance_probability = min(1, safe_exp(delta_cost / temperature))
        if np.random.rand() < acceptance_probability:
            best_cost = current_cost
            best_stores = stores_copy.copy()
        else:
            # revert the stores_copy to the previous state if the solution is not accepted
            stores_copy = best_stores.copy()

    temperature *= cooling_rate
0

There are 0 best solutions below