Python变量赋值避免共享相同的内存空间

问题描述 投票:0回答:1

我需要一些帮助,以下 Python 代码是我尝试创建的 VRP ALNS 启发式实现。

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 

我不明白为什么

s_star
的值总是用当前值
 s_new
更新。 我的意思是,当我检查可行性并打印
s_star
s_new
时,我获得了相同的打印值(这是错误的,因为我还没有可能用 s_new 的值更新 s_star 的值。),而我获取正确的
s_star_obj
值(找到的最佳值)。因此,在迭代结束时,值
s_star_obj
是正确的并且包含最佳值,而
s_star
包含最后一个解决方案而不是最佳解决方案。

有人有任何提示吗?

python variables variable-assignment assignment-operator copy-assignment
1个回答
0
投票

我找到了使用

s_star = copy.deepcopy(s_new)
而不是
s_star = copy.copy(s_new)

的解决方案
© www.soinside.com 2019 - 2024. All rights reserved.