Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            var permute = new Permuter<string>(new List<string>(){"a", "b", "c", "d"});

            var results = permute.AllPermutations();

            foreach (var list in results)
            {
                foreach (var x in list)
                {
                    Console.Write(x.ToString());
                }
                Console.WriteLine();
            }

            var permute2 = new Permuter<int>(new List<int>(){1, 2, 3, 4, 5, 6});
            var results2 = permute2.AllPermutations();

            foreach (var list in results2)
            {
                foreach (var x in list)
                {
                    Console.Write(x.ToString());
                }
                Console.WriteLine();
            }

            Console.ReadKey();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Enumerates all solutions.  WARNING: if there are n numbers then
        /// there are n! permutations.  This number gets large very quickly.
        /// Assumes the first and last points in the array are fixed.
        /// </summary>
        public void Solve()
        {
            var initSolution = new List<int>();

            for (int i = 1; i <= solution.Count - 2; i++)
            {
                initSolution.Add(solution[i]);
            }

            var permute = new Permuter<int>(initSolution);
            var results = permute.AllPermutations();

            double bestSolutionCost = objective.Value(solution);
            var bestSolution = solution;

            foreach (var permutation in results)
            {
                //add start location to the start and end or list
                permutation.Insert(0, 0);
                permutation.Add(0);

                var cost = this.objective.Value(permutation);

                if (cost < bestSolutionCost)
                {
                    bestSolutionCost = cost;
                    bestSolution = permutation;
                }
            }
        }