예제 #1
0
 public EmitterBase(EmitterActionType emitterType, Vector2 position, Parameters parameters)
 {
     IsActive = true;
     EmitterType = emitterType;
     Position = position;
     Parameters = parameters;
 }
예제 #2
0
파일: Parameters.cs 프로젝트: kbo4sho/Swarm
 public Parameters(Parameters parent)
 {
     neighborhoodRadius = parent.neighborhoodRadius;
     normalSpeed = parent.normalSpeed;
     maxSpeed = parent.maxSpeed;
     c1 = parent.c1;
     c2 = parent.c2;
     c3 = parent.c3;
     c4 = parent.c4;
     c5 = parent.c5;
 }
예제 #3
0
파일: Individual.cs 프로젝트: kbo4sho/Swarm
        public Individual(int id, double x, double y, double dx, double dy, double dx2, double dy2, Parameters g, EmitterActionType emitterType, bool isMobile)
        {
            ID = id;
            X = x;
            Y = y;
            Dx = Dx2 = dx;
            Dy = Dy2 = dy;
            Genome = g;
            EmitterType = emitterType;
            IsMobile = isMobile;

            color = Genome.getDisplayColor(emitterType);
            rankInXOrder = 0;
            rankInYOrder = 0;
        }
예제 #4
0
파일: Individual.cs 프로젝트: kbo4sho/Swarm
 public Individual(int id, double x, double y, double dx, double dy, Parameters g, EmitterActionType emitterType, bool isMobile)
     : this(id, x, y, dx, dy, dx, dy, g, emitterType, isMobile)
 {
 }
예제 #5
0
파일: Individual.cs 프로젝트: kbo4sho/Swarm
 public Individual(int id, double x, double y, double dx, double dy, Parameters g)
     : this(id, x, y, dx, dy, g, EmitterActionType.Still, true)
 {
 }
예제 #6
0
 public Individual(int id, double x, double y, double dx, double dy, Parameters g)
     : this(id, x, y, dx, dy, g, EmitterActionType.Still, true)
 {
 }
예제 #7
0
        public Individual(int id, double x, double y, double dx, double dy, double dx2, double dy2, Parameters g, EmitterActionType emitterType, bool isMobile)
        {
            ID          = id;
            X           = x;
            Y           = y;
            Dx          = Dx2 = dx;
            Dy          = Dy2 = dy;
            Genome      = g;
            EmitterType = emitterType;
            IsMobile    = isMobile;

            color        = Genome.getDisplayColor(emitterType);
            rankInXOrder = 0;
            rankInYOrder = 0;
        }
예제 #8
0
 public Individual(int id, double x, double y, double dx, double dy, Parameters g, EmitterActionType emitterType, bool isMobile)
     : this(id, x, y, dx, dy, dx, dy, g, emitterType, isMobile)
 {
 }
예제 #9
0
        public void stepSimulation(List<Individual> temporaryIndividuals, int weightOfTemporaries)
        {
            int numberOfSwarm = swarmInBirthOrder.Count();

            for (int i = 0; i < numberOfSwarm; i++)
            {
                currentInd = swarmInBirthOrder[i];
                param = currentInd.Genome;
                tempX = currentInd.X;
                tempY = currentInd.Y;

                neighborhoodRadiusSquared = param.getNeighborhoodRadius()
                        * param.getNeighborhoodRadius();

                List<Individual> neighbors = new List<Individual>();

                // Detecting neighbors using sorted lists
                minX = tempX - param.getNeighborhoodRadius();
                maxX = tempX + param.getNeighborhoodRadius();
                minY = tempY - param.getNeighborhoodRadius();
                maxY = tempY + param.getNeighborhoodRadius();
                minRankInXOrder = currentInd.RankInXOrder;
                maxRankInXOrder = currentInd.RankInXOrder;
                minRankInYOrder = currentInd.RankInYOrder;
                maxRankInYOrder = currentInd.RankInYOrder;

                //TODO: Make this faster
                for (int j = currentInd.RankInXOrder - 1; j >= 0; j--)
                {
                    if (swarmInXOrder[j].X >= minX)
                        minRankInXOrder = j;
                    else
                        break;
                }
                for (int j = currentInd.RankInXOrder + 1; j < numberOfSwarm; j++)
                {
                    if (swarmInXOrder[j].X <= maxX)
                        maxRankInXOrder = j;
                    else
                        break;
                }
                for (int j = currentInd.RankInYOrder - 1; j >= 0; j--)
                {
                    if (swarmInYOrder[j].Y >= minY)
                        minRankInYOrder = j;
                    else
                        break;
                }
                for (int j = currentInd.RankInYOrder + 1; j < numberOfSwarm; j++)
                {
                    if (swarmInYOrder[j].Y <= maxY)
                        maxRankInYOrder = j;
                    else
                        break;
                }

                if (maxRankInXOrder - minRankInXOrder < maxRankInYOrder
                        - minRankInYOrder)
                {
                    for (int j = minRankInXOrder; j < maxRankInXOrder; j++)
                    {
                        tempSwarm2 = swarmInXOrder[j];
                        if (currentInd != tempSwarm2)
                            if (tempSwarm2.RankInYOrder >= minRankInYOrder
                                    && tempSwarm2.RankInYOrder <= maxRankInYOrder)
                            {
                                if ((tempSwarm2.X - currentInd.X)
                                        * (tempSwarm2.X - currentInd.X)
                                        + (tempSwarm2.Y - currentInd.Y)
                                        * (tempSwarm2.Y - currentInd.Y) < neighborhoodRadiusSquared)
                                    neighbors.Add(tempSwarm2);
                            }
                    }
                }
                else
                {
                    for (int j = minRankInYOrder; j < maxRankInYOrder; j++)
                    {
                        tempSwarm2 = swarmInYOrder[j];
                        if (currentInd != tempSwarm2)
                            if (tempSwarm2.RankInXOrder >= minRankInXOrder
                                    && tempSwarm2.RankInXOrder <= maxRankInXOrder)
                            {
                                if ((tempSwarm2.X - currentInd.X)
                                        * (tempSwarm2.X - currentInd.X)
                                        + (tempSwarm2.Y - currentInd.Y)
                                        * (tempSwarm2.Y - currentInd.Y) < neighborhoodRadiusSquared)
                                    neighbors.Add(tempSwarm2);
                            }
                    }
                }

                for (int k = 0; k < temporaryIndividuals.Count; k++)
                {
                    if ((temporaryIndividuals[k].X - currentInd.X)
                        * (temporaryIndividuals[k].X - currentInd.X)
                        + (temporaryIndividuals[k].Y - currentInd.Y)
                        * (temporaryIndividuals[k].Y - currentInd.Y) < neighborhoodRadiusSquared)
                        for (int j = 0; j < weightOfTemporaries; j++)
                            neighbors.Add(temporaryIndividuals[k]);
                }

                // simulating the behavior of swarm agents

                int n = neighbors.Count();

                if (n == 0)
                {

                    tempAx = rand.NextDouble() - 0.5;
                    tempAy = rand.NextDouble() - 0.5;
                }
                else
                {
                    localCenterX = 0;
                    localCenterY = 0;
                    localDX = 0;
                    localDY = 0;
                    for (int j = 0; j < n; j++)
                    {
                        tempSwarm2 = neighbors[j];
                        localCenterX += tempSwarm2.X;
                        localCenterY += tempSwarm2.Y;
                        localDX += tempSwarm2.Dx;
                        localDY += tempSwarm2.Dy;
                    }
                    localCenterX /= n;
                    localCenterY /= n;
                    localDX /= n;
                    localDY /= n;

                    tempAx = tempAy = 0;

                    tempAx += (localCenterX - tempX) * param.getC1();
                    tempAy += (localCenterY - tempY) * param.getC1();

                    tempAx += (localDX - currentInd.Dx) * param.getC2();
                    tempAy += (localDY - currentInd.Dy) * param.getC2();

                    for (int j = 0; j < n; j++)
                    {
                        tempSwarm2 = neighbors[j];
                        tempX2 = tempSwarm2.X;
                        tempY2 = tempSwarm2.Y;
                        double d = (tempX - tempX2) * (tempX - tempX2) + (tempY - tempY2)
                                * (tempY - tempY2);
                        if (d == 0)
                            d = 0.001;
                        tempAx += (tempX - tempX2) / d * param.getC3();
                        tempAy += (tempY - tempY2) / d * param.getC3();
                    }

                    //rand = new Random();
                    //if (rand.NextDouble() < param.getC4())
                    //{
                    //    tempAx += rand.NextDouble() * 10 - 5;
                    //    tempAy += rand.NextDouble() * 10 - 5;
                    //}
                }

                currentInd.accelerate(tempAx, tempAy, param.getMaxSpeed());

                tempDX = currentInd.Dx2;
                tempDY = currentInd.Dy2;
                double f = Math.Sqrt(tempDX * tempDX + tempDY * tempDY);
                if (f == 0)
                    f = 0.001;
                currentInd.accelerate(tempDX * (param.getNormalSpeed() - f) / f
                        * param.getC5(),
                        tempDY * (param.getNormalSpeed() - f) / f * param.getC5(),
                        param.getMaxSpeed());

            };
            updateInternalState();
        }
예제 #10
0
        public void stepSimulation(List <Individual> temporaryIndividuals, int weightOfTemporaries)
        {
            int numberOfSwarm = swarmInBirthOrder.Count();

            for (int i = 0; i < numberOfSwarm; i++)
            {
                currentInd = swarmInBirthOrder[i];
                param      = currentInd.Genome;
                tempX      = currentInd.X;
                tempY      = currentInd.Y;

                neighborhoodRadiusSquared = param.getNeighborhoodRadius()
                                            * param.getNeighborhoodRadius();

                List <Individual> neighbors = new List <Individual>();

                // Detecting neighbors using sorted lists
                minX            = tempX - param.getNeighborhoodRadius();
                maxX            = tempX + param.getNeighborhoodRadius();
                minY            = tempY - param.getNeighborhoodRadius();
                maxY            = tempY + param.getNeighborhoodRadius();
                minRankInXOrder = currentInd.RankInXOrder;
                maxRankInXOrder = currentInd.RankInXOrder;
                minRankInYOrder = currentInd.RankInYOrder;
                maxRankInYOrder = currentInd.RankInYOrder;

                //TODO: Make this faster
                for (int j = currentInd.RankInXOrder - 1; j >= 0; j--)
                {
                    if (swarmInXOrder[j].X >= minX)
                    {
                        minRankInXOrder = j;
                    }
                    else
                    {
                        break;
                    }
                }
                for (int j = currentInd.RankInXOrder + 1; j < numberOfSwarm; j++)
                {
                    if (swarmInXOrder[j].X <= maxX)
                    {
                        maxRankInXOrder = j;
                    }
                    else
                    {
                        break;
                    }
                }
                for (int j = currentInd.RankInYOrder - 1; j >= 0; j--)
                {
                    if (swarmInYOrder[j].Y >= minY)
                    {
                        minRankInYOrder = j;
                    }
                    else
                    {
                        break;
                    }
                }
                for (int j = currentInd.RankInYOrder + 1; j < numberOfSwarm; j++)
                {
                    if (swarmInYOrder[j].Y <= maxY)
                    {
                        maxRankInYOrder = j;
                    }
                    else
                    {
                        break;
                    }
                }

                if (maxRankInXOrder - minRankInXOrder < maxRankInYOrder
                    - minRankInYOrder)
                {
                    for (int j = minRankInXOrder; j < maxRankInXOrder; j++)
                    {
                        tempSwarm2 = swarmInXOrder[j];
                        if (currentInd != tempSwarm2)
                        {
                            if (tempSwarm2.RankInYOrder >= minRankInYOrder &&
                                tempSwarm2.RankInYOrder <= maxRankInYOrder)
                            {
                                if ((tempSwarm2.X - currentInd.X)
                                    * (tempSwarm2.X - currentInd.X)
                                    + (tempSwarm2.Y - currentInd.Y)
                                    * (tempSwarm2.Y - currentInd.Y) < neighborhoodRadiusSquared)
                                {
                                    neighbors.Add(tempSwarm2);
                                }
                            }
                        }
                    }
                }
                else
                {
                    for (int j = minRankInYOrder; j < maxRankInYOrder; j++)
                    {
                        tempSwarm2 = swarmInYOrder[j];
                        if (currentInd != tempSwarm2)
                        {
                            if (tempSwarm2.RankInXOrder >= minRankInXOrder &&
                                tempSwarm2.RankInXOrder <= maxRankInXOrder)
                            {
                                if ((tempSwarm2.X - currentInd.X)
                                    * (tempSwarm2.X - currentInd.X)
                                    + (tempSwarm2.Y - currentInd.Y)
                                    * (tempSwarm2.Y - currentInd.Y) < neighborhoodRadiusSquared)
                                {
                                    neighbors.Add(tempSwarm2);
                                }
                            }
                        }
                    }
                }

                for (int k = 0; k < temporaryIndividuals.Count; k++)
                {
                    if ((temporaryIndividuals[k].X - currentInd.X)
                        * (temporaryIndividuals[k].X - currentInd.X)
                        + (temporaryIndividuals[k].Y - currentInd.Y)
                        * (temporaryIndividuals[k].Y - currentInd.Y) < neighborhoodRadiusSquared)
                    {
                        for (int j = 0; j < weightOfTemporaries; j++)
                        {
                            neighbors.Add(temporaryIndividuals[k]);
                        }
                    }
                }

                // simulating the behavior of swarm agents

                int n = neighbors.Count();

                if (n == 0)
                {
                    tempAx = rand.NextDouble() - 0.5;
                    tempAy = rand.NextDouble() - 0.5;
                }
                else
                {
                    localCenterX = 0;
                    localCenterY = 0;
                    localDX      = 0;
                    localDY      = 0;
                    for (int j = 0; j < n; j++)
                    {
                        tempSwarm2    = neighbors[j];
                        localCenterX += tempSwarm2.X;
                        localCenterY += tempSwarm2.Y;
                        localDX      += tempSwarm2.Dx;
                        localDY      += tempSwarm2.Dy;
                    }
                    localCenterX /= n;
                    localCenterY /= n;
                    localDX      /= n;
                    localDY      /= n;

                    tempAx = tempAy = 0;

                    tempAx += (localCenterX - tempX) * param.getC1();
                    tempAy += (localCenterY - tempY) * param.getC1();

                    tempAx += (localDX - currentInd.Dx) * param.getC2();
                    tempAy += (localDY - currentInd.Dy) * param.getC2();

                    for (int j = 0; j < n; j++)
                    {
                        tempSwarm2 = neighbors[j];
                        tempX2     = tempSwarm2.X;
                        tempY2     = tempSwarm2.Y;
                        double d = (tempX - tempX2) * (tempX - tempX2) + (tempY - tempY2)
                                   * (tempY - tempY2);
                        if (d == 0)
                        {
                            d = 0.001;
                        }
                        tempAx += (tempX - tempX2) / d * param.getC3();
                        tempAy += (tempY - tempY2) / d * param.getC3();
                    }

                    //rand = new Random();
                    //if (rand.NextDouble() < param.getC4())
                    //{
                    //    tempAx += rand.NextDouble() * 10 - 5;
                    //    tempAy += rand.NextDouble() * 10 - 5;
                    //}
                }


                currentInd.accelerate(tempAx, tempAy, param.getMaxSpeed());

                tempDX = currentInd.Dx2;
                tempDY = currentInd.Dy2;
                double f = Math.Sqrt(tempDX * tempDX + tempDY * tempDY);
                if (f == 0)
                {
                    f = 0.001;
                }
                currentInd.accelerate(tempDX * (param.getNormalSpeed() - f) / f
                                      * param.getC5(),
                                      tempDY * (param.getNormalSpeed() - f) / f * param.getC5(),
                                      param.getMaxSpeed());
            }
            ;
            updateInternalState();
        }