Advertisement
soulrpg

tsp_test

Oct 19th, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.59 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3. #include <fstream>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. class City{
  9. private:
  10.     int id;
  11.     int coord_x;
  12.     int coord_y;
  13.     bool visited;
  14. public:
  15.     City(){
  16.         id = -1;
  17.         coord_x = 0;
  18.         coord_y = 0;
  19.         visited = false;
  20.     }
  21.     City(int new_id, int new_x, int new_y){
  22.         id = new_id;
  23.         coord_x = new_x;
  24.         coord_y = new_y;
  25.         visited = false;
  26.     }
  27.     void set_id(int new_id){
  28.         id = new_id;
  29.     }
  30.     void set_x(int new_x){
  31.         coord_x = new_x;
  32.     }
  33.     void set_y(int new_y){
  34.         coord_y = new_y;
  35.     }
  36.     void change_visited(){
  37.         visited = !visited;
  38.     }
  39.     int get_id(){
  40.         return id;
  41.     }
  42.     void print_city(){
  43.         cout << "Id: " << id << endl;
  44.         cout << "X coord: " << coord_x << endl;
  45.         cout << "Y ooord: " << coord_y << endl;
  46.         cout << "Visited: " << visited << endl;
  47.         cout << "###" << endl;
  48.     }
  49.     double distance_between(City other){
  50.         double dist = sqrt(pow((coord_x - other.coord_x), 2) + pow((coord_y - other.coord_y), 2));
  51.         return dist;
  52.     }
  53. };
  54.  
  55. int read_file(string file_name, vector<City> &cities, int beginning = 0)
  56. {
  57.     ifstream my_file;
  58.     int amount;
  59.     int tmp[3];
  60.     int index = 0;
  61.     my_file.open(file_name.c_str());
  62.     if(my_file.is_open() == false){
  63.         cout << "Problem otwarcia pliku!" << endl;
  64.     }
  65.     my_file >> amount;
  66.     while(!my_file.eof()){
  67.         my_file >> tmp[0];
  68.         my_file >> tmp[1];
  69.         my_file >> tmp[2];
  70.         cities.push_back(City(tmp[0], tmp[1], tmp[2]));
  71.         index++;
  72.     }
  73.     my_file.close();
  74.     return amount;
  75. }
  76.  
  77. double find_shortest_dist(vector<City> &c_not_visited, City start, int &pos)
  78. {
  79.     double shortest_dist;
  80.     bool first_time_set = false;
  81.     int destination_pos = 0;
  82.     for(int i = 0; i < c_not_visited.size(); i++){
  83.         if(first_time_set == false){
  84.             shortest_dist = start.distance_between(c_not_visited.at(i));
  85.             destination_pos = i;
  86.             first_time_set = true;
  87.         }
  88.         else{
  89.             if(shortest_dist > start.distance_between(c_not_visited.at(i))){
  90.                 shortest_dist = start.distance_between(c_not_visited.at(i));
  91.                 destination_pos = i;
  92.             }
  93.         }
  94.     }
  95.     pos = destination_pos;
  96.     return shortest_dist;
  97. }
  98.  
  99. void TSP_greedy(vector<City> cities_copy, vector<City> &cities, int amount){
  100.     double road_length = 0;
  101.     int next_pos;
  102.     vector<int> road_by_id;
  103.     road_by_id.push_back(cities.at(0).get_id());
  104.     City from_city = cities_copy.at(0);
  105.     cities_copy.erase(cities_copy.begin());
  106.     while(cities_copy.size() > 0){
  107.         road_length += find_shortest_dist(cities_copy, from_city, next_pos);
  108.         road_by_id.push_back(cities.at(next_pos).get_id());
  109.         if(cities_copy.size() > 1)
  110.             from_city = cities_copy.at(next_pos);
  111.         cities_copy.erase(cities_copy.begin()+next_pos);
  112.     }
  113.     cout << "Dlugosc drogi: " << road_length << endl;
  114.     cout << "Sciezka: ";
  115.     for(int i = 0; i <= road_by_id.size(); i++){
  116.         cout << road_by_id.at(i) << " ";
  117.     }
  118.     cout << endl;
  119. }
  120.  
  121. int main()
  122. {
  123.     string file_name;
  124.     vector<City> cities;
  125.     cout << "Podaj nazwe pliku: ";
  126.     cin >> file_name;
  127.     int amount = read_file(file_name, cities);
  128.     cout << "Ilosc miast: " << amount << endl;
  129.     for(int i = 0; i < amount; i++)
  130.     {
  131.         cities.at(i).print_city();
  132.     }
  133.     TSP_greedy(cities, cities, amount);
  134.     cin.sync();
  135.     cin.get();
  136.     return 0;
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement