00001
00009 #include <stdio.h>
00010 #include <stdlib.h>
00011 #include "cellularAutomata.h"
00012
00013
00015 #define MAX_CA_ITERATIONS 200
00016
00017
00022 typedef struct {
00024 int isClassifiedCorrectly;
00027 int nsteps;
00028 } SimResult;
00029
00030
00031
00032 void simulate( CA* curr , CA* next , int rule[] , SimResult* result );
00033 void usage();
00034 void correctClassification( int argc , char** argv );
00035
00036
00045 int main( int argc , char** argv ){
00046 if( argc < 2 ){
00047 usage();
00048 exit(-1);
00049 }
00050
00051
00052 if( strcmp(argv[1],"--class") == 0 ){
00053 correctClassification(argc,argv);
00054 }
00055 else{
00056 usage();
00057 exit(-1);
00058 }
00059
00060 return 0;
00061 }
00062
00063
00072 void
00073 correctClassification( int argc , char** argv ){
00074 if( argc != 6 ){
00075 usage();
00076 exit(-1);
00077 }
00078
00079 int* rule = getRule( argv[2] );
00080 int nTrials = atoi( argv[3] );
00081 int caSize = atoi( argv[4] );
00082 FILE* fp;
00083 if( (fp = fopen(argv[5],"w")) == NULL ){
00084 perror("correctClassification(): couldn't open output file");
00085 }
00086 CA* ca = createCA( caSize );
00087 CA* nextca = createCA( caSize );
00088 SimResult* result = (SimResult*) malloc( sizeof(SimResult) );
00089
00090
00091 int per, trial, correctTrials, avgIters;
00092 for( per=1 ; per<=99 ; per++ ){
00093
00094 correctTrials = 0;
00095 avgIters = 0;
00096 for( trial=0 ; trial<nTrials ; trial++ ){
00097 randomizeToPercentage( ca , per , 0 );
00098 simulate( ca , nextca , rule , result );
00099 avgIters += result->nsteps;
00100 if( result->isClassifiedCorrectly ){
00101 ++correctTrials;
00102 }
00103 }
00104
00105 fprintf( fp , "%f %f\n" , (double) correctTrials / nTrials
00106 , (double) avgIters / nTrials );
00107 }
00108
00109
00110 free( result );
00111 fclose( fp );
00112 freeCA( ca );
00113 freeCA( nextca );
00114 free( rule );
00115 }
00116
00117
00118
00132 void
00133 simulate( CA* curr , CA* next , int rule[] , SimResult* result ){
00134 int maj = majority( curr );
00135 int iters = 0;
00136 void* temp;
00137 while( iters < MAX_CA_ITERATIONS && !finished(curr) ){
00138 step( curr , next , rule );
00139 temp = curr;
00140 curr = next;
00141 next = temp;
00142 ++iters;
00143 }
00144 result->isClassifiedCorrectly = finished(curr) && maj == *curr->grid[1][1];
00145 result->nsteps = iters;
00146 }
00147
00148
00152 void usage(){
00153 printf("\
00154 USAGE: There are multiple ways to use this program which are described below.\n\
00155 empirical --class RULEFILE NTRIALS CASIZE RESULTFILE\n\
00156 Runs the experiment that determines the frequency with which the rule\n\
00157 contained in RULEFILE correctly classifies a CASIZExCASIZE CA over a\n\
00158 range of percentages. The experiment is averaged over NTRIALS trials,\n\
00159 since the generatation of the CAs based on a percentage doesn't *really*\n\
00160 give the perfect percentage, and the data is put into file with name\n\
00161 RESULTFILE.\n\
00162 ");
00163 }
00164