Julia 1.0.2.1:为什么变量没有分配就改变值?

时间:2019-04-01 14:07:56

标签: julia atom-editor

出于研究目的,我正在Julia中建立元启发法。

目的是找到最佳的包装盒顺序。

1)我从定义为的初始顺序(随机顺序)开始。在我的while循环之前,Order = InitOrder。

2)对于while循环中的每个迭代,我设置CurrentOrder = Order

3)当CurrentOrder更改时,Order也会更改。为什么未分配订单就更改值?以及如何避免呢?

版本: JuliaPro 1.0.2.1

编辑器:Atom

        while ( (time_ns()-timestart)/1.0e9 < RunLength && done == false ) #Stopping Criteria
            done = true #Starting point
            IterationCount = IterationCount + 1
            BestCurrentValue = sum(H) #Worst case solutio
            CurrentOrder = Order #(From,To)

            for n1=1:N
                for n2=1:N
                    if n1 != n2
                        (CurrentOrder,CopyTo) = SwapBox(CurrentOrder,n1,n2) #Swap boxes
                        (CurrentLayout,L) = DeltaCopy(CurrentLayout,CopyTo,CurrentOrder) #Delta Copy to minimise calculations

                        (TempLayout,L) = BLV(BinW,CurrentLayout,CopyTo,CurrentOrder,W,H,L) #Evalueate by BLV
                        if L < BestCurrentValue #check if TempLayout is better than Best Current
                            BestCurrentValue = L
                            BestCurrentOrder = CurrentOrder
                            BestCurrentLayout = CurrentLayout
                        end #if L<...
                    end #if n1 != n2
##############################################################################
                    CurrentOrder = Order
##############################################################################
                end #n2 in N
            end #n1 in N

            if BestCurrentValue < BestValue
                done = false #Look further
                BestValue = BestCurrentValue
                BestOrder = BestCurrentOrder
                BestLayout = BestCurrentLayout
                Order = BestOrder
            end #if BestCurrentValue...

        end #while

1 个答案:

答案 0 :(得分:2)

您的分配NewOrder=Order不会在内存中复制任何信息,它只是说变量NewOrder应当与Order指向相同的内存位置。因此,更改其中一个变量也会更改另一个变量。如果要复制变量,可以使用NewOrder=deepcopy(Order)