Пример #1
0
        public Ant[] reproduce(Ant otherParent)
        {
            Chromosome[] chromos = this.genome.crossOver(otherParent.Genome);

            Ant child1 = new Ant();
            Ant child2 = new Ant();

            chromos[0].mutate(mutationRate, mutationImpact);
            chromos[1].mutate(mutationRate, mutationImpact);

            child1.brain.setWeights(chromos[0].NeuralWeights);
            child2.brain.setWeights(chromos[1].NeuralWeights);


            Ant[] children = { child1, child2 };

            return(children);
        }
Пример #2
0
        public Rect GetBounds(Ant of, Canvas from)
        {
            Point origin = new Point(of.Position.X + of.Width / 2, of.Position.Y + of.Height / 2);
            Point lt     = new Point(of.Position.X, of.Position.Y);
            Point rt     = new Point(of.Position.X + of.Width, of.Position.Y);

            Vector originToLT = lt - origin;
            Vector originToRT = rt - origin;
            Vector vec        = rotateVector(originToLT, of.Angle);
            Vector vec2       = rotateVector(originToRT, of.Angle);

            Point newLT = origin + vec;
            Point newRB = origin - vec;

            Point newRT = origin + vec2;
            Point newLB = origin - vec2;

            Point boundsLT = GetTopLeft(newLT, newRB, newRT, newLB);
            Point boundsRB = GetBotRight(newLT, newRB, newRT, newLB);

            Rect bounds = new Rect(boundsLT, boundsRB);

            return(bounds);
        }
Пример #3
0
        public Vector move(Ant ant, double forceLeft, double forceRight)
        {
            Vector movement = new Vector();

            double v = 0;
            double forceToVelocity = 0.05;

            if (forceLeft * forceRight > 0)
            {
                if (forceLeft > 0)
                {
                    v = Math.Min(forceLeft, forceRight) * forceToVelocity;
                }
                else if (forceLeft < 0)
                {
                    v = Math.Max(forceLeft, forceRight) * forceToVelocity;
                }
            }

            movement.X = ant.FacingVector.X * v;
            movement.Y = ant.FacingVector.Y * v;

            return(movement);
        }
Пример #4
0
        public Vector antOriginToPoint(Ant ant, Point point)
        {
            Vector aotp = point - new Point(ant.Position.X + ant.Width / 2, ant.Position.Y + ant.Height / 2);

            return(aotp);
        }
Пример #5
0
        private void reproduce()
        {
            List <Ant> newPop = new List <Ant>();

            double coupons = 0;

            for (int i = 0; i < populationSize; i++)
            {
                coupons += ants.ElementAt(i).Genome.fitness;
            }

            for (int i = 0; i < populationSize / 2; i++)
            {
                Ant firstParent  = new Ant();
                Ant secondParent = new Ant();

                double tmpCoupons = coupons;
                double r          = rand.NextDouble() * tmpCoupons;

                //selecting 1st parent
                for (int j = 0; j < ants.Count; j++)
                {
                    Ant ant = ants.ElementAt(j);

                    if (r <= ant.Genome.fitness)
                    {
                        tmpCoupons -= ant.Genome.fitness;
                        firstParent = ant;
                        break;
                    }
                    else
                    {
                        r -= ant.Genome.fitness;
                    }
                }

                //selecting 2nd parent
                List <Ant> sec = new List <Ant>();

                sec = ants.ToArray().ToList();
                sec.Remove(firstParent);

                r = rand.NextDouble() * tmpCoupons;

                for (int j = 0; j < sec.Count; j++)
                {
                    Ant ant = sec.ElementAt(j);

                    if (r <= ant.Genome.fitness)
                    {
                        secondParent = ant;
                        break;
                    }
                    else
                    {
                        r -= ant.Genome.fitness;
                    }
                }

                //crossover
                Ant[] newAnts;

                //System.Diagnostics.Debug.WriteLine("1st parent fitness: " + firstParent.Genome.fitness + " 2nd parent fitness: " + secondParent.Genome.fitness);
                newAnts = firstParent.reproduce(secondParent);
                for (int k = 0; k < 2; k++)
                {
                    newAnts[k].body.Fill = new SolidColorBrush(Color.FromRgb((byte)rand.Next(255), (byte)rand.Next(255), (byte)rand.Next(255)));

                    newAnts[k].Angle = rand.Next(360);

                    mainCanvas.Children.Add(newAnts[k]);

                    newPop.Add(newAnts[k]);
                }
            }

            foreach (Ant ant in ants)
            {
                mainCanvas.Children.Remove(ant);
            }

            ants.Clear();
            ants = newPop;
        }
Пример #6
0
        //genetics
        private void killTheWeak(List <Ant> pop)
        {
            List <Ant> newPop = new List <Ant>();

            double coupons = 0;

            for (int i = 0; i < populationSize; i++)
            {
                coupons += pop.ElementAt(i).Genome.fitness;
            }

            for (int i = 0; i < populationSize / 2; i++)
            {
                if (pop.First().Genome.fitness != 0)
                {
                    double tmp = rand.NextDouble() * coupons;
                    for (int j = 0; j < pop.Count; j++)
                    {
                        Ant ant = pop.ElementAt(j);

                        if (tmp <= ant.Genome.fitness)
                        {
                            coupons -= ant.Genome.fitness;
                            newPop.Add(ant);
                            pop.Remove(ant);
                            break;
                        }
                        else
                        {
                            tmp -= ant.Genome.fitness;
                        }
                    }
                }
                else
                {
                    int tmp = rand.Next(pop.Count);
                    Ant ant = pop.ElementAt(tmp);

                    newPop.Add(ant);
                    pop.Remove(ant);
                }
            }


            foreach (Ant ant in pop)
            {
                mainCanvas.Children.Remove(ant);
            }

            System.Diagnostics.Debug.Write("New population: ");
            foreach (Ant ant in newPop)
            {
                System.Diagnostics.Debug.Write(ant.Spieces.ToString() + " ");
            }

            System.Diagnostics.Debug.Write("\n");

            pop.Clear();
            foreach (Ant ant in newPop)
            {
                pop.Add(ant);
            }
        }
Пример #7
0
        public void rotate(Ant ant, double forceLeft, double forceRight)
        {
            double alfa = (forceLeft - forceRight) * 0.05;

            ant.Angle += alfa;
        }