string padre1[10];
string padre2[10];
string genes[10];
string hijo1[10];
string hijo2[10];
string rutas[10][10];
struct Vertices{
char nombre[15];
int rutaHabilitada; /// 1-Si // 0-No
int visitado; /// 1-Si // 0-No
struct Vertices *sigV;
struct Arco *sigA;
Vertices(const char *nomC,int rutHab){
strcpy(nombre,nomC);
rutaHabilitada = rutHab;
visitado = 0;
sigV = NULL;
sigA = NULL;
}
}*primeroVertices;
struct Arco{ /// Cada Arco Representará las Autopistas o Caminos entre las Ciudades
char tipoTransporte[15];
int costo;
int duracion;
int distancia;
int estadoRuta; /// 1- Activo // 0 -Inactivo
struct Vertices *sigV; /// Destino
struct Arco *sigA;
Arco(const char *transp, int cost,int dur,int dist,int estado){
strcpy(tipoTransporte,transp);
costo = cost;
duracion = dur;
distancia = dist;
estadoRuta = estado;
sigV = NULL;
}
};
struct Pila{ /// Para utilizado para diferentes consultas, ruta mas corta, tamaño de la ruta, velocidad maxima,etc
string ciudad;
struct Pila * sig;
struct Arco *sigA;
Pila(string c){
ciudad=c;
sigA = NULL;
sig = NULL;
}
}*pila,*pilaMin;
void mutarGen(){
srand(time(NULL));
int genRandom=rand()%g;
cout<<"Padre sin mutacion: ";
for(int i =0;i<10;i++){
if(padre1[i]=="")
break;
cout<<padre1[i];
}
cout<<endl;
if (padre1[2]!="")
padre1[2]= genes[genRandom];
else
padre1[1]= genes[genRandom];
cout<<"Padre mutado: ";
for(int i =0;i<10;i++){
if(padre1[i]=="")
break;
cout<<padre1[i];
}
cout<<endl;
}
void seleccionarPadres(int factor){
int numeroPadres = 1;
for(int i=0;i<10;i++){
if((tCostos[i] <= factor) and (tCostos[i] != 0)){
if (numeroPadres==1){
for(int j=0;j<10;j++){
padre1[j] = rutas[i][j];
}
}
else{
for(int j=0;j<10;j++){
padre2[j] = rutas[i][j];
}
}
numeroPadres++;
}
if((numeroPadres>2) or (rutas[i][0]==""))
break;
}
if (numeroPadres>2){
hacerCruce();
}
else
mutarGen();
}
void hacerCruce(){
for(int i=0;i<10;i++){
if (i<=1){
hijo1[i] = padre1[i];
hijo2[i] = padre2[i];
}
else{
hijo1[i] = padre2[i];
hijo2[i] = padre1[i];
}
}
cout<<"Padre 1: ";
for(int k=0;k<10;k++){
if (padre1[k] == "")
break;
cout<<padre1[k];
}
cout<<endl;
cout<<"Padre 2: ";
for(int k=0;k<10;k++){
if (padre2[k] == "")
break;
cout<<padre2[k];
}
cout<<endl;
cout<<"Hijo 1: ";
for(int k=0;k<10;k++){
if (hijo1[k] == "")
break;
cout<<hijo1[k];
}
cout<<endl;
cout<<"Hijo 2: ";
for(int k=0;k<10;k++){
if (hijo2[k] == "")
break;
cout<<hijo2[k];
}
}
void extraerPadres(struct Pila *pila){
if(pila == NULL)
return;
extraerPadres(pila->sig);
//cout<<pila->ciudad<<", ";
rutas[u][w] = pila->ciudad;
w++;
}
void sumarRutasCosto(){
int i=0;
int j=0;
x = 0;
int suma = 0;
while (i<10){
struct Vertices * tempVertice = buscarVertice(rutas[i][j]);
if (tempVertice!=NULL){
struct Arco * tempArco=buscarArco(tempVertice,rutas[i][j+1]);
if (tempArco!=NULL){
suma = suma + tempArco->costo;
}
}
if(j==8){
tCostos[x] = suma;
suma = 0;
i++;
x++;
j=-1;
}
j++;
}
}
void ruta(struct Vertices * origen, string destino){
if(origen == NULL)
return;
if(origen->visitado == true){
//totalCosto = 0;
return;
}
push(origen->nombre);
if(origen->nombre == destino){
///cout<<"Si existe ruta.."<<endl;
hayRuta = true;
///tCostos[x] = totalCosto;
extraerPadres(pila);
///totalCosto = 0;
conta++;
u++;
w=0;
///x++;
cout<<endl;
return;
}
else{
origen->visitado = true; /// Lo voy a visitar
struct Arco * tempArco = origen->sigA;
while(tempArco!=NULL){
if (tempArco->estadoRuta!=0){
ruta(buscarVertice(tempArco->sigV->nombre),destino);
}
/// totalCosto = totalCosto + tempArco->costo;
tempArco = tempArco->sigA;
pop(); /// Se coloca aqui porque ya aqui esta en un nuevo camino o ruta
}
origen->visitado = false;
}
}