Exemple #1
0
        public Int32 LongestDistance()
        {
            List <T[]> permutations = Permute <T> .PermuteToList(_ends.ToArray());

            Int32 longestRoute = 0;

            T[] longestRoutePermutation = new T[_ends.Count()];

            foreach (T[] permutation in permutations)
            {
                Int32 currentDistance = 0;

                for (int i = 0; i < permutation.Length - 1; i++)
                {
                    IPath <T> path = _paths.Where(x => x.Ends.Contains(permutation[i]))
                                     .Where(y => y.Ends.Contains(permutation[i + 1])).First();

                    currentDistance = currentDistance + path.Distance;
                }

                if (currentDistance > longestRoute)
                {
                    longestRoute = currentDistance;
                    permutation.CopyTo(longestRoutePermutation, 0);
                }
            }

            return(longestRoute);
        }
Exemple #2
0
        public Int32 BestHappinessSeatingPlan()
        {
            GetPeopleFromRelationships();

            List <String[]> peoplePermutations = Permute <String> .PermuteToList(People.ToArray());

            int maxHappinessLevel = 0;

            foreach (String[] peoplePermutation in peoplePermutations)
            {
                int permutationHappiness = 0;

                for (int i = 0; i < peoplePermutation.Length; i++)
                {
                    //Calculate happiness of that person to the left and right.  i is the source.
                    int rightHappiness = Relationships.Where(x => x.SourceName.Equals(peoplePermutation[i]))
                                         .Where(y => y.AcquaintanceName.Equals(peoplePermutation[(i + 1) % peoplePermutation.Length]))
                                         .First().Happiness;

                    int leftHappiness = Relationships.Where(x => x.SourceName.Equals(peoplePermutation[i]))
                                        .Where(y => y.AcquaintanceName.Equals(peoplePermutation[(i - 1 + peoplePermutation.Length) % peoplePermutation.Length]))
                                        .First().Happiness;

                    permutationHappiness = permutationHappiness + rightHappiness + leftHappiness;
                }

                if (permutationHappiness > maxHappinessLevel)
                {
                    maxHappinessLevel = permutationHappiness;
                }
            }

            return(maxHappinessLevel);
        }
Exemple #3
0
        public Int32 ShortestDistance()
        {
            List <T[]> permutations = Permute <T> .PermuteToList(_ends.ToArray());

            Int32 shortestRoute = Int32.MaxValue;

            T[] shortestRoutePermutation = new T[_ends.Count()];

            foreach (T[] permutation in permutations)
            {
                Int32 currentDistance = 0;
                bool  exitedEarly     = false;

                for (int i = 0; i < permutation.Length - 1; i++)
                {
                    IPath <T> path = _paths.Where(x => x.Ends.Contains(permutation[i]))
                                     .Where(y => y.Ends.Contains(permutation[i + 1])).First();

                    currentDistance = currentDistance + path.Distance;

                    if (currentDistance >= shortestRoute)
                    {
                        exitedEarly = true;
                        break;
                    }
                }

                if (exitedEarly == false)
                {
                    if (currentDistance < shortestRoute)
                    {
                        shortestRoute = currentDistance;
                        permutation.CopyTo(shortestRoutePermutation, 0);
                    }
                }
            }

            return(shortestRoute);
        }