+ Reply to Thread
Page 1 of 22 12311 ... LastLast
Results 1 to 10 of 216
Like Tree1Likes

Thread: The gBrain Project

  1. #1
    Wiremod Helper Lyinginbedmon's Avatar
    Join Date
    Mar 2009
    Location
    England
    Posts
    2,659

    Lightbulb The gBrain Project

    Summary
    The gBrain (Or the "Garry's Mod Brain", not necessarily "global variable brain") is an effort to create an artificial "brain" in Garry's Mod which can learn to perform tasks and avoid harm, based on the abilities of any drone chassis it is attached to. This will mean you can build whatever chassis you want, and it will figure out how to operate and function effectively without the need for you to go into the code and spend hours making a complex expression to control it. Essentially, it's an open source drone operating system with the capacity to adapt.

    Composition
    The gBrain is presently structured as two separate halves with a central control core between them which mediates communication between the halves. Think of these as the left and right hemispheres and the cerebellum (I'm not a medical professional of any sort, don't quote me on these relations).

    Side A: Intuition
    One half orchestrates at-the-moment intuition and guestimation, taking the various inputs involved with a current decision and using it's experience from the past and evolution of thought processes to decide what should be done.
    I'm currently modelling this side of the gBrain as an artificial neural network, attaching different clumps and populations of neurons to different actions such that there is little branching between, for example, "turn left" and "extend arm".

    Side B: Patterns
    The other half is the pattern recognition and development portion of the brain, which is far more computer-like than the intuition half, as it is predominantly concerned with developing efficiency in sequences of actions, not singular actions by themselves. This half is concerned with things like figuring out how to walk and to run and get from one place to another.
    I haven't started at all modelling this side, as I've mostly been considering how to populate the intution half first, though the development of patterns is rather simple and is detailed below already.

    The Central Core
    Inbetween the two is our "gCerebellum", the central control section, which mediates the uneasy truce between the two sides of the brain (The Intuition side saying almost blindly to do something or avoid something despite the Pattern's schedule, the Pattern side saying you have to do this next action on schedule despite the Intuition).

    It takes the outputs of the two brain halves and calculates which is more sure of itself, which is more certain that it's decided the right thing. If the intuition side is taking a wild guess, it ranks it's certainty fairly low, whilst the pattern side is very sure you should take that next action in your walk pattern. The control section then has the pattern side undertake the action decided from the two (Because the intuition uses basic definitions (such as "turn left") whereas the pattern side stores more complex measures to take (rotate left hip upwards in pitch, rotate left knee upwards in pitch, lean forwards, etc. for a single step))

    Development
    To have the intuition side learn from past mistakes and successes, we need to modify it's different weights and activation values. If you don't know what that means, I'll just briefly describe an artificial neuron.

    Intuition: Neurons
    Each neuron has N inputs and a single output. The values of each input is multiplied by a weight (The "importance" of the input) and if the sum of those resulting values is greater than the neuron's activation value, the output becomes 1. The network of neurons can be made to respond to different overall inputs by altering the weights and activation values, as neurons are linked in chains and webs between the input sensors and the central control chip. Each new layer of neurons stores its activation values in an array from which the next layer will react to. When the gBrain is finished running through one layer of neurons, it sets the previous array to be the current array and clears the current (Vals=Acts Acts=array()), completely ready for the next layer to work through them. Eventually, only a handful of neurons are left to decide and determine at last which action to undertake.

    Patterns: Sequence development
    The pattern side develops efficient sequences by determining a basic goal, generating a group of potential sequences, and seeing which one is the most efficient between time taken and degree of success.
    Genetic Algorithms
    To improve themselves, both sides use what are called "genetic algorithms", which are lines of code nearly incomprehensible to us players but which the gBrain fundamentally understands, and which are something akin to the way our cells "read" DNA to decide what to make and do.
    The genetic strings are broken apart into different segments for each neuron and has a given neuron's weights and activation values in a given segment, or for the pattern side has a more complex detail segment telling the pattern side which action to undertake, on what, where, and to what extent (Differentiating "set discovered NPC entity to be target entity" and "rotate left hip upward in pitch locally by 9 degrees").
    • For the intuition side, the central core tells it "good" and "bad" and depending on that response the genes will alter to avoid that outcome again or to more closely respond positively to it, by changing the weights and activation values along the way.
    • For the pattern side, the more successful a given sequence, the higher the likelihood it will be "bred" into the next generation of sequences, there is also a small chance of mutation for each portion of a sequence to allow for otherwise unencountered decisions, and eventually the sequences become very efficient and the pattern side picks the most efficient from them.
    Statement of Intent
    Like Cronus, this is an OPEN PROJECT! That means that everyone can get involved, test out their theories, and work together as a community to get this working for the benefit of all. I'll post up the presently-accepted codes in the second post of this thread and modify details under the headings in this one as the project develops, but I'm merely the book keeper here, though I will lend my thoughts to the discussions, don't consider me the end-all be-all.

    And with that, let's see just what Wiremod can do with this delve into artificial intelligence in Garry's Mod. I'm sure we'll see some amazing things.
    Last edited by Lyinginbedmon; 02-23-2010 at 09:45 AM.

  2. #2
    Wiremod Helper Lyinginbedmon's Avatar
    Join Date
    Mar 2009
    Location
    England
    Posts
    2,659

    Default Re: The gBrain Project

    ==Current Codes==
    =Implementation=

    Backpropagation neuron chip
    Houses code for 1 neuron, to be networked and such in a physical non-loop model. Presently only functions properly in about 1 in 6 runs due to unknown code glitches, which also seem to hinder networking.
    Code:
    @name BProp neuron
    @inputs [In1 In2 In3 In4 In5 In6 In7 In8 In9 In10] End
    @inputs [Ou1 Ou2 Ou3 Ou4 Ou5 Ou6 Ou7 Ou8 Ou9 Ou10]
    @persist Weights:array N ID It NetOut Learn
    @outputs OUT ID
    
    if(first()){
            #Learning rate
        Learn=0.25
            #T is the target, a string (to allow learning towards zero)
        
        ID=entity():id()
        
            #Initial randomised weights
        Weights=array()
        for(V=1,10){
            Weights:pushNumber(round(random(-1,1),2))
        }
        
            #Indication holograms
            B=entity():toWorld(vec(0,0,5))
            A=entity():angles()
        holoCreate(1,B,vec(),A) holoCreate(2,B,vec(),A)
        holoModel(1,"pyramid") holoModel(2,"pyramid")
        holoScaleUnits(1,vec(1,1,1)) holoScaleUnits(2,vec(5,5,5))
        holoParent(1,entity()) holoParent(2,entity())
        holoAlpha(2,170)
    }
    interval(150)
        #Input data (previous layer) and forward data IDs (next layer)
        Input=array() IDs=array()
    Input:pushNumber(gGetNum(In1))    IDs:pushNumber(Ou1)
    Input:pushNumber(gGetNum(In2))    IDs:pushNumber(Ou2)
    Input:pushNumber(gGetNum(In3))    IDs:pushNumber(Ou3)
    Input:pushNumber(gGetNum(In4))    IDs:pushNumber(Ou4)
    Input:pushNumber(gGetNum(In5))    IDs:pushNumber(Ou5)
    Input:pushNumber(gGetNum(In6))    IDs:pushNumber(Ou6)
    Input:pushNumber(gGetNum(In7))    IDs:pushNumber(Ou7)
    Input:pushNumber(gGetNum(In8))    IDs:pushNumber(Ou8)
    Input:pushNumber(gGetNum(In9))    IDs:pushNumber(Ou9)
    Input:pushNumber(gGetNum(In10))    IDs:pushNumber(Ou10)
        #For ease of reference and collection, stored in arrays
    
        #Output production
    O=0
    for(V=1,Input:count()){
        O+=Input[V,number]*Weights[V,number]
    }
    OUT=1/1-exp(-O)
        gSetNum(ID,OUT)
        #Network output definer (Wire 1 into End if multiple chips)
    if(!IDs:sum()|End){gSetNum("NetOut",OUT)}
    
        #Backpropagation
    NetOut=gGetNum("NetOut")    #Network output
    N=gGetStr("Target"):toNumber()   #Target output
    Error=N-NetOut  #Network error
    if($N & Error){
            #Forward alteration
        if(IDs:sum()){
            Fo=0
            for(V=1,IDs:count()){
                Fo+=gGetNum(IDs[V,number])
            }
            For=Fo/Input:count()
            Mod=Learn*Error*OUT*For*(1-For)
        }
        else{
            Mod=Learn*Error*OUT
        }
        
            #Weight modification
        for(V=1,Input:count()){
            W=Weights[V,number]+Mod*Input[V,number]
            Weights[V,number]=round(W,4)
        }
        
        S=round((1-Error)/(!N ? 1 : N),2)*5
        holoScaleUnits(1,vec(S,S,S))
        if(S>5|S<0){holoColor(2,vec(1,0,0)*255)}
        else{holoColor(2,vec(1,1,1)*255)}
    }
    
    
        #Rebooting simultaneous with nearby training chip
    runOnSignal("Start",1,1)
    Dis=signalSender():pos():distance(entity():pos())
    if(signalClk("Start") & Dis<=100){
        reset()
    }
    Code:
    @name BProp Learner
    @outputs A B
    @persist N Err AE BE
    
    if(first()){
        N=1
        gDeleteAll()
        A=entity():id()
        B=A*10
        holoCreate(1,entity():toWorld(vec(0,0,10)))
        holoCreate(2,entity():toWorld(vec(0,0,10)))
        holoScaleUnits(1,vec(3,3,10))
        holoScaleUnits(2,vec(3,3,1))
        holoAlpha(1,170)
        
        signalSend("Start",1)
    }
    interval(150)
    
    if(N){
        BE=(gGetStr("Target"):toNumber()-gGetNum("NetOut"))^2
        gSetNum(A,0)
        gSetNum(B,1)
        gSetStr("Target","0")
    }
    else{
        AE=(gGetStr("Target"):toNumber()-gGetNum("NetOut"))^2
        gSetNum(A,1)
        gSetNum(B,1)
        gSetStr("Target","1")
    }
    N=!N
    
    if(AE & BE){
        Err=round(AE+BE,2)
        Cap=min((1-Err),1)
        Cap=max(Cap,0)*10
        holoScaleUnits(2,vec(3,3,Cap))
    }
    Forwardpropagation neuron chip
    Houses code for 1 neuron with forward propagation. Works well in singular and networked states on a ground-level physical model.
    Code:
    @name Neuron
    @inputs V1 V2 Target Reset
    @persist W1 W2 Learn It Train:entity
    @persist IN1 IN2 N Wire
    @outputs OUT
    
    if(first()){
        Wire=0
            #If null, globals from the Train chip
            #otherwise, from the input wires
        
            #Learning rate
        Learn=0.25
            #T is the target, a string (to allow learning towards zero)
        
            #Initial randomised weights
        W1=round(random(-1,1),2)
        W2=round(random(-1,1),2)
            print("Weight 1: "+W1)
            print("Weight 2: "+W2)
        
            #Indication holograms
            B=entity():toWorld(vec(0,0,5))
        holoCreate(1,B) holoCreate(2,B)
        holoModel(1,"pyramid") holoModel(2,"pyramid")
        holoScaleUnits(1,vec(1,1,1)) holoScaleUnits(2,vec(5,5,5))
        holoParent(1,entity()) holoParent(2,entity())
        holoAlpha(2,170)
        
        if(!Wire){
            holoCreate(3,entity():toWorld(vec(0,0,1)))
            holoParent(3,entity()) holoModel(3,"hqcone")
            holoScaleUnits(3,vec(3,3,7))
                Train=owner()
        }
    }
    interval(150)
        #Training data
    if(!Wire){
            T=gGetStr(Train:id()*3+3):explode("/")
        IN1=T[1,string]:toNumber()
        IN2=T[2,string]:toNumber()
        N=T[3,string]:toNumber()  #Target output number
    }
    else{
        IN1=V1
        IN2=V2
        N=Target
    }
    
        #Output production
    O=(IN1*W1)+(IN2*W2)
    OUT=1/1-exp(-O)
    
        #Forward propagation
    if($N){
        Error=N-OUT
        
        W1+=(Learn*IN1*Error) #Modify weight 1
        W2+=(Learn*IN2*Error) #Modify weight 2
        
            #Scale indication hologram
        S=((1-Error)/(!N ? 1 : N))*5
        holoScaleUnits(1,vec(S,S,S))
        
        if(Error){
            holoColor(1,vec(1,1,1)*255)
            It=1
        }
        else{
            holoColor(1,vec(0,1,0)*255)
            if(It){
                holoEntity(1):soundPlay(1,0,"buttons/blip1.wav")
                soundPitch(1,150)
                
                It=0
            }
        }
        
        if(N){gSetNum(Train:id()*3+1,Error)}
        else{gSetNum(Train:id()*3+2,Error)}
    }
    
        #Rebooting simultaneous with nearby training chip
    runOnSignal("Start",1,1)
    Dis=signalSender():pos():distance(entity():pos())
    if(signalClk("Start") & Dis<=100 & !Wire|Wire & Reset & $Reset){
        W1=round(random(-1,1),2)
        W2=round(random(-1,1),2)
            Train=signalSender()
    }
    V=Train:pos()-holoEntity(3):pos()
    holoAng(3,V:toAngle()+ang(90,0,0))
    Code:
    @persist T:string IN1 IN2
    @persist A Net Stop
    
    if(first()){
        A=1
        
            #Net error computation
        gDeleteAll()
        signalSend("Start",1)
        Net=100
        
            #Indication holograms
            B=entity():toWorld(vec(0,0,5))
        holoCreate(1,B) holoCreate(2,B)
        holoModel(1,"hqicosphere2") holoModel(2,"hqicosphere2")
        holoParent(1,entity()) holoParent(2,entity())
        holoScaleUnits(1,vec(1,1,1)) holoScaleUnits(2,vec(3,3,3))
        holoAlpha(2,170)
    }
    interval(150)
    
    if(A){
        IN1=1
        IN2=1
        T="1"
        A=0
    }
    else{
        IN1=0
        IN2=1
        T="0"
        A=1
    }
        gSetStr(entity():id()*3+3,IN1+"/"+IN2+"/"+T)
    
    
    if(A & $A){
            #Net error
        Net=gGetNum(entity():id()*3+1)^2
        Net+=gGetNum(entity():id()*3+2)^2
        
        if(round($Net,2)){print("Net error: "+round(Net,2))}
        
        S=(1-Net)*3
        holoScaleUnits(1,vec(S,S,S))
    }
    =Testing=
    Holo-drone for testing Neural Network concepts[highlight=E2]@name gBrain neural net test drone
    @inputs R L F B
    @outputs Ins:array
    @persist H:entity [Dow Rv Bv]:vector

    if(first()){
    S=rangerOffset(6000,entity()os(),vec(0,0,-1))osition()

    holoCreate(1,S+vec(0,0,5),vec(1,1,1),ang())
    H=holoEntity(1)
    runOnTick(1)

    Hcen=H:toWorld(H:boxCenter())
    holoCreate(2,Hcen-vec(0,7,0),vec(1,1,1)/4,ang(90,-90,0))
    holoCreate(3,Hcen+vec(0,7,0),vec(1,1,1)/4,ang(90,90,0))
    holoCreate(4,Hcen+vec(7,0,0),vec(1,1,1)/4,ang(90,0,0))
    holoCreate(5,Hcen-vec(7,0,0),vec(1,1,1)/4,ang(-90,0,0))
    holoCreate(6,Hcen+vec(6,0,0),vec(2,2,3)/4,ang(90,0,0))
    holoCreate(7,vec(),vec(1,1,1)/8,ang())

    holoParent(2,1) holoParent(3,1) holoParent(4,1)
    holoParent(5,1) holoParent(6,1)

    Co="hqcone"
    holoModel(2,Co) holoModel(3,Co) holoModel(4,Co)
    holoModel(5,Co) holoModel(6,"hqtorus2") holoModel(7,"hqicosphere2")

    Rv=vec(1,0,0)*255
    Bv=-vec(1,1,1)*255
    Dow=vec(0,0,1)
    }

    ##Operation##
    #Sensory perception
    Hcen=H:toWorld(H:boxCenter())
    Forw=vec(1,0,0):rotate(ang(0,H:angles():yaw(),0))
    Righ=vec(0,1,0):rotate(ang(0,H:angles():yaw(),0))
    Fo=rangerOffset(500,Hcen,Forw)
    Ba=rangerOffset(500,Hcen,-Forw)
    Ri=rangerOffset(500,Hcen,Righ)
    Le=rangerOffset(500,Hcen,-Righ)
    rangerHitWater(1)
    Do=Forw-Dow/3
    Wa=rangerOffset(500,Hcen,Do)
    Wat=Wa:distance()
    holoPos(7,Hcen+Do*Wa:distance()+2)
    Wet=holoEntity(7):isUnderWater()

    Fod=Fo:distance()
    Bad=Ba:distance()
    Rid=Ri:distance()
    Led=Le:distance()

    holoColor(2,(Led/500)*(Led<=15 ? Rv : Bv))
    holoColor(3,(Rid/500)*(Rid<=15 ? Rv : Bv))
    holoColor(4,(Fod/500)*(Fod<=15 ? Rv : Bv))
    holoColor(5,(Bad/500)*(Bad<=15 ? Rv : Bv))

    #Sensory outputs
    Ins[1,number]=Fod #Forward distance
    Ins[2,number]=Bad #Reverse distance
    Ins[3,number]=Rid #Right distance
    Ins[4,number]=Led #Left distance
    Ins[5,number]=Wet #Whether Wa ranger is hitting water
    Ins[6,number]=Wat #Forward distance to water

    #Good/Bad responses
    A=F & Fod<=15|F & Wet & Wat
    #Going forward with little room
    #Similarly, going forward towards nearby water
    B=B & Bad<=15
    #Reversing with little room
    C=H:isUnderWater()
    #If the drone has fallen into the water
    D=B & Fod<=15|B & Wet & Wat
    #Moving away from water and cramped spaces
    E=F & Bad<=15
    #Moving away from backing walls/objects

    if(A|B|C){ #Bad
    Out=-1
    if(C){reset()} #Drone "dies" in water
    }
    elseif(A|B){ #Good
    Out=1
    }
    else{Out=0} #Neutral response

    #Functional motion
    An=H:angles()+ang(0,R-L,0)
    holoAng(1,An)

    Do=rangerOffset(500,Hcen,vec(0,0,-1))osition()+vec(0,0,5)
    Do+=Forw*(F-B)
    holoPos(1,Do)[/highlight]
    Last edited by Lyinginbedmon; 03-23-2010 at 10:34 AM.

  3. #3
    Wiremod Helper Lyinginbedmon's Avatar
    Join Date
    Mar 2009
    Location
    England
    Posts
    2,659

    Default Re: The gBrain Project

    So here's my current brief model of a neuron as part of a loop, such that the code could run through a bunch of neurons having first broken apart the neuron gene string to give an array list of the neuron data
    Code:
    Weights=Neur[N,string]:explode(".")   #The weights extracted from the neuron data array
    Act=Weights[Weights:count(),string]:toNumber()   #The activation value of the neuron we're looking at
    Ins=min(Vals:count(),Weights:count()-1) #The cap of input values versus inputs of the neuron (The final value being activation)
    
    M=1
    while(M<=Ins){
         Sum+=Vals[M,number]*Weights[M,string]:toNumber()   #Each value successively weighted and added to the Sum
         M++
    }
    if(M>=Vals:count()){
         if(Sum>=Act){Acts[N,number]=1}
         else{Acts[N,number]=0}
            #The Vals array becomes the Acts array once the loop is completed
         N++
    }
    Last edited by Lyinginbedmon; 02-23-2010 at 09:40 AM.

  4. #4
    Wirererer cooldudetb's Avatar
    Join Date
    Sep 2008
    Location
    UK
    Posts
    253

    Default Re: The gBrain Project

    At last, an AI project!

    Looking great lying, I hope I can help with this.
    [E2] Extended Timers

    Bulls pro-tip of the day:
    When you honestly think that Led3=1,0,1,0,1,0,1 can make a light blink, stop trying to make an OS and do something easier.

  5. #5
    Wirererer tepholman's Avatar
    Join Date
    Apr 2008
    Posts
    218

    Default Re: The gBrain Project

    I'm from the future to warn you! DON'T CONTINUE THIS PROJECT! It became sentient and took over all of our E2 chips!

    In all seriousness, this is ambitious, but it could work...

  6. #6
    Wirererer cooldudetb's Avatar
    Join Date
    Sep 2008
    Location
    UK
    Posts
    253

    Default Re: The gBrain Project

    (Almost) Nothing is impossible.

    The only real limit on this project is the processing power of your everyday desktop computer.
    [E2] Extended Timers

    Bulls pro-tip of the day:
    When you honestly think that Led3=1,0,1,0,1,0,1 can make a light blink, stop trying to make an OS and do something easier.

  7. #7
    billywitchdoctor.com Schilcote's Avatar
    Join Date
    Jan 2009
    Location
    There.
    Posts
    2,006

    Default Re: The gBrain Project

    (my inner monologe is in a debate-style, with two voices arguing with eachother. It's just how I think...)

    1:gBrain? What's this?
    1:Oh, someone wants to make a neural network in Gmod. Ha. Ridiculous.
    2:It's being coded by Lyinginbedmon.
    1:Oh.

    I'm very interested in this project. I've dabbled in genetic algorithms before, with the intent of learning neural networks. I have a little bit of experience with them...

    EDIT:

    Oh, and you should call the sections of the brain Appolo, Dinoysious, and Cygnus, after the storyline of Cygnus X-1 Book 2 (Last.fm seems to have asploded, but it's on the album Hemispheres by Rush).
    Last edited by Schilcote; 02-23-2010 at 11:04 AM.


    [19:16:47]Client "rcdraco" spawned in server
    [19:17:10]rcdraco: hamburgertime
    [19:18:04]rcdraco was killed by worldspawn
    [19:21:50]Dropped "rcdraco" from server

  8. #8
    Wire Sofaking Solece's Avatar
    Join Date
    Jul 2008
    Location
    Pittsburgh, PA
    Posts
    673

    Default Re: The gBrain Project

    Awesome, this project looks very promising.

  9. #9
    Lifetime Supporter Nikita's Avatar
    Join Date
    May 2009
    Posts
    769

    Default Re: The gBrain Project

    Awesome... but how in the world did you come up with all that? Do you have 11 PhD's or something?

    I don't want to promise anything, but would the project benefit from a partial ontology of English language? (Aka dictionary or common sense database)

  10. #10
    Wiremod Helper Lyinginbedmon's Avatar
    Join Date
    Mar 2009
    Location
    England
    Posts
    2,659

    Default Re: The gBrain Project

    Quote Originally Posted by Nikita View Post
    Awesome... but how in the world did you come up with all that? Do you have 11 PhD's or something?
    I think about stuff. A lot.

    Probably more than is healthy in many instances.
    Quote Originally Posted by Nikita View Post
    I don't want to promise anything, but would the project benefit from a partial ontology of English language? (Aka dictionary or common sense database)
    For what purpose exactly? Getting the brain to speak would be awesome but would require quite a bit of learn time, and it's mostly intended for operational rather than interactional learning.

    The Olympus Technologies drones, totally not SkyNet in Gmod form.
    Cronus: The Ultimate Drone, definitely SkyNet in Gmod form.
    The gBrain Project, the drone controls system that thinks it's better than you

+ Reply to Thread
Page 1 of 22 12311 ... LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
proceed-collector
proceed-collector
proceed-collector
proceed-collector
linguistic-parrots
linguistic-parrots
linguistic-parrots
linguistic-parrots