12,072,067 members (61,683 online)

291.5K views
529 bookmarked
Posted

Genetic Algorithm Library

, 7 Apr 2012 GPL3
A framework for genetic algorithms
 ```/* * * website: http://www.coolsoft-sd.com/ * contact: support@coolsoft-sd.com * */ #include "StdAfx.h" #include "TspCity.h" #include int TspCity::_nextId = 0; float TspCity::GetDistance(const TspCity& city) const { int s = abs( _x - city._x ); int v = abs( _y - city._y ); return sqrt( (float)( s * s + v * v ) ); } void TspCity::Draw(CDC& dc) const { dc.Ellipse( _x - CITY_SIZE / 2, _y - CITY_SIZE / 2, _x + CITY_SIZE / 2, _y + CITY_SIZE / 2 ); dc.TextOutW( _x - 5, _y - CITY_SIZE / 2 - 17, _name.c_str(), (int)_name.length() ); } void TspCities::AddCity(const wstring& name, int x, int y) { TspCity* _newCity = new TspCity( name, x, y ); _cities.insert( pair( _newCity->GetID(), _newCity ) ); } TspCities TspCities::_instance; bool TspCities::RemoveCity(int id) { TspCity* city = _cities[ id ]; if( city ) { _cities.erase( id ); delete city; return true; } return false; } void TspCities::Clear() { for( hash_map::iterator it = _cities.begin(); it != _cities.end(); ++it ) delete it->second; _cities.clear(); } void TspCities::DrawCities(CDC& dc) const { for( hash_map::const_iterator it = _cities.begin(); it != _cities.end(); ++it ) it->second->Draw( dc ); } float TspCities::GetDistance(int cityA, int cityB) const { const TspCity* a = GetCityById( cityA ); if( !a ) return -1; const TspCity* b = GetCityById( cityB ); if( !b ) return -1; return a->GetDistance( *b ); } const TspCity* TspCities::GetCityById(int id) const { hash_map::const_iterator it = _cities.find( id ); if( it != _cities.end() ) return it->second; return NULL; } const TspCity* TspCities::GetCityByPoint(int x, int y) const { for( hash_map::const_iterator it = _cities.begin(); it != _cities.end(); ++it ) { if( it->second->PointWithinCity( x, y ) ) return it->second; } return NULL; } void TspCities::GetCities(vector& output) const { output.resize( _cities.size() ); int i = 0; for( hash_map::const_iterator it = _cities.begin(); it != _cities.end(); ++it, ++i ) output[ i ] = it->second; } ```

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.