Roxster10
Roxster10

Reputation: 1

Julia 1.0.2.1: Why is a variable changing value, without being assigned?

I am building a metaheuristic in Julia for study purpose.

The purpose is to find the best order of boxes.

1) I start with an initial order (random order) defined as. Order = InitOrder before my while loop.

2) For each iteration in the while loop I set CurrentOrder = Order

3) When the CurrentOrder is changed, Order changes too. Why does Order change value without being assigned? And how do I avoid it?

Version: JuliaPro 1.0.2.1

Editor: 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

Upvotes: 0

Views: 200

Answers (1)

Korsbo
Korsbo

Reputation: 724

Your assignment NewOrder=Order does not copy any information in memory, it just says that the variable NewOrder should point to the same memory location as Order. Changing one of these variables will thus also change the other. If you want to copy a variable you could use NewOrder=deepcopy(Order)

Upvotes: 2

Related Questions