Guest User

Untitled

a guest
Mar 10th, 2016
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.56 KB | None | 0 0
  1. local data={{1,0,0},{0,1,0},{0,0,1}} -- starting colors
  2.  
  3. local hidden={} -- hidden layers node counts
  4.  
  5. -- generate random goal colors
  6. local goal={}
  7. math.randomseed(tick())
  8. for i=1,10 do goal[#goal+1]={math.random(),math.random(),math.random()}end
  9.  
  10. local outputs=#goal
  11. local inputs=#data
  12. function createnetwork()
  13.     local weights={}
  14.     local network={weights=weights}
  15.     do
  16.         local input={}for i=1,inputs do input[i]=0 end network[1]=input
  17.         for h=1,#hidden do local layer={}for i=1,hidden[h]do layer[i]=math.random() end network[h+1]=layer end
  18.         local out={}for i=1,outputs do out[i]=0 end network[#network+1]=out
  19.     end
  20.     for i=1,#network-1 do
  21.         local t,b={},#network[i+1]
  22.         for j=1,#network[i]do
  23.             local x={}
  24.             for k=1,b do
  25.                 x[k]=math.random()
  26.             end
  27.             t[j]=x
  28.         end
  29.         weights[i]=t
  30.     end
  31.  
  32.     return network
  33. end
  34.  
  35. -- random(-1 to 1) / 10^random(-1 to 6; curved)
  36. function offset()return(math.random()*2-1)/10^((math.random()*7^(1/3))^3-1)end
  37.  
  38. function alternetwork(old,f)
  39.     local nweights=old.weights
  40.  
  41.     local weights,paths={},{}
  42.     local new={weights=weights}
  43.     do
  44.         local input={}for i=1,inputs do input[i]=0 end new[1]=input
  45.         for h=1,#hidden do local layer={}for i=1,hidden[h]do layer[i]=old[h+1][i]paths[#paths+1]={layer,i}end new[h+1]=layer end
  46.         local out={}for i=1,outputs do out[i]=0 end new[#new+1]=out
  47.     end
  48.     for i=1,#new-1 do
  49.         local t,b={},#new[i+1]
  50.         for j=1,#new[i]do
  51.             local x={}
  52.             for k=1,b do
  53.                 x[k]=nweights[i][j][k]
  54.                 paths[#paths+1]={x,k}
  55.             end
  56.             t[j]=x
  57.         end
  58.         weights[i]=t
  59.     end
  60.  
  61.     if f then
  62.         f(paths)
  63.     else
  64.         for i=1,math.random(1)do
  65.             local p=paths[math.random(#paths)]
  66.             --p[1][p[2]]=p[1][p[2]]+offset() -- unbounded
  67.             p[1][p[2]]=math.max(0,math.min(1,p[1][p[2]]+offset()))
  68.         end
  69.     end
  70.  
  71.     return new,#paths
  72. end
  73.  
  74.  
  75. function readn(network,val)
  76.     local weights=network.weights
  77.     local net,paths={weights=weights},{}
  78.     do
  79.         local input={}for i=1,inputs do input[i]=0 end net[1]=input
  80.         for h=1,#hidden do local layer={}for i=1,hidden[h]do layer[i]=network[h+1][i]end net[h+1]=layer end
  81.         local out={}for i=1,outputs do out[i]=network[#network][i]end net[#net+1]=out
  82.     end
  83.     for i=1,#val do
  84.         local v=val[i]
  85.         local b=network[1][i]
  86.         net[1][i]={math.min(1,v[1]+b),math.min(1,v[2]+b),math.min(1,v[3]+b)}
  87.     end
  88.     for i=1,#net-1 do
  89.         local n1,n2,w=net[i],net[i+1],weights[i]
  90.         local t={}
  91.         for j=1,#n2 do
  92.             local v=n2[j]
  93.             local v={v,v,v}
  94.             for k=1,#n1 do
  95.                 for q=1,3 do
  96.                     v[q]=math.min(1,v[q]+n1[k][q]*w[k][j])
  97.                 end
  98.             end
  99.             t[j]=v
  100.         end
  101.         net[i+1]=t
  102.     end
  103.     local out=net[#net]
  104.     for i=1,outputs do
  105.         for j=1,3 do
  106.             out[i][j]=math.min(1,out[i][j])
  107.         end
  108.     end
  109.     return out,net
  110. end
  111.  
  112. function eval(network)
  113.     local s=0
  114.     local outs,net=readn(network,data)
  115.     for k,a in ipairs(outs)do
  116.         local dr,dg,db=goal[k][1]-a[1],goal[k][2]-a[2],goal[k][3]-a[3]
  117.         s=s+math.sqrt(3)-math.sqrt(dr*dr+dg*dg+db*db)
  118.     end
  119.     if math.random(1,20)==1 then wait()end
  120.     return s,net
  121. end
  122.  
  123. function percent(n)
  124.     return 100*n/(math.sqrt(3)*outputs)
  125. end
  126.  
  127. local gui=workspace:FindFirstChild("Model")or Instance.new("Model",workspace)
  128. function display(network)
  129.     gui:ClearAllChildren()
  130.     local layers=#network
  131.     local prev={}
  132.     for h=1,layers do
  133.         local cur={}
  134.         local hidden=network[h]
  135.         for i=1,#hidden do
  136.             local r,g,b=hidden[i][1],hidden[i][2],hidden[i][3]
  137.             local e=Instance.new("Part",gui)
  138.             e.TopSurface,e.BottomSurface="Smooth","Smooth"
  139.             e.Anchored,e.CanCollide=true,false
  140.             e.Size=Vector3.new(5,.2,5)
  141.             e.CFrame=CFrame.new(h*30,.2,(i-#hidden/2)*6)
  142.             e.Transparency=1
  143.             local m=Instance.new("SurfaceGui",e)
  144.             m.Face="Top"
  145.             local f=Instance.new("Frame",m)
  146.             f.Size=UDim2.new(1,0,1,0)
  147.             f.BorderSizePixel=0
  148.             f.BackgroundColor3=Color3.new(r,g,b)
  149.             cur[#cur+1]=e
  150.         end
  151.         if prev then
  152.             local weights=network.weights
  153.             for i=1,#prev do
  154.                 for j=1,#cur do
  155.                     local w=weights[h-1][i][j]
  156.                     local e=Instance.new("Part",gui)
  157.                     e.TopSurface,e.BottomSurface="Smooth","Smooth"
  158.                     e.Anchored,e.CanCollide=true,false
  159.                     local p1,p2=prev[i].Position,cur[j].Position
  160.                     local width=math.abs(w)
  161.                     e.BrickColor=BrickColor.new(w>0 and"White"or"Black")
  162.                     local b=Instance.new("BlockMesh",e)
  163.                     b.Scale=Vector3.new(width,width,1)
  164.                     e.Size=Vector3.new(1,1,(p1-p2).magnitude)
  165.                     e.CFrame=CFrame.new((p1+p2)/2,p2)
  166.                 end
  167.             end
  168.         end
  169.         prev=cur
  170.     end
  171.     for i=1,#goal do
  172.         local r,g,b=goal[i][1],goal[i][2],goal[i][3]
  173.         local f=Instance.new("Frame",prev[i].SurfaceGui)
  174.         f.Size=UDim2.new(.5,0,.5,0)
  175.         f.Position=UDim2.new(.25,0,.25,0)
  176.         f.BorderSizePixel=0
  177.         f.BackgroundColor3=Color3.new(r,g,b)
  178.     end
  179. end
  180.  
  181. local network=createnetwork()
  182. local best,score=network,eval(network)
  183.  
  184. function try(network)
  185.     local s,net=eval(network)
  186.     if s>score then
  187.         best,score=network,s
  188.         display(net)
  189.         print(percent(score))
  190.     end
  191.     return percent(score),percent(s)
  192. end
  193.  
  194.  
  195. -- generate some random networks
  196. for i=1,100 do
  197.     try(createnetwork())
  198. end
  199.  
  200. -- alter values until local min
  201. for i=1,math.huge do
  202.     local net,paths=alternetwork(best)
  203.     local f=offset()
  204.     try(alternetwork(best,function(t)
  205.         local p=t[math.random(#t)]
  206.         p[1][p[2]]=math.max(0,math.min(1,p[1][p[2]]+f))
  207.     end))
  208.     try(alternetwork(best,function(t)
  209.         local p=t[math.random(#t)]
  210.         p[1][p[2]]=math.max(0,math.min(1,p[1][p[2]]-f))
  211.     end))
  212.     for f=1,10 do
  213.         for x=1,paths do
  214.             try(alternetwork(best,function(paths)
  215.                 local p=paths[x]
  216.                 p[1][p[2]]=math.max(0,math.min(1,p[1][p[2]]+2^-f))
  217.             end))
  218.             try(alternetwork(best,function(paths)
  219.                 local p=paths[x]
  220.                 p[1][p[2]]=math.max(0,math.min(1,p[1][p[2]]-2^-f))
  221.             end))
  222.         end
  223.     end
  224. end
Add Comment
Please, Sign In to add comment