I use @: instead of @ by default but if conciseness in terms on number of
characters matters then one could rewrite:
evolve=. (((f 0&({::)@]) ; >:@(_1&({::)@]))^:(done 0&({::)@])^:_) (0 ;~ initial)
I would also rename "done" as "NotDone" since "f" is being executed while the
the task is not done yet.
________________________________
From: Jose Mario Quintana <[email protected]>
To: Programming forum <[email protected]>
Sent: Mon, November 2, 2009 12:45:43 PM
Subject: Re: [Jprogramming] Limit limitation
This is a slight modification (just in the definition of evolve) of Dan's
original version that avoids the problem by counting the number of iterations:
CHARS =: ' ABCDEFGHIJKLMNOPQRSTUVWXYZ'
randomize =: fivePct`mutation}
fivePct =: 0.05 >: $ ?...@$ 0:
mutation =: CHARS&(] ,: [ {~ $...@] ?...@$#@[)
score =: +/@:~:"1
copy100 =: 100 $ ,:
done =: 1 - -:
initial =: CHARS ([ {~ ?...@$&#~ ) [
f =: ([ (] {~ (i. <./)@:score) randomize@:copy100@:])
evolve=. (((f 0&({::)@:]) ; >:@:(_1&({::)@:]))^:(done 0&({::)@:])^:_) (0 ;~
initial)
evolve 'METHINKS IT IS LIKE A WEASEL'
+----------------------------+--+
|METHINKS IT IS LIKE A WEASEL|67|
+----------------------------+--+
One can, of course, drop the number of iterations at the end. By the way, one
can also have a while with an iterations upper bound by replacing _ with a
suitable number.
________________________________
From: Jose Mario Quintana <[email protected]>
To: Programming forum <[email protected]>
Sent: Mon, November 2, 2009 10:59:50 AM
Subject: Re: [Jprogramming] Limit limitation
"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?
________________________________
From: "Sherlock, Ric" <[email protected]>
To: Programming forum <[email protected]>
Sent: Mon, November 2, 2009 4:20:10 AM
Subject: Re: [Jprogramming] Limit limitation
> > 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