Guest User

Untitled

a guest
Jul 22nd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.27 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include <time.h>
  4. typedef struct train
  5. { int ends[2];
  6. } train;
  7.  
  8. int** allocate(int n)
  9. { int k,i;
  10. k=n;
  11. int **c=(int **)malloc(k*sizeof(int*));
  12. for( i=0;i<k;i++)
  13. { c[i] = (int *)malloc(k*sizeof(int));
  14. }
  15. return c;
  16. }
  17.  
  18. int*** allocatethreed(int n,int x,int y)
  19. { int i,j;
  20. int *allElements = malloc(x * y * n * sizeof(int));
  21. int ***array3D = malloc(x * sizeof(int **));
  22. for(i = 0; i < x; i++)
  23. {
  24. array3D[i] = malloc(y* sizeof(int *));
  25. for(j = 0; j < y; j++)
  26. {
  27. array3D[i][j] = allElements + (i * y * n) + (j * n);
  28. }
  29. }
  30. return array3D;
  31.  
  32. }
  33.  
  34. void initialize( int ***timegraph, int **graph,int n)
  35. {
  36. int i,j,k;
  37. for(i=0;i<n;i++)
  38. { for(j=0;j<n;j++)
  39. { for(k=0;k<(n*n/4);k++)
  40. { timegraph[k][i][j]=graph[i][j];
  41. }
  42. //printf(" %d ",graph[i][j]);
  43. }
  44. // printf("\n");
  45. }
  46. }
  47.  
  48. //int readint(const char* file_name)
  49. void ReadFromFile (const char* file_name,int *length,train ** to,int *tot,int ***graph)
  50. {
  51. FILE* file = fopen (file_name, "r");
  52. int i,j;
  53. fscanf (file, "%d",length);
  54. *graph = allocate(*length);
  55. for(i = 0; i < *length; i++)
  56. { for(j = 0; j < *length; j++)
  57. { fscanf (file, "%d",(*(*graph+i)+j));
  58. }
  59. }
  60. fscanf (file, "%d",tot);
  61. train *t = (train *)malloc((*tot) *sizeof(train));
  62. for(i=0;i<*tot;i++)
  63. { for(j=0;j< 2;j++)
  64. { fscanf(file, "%d", &(t[i].ends[j]));
  65. }
  66. }
  67. *to=t;
  68. fclose (file);
  69. }
  70.  
  71. void printmatrix(int** p,int x,int y) // to print matrice
  72. { int i,j;
  73. for( i=0;i<x;i++)
  74. { for(j=0;j<y;j++)
  75. { printf(" %d ",p[i][j]);
  76. }
  77. printf("\n");
  78. }
  79. }
  80. int isNotIn(int *t, int num,int n)
  81. { int i;
  82. //printf(" %d",num);
  83. for(i=0;i<n;i++)
  84. { if(t[i] == num)
  85. { return 0;
  86. }
  87. }
  88. return 1;
  89.  
  90. }
  91.  
  92. int findpath(int ***timegraph,int i,int j,int k,int *path,int tot,int num,int **graph)
  93. { int pathfound=0;
  94. // printf(" ddd");
  95. while(pathfound == 0 ) {
  96. printf(" ccc");
  97. if(timegraph[k][i][j] == 1 )
  98. { path[k]=j;
  99. timegraph[k][i][j]=1;
  100. path[k+1]=-1;
  101. pathfound=1;
  102. }
  103. else
  104. { //srand ( time(NULL) );
  105. int n = rand()%num;
  106. // printf(" ");
  107. int cond=0;
  108. if ( timegraph[k][i][n] == 1)
  109. { if( isNotIn(path,n,k) == 1)
  110. { cond = 1;
  111. }
  112. }
  113. int count = 0;
  114. while( cond != 1 && count <(2*num))
  115. { //srand ( time(NULL) );
  116. n = rand()%num;
  117. if ( timegraph[k][i][n] == 1)
  118. { if( isNotIn(path,n,k) == 1)
  119. { cond = 1;
  120. }
  121. }
  122.  
  123. }
  124. if(cond =0 )
  125. { if( isNotIn(graph[i],1,n) ) //no path from i to k exists;change i
  126. { pathfound =0;
  127. path[k-1]=0;
  128. // printf(" aaa");
  129. return pathfound;
  130. }
  131. else
  132. { path[k]=i; //path from i to k exists but is already occupied
  133. pathfound =findpath(timegraph,i,j,k+1,path,tot,num,graph);
  134. }
  135. }
  136. else
  137. { path[k]=n;
  138. timegraph[k][i][n]=1;
  139. pathfound =findpath(timegraph,n,j,k+1,path,tot,num,graph);
  140. printf(" bbb");
  141. }
  142. }
  143. }
  144. return pathfound;
  145.  
  146. }
  147. int allowed(int *route, int ***timegraph,int n)
  148. { int i;
  149. for(i=0;i<n;i++)
  150. { if(timegraph[i][route[i]][route[i+1]] == 1)
  151. { return 0;
  152. }
  153. }
  154. return 1;
  155. }
  156.  
  157. void copy(int* dest,int * source,int n)
  158. { int i;
  159. for(i=0;i<n;i++)
  160. {dest[i]=source[i];
  161. }
  162. }
  163. void update(int ***timegraph,int *route,int n)
  164. { int i;
  165. for(i=0;i<n;i++)
  166. { timegraph[i][route[i]][route[i+1]] = 1;
  167. }
  168. }
  169.  
  170.  
  171. void crossover(int ** graph,int*** paths,float prob,int n,int tot,int size,int*** children,train* t)
  172. { int i;
  173. for(i =0;i<size;i++)
  174. {
  175. float random = (float)(rand()%10)/10;
  176. printf("%f ", random);
  177. if(random < prob ) //crossover
  178. { printf(" bbb ");
  179. int ***timegraph;
  180. timegraph=allocatethreed(n,(n*n/4),n);
  181. initialize(timegraph,graph,n);
  182. int first = rand()%size;
  183. int second= rand()%size;
  184. int j;
  185. for(j=0;j< tot; j++)
  186. { int bin = rand()%2;
  187. if(bin == 1)
  188. { if( allowed(paths[first][j],timegraph,n))
  189. { copy(children[i][j],paths[first][j],n);
  190. update(timegraph,paths[first][j],n);
  191. }
  192. else
  193. { if( allowed(paths[second][j],timegraph,n))
  194. { copy(children[i][j],paths[second][j],n);
  195. update(timegraph,paths[second][j],n);
  196. }
  197. else
  198. { findpath(timegraph,t[j].ends[0],t[j].ends[1],0,children[i][j],tot,n,graph);
  199. }
  200. }
  201. }
  202. else
  203. { if( allowed(paths[second][j],timegraph,n))
  204. { copy(children[i][j],paths[second][j],n);
  205. update(timegraph,paths[second][j],n);
  206. }
  207. else
  208. { if( allowed(paths[first][j],timegraph,n))
  209. { copy(children[i][j],paths[first][j],n);
  210. update(timegraph,paths[first][j],n);
  211. }
  212. else
  213. { findpath(timegraph,t[j].ends[0],t[j].ends[1],0,children[i][j],tot,n,graph);
  214. }
  215. }
  216. }
  217. }
  218. }
  219. else // no crossover;just copy a random string
  220. { printf("aaa");
  221. int p = rand()%size,l;
  222. for(l=0;l<tot;l++)
  223. { copy(children[i][l],paths[p][l],n);
  224. }
  225. }
  226. }
  227. }
  228. int fitness(int** solution,int tot)
  229. { int i,j,sum=0;
  230. for(i=0;i<tot;i++)
  231. { j=0;
  232. while(solution[i][j] >0)
  233. { sum = sum+1;
  234. j++;
  235. }
  236. }
  237. return sum;
  238. }
  239. void reproduction(int*** parents,int ***children,int tot,int size)
  240. { int *far = (int *)malloc(k*sizeof(int));
  241. for(i=0;i<size;i++)
  242. { far[i]=fitness(parents[i],tot);
  243. }
  244.  
  245.  
  246. int main()
  247. {
  248. int **graph;
  249. int ***timegraph;
  250. int n,tot,i,j,size=8;
  251. train *t;
  252. //int length;
  253. ReadFromFile("input.txt",&n,&t,&tot,&graph);
  254. //printmatrix(graph,n);
  255. /*for(i=0;i<tot;i++)
  256. { for(j=0;j< 2;j++)
  257. { printf("%d ", t[i].ends[j]);
  258. }
  259. printf("\n");
  260. }*/
  261. timegraph=allocatethreed(n,(n*n/4),n);
  262. initialize(timegraph,graph,n);
  263. srand ( time(NULL) );
  264. int ***paths= allocatethreed(n,size,tot);
  265. for(i=0;i<size;i++)
  266. { for(j=0;j<tot;j++)
  267. { findpath(timegraph,t[j].ends[0],t[j].ends[1],0,paths[i][j],tot,n,graph);
  268. }
  269. printmatrix(paths[i],tot,n);
  270. printf("-----------\n");
  271. }
  272. int ***children= allocatethreed(n,size,tot);
  273. crossover(graph,paths,0.8,n,tot,size,children,t);
  274. printf("Children are \n");
  275. for(i=0;i<size;i++)
  276. { printmatrix(children[i],tot,n);
  277. int sum =fitness(children[i],tot);
  278. printf("Fitness : %d \n",sum);
  279. printf("----------\n");
  280. }
  281. return 0;
  282. }
Add Comment
Please, Sign In to add comment