> From: Jose Mario Quintana
> 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.

Looking back in the forum archive Dan says that he adopted the concatenated 
boolean approach, negating it each iteration. However he did not consider it 
"stylish".

IMO the iteration counter is nicer (provides extra useful info) but for this 
case I like keeping the history of the best parents. It is cleaner and provides 
a better information for analysing the algorithms progress. The downside is 
that it will require more space for problems that iterate many times.
 
> 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
> 

I had a go at adapting this approach to my solution:

NB. COMMON CODE
CHARSET=: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ '
NPROG=:   100                       NB. "C" from specification

mutate=: dyad define
  idxmut=. I. x >: (*/$y) ?...@$ 0
  (populate idxmut) idxmut"_} y
)
populate=: (?...@$&# { ])&CHARSET      NB. get random list from charset of same 
length as y
fitness=: +/@:~:"1
select=: ] {~ (i. <./)@:fitness     NB. select fittest member of population
mrate     =: %@:#
copy      =: # ,:

NB. NOT USING WHILE
notperfect =: (0 < fitness) {:
nextgen    =: ] , [ select {:@] , mr...@[ mutate NPROG copy {:@]
evolve=: nextgen ^: notperfect ^:_ ,:@populate

NB. USING WHILE
while      =: 2 : '(((] , (u {:))^:(v {:)^:_) ,:)'
notperfect1=: 0 < fitness
nextgen1   =: select ] , mr...@[ mutate NPROG copy ]
evolve1=: nextgen1 while notperfect1 populate

I'm not sure I know which version I like better. The while certainly cleans up 
the other verbs, letting them concentrate on their core purpose, but even this 
much simpler while, adds an extra line of dense code. 

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to