Needs tuning
This commit is contained in:
parent
0cee652110
commit
65359ecbfa
@ -61,7 +61,7 @@ public class Nucleus {
|
|||||||
|
|
||||||
#region Runtime state (not serialized)
|
#region Runtime state (not serialized)
|
||||||
|
|
||||||
public NanoBrainObj brain { get; protected set; }
|
public NanoBrainObj brain { get; set; }
|
||||||
|
|
||||||
public virtual Vector3 outputValue { get; set; }
|
public virtual Vector3 outputValue { get; set; }
|
||||||
|
|
||||||
@ -110,9 +110,11 @@ public class Nucleus {
|
|||||||
receiver.nucleus.synapses.RemoveAll(s => s.nucleus == nucleus);
|
receiver.nucleus.synapses.RemoveAll(s => s.nucleus == nucleus);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (nucleus.brain != null) {
|
||||||
nucleus.brain.nuclei.RemoveAll(n => n == nucleus);
|
nucleus.brain.nuclei.RemoveAll(n => n == nucleus);
|
||||||
nucleus.brain.GarbageCollection();
|
nucleus.brain.GarbageCollection();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void GetInputFrom(Nucleus input, float weight = 1.0f) {
|
public void GetInputFrom(Nucleus input, float weight = 1.0f) {
|
||||||
input.AddReceiver(this);
|
input.AddReceiver(this);
|
||||||
|
|||||||
@ -123,19 +123,19 @@ public class Perceptoid : Neuroid {
|
|||||||
public void UpdateState(int thingId, Vector3 receptorValue) {
|
public void UpdateState(int thingId, Vector3 receptorValue) {
|
||||||
this.thingId = thingId;
|
this.thingId = thingId;
|
||||||
Vector3 result = receptorValue;
|
Vector3 result = receptorValue;
|
||||||
foreach (Synapse synapse in this.synapses) {
|
// foreach (Synapse synapse in this.synapses) {
|
||||||
Nucleus nucleus = synapse.nucleus;
|
// Nucleus nucleus = synapse.nucleus;
|
||||||
float weight = synapse.weight;
|
// float weight = synapse.weight;
|
||||||
Vector3 direction = nucleus.outputValue.normalized;
|
// Vector3 direction = nucleus.outputValue.normalized;
|
||||||
float magnitude = nucleus.outputValue.magnitude;
|
// float magnitude = nucleus.outputValue.magnitude;
|
||||||
|
|
||||||
magnitude = weight * Mathf.Pow(magnitude, exponent);
|
// magnitude = weight * Mathf.Pow(magnitude, exponent);
|
||||||
if (inverse)
|
// if (inverse)
|
||||||
magnitude = 1 / magnitude;
|
// magnitude = 1 / magnitude;
|
||||||
result += direction * magnitude;
|
// result += direction * magnitude;
|
||||||
}
|
// }
|
||||||
if (average && this.synapses.Count > 0)
|
// if (average && this.synapses.Count > 0)
|
||||||
result /= this.synapses.Count + 1;
|
// result /= this.synapses.Count + 1;
|
||||||
|
|
||||||
this.outputValue = result;
|
this.outputValue = result;
|
||||||
foreach (Receiver receiver in this.receivers)
|
foreach (Receiver receiver in this.receivers)
|
||||||
|
|||||||
@ -40,7 +40,7 @@ public class Receptor {
|
|||||||
Debug.Log("No perceptoid selected, stimulus is ignored");
|
Debug.Log("No perceptoid selected, stimulus is ignored");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Debug.Log($"Stimulus {thingId} {selectedPerceptoid.thingId}");
|
//Debug.Log($"Stimulus {thingId} {selectedPerceptoid.thingId}");
|
||||||
selectedPerceptoid.UpdateState(this.thingId, this.localPosition);
|
selectedPerceptoid.UpdateState(this.thingId, this.localPosition);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -228,7 +228,7 @@ public class NanoBrainInspector : Editor {
|
|||||||
|
|
||||||
private void DrawGraph() {
|
private void DrawGraph() {
|
||||||
float size = 20;
|
float size = 20;
|
||||||
Vector3 position = new(200, 210, 0);
|
Vector3 position = new(150, 210, 0);
|
||||||
|
|
||||||
DrawReceivers(this.currentNucleus, position, size);
|
DrawReceivers(this.currentNucleus, position, size);
|
||||||
DrawSynapses(this.currentNucleus, position, size);
|
DrawSynapses(this.currentNucleus, position, size);
|
||||||
@ -263,7 +263,7 @@ public class NanoBrainInspector : Editor {
|
|||||||
if (receiverNucleus == null)
|
if (receiverNucleus == null)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
Vector3 pos = new(100, margin + row * spacing, 0.0f);
|
Vector3 pos = new(50, margin + row * spacing, 0.0f);
|
||||||
Handles.color = Color.white;
|
Handles.color = Color.white;
|
||||||
Handles.DrawLine(parentPos, pos);
|
Handles.DrawLine(parentPos, pos);
|
||||||
|
|
||||||
@ -294,7 +294,7 @@ public class NanoBrainInspector : Editor {
|
|||||||
foreach (Synapse receiver in nucleus.synapses) {
|
foreach (Synapse receiver in nucleus.synapses) {
|
||||||
Nucleus receiverNucleus = receiver.nucleus;
|
Nucleus receiverNucleus = receiver.nucleus;
|
||||||
|
|
||||||
Vector3 pos = new(300, margin + row * spacing, 0.0f);
|
Vector3 pos = new(250, margin + row * spacing, 0.0f);
|
||||||
Handles.color = Color.white;
|
Handles.color = Color.white;
|
||||||
Handles.DrawLine(parentPos, pos);
|
Handles.DrawLine(parentPos, pos);
|
||||||
|
|
||||||
@ -400,8 +400,7 @@ public class NanoBrainInspector : Editor {
|
|||||||
this.currentNucleus.name = EditorGUILayout.TextField(this.currentNucleus.name);
|
this.currentNucleus.name = EditorGUILayout.TextField(this.currentNucleus.name);
|
||||||
if (this.currentNucleus is Perceptoid currentPerceptoid)
|
if (this.currentNucleus is Perceptoid currentPerceptoid)
|
||||||
currentPerceptoid.thingType = EditorGUILayout.IntField("Thing Type", currentPerceptoid.thingType);
|
currentPerceptoid.thingType = EditorGUILayout.IntField("Thing Type", currentPerceptoid.thingType);
|
||||||
|
else if (this.currentNucleus is Neuroid neuroid) {
|
||||||
if (this.currentNucleus is Neuroid neuroid) {
|
|
||||||
EditorGUILayout.BeginHorizontal();
|
EditorGUILayout.BeginHorizontal();
|
||||||
EditorGUILayout.LabelField("Activation Curve", GUILayout.Width(150));
|
EditorGUILayout.LabelField("Activation Curve", GUILayout.Width(150));
|
||||||
if (neuroid.curveMax > 0)
|
if (neuroid.curveMax > 0)
|
||||||
@ -467,10 +466,14 @@ public class NanoBrainInspector : Editor {
|
|||||||
protected virtual void AddInputNeuron(Nucleus nucleus) {
|
protected virtual void AddInputNeuron(Nucleus nucleus) {
|
||||||
Neuroid newNeuroid = new(this.brain, "New neuron");
|
Neuroid newNeuroid = new(this.brain, "New neuron");
|
||||||
newNeuroid.AddReceiver(nucleus);
|
newNeuroid.AddReceiver(nucleus);
|
||||||
|
this.currentNucleus = newNeuroid;
|
||||||
BuildLayers();
|
BuildLayers();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected virtual void DeleteNeuron(Nucleus nucleus) {
|
protected virtual void DeleteNeuron(Nucleus nucleus) {
|
||||||
|
if (nucleus == null)
|
||||||
|
return;
|
||||||
|
if (nucleus.brain != null)
|
||||||
this.currentNucleus = nucleus.brain.root;
|
this.currentNucleus = nucleus.brain.root;
|
||||||
foreach (Receiver receiver in nucleus.receivers) {
|
foreach (Receiver receiver in nucleus.receivers) {
|
||||||
if (receiver.nucleus != null) {
|
if (receiver.nucleus != null) {
|
||||||
@ -485,19 +488,30 @@ public class NanoBrainInspector : Editor {
|
|||||||
protected virtual void AddPerceptoid(Nucleus nucleus) {
|
protected virtual void AddPerceptoid(Nucleus nucleus) {
|
||||||
Perceptoid newPerceptoid = new(this.brain, 0, "New Perceptoid");
|
Perceptoid newPerceptoid = new(this.brain, 0, "New Perceptoid");
|
||||||
newPerceptoid.AddReceiver(nucleus);
|
newPerceptoid.AddReceiver(nucleus);
|
||||||
|
this.currentNucleus = newPerceptoid;
|
||||||
BuildLayers();
|
BuildLayers();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected virtual void ConnectNucleus(Nucleus nucleus) {
|
protected virtual void ConnectNucleus(Nucleus nucleus) {
|
||||||
if (this.currentNucleus.brain == null)
|
if (this.currentNucleus.brain == null)
|
||||||
return;
|
return;
|
||||||
string[] names = this.currentNucleus.brain.perceptei.Select(i => i.name).ToArray();
|
|
||||||
|
//string[] names = this.currentNucleus.brain.perceptei.Select(i => i.name).ToArray();
|
||||||
|
IEnumerable<string> perceptei = this.currentNucleus.brain.perceptei.Select(i => i.name);
|
||||||
|
IEnumerable<string> nuclei = this.currentNucleus.brain.nuclei.Select(i => i.name);
|
||||||
|
string[] names = perceptei.Concat(nuclei).ToArray();
|
||||||
int selectedIndex = -1;
|
int selectedIndex = -1;
|
||||||
selectedIndex = EditorGUILayout.Popup("Connect to", selectedIndex, names);
|
selectedIndex = EditorGUILayout.Popup("Connect to", selectedIndex, names);
|
||||||
if (selectedIndex >= 0 && selectedIndex < this.currentNucleus.brain.perceptei.Count) {
|
if (selectedIndex >= 0) {
|
||||||
|
if (selectedIndex < perceptei.Count()) {
|
||||||
Nucleus n = this.currentNucleus.brain.perceptei[selectedIndex];
|
Nucleus n = this.currentNucleus.brain.perceptei[selectedIndex];
|
||||||
n.AddReceiver(this.currentNucleus);
|
n.AddReceiver(this.currentNucleus);
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
Nucleus n = this.currentNucleus.brain.nuclei[selectedIndex - perceptei.Count()];
|
||||||
|
n.AddReceiver(this.currentNucleus);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected virtual void DisconnectNucleus(Nucleus nucleus) {
|
protected virtual void DisconnectNucleus(Nucleus nucleus) {
|
||||||
@ -544,7 +558,7 @@ public class NanoBrainInspector : Editor {
|
|||||||
#region Update
|
#region Update
|
||||||
|
|
||||||
private void UpdateLayout(float containerWidth) {
|
private void UpdateLayout(float containerWidth) {
|
||||||
if (containerWidth > 800f) {
|
if (containerWidth > 700f) {
|
||||||
mainContainer.style.flexDirection = FlexDirection.Row;
|
mainContainer.style.flexDirection = FlexDirection.Row;
|
||||||
inspectorContainer.style.width = 400; // fixed sidebar width
|
inspectorContainer.style.width = 400; // fixed sidebar width
|
||||||
inspectorContainer.style.flexGrow = 0;
|
inspectorContainer.style.flexGrow = 0;
|
||||||
|
|||||||
@ -78,6 +78,9 @@ public class NanoBrainObj : ScriptableObject, ISerializationCallbackReceiver {
|
|||||||
if (nucleus is null)
|
if (nucleus is null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
if (nucleus.brain == null)
|
||||||
|
nucleus.brain = this;
|
||||||
|
|
||||||
visitedNuclei.Add(nucleus);
|
visitedNuclei.Add(nucleus);
|
||||||
if (nucleus.synapses != null) {
|
if (nucleus.synapses != null) {
|
||||||
HashSet<Synapse> visitedSynapses = new();
|
HashSet<Synapse> visitedSynapses = new();
|
||||||
@ -89,5 +92,16 @@ public class NanoBrainObj : ScriptableObject, ISerializationCallbackReceiver {
|
|||||||
}
|
}
|
||||||
nucleus.synapses.RemoveAll(synapse => visitedSynapses.Contains(synapse) == false);
|
nucleus.synapses.RemoveAll(synapse => visitedSynapses.Contains(synapse) == false);
|
||||||
}
|
}
|
||||||
|
if (nucleus.receivers != null) {
|
||||||
|
HashSet<Receiver> visitedReceivers = new();
|
||||||
|
foreach (Receiver receiver in nucleus.receivers) {
|
||||||
|
if (receiver != null && receiver.nucleus != null) {
|
||||||
|
visitedReceivers.Add(receiver);
|
||||||
|
visitedNuclei.Add(receiver.nucleus);
|
||||||
|
//MarkNuclei(visitedNuclei, receiver.nucleus);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
nucleus.receivers.RemoveAll(receiver => visitedReceivers.Contains(receiver) == false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -371,13 +371,13 @@ MonoBehaviour:
|
|||||||
m_Script: {fileID: 11500000, guid: 0464906885ae3494f8fd0314719fb2db, type: 3}
|
m_Script: {fileID: 11500000, guid: 0464906885ae3494f8fd0314719fb2db, type: 3}
|
||||||
m_Name:
|
m_Name:
|
||||||
m_EditorClassIdentifier: Assembly-CSharp::SwarmControl
|
m_EditorClassIdentifier: Assembly-CSharp::SwarmControl
|
||||||
speed: 2
|
speed: 1
|
||||||
inertia: 0.5
|
inertia: 0.8
|
||||||
alignmentForce: 2
|
alignmentForce: 2
|
||||||
cohesionForce: 2
|
cohesionForce: 2
|
||||||
avoidanceForce: 1
|
avoidanceForce: 1
|
||||||
separationDistance: 0.3
|
separationDistance: 0.3
|
||||||
perceptionDistance: 1
|
perceptionDistance: 2
|
||||||
boundaryForce: 5
|
boundaryForce: 5
|
||||||
spaceSize: {x: 10, y: 10, z: 10}
|
spaceSize: {x: 10, y: 10, z: 10}
|
||||||
boundaryWidth: {x: 1, y: 1, z: 1}
|
boundaryWidth: {x: 1, y: 1, z: 1}
|
||||||
@ -393,7 +393,7 @@ MonoBehaviour:
|
|||||||
m_Script: {fileID: 11500000, guid: ec888ca5333d45a438f9f417fa5ce135, type: 3}
|
m_Script: {fileID: 11500000, guid: ec888ca5333d45a438f9f417fa5ce135, type: 3}
|
||||||
m_Name:
|
m_Name:
|
||||||
m_EditorClassIdentifier: Assembly-CSharp::SwarmSpawn
|
m_EditorClassIdentifier: Assembly-CSharp::SwarmSpawn
|
||||||
count: 30
|
count: 100
|
||||||
boidPrefab: {fileID: 8702527964058765413, guid: f9c706268554ce449a8773675b2864b8, type: 3}
|
boidPrefab: {fileID: 8702527964058765413, guid: f9c706268554ce449a8773675b2864b8, type: 3}
|
||||||
spawnAreaSize: {x: 0.5, y: 0.5, z: 0.5}
|
spawnAreaSize: {x: 0.5, y: 0.5, z: 0.5}
|
||||||
minDelay: 0.05
|
minDelay: 0.05
|
||||||
|
|||||||
@ -22,8 +22,42 @@ MonoBehaviour:
|
|||||||
synapses:
|
synapses:
|
||||||
- nucleusId: -112538112
|
- nucleusId: -112538112
|
||||||
weight: 1
|
weight: 1
|
||||||
|
curvePreset: 0
|
||||||
|
curve:
|
||||||
|
serializedVersion: 2
|
||||||
|
m_Curve: []
|
||||||
|
m_PreInfinity: 2
|
||||||
|
m_PostInfinity: 2
|
||||||
|
m_RotationOrder: 4
|
||||||
|
curveMax: 0
|
||||||
receivers: []
|
receivers: []
|
||||||
nucleusType:
|
nucleusType:
|
||||||
|
_curvePreset: 0
|
||||||
|
curve:
|
||||||
|
serializedVersion: 2
|
||||||
|
m_Curve:
|
||||||
|
- serializedVersion: 3
|
||||||
|
time: 0
|
||||||
|
value: 0
|
||||||
|
inSlope: 0
|
||||||
|
outSlope: 1
|
||||||
|
tangentMode: 0
|
||||||
|
weightedMode: 0
|
||||||
|
inWeight: 0
|
||||||
|
outWeight: 0
|
||||||
|
- serializedVersion: 3
|
||||||
|
time: 1
|
||||||
|
value: 1
|
||||||
|
inSlope: 1
|
||||||
|
outSlope: 0
|
||||||
|
tangentMode: 0
|
||||||
|
weightedMode: 0
|
||||||
|
inWeight: 0
|
||||||
|
outWeight: 0
|
||||||
|
m_PreInfinity: 2
|
||||||
|
m_PostInfinity: 2
|
||||||
|
m_RotationOrder: 4
|
||||||
|
curveMax: 1
|
||||||
average: 0
|
average: 0
|
||||||
inverse: 0
|
inverse: 0
|
||||||
exponent: 1
|
exponent: 1
|
||||||
@ -32,9 +66,25 @@ MonoBehaviour:
|
|||||||
synapses:
|
synapses:
|
||||||
- nucleusId: 407735232
|
- nucleusId: 407735232
|
||||||
weight: -1
|
weight: -1
|
||||||
|
curvePreset: 0
|
||||||
|
curve:
|
||||||
|
serializedVersion: 2
|
||||||
|
m_Curve: []
|
||||||
|
m_PreInfinity: 2
|
||||||
|
m_PostInfinity: 2
|
||||||
|
m_RotationOrder: 4
|
||||||
|
curveMax: 0
|
||||||
receivers:
|
receivers:
|
||||||
- nucleusId: -1707533328
|
- nucleusId: -1707533328
|
||||||
nucleusType:
|
nucleusType:
|
||||||
|
_curvePreset: 0
|
||||||
|
curve:
|
||||||
|
serializedVersion: 2
|
||||||
|
m_Curve: []
|
||||||
|
m_PreInfinity: 2
|
||||||
|
m_PostInfinity: 2
|
||||||
|
m_RotationOrder: 4
|
||||||
|
curveMax: 0
|
||||||
average: 0
|
average: 0
|
||||||
inverse: 0
|
inverse: 0
|
||||||
exponent: 1
|
exponent: 1
|
||||||
@ -45,6 +95,14 @@ MonoBehaviour:
|
|||||||
receivers:
|
receivers:
|
||||||
- nucleusId: -112538112
|
- nucleusId: -112538112
|
||||||
nucleusType: Perceptoid
|
nucleusType: Perceptoid
|
||||||
|
_curvePreset: 0
|
||||||
|
curve:
|
||||||
|
serializedVersion: 2
|
||||||
|
m_Curve: []
|
||||||
|
m_PreInfinity: 2
|
||||||
|
m_PostInfinity: 2
|
||||||
|
m_RotationOrder: 4
|
||||||
|
curveMax: 0
|
||||||
average: 0
|
average: 0
|
||||||
inverse: 0
|
inverse: 0
|
||||||
exponent: 1
|
exponent: 1
|
||||||
|
|||||||
@ -39,6 +39,7 @@ public class Boid : MonoBehaviour {
|
|||||||
continue;
|
continue;
|
||||||
|
|
||||||
Vector3 localPosition = this.transform.InverseTransformPoint(neighbour.transform.position);
|
Vector3 localPosition = this.transform.InverseTransformPoint(neighbour.transform.position);
|
||||||
|
localPosition = localPosition.normalized * (localPosition.magnitude - sc.separationDistance);
|
||||||
//Debug.DrawRay(this.transform.position, this.transform.TransformDirection(localPosition), Color.magenta);
|
//Debug.DrawRay(this.transform.position, this.transform.TransformDirection(localPosition), Color.magenta);
|
||||||
|
|
||||||
int thingId = neighbour.GetInstanceID();
|
int thingId = neighbour.GetInstanceID();
|
||||||
@ -61,13 +62,13 @@ public class Boid : MonoBehaviour {
|
|||||||
this.velocity = this.velocity.normalized * sc.speed;
|
this.velocity = this.velocity.normalized * sc.speed;
|
||||||
else
|
else
|
||||||
this.velocity = this.transform.forward * sc.speed;
|
this.velocity = this.transform.forward * sc.speed;
|
||||||
Debug.DrawRay(this.transform.position, this.velocity, Color.blue);
|
//Debug.DrawRay(this.transform.position, this.velocity, Color.blue);
|
||||||
|
|
||||||
this.transform.position += this.velocity * Time.deltaTime;
|
this.transform.position += this.velocity * Time.deltaTime;
|
||||||
|
|
||||||
if (this.velocity != Vector3.zero) {
|
if (this.velocity != Vector3.zero) {
|
||||||
Quaternion targetRotation = Quaternion.LookRotation(this.velocity);
|
Quaternion targetRotation = Quaternion.LookRotation(this.velocity);
|
||||||
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 2f); // Adjust the speed of rotation
|
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 5f); // Adjust the speed of rotation
|
||||||
}
|
}
|
||||||
|
|
||||||
nanoBrain.brain.UpdateNuclei();
|
nanoBrain.brain.UpdateNuclei();
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user