<table cellspacing="0" cellpadding="0" border="0" ><tr><td valign="top" style="font: inherit;">My previous program has quite a few bugs. I fixed them in the program below. However, both programs are very slow in Clean. As I told before, I believe that the problem lies in the Mersennetwister library. In order to see how slow it is, change the population size to something like 5000.<br><br><br>module gp;<br>import StdEnv, MersenneTwister, ArgEnv, StdTime;<br><br>:: Op = AND | OR | NOT;<br>:: Tree= L Int | T Op [Tree];<br>::TT :== [(Bool,Bool,Bool)];<br>::Stt= !{ psz::Int, beastSz::Int, seed::[Int], <br> thr::Real, fs::[Op]};<br> <br>table= [(False, False, False),<br> (True, False, True),<br> (True, True,
False),<br> (False, True, True)];<br><br>Start w<br> # (ct,w)= getCurrentTime w;<br> seed= 1+ct.seconds+ct.minutes*60;<br> xs= genRandInt seed;<br> st = { psz=popsz,beastSz=5, seed= xs, <br> fs= [OR, AND, NOT], thr=4.0};<br> (p, st) = gen0 population st;<br> (gate, st)= evolve 30 p (L 0) st; <br> = gate;<br>where {<br> popsz= 50;<br> population :: .{!Tree};<br> population = createArray popsz (L 0);<br>}<br>rn n st=:{seed}<br> # [x:seed] = seed; <br> = ((abs x) rem n, {st&seed= seed});<br>rnLen (T _ s) st= rn (length s) st;<br><br>nGates (L i)= 0.0;<br>nGates (T p xs) = -0.1 + sum[nGates g \\ g <- xs];<br><br>run :: Tree {Bool} -> Bool;// Interpreter<br>run (L i) v= v.[i];<br>run (T AND
xs) v = and [run c v \\ c <- xs];<br>run (T OR xs) v= or [run c v \\ c <- xs];<br>run (T NOT [t:_]) v= not (run t v);<br><br>mutate e (L i, st) = (e, st);<br>mutate e (t, st) = ins t (rnLen t st) where { <br> ins (T p [x:xs]) (n, st) | n > 0 <br> # (T _ mt, st)= ins (T p xs) (n-1, st);<br> = (T p [x:mt], st);<br> ins (T p [L i:xs]) (0, st)=(T p[e:xs], st); <br> ins (T p [t:xs]) (0,st) <br> # (coin, st)= rn 2 st<br> | coin==0 = (T p [e:xs], st);<br> # (xpr, st)= mutate e (t, st);<br> = (T p [xpr:xs], st); }<br><br>crossover e1 e2 st<br> # (g1, st) = frag (e1, st);<br> (g2, st) = frag (e2, st);<br> (c1, st) = mutate g1 (e2, st);<br> (c2, st) = mutate g2 (e1, st);<br> = ([c2, c1], st) where{<br> frag (L i,
st) = (L i, st);<br> frag (T p xs, st)<br> # (n, st)= rnLen (T p xs) st;<br> # xpr = xs!!n;<br> # (coin, st)= rn 2 st;<br> | coin== 0= (xpr, st);<br> = frag (xpr, st); }<br><br>rndXpr st=:{fs, beastSz}= loop beastSz st where {<br> rth s st<br> # (n, st) = rn (length s) st; <br> = (s!!n, st);<br> fxy NOT st<br> # (n, st)= rn 2 st;<br> = (T NOT [L n], st);<br> fxy AND st = (T AND [L 0, L 1], st);<br> fxy OR st = (T OR [L 0, L 1], st);<br> loop n st | n<1 <br> # (fn, st)= rth fs st;<br> # (f, st)= fxy fn st;<br> = (f, st);<br> loop n st<br> # (f1, st) = loop (n-1) st;<br> # (fn, st) = rth fs st; <br> # (e, st) = fxy fn st;<br> = mutate e (f1, st); }<br><br>gen0 population st=:{psz, fs}=
loop population 0 st where {<br> loop p i st | i >= size p = (p, st);<br> loop p i st<br> # (g, ts)= rndXpr st;<br> = loop {p&[i]=g} (i+1) st;}<br><br>fitness gt= ng+1.0+sum[ft t \\ t <- table] <br>where{ ng= nGates gt;<br> ft (out, t1, t2) | run gt {t1, t2} == out= 1.0;<br> ft _ = 0.0; }<br> <br>evolve n p b st | n < 1 <br> # (p, st)= gen0 p st;<br> = evolve 30 p b st;<br>evolve n p b st=:{thr, fs, psz} <br> # (n1, st)= rn psz st;<br> (n2, st)= rn psz st;<br> (g1, p) = p![n1];<br> (g2, p) = p![n2];<br> ([c1, c2:_], st) = crossover g1 g2 st;<br> p= insrt c1 p 0 psz;<br> p= insrt c2 p 0 psz;<br> (g, p)= best 0 b p;<br> f= fitness g;<br> | f>thr
= (g, st);<br> = evolve (n-1) p b st;<br> <br> best i res p | i >= size p= (res, p);<br> best i fg1 p =:{[i]=fg}<br> | (fitness fg) > (fitness fg1) = best (i+1) fg p;<br> = best (i+1) fg1 p;<br> <br> insrt g v i sz | i >= sz = v;<br> insrt g v=:{[i]=a} i sz <br> | (fitness g) > (fitness a) = {v&[i]=g};<br> = insrt g v (i+1) sz;<br></td></tr></table><br>
<hr size=1> <a href="http://ca.promos.yahoo.com/jacko/">Get the name you've always wanted </a>! <strong>@ymail.com </strong>or <strong>@rocketmail.com</strong>.