en otra ventana

Anuncio
Apéndice B
Código Rutas Cortas
#i n c l u d e
#i n c l u d e
#i n c l u d e
#i n c l u d e
#i n c l u d e
#i n c l u d e
< io st r ea m >
< f st r ea m >
<string >
<vector >
< a lg o r it hm >
” MersenneTwister . h”
#d e f i n e PI 3 . 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4 6 2 6 4 3 3 8 3 2 8
#d e f i n e MAX 1 0 0 0
#d e f i n e Obj 2
#d e f i n e x e x c e l ” . out ”
#d e f i n e x l a t e x ” . t e x ”
#d e f i n e x t e x t ” . t x t ”
u s i n g namespace s t d ;
/ / S e l e c c i o n a s i e s e s t o c ’ a s t i c o o determin ’ i s t i c o
bool s t o c h a s t i c = true ;
/ / Par ’ ametros
i nt Generations = 100 ,
PopCount = 2 0 ,
RunCount = 1 0 ;
do uble MutationRate = 0 . 3 ,
Cr o sso ver R a t e = 0 . 5 ;
/ / Generador de n ’ umeros a l e a t o r i o s
MTRand random ;
/ / Ar chivo s de e n t r a d a y s a l i d a
ofstream oexcel ;
ofstream olatex ;
ifstream in ;
i f s t r e a m iparams ;
t y p e d e f v e c t o r <i n t > e d g e s ;
t y p e d e f v e c t o r <edges > graph ;
60
APÉNDICE B. CÓDIGO RUTAS CORTAS
61
do uble Co st o s [ Obj ] ;
st r u ct Tcosts{
do uble f i j o ;
cha r f u n c i o n ;
i n t pa r a e [ 3 ] ;
do uble parad [ 3 ] ;
};
s t r u c t path {
do uble Cost [ Obj ] ;
v e c t o r <i n t > Nodes ;
};
t y p e d e f v e c t o r <path > p o p u l a t i o n ;
/ / Grafo de e n t r a d a
graph Graph ;
/ / Ma t r iz de c o s t o s
T c o s t s Co st s [ Obj ] [MAX] [MAX] ;
i n t NNodes , NEdges ;
cha r D e s c r i p t i o n [ 1 0 0 ] ;
/ / Po bla ci ’ on
p o p u l a t i o n Paths ;
/ / Conjunto de Pa r et o
p o p u l a t i o n Front ;
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
/ / F uncio nes de D i s t r i b u c i ’ on
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
// Simula una d i s t r i b u c i ’ on e x p o n e n c i a l
do uble e x p o n e n c i a l ( do uble lambda ) {
r e t u r n − l o g (1 − random . randExc ( ) ) / lambda ;
}
// Simula una d i s t r i b u c i ’ on gamma
APÉNDICE B. CÓDIGO RUTAS CORTAS
do uble gamma( i n t n , do uble lambda ) {
i n t k=0;
do uble suma ;
suma =0;
f o r ( k=0;k<n ; k++)
suma=suma+e x p o n e n c i a l ( lambda ) ;
r e t u r n suma ;
}
// Simula una d i s t r i b u c i ’ on normal ( 0 , 1 )
do uble normal01 ( ) {
r e t u r n ( s q r t ( −2.0∗ l o g (1 − random . randExc ( ) ) ) )
∗ c o s ( 2 . 0 ∗ PI ∗(1 − random . randExc ( ) ) ) ;
}
// Simula una d i s t r i b u c i ’ on normal ( media , v a r i a n z a )
do uble normal ( do uble myu , do uble sigmacuad ) {
r e t u r n ( normal01 ( ) ∗ s q r t ( sigmacuad )+myu ) ;
}
// Simula una d i s t r i b u c i ’ on unif o r me c o n t i n u a ( 0 , 1 )
do uble u n i f o r m e c 0 1 ( ) {
r e t u r n random . randExc ( ) ;
}
// Simula una d i s t r i b u c i ’ on unif o r me c o n t i n u a ( a , b )
do uble u n i f o r m e c ( do uble a , do uble b ) {
r e t u r n ( u n i f o r m e c 0 1 ( ) ∗ ( b−a)+a ) ;
}
// Simula una d i s t r i b u c i ’ on Chi cuadrada
do uble c h i ( i n t g r a d o s ) {
i n t k=g r a d o s / 2 ;
do uble x , a ;
x=gamma( k , 0 . 5 ) ;
i f ( g r a d o s % 2==1) {
a=normal01 ( ) ;
x=x+a∗a ;
}
return x ;
62
APÉNDICE B. CÓDIGO RUTAS CORTAS
}
// Simula una d i s t r i b u c i ’ on p o i s s o n
i n t p o i s s o n ( do uble lambda ) {
do uble suma ;
int x ;
suma=e x p o n e n c i a l ( lambda ) ;
x=0;
w h i l e ( suma<=1) {
suma=suma+ e x p o n e n c i a l ( lambda ) ;
x++;
}
return x ;
}
// Simula una d i s t r i b u c i ’ on hipergeom ’ e t r i c a
i n t h i p e r g e o ( i n t M, i n t K, i n t n ) {
int i , x ;
int fish ;
v e c t o r <i n t > Pbl ;
Pbl . c l e a r ( ) ;
f o r ( i = 0 ; i <K ; i ++)
Pbl . push back ( 1 ) ;
f o r ( ; i <M; i ++)
Pbl . push back ( 0 ) ;
x=0;
f o r ( i = 0 ; i <n ; i ++) {
f i s h =( i n t ) ( random . randExc ( ) ∗ Pbl . s i z e ( ) ) ;
x=x+Pbl [ f i s h ] ;
Pbl . e r a s e ( Pbl . b e g i n ()+ f i s h ) ;
}
return x ;
}
// Simula una d i s t r i b u c i ’ on geom ’ e t r i c a
i n t g e o m e t r i c a ( do uble p ) {
do uble pp ;
i n t i , suma ;
63
APÉNDICE B. CÓDIGO RUTAS CORTAS
suma =0;
pp= random . randExc ( ) ;
w h i l e ( pp>p ) {
suma++;
pp= random . randExc ( ) ;
}
r e t u r n suma ;
}
// Simula una d i s t r i b u c i ’ on b i n o m i a l
i n t b i n o m i a l ( i n t n , do uble p ) {
do uble pp ;
i n t i , suma ;
suma =0;
f o r ( i =0; i <n ; i ++) {
pp= random . randExc ( ) ;
i f ( pp<=p ) suma++;
}
r e t u r n suma ;
}
// Simula una d i s t r i b u c i ’ on unif o r me d i s c r e t a
i n t unif o r med ( i n t n ) {
r e t u r n i n t ( n∗random . randExc ( ) + 1 ) ;
}
// Simula una d i s t r i b u c i ’ on b e r n o u l l i
i n t b e r n u l i ( do uble p ) {
return binomial ( 1 , p ) ;
}
// Simula una d i s t r i b u c i ’ on b i n o m i a l n e g a t i v a
i n t binneg ( i n t r , do uble p ) {
do uble pp ;
i n t i , suma ;
suma =0;
while ( r ) {
pp = random . randExc ( ) ;
w h i l e ( pp>p ) {
64
APÉNDICE B. CÓDIGO RUTAS CORTAS
65
suma++;
pp= random . randExc ( ) ;
}
r −−;
}
r e t u r n suma ;
}
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
/ / L ect ur a de par ’ ametros de l a s f u n c i o n e s de d i s t r i b u c i ’ on
v o i d l e e r p a r a m e t r o s ( i n t x , i n t y , i n t z ){
s w i t c h ( Co st s [ x ] [ y ] [ z ] . f u n c i o n ) {
c a s e ’ a ’ : c a s e ’A ’ :
in >> Co st s [ x ] [ y ] [ z ] . parad [ 0 ] ;
break ;
case ’ b ’ : case ’B’ :
in >> Co st s [ x ] [ y ] [ z ] . pa r a e [ 0 ] ;
in >> Co st s [ x ] [ y ] [ z ] . parad [ 0 ] ;
break ;
case ’ c ’ : case ’C’ :
break ;
c a s e ’ d ’ : c a s e ’D ’ :
in >> Co st s [ x ] [ y ] [ z ] . parad [ 0 ] ;
in >> Co st s [ x ] [ y ] [ z ] . parad [ 1 ] ;
break ;
case ’ e ’ : case ’E ’ :
break ;
case ’ f ’ : case ’F ’ :
in >> Co st s [ x ] [ y ] [ z ] . parad [ 0 ] ;
in >> Co st s [ x ] [ y ] [ z ] . parad [ 1 ] ;
break ;
c a s e ’ g ’ : c a s e ’G’ :
in >> Co st s [ x ] [ y ] [ z ] . pa r a e [ 0 ] ;
break ;
c a s e ’ h ’ : c a s e ’H ’ :
in >> Co st s [ x ] [ y ] [ z ] . parad [ 0 ] ;
break ;
case ’ i ’ : case ’ I ’ :
in >> Co st s [ x ] [ y ] [ z ] . pa r a e [ 0 ] ;
in >> Co st s [ x ] [ y ] [ z ] . pa r a e [ 1 ] ;
APÉNDICE B. CÓDIGO RUTAS CORTAS
66
in >> Co st s [ x ] [ y ] [ z ] . pa r a e [ 2 ] ;
break ;
case ’ j ’ : case ’ J ’ :
in >> Co st s [ x ] [ y ] [ z ] . parad [ 0 ] ;
break ;
c a s e ’ k ’ : c a s e ’K’ :
in >> Co st s [ x ] [ y ] [ z ] . pa r a e [ 0 ] ;
in >> Co st s [ x ] [ y ] [ z ] . parad [ 0 ] ;
break ;
case ’ l ’ : case ’L ’ :
in >> Co st s [ x ] [ y ] [ z ] . pa r a e [ 0 ] ;
break ;
c a s e ’m’ : c a s e ’M’ :
in >> Co st s [ x ] [ y ] [ z ] . parad [ 0 ] ;
break ;
c a s e ’ n ’ : c a s e ’N ’ :
in >> Co st s [ x ] [ y ] [ z ] . pa r a e [ 0 ] ;
in >> Co st s [ x ] [ y ] [ z ] . parad [ 0 ] ;
break ;
case ’\ n ’ : case ’ ’ :
break ;
default :
break ;
}
}
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
/ / L ect ur a d e l a r c h i v o de e n t r a d a
v o i d LoadGraph ( cha r ∗ FileName ) {
int i , j , a , b;
edges Childs ;
i n . open ( FileName ) ;
in . g e t l i n e ( Description , 1 0 0 ) ;
i n >> NNodes >> NEdges ;
Childs . c l e a r ( ) ;
f o r ( i = 0 ; i <NNodes ; i ++)
Graph . push back ( C h i l d s ) ;
f o r ( i = 0 ; i <NEdges ; i ++) {
i n >> a >> b ;
APÉNDICE B. CÓDIGO RUTAS CORTAS
67
a −−; b−−;
f o r ( j =0; j <Obj ; j ++) {
i n >> Co st s [ j ] [ a ] [ b ] . f i j o ;
i n >> Co st s [ j ] [ a ] [ b ] . f u n c i o n ;
leerparametros ( j , a , b ) ;
}
Graph [ a ] . push back ( b ) ;
}
in . close ( ) ;
co ut << D e s c r i p t i o n < < ”:” < < e n d l ;
co ut << ”Graph with ” < < NNodes < < ” nodes and ”
<< NEdges < < ” e d g e s l o a d e d .” < < e n d l ;
/ ∗ Latex ∗ /
o l a t e x << D e s c r i p t i o n < < ”:” < < e n d l ;
o l a t e x << ”Graph with ” < < NNodes < < ” nodes and ”
<< NEdges < < ” e d g e s l o a d e d .” < < e n d l << e n d l ;
}
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
/ / Impr esi ’ on de l a r ed en a r c h i v o de Excel
v o i d PrintGraph ( ) {
int i , j , v ;
f o r ( i = 0 ; i <NNodes ; i ++) {
o e x c e l << ( i +1) < < ”:”;
f o r ( j = 0 ; j <Graph [ i ] . s i z e ( ) ; j ++) {
v = Graph [ i ] [ j ] ;
o e x c e l < < ” (” < < ( v + 1 ) < < ” , ” ;
f o r ( i n t k = 0 ; k<Obj ; k++){
o e x c e l << Co st s [ k ] [ i ] [ v ] . f i j o < < ” ”;
}
o e x c e l < < ”)”;
}
o e x c e l << e n d l ;
}
o e x c e l << e n d l ;
}
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
APÉNDICE B. CÓDIGO RUTAS CORTAS
68
/ / Cr ea ci ’ on de r u t a s a l e a t o r i a s
v o i d RandomPaths ( i n t i n i , i n t f i n ) {
i n t i , v , next ;
path Path ;
Paths . c l e a r ( ) ;
f o r ( i = 0 ; i <PopCount ; i ++) {
v = ini ;
f o r ( i n t k=0;k<Obj ; k++){
Path . Cost [ k ] = 0 ;
}
Path . Nodes . c l e a r ( ) ;
Path . Nodes . push back ( v ) ;
w h i l e ( v!= f i n ) {
next = Graph [ v ] [ random . r a n d I n t ( Graph [ v ] . s i z e ( ) − 1 ) ] ;
Path . Nodes . push back ( next ) ;
v = next ;
}
Paths . push back ( Path ) ;
}
o l a t e x << ” P o p u l a t i o n o f s i z e ” < < PopCount
<< ” c r e a t e d .” < < e n d l ;
}
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
/ / Co st o s a l e a t o r i o s
v o i d VarCost ( i n t a , i n t b ) {
int k ;
do uble x ;
x=0;
f o r ( k=0;k<Obj ; k++){
Co st o s [ k]= Co st s [ k ] [ a ] [ b ] . f i j o ;
i f ( stochastic ) {
s w i t c h ( Co st s [ k ] [ a ] [ b ] . f u n c i o n ) {
c a s e ’ a ’ : c a s e ’A ’ :
x=e x p o n e n c i a l ( Co st s [ k ] [ a ] [ b ] . parad [ 0 ] ) ;
break ;
APÉNDICE B. CÓDIGO RUTAS CORTAS
case ’ b ’ : case ’B’ :
x=gamma( Co st s [ k ] [ a ] [ b ] . pa r a e [ 0 ] ,
Co st s [ k ] [ a ] [ b ] . parad [ 0 ] ) ;
break ;
case ’ c ’ : case ’C’ :
x=normal01 ( ) ;
break ;
c a s e ’ d ’ : c a s e ’D ’ :
x=normal ( Co st s [ k ] [ a ] [ b ] . parad [ 0 ] ,
Co st s [ k ] [ a ] [ b ] . parad [ 1 ] ) ;
break ;
case ’ e ’ : case ’E ’ :
x=u n i f o r m e c 0 1 ( ) ;
break ;
case ’ f ’ : case ’F ’ :
x=u n i f o r m e c ( Co st s [ k ] [ a ] [ b ] . parad [ 0 ] ,
Co st s [ k ] [ a ] [ b ] . parad [ 1 ] ) ;
break ;
c a s e ’ g ’ : c a s e ’G’ :
x=c h i ( Co st s [ k ] [ a ] [ b ] . pa r a e [ 0 ] ) ;
break ;
c a s e ’ h ’ : c a s e ’H ’ :
x=p o i s s o n ( Co st s [ k ] [ a ] [ b ] . parad [ 0 ] ) ;
break ;
case ’ i ’ : case ’ I ’ :
x=h i p e r g e o ( Co st s [ k ] [ a ] [ b ] . pa r a e [ 0 ] ,
Co st s [ k ] [ a ] [ b ] . pa r a e [ 1 ] ,
Co st s [ k ] [ a ] [ b ] . pa r a e [ 2 ] ) ;
break ;
case ’ j ’ : case ’ J ’ :
x=g e o m e t r i c a ( Co st s [ k ] [ a ] [ b ] . parad [ 0 ] ) ;
break ;
c a s e ’ k ’ : c a s e ’K’ :
x=b i n o m i a l ( Co st s [ k ] [ a ] [ b ] . pa r a e [ 0 ] ,
Co st s [ k ] [ a ] [ b ] . parad [ 0 ] ) ;
break ;
case ’ l ’ : case ’L ’ :
x=unif o r med ( Co st s [ k ] [ a ] [ b ] . pa r a e [ 0 ] ) ;
break ;
c a s e ’m’ : c a s e ’M’ :
x=b e r n u l i ( Co st s [ k ] [ a ] [ b ] . parad [ 0 ] ) ;
break ;
69
APÉNDICE B. CÓDIGO RUTAS CORTAS
70
c a s e ’ n ’ : c a s e ’N ’ :
x=binneg ( Co st s [ k ] [ a ] [ b ] . pa r a e [ 0 ] ,
Co st s [ k ] [ a ] [ b ] . parad [ 0 ] ) ;
break ;
case ’\ n ’ : case ’ ’ :
break ;
default :
co ut << ” Funcion no enco nt r a da ” ;
break ;
}
Co st o s [ k]+=x ;
}
}
}
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
/ / C a l c u l a Co st o s de toda l a p o b l a c i ’ on
void ObtainFitness ( ) {
i n t i , j , k , v , next ;
f o r ( i =0; i <Obj ; i ++) {
Co st o s [ i ] = 0 ;
}
f o r ( i = 0 ; i <Paths . s i z e ( ) ; i ++) {
f o r ( k = 0 ; k< Obj ; k++) {
Paths [ i ] . Cost [ k ] = 0 ;
}
v = Paths [ i ] . Nodes [ 0 ] ;
f o r ( j = 1 ; j <Paths [ i ] . Nodes . s i z e ( ) ; j ++) {
next = Paths [ i ] . Nodes [ j ] ;
VarCost ( v , next ) ;
f o r ( k = 0 ; k<Obj ; k++) {
Paths [ i ] . Cost [ k]+= Co st o s [ k ] ;
}
v = next ;
}
}
APÉNDICE B. CÓDIGO RUTAS CORTAS
71
}
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
/ / Cr ea ci ’ on de c o n j u n t o s de Pa r et o
v o i d Pa r et o 2 ( ) {
int i , j , k , f , l ;
b o o l band , e n t r a ;
f=Front . s i z e () −1;
f o r ( i =1; i <Paths . s i z e ( ) ; i ++) {
e n t r a=t r u e ;
f o r ( k = 0 ; k<=f ; k++) {
band=t r u e ;
f o r ( l =0; l <Obj ; l ++){
i f ( Paths [ i ] . Cost [ l ]<=Front [ k ] . Cost [ l ] ) ;
e l s e band=f a l s e ;
}
i f ( band ) {
Front . e r a s e ( Front . b e g i n ()+ k ) ;
f=f −1;
k=k−1;
} else {
band=t r u e ;
f o r ( l =0; l <Obj ; l ++) {
i f ( Paths [ i ] . Cost [ l ]>=Front [ k ] . Cost [ l ] ) ;
e l s e band=f a l s e ;
}
i f ( band ) e n t r a=f a l s e ;
}
}
i f ( entra ) {
Front . push back ( Paths [ i ] ) ;
f ++;
}
}
}
APÉNDICE B. CÓDIGO RUTAS CORTAS
72
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
/ / Impr esi ’ on de c o n j u n t o s de Pa r et o
void PrintPareto ( ) {
int i , j , k ;
o e x c e l << Front . s i z e () < < e n d l ;
f o r ( i = 0 ; i <Front . s i z e ( ) ; i ++) {
o e x c e l < < ”(”;
f o r ( j = 0 ; j <Front [ i ] . Nodes . s i z e ( ) ; j ++) {
i f ( j ) oexcel < < ” , ”;
o e x c e l << Front [ i ] . Nodes [ j ] + 1 ;
}
o excel < < ”)” ;
f o r ( k=0;k<Obj ; k++) {
o e x c e l << ”\ t ” << Front [ i ] . Cost [ k ] ;
}
o e x c e l << e n d l ;
}
o e x c e l << e n d l ;
// latex
o l a t e x << ”\\ b e g i n { t a b u l a r }{ l ” ;
f o r ( k = 0 ; k<Obj ; k++) o l a t e x << ” l ” ;
o l a t e x << ”}” << e n d l ;
f o r ( i = 0 ; i <Front . s i z e ( ) ; i ++) {
olatex << ” (”;
f o r ( j = 0 ; j <Front [ i ] . Nodes . s i z e ( ) ; j ++) {
i f ( j ) olatex < < ” , ”;
o l a t e x << Front [ i ] . Nodes [ j ] + 1 ;
}
o la tex < < ”)” ;
f o r ( k=0;k<Obj ; k++) {
o l a t e x << ”\ t&\t ” << Front [ i ] . Cost [ k ] ;
}
o l a t e x < < ” \\\\” < < e n d l ;
}
o l a t e x << ”\\ end { t a b u l a r }” << e n d l << e n d l ;
}
APÉNDICE B. CÓDIGO RUTAS CORTAS
73
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
/ / Impr esi ’ on de l a p o b l a c i ’ on
void PrintPaths ( ) {
int i , j , k ;
// latex
o l a t e x << ”\\ b e g i n { t a b u l a r }{ l ” ;
f o r ( k = 0 ; k<Obj ; k++) o l a t e x << ” l ” ;
o l a t e x << ”}” << e n d l ;
f o r ( i = 0 ; i <Paths . s i z e ( ) ; i ++) {
olatex << ” (”;
f o r ( j = 0 ; j <Paths [ i ] . Nodes . s i z e ( ) ; j ++) {
i f ( j ) olatex < < ” , ”;
o l a t e x << Paths [ i ] . Nodes [ j ] + 1 ;
}
o la tex < < ”)” ;
f o r ( k=0;k<Obj ; k++) {
o l a t e x << ”\ t&\t ” << Paths [ i ] . Cost [ k ] ;
}
o l a t e x < < ” \\\\” < < e n d l ;
}
o l a t e x << ”\\ end { t a b u l a r }” << e n d l << e n d l ;
}
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
/ / A c t u a l i z a c i ’ on de r u t a s por r eco mbina ci ’ on y mutaci ’ on
v o i d NewRandomPaths ( i n t i n i , i n t f i n ) {
i n t i , v , next ;
i n t ToCrossOver = ( i n t ) ( PopCount ∗(1 − MutationRate ) ) ;
i n t nodes ;
/ ∗ Rutas mutadas ∗ /
f o r ( i=ToCrossOver ; i <PopCount ; i ++) {
Paths [ i ] . Nodes . c l e a r ( ) ;
}
/ ∗ Rutas recombinadas ∗ /
f o r ( i = 0 ; i <ToCrossOver ; i ++) {
nodes = ( i n t ) ( Cr o sso ver R a t e ∗ Paths [ i ] . Nodes . s i z e ( ) ) ;
w h i l e ( nodes ) {
Paths [ i ] . Nodes . pop back ( ) ;
APÉNDICE B. CÓDIGO RUTAS CORTAS
74
nodes −−;
}
}
/ ∗ R eg ener a r p o b l a c i ’ on ∗ /
f o r ( i = 0 ; i <PopCount ; i ++) {
i f ( ! Paths [ i ] . Nodes . s i z e ( ) )
Paths [ i ] . Nodes . push back ( i n i ) ;
v = Paths [ i ] . Nodes . back ( ) ;
w h i l e ( v!= f i n ) {
next =
Graph [ v ] [ random . r a n d I n t ( Graph [ v ] . s i z e ( ) − 1 ) ] ;
Paths [ i ] . Nodes . push back ( next ) ;
v = next ;
}
}
}
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
/ / L ect ur a de par ’ ametros
i n t LoadParams ( ) {
iparams . open ( ” params . c n f ” ) ;
iparams >> G e n e r a t i o n s >> PopCount >> RunCount ;
iparams >> MutationRate >> Cr o sso ver R a t e ;
co ut < < ”(G=” << G e n e r a t i o n s < < ” , P=” << PopCount
< < ” , M=” << MutationRate < < ” , C=” << Cr o sso ver R a t e
<< ”)” << e n d l ;
iparams . c l o s e ( ) ;
}
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
/ / Codigo p r i n c i p a l
i n t main ( i n t argc , cha r ∗ a r g s [ ] ) {
clock t start = clock ( ) ;
int i
cha r
cha r
cha r
, run ;
basefile [80];
archivo [ 8 0 ] ;
outfile [80];
APÉNDICE B. CÓDIGO RUTAS CORTAS
cha r o p c i o n [ 8 0 ] ;
i f ( argc <=3) {
co ut << ”Forma de uso : ” < < e n d l ;
co ut < < ” pa t hs3 i n p u t output e s t o c a s t i c o ” << e n d l ;
return 0 ;
}
/ / L eer v a l o r e s de l o s pa r a met r o s
LoadParams ( ) ;
/ / L e c t o r de par ’ ametros de e n t r a d a
strcpy ( archivo , args [ 1 ] ) ;
s t r c a t ( archivo , xtext ) ;
strcpy ( b a sef i l e , args [ 2 ] ) ;
s t r c p y ( opcion , a r g s [ 3 ] ) ;
/ / Opci ’ on c a s o determin ’ i s t i c o o e s t o c ’ a s t i c o
i f ( ! strcmp ( opcion , ” f a l s e ” ) ) s t o c h a s t i c = f a l s e ;
e l s e stochastic = true ;
/ / Abr ir a r c h i v o s de e n t r a d a y s a l i d a
strcpy ( outfile , b a s e f i l e ) ;
strcat ( outfile , xexcel ) ;
o e x c e l . open ( o u t f i l e ) ;
strcpy ( outfile , b a s e f i l e ) ;
strcat ( outfile , xlatex ) ;
o l a t e x . open ( o u t f i l e ) ;
LoadGraph ( a r c h i v o ) ;
/ / C i c l o p r i n c i p a l d e l programa
co ut << ”Running ” < < RunCount < < ” c a s e s . . . ” << e n d l ;
o e x c e l << RunCount << e n d l ;
f o r ( run = 0 ; run<RunCount ; run ++) {
o e x c e l << ”Case ” < < ( run +1) < < ”:” < < e n d l ;
o l a t e x << ”Case ” < < ( run +1) < < ”:” < < e n d l ;
Front . c l e a r ( ) ;
RandomPaths ( 0 , NNodes − 1 ) ;
ObtainFitness ( ) ;
PrintPaths ( ) ;
75
APÉNDICE B. CÓDIGO RUTAS CORTAS
76
o l a t e x << ”Running ” < < G e n e r a t i o n s
<< ” g e n e r a t i o n s . . . ” < < e n d l << e n d l ;
f o r ( i = 0 ; i <G e n e r a t i o n s ; i ++) {
NewRandomPaths ( 0 , NNodes − 1 ) ;
ObtainFitness ( ) ;
Pa r et o 2 ( ) ;
}
PrintPareto ( ) ;
}
/ / C e r r a r a r c h i v o s de s a l i d a
oexcel . close ( ) ;
olatex . close ( ) ;
/ / R epo r t a r tiempo de e j e c u c i ’ on
co ut << ” time : ” < < ( c l o c k ()− s t a r t ) / ( do uble )CLOCKS PER SEC
<< ” s e g s ” << e n d l ;
return 0 ;
}
Documentos relacionados
Descargar