public void FromIntList(IEnumerator <int> list) { subnetNeurons = list.Current; list.MoveNext(); layers = list.Current; list.MoveNext(); baseConnNum = list.Current; list.MoveNext(); inputOutputConnNum = list.Current; list.MoveNext(); EmotionState = list.Current; list.MoveNext(); Init(); subNets = new NeuronCluster[list.Current]; list.MoveNext(); for (int i = 0; i < subNets.Length; i++) { subNets[i] = NeuronCluster.FromIntList(list); } connections = new NNConnection[list.Current]; list.MoveNext(); for (int i = 0; i < connections.Length; i++) { connections[i] = NNConnection.FromIntList(list); } }
public void DoTimestep() { if (EmotionState > 0) { EmotionState--; } else if (EmotionState < 0) { EmotionState++; } int finalEmotionState = (EmotionState > EMOTION_MAGNITUDE_THRESH ? 1 : (EmotionState < -EMOTION_MAGNITUDE_THRESH ? -1 : 0)); // First, copy inputs / connection values into all subnets for (int i = 0; i < connections.Length; i++) { NNConnection conn = connections[i]; NeuronCluster srcNet = subNets[conn.srcNet]; // destNet can be -1, which means access the IO ports if (conn.destNet >= 0) { NeuronCluster destNet = subNets[conn.destNet]; srcNet.SetExternalInput(conn.srcIdx, destNet.GetExternalOutput(conn.destIdx)); destNet.SetExternalInput(conn.destIdx, srcNet.GetExternalOutput(conn.srcIdx)); } else { srcNet.SetExternalInput(conn.srcIdx, inputVector[conn.destIdx]); // copy outputs after processing // outputVector[conn.destIdx] = srcNet.GetExternalOutput(conn.srcIdx); } } // Second, subnets process based on emotional state for (int i = 0; i < subNets.Length; i++) { subNets[i].FullTimestep(finalEmotionState); } // Third, set outputs for (int i = 0; i < connections.Length; i++) { NNConnection conn = connections[i]; // destNet must be -1 if (conn.destNet != -1) { continue; } NeuronCluster srcNet = subNets[conn.srcNet]; outputVector[conn.destIdx] = srcNet.GetExternalOutput(conn.srcIdx); } }
public static NeuronCluster FromIntList(IEnumerator <int> list) { int numNeurons = list.Current; list.MoveNext(); int numExposed = list.Current; list.MoveNext(); NeuronCluster result = new NeuronCluster(numNeurons, numExposed); for (int i = 0; i < numExposed; i++) { result.externalInputs[i] = list.Current; list.MoveNext(); } for (int i = 0; i < numExposed; i++) { result.externalOutputs[i] = list.Current; list.MoveNext(); } for (int i = 0; i < numNeurons; i++) { result.activations[i] = list.Current; list.MoveNext(); } for (int i = 0; i < numNeurons; i++) { result.cooldowns[i] = list.Current; list.MoveNext(); } for (int i = 0; i < numNeurons; i++) { for (int j = 0; j < numNeurons; j++) { result.connections[i][j] = list.Current; list.MoveNext(); } } return(result); }
private void Init() { inputVector = new int[InputOutputConnTotal]; outputVector = new int[InputOutputConnTotal]; subNets = new NeuronCluster[(int)Mathf.Pow(2, layers) - 1]; List <NNConnection> listConnections = new List <NNConnection>(); for (int layer = 0; layer < layers; layer++) { int startIdx = (int)Mathf.Pow(2, layer) - 1; int endIdx = (int)Mathf.Pow(2, layer + 1) - 1; for (int i = startIdx; i < endIdx; i++) { int exposed = 3 * baseConnNum; if (layer == layers - 1) { exposed = inputOutputConnNum + baseConnNum; } else if (layer == 0) { exposed = 2 * baseConnNum; } subNets[i] = new NeuronCluster(subnetNeurons, exposed); // set up connections. Connect first 2 * base to the lower layer and last 1 * base to the higher layer // top layer only has the 2 * base to the lower layer // the index of the net in the next layer is the index of this one times two, and times two plus one int idxInLayer = i - startIdx; if (layer < layers - 1) { int destNetIdx = endIdx + 2 * idxInLayer; int destConnStart = 2 * baseConnNum; if (layer == layers - 2) { // the dest conns on the last layer hook up differently destConnStart = inputOutputConnNum; } for (int c = 0; c < baseConnNum; c++) { listConnections.Add(new NNConnection() { srcNet = i, destNet = destNetIdx, srcIdx = c, destIdx = destConnStart + c, }); } for (int c = 0; c < baseConnNum; c++) { listConnections.Add(new NNConnection() { srcNet = i, destNet = destNetIdx + 1, srcIdx = baseConnNum + c, destIdx = destConnStart + c, }); } } else { // connect to inputs for (int c = 0; c < inputOutputConnNum; c++) { listConnections.Add(new NNConnection() { srcNet = i, destNet = -1, // the IO connections srcIdx = c, destIdx = idxInLayer * inputOutputConnNum + c, }); } } } } connections = listConnections.ToArray(); DebugBrainViewer.inst.SetupTexture(DebugTextureWidthPixels, DebugTextureHeightPixels); }