void CreateBraidVectorsFromTree(BraidNode parentNode, int layer) { // add reference branch point to list if (parentNode.children.Count > 1) { brancedNodes.Add(parentNode); } radiusValues.Add(parentNode.data.radius); vects.Add(parentNode.data.vector); // add reference node vect value to list if (parentNode.children.Count == 0 && brancedNodes.Count != 0) { // get back to last reference point int i = brancedNodes.Count - 1; braidVectors.Add(vects.ToArray()); vects.Clear(); vects.Add(brancedNodes[i].data.vector); brancedNodes.RemoveAt(i); } else if (parentNode.children.Count == 0 && brancedNodes.Count == 0) { braidVectors.Add(vects.ToArray()); } foreach (BraidNode subNode in parentNode.children) { CreateBraidVectorsFromTree(subNode, layer + 1); } }
void Test3() { Debug.Log("Test 3"); BraidNode root = new BraidNode(new BraidNodeData("root", Vector3.zero, 1.5f)); BraidNode n1 = new BraidNode(new BraidNodeData("n1", new Vector3(0.0f, 2.0f, 0.0f), 1.0f)); BraidNode n2 = new BraidNode(new BraidNodeData("n2", new Vector3(-2.0f, 2.0f, 0.0f), 1.0f)); BraidNode n3 = new BraidNode(new BraidNodeData("n3", new Vector3(2.0f, 2.0f, 0.0f), 1.0f)); BraidNode n4 = new BraidNode(new BraidNodeData("n4", new Vector3(-2.0f, 4.0f, 0.0f), 1.0f)); BraidNode n5 = new BraidNode(new BraidNodeData("n5", new Vector3(2.0f, 4.0f, 0.0f), 1.0f)); root.children.Add(n1); n1.children.Add(n2); n1.children.Add(n3); n2.children.Add(n4); n3.children.Add(n5); List <BraidNode> firstStack = new List <BraidNode>(); firstStack.Add(root); CreateBraidVectorsFromTree(root, 0); string message = JsonHelper.CreateJSONFromVectors(braidVectors); Debug.Log(message); sender.SendString(message); }
BraidNode CreateNewNode(float yValue, float layer) { Vector4 v = CreateOutput(yValue, layer); NormalizeOutput(v); Vector3 segment_vector = new Vector3(0.0f, yValue, 0.0f) + new Vector3(v.x, v.y, v.z); BraidNodeData data = new BraidNodeData("ann_node_", new Vector3(0.0f, yValue, 0.0f), 1.0f, _nodeid++); BraidNode b = new BraidNode(data); return(b); }
public static void AttachChildren(BraidNode parent, int amount, int id) { for (int i = 0; i < amount; i++) { float radius = parent.parent.data.radius / 2.0f; float yValue = parent.data.vector.y + 2.0f; BraidNode n = new BraidNode(new BraidNodeData("ann_node" + id.ToString(), new Vector3(0.0f, yValue, 0.0f), radius)); id++; parent.children.Add(n); parent = n; } }
public static BraidNode CreateInputTree(int size, float radius = 1.0f) { BraidNode root = new BraidNode(new BraidNodeData("root", Vector3.zero, radius + 1.0f)); BraidNode temp = root; for (int i = 0; i < size; i++) { BraidNode n = new BraidNode(new BraidNodeData("n" + i.ToString(), new Vector3(0.0f, i * 2 + 2, 0.0f), radius)); temp.children.Add(n); temp = n; } return(root); }
public static string CreateJSONFromDataTree(BraidNode root) { List <Vector3> vectors = new List <Vector3>(); List <BraidNode> firstStack = new List <BraidNode>(); firstStack.Add(root); List <List <BraidNode> > childListStack = new List <List <BraidNode> >(); childListStack.Add(firstStack); while (childListStack.Count > 0) { List <BraidNode> childStack = childListStack[childListStack.Count - 1]; if (childStack.Count == 0) { childListStack.RemoveAt(childListStack.Count - 1); } else { root = childStack[0]; childStack.RemoveAt(0); vectors.Add(root.data.vector); if (root.children.Count > 0) { childListStack.Add(new List <BraidNode>(root.children)); } } } Braid[] braids = new Braid[9]; float[] radiusArray = new float[9]; for (int i = 0; i < 9; i++) { radiusArray[i] = 1.0f; } for (int i = 0; i < 9; i++) { braids[i] = new Braid("braid_", i, vectors.ToArray(), null, radiusArray); } return(CreateJSONFromBraids(9, braids, 10)); }
private void Test4() { Debug.Log("Performing test 4"); BraidNode root = new BraidNode(new BraidNodeData("root", Vector3.zero, 1.5f)); // left branch BraidNode n1 = new BraidNode(new BraidNodeData("n1", new Vector3(0.0f, 2.0f, 0.0f), 1.0f)); BraidNode n2 = new BraidNode(new BraidNodeData("n2", new Vector3(-2.0f, 2.0f, 0.0f), 1.0f)); BraidNode n4 = new BraidNode(new BraidNodeData("n4", new Vector3(-2.0f, 4.0f, 0.0f), 1.0f)); BraidNode n6 = new BraidNode(new BraidNodeData("n6", new Vector3(-2.0f, 6.0f, 0.0f), 1.0f)); BraidNode n9 = new BraidNode(new BraidNodeData("n9", new Vector3(0.0f, 8.0f, 0.0f), 1.0f)); // right branch BraidNode n3 = new BraidNode(new BraidNodeData("n3", new Vector3(2.0f, 2.0f, 0.0f), 1.0f)); BraidNode n5 = new BraidNode(new BraidNodeData("n5", new Vector3(2.0f, 4.0f, 0.0f), 1.0f)); BraidNode n7 = new BraidNode(new BraidNodeData("n7", new Vector3(1.0f, 6.0f, 0.0f), 1.0f)); BraidNode n8 = new BraidNode(new BraidNodeData("n8", new Vector3(4.0f, 8.0f, 0.0f), 1.0f)); BraidNode n10 = new BraidNode(new BraidNodeData("n10", new Vector3(6.0f, 10.0f, 0.0f), 1.0f)); root.children.Add(n1); n1.children.Add(n2); n1.children.Add(n3); // left branch n2.children.Add(n4); n4.children.Add(n6); n6.children.Add(n9); // right branch n3.children.Add(n5); n5.children.Add(n7); n5.children.Add(n8); n8.children.Add(n10); root.PrintTree(); List <BraidNode> firstStack = new List <BraidNode>(); firstStack.Add(root); CreateBraidVectorsFromTree(root, 0); string message = JsonHelper.CreateJSONFromVectors(braidVectors); Debug.Log(message); sender.SendString(message); }
void Test1() { BraidNodeData data = new BraidNodeData("root", Vector3.zero, 1.5f); BraidNode root = new BraidNode(data); data = new BraidNodeData("n1", Vector3.left, 1.0f); BraidNode child1 = new BraidNode(new BraidNodeData("n1", Vector3.right, 1.0f)); BraidNode child2 = new BraidNode(new BraidNodeData("n2", Vector3.right, 1.0f)); BraidNode child3 = new BraidNode(new BraidNodeData("n3", Vector3.right, 1.0f)); root.children.Add(child1); root.children.Add(child2); root.children.Add(child3); root.PrintTree(); JsonHelper.CreateJSONFromDataTree(root); }
protected void ActivateBraidController() { Debug.Log("Creating braid with this controller"); TREE_SIZE = 10; MAIN_RADIUS = 2.5f; _nodeid = 0; BraidNode tree = BraidTreeUtility.CreateInputTree(TREE_SIZE, MAIN_RADIUS); ActivateCPPNSplit(tree, -1.0f); //tree.PrintTree(); CreateBraidVectorsFromTree(tree, 0); Braid b = new Braid("braid_", braidId, braidVectors, null, radiusValues.ToArray()); messenger.AddBraid(b, braidId); BraidSimulationManager.vectorArraysMade++; }
void Test2() { BraidNode root = new BraidNode(new BraidNodeData("root", Vector3.zero, 1.5f)); BraidNode temp = root; for (int i = 0; i < 5; i++) { BraidNode n = new BraidNode(new BraidNodeData("n" + i.ToString(), new Vector3(0.0f, i * 2 + 2, 0.0f))); temp.children.Add(n); temp = n; } root.PrintTree(); string message = JsonHelper.CreateJSONFromDataTree(root); sender.SendString(message); }
public void PrintTree() { Debug.Log("Depth: " + this.Depth); BraidNode root = this.root; List <BraidNode> firstStack = new List <BraidNode>(); firstStack.Add(root); List <List <BraidNode> > childListStack = new List <List <BraidNode> >(); childListStack.Add(firstStack); while (childListStack.Count > 0) { List <BraidNode> childStack = childListStack[childListStack.Count - 1]; if (childStack.Count == 0) { childListStack.RemoveAt(childListStack.Count - 1); } else { root = childStack[0]; childStack.RemoveAt(0); string indent = ""; for (int i = 0; i < childListStack.Count - 1; i++) { indent += (childListStack[i].Count > 0) ? "| " : " "; } Debug.Log(indent + "+- " + root.ToString()); if (root.children.Count > 0) { childListStack.Add(new List <BraidNode>(root.children)); } } } }
void ActivateCPPNSplit(BraidNode parentNode, float layer) { Vector4 res = CreateOutput(parentNode.data.vector.y, layer); res = NormalizeOutput(res); // check if we should branch and add new node with attached children if (res.w > 0.8f && layer < 1.0f) { layer += 0.5f; BraidNode b = CreateNewNode(parentNode.data.vector.y, layer); parentNode.children.Add(b); BraidTreeUtility.AttachChildren(b, TREE_SIZE - b.Depth, _nodeid++); } parentNode.data.vector += new Vector3(res.x, res.y, res.z); // recursion for the win foreach (BraidNode subNode in parentNode.children) { ActivateCPPNSplit(subNode, layer); } }
public BraidNode(BraidNodeData data, BraidNode parent) { this.data = data; this.parent = parent; children = new BraidNodeList(this); }