1

I'm having a problem with an accumulation of Point3ds in List. When I change the number of int agents (via a gui slider in grasshopper) the quantity keeps increasing rather than resetting to whatever the new quantity should be. I'm guessing that somewhere I should be re-initializing the list or clearing it everytime the value is changed? What would be the correct to do this?

        protected override void SolveInstance(IGH_DataAccess DA)
        {
            BoundingBox box = new BoundingBox(0.0, 0.0, 0.0, boundx, boundy, boundz);
            DA.SetData("Bounding Box", box);
            DA.SetData("Start", "The current trigger is set to " + started.ToString());

            // Initialize Agents
            for (int i = 0; i < agents; i++)
            {
                double xPos = RandomfromDouble(0.0, boundx);
                double yPos = RandomfromDouble(0.0, boundy);
                double zPos = RandomfromDouble(0.0, boundz);

                Point3d pos = new Point3d(xPos, yPos, zPos);        // Create Agent Start Position
                Vector3d vec = new Vector3d(xPos + RandomfromDouble(-360.00, 360.00), yPos + RandomfromDouble(-360.00, 360.00), zPos + RandomfromDouble(-360.00, 360.00));  // Create Agent Start Vector

                Agent agent = new Agent(pos, vec, alignment, separation, cohesion, neighborRadius);
                allAgents.Add(agent);

                DA.SetData("Debug", "# of Agents Created: " + allAgents.Count);
            }

            // Get agent positions
            List<Point3d> agentPositions = new List<Point3d>();
            List<Vector3d> agentVectors = new List<Vector3d>();

            agentPositions = allAgents.Select(agent => agent.Pos).ToList();
            agentVectors = allAgents.Select(agent => agent.Vec).ToList();

            DA.SetData("Agent Count", allAgents.Count);
            DA.SetDataList("Agent Points", agentPositions);
            DA.SetDataList("Agent Vectors", agentVectors);

            if (started)
            {
                DA.SetData("Start", "The current trigger is set to " + started.ToString());

                for (int i = 0; i < generations; i++)
                {
                    DA.SetData("Debug", "# of Generations: " + i);

                    foreach (Agent agent in allAgents)
                    {
                        DA.SetData("Debug", "# of Agents: " + i);

                        agent.UpdateAgent(allAgents);
                    }
                }
            }
            else if (!started)
            {
                DA.SetData("Start", "The current trigger is set to " + started.ToString());
                //return;
            }
        }

        public double RandomfromDouble(double from, double to)
        {
            double diff = Math.Abs(from - to);
            return rnd.NextDouble() * diff + from ;
        }
greyBow
  • 1,298
  • 3
  • 28
  • 62

1 Answers1

1

If I'm reading your code correctly, your issue is that the allAgents list keeps getting longer. As you guessed, that's because you're creating the list once, at the top, and then you only ever add to it, within the for loop that says // Initialize Agents.

If your intent is to reset the list at this point, then before you enter the for loop I think you need to do allAgents.Clear(). This will empty the list, and then you loop through and add the new Agents within the loop.

Trevor Reid
  • 3,310
  • 4
  • 27
  • 46
asherber
  • 2,508
  • 1
  • 15
  • 12
  • thank you, i've edited my posted code to be more concise. – greyBow Oct 30 '18 at 18:04
  • Also just realized that Moving List allAgents = new List(); back into SolveInstance function, for the for loop also fixes the issue. – greyBow Oct 30 '18 at 18:06
  • 1
    You can put that line into `SolveInstance()` as long as that `allAgents` doesn't need to be referenced from outside that method. If it does, then you need to create the list where you had it before, and just clear it inside of `SolveInstance()`. – asherber Oct 30 '18 at 19:14