Reassigning an array of a class.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • MS

    #1

    Reassigning an array of a class.

    Hi,

    In my Genetic Algorithm program, I have a class called Genome. Another
    class called GA has a class variable called 'population' which is an array
    of type Genome.

    One of the methods in GA, called CreateNextGener ation(), creates a local
    array of class Genome called 'nextGeneration ', and at the end of that
    method I want the class variable 'population' to hold the array
    'nextGeneration '.

    Is it as simple as?

    population = nextGeneration;

    At the moment class Genome contains a copy method, and I have a loop which
    copies all the Genomes in 'nextGeneration ' to 'population' (the 2 arrays
    are always the same size). This is obviously ineffecient, but I was
    worried about what memory and garbage collection overheads there would be
    it I used the line:

    population = nextGeneration;

    Should using this line be ok?

    Many thanks,

    MS
  • Eric Sosman

    #2
    Re: Reassigning an array of a class.



    MS wrote:[color=blue]
    > Hi,
    >
    > In my Genetic Algorithm program, I have a class called Genome. Another
    > class called GA has a class variable called 'population' which is an array
    > of type Genome.
    >
    > One of the methods in GA, called CreateNextGener ation(), creates a local
    > array of class Genome called 'nextGeneration ', and at the end of that
    > method I want the class variable 'population' to hold the array
    > 'nextGeneration '.
    >
    > Is it as simple as?
    >
    > population = nextGeneration;
    >
    > At the moment class Genome contains a copy method, and I have a loop which
    > copies all the Genomes in 'nextGeneration ' to 'population' (the 2 arrays
    > are always the same size). This is obviously ineffecient, but I was
    > worried about what memory and garbage collection overheads there would be
    > it I used the line:
    >
    > population = nextGeneration;
    >
    > Should using this line be ok?[/color]

    Yes. Neither `population' nor `nextGeneration ' is
    actually an array; both are references to array objects.
    Here's a crude schematic:

    population -> [a,b,c]

    nextGeneration -> [x,y,z]

    When you execute `population = nextGeneration' you redirect
    the upper arrow to point to the lower array object. If there
    are no other references to the upper array, it becomes
    eligible for garbage collection.

    --
    Eric.Sosman@sun .com

    Comment

    • MS

      #3
      Re: Reassigning an array of a class.

      > Yes. Neither `population' nor `nextGeneration ' is[color=blue]
      > actually an array; both are references to array objects.
      > Here's a crude schematic:
      >
      > population -> [a,b,c]
      >
      > nextGeneration -> [x,y,z]
      >
      > When you execute `population = nextGeneration' you redirect
      > the upper arrow to point to the lower array object. If there
      > are no other references to the upper array, it becomes
      > eligible for garbage collection.[/color]

      Many thanks Eric.

      Comment

      • Nigel Wade

        #4
        Re: Reassigning an array of a class.

        Eric Sosman wrote:
        [color=blue]
        >
        >
        > MS wrote:[color=green]
        >> Hi,
        >>
        >> In my Genetic Algorithm program, I have a class called Genome. Another
        >> class called GA has a class variable called 'population' which is an[/color][/color]
        array[color=blue][color=green]
        >> of type Genome.
        >>
        >> One of the methods in GA, called CreateNextGener ation(), creates a local
        >> array of class Genome called 'nextGeneration ', and at the end of that
        >> method I want the class variable 'population' to hold the array
        >> 'nextGeneration '.
        >>
        >> Is it as simple as?
        >>
        >> population = nextGeneration;
        >>
        >> At the moment class Genome contains a copy method, and I have a loop[/color][/color]
        which[color=blue][color=green]
        >> copies all the Genomes in 'nextGeneration ' to 'population' (the 2 arrays
        >> are always the same size). This is obviously ineffecient, but I was
        >> worried about what memory and garbage collection overheads there would be
        >> it I used the line:
        >>
        >> population = nextGeneration;
        >>
        >> Should using this line be ok?[/color]
        >
        > Yes. Neither `population' nor `nextGeneration ' is
        > actually an array; both are references to array objects.
        > Here's a crude schematic:
        >
        > population -> [a,b,c]
        >
        > nextGeneration -> [x,y,z]
        >
        > When you execute `population = nextGeneration' you redirect
        > the upper arrow to point to the lower array object. If there
        > are no other references to the upper array, it becomes
        > eligible for garbage collection.
        >[/color]

        One important point though, is that after 'population = nextGeneration' both
        population and nextGeneration refer to the *same* array.

        If your CreateNextGener ation() method simply "populates" the array
        nextGeneration, then you will also change the data which population refers
        to. You need to make sure that CreateNextGener ation() creates a new array.

        --
        Nigel Wade, System Administrator, Space Plasma Physics Group,
        University of Leicester, Leicester, LE1 7RH, UK
        E-mail : nmw@ion.le.ac.u k
        Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555

        Comment

        • MS

          #5
          Re: Reassigning an array of a class.

          >> Yes. Neither `population' nor `nextGeneration ' is[color=blue][color=green]
          >>actually an array; both are references to array objects.
          >>Here's a crude schematic:
          >>
          >> population -> [a,b,c]
          >>
          >> nextGeneration -> [x,y,z]
          >>
          >>When you execute `population = nextGeneration' you redirect
          >>the upper arrow to point to the lower array object. If there
          >>are no other references to the upper array, it becomes
          >>eligible for garbage collection.
          >>[/color]
          >
          >
          > One important point though, is that after 'population = nextGeneration' both
          > population and nextGeneration refer to the *same* array.
          >
          > If your CreateNextGener ation() method simply "populates" the array
          > nextGeneration, then you will also change the data which population refers
          > to. You need to make sure that CreateNextGener ation() creates a new array.[/color]

          Thanks.

          CreateNextGener ation() does create a new local array, nextGeneration, and
          fills it with new Genomes. It does some stuff and then finally reassigns
          population: 'population = nextGegeration; '.

          Cheers,

          MS

          Comment

          Working...