> From: Jose Mario Quintana
>
> "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."
>
> Would that change not affect the nature of the evolutionary algorithm
> and the task itself?
No. The J code probably explained it better than the English! Your solution
with the iteration count is very similar. It "only works with" the contents of
the first box of the right arg, which is the best parent from the last
generation (the iteration count is ignored except to increment it). My solution
"only works with" the last item of the right arg which is the best parent from
the last generation. After creating the next generation it selects the best
parent and catenates it to the end of the right arg.
The output of your solution is a 2-item list of the boxed best (perfect) parent
and the last iteration count.
The output of my solution is a list of the best parent for each generation, the
last being the perfect parent.
({: ,~ ] #~ 0 = 50 | i...@#) evolve 'METHINKS IT IS LIKE A WEASEL'
RQWBA FDJXIJ TK D BYNNAHFSE
MWTHANKS IT IS LIGE A WEASEL
METHANKS IT IS LIKE A WEASEL
METHINKS IT IS LIKE A WEASEL
>
> 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
>
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm