Sie sind auf Seite 1von 19

ANT COLONY

OPTIMIZATION
Introduction
• The ant colony algorithm is an algorithm for finding optimal paths
that is based on the behavior of ants searching for food.
• Because the ants drop pheromones every time they bring food,
shorter paths are more likely to be stronger, hence optimizing the
"solution.“
• In the meantime, some ants are still randomly scouting for closer
food sources.
• A similar approach can be used find near-optimal solution to
the traveling salesman problem.
Working of ACO:-
CODE:-
#include <iostream> struct antType{
#include <math.h>
#include <stdlib.h> int curCity, nextCity, pathIndex;
#include <fstream> int tabu[MAX_CITIES];
#include <ctime> int path[MAX_CITIES];
#include "assert.h" double tourLength;
using namespace std; };

//parameters required for the Ant Colony //the parameters by which the algorithm will
Algorithms. converge to a solution.
#define MAX_CITIES 5
#define MAX_DIST 15 #define ALPHA 1.0
#define MAX_TOUR (MAX_CITIES * #define BETA 5.0 //This parameter raises the weight
MAX_DIST) of distance over pheromone
#define MAX_ANTS 5 #define RHO 0.5 //Evapouration rate
//the environment fro the Traveling #define QVAL 100
Salesman Problem (TSP). #define MAX_TOURS 20
//Initial Definiton of the problem #define MAX_TIME (MAX_TOURS * MAX_CITIES)
struct cityType #define INIT_PHER (1.0/MAX_CITIES)
{ //weigh the edges with pheromones with the value
int x,y; of (1/MAX_CITIES).
}; //runtime Structures and global variables
cityType cities[MAX_CITIES]; for(to=0;to<MAX_CITIES;to++)
antType ants[MAX_ANTS]; {
dist[from][to] = 0.0;
double dist[MAX_CITIES][MAX_CITIES]; phero[from][to] = INIT_PHER;
}
double phero[MAX_CITIES][MAX_CITIES]; }

double best=(double)MAX_TOUR; //computing distance


int bestIndex;
void init() for(from = 0; from < MAX_CITIES; from++)
{ {
int from,to,ant; for( to =0; to < MAX_CITIES; to++)
{
//creating cities if(to!=from && dist[from][to]==0.0)
{
for(from = 0; from < MAX_CITIES; from++) int xd = pow( abs(cities[from].x - cities[to].x), 2);
{ int yd = pow( abs(cities[from].y - cities[to].y), 2);
//randomly place cities dist[from][to] = sqrt(xd + yd);
dist[to][from] = dist[from][to];
cities[from].x = rand()%MAX_DIST;
}
cities[from].y = rand()%MAX_DIST; }
//printf("\n %d %d",cities[from].x, cities[from].y); }
}
//initializing the ANTs
//reinitialize all ants and redistribute them
to = 0;
void restartAnts()
for( ant = 0; ant < MAX_ANTS; ant++)
{
{
int ant,i,to=0;
if(to == MAX_CITIES)
to=0;
for(ant = 0; ant<MAX_ANTS; ant++)
{
ants[ant].curCity = to++;
if(ants[ant].tourLength < best)
{
for(from = 0; from < MAX_CITIES; from++)
best = ants[ant].tourLength;
{
bestIndex = ant;
ants[ant].tabu[from] = 0;
}
ants[ant].path[from] = -1;
}
ants[ant].nextCity = -1;
ants[ant].pathIndex = 1;
ants[ant].tourLength = 0.0;
ants[ant].path[0] = ants[ant].curCity;
for(i=0;i<MAX_CITIES;i++)
ants[ant].nextCity = -1;
{
ants[ant].tourLength = 0;
ants[ant].tabu[i] = 0;
ants[ant].path[i] = -1;
//loading first city into tabu list
}
ants[ant].tabu[ants[ant].curCity] =1;
if(to == MAX_CITIES)
to=0;
}
ants[ant].curCity = to++; denom += antProduct( from, to );
}
ants[ant].pathIndex = 1; }
ants[ant].path[0] = ants[ant].curCity; assert(denom != 0.0);
do
ants[ant].tabu[ants[ant].curCity] = 1; { double p;
} to++;
} if(to >= MAX_CITIES)
double antProduct(int from, int to) to=0;
{ if(ants[ant].tabu[to] == 0)
return(( pow( phero[from][to], ALPHA) * pow( {
(1.0/ dist[from][to]), BETA))); p = antProduct(from,to)/denom;
} //printf("\n%lf %lf", (double)rand()/RAND_MAX,p);
int selectNextCity( int ant ) double x = ((double)rand()/RAND_MAX);
{ if(x < p)
int from, to; {
double denom = 0.0; //printf("%lf %lf Yo!",p,x);

from=ants[ant].curCity; break;
}
for(to=0;to<MAX_CITIES;to++) }
{ }while(1);
if(ants[ant].tabu[to] == 0)
{ return to;
} }
int simulateAnts() }
{
int k; return moving;
int moving = 0; }

for(k=0; k<MAX_ANTS; k++) //Updating trails


{
//checking if there are any more cities to visit void updateTrails()
{
if( ants[k].pathIndex < MAX_CITIES ) { int from,to,i,ant;
ants[k].nextCity = selectNextCity(k);
ants[k].tabu[ants[k].nextCity] = 1; //Pheromone Evaporation
ants[k].path[ants[k].pathIndex++] = ants[k].nextCity;
ants[k].tourLength += dist[ants[k].curCity][ants[k].nextCity]; for(from=0; from<MAX_CITIES;from++)
//handle last case->last city to first {
for(to=0;to<MAX_CITIES;to++)
if(ants[k].pathIndex == MAX_CITIES) {
{ if(from!=to)
ants[k].tourLength += {
dist[ants[k].path[MAX_CITIES -1]][ants[k].path[0]]; phero[from][to] *=( 1.0 - RHO);
}
ants[k].curCity = ants[k].nextCity; if(phero[from][to]<0.0)
moving++; {
phero[from][to] = INIT_PHER;
} }
} }
}
} for (from=0; from < MAX_CITIES;from++)
{
//Add new pheromone to the trails for( to=0; to<MAX_CITIES; to++)
{
for(ant=0;ant<MAX_ANTS;ant++) phero[from][to] *= RHO;
{ }
for(i=0;i<MAX_CITIES;i++) }
{
if( i < MAX_CITIES-1 ) }
{ void emitDataFile(int bestIndex)
from = ants[ant].path[i]; {
to = ants[ant].path[i+1]; char text;
} fstream f1;
else f1.open("Data.txt", ios::out|ios::in);
{ f1>>text;
from = ants[ant].path[i]; antType antBest;
to = ants[ant].path[0]; antBest = ants[bestIndex];
} //f1<<antBest.curCity<<" "<<antBest.tourLength<<"\n";
phero[from][to] += (QVAL/ ants[ant].tourLength); int i;
phero[to][from] = phero[from][to]; for(i=0;i<MAX_CITIES;i++)
{ {
f1<<antBest.path[i]<<" "; if( simulateAnts() == 0)
} {
updateTrails();
f1.close();
if(curTime != MAX_TIME)
f1.open("city_data.txt"); restartAnts();
for(i=0;i<MAX_CITIES;i++)
{ }
f1<<cities[i].x<<" "<<cities[i].y<<"\n"; }
} cout<<"\nThe number of iterations is
cout<<text<<"\t"; "<<curTime<<"("<<best<<")";
f1.close(); cout<<"\nBest tour = "<<best<<endl;
}
int main() emitDataFile(bestIndex);
{
int curTime = 0; return 0;
cout<<"MaxTime="<<MAX_TIME; }
srand(time(NULL));

init();

while( curTime++ < MAX_TIME)


{
OUTPUT
Implementation
load('usborder.mat','x','y','xx','yy');
rng(3,'twister')
plot(x,y,'Color','green');
nStops = 200;
stopsLon = zeros(nStops,1); hold on
plot(stopsLon,stopsLat,'*b')
stopsLat = stopsLon;
n = 1; hold off
while (n <= nStops)
xp = rand*1.5; idxs = nchoosek(1:nStops,2);
dist = hypot(stopsLat(idxs(:,1)) - stopsLat(idxs(:,2)), ...
yp = rand;
if inpolygon(xp,yp,x,y) stopsLon(idxs(:,1)) - stopsLon(idxs(:,2))); lendist = length(dist);
stopsLon(n) = xp; tsp = optimproblem;
stopsLat(n) = yp;
n = n+1;
end
end
trips =

optimvar('trips',lendist,1,'Type','integer','LowerBound',0,'UpperBo

und',1);

tsp.Objective = dist'*trips; constrips = sum(trips) == nStops; tsp.Constraints.constrips = constrips;


constr2trips = optimconstr(nStops,1); for stops = 1:nStops

whichIdxs = (idxs == stops); whichIdxs = any(whichIdxs,2); constr2trips(stops) =


sum(trips(whichIdxs)) == 2;

end

tsp.Constraints.constr2trips = constr2trips; hold on