- 最後登錄
- 2024-11-17
- 在線時間
- 29046 小時
- 註冊時間
- 2011-12-11
- 閱讀權限
- 95
- 精華
- 3
- UID
- 10643381
- 帖子
- 3230
- 積分
- 24427 點
- 潛水值
- 78810 米
| 雷亂圈 發表於 2015-12-2 06:36 PM
傳進去的是動態配置而來的 - #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define print printf
- #define N 3
- #define M 8
- void mutation(double **parent , double pm , int n , int m , double ***child)
- {
- int i , j;
- double rd;
-
- srand(time(NULL));
- *child = (double**) malloc(sizeof(double**) * n);
-
- for(i = 0 ; i < n ; ++i)
- (*child)[i] = (double*) malloc(sizeof(double) * m);
-
- rd = (double) rand() / RAND_MAX;
-
-
- if(rd < pm)
- {
- int *mpoint = (int*) malloc(sizeof(int) * 100);
-
- for(i = 0 ; i < 100 ; ++i)
- mpoint[i] = (double) rand() / RAND_MAX * (m - 1) + 0.5;
- for(i = 0 ; i < n ; ++i)
- memcpy((*child)[i] , parent[i] , sizeof(double) * m);
-
- for(i = 0 ; i < 100 ; ++i)
- for(j = 0 ; j < n ; ++j)
- (*child)[j][mpoint[i]] = abs(parent[j][mpoint[i]] - 1);
- free(mpoint);
- }
- else
- {
- for(i = 0 ; i < n ; ++i)
- memcpy((*child)[i] , parent[i] , sizeof(double) * m);
- }
- }
- int main()
- {
- double **parent;
- double **child = NULL;
- double val = 0;
- int i , j;
-
- parent = (double**) malloc(sizeof(double*) * N);
-
- for(i = 0 ; i < N ; ++i)
- parent[i] = (double*) malloc(sizeof(double) * M);
- for(i = 0 ; i < N ; ++i)
- for(j = 0 ; j < M ; ++j)
- parent[i][j] = val++;
- mutation(parent , 0.3 , N , M , &child);
-
- for(i = 0 ; i < N ; ++i)
- {
- for(j = 0 ; j < M ; ++j)
- {
- print("%f " , child[i][j]);
- }
- putchar('\n');
- }
-
- for(i = 0 ; i < N ; ++i)
- free(parent[i]);
-
- free(parent);
- for(i = 0 ; i < N ; ++i)
- free(child[i]);
-
- free(child);
-
- return 0;
- }
複製代碼
... |
|