Ejemplo n.º 1
0
        //kreiranje nove generacije od selektovanih jedinki
        public static List <SpaceShip> CreateGeneration(List <SpaceShip> selectedPopulation)
        {
            int generationSize             = 0;
            List <SpaceShip> newGeneration = new List <SpaceShip>();
            Random           r             = new Random(Guid.NewGuid().GetHashCode());

            while (generationSize < GenerationSize - ElitePopulation)
            {
                List <SpaceShip> parents = selectedPopulation.OrderBy(x => r.Next()).Take(2).ToList();
                SpaceShip        p1 = parents[0]; SpaceShip p2 = parents[1];
                //dobijaju se deca ukrstanjem
                List <SpaceShip> children = new List <SpaceShip>();
                if (CrossoverMethod == "onepoint")
                {
                    children = CrossoverOnePoint(p1, p2);
                }
                else if (CrossoverMethod == "twopoints")
                {
                    children = CrossoverTwoPoints(p1, p2);
                }
                SpaceShip c1 = children[0]; SpaceShip c2 = children[1];

                //deca mutiraju
                c1 = Mutate(c1); c2 = Mutate(c2);

                //dodaju se u generaciju
                newGeneration.Add(c1); newGeneration.Add(c2);
                generationSize += 2;
            }
            return(newGeneration);
        }
Ejemplo n.º 2
0
        //Mutacija treba da spreci da jedinke iz populacije postanu suvise slicne i da pomogne u obnavljanju genetskog materijala
        //ukoliko u jednoj generaciji sve jedinke imaju istu vrednost jednog gena, onda taj gen samo ukrstanjem nikada ne bi mogao da se promeni
        //omogucavaju razmatranje novih delova prostrora pretrage u nadi da ce se naici na globalni ekstremum
        //Ukoliko je verovatnoca mutacije velika, onda usmeravanje pretrage postaje preslabo i ona pocinje da lici na slucajnu pretragu

        public static SpaceShip Mutate(SpaceShip ship)
        {
            Random r           = new Random(Guid.NewGuid().GetHashCode());
            double randomValue = r.NextDouble();

            if (randomValue < MutationRate)
            {
                int randomIndex = r.Next(SpaceShip.ChromosomeSize);
                //Console.WriteLine(randomIndex);
                int actionR = r.Next(4);
                //Console.WriteLine("akcija" + actionR);
                if (actionR == 0)
                {
                    Tuple <string, int> newRun = new Tuple <string, int>("NT", r.Next(100, 400));
                    ship.run[randomIndex] = newRun;
                }
                if (actionR == 1)
                {
                    Tuple <string, int> newRun = new Tuple <string, int>("RL", r.Next(10, 25));
                    ship.run[randomIndex] = newRun;
                }

                if (actionR == 2)
                {
                    Tuple <string, int> newRun = new Tuple <string, int>("RR", r.Next(10, 25));
                    ship.run[randomIndex] = newRun;
                }
                if (actionR == 3)
                {
                    Tuple <string, int> newRun = new Tuple <string, int>("TH", r.Next(10, 25));
                    ship.run[randomIndex] = newRun;
                }
            }
            return(ship);
        }
Ejemplo n.º 3
0
        public int CompareTo(object obj)
        {
            if (obj == null)
            {
                return(1);
            }

            SpaceShip sp = obj as SpaceShip;

            return(GAFitnessFunction.CompareTo(GAFitnessFunction));
        }
Ejemplo n.º 4
0
        //jednopoziciono ukrstanje sa nasumicnom tackom
        public static List <SpaceShip> CrossoverOnePoint(SpaceShip parent1, SpaceShip parent2)
        {
            //public List<Tuple<string, int>> run
            List <SpaceShip> children = new List <SpaceShip>(2);
            Random           r        = new Random(Guid.NewGuid().GetHashCode());
            int breakPoint            = r.Next(1, SpaceShip.ChromosomeSize - 1);

            List <Tuple <string, int> > child1Run = parent1.run.Take(breakPoint).ToList().Concat(parent2.run.Skip(breakPoint).ToList()).ToList();
            List <Tuple <string, int> > child2Run = parent2.run.Take(breakPoint).ToList().Concat(parent1.run.Skip(breakPoint).ToList()).ToList();
            SpaceShip child1 = new SpaceShip(child1Run, GeneticAlgorithm.MapStartX, GeneticAlgorithm.MapStartY);
            SpaceShip child2 = new SpaceShip(child2Run, GeneticAlgorithm.MapStartX, GeneticAlgorithm.MapStartY);

            children.Add(child1);
            children.Add(child2);
            return(children);
        }
Ejemplo n.º 5
0
        //jednopoziciono ukrstanje sa nasumicnom tackom
        public static List <SpaceShip> CrossoverTwoPoints(SpaceShip parent1, SpaceShip parent2)
        {
            //public List<Tuple<string, int>> run
            List <SpaceShip> children    = new List <SpaceShip>(2);
            List <int>       breakPoints = GetNRandomIntegers(2, SpaceShip.ChromosomeSize - 1, 1);

            breakPoints.Sort();

            List <Tuple <string, int> > child1Run = parent1.run.Take(breakPoints[0]).ToList().Concat(parent2.run.Skip(breakPoints[0]).Take(breakPoints[1] - breakPoints[0]).ToList()).ToList().Concat(parent1.run.Skip(breakPoints[1]).ToList()).ToList();
            List <Tuple <string, int> > child2Run = parent2.run.Take(breakPoints[0]).ToList().Concat(parent1.run.Skip(breakPoints[0]).Take(breakPoints[1] - breakPoints[0]).ToList()).ToList().Concat(parent2.run.Skip(breakPoints[1]).ToList()).ToList();
            SpaceShip child1 = new SpaceShip(child1Run, GeneticAlgorithm.MapStartX, GeneticAlgorithm.MapStartY);
            SpaceShip child2 = new SpaceShip(child2Run, GeneticAlgorithm.MapStartX, GeneticAlgorithm.MapStartY);

            children.Add(child1);
            children.Add(child2);
            return(children);
        }