Exemplo n.º 1
0
        public void CreatePopulation(int i_size)
        {
            int[] permutation = new int[i_size];
            for (int i = 0; i < i_size; i++)
            {
                permutation[i] = i;
            }
            Inhabitants[0] = new Individual(permutation);
            var temp = EvalFunctions.GetFitness(Inhabitants[0]);

            Inhabitants[0].FitnessD = temp.Item1;
            Inhabitants[0].FitnessT = temp.Item2;
            Inhabitants[0].FitnessC = temp.Item3;
            Inhabitants[0].Fitness  = temp.Item4;
            //Creating random permutations using Knuth algorithm.
            for (int i = 1; i < Size; i++)
            {
                permutation    = ExtensionMethods.GetRandomPermutation(permutation);
                Inhabitants[i] = new Individual(permutation);
                var fitness = EvalFunctions.GetFitness(Inhabitants[i]);
                Inhabitants[i].FitnessD = fitness.Item1;
                Inhabitants[i].FitnessT = fitness.Item2;
                Inhabitants[i].FitnessC = fitness.Item3;
                Inhabitants[i].Fitness  = fitness.Item4;
            }
            Sort();
        }
Exemplo n.º 2
0
        public Individual CreateIndividual()
        {
            HashSet <int> inPath;

            inPath = new HashSet <int>();
            Random rd = new Random();

            int[]  new_path = Enumerable.Range(0, path_size).ToArray();
            double threshold, cumulative_probability;

            //Creating path.
            //i -> x; j -> y
            for (int i = 0; i < path_size - 1; i++)
            {
                threshold = rd.NextDouble();
                cumulative_probability = 0;
                for (int j = 0; j < path_size; j++)
                {
                    cumulative_probability += distribution[i, j];

                    if (cumulative_probability < threshold)
                    {
                        continue;
                    }
                    //If a node is already in the set, start the search for another node.
                    if (inPath.Contains(j))
                    {
                        break;
                    }
                    else
                    {
                        //If the node isn't in the set, add it.
                        //place node in the right position.
                        inPath.Add(j);
                        int position = new_path.FindValue(j);
                        int temp;
                        temp               = new_path[i];
                        new_path[i]        = new_path[position];
                        new_path[position] = temp;
                        break;
                    }
                }
            }

            Individual new_individual = new Individual(new_path);
            var        fitness        = EvalFunctions.GetFitness(new_individual);

            new_individual.FitnessD = fitness.Item1;
            new_individual.FitnessT = fitness.Item2;
            new_individual.FitnessC = fitness.Item3;
            new_individual.Fitness  = fitness.Item4;

            return(new_individual);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Creates a new solution in the neighborhood of the given solution.
        /// </summary>
        /// <param name="solution"></param>
        /// <returns></returns>
        private Individual newSolution(Individual solution)
        {
            rd = new Random();
            int[] new_path = solution.Path;
            //Creating new path in the neighborhood.
            for (int i = 0; i < new_path.Length / 10; i++)
            {
                swap(ref new_path[rd.Next() % new_path.Length], ref new_path[rd.Next() % new_path.Length]);
            }
            Individual new_solution = new Individual(new_path);

            //Setting fitness for new solution.
            var fitness = EvalFunctions.GetFitness(new_solution);

            new_solution.FitnessD = fitness.Item1;
            new_solution.FitnessT = fitness.Item2;
            new_solution.FitnessC = fitness.Item3;
            new_solution.Fitness  = fitness.Item4;

            return(new_solution);
        }