00001 #include "cellularAutomata.h"
00002
00003
00004 CA* randomizeToPercentage( CA* ca , int percent , int size ){
00005 if( ca == NULL ){
00006 ca = createCA( size );
00007 }
00008 int row, col;
00009 for( row=1 ; row<=ca->size ; row++ ){
00010 for( col=1 ; col<=ca->size ; col++ ){
00011 *ca->grid[row][col] = (rand() % 100) < percent;
00012 }
00013 }
00014
00015 return ca;
00016 }
00017
00018
00019 void cpCA( CA* dest , CA* src ){
00020 int row, col;
00021 for( row=1 ; row<=src->size ; row++ ){
00022 for( col=1 ; col<=src->size ; col++ ){
00023 *dest->grid[row][col] = *src->grid[row][col];
00024 }
00025 }
00026 }
00027
00028
00029 void
00030 freeCA( CA* ca ){
00031 int row, col;
00032
00033
00034 for( row=1 ; row<=ca->size ; row++ ){
00035 for( col=1 ; col<=ca->size ; col++ ){
00036 free( ca->grid[row][col] );
00037 }
00038 }
00039
00040
00041 for( row=0 ; row<ca->size+1 ; row++ ){
00042 free( ca->grid[row] );
00043 }
00044
00045 free( ca->grid );
00046 free( ca );
00047 }
00048
00049
00050 CA**
00051 randomCAs( int nCAs , int size ){
00052 CA** cas = (CA**) calloc( nCAs , sizeof(CA*) );
00053 int i;
00054 for( i=0 ; i<nCAs ; i++ ){
00055 cas[i] = randomCA( size );
00056 }
00057 return cas;
00058 }
00059
00060
00061 CA*
00062 randomCA( int size ){
00063 CA* ca = createCA( size );
00064 int r,c;
00065 for( r=1 ; r<=size ; r++ ){
00066 for( c=1 ; c<=size ; c++ ){
00067 *ca->grid[r][c] = rand() % 2;
00068 }
00069 }
00070 return ca;
00071 }
00072
00073
00074
00075 int*
00076 getRule( char* file ){
00077
00078 int* rule = (int*) calloc( 512 , sizeof(int) );
00079 char rawrule[512];
00080 FILE* rawrule_fp;
00081
00082
00083 if( (rawrule_fp = fopen(file,"r")) == NULL )
00084 perror( "fopen of raw rule file failed\n" );
00085 if( fgets(rawrule,513,rawrule_fp) == NULL )
00086 perror( "getline of raw rule failed\n" );
00087 fclose( rawrule_fp );
00088
00089
00090 int i;
00091 for( i=0 ; i<512 ; i++ ){
00092 rule[i] = rawrule[i] == '1';
00093 }
00094
00095
00096 return rule;
00097 }
00098
00099
00100 CA*
00101 getInitCA( char* file , int size ){
00102
00103 int length = size*size;
00104 char rawCA[length];
00105 CA* ca = createCA( size );
00106 FILE* rawCA_fp;
00107
00108
00109 if( (rawCA_fp = fopen(file,"r")) == NULL )
00110 perror( "getInitCA(): fopen of rawCA file failed" );
00111 if( fgets(rawCA,length+1,rawCA_fp) == NULL )
00112 perror( "getInitCA(): getline of rawCA file failed" );
00113 fclose( rawCA_fp );
00114
00115
00116 int r, c;
00117 for( r=1 ; r<=size ; r++ ){
00118 for( c=1 ; c<=size ; c++ ){
00119 *ca->grid[r][c] = rawCA[size*(r-1)+(c-1)] == '1';
00120 }
00121 }
00122
00123
00124 return ca;
00125 }
00126
00127
00128 int
00129 finished( CA* ca ){
00130 int r, c, sum = 0;
00131 for( r=1 ; r<=ca->size ; r++ ){
00132 for( c=1 ; c<=ca->size ; c++ ){
00133 sum += *ca->grid[r][c];
00134 }
00135 }
00136
00137 return sum == 0 || sum == ca->size*ca->size;
00138 }
00139
00140
00141 int
00142 aggregate( CA* ca ){
00143 int r, c, sum = 0;
00144 for( r=1 ; r<=ca->size ; r++ ){
00145 for( c=1 ; c<=ca->size ; c++ ){
00146 sum += *ca->grid[r][c];
00147 }
00148 }
00149 return sum;
00150 }
00151
00152
00153 int
00154 majority( CA* ca ){
00155 int sum = aggregate( ca );
00156 return 2*sum >= ca->size*ca->size;
00157 }
00158
00159
00160 CA*
00161 createCA( int size ){
00162
00163 CA* ca = (CA*) malloc( sizeof(CA) );
00164 ca->size = size;
00165
00166
00167 int row, col;
00168 ca->grid = (int***) calloc( size+2 , sizeof(int**) );
00169 for( row = 0 ; row < size+2 ; row++ ){
00170 ca->grid[row] = (int**) calloc( size+2 , sizeof(int*) );
00171 if( row >= 1 && row <= size ){
00172 for( col = 1 ; col <= size ; col++ ){
00173 ca->grid[row][col] = (int*) malloc( sizeof(int) );
00174 }
00175 }
00176 }
00177
00178
00179 int i;
00180 for( i=1 ; i<=size ; i++ ){
00181 ca->grid[i][0] = ca->grid[i][size];
00182 ca->grid[i][size+1] = ca->grid[i][1];
00183 ca->grid[0][i] = ca->grid[size][i];
00184 ca->grid[size+1][i] = ca->grid[1][i];
00185 }
00186
00187
00188 ca->grid[0][0] = ca->grid[size][size];
00189 ca->grid[0][size+1] = ca->grid[size][1];
00190 ca->grid[size+1][0] = ca->grid[1][size];
00191 ca->grid[size+1][size+1] = ca->grid[1][1];
00192
00193 return ca;
00194 }
00195
00196
00197 void
00198 printInternalCA( CA* ca ){
00199 int r,c;
00200 for( r = 0 ; r < ca->size+2 ; r++ ){
00201 for( c = 0 ; c < ca->size+2 ; c++ ){
00202 printf( "%i " , *(ca->grid[r][c]) );
00203 }
00204 printf( "\n" );
00205 }
00206 printf( "\n" );
00207 }
00208
00209
00210 void
00211 printCA( CA* ca ){
00212 int r,c;
00213 for( r = 1 ; r <= ca->size ; r++ ){
00214 for( c = 1 ; c <= ca->size ; c++ ){
00215 printf( "%i " , *(ca->grid[r][c]) );
00216 }
00217 printf( "\n" );
00218 }
00219 printf( "\n" );
00220 }
00221
00222
00223
00224 void
00225 step( CA* curr , CA* next , int* rules ){
00226 int r, c, rule;
00227 for( r = 1 ; r <= curr->size ; r++ ){
00228 for( c = 1 ; c <= curr->size ; c++ ){
00229 rule = 1 * *curr->grid[r-1][c-1]
00230 + 2 * *curr->grid[r-1][ c ]
00231 + 4 * *curr->grid[r-1][c+1]
00232 + 8 * *curr->grid[ r ][c-1]
00233 + 16 * *curr->grid[ r ][ c ]
00234 + 32 * *curr->grid[ r ][c+1]
00235 + 64 * *curr->grid[r+1][c-1]
00236 + 128 * *curr->grid[r+1][ c ]
00237 + 256 * *curr->grid[r+1][c+1];
00238 *next->grid[r][c] = rules[rule];
00239 }
00240 }
00241 next->size = curr->size;
00242 }
00243
00244
00245 double
00246 stepWithCount( CA* curr , CA* next , int* rules ){
00247 double sum = 0;
00248 int r, c, rule;
00249 for( r = 1 ; r <= curr->size ; r++ ){
00250 for( c = 1 ; c <= curr->size ; c++ ){
00251 rule = 1 * *curr->grid[r-1][c-1]
00252 + 2 * *curr->grid[r-1][ c ]
00253 + 4 * *curr->grid[r-1][c+1]
00254 + 8 * *curr->grid[ r ][c-1]
00255 + 16 * *curr->grid[ r ][ c ]
00256 + 32 * *curr->grid[ r ][c+1]
00257 + 64 * *curr->grid[r+1][c-1]
00258 + 128 * *curr->grid[r+1][ c ]
00259 + 256 * *curr->grid[r+1][c+1];
00260 *next->grid[r][c] = rules[rule];
00261 sum += (double) rules[rule];
00262 }
00263 }
00264 next->size = curr->size;
00265
00266 return sum;
00267 }