Yes I did say 'my final contribution' at the start of my previous e-mail. But
it
kept me busy, so I concentrated on a purely evolutionary algo. Problem was
finding a good set of parameters for a repeatable optimal result. After some
experimenting I found such a set (see below) with a high score for optimal
distributions.
In terms of speed I'll make use of R.E.Boss's imbalance measure verb.
NB. measure of imbalance of the distribution of weights
NB. used as fitness function
ds=:+/^:2 @:*:@(-/~)
NB. mutation verb for equal sized buckets
NB. matation rate : %x
mutateF=: 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 mutateF (ix=.2?bc) { ])"2 ws
p=. ba {. (/: ds@:(+/"1)"2) p,b1
b1=.{.p
ws=.ne$ p
ng=.ng-1
end.
b1
)
Example sessions for input:
M=.10 20 $ 1 + ?.200#1000
Ideal distribution fitness:
ds ((,>:)<.av) #~(,~bc&-) bc * (-<.) x: av=.bc%~+/,M
32
ds@:(+/"1)"2 A [smoutput (6!:2) 'A=.14 6000 2 1 distribww^:a: M'
4.25697
468797952 72 32
(+/"1)"2 A
8104 11199 9778 9348 11073 12426 8031 7426 10832 9555
9778 9778 9777 9776 9777 9777 9777 9777 9778 9777
9777 9778 9777 9777 9777 9777 9777 9777 9778 9777
ds@:(+/"1)"2 A [smoutput (6!:2) 'A=.14 6000 2 1 distribww^:a: M'
3.4478
468797952 32
(+/"1)"2 A
8104 11199 9778 9348 11073 12426 8031 7426 10832 9555
9777 9777 9777 9777 9778 9777 9777 9777 9778 9777
ds@:(+/"1)"2 A [smoutput (6!:2) 'A=.14 6000 2 1 distribww^:a: M'
1.88214
468797952 32
(+/"1)"2 A
8104 11199 9778 9348 11073 12426 8031 7426 10832 9555
9777 9777 9778 9777 9777 9777 9777 9777 9777 9778
--
Met vriendelijke groet,
=@@i
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm