I see; English is ambiguous and I interpreted it differently because some 
genetic algorithms include the parent(s) in the next generation to avoid a 
potential degradation.  Anyway, going back with Dan's original request for a 
workaround, his original sentence:
 
   (f^:done^:_: initial) 'METHINKS IT IS LIKE A WEASEL'
 
could be replaced by,
 
   (f while done initial) 'METHINKS IT IS LIKE A WEASEL'
METHINKS IT IS LIKE A WEASEL
 
where,
 
   u while v
0&({::)@:((((u (0 {:: ])) ; >:@(_1 {:: ]))^:(v (0 {:: ]))^:_) (0 ;~ ]))
 
This version of "while" is shorter, leaner and faster than my original 
suggestion,
 
   u while v
0&({::)@:((u^:([`1:`(0&({::)@])) ,&< -.@:(1&({::)@:]))^:(''"_ $ 
,@:(v^:([`1:`(0&({::)@]))))^:_^:([`1:`((] ,&< 0:)@])))
 
because this new version is designed only for dyadic verbs; however, it 
apparently can handle the monadic cases by forcing u and v to be monadic.  That 
is, one can use it in the form,
 
  ( (u...@]) while (v...@])  )Y
 
By the way, I recall Dan mentioning that he settled for the iteration counter 
approach but he did not show (or uploaded) that version as far as I know.
 

________________________________
From: "Sherlock, Ric" <[email protected]>
To: Programming forum <[email protected]>
Sent: Mon, November 2, 2009 2:58:09 PM
Subject: Re: [Jprogramming] Limit limitation

> 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
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to