map.cpp
来自「TSP问题的一个类库 有源代码和stl」· C++ 代码 · 共 107 行
CPP
107 行
/*
travelsalesman by Kai Schutte (skander@skander.com)
started Feb 8th 2000, file: map.cpp
personal research for evolutionary computation (IEEE Spectrum Magazine, Feb '00)
http://www.webpatterns.net/
*/
/*
Copyright (C) 2000 Kai Schutte
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "map.h"
#include <stdlib.h>
#include <iostream.h>
#include <fstream.h>
#include <sys/time.h>
#include <math.h>
#include <unistd.h>
void cmap::read(char* filename) {
ifstream file;
file.open(filename, ios::in);
if (file.bad()) {
cerr << "fatal: Error opening file " << filename << " for read\n";
exit(1);
}
file >> num_cities;
char temp;
cities = new smap[num_cities];
for (int t=0; t<num_cities; t++) {
file >> cities[t].x >> temp >> cities[t].y;
}
}
void cmap::make(char* filename, int make_num_cities, double xax, double yax) {
ofstream file;
file.open(filename, ios::out);
if (file.bad()) {
cerr << "fatal: Error opening file " << filename << " for write\n";
exit(1);
}
struct timeval tv;
num_cities = make_num_cities;
cities = new smap[num_cities];
file << num_cities << endl;
for (int t=0;t<num_cities;t++) {
gettimeofday(&tv,NULL);
srand(tv.tv_usec*(t+1));
cities[t].x = (double) rand();
cities[t].x = (cities[t].x / (double)RAND_MAX) * xax;
if (rand() % 2)
cities[t].x = -cities[t].x;
file << cities[t].x << ";";
cities[t].y = (double) rand();
cities[t].y = (cities[t].y / (double)RAND_MAX) * yax;
if (rand() % 2)
cities[t].y = -cities[t].y;
file << cities[t].y << endl;
}
}
double cmap::get_x(int city_id) {
return cities[city_id].x;
}
double cmap::get_y(int city_id) {
return cities[city_id].y;
}
float cmap::get_distance(int city_id_1, int city_id_2) {
return sqrt(pow(cities[city_id_1].x - cities[city_id_2].x, 2) +
pow(cities[city_id_1].y - cities[city_id_2].y, 2));
}
int cmap::get_count() {
return num_cities;
}
void cmap::check(int should_be_count, double xax, double yax) {
if (should_be_count != num_cities)
cout << "\t\tcmap: num_cities isn't what it should be!! should be: "
<< should_be_count << " is: " << num_cities << endl;
for (int t=0; t<num_cities; t++) {
if (cities[t].x > xax || cities[t].x < -xax || cities[t].x == 0)
cout << "\t\tcmap: point out of range in X: "
<< t << " is: " << cities[t].x << endl;
if (cities[t].y > yax || cities[t].y < -yax || cities[t].y == 0)
cout << "\t\tcmap: point out of range in Y: "
<< t << " is: " << cities[t].y << endl;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?