`(i$$$666HHH[[[mmm D$404mNJNL04e$FHuqaY}},m,Ҫm^ 02TvͫgE#2Tv2Tvffffffffffffffffffffffffffffffff :33:33:033 :3333::033 :3::033 :::3:::033 [U[UUU[U[[PUU`fffaffffaffffafaff"("(("("(( "" DIDIDDIII@DD/"""""/""""" >=UODDD U@DDDDྻ  @DDDDww DDDzwww 4wwww 333 zwwwww0 33333 wwwwww 03:3辻Uwwwwww H33 [Uwwwwww H `Uwwwwww73h0[PUUwwwwww 333hI `ZUPUUzwwwww333Q `@uPUPUwwww333Q0Э @wwuUUUUzwww eG zwwwZPUUUww v8 wwwwWUUUP _ ]H zwwwwwuPUUPPP Qm@p7`wwwwwwWWUPP eep wwwwwwWP PF @0DIDDDDDwwwwwwWzPPPPF H[dDDDsG`@[wwwwwwWuPPe  Q`0p  "wwwwwwWUPPP`ww Mv)""uzwwwwwPpUUUU _UF|www 00w " խwwwwzPpUUUUUwwww5 Pl#"w@ ݪzwww wUUUUP`|wwwww 1P L""0wYРwwg uPP UUUwwwwww  0 & w تwUPFwwwwww 3Y!%k@0@ w}U PuP`@wwwwww  2U`@ðw Y `PwP`Uwwwwww Y 0PwP pp_UUUwwwwww 3 Uk@Vpp UUUP|wwwww̙ 3_=ّe ݰ=[ eYUU`@pwwwww31Yݽ}nPUUW pP`wp|www 0Y D=^VpPwwwp0_X`[մP pWp uPS]Kp8`P pw`Pp p3g01SֻENdPpw@ 0_pU0U` ]%F ypptPp0X\Ul@2`@Aw`w`_pUMUPT%"] `` PDw`wwP""s_̇P5Pm$R Pt9P=wffv wmwP`I p3! 4nH4P1g#Kffffp"P pgY~f`"{ nUXSq9i\5cfh|F 33p" 3p0nVuS1IE v'Pfow w  d&3333`" 0 8`nЈ3RlrgfxfwWl` Py@b93ww73'0pX5Wi`fw `Pp f3swwwc"p 8[`U35y)Ffx, @ 03wwww73"0pwwSXfhP` mp9swwwww"p 0wwww5 6XQ\ if̌ ` `]@p3wwwwww7 0ppw|wݰ3kxF5Y9fhV\3wwwwww7p0ww]3PPh>u9ufhV  P 3wwwwww7 0sw|w[ 35@؆31i,` p3wwwwwwgfffffvpwXPPm46QWpifV `.`p 3wwwwww7`gfffaw|SP83h0YǑffQ րA P !0swwwww3p`fffw|53=ݶȆ0 u|ifhf HL R"p03wwww73 w|8S 7T`+m(XŇfjVm Nb" p3swww33pw|=SYx|Z  ",  03ww730 w|Z} l(=Ywww @" &P 3333 0pDtwX=l 6]zwww ` " `R"p333@Apw|w"]Gڣ&hwwww`"@ `"  pwwx"p0bYzwwwww" "% 0GD pw|w[w"B\ b̀:QZYwwwwww'"@(P q 0Dtwwww ,R#͆0wwwwwwj@ `P q33 3@Dww u%R2mPwwwwwwD  i  3333# Cp w" ІXXUUYwwwwwwGD =3ww72@D0  tURU5Uր;wwwwwwg@DDD" `P 3swww3 D0p_UUuUU%`͈0aRuwwwwwkDDD `P93wwww3@D0 ̒P "((Uwwww @"@D0swwwww3D3 pO\r' 2&mUzwww   HDLF3wwwwwwG[pP"#`Vwwj@ .  `E3wwwwww7:|(R0"8`U@`P3wwwwww7 0pOL !{'&8 V@  V3wwwwww73L$"{P=``"3c5wwwwww pB$"0A" 03`5swwwww3 0DDp%"B pP`8"@ 33V3wwww730pDDDD("!D }1k8@233V3swww3 3GDwwGDXDQ433 `53ww73 3DtwwwDD p0`0 l3333 @DwwwwGD( Dw]c8@ V33@twwwwwDx D8@ V DwwwwwwG wpP;k lDwwwwwwG(yw @w  DwwwwwwG" w@P98@ ș DwwwwwwGэ pw @pc@ "" DwwwwwwG w@ P0 )""" @twwwwwD  pwIp5DD """" @DwwwwGD w@pPcDDDD. )""""" DtwwwDpw@EDwwG.̙"""""" @DwwGD wDpDtwwwB """""" DDDDXUUU pwDDwwwwD"""""" DD PUUUUwGtwwwww33333"""""" PUUUUDwwwwwwG333333""""""  UUUDwwwwww733333)"""""DwwwwwwG"""" UUUDwwwwwwG)""" UUUUUDwwwwwwG""PUUUU@twwwwwD _UUU@DwwwwGD DtwwwD@DwwGDDDDDDDZ-- title: Neural Wire -- author: fachi177 -- desc: train a neural network to run wireworld automata -- script: lua wmin,wmax=0,30*8 hmin,hmax=0,17*8 capas={} function mapeo(val,fromMin,fromMax,toMin,toMax) return (val-fromMin)*(toMax-toMin)/(fromMax-fromMin)+toMin end ---start of the neural network tool --this code is free to use rnd,exp=math.random,math.exp min,max=math.min,math.max function tanh(val, deriv) if deriv then return 1-val*val else return 2/(1+exp(-2*val))-1 end end function relu(val, deriv) if deriv then if val>=0 then return 1 else return 0 end else return max(0,val) end end function lrelu(val, deriv) if deriv then if val>=0 then return 1 else return 0.01 end else return max(val,val*0.01) end end function sigmoid(val, deriv) if deriv then return val*(1-val) else return 1/(1+exp(-val)) end end --learnig rate lr=0.1 --activation function --act=tanh --act=sigmoid act=lrelu neu={} neu.ron=function(input) local n={} for i=1,input do n[i]=rnd()*2-1 end n.bias=rnd()*2-1 return setmetatable(n,{__index=neu}) end neu.predict=function(n,inpu) local p=n.bias for i=1,#n do p=p+n[i]*inpu[i] end return act(p) end neu.train=function(n,inp,out,err) local er={} local grad=act(out,true)*err for i=1,#n do er[i]=n[i]*grad n[i]=n[i]-lr*grad*inp[i] end n.bias=n.bias-lr*grad return er end lay={} lay.er=function(input, neurons) local l={} for i=1,neurons do l[i]=neu.ron(input) end return setmetatable(l,{__index=lay}) end lay.predict=function(l,input) local p={} for i=1,#l do p[i]=l[i]:predict(input) end return p end lay.train=function(l,inp,out,err) local er={} for i=1,#inp do er[i]=0 end local ner={} for i=1,#l do ner=l[i]:train(inp,out[i],err[i]) for j=1,#er do er[j]=er[j]+ner[j] end end return er end --neural network constructor --input: number of input --neuByLay: an array with the number of neurons per layer netw={} netw.ork=function(input, neuByLay) local net={} local inp=input for i=1,#neuByLay do net[i]=lay.er(inp,neuByLay[i]) inp=neuByLay[i] end net.hid={} return setmetatable(net,{__index=netw}) end --Predict the result for input netw.predict=function(net,inputs) local p={} net.hid[0]=inputs for i=1,#net do net.hid[i]=net[i]:predict(net.hid[i-1]) end return net.hid[#net] end --train the network so that for input return target netw.train=function(net,input,target) local out=net:predict(input) local err={} for i=1,#out do err[i]=out[i]-target[i] end for i=#net,1,-1 do err=net[i]:train(net.hid[i-1],net.hid[i],err) end return out end ---end of the neural network tool capas={8,6,4} entra=12 net=netw.ork(entra,capas) capas[0]=entra inp={1,0,0,0,1,0,0,0,0,1,0,1} pre=net:predict(inp) error=0 gler=0 tra={ inp={0,1,1,1,0,0,0,1,0,0,0,0}, tar={1,0,0,0}, } tam=25 tamx=35 cli=false dra=1 gm={} for i=1,tamx do gm[i]={} for j=1,tam do --gm[i][j]=rnd(0,1) gm[i][j]={0,0,0,0} end end op={{1,0,0,0},{0,1,0,0},{0,0,1,0},{0,0,0,1}} wire={function(n)return 1 end, function(n)return 3 end, function(n) if n==1 or n==2 then return 4 else return 3 end end, function(n)return 2 end} function loadmap() gm={} for i=1,tamx do gm[i]={} for j=1,tam do --gm[i][j]=rnd(0,1) gm[i][j]=op[mget(i-1,j-1)] end end end nor=true function normal(ent) local max,val=0,-10 for i=1,4 do if val8 then if r==4 then ngb=ngb+1 tra.inp[j]=1 else tra.inp[j]=0 end end end tra.tar=op[wire[mid](ngb)] pre=net:train(tra.inp,tra.tar) error=0 for i=1,#pre do error=error+((pre[i]-tra.tar[i])^2)/2 end gler=gler+error end end if btn(4) then local ax={} for i=1,tamx do ax[i]={} for j=1,tam do local inp={} for k=-1,1 do for l=-1,1 do if k==0 and l==0 then for h=1,4 do table.insert(inp,gm[(i-1+k)%tamx+1][(j-1+l)%tam+1][h]) end else table.insert(inp,gm[(i-1+k)%tamx+1][(j-1+l)%tam+1][4]) end end end if not nor then ax[i][j]=net:predict(inp) else ax[i][j]=normal(net:predict(inp)) end end end gm=ax end if btnp(5) then loadmap() end if btnp(7) then reset() end -- for i=1,#pre do -- print(pre[i],1,(i-1)*8,mapeo(pre[i],-1,1,1,15)) -- end print(gler) for i=1,#net do for j=1,#net[i] do for k=1,#net[i][j] do -- print(net[i][j][k],(i-1)*100,(k-1)*8+(j-1)*8*#net[i][j],mapeo(net[i][j][k],-1,1,1,15)) line( mapeo(i-1,-1,#capas+1,wmin,wmax), mapeo(k,0,#net[i][j]+1,hmin,hmax), mapeo(i,-1,#capas+1,wmin,wmax), mapeo(j,0,capas[i]+1,hmin,hmax), mapeo(net[i][j][k],-1,1,1,15) ) end circ( mapeo(i,-1,#capas+1,wmin,wmax), mapeo(j,0,capas[i]+1,hmin,hmax), 8, mapeo(net[i][j].bias,-1,1,1,15) ) circ( mapeo(i,-1,#capas+1,wmin,wmax), mapeo(j,0,capas[i]+1,hmin,hmax), 6, 0 ) end end if #net.hid>0 then for i=0,#net.hid do for j=1,#net.hid[i] do circ( mapeo(i,-1,#capas+1,wmin,wmax), mapeo(j,0,capas[i]+1,hmin,hmax), 6, mapeo(net.hid[i][j],-1,1,1,15) ) end end end -- print(error,1,16*8) local cel=5 local ofx,ofy=50,0 if edit then for i=1,tamx do for j=1,tam do rect(ofx+i*cel,ofy+j*cel,cel,cel,mapeo(gm[i][j][1]+gm[i][j][2]*2+gm[i][j][3]*3+gm[i][j][4]*4,0.9,4.1,1,15)) end end tex={"empty","elec. tail","conductor","elec. head"} spr(10,5,50) spr(dra,20,50) print(tex[dra],0,60,1) if p or s then local cx,cy=(x-ofx)//cel,(y-ofy)//cel if cx>0 and cx<=tamx and cy>0 and cy<=tam then if not cli then cli=true end gm[cx][cy]=op[dra] end else cli=false end else for i=1,tamx do for j=1,tam do rect(170+i*2,j*2,2,2,mapeo(gm[i][j][1]+gm[i][j][2]*2+gm[i][j][3]*3+gm[i][j][4]*4,0.9,4.1,1,15)) end end end end