salonmonk has asked for the wisdom of the Perl Monks concerning the following question:

Hello all!

I've been experimenting with building Genetic Algorithms using Perl, but could do with a few tips on optimization. For anyone new to the subject - they are great things, very easy to build and fun to play around with. I'm very new to the subject and all the new lingo was rather intimidating, but I'm really starting to enjoy programming them... anyhow enough evangelizing. I'm building a simple exam timetabling GA. I've used natural encoding, which is basically an array of arrays of arrays @Rooms->@Days->@Hours. To perform a crossover I'm having to loop through this structure like so

for (@room) { for (@days) { for(@hours) { do stuff here; if (crossover_loc <= current_location) { use parent1; +} else { use parent2; } } } }

I think that is correct - it's single point crossover, and everything seems to work properly... however I'm wondering if any monk could tell me how I might speed that up. I realize speed isn't something I should really concern myself with - but I'm sure there are some creative ways to loop through those arrays and perform the actions, but I my Perl certainly isn't up to it. Would I be barking up the wrong tree?
many thanks for any insight you may be able to provide.

Si.

Replies are listed 'Best First'.
Re: Optimization of array looping...
by TomDLux (Vicar) on Mar 04, 2004 at 03:23 UTC

    Why not use slices to obtain and copy a complete subset of parent 1 in one operation and then parent2 in a second operation?

    @newArray = ( @parent1[0..$crossover], @parent2[$crossover+1..$#parent2] );

    Darn, my brain has gone blank. There must be a better way to specify the slice continues until the end of the array: $parent2[-1] is the last element, but @parent[3..-1] is empty even if @parent2 has more than four elements.

    --
    TTTATCGGTCGTTATATAGATGTTTGCA

      Darn, my brain has gone blank. There must be a better way to specify the slice continues until the end of the array: $parent2[-1] is the last element, but @parent[3..-1] is empty even if @parent2 has more than four elements.
      This will DWYM:
      @parent[3..@parent-1]
      But this is simply the replacement for the deprecated $#array syntax.

      Update: removed deprecated.

      -QM
      --
      Quantum Mechanics: The dreams stuff is made of

        I think you are a bit confused.

        Taking a look at perldoc perlvar, one might note that $# is deprecated. This is not the same $#array of which you speak. Your code above breaks whenever anyone bothers changing $[ (perhaps an insane programmer wishing to update your code?). Your code above should instead be written as:

        @parent[3..@parent-1+$[]

        Of course, @parent - 1 + $[ is a long way of saying $#parent. So why not just use it?

        antirice    
        The first rule of Perl club is - use Perl
        The
        ith rule of Perl club is - follow rule i - 1 for i > 1

        A reply falls below the community's threshold of quality. You may see it by logging in.
        perldata and perldelta don't seem to mention this deprecation. Is this a bleedperl change?

        Personally, I think the $#array syntax is more manageable than the ugly @array-1 (semantically, this makes little sense) and (heaven forbid!) scalar(@array)-1 (clearer but just as semantically useless.)

        Just by $.02



        Code is (almost) always untested.
        http://www.justicepoetic.net/