📄 ants.c
字号:
/* AAAA CCCC OOOO TTTTTT SSSSS PPPPP AA AA CC OO OO TT SS PP PP AAAAAA CC OO OO TT SSSS PPPPP AA AA CC OO OO TT SS PP AA AA CCCC OOOO TT SSSSS PP################################################################ ACO algorithms for the TSP ################################################################ Version: 1.0 File: ants.c Author: Thomas Stuetzle Purpose: implementation of procedures for ants' behaviour Check: README.txt and legal.txt Copyright (C) 2002 Thomas Stuetzle*//*************************************************************************** Program's name: acotsp Ant Colony Optimization algorithms (AS, ACS, EAS, RAS, MMAS, BWAS) for the symmetric TSP Copyright (C) 2004 Thomas Stuetzle 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 email: stuetzle no@spam informatik.tu-darmstadt.de mail address: Universitaet Darmstadt Fachbereich Informatik Hochschulstr. 10 D-64283 Darmstadt Germany***************************************************************************/#include <stdio.h>#include <stdlib.h>#include <math.h>#include <assert.h>#include <limits.h>#include <time.h>#include "InOut.h"#include "TSP.h"#include "ants.h"#include "ls.h"#include "utilities.h"#include "timer.h"ant_struct *ant;ant_struct *best_so_far_ant;ant_struct *restart_best_ant;double **pheromone;double **total;double *prob_of_selection;long int n_ants; /* number of ants */long int nn_ants; /* length of nearest neighbor lists for the ants' solution construction */double rho; /* parameter for evaporation */double alpha; /* importance of trail */double beta; /* importance of heuristic evaluate */double q_0; /* probability of best choice in tour construction */long int as_flag; /* ant system */long int eas_flag; /* elitist ant system */long int ras_flag; /* rank-based version of ant system */long int mmas_flag; /* MAX-MIN ant system */long int bwas_flag; /* best-worst ant system */long int acs_flag; /* ant colony system */long int elitist_ants; /* additional parameter for elitist ant system, no. elitist ants */long int ras_ranks; /* additional parameter for rank-based version of ant system */double trail_max; /* maximum pheromone trail in MMAS */double trail_min; /* minimum pheromone trail in MMAS */long int u_gb; /* every u_gb iterations update with best-so-far ant */double trail_0; /* initial pheromone level in ACS and BWAS */void allocate_ants ( void )/* FUNCTION: allocate the memory for the ant colony, the best-so-far and the iteration best ant INPUT: none OUTPUT: none (SIDE)EFFECTS: allocation of memory for the ant colony and two ants that store intermediate tours*/{ long int i; if((ant = malloc(sizeof( ant_struct ) * n_ants + sizeof(ant_struct *) * n_ants )) == NULL){ printf("Out of memory, exit."); exit(1); } for ( i = 0 ; i < n_ants ; i++ ) { ant[i].tour = calloc(n+1, sizeof(long int)); ant[i].visited = calloc(n, sizeof(char)); } if((best_so_far_ant = malloc(sizeof( ant_struct ) )) == NULL){ printf("Out of memory, exit."); exit(1); } for ( i = 0 ; i < n_ants ; i++ ) { (*best_so_far_ant).tour = calloc(n+1, sizeof(long int)); (*best_so_far_ant).visited = calloc(n, sizeof(char)); } if((restart_best_ant = malloc(sizeof( ant_struct ) )) == NULL){ printf("Out of memory, exit."); exit(1); } for ( i = 0 ; i < n_ants ; i++ ) { (*restart_best_ant).tour = calloc(n+1, sizeof(long int)); (*restart_best_ant).visited = calloc(n, sizeof(char)); } if((prob_of_selection = malloc(sizeof( double ) * nn_ants )) == NULL){ printf("Out of memory, exit."); exit(1); }}long int find_best( void ) /* FUNCTION: find the best ant of the current iteration INPUT: none OUTPUT: index of struct containing the iteration best ant (SIDE)EFFECTS: none*/{ long int min; long int k, k_min; min = ant[0].tour_length; k_min = 0; for( k = 1 ; k < n_ants ; k++ ) { if( ant[k].tour_length < min ) { min = ant[k].tour_length; k_min = k; } } return k_min;}long int find_worst( void ) /* FUNCTION: find the worst ant of the current iteration INPUT: none OUTPUT: pointer to struct containing iteration best ant (SIDE)EFFECTS: none*/{ long int max; long int k, k_max; max = ant[0].tour_length; k_max = 0; for( k = 1 ; k < n_ants ; k++ ) { if( ant[k].tour_length > max ) { max = ant[k].tour_length; k_max = k; } } return k_max;}/************************************************************ ************************************************************Procedures for pheromone manipulation ************************************************************ ************************************************************/void init_pheromone_trails( double initial_trail )/* FUNCTION: initialize pheromone trails INPUT: initial value of pheromone trails "initial_trail" OUTPUT: none (SIDE)EFFECTS: pheromone matrix is reinitialized*/{ long int i, j; TRACE ( printf(" init trails with %.15f\n",initial_trail); ); /* Initialize pheromone trails */ for ( i = 0 ; i < n ; i++ ) { for ( j =0 ; j <= i ; j++ ) { pheromone[i][j] = initial_trail; pheromone[j][i] = initial_trail; total[i][j] = initial_trail; total[j][i] = initial_trail; } }}void evaporation( void )/* FUNCTION: implements the pheromone trail evaporation INPUT: none OUTPUT: none (SIDE)EFFECTS: pheromones are reduced by factor rho*/{ long int i, j; TRACE ( printf("pheromone evaporation\n"); ); for ( i = 0 ; i < n ; i++ ) { for ( j = 0 ; j <= i ; j++ ) { pheromone[i][j] = (1 - rho) * pheromone[i][j]; pheromone[j][i] = pheromone[i][j]; } }}void evaporation_nn_list( void )/* FUNCTION: simulation of the pheromone trail evaporation INPUT: none OUTPUT: none (SIDE)EFFECTS: pheromones are reduced by factor rho REMARKS: if local search is used, this evaporation procedure only considers links between a city and those cities of its candidate list*/{ long int i, j, help_city; TRACE ( printf("pheromone evaporation nn_list\n"); ); for ( i = 0 ; i < n ; i++ ) { for ( j = 0 ; j < nn_ants ; j++ ) { help_city = instance.nn_list[i][j]; pheromone[i][help_city] = (1 - rho) * pheromone[i][help_city]; } }}void global_update_pheromone( ant_struct *a )/* FUNCTION: reinforces edges used in ant k's solution INPUT: pointer to ant that updates the pheromone trail OUTPUT: none (SIDE)EFFECTS: pheromones of arcs in ant k's tour are increased*/{ long int i, j, h; double d_tau; TRACE ( printf("global pheromone update\n"); ); d_tau = 1.0 / (double) (*a).tour_length; for ( i = 0 ; i < n ; i++ ) { j = (*a).tour[i]; h = (*a).tour[i+1]; pheromone[j][h] += d_tau; pheromone[h][j] = pheromone[j][h]; }}void global_update_pheromone_weighted( ant_struct *a, long int weight )/* FUNCTION: reinforces edges of the ant's tour with weight "weight" INPUT: pointer to ant that updates pheromones and its weight OUTPUT: none (SIDE)EFFECTS: pheromones of arcs in the ant's tour are increased*/{ long int i, j, h; double d_tau; TRACE ( printf("global pheromone update weighted\n"); ); d_tau = (double) weight / (double) (*a).tour_length; for ( i = 0 ; i < n ; i++ ) { j = (*a).tour[i]; h = (*a).tour[i+1]; pheromone[j][h] += d_tau; pheromone[h][j] = pheromone[j][h]; } }void compute_total_information( void )/* FUNCTION: calculates heuristic info times pheromone for each arc INPUT: none OUTPUT: none*/{ long int i, j; TRACE ( printf("compute total information\n"); ); for ( i = 0 ; i < n ; i++ ) { for ( j = 0 ; j < i ; j++ ) { total[i][j] = pow(pheromone[i][j],alpha) * pow(HEURISTIC(i,j),beta); total[j][i] = total[i][j]; } }}void compute_nn_list_total_information( void )/* FUNCTION: calculates heuristic info times pheromone for arcs in nn_list INPUT: none OUTPUT: none*/{ long int i, j, h; TRACE ( printf("compute total information nn_list\n"); ); for ( i = 0 ; i < n ; i++ ) { for ( j = 0 ; j < nn_ants ; j++ ) { h = instance.nn_list[i][j]; if ( pheromone[i][h] < pheromone[h][i] ) /* force pheromone trails to be symmetric as much as possible */ pheromone[h][i] = pheromone[i][h]; total[i][h] = pow(pheromone[i][h], alpha) * pow(HEURISTIC(i,h),beta); total[h][i] = total[i][h]; } }}/**************************************************************** ****************************************************************Procedures implementing solution construction and related things **************************************************************** ****************************************************************/void ant_empty_memory( ant_struct *a ) /* FUNCTION: empty the ants's memory regarding visited cities INPUT: ant identifier OUTPUT: none (SIDE)EFFECTS: vector of visited cities is reinitialized to FALSE*/{ long int i; for( i = 0 ; i < n ; i++ ) { (*a).visited[i]=FALSE; }}void place_ant( ant_struct *a , long int step )/* FUNCTION: place an ant on a randomly chosen initial city INPUT: pointer to ant and the number of construction steps OUTPUT: none (SIDE)EFFECT: ant is put on the chosen city*/{ long int rnd; rnd = (long int) (ran01( &seed ) * (double) n); /* random number between 0 .. n-1 */ (*a).tour[step] = rnd; (*a).visited[rnd] = TRUE;}void choose_best_next( ant_struct *a, long int phase )/* FUNCTION: chooses for an ant as the next city the one with maximal value of heuristic information times pheromone INPUT: pointer to ant and the construction step OUTPUT: none (SIDE)EFFECT: ant moves to the chosen city*/{ long int city, current_city, next_city; double value_best; next_city = n; DEBUG( assert ( phase > 0 && phase < n ); ); current_city = (*a).tour[phase-1]; value_best = -1.; /* values in total matrix are always >= 0.0 */ for ( city = 0 ; city < n ; city++ ) { if ( (*a).visited[city] ) ; /* city already visited, do nothing */ else { if ( total[current_city][city] > value_best ) { next_city = city; value_best = total[current_city][city]; } } } DEBUG( assert ( 0 <= next_city && next_city < n); ); DEBUG( assert ( value_best > 0.0 ); ) DEBUG( assert ( (*a).visited[next_city] == FALSE ); ) (*a).tour[phase] = next_city; (*a).visited[next_city] = TRUE;}void neighbour_choose_best_next( ant_struct *a, long int phase )/* FUNCTION: chooses for an ant as the next city the one with maximal value of heuristic information times pheromone INPUT: pointer to ant and the construction step "phase" OUTPUT: none (SIDE)EFFECT: ant moves to the chosen city*/{ long int i, current_city, next_city, help_city; double value_best, help; next_city = n; DEBUG( assert ( phase > 0 && phase < n ); ); current_city = (*a).tour[phase-1]; DEBUG ( assert ( 0 <= current_city && current_city < n ); ) value_best = -1.; /* values in total matix are always >= 0.0 */ for ( i = 0 ; i < nn_ants ; i++ ) { help_city = instance.nn_list[current_city][i]; if ( (*a).visited[help_city] ) ; /* city already visited, do nothing */ else { help = total[current_city][help_city]; if ( help > value_best ) { value_best = help; next_city = help_city; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -