Beispiel #1
0
    public void Initialize(float maxForce, int lifeTime, Vector2 targetPos, Vector2[] genes, Vector3 startpos)
    {
        dnaBoid        = new DNAboid(genes, maxForce, lifeTime);
        this.lifeTime  = lifeTime;
        this.targetpos = targetpos;

        transform.position    = startpos;
        this.startPos         = startPos;
        this.distanceToTarget = 10000;
        finishedTime          = 0;
    }
    // Creating a new Generation
    public void Generate()
    {
        for (int i = 0; i < boids.Length; i++)
        {
            Boid    randomBoid_1 = mattingPool [Random.Range(0, mattingPool.Count)];
            Boid    randomBoid_2 = mattingPool [Random.Range(0, mattingPool.Count)];
            DNAboid newChild     = randomBoid_1.dnaBoid.CrossOver(randomBoid_2.dnaBoid);
            newChild.Mutate(mutationRate);
            boids [i].dnaBoid = null;
            boids [i].dnaBoid = newChild;
            boids [i].Initialize(maxForce, lifeSpan, targetPos, boids [i].dnaBoid.genes, startPos);
        }

        generation++;
    }
    public DNAboid CrossOver(DNAboid patner)
    {
        Vector2[] childGenes     = new Vector2[patner.genes.Length];
        int       randomMidPoint = Random.Range(0, childGenes.Length);

        for (int i = 0; i < childGenes.Length; i++)
        {
            if (i > randomMidPoint)
            {
                childGenes [i] = this.genes [i];
            }
            else
            {
                childGenes [i] = patner.genes [i];
            }
        }
        DNAboid newChild = new DNAboid(childGenes, this.maxForce, this.lifeTime);

        return(newChild);
    }