亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關于我們
? 蟲蟲下載站

?? antsystemtsp.cpp

?? 這是用Ant System 解決tsp問題的C++源碼 希望對大家研究螞蟻算法有所幫助
?? CPP
字號:
/*  >->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->
     AntSystemTSP.cc, Heiko Stamer <stamer@informatik.uni-leipzig.de>

       Ant System (AS) for the Traveling Salesman Problem (TSP)
	   
         [The Ant System: Optimization by a colony of cooperating agents]
                  by M. Dorigo, V. Maniezzo, A. Colorni
    IEEE Transactions on Systems, Man and Cybernetics - Part B, Vol.26-1 1996
	
            http://stinfwww.informatik.uni-leipzig.de/~mai97ixb
    >->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->

  Copyright (C) 2000-until_the_end_of_the_world  <Heiko Stamer>

    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., 675 Mass Ave, Cambridge, MA 02139, USA.                 */
	
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <process.h>
//#include <unistd.h>
#include <time.h>

#define N 70
double C[N][2] = { {64 , 96} , {80 , 39} , {69 , 23} , {72 , 42} , {48 , 67} , {58 ,
43} , {81 , 34} , {79 , 17} , {30 , 23} , {42 , 67} , {7 , 76} , {29 , 51} , {78
, 92} , {64 , 8} , {95 , 57} , {57 , 91} , {40 , 35} , {68 , 40} , {92 , 34} ,
{62 , 1} , {28 , 43} , {76 , 73} , {67 , 88} , {93 , 54} , {6 , 8} , {87 , 18} ,
{30 , 9} , {77 , 13} , {78 , 94} , {55 , 3} , {82 , 88} , {73 , 28} , {20 , 55}
, {27 , 43} , {95 , 86} , {67 , 99} , {48 , 83} , {75 , 81} , {8 , 19} , {20 ,
18} , {54 , 38} , {63 , 36} , {44 , 33} , {52 , 18} , {12 , 13} , {25 , 5} , {58
, 85} , {5 , 67} , {90 , 9} , {41 , 76} , {25 , 76} , {37 , 64} , {56 , 63} ,
{10 , 55} , {98 , 7} , {16 , 74} , {89 , 60} , {48 , 82} , {81 , 76} , {29 , 60}
, {17 , 22} , {5 , 45} , {79 , 70} , {9 , 100} , {17 , 82} , {74 , 67} , {10 ,
68} , {48 , 19} , {83 , 86} , {84 , 94} };

typedef int Tour[N][2];
typedef double doubleMatrix[N][N];

doubleMatrix D;

double dist(int i, int j)
{
	return (sqrt(pow((C[i][0]-C[j][0]), 2.0) + pow((C[i][1]-C[j][1]), 2.0)));
}

void calc_dist()
{
	for (int i = 0; i < N; i++)
		for (int j = 0; j < N; j++)
			D[i][j] = dist(i, j);
}

double calc_length(Tour tour)
{
	double l = 0.0;
	for (int n = 0; n < N; n++)
	{
		int i = tour[n][0];
		int j = tour[n][1];
		l += D[i][j];
	}
	return (l);
}

void print_tour(Tour tour)
{
	for (int n = 0; n < N; n++)
		printf("( %d , %d ) ", tour[n][0], tour[n][1]);
	printf("\n");
}

int sum_sequence(int array[], int count)
{
	int sum = 0;
	for (int i = 0; i < count; i++)
		sum += array[i];
	return (sum);
}

class AntSystem;
class Ant
{
	private:
		AntSystem *AS;
		int START_CITY, CURRENT_CITY;
		int ALLOWED[N];
		Tour CURRENT_TOUR;
		int CURRENT_TOUR_INDEX;
		
	public:	
        Ant(AntSystem *as, int start_city);		
		inline void moveTo(int to_city);
		inline int choose();
		inline Tour *search();
};

class AntSystem
{
    private:
		double ALPHA, BETA, RHO, Q;
		doubleMatrix TAU, dTAU;
		int ACTIVE[N][N];
		static const int M = 6 * N;
		Ant *ANTS[M];
		
	public:	
        AntSystem(double alpha, double beta, double rho, double q);
		inline void init_tau_by_value(double value);
		inline void init_tau_by_matrix(doubleMatrix matrix);
		inline void	init_uniform();
		inline void	init_random();
		inline double ETA(int i, int j);
		inline double transition(int i, int j);
		inline double sum_transition(int i, int allowed[]);
		inline void	clear_update_transitions();
		inline int check_active(int what);
		inline void add_update_transitions(Tour tour, double length);
		inline void update_transitions();
		inline doubleMatrix *get_tau();
		inline Tour *search(int T);
};

Ant::Ant(AntSystem *as, int start_city)
{
	AS = as;
	START_CITY = start_city;
}
		
inline void Ant::moveTo(int to_city)
{
	ALLOWED[to_city] = 0;
	CURRENT_TOUR[CURRENT_TOUR_INDEX][0] = CURRENT_CITY;
	CURRENT_TOUR[CURRENT_TOUR_INDEX][1] = to_city;
	CURRENT_TOUR_INDEX++;
	CURRENT_CITY = to_city;
}
		
inline int Ant::choose()
{
	double sum = AS->sum_transition(CURRENT_CITY, ALLOWED);
	double p = (double)rand() / RAND_MAX;
	double p_j = 0.0;
			
	for (int j = 0; j < N; j++)
	{
		if (ALLOWED[j] == 1) p_j += AS->transition(CURRENT_CITY, j) / sum;
		if ((p < p_j) && (ALLOWED[j] == 1))
			return (j);
	}
	return (-1);
}
		
inline Tour *Ant::search()
{
	CURRENT_CITY = START_CITY;
	CURRENT_TOUR_INDEX = 0;
	for (int i = 0; i < N; i++)
		ALLOWED[i] = 1;
	ALLOWED[CURRENT_CITY] = 0;
	while (sum_sequence(ALLOWED, N) > 0)
		moveTo(choose());
	ALLOWED[START_CITY] = 1;
	moveTo(START_CITY);
	return (&CURRENT_TOUR);
}

AntSystem::AntSystem(double alpha, double beta, double rho, double q)
{
	ALPHA = alpha;
	BETA = beta;
	RHO = rho;
	Q = q;
}

inline void AntSystem::init_tau_by_value(double value)	
{
	for (int i = 0; i < N; i++)
		for (int j = 0; j < N; j++)
			TAU[i][j] = value;
}

inline void AntSystem::init_tau_by_matrix(doubleMatrix matrix)	
{
	for (int i = 0; i < N; i++)
		for (int j = 0; j < N; j++)
			TAU[i][j] = matrix[i][j];
}

inline void	AntSystem::init_uniform()
{
	// uniformly distributed
	for (int k = 0; k < M; k++)
		ANTS[k] = new Ant(this, (k % N));
}

inline void	AntSystem::init_random()
{	
	// randomly distributed
	for (int k = 0; k < M; k++)
		ANTS[k] = new Ant(this, (int)(N * (rand() / RAND_MAX)));
}

inline double AntSystem::ETA(int i, int j)
{
	return ( 1.0 / D[i][j] );
}
		
inline double AntSystem::transition(int i, int j)	
{
	if (i != j)
		return ( pow( TAU[i][j], ALPHA ) * pow( ETA(i, j), BETA ) );
	else
		return(0.0);
}
	
inline double AntSystem::sum_transition(int i, int allowed[])
{
	double sum = 0.0;
	for (int j = 0; j < N; j++) 
		sum += ((double)allowed[j] * transition(i, j));
	return (sum);
}
	
inline void	AntSystem::clear_update_transitions()
{
	for (int i = 0; i < N; i++)
		for (int j = 0; j < N; j++)
		{
			dTAU[i][j] = 0.0;
			ACTIVE[i][j] = 0;
		}
}
				
inline int AntSystem::check_active(int what)
{
	int	sum = 0;
	for (int i = 0; i < N; i++)
	{	
		sum += sum_sequence(ACTIVE[i], N);
		if (sum > what)
			return (1);
	}
	return (0);
}

inline void AntSystem::add_update_transitions(Tour tour, double length)
{
	for (int n = 0; n < N; n++)
	{
		int i = tour[n][0];
		int j = tour[n][1];
		// symmetric TSP
		dTAU[i][j] += (Q / length);
		dTAU[j][i] += (Q / length);
		ACTIVE[i][j] = 1;
		ACTIVE[j][i] = 1;
	}
}

inline void AntSystem::update_transitions()
{
	for (int i = 0; i < N; i++)
		for (int j = 0; j < N; j++)
			TAU[i][j] = RHO * TAU[i][j] + dTAU[i][j];
}

inline doubleMatrix *AntSystem::get_tau()
{
	return (&TAU);
}
	
inline Tour *AntSystem::search(int T)
{
	Tour best_tour, tour;
	double best_length = 1000000.0, tour_length;
	int	no_action_runs = 0;
	int	max_no_action_runs = 10;

	// do T iterations of ant-cycle algorithm
	int t;
	for (t = 0; t < T; t++)
	{
		clear_update_transitions();
		for (int k = 0; k < M; k++)
		{
			tour = *(ANTS[k]->search());
			tour_length = calc_length(tour);
			if (tour_length < best_length)
			{
				best_tour = tour;
				best_length = tour_length;
				//printf("%f \n", tour_length);
				//print "[%s/%s] best tour (length = %s): %s"\
				//% (t + 1, T, best_length, best_tour)
			}
			add_update_transitions(tour, tour_length);
		}
		update_transitions();
		
		// checking for stagnation behaviour
		if (!(check_active(2 * N)))
			no_action_runs += 1;
		if (no_action_runs > max_no_action_runs)
			break;
	}
		
	//printf("[%d/%d] best tour (length = %f):\n", t, T, best_length);
	//print_tour(best_tour);
	//printf("[%d/%d] iterations done\n", t, T);
	printf("%f\n", best_length);
	return (&best_tour);
}

int main(int argc, char* argv[])
{
	// PRNG initalisieren
	time_t timer;
	time(&timer);
	pid_t pid = getpid() + getppid();	
	unsigned long seed = (timer * pid);
	if (seed == 0) 
	{
	    time(&timer);
	    seed = 7 * timer * pid;
	    if (seed == 0) seed = pid; else seed = seed % 56000;
	} else seed = seed % 56000;
	srand((unsigned int)seed);
	
	// EUC2D
	calc_dist();
	
	// Ant System		
	AntSystem *as = new AntSystem(1.0, 5.0, 0.5, 100.0);
	as->init_tau_by_value(0.01);
	as->init_uniform();
	as->search(100);
	
	return(0);
}

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
久久久久久久久久久久久夜| 亚洲欧美日韩国产另类专区| 国产日韩亚洲欧美综合| 亚洲资源中文字幕| 国产一区美女在线| 欧美视频你懂的| 亚洲欧洲日韩女同| 国产精品99久久久久久有的能看| 欧美综合久久久| 国产精品丝袜91| 国产乱妇无码大片在线观看| 欧美日韩国产综合视频在线观看| 国产精品麻豆久久久| 激情五月播播久久久精品| 欧美伦理影视网| 亚洲最快最全在线视频| 色综合久久久久网| 中文一区二区在线观看| 韩国精品久久久| 精品免费一区二区三区| 免费成人美女在线观看.| 欧美日韩mp4| 亚洲国产aⅴ天堂久久| 色婷婷综合久久久中文字幕| 国产精品国产精品国产专区不片 | 日韩三级免费观看| 亚洲国产精品一区二区www | 欧美丝袜第三区| 18欧美亚洲精品| av不卡在线播放| 国产精品少妇自拍| 波多野结衣一区二区三区| 国产精品日韩精品欧美在线| 国产福利一区二区三区在线视频| 久久久蜜桃精品| 国产精一品亚洲二区在线视频| 日韩精品一区二区三区视频播放 | 成人性生交大片免费看在线播放| 久久天天做天天爱综合色| 久久精品国产精品亚洲红杏 | 国产在线国偷精品免费看| 亚洲精品一区二区三区在线观看 | 成人av在线网| 亚洲精品国产精品乱码不99 | 成人激情黄色小说| 亚洲日本在线视频观看| 91国产精品成人| 午夜av电影一区| 精品国产91洋老外米糕| 岛国av在线一区| 一区二区视频免费在线观看| 8v天堂国产在线一区二区| 韩国女主播一区二区三区| 国产精品毛片久久久久久| 色狠狠桃花综合| 麻豆一区二区三| 中文乱码免费一区二区| 欧美伊人久久久久久久久影院 | 91久久精品一区二区| 婷婷开心激情综合| 久久影院午夜片一区| 99九九99九九九视频精品| 亚洲国产aⅴ成人精品无吗| 日韩精品一区二区三区蜜臀| 高清不卡一区二区| 天堂蜜桃一区二区三区| 国产午夜精品久久久久久免费视| 色成年激情久久综合| 蜜臀久久99精品久久久画质超高清 | 一区二区三区中文在线观看| 日韩欧美一二区| 97国产一区二区| 精品在线免费视频| 亚洲自拍偷拍九九九| 精品久久人人做人人爽| 欧美综合在线视频| 成人精品免费视频| www.亚洲色图.com| 日韩精品一级二级 | 丁香六月久久综合狠狠色| 五月天激情小说综合| 欧美极品aⅴ影院| 91精品国产欧美一区二区成人 | 久久电影网站中文字幕| 亚洲三级在线免费观看| 久久久久国产一区二区三区四区 | 99精品国产91久久久久久| 麻豆国产精品一区二区三区 | 在线亚洲一区二区| 国产成a人亚洲精| 麻豆极品一区二区三区| 亚洲高清在线视频| 亚洲精品国产一区二区精华液| 国产欧美日韩在线| 久久亚洲精精品中文字幕早川悠里 | 国产美女一区二区三区| 日韩av网站免费在线| 亚洲五码中文字幕| 亚洲美女视频一区| 国产精品免费丝袜| 国产午夜亚洲精品午夜鲁丝片| 欧美一区二区三区的| 欧美日韩激情一区二区三区| 色综合久久久久网| 色婷婷综合久久久中文字幕| 99久久精品国产毛片| 白白色 亚洲乱淫| 国产乱子伦视频一区二区三区| 日韩成人dvd| 日韩二区在线观看| 人妖欧美一区二区| 日本aⅴ免费视频一区二区三区| 日日嗨av一区二区三区四区| 午夜免费久久看| 日韩av中文在线观看| 免费观看在线色综合| 九九**精品视频免费播放| 精品一区二区三区香蕉蜜桃| 精品系列免费在线观看| 国产在线播精品第三| 国产精品一线二线三线| 国产iv一区二区三区| 99久久精品国产网站| 在线观看亚洲精品| 在线不卡一区二区| 日韩欧美一区二区免费| 久久久久综合网| 国产精品久久久久影院亚瑟| 亚洲色图19p| 天堂va蜜桃一区二区三区漫画版| 日本美女视频一区二区| 国产一区二区三区观看| 懂色av一区二区在线播放| 91日韩在线专区| 欧美二区乱c少妇| 久久亚洲一级片| 亚洲色大成网站www久久九九| 亚洲地区一二三色| 久久国产人妖系列| 成人h版在线观看| 欧美日韩一区二区三区四区 | 欧美亚洲国产bt| 91精品国产一区二区三区蜜臀 | 欧美肥大bbwbbw高潮| 久久精品一区蜜桃臀影院| 欧美高清在线一区二区| 亚洲一区二区三区爽爽爽爽爽| 奇米精品一区二区三区在线观看 | 精品国产免费一区二区三区四区| 国产亚洲一区字幕| 一区二区理论电影在线观看| 日本中文字幕一区二区有限公司| 日本va欧美va欧美va精品| 成人精品视频.| 日韩亚洲国产中文字幕欧美| 中文av一区二区| 日本不卡123| 94色蜜桃网一区二区三区| 5858s免费视频成人| 中文字幕一区二区三区av| 日韩成人免费看| 一本大道av伊人久久综合| 久久综合色综合88| 亚洲成人激情社区| 成人黄色免费短视频| 日韩欧美国产一区二区三区| 亚洲人成电影网站色mp4| 久久精品国产久精国产| 欧美日韩激情一区| 日韩伦理电影网| 东方aⅴ免费观看久久av| 日韩视频一区在线观看| 亚洲一线二线三线久久久| 粉嫩av一区二区三区粉嫩| 欧美va在线播放| 日欧美一区二区| 欧美日韩国产大片| 一区二区高清在线| 国产成人久久精品77777最新版本 国产成人鲁色资源国产91色综 | 91女厕偷拍女厕偷拍高清| 久久久久久久久久美女| 蜜臀av亚洲一区中文字幕| 欧美在线不卡视频| 一区二区在线看| a在线播放不卡| 国产欧美日韩三区| 国产一区二区三区四区五区美女| 欧美一级电影网站| 日韩国产精品大片| 欧美日韩在线播放三区四区| 一区二区不卡在线播放| 91视频一区二区| 最近日韩中文字幕| 99久久99久久综合| 国产一区二区网址| 精品入口麻豆88视频| 精一区二区三区| 久久精品一区二区三区四区| 国产做a爰片久久毛片| 久久久综合网站|