- Home /
Building a 3D Neural Network for Bot AI?
So the Idea
This is supposed to be a neural network in 3d cubes drag and drop at some point...
1...The cube sends out a Pulse (name,name,name,number) call function to each other cube / clone .
2...Each cube checks if the Pulse caries its name.
3...If it true it edits the number and adds the next 3 names.
4... Finally it sends a Pulse to the next script and function as a Pulse(name,name,name,number).
1-4..... this repeats until the pulse hits an output block or fails to pass all thresholds on the way.
Working On / Questions
A?...At the moment its a structure, and only transmits if you build and link each block by hand,
so how to make it link like a chain while instantiating to avoid the by hand part?
B?... How to do drag and drop cubes?
C?... what is wrong with the set parent functions i can not get the links to stay stable....note there is an edited set if instantiate calls that have a glitch in the nerveController script?
E?... what should i change the name of this question to...?
Notes And thank you Phil Rousse, for the awesome first response it is appreciated
Link to package: 77.81 kbs: 4 scripts, 2 folders, 3 pngs, 4 prefabs...very simple is the goal https://www.dropbox.com/s/jykmhw9prj42lh7/Neural%20Cubes.unitypackage
Nerve Controller
public class nerveController : MonoBehaviour
{
// Nerve cells use to send/recive Pulses
public inputCell linkInputCell;
public nerveCell linkNerveCell;
public outputCell linkOutputCell;
public Transform NerveController;
public Transform inputCell;
public Transform nerveCell;
public Transform outputCell;
public Vector3 grid=new Vector3(9.0f,9.0f,9.0f);
Vector3 location=new Vector3(0.0f,0.0f,0.0f);
void Awake(){
location= transform.position;
Instantiate (inputCell, new Vector3 (location.x+1, location.y+0, location.z+1), Quaternion.identity);
Instantiate (outputCell, new Vector3 (location.x+grid.x, location.y+grid.y+1, location.z+grid.z), Quaternion.identity);
for(int x=1;x<=grid.x;x++){
for(int y=1;y<=grid.y;y++){
for(int z=1;z<=grid.z;z++){
Instantiate (nerveCell, new Vector3(location.x+x, location.y+y, location.z+z), Quaternion.identity);
//GameObject instance = Instantiate (nerveCell, new Vector3(location.x, location.y, location.z), Quaternion.identity) as GameObject;
//instance.transform.parent = NerveController;
}
}
}
//GameObject instance = Instantiate (outputCell, new Vector3 (location.x+grid.x, location.y+grid.y+1, location.z+grid.z), Quaternion.identity) as GameObject;
//instance.transform.parent = NerveController;
//GameObject instance = Instantiate (inputCell, new Vector3 (location.x+1, location.y+0, location.z+1), Quaternion.identity) as GameObject;
//instance.transform.parent = NerveController;
}
}
Nerve Cells
public class nerveCell : MonoBehaviour
{
// Nerve cells use to send/recive Pulses
public inputCell linkInputCell;
public nerveCell linkNerveCell;
public outputCell linkOutputCell;
public float output = 0;
public float multiplyer;
public float threshhold;
public Vector3 Name = new Vector3 (0f, 0f, 0f);
Vector3 callNamea = new Vector3 (1f, 0f, 0f);
Vector3 callNameb = new Vector3 (0f, 1f, 0f);
Vector3 callNamec = new Vector3 (0f, 0f, 1f);
// awake so choose its settings for multi and thresh
float awake = 0;
void Awake ()
{
if (awake == 0) {
//update own name
Name = transform.position;
multiplyer = Random.Range (-1.5f, 2.5f);
threshhold = Random.Range (-1.5f, 2.5f);
awake++;
}
}
public void Pulse (float pulseIntensity, Vector3 callNamex, Vector3 callNamey, Vector3 callNamez)
{
//
if (pulseIntensity * multiplyer >= threshhold) {
//update own name
Name = transform.position;
//check if Pulse is ment for self or else ignore
if (Name == callNamex || Name == callNamey || Name == callNamez) {
//Update the Pulseintensity before sending
pulseIntensity = pulseIntensity * multiplyer;
output = pulseIntensity;
//update the call names before sending
callNamex = callNamea + Name;
callNamey = callNameb + Name;
callNamez = callNamec + Name;
// now send updated pulse to next nerves called by location
linkNerveCell.Pulse (pulseIntensity, callNamex, callNamey, callNamez);
linkOutputCell.Pulse (pulseIntensity, callNamex, callNamey, callNamez);
}
}
}
}
Input Cell
public class inputCell : MonoBehaviour
{
// Nerve cells use to send/recive Pulses
public inputCell linkInputCell;
public nerveCell linkNerveCell;
public outputCell linkOutputCell;
//this is visibal while in play so to adjust the settint
public float inputIntensity = 3;
public float output = 0;
//These store the cells own name and the names of those it talks to....vector phone numbers
public Vector3 Name = new Vector3 (0f, 0f, 0f);
Vector3 callNamea = new Vector3 (1f, 0f, 0f);
Vector3 callNameb = new Vector3 (0f, 1f, 0f);
Vector3 callNamec = new Vector3 (0f, 0f, 1f);
//The Function to fire data in to the network
void Pulse (float pulseIntensity)
{
//let us check if pulse came in...
output = pulseIntensity;
//update own name
Name = transform.position;
//make the call names before sending
Vector3 callNamex = callNamea + Name;
Vector3 callNamey = callNameb + Name;
Vector3 callNamez = callNamec + Name;
// now send updated pulse to next nerves called by location
linkNerveCell.Pulse(pulseIntensity, callNamex, callNamey, callNamez);
linkOutputCell.Pulse(pulseIntensity, callNamex, callNamey, callNamez);
}
// NullReferenceException: Object reference not set to an instance of an object inputCell.Pulse (Single pulseIntensity) (at Assets/xyz Nerve System/inputCell.cs:37) inputCell.OnGUI () (at Assets/xyz Nerve System/inputCell.cs:48)
//A TEST button
void OnGUI ()
{
// make a button that fires a pulse out of the input node in to the network
if (GUI.Button (new Rect (10, 10, 60, 30), "Pulse")) {
Pulse(inputIntensity);
// SendMessage("Pulse",inputIntensity,callNamex,callNamey,callNamez);
//Pulse(inputIntensity,callNamex,callNamey,callNamez);
//Pulse(0+1);
}
}
}
Output Cell
public class outputCell : MonoBehaviour
{
// Nerve cells use to send/recive Pulses
public inputCell linkInputCell;
public nerveCell linkNerveCell;
public outputCell linkOutputCell;
//make shure the Cell only wakes up once
private float awake = 0;
public float output=0;
// when created and wake up takes a name from its position, decides on output targets
public Vector3 Name = new Vector3(0f,0f,0f);
Vector3 callNamea = new Vector3(1f,0f,0f);
Vector3 callNameb = new Vector3(0f,1f,0f);
Vector3 callNamec = new Vector3(0f,0f,1f);
// then and choose its settings for multi and thresh
void Awake(){
if (awake==0){
Name=transform.position;
awake++;
}
}
public void Pulse(float pulseIntensity, Vector3 callNamex,Vector3 callNamey, Vector3 callNamez){
//update own name
Name = transform.position;
if(Name==callNamex || Name==callNamey || Name==callNamez){
output=pulseIntensity;
}
}
void OnGUI() {
GUI.Button(new Rect(80, 10, 50, 50), "#"+output);
}
}
suggestions on the code would be awesome.
This is supposed to resemble a neural network in 3d
thanks for any and all help
feel free to learn, i wrote it to share....lol : )/
Answer by PhilRousse · Nov 28, 2013 at 02:42 PM
There multiple way:
You need to keep an handle on the GameObject or Component that you are trying to access. Here some code example
nerveCell[][][] grid;
inputCell inCell;
outputCell outCell;
[...] void InitGrid() { //Initialize the grid grid = new nerveCell[gridx][][]; for(int i = 0; i < gridx; i++) { grid[i] = new GridPiece[gridy][]; for(int j = 0; j < gridy; j++) { grid[i][j] = new GridPiece[gridz]; } } } void Awake() { InitGrid(); //We instantiate and keep a reference on their Cell script. inCell = Instantiate(inputCell, new Vector3 (1, 0, 1), Quaternion.identity).GetComponent<inputCell>(); outCell = Instantiate (outputCell, new Vector3 (gridx, gridy+1, gridz), Quaternion.identity).GetComponent<outputCell>(); for(int x=0;x< gridx;x++) { for(int y=0;y< gridy;y++) { for(int z=0;z< gridz;z++) { //We instantiate and keep a reference on their nerveCell script. grid[x][y][z] = Instantiate (nerveCell, new Vector3 (x+1, y+1, z+1), Quaternion.identity).GetComponent<nerveCell>(); //That permit to call any of there fonction grid[x][y][z].Pulse(2.0f, new Vector3(0,6,9),new Vector3(0,6,9),new Vector3(0,6,9)); } } }
You can send a message. But your grid need to be the children of another gameObject and you can't target a specific gameObject
You can seach for your gameObject, Get there component and call the function. This can be an Heavy function for the CPU.
If you want me to develop option 2 and 3, please comment
Thank You Phil! i updated the question and set it to public... and attached a pic and link to my file. for anyone who wants to play...
i need to learn more about matrices, i just can't make it fly that way yet....another week maybe
This is the complete project code if you want to look.