elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Los 10 CVE más críticos (peligrosos) de 2020


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación C/C++ (Moderadores: Eternal Idol, Littlehorse, K-YreX)
| | |-+  Algoritmo Genetico problema de las n reinas
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Algoritmo Genetico problema de las n reinas  (Leído 2,925 veces)
kjg

Desconectado Desconectado

Mensajes: 3


Ver Perfil
Algoritmo Genetico problema de las n reinas
« en: 24 Noviembre 2018, 22:43 pm »


tengo el siguiente algoritmo para resolver el problema de la n reinas con algoritmo genetico, mi duda es como podria adaptarlo para un estilo de seleccion por torneo

Código
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. #include <math.h>
  6.  
  7.  
  8.  
  9.  
  10. int TamT = 8;
  11.  
  12. int area[50][50];
  13.  
  14. int chromosomeMatriz[50][100000];
  15. int CostoMatriz[10000];
  16. int MatrizCruz[50][100000];
  17. int Poblacion = 100000;
  18. int Iteracion = 100000;
  19. float MutacionR = 0.5;
  20.  
  21.  
  22. void Clear(){
  23. int i, j;
  24. for (i=0;i<TamT;i++)
  25. for (j=0;j<TamT;j++)
  26. area[i][j]=0;
  27. }
  28.  
  29.  
  30. void PoblacionInicial(){
  31. int random, index, a, b, bCheck;
  32.  
  33. for (index=0; index<=Poblacion-1; index++){
  34. for (a=0; a<TamT; a++){
  35. random = rand();
  36. bCheck = 1;
  37.  
  38. for(b=0; b<a; b++){
  39. if(random % TamT == chromosomeMatriz[b][index])
  40. bCheck=0;
  41. if(bCheck)
  42. chromosomeMatriz[a][index] = random % TamT;
  43. else
  44. a--;
  45. }
  46. }
  47. }
  48. }
  49.  
  50.  
  51. void LlenarArea(int index){
  52. int i;
  53.  
  54.    Clear();
  55.    for (i=0; i<TamT; i++)
  56.        area[i][chromosomeMatriz[i][index]]=1;
  57. }
  58.  
  59.  
  60. int CostFunc(int index){
  61.    int costValue=0;
  62.    int m,n;
  63.    int i,j;
  64.  
  65.    for(i=0;i<TamT;i++){    
  66.        j=chromosomeMatriz[i][index];
  67.  
  68.        m=i+1;
  69.        n=j-1;
  70.        while(m<TamT && n>=0){
  71.            if(area[m][n]==1) costValue++;
  72.            m++;
  73.            n--;
  74.        }
  75.  
  76.        m=i+1;
  77.        n=j+1;
  78.        while(m<TamT && n<TamT ){        
  79.            if(area[m][n]==1) costValue++;            
  80.            m++;
  81.            n++;
  82.        }
  83.  
  84.        m=i-1;
  85.        n=j-1;
  86.        while(m>=0 && n>=0){        
  87.            if(area[m][n]==1) costValue++;
  88.            m--;
  89.            n--;
  90.        }
  91.  
  92.        m=i-1;
  93.        n=j+1;
  94.        while(m>=0 && n<TamT){        
  95.            if(area[m][n]==1) costValue++;            
  96.            m--;
  97.            n++;
  98.        }
  99.    }
  100.  
  101.    return costValue;
  102. }
  103.  
  104.  
  105. void PopulationSort(){
  106.    int k=1, i, j;
  107.    int Temp;
  108.    while (k){
  109.        k=0;
  110.        for (i=0;i<=Poblacion-2;i++){
  111.            if (CostoMatriz[i]>CostoMatriz[i+1]){
  112.                Temp=CostoMatriz[i];
  113.                CostoMatriz[i] = CostoMatriz[i+1];
  114.                CostoMatriz[i+1] = Temp;
  115.  
  116.            for (j=0; j<TamT; j++){
  117.                Temp=chromosomeMatriz[j][i];
  118.                chromosomeMatriz[j][i] = chromosomeMatriz[j][i+1];
  119.                chromosomeMatriz[j][i+1] = Temp;
  120.            }            
  121.            k=1;
  122.            }
  123.        }
  124.    }
  125. }
  126.  
  127.  
  128. void GenerateCrossOverMatrix(){
  129.    int randomCrossOver, index, a;
  130.  
  131.    for (index=0;index<=Poblacion-1;index++){
  132.        for (a=0;a<TamT;a++){
  133.            randomCrossOver=rand();
  134.           MatrizCruz[a][index]=randomCrossOver%2;
  135.        }
  136.    }
  137. }
  138.  
  139.  
  140. void Mating(){
  141. int TempMatrix[50][2];
  142. int TempMatrix0[50],TempMatrix1[50];
  143. int Temp,j,k, index, t, i;
  144.  
  145. for (index=0;index<=(Poblacion/4)-1;index++){
  146. for (t=0;t<=1;t++){
  147.  
  148. for(i=0;i<TamT;i++){
  149. TempMatrix0[i]=chromosomeMatriz[i][2*index];
  150. TempMatrix1[i]=chromosomeMatriz[i][2*index+1];
  151. }
  152. for (i=0;i<TamT;i++)
  153. if(MatrizCruz[i][2*index+t]==0){
  154. for (j=0;j<TamT;j++)
  155. if(TempMatrix0[j]!=100){
  156. TempMatrix[i][t]=TempMatrix0[j];
  157. Temp=TempMatrix0[j];
  158. TempMatrix0[j]=100;
  159. j=TamT-1;
  160.  
  161. for (k=0;k<TamT;k++){
  162. if (TempMatrix1[k]==Temp){
  163. TempMatrix1[k]=100;
  164. k=TamT-1;
  165. }
  166. }
  167. }
  168. }else{
  169. for (j=0;j<TamT;j++)
  170. if(TempMatrix1[j]!=100){
  171. TempMatrix[i][t]=TempMatrix1[j];
  172. Temp=TempMatrix1[j];
  173. TempMatrix1[j]=100;
  174. j=TamT-1;
  175.  
  176. for (k=0;k<TamT;k++){
  177. if (TempMatrix0[k]==Temp){
  178. TempMatrix0[k]=100;
  179. k=TamT-1;
  180. }
  181. }
  182. }
  183. }
  184.  
  185. for(i=0;i<TamT;i++)
  186. chromosomeMatriz[i][2*index+Poblacion/2+t]=TempMatrix[i][t];
  187.  
  188. }
  189. }
  190. }
  191.  
  192.  
  193. void ApplyMutation(){
  194. int randomChromosome;
  195. int randomGen0,randomGen1;
  196. int Temp, k;
  197. int NumberOfMutation = (int)MutacionR*(Poblacion-1)*TamT;
  198.  
  199. for(k=0;k<=NumberOfMutation;k++){
  200. randomChromosome=0;
  201. while((randomChromosome=rand()%Poblacion)==0);
  202.  
  203. randomGen0=rand()%TamT;
  204. while((randomGen1=rand()%TamT)==randomGen0);
  205.  
  206. Temp=chromosomeMatriz[randomGen0][randomChromosome];
  207. chromosomeMatriz[randomGen0][randomChromosome]=chromosomeMatriz[randomGen1][randomChromosome];
  208. chromosomeMatriz[randomGen0][randomChromosome]=Temp;
  209. }
  210.  
  211. }
  212.  
  213.  
  214. void DisplayBoard(){
  215. int i, j;
  216.  
  217. for(i=0; i<=TamT-1; i++){
  218. for(j=0; j<=TamT-1; j++){
  219. if(j == chromosomeMatriz[i][0]){
  220. printf("0  ");
  221. }else{
  222. printf(".  ");
  223. }
  224. }
  225. printf("\n");
  226. }
  227.  
  228. printf("\n");
  229. }
  230.  
  231. int main(){
  232.  
  233. int i,k,g,num;
  234. char a='g';
  235.  
  236. k=0;
  237. g=0;
  238. num=0;
  239.  
  240. printf("\nNumero de reinas: ");
  241. scanf("%d", &TamT);
  242.  
  243. printf("Numero de poblacion (ex 1000): ");
  244. scanf("%d", &Poblacion);
  245.  
  246. printf("Numarul de iteratii (ex 1000): ");
  247. scanf("%d", &Iteracion);
  248.  
  249. printf("Rata de mutatie (ex 0.5): ");
  250. scanf("%f", &MutacionR);
  251.  
  252. printf("\nSolutie:\n");
  253.  
  254. PoblacionInicial();
  255.  
  256. while(g==0 && num<Iteracion){
  257. num++;
  258. g=0;
  259.  
  260. for (k=0;k<=Poblacion-1;k++){
  261. LlenarArea(k);
  262. CostoMatriz[k]=CostFunc(k);
  263. }
  264.  
  265. PopulationSort();
  266.  
  267. if (CostoMatriz[0]==0) g=1;
  268.  
  269. DisplayBoard();
  270.  
  271. GenerateCrossOverMatrix();
  272.  
  273. Mating();
  274.  
  275. ApplyMutation();
  276.  
  277. system("PAUSE");
  278. return 0;
  279. }
  280. }
  281.  
  282.  
  283.  
  284.  
  285.  


« Última modificación: 25 Noviembre 2018, 00:10 am por kjg » En línea

MAFUS


Desconectado Desconectado

Mensajes: 1.603



Ver Perfil
Re: Algoritmo Genetico problema de las n reinas
« Respuesta #1 en: 24 Noviembre 2018, 22:49 pm »

Edita el post y pon el código entre etiquetas GeSHi. Las tienes justo encima de la caja de texto.


En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
algoritmo genetico « 1 2 »
.NET (C#, VB.NET, ASP)
juanlu16 18 13,449 Último mensaje 26 Abril 2008, 18:48 pm
por krackwar
Algoritmo genetico en VB
Programación Visual Basic
danicasasm 2 7,755 Último mensaje 23 Junio 2010, 03:51 am
por danicasasm
Algoritmo Genetico Python
Scripting
Camilo2001 0 4,727 Último mensaje 12 Marzo 2017, 04:21 am
por Camilo2001
Problema de las 8 reinas
Programación C/C++
Eskizoide 5 3,059 Último mensaje 12 Marzo 2018, 17:22 pm
por MAFUS
1000 reinas de ajedrez solucionado
Programación General
dav_s_mart 1 2,661 Último mensaje 25 Julio 2019, 18:32 pm
por Markski
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines