Python variable assignment to avoid sharing the same memory space

31 Views Asked by At

I need some help eith the following code in Python, is an implementation of an ALNS heuristic for vrp that I'm trying to create.

def run_heuristic(num_customers, num_vehicles, num_iterations, distance_matrix):
    num_of_solutions = 0
    start = time.time()

    s = construct_initial_solution(num_customers, num_vehicles)
    prev_obj_cost = evaluate_solution(s, distance_matrix)
    print("Initial solution", s)
    print("Objective value: ", prev_obj_cost)
    print(" ")
    s_star = copy.copy(s)
    s_star_obj = evaluate_solution(s, distance_matrix)

    for segment in range(1, 4):
        operator_scores = {op: 0 for op in insertion_operators + removal_operators}
        operator_attempts = {op: 0 for op in insertion_operators + removal_operators}

        iteration = 1

        while iteration <= num_iterations:
            customers_to_remove = random.randint(2, 5)
            insert, removal = choose_operator()
            s_new = copy.copy(s)

            s_new, removed = removal(s_new, customers_to_remove)
            s_new = do_insertion(insert, s_new, removed)

            if check_feasibility(s_new):
                print("Feasible Solution after insertion: ", s_new)
                obj_cost = evaluate_solution(s_new, distance_matrix)
                print("Objective value: ", obj_cost)
                print("s_star: ", s_star, s_star_obj)
                print(" ")

                solution_key = (insert, removal, obj_cost)
                is_accepted_solution = solution_key not in explored_solutions[
                    (insert, removal)]

                if is_accepted_solution:
                    explored_solutions[(insert, removal)].add(solution_key)

                    if obj_cost < s_star_obj:
                        s_star = s_new.copy()
                        s_star_obj = evaluate_solution(s_new, distance_matrix)
                        operator_scores[removal] += 33
                        operator_scores[insert] += 33
                    elif obj_cost > prev_obj_cost:
                        s = copy.copy(s_new)
                        operator_scores[removal] += 9
                        operator_scores[insert] += 9
                    elif obj_cost <= prev_obj_cost:
                        s = copy.copy(s_new)
                        operator_scores[removal] += 13
                        operator_scores[insert] += 13

                    operator_attempts[insert] += 1
                    operator_attempts[removal] += 1

                prev_obj_cost = copy.copy(obj_cost)

                iteration += 1

            num_of_solutions += 1


    end = time.time()
    print("*********************************************************************************")
    print("Final solution: ", s_star)
    print("Objective value: ", s_star_obj)
    print("Computation time: ", end - start)
    print("Number of solution explored: ", num_of_solutions)
    print("*********************************************************************************")
    return None 

I can't understand why the value of s_star is always updated with the current value s_new. I mean, when I check the feasibility and I print both the s_star and s_new I obtain the same print value (which is wrong since I have not yet possibly updated the value of s_star with that of s_new.), while I obtain the correct value of s_star_obj (the best value found). Hence at the end of the iterations, the value s_star_obj is correct and contains the best value, while the s_star contains the last one solution instead of the best one.

Does anyone have any hints?

1

There are 1 best solutions below

0
Lkmuraz On

I found a solution using s_star = copy.deepcopy(s_new) instead of s_star = copy.copy(s_new)