Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // TSP GA.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
- //
- #include <iostream>
- #include <vector>
- #include <ctime>
- #include <random>
- using namespace std;
- default_random_engine eng(time(0));
- const double N = 10;
- using Graph = vector<vector<double>>;
- using Way = vector<int>; // они же геномы
- struct Creature //особь
- {
- Way way;
- double fitness;
- Creature() : way(N - 1, 0), fitness(0) {}
- };
- using Generation = vector <Creature>; //поколение
- //допустим что все пути по умолчанию начинаются в 0 и заканчиваются в 0
- Graph GraphGenerator() {
- Graph graph(N);
- for (int i = 0; i < N; i++) {
- graph[i].resize(N);
- }
- uniform_real_distribution<double> distr(1, 10);
- for (int i = 0; i < N; i++) {
- for (int j = 0; j <= i; j++) {
- if (i == j) {
- graph[i][j] = 0;
- graph[j][i] = 0;
- continue;
- }
- graph[i][j] = distr(eng);
- graph[j][i] = distr(eng);
- }
- }
- return graph;
- }
- void WayShuffling(Creature &creature) {
- for (int i = 0; i < creature.way.size(); i++) {
- creature.way[i] = i + 1;
- }
- shuffle(creature.way.begin(), creature.way.end(), eng);
- }
- Generation GenerationGenerator(int creaturesnumber) {
- Generation generation;
- for (int i = 0; i < creaturesnumber; i++) {
- Creature creature;
- WayShuffling(creature);
- generation.push_back(creature);
- }
- }
- void Fitness(Creature &creature, const Graph &graph) {
- double waylong=0;
- for (int i = 0; i < creature.way.size()-1; i++) {
- waylong += graph[creature.way[i]][creature.way[i + 1]];
- }
- waylong += graph[0][creature.way[0]] + graph[creature.way.back()][0];
- creature.fitness = 1/waylong;
- }
- bool CreaturesComparing(const Creature &creature1, const Creature &creature2) { return (creature1.fitness > creature2.fitness); }
- void FitnessSort(Generation &generation) {
- sort(generation.begin(), generation.end(), CreaturesComparing);
- }
- Creature TournamentSelection(const Generation &generation, int creaturesnumber) {
- int num, maxindex, max = 0;
- for (int i = 0; i < 0.1 * creaturesnumber; i++) {
- num = rand() % creaturesnumber;
- if (generation[num].fitness > max) {
- max = generation[num].fitness;
- maxindex = num;
- }
- }
- return generation[maxindex];
- }
- int main()
- {
- }
- // Запуск программы: CTRL+F5 или меню "Отладка" > "Запуск без отладки"
- // Отладка программы: F5 или меню "Отладка" > "Запустить отладку"
- // Советы по началу работы
- // 1. В окне обозревателя решений можно добавлять файлы и управлять ими.
- // 2. В окне Team Explorer можно подключиться к системе управления версиями.
- // 3. В окне "Выходные данные" можно просматривать выходные данные сборки и другие сообщения.
- // 4. В окне "Список ошибок" можно просматривать ошибки.
- // 5. Последовательно выберите пункты меню "Проект" > "Добавить новый элемент", чтобы создать файлы кода, или "Проект" > "Добавить существующий элемент", чтобы добавить в проект существующие файлы кода.
- // 6. Чтобы снова открыть этот проект позже, выберите пункты меню "Файл" > "Открыть" > "Проект" и выберите SLN-файл.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement