> From: Sherlock, Ric
> 
> > > From: Dan Bron
> > >
> > > I'm trying to code the "evoluationary algorithm"
> > > from RC  http://rosettacode.org/wiki/Evolutionary_algorithm  .  The
> code I
> > > have so far reads [1]:
> > >
> [snip]
> > >
> > > The problem I'm hitting is that f here isn't deterministic.  It
> takes a
> > > "parent", generates a random population, and selects the fittest
> member of
> > > that population -- which could legitimately be the original parent.
> Hence
> > > input and output are identical and  f^:done^:_:  terminates
> "early".  What
> > > is I want is for  f^:done^:_:  to terminate iff  done  is true
> (that is,
> > > 'METHINKS IT IS LIKE A WEASEL' -: y  ).
> > >
> > > Now, I understand that  ^:_  is doing the right thing.  I just want
> it to
> > > do a slightly different thing, and I'm asking for elegant ways to
> achieve
> > > that.  I know I've hit this wall before.  Has anyone else?  Can you
> > > suggest a stylish workaround?
> 
> > Ric wrote:
> > I've submitted a solution for [evolutionary algorithm] on Rosetta
> > Code
> > A tacit version of the while loop would also be good but
> > I think it should be included in addition to the explicit one
> 
> I had a go at putting together a tacit while loop for this problem and
> came up with a solution but am not sure it is what you were after! It
> gets around the issue you describe by catenating the best solution for
> each generation to previous ones, but only "works" with the last best
> solution.
> 
> NB. From current solution on RC
> CHARSET=: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ '
> NPROG=:   100                             NB. "C" from specification
> 
> fitness=: +/@:~:"1
> select=: ] {~ (i. <./)@:fitness           NB. select fittest member of
> population
> populate=: (?...@$&# { ])&CHARSET            NB. get random list from
> charset of same length as y
> mutate=: dyad define
>   idxmut=. I. x >: (*/$y) ?...@$ 0
>   (populate idxmut) idxmut"_} y
> )
> 
> NB. Tacit alternative for current evolve
> mrate         =: %@:#
> nextgeneration=: ] , [ select {:@] , mr...@[ mutate NPROG # ,:@{:@]
> notPerfect    =: 0 < [ fitness {:@]
> 
> evolve=: ] (nextgeneration ^: notPerfect  ^:_) ,:@populate

Or simpler:
evolve=: nextgeneration ^: notPerfect ^:_ ,:@populate
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to