コード例 #1
0
ファイル: Location.cs プロジェクト: zokikizz/AI-Labs
        //works
        public void Mutate(Trevel individual)
        {
            Random rand = new Random();

            if (rand.NextDouble() < this.mutationRate)
            {
                int first = (int)(rand.NextDouble() * individual.listOfCities.Count);

                int second = (int)(rand.NextDouble() * individual.listOfCities.Count);


                // Console.WriteLine("Start: " + first + " end: " + second);

                while (first == second)
                {
                    first = (int)(rand.NextDouble() * individual.listOfCities.Count);

                    second = (int)(rand.NextDouble() * individual.listOfCities.Count);
                }


                Location[] arr  = individual.listOfCities.ToArray();
                Location   temp = arr[first];

                arr[first]              = arr[second];
                arr[second]             = temp;
                individual.listOfCities = arr.ToList();
            }
        }
コード例 #2
0
ファイル: Location.cs プロジェクト: zokikizz/AI-Labs
 public void saveTheBest(Trevel bestInGen)
 {
     if ((this.theBest == null))
     {
         this.theBest = bestInGen;
     }
     else if (this.theBest.fitness > bestInGen.fitness)
     {
         this.theBest = bestInGen;
     }
 }
コード例 #3
0
ファイル: Location.cs プロジェクト: zokikizz/AI-Labs
        public Trevel findTheFittiestInGeneration(List <Trevel> pop)
        {
            Trevel best = pop[0];

            foreach (Trevel temp in pop)
            {
                if (temp.fitness < best.fitness)
                {
                    best = temp;
                }
            }

            return(best);
        }
コード例 #4
0
ファイル: Location.cs プロジェクト: zokikizz/AI-Labs
        public GenericAlgorithmForTSP(int maxgeneration, int maxInOneGeneration, Trevel listOfCities, Location s)
        {
            this.Generations = new List <Generation>();

            this.MaxInOneGeneration = maxInOneGeneration;

            this.MaxGenerations = maxgeneration;

            this.places = listOfCities;

            this.start = s;

            this.places.listOfCities.Remove(s);
        }
コード例 #5
0
ファイル: Location.cs プロジェクト: zokikizz/AI-Labs
        //works
        public Trevel crossOver(Trevel parent1, Trevel parent2)
        {
            Trevel child = new Trevel();
            Random rand  = new Random();

            int start = (int)(rand.NextDouble() * parent1.listOfCities.Count);
            int end   = (int)(rand.NextDouble() * parent1.listOfCities.Count);


            Location[] childLoc = new Location[parent1.listOfCities.Count];

            for (int i = 0; i < childLoc.Length; i++)
            {
                childLoc[i] = null;
            }

            for (int i = 0; i < parent1.listOfCities.Count; i++)
            {
                if ((start < end) && (i > start) && (i < end))
                {
                    childLoc[i] = parent1.listOfCities[i];
                }
                else if ((end < start) && (i > end) && (i < start))
                {
                    childLoc[i] = parent1.listOfCities[i];
                }
            }

            for (int i = 0; i < parent2.listOfCities.Count; i++)
            {
                if (!childLoc.Contains(parent2.listOfCities[i]))
                {
                    for (int j = 0; j < childLoc.Length; j++)
                    {
                        if (childLoc[j] == null)
                        {
                            childLoc[j] = parent2.listOfCities[i];
                            break;
                        }
                    }
                }
            }

            child.listOfCities = childLoc.ToList();

            return(child);
        }
コード例 #6
0
ファイル: Location.cs プロジェクト: zokikizz/AI-Labs
        public void generateFirstgeneration()
        {
            Generation init = new Generation();
            // new population
            Trevel pop = new Trevel();

            Random random = new Random();
            List <KeyValuePair <int, int> > listOfAddedPlaces = new List <KeyValuePair <int, int> >();



            for (int i = 0; i < MaxInOneGeneration; i++)
            {
                //generate some random id
                foreach (Location item in places.listOfCities)
                {
                    listOfAddedPlaces.Add(new KeyValuePair <int, int>(random.Next(), item.ID));
                }

                var sortedByRandomValue = from item in listOfAddedPlaces
                                          orderby item.Key
                                          select item;

                //pop.listOfCities.Add(this.start);

                foreach (KeyValuePair <int, int> item in sortedByRandomValue)
                {
                    pop.listOfCities.Add(places.listOfCities.Find(x => x.ID == item.Value));
                }

                //pop.listOfCities.Add(this.start);

                init.population.Add(pop);
                listOfAddedPlaces = new List <KeyValuePair <int, int> >();
                pop = new Trevel();
            }

            this.Generations.Add(init);
        }
コード例 #7
0
        public static void readListOfCitiesFromFile(string filename, Trevel tour)
        {
            using (StreamReader fileReader = new StreamReader(filename))
            {
                string line = string.Empty;

                int numberOfCities = 0;

                string[] words;

                Location current;

                Console.WriteLine("Information about input data:");
                for (int i = 0; i < 6; i++)
                {
                    Console.WriteLine(line = fileReader.ReadLine());
                    words = line.Split(' ');
                    if (words[0] == "DIMENSION:")
                    {
                        // Console.WriteLine("Read:" + arr[1]);\
                        numberOfCities = Int32.Parse(words[1]);
                    }
                }

                for (int i = 0; i < numberOfCities; i++)
                {
                    line = fileReader.ReadLine();

                    words = line.Split(" ");

                    current = new Location(Int32.Parse(words[0]), Double.Parse(words[1]), Double.Parse(words[2]));

                    //  Console.WriteLine("Id: " + current.ID + " x: " + current.X + " y: " + current.Y);

                    tour.listOfCities.Add(current);
                }
            }
        }
コード例 #8
0
ファイル: Location.cs プロジェクト: zokikizz/AI-Labs
        public int CompareTo(Object n)
        {
            Trevel par = (Trevel)n;

            return((int)(Math.Round(this.fitness, 0) - Math.Round(par.fitness, 0)));
        }
コード例 #9
0
ファイル: Location.cs プロジェクト: zokikizz/AI-Labs
        public void Selection_CreationOfOffspring_And_MutateNewIndividuals(int numberOfGeneration)
        {
            //we will create new generation
            Generation newGeneration = new Generation();

            List <Trevel> currentPopulation = this.Generations[numberOfGeneration].population;

            double totalFitness = 0.0;

            //calculate fitness for every indiviual
            foreach (Trevel trevel in currentPopulation)
            {
                totalFitness += 1 / trevel.calculateFitness(this.start);
            }


            double previous = 0;

            //calculate probability
            foreach (Trevel temp in currentPopulation)
            {
                temp.probability = ((1 / temp.fitness / totalFitness)) + previous;
                previous         = temp.probability;
            }


            Random        roullete = new Random();
            List <Trevel> parents  = new List <Trevel>();

            while (newGeneration.population.Count < this.MaxInOneGeneration - (int)((currentPopulation.Count / 100.0) * 5))
            {
                while (parents.Count < 2)
                {
                    Trevel temp = this.roullete(numberOfGeneration);
                    if (temp != null)
                    {
                        parents.Add(temp);
                    }
                }

                if (parents.Count == 2 && parents[0] != parents[1])
                {
                    //create offspring
                    //and add new individual to new generation
                    newGeneration.population.Add(crossOver(parents[0], parents[1]));
                }

                previous = 0;
                //calculate probability
                foreach (Trevel temp in currentPopulation)
                {
                    temp.probability = ((1 / temp.fitness / totalFitness)) + previous;
                    previous         = temp.probability;
                }

                parents = new List <Trevel>();
            }



            Trevel bestInGen = findTheFittiestInGeneration(currentPopulation);

            //to save the best if it is better than someone form earlyer solution
            this.saveTheBest(bestInGen);

            //mutation on new individuals
            foreach (Trevel ind in newGeneration.population)
            {
                Mutate(ind);
            }


            //add beast 5 % offparents into newgeneration
            for (int i = 0; i < (int)((currentPopulation.Count / 100.0) * 5); i++)
            {
                currentPopulation.Remove(bestInGen);
                newGeneration.population.Add(bestInGen);
                bestInGen = findTheFittiestInGeneration(currentPopulation);
            }

            this.Generations.Add(newGeneration);
        }