⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tspcity.h

📁 采用visual c解决tsp问题。里面有遗传算法的选择、交叉、变异函数。
💻 H
字号:

/*
 * 
 * website: http://www.coolsoft-sd.com/
 * contact: support@coolsoft-sd.com
 *
 */

/*
 * Genetic Algorithm Library
 * Copyright (C) 2007-2008 Coolsoft Software Development
 * 
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */

#pragma once

#include <string>
#include <hash_map>

using namespace std;
using namespace stdext;

#define CITY_SIZE 14

class TspCity
{

private:

	static int _nextId;

	int _id;

	wstring _name;

	int _x;

	int _y;

public:

	TspCity(const wstring& name,
		int x,
		int y) : _name(name),
		_x(x),
		_y(y) { _id = ++_nextId; }

	inline int GetID() const { return _id; }

	inline int GetX() const { return _x; }

	inline int GetY() const { return _y; }

	float GetDistance(const TspCity& city) const;

	inline const wstring& GetName() const { return _name; }

	inline bool PointWithinCity(int x, int y) { return	x >= _x - CITY_SIZE / 2 && x <= _x + CITY_SIZE / 2 && y >= _y - CITY_SIZE / 2 && y <= _y + CITY_SIZE / 2; }

	void Draw(CDC& dc) const;

};

class TspCities
{

private:

	static TspCities _instance;

	hash_map<int, TspCity*> _cities;

public:

	inline static TspCities& GetInstance() { return _instance; }

	~TspCities() { Clear(); }

	void AddCity(const wstring& name,
		int x,
		int y);

	bool RemoveCity(int id);

	void Clear();

	void DrawCities(CDC& dc) const;

	float GetDistance(int cityA, int cityB) const;

	const TspCity* GetCityById(int id) const;

	const TspCity* GetCityByPoint(int x,
		int y) const;

	void GetCities(vector<const TspCity*>& output) const;

	inline int GetCount() const { return (int)_cities.size(); }

};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -