예제 #1
0
        /***************************************
         * Function Name: Calculate Estimate
         * Pre-Conditions: City theCity
         *                 int Distance
         * Post-Condition: int
         *
         * Calculates the Estimate between the passed
         * City and the
         * *************************************/
        public static int CalculateEstimate(City theCity)
        {
            int i = 0;
            for (i = 0; i < allCities.Count; i++)
            {
                if (allCities[i].Name.CompareTo(destination) == 0)
                {
                    break;
                }
            }

            return (int)Distance(theCity, allCities[i]);
        }
예제 #2
0
        /************************************
         * Function: Main
         * Pre-Conditions: string[] args
         * Post-Conditions: void
         *
         * This is the main driver function
         * **********************************/
        static void Main(string[] args)
        {
            /* Read in Files */
            Console.WriteLine("Welcome to the Solver");
            Console.WriteLine("Enter Location of locations File:");
            string loc_text = Console.ReadLine();
            TextReader TR_locations = File.OpenText(loc_text);
            Console.WriteLine("Enter Location of connections File:");
            string conn_text = Console.ReadLine();
            TextReader TR_connections = File.OpenText(conn_text);

            /* Get Locations */
            getLocations(TR_locations);

            /* Get Connections */
            getConnections(TR_connections);

            /* Print City Information to the User */
            printCityInfo();

            /* Read In: Start, End, Exclude */
            getUserInput();

            /* Initialize Possible List */
            initializePossibleList();

            /* Initialize Best City */
            City Best = new City();
            int originPlace = 0;
            for (int i = 0; i < allCities.Count; i++)
            {
                if (allCities[i].Name.CompareTo(origin) == 0)
                {
                    allCities[i].prevVisited = true;
                    allCities[i].prevCity = null;
                    originPlace = i;
                    allCities[i].distToStart = 0;

                }
            }

            /* Initialize "Best" as the origin city */
            Best = allCities[originPlace];

            /* while(Best != end && ListPossible != empty){ */
            while ( possibleCities.Count != 0 && Best.Name.CompareTo(destination) != 0 )
            {
                /* Best <- ComparePossible */
                City previousCity = Best;

                //Remove the best from list of possible cities
                RemoveFromPossible(previousCity);

                //Update list of possibles [while doing this, set prevCity]
                updatePossibleList(previousCity.Name);

                //Choose the next city in the path
                Best = ComparePossibilities();

            }

            /* Print the list of all visited cities */
            printPrevVisited(Best);

            /* Print the List of Traveled Cities and distance traveled*/
            printList(Best);

            Console.Write("Press ENTER to finish program");
            string temp = Console.ReadLine();

            TR_output.Close();
        }
예제 #3
0
        /***************************************
         * Function: UpdatePrevious
         * PreCondition: City Old
         * PostCondition: void
         *
         * This takes in a city, and updates
         * the prevVisited and prevCity for the
         * corresponding city in allCities
         * *************************************/
        public static void UpdatePrevious(City toUpdate)
        {
            for (int i = 0; i < allCities.Count; i++)
            {
                if (allCities[i].Name.CompareTo(toUpdate.Name) == 0)
                {
                    allCities[i].prevCity = toUpdate.prevCity;
                    allCities[i].prevVisited = toUpdate.prevVisited;

                    break;
                }
            }
        }
예제 #4
0
        /***************************************
        * Function: printPrevVisited
        * PreCondition: City
        * PostCondition: void
        *
        * Prints out all the cities that were
        * visited by the algorithm
        * *************************************/
        public static void printPrevVisited(City Best)
        {
            Console.WriteLine("---------------------");
            Console.WriteLine("Visited Cities");

            TR_output.WriteLine("---------------------");
            TR_output.WriteLine("Visited Cities");

            for (int i = 0; i < allCities.Count; i++)
            {
                if (Best != null)
                {
                    if (allCities[i].Name.CompareTo(Best.Name) == 0)
                    {
                        allCities[i].prevVisited = true;
                    }

                    if (allCities[i].Name.CompareTo(Best.prevCity.Name) == 0)
                    {
                        allCities[i].prevVisited = true;
                    }
                }
                if (allCities[i].prevVisited)
                {
                    Console.Write(allCities[i].Name + " ");
                    TR_output.Write(allCities[i].Name+ " ");
                }
            }
            Console.WriteLine();
            TR_output.WriteLine();
        }
예제 #5
0
        /***************************************
        * Function Name: RemoveFromPossible
        * Pre-Conditions: City Best
        * Post-Condition: void
        *
        * Removes the Estimate from possible
        * *************************************/
        public static void RemoveFromPossible(City Best)
        {
            for (int i = 0; i < possibleCities.Count; i++)
            {
                if (Best.Name.CompareTo(possibleCities[i].Name) == 0)
                {

                    foreach (var c in allCities)
                    {
                        if (possibleCities[i].Name.CompareTo(c.Name) == 0) c.prevVisited = true;
                    }

                    possibleCities.Remove(possibleCities[i]);

                    break;
                }
            }
        }
예제 #6
0
        /***************************************
        * Function: printList
        * PreCondition: City
        * PostCondition: void
        *
        * Prints out all the cities that were
        * travered by the solution
        * *************************************/
        public static void printList(City destination)
        {
            Console.WriteLine("------------------------");
            Console.WriteLine("Traversed Path:");

            TR_output.WriteLine("------------------------");
            TR_output.WriteLine("Traversed Path:");

            if (destination == null || destination.Name.CompareTo("") == 0 )
            {
                Console.WriteLine("No Possible Path Found");
                TR_output.WriteLine("No Possible Path Found");
                return;
            }

            int place = 0;
            finalList.Add(destination.Name);

            for (int i = 0; i < allCities.Count; i++)
            {
                if (allCities[i].Name.CompareTo(destination.Name) == 0)
                {
                    place = i;
                    break;
                }
            }

            while (allCities[place].prevCity != null)
            {

                finalList.Add(allCities[place].prevCity.Name);

                for (int j = 0; j < allCities.Count; j++)
                {
                    if (allCities[j].Name.CompareTo(allCities[place].prevCity.Name) == 0)
                    {
                        place = j;
                        break;
                    }
                }
            }

            for (int k = finalList.Count - 1; k >= 0; k--)
            {
                Console.Write(finalList[k] + " ");
                TR_output.Write(finalList[k] + " ");
            }
            Console.WriteLine();
            TR_output.WriteLine();

            Console.WriteLine("Distance traveled: " + destination.distToStart);
        }
예제 #7
0
        /***************************************
        * Function Name: getLocations
        * Pre-Conditions: StreamReader locations
        * Post-Condition: void
        *
        * Reads the file from StreamReader and
        * add the locations to allCities
        * *************************************/
        public static void getLocations(TextReader locations)
        {
            TR_output.WriteLine("Reading in locations...");
            string input = null;
            while ((input = locations.ReadLine()) != null && input != "END")
            {
                int j = 0;
                int lastspace = -1;
                int nextspace = 0;
                City tempCity = new City();
                for (int i = 0; i < input.Length; i++)
                {
                    if (input[i].CompareTo(' ') == 0)
                    {
                        lastspace = i;
                        if (j == 0)
                        {
                            tempCity.Name = input.Substring(nextspace, lastspace);
                            nextspace = lastspace + 1;
                        }
                        else if (j == 1)
                        {

                            try
                            {
                                tempCity.x = Convert.ToInt32(input.Substring(nextspace, lastspace - nextspace));
                            }
                            catch (FormatException e)
                            {
                                Console.WriteLine("Input string is not a sequence of digits.");
                            }
                            catch (OverflowException e)
                            {
                                Console.WriteLine("The number cannot fit in an Int32.");
                            }

                            nextspace = lastspace + 1;
                        }
                        j++;
                    }
                }

                try
                {
                    tempCity.y = Convert.ToInt32(input.Substring(nextspace, (input.Length - nextspace)));
                }
                catch (FormatException e)
                {
                    Console.WriteLine("Input string is not a sequence of digits.");
                }
                catch (OverflowException e)
                {
                    Console.WriteLine("The number cannot fit in an Int32.");
                }

                // Print City to the Log
                TR_output.WriteLine(tempCity.Name + " " + tempCity.x + " " + tempCity.y);

                // Add the city to the list
                allCities.Add(tempCity);

            }
        }
예제 #8
0
 /***************************************
 * Function Name: Distance
 * Pre-Conditions: City A, City B
 * Post-Condition: Double
 *
 * Return the numerical distance between
 * City A and City B as a double
 * *************************************/
 public static double Distance(City A, City B)
 {
     return Math.Sqrt((B.x - A.x)*(B.x - A.x) + (B.y - A.y)*(B.y - A.y));
 }
예제 #9
0
        //Methods
        public bool isConnected(City city)
        {
            foreach (var c in this.connections)
            {
                if (c.Name == city.Name) { return true; }
                else { return false; }
            }

            return false;
        }
예제 #10
0
 public void addConnection(City theCity)
 {
     connections.Add(theCity);
 }