It looks like the following set of params is even better:

15 4000 1 1
         |
         +---- mutate all the time


    ts 'K=. 15 4000 1 1 TEST 30'
62.072 128192
    fitness@:(+/"1)"2 K
32 32 32 32 32 32 32 32 32 32 32 32 32 32 32
32 32 32 32 32 32 32 32 32 32 32 32 32 32 32

Average execution time:

    62.072%30
2.06907



TEST=:4 :0
z=.i.0 0 0
R=. 10 20 $ 1 + M
for. i.y do.
z=.z,x distribww^:_ R
end.

Replaced the while. with a for. loop with a break.

distribww=: 4 :0
bc=. # y
'ne ng mi ba' =.x
o=. fitness ((,>:)<.av) #~(,~bc&-) bc * (-<.) x: av=.bc%~+/,y
ws=.ne $ ,: b1=.y
for. i.ng do.
   if. o= fitness +/"1 b1 do. break. end.
   p=. (ix}~ mi mutate (ix=.2?bc) { ])"2 ws
   p=. ba {. (/: fitness@:(+/"1)"2) p,b1
   b1=.{.p
   ws=.ne$ p
end.
b1
)



>
> NB. measure of imbalance of the distribution of weights
> NB. used as fitness function
> ds=:+/^:2 @:*:@(-/~)
>
> NB. mutation verb for equal sized buckets
> NB. mutation rate : %x
> mutate=: 4 :0
> if. 0=?x do.
>    'a b'=. y
>    ((j{b)i} a),:(a{~i=.?n) (j=.?n=.#a)}b
> else.
>    y
> end.
> )
>
> NB. only use if 0 = buckets |&# weights
> NB. y = a0,a1,a2,....,:ak
> NB. ai : equal sized sets of weights
> NB. x -->
> NB. ne : population size
> NB. ng : max. # generations
> NB. mi : %mi :: mutation rate
> NB. ba : take ba of the fittest to populate the next generation
> NB. very good results with
> NB. 14 6000 2 1 distribww^:a: 10 20 $ 1+ M
>
> distribww=: 4 :0
> bc=. # y
> 'ne ng mi ba' =.x
> o=. ds ((,>:)<.av) #~(,~bc&-) bc * (-<.) x: av=.bc%~+/,y
> ws=.ne $ ,: b1=.y
> while. (0<ng) *. o~:ds+/"1 b1 do.
>     p=. (ix}~ mi mutate (ix=.2?bc) { ])"2 ws
>     p=. ba {. (/: ds@:(+/"1)"2) p,b1
>     b1=.{.p
>     ws=.ne$ p
>     ng=.ng-1
> end.
> b1
> )
>

-- 
Met vriendelijke groet,
=@@i

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

Reply via email to