Beispiel #1
0
    public void AddConnection(PSOParticle particle)
    {
        if (connectionPrefab == null)
        {
            return;
        }

        // Just keep one direction of the pair of particles (halves the line renderers needed)
        if (particle.particleId < particleId)
        {
            return;
        }

        if (connections == null)
        {
            connections = new List <Connection>();
        }

        Connection conn = new Connection
        {
            particle     = particle,
            lineRenderer = Instantiate(connectionPrefab, transform)
        };

        connections.Add(conn);
    }
Beispiel #2
0
        public PSOContext(int PopSize, int Dimensions, double MaxVel, double[] UpperBounds, double[] LowerBounds)
        {
            rnd = new Random();
            myPopSize = PopSize;
            myDimension = Dimensions;
            myUpperBounds = UpperBounds;
            myLowerBounds = LowerBounds;
            myParticles = new PSOParticle[myPopSize];
            for (int i = 0; i < myPopSize; i++) {
                myParticles[i] = new PSOParticle(Dimensions, ref myUpperBounds, ref myLowerBounds, MaxVel, ref rnd);
            }

            myGlobalBest = myParticles[rnd.Next(myPopSize)];
        }
Beispiel #3
0
        public void iterate(double[] scores)
        {
            if (scores.Length != myParticles.Length)
                throw new Exception("Array length does not match population size.");

            for (int i = 0; i < myParticles.Length; i++) {
                if (scores[i] < myBestFitness) {
                    myBestFitness = scores[i];
                    myGlobalBest = myParticles[i];
                    //myGlobalBest.Fitness = myBestFitness;
                }
            }

            for (int i = 0; i < myParticles.Length; i++) {
                myParticles[i].GlobalBest = myGlobalBest;
                myParticles[i].iterate(scores[i]);
            }
        }
Beispiel #4
0
    public PSOParticle GetWorstParticle()
    {
        if (particles == null)
        {
            return(null);
        }

        float       val      = -float.MaxValue;
        PSOParticle particle = null;

        foreach (var p in particles)
        {
            if (p.transform.position.y > val)
            {
                particle = p;
                val      = p.transform.position.y;
            }
        }

        return(particle);
    }
Beispiel #5
0
    override public bool Restart(int seed, float estimatedTime)
    {
        int tries    = 0;
        int maxTries = 25;

        rndGen = new System.Random(seed);

        while (tries < maxTries)
        {
            elapsedTime = 0;

            PSORender psoRender = FindObjectOfType <PSORender>();

            startLookPos = new Vector3(rndGen.Range(-1, 1), rndGen.Range(0.5f, 1.5f), rndGen.Range(-1, 1)).normalized *outerRadius *scale;

            startLookPos.y = Mathf.Max(startLookPos.y, psoRender.extentsY.y);

            PSOParticle particle = null;

            switch (targetParticle)
            {
            case TargetParticle.None:
                targetPos = rndGen.onUnitSphere() * innerRadius * scale;
                if (targetPos.y < 0)
                {
                    targetPos.y = Mathf.Abs(targetPos.y);
                }
                break;

            case TargetParticle.Best:
                particle = psoRender.GetBestParticle();
                break;

            case TargetParticle.Worst:
                particle = psoRender.GetBestParticle();
                break;

            case TargetParticle.Random:
                int index = rndGen.Range(0, psoRender.GetParticleCount());
                particle = psoRender.GetParticle(index);
                break;

            default:
                break;
            }

            transform.position = startLookPos;

            if (particle)
            {
                if (usePrediction)
                {
                    targetPos = particle.Predict(estimatedTime);
                }
                else
                {
                    particleTransform = particle.transform;
                    targetPos         = particleTransform.position;
                }

                Vector3 toTarget = targetPos - startLookPos;
                Vector3 dir      = toTarget.normalized;
                float   maxDist  = toTarget.magnitude;

                if (dir.y < -0.1f)
                {
                    // Check raycast
                    if (!Physics.Raycast(startLookPos, dir, maxDist))
                    {
                        break;
                    }
                }

                tries++;
            }
            else
            {
                Vector3 toTarget = targetPos - startLookPos;
                Vector3 dir      = toTarget.normalized;
                float   maxDist  = toTarget.magnitude;

                if (dir.y < -0.1f)
                {
                    break;
                }
            }
        }

        if (tries == maxTries)
        {
            return(false);
        }

        FixedUpdate();

        return(true);
    }