internal void mergeGroups(Agent agent1, Agent agent2) { if (agent1.groupBelongingTo_ == null && agent2.groupBelongingTo_ == null) { // Create new group GroupAgent group = new GroupAgent(this); group.agents_.Add(agent1); group.agents_.Add(agent2); agent1.groupBelongingTo_ = group; agent2.groupBelongingTo_ = group; groupAgents_.Add(group); } else if (agent1.groupBelongingTo_ == null) { agent1.groupBelongingTo_ = agent2.groupBelongingTo_; agent2.groupBelongingTo_.agents_.Add(agent1); } else if (agent2.groupBelongingTo_ == null) { agent2.groupBelongingTo_ = agent1.groupBelongingTo_; agent1.groupBelongingTo_.agents_.Add(agent2); } else if (agent1.groupBelongingTo_ != agent2.groupBelongingTo_) { // Merge groups IList <Agent> agentsList2 = agent2.groupBelongingTo_.agents_; foreach (Agent a in agentsList2) { Agent agent = a; agent.groupBelongingTo_ = agent1.groupBelongingTo_; agent1.groupBelongingTo_.agents_.Add(agent); } agentsList2.Clear(); } }
public IList <int> getGroupMembers(GroupAgent ga) { IList <Agent> groupAgents = ga.getAgents(); IList <int> toReturn = new List <int>(); foreach (Agent a in groupAgents) { toReturn.Add(a.id_); } return(toReturn); }
public void unitTests() { // Test Compute tangents Points setAgentDefaults(15.0f, 10, 1.0f, 10.0f, 0.5f, 2.0f, new Vector2()); addAgent(new Vector2(-1, 1), 0, true, true); addAgent(new Vector2(0, 1), 0, true, true); setAgentRadius(1, 1.0f); addAgent(new Vector2(0, 0), 0, true, true); addAgent(new Vector2(1, 2), 0, true, true); addAgent(new Vector2(1, 0), 0, true, true); GroupAgent group = new GroupAgent(this); Pair <Vector2, Vector2> p1 = group.computeTangentsPoints(getAgent(0), getAgent(1)); Pair <Vector2, Vector2> p2 = group.computeTangentsPoints(getAgent(2), getAgent(1)); Pair <Vector2, Vector2> p3 = group.computeTangentsPoints(getAgent(3), getAgent(1)); Pair <Vector2, Vector2> p4 = group.computeTangentsPoints(getAgent(4), getAgent(1)); Pair <Vector2, Vector2> p5 = group.computeTangentsPoints(getAgent(1), getAgent(0)); Pair <Vector2, Vector2> p6 = group.computeTangentsPoints(getAgent(1), getAgent(2)); Pair <Vector2, Vector2> p7 = group.computeTangentsPoints(getAgent(1), getAgent(3)); Pair <Vector2, Vector2> p8 = group.computeTangentsPoints(getAgent(1), getAgent(4)); Console.Write("Compute Tangents Points\n"); Console.Write("T1" + p1.First + "Expected ~(-1,0.5) - T2" + p1.Second + "Expected ~(-1,1.5)\n"); Console.Write("T1" + p2.First + "Expected ~(0.5,0) - T2" + p2.Second + "Expected ~(-0.5,0)\n"); Console.Write("T1" + p3.First + "Expected ~(0.64,2.35) - T2" + p3.Second + "Expected ~(1.35,1.64)\n"); Console.Write("T1" + p4.First + "Expected ~(1.35,0.35) - T2" + p4.Second + "Expected ~(0.64,-0.35)\n\n"); Console.Write("T1" + p5.First + "Expected ~(0,1.5) - T2" + p5.Second + "Expected ~(0,0.5)\n"); Console.Write("T1" + p6.First + "Expected ~(-0.5,1) - T2" + p6.Second + "Expected ~(0.5,1)\n"); Console.Write("T1" + p7.First + "Expected ~(0.35,0.64) - T2" + p7.Second + "Expected ~(-0.35,1.35)\n"); Console.Write("T1" + p8.First + "Expected ~(-0.35,0.64) - T2" + p8.Second + "Expected ~(0.35,1.35)\n\n"); // Test detect groups setAgentRadius(1, 0.5f); addAgent(new Vector2(1, 0), 0, true, true); setAgentPosition(0, new Vector2(0.0f, 3 * getDefaultRadius())); setAgentPosition(1, new Vector2(0.0f, 0.0f)); setAgentPosition(2, new Vector2(-5 * getDefaultRadius(), 0.0f)); setAgentPosition(3, new Vector2(-5 * getDefaultRadius(), -3 * getDefaultRadius())); setAgentPosition(4, new Vector2(3 * getDefaultRadius(), 0.0f)); setAgentPosition(5, new Vector2(6 * getDefaultRadius(), 0.0f)); setAgentVelocity(0, new Vector2(0.87f, 0.0f)); setAgentVelocity(1, new Vector2(1.0f, 0.0f)); setAgentVelocity(2, new Vector2(0.93f, 0.0f)); setAgentVelocity(3, Vector2.rotation(new Vector2(0.93f, 0.0f), (float)Math.PI / 6 + 0.1f)); setAgentVelocity(4, Vector2.rotation(new Vector2(0.93f, 0.0f), (float)Math.PI / 6 - 0.1f)); setAgentVelocity(5, Vector2.rotation(new Vector2(0.87f, 0.0f), 2 * ((float)Math.PI / 6 - 0.1f))); detectGroups(false); Console.Write("Detect Groups\n"); for (int i = 0; i < agents_.Count; i++) { Console.Write("Agent:" + i + " Address:" + agents_[i] + " group:" + agents_[i].groupBelongingTo_ + "\n"); } GroupAgent group1 = agents_[1].groupBelongingTo_; Console.Write("\n Group1 : Agents = "); for (int i = 0; i < group1.agents_.Count; i++) { Console.Write(group1.agents_[i] + " "); } Console.Write("\n\n"); // Test Represent group setAgentVelocity(0, new Vector2(1, 0)); detectGroups(false); Console.Write("Detect Groups\n"); for (int i = 0; i < agents_.Count; i++) { Console.Write("Agent:" + i + " Address:" + agents_[i] + " group:" + agents_[i].groupBelongingTo_ + "\n"); } group1 = agents_[1].groupBelongingTo_; Console.Write("\n Group1 : Agents = "); for (int i = 0; i < group1.agents_.Count; i++) { Console.Write(group1.agents_[i] + " "); } Console.Write("\n\n"); addAgent(new Vector2(3, 3), 0, true, true); SuperAgent sa = group1.RepresentGroup(agents_.Last()); Console.Write("Represent group\n"); Console.Write("Position: " + sa.position_ + ". Velocity: " + sa.velocity_ + ". Radius: " + sa.radius_ + "\n"); setAgentPosition(agents_.Count - 1, new Vector2(2, 1.5f)); sa = group1.RepresentGroup(agents_.Last()); Console.Write("Position: " + sa.position_ + ". Velocity: " + sa.velocity_ + ". Radius: " + sa.radius_ + "\n"); setAgentPosition(agents_.Count - 1, new Vector2(0.75f, 0.75f)); sa = group1.RepresentGroup(agents_.Last()); Console.Write("Position: " + sa.position_ + ". Velocity: " + sa.velocity_ + ". Radius: " + sa.radius_ + "\n"); }
/** * <summary>Computes the interaction between this agent and all the others agents.</summary> */ public void interactWithAgents() { IList <GroupAgent> groups = new List <GroupAgent>(); for (int i = 0; i < agentNeighbors_.Count; ++i) { Agent neighbor = agentNeighbors_[i].Value; if (considerGroups_) { bool present = false; GroupAgent agentGroup = neighbor.groupBelongingTo_; // If the agent does not belong to any group or if it belongs to the same group, interact with it if (agentGroup == null || agentGroup == groupBelongingTo_) { interactWith(neighbor); } // Otherwise, add its group to the list of the groups to interact with else { foreach (GroupAgent g in groups) { if (agentGroup == g) { present = true; } } // If the group has not been selected yet, add it to the group list if (!present) { groups.Add(agentGroup); } } } else { interactWith(neighbor); } } // Interact with each group of the list for (int i = 0; i < groups.Count; ++i) { GroupAgent group = groups[i]; // If the group's representation succeded, interact with it SuperAgent groupRepresentation = group.representGroup(this); if (groupRepresentation.sim_ != null) { interactWith(groupRepresentation); } // Otherwise, interact with each agent of the group else { IList <Agent> agentsOfTheGroup = group.getAgents(); foreach (Agent a in agentsOfTheGroup) { interactWith(a); } } } }