/*********************************************************************/
/* 基本遗传分类系统 SCS.C */
/*********************************************************************/
#include <stdio.h>
#include<graphics.h>
#include <math.h>
/* #include "graph.c" */
static double oldrand[55];
static int jrand;
static double rndx2;
static int rndcalcflag;
const int maxposition=20;
const int maxclass=20;
const int wildcard=-1;
const int iterationsperblock=10000;
const int maxmating=10;
const double randomseed=0.3456;
/* 全局变量 */
struct classtype
{
int *c;
unsigned a;
double strength,bid,ebid;
unsigned matchflag;
int specificity;
} ;
struct classlist
{
int *clist;
int nactive;
} ;
struct poptype
{
struct classtype *classifier;
int nclassifier,nposition;
double pgeneral,cbid,bidsigma,bidtax,lifetax,bid1,bid2,ebid1,ebid2;
double sumstrength,maxstrength,avgstrength,minstrength;
};
struct trecord
{
int initialiteration,initialblock,iteration,block,reportperiod;
int gaperiod,consolereportperiod,plotreportperiod,nextplotreport;
int nextconsolereport,nextreport,nextga;
unsigned reportflag,gaflag,consolereportflag,plotreportflag;
};
struct erecord
{
int laddress,ldata,lsignal,address,output,classifieroutput;
unsigned *signal;
};
struct crecord
{
int winner,oldwinner;
unsigned bucketbrigadeflag;
};
struct rrecord
{
double reward,rewardcount,totalcount,count50,rewardcount50;
double proportionreward,proportionreward50;
int lastwinner;
};
struct mrecord
{
int mate1,mate2,mort1,mort2,sitecross;
};
struct grecord
{
double populationselect,pmutation,pcrossover;
int ncrossover,nmutation,crowdingfactor,crowdingsubpop,nselect;
struct mrecord *mating;
};
unsigned *envmessage;
struct poptype population;
struct classlist matchlist;
struct trecord timekeeprec;
struct erecord environrec;
struct crecord clearingrec;
struct rrecord reinforcementrec;
struct grecord garec;
FILE *rep;
FILE *pfile;
FILE *tfile;
FILE *efile;
FILE *cfile;
FILE *rfile;
FILE *gfile;
void initrepheader(FILE *);
void interactiveheader();
void initialization();
void detectors(struct erecord,unsigned *);
void writemessage(FILE *, unsigned *,int);
void reportdetectors(FILE *,unsigned *,int);
void reportheader(FILE *);
void report(FILE *);
void consolereport(struct rrecord);
int addtime(int,int);
void plotreport(FILE *,struct rrecord);
void inittimekeeper(FILE *,struct trecord);
void initreptimekeeper(FILE *,struct trecord);
void timekeeper(struct trecord);
void reporttime(FILE *,struct trecord);
void generatesignal(struct erecord);
int decode(unsigned *,int, int);
void multiplexeroutput(struct erecord);
void environment(struct erecord);
void initenvironment(FILE *,struct erecord);
void initrepenvironment(FILE *,struct erecord);
void writesignal(FILE *,unsigned *,int );
void reportenvironment(FILE *,struct erecord);
int randomchar(double);
void readcondition(FILE *,int *,double,int);
void readclassifier(FILE *,struct classtype,double,int);
int counterspecificity(int *, int);
void initclassifiers(FILE *,struct poptype);
void initrepclassifiers(FILE *,struct poptype);
void writecondition(FILE *,int *,int);
void writeclassifier(FILE *,struct classtype,int,int);
void reportclassifiers(FILE *rep,struct poptype population);
unsigned match(int *c,unsigned *m, int nposition);
void matchclassifiers(struct poptype,unsigned *,struct classlist);
unsigned initaoc();
void initrepaoc(FILE *,struct crecord);
int auction(struct poptype,struct classlist,int);
void clearinghouse(struct poptype,struct crecord);
void taxcollector(struct poptype);
void aoc(struct poptype,struct classlist,struct crecord);
void reportaoc(FILE *,struct crecord);
void initreinforcement(FILE *,struct rrecord);
void initrepreinforcement(FILE *,struct rrecord);
unsigned criterion(struct rrecord,struct erecord);
void payreward(struct poptype,struct rrecord,struct crecord);
void reportreinforcement(FILE *,struct rrecord);
void reinforcement(struct rrecord,struct poptype,struct crecord,struct erecord);
void advance(struct crecord);
void initga(FILE *,struct grecord, struct poptype);
void initrepga(FILE *,struct grecord);
int select(struct poptype);
int mutation(int,double,int);
int bmutation(int,double,int);
void crossover(struct classtype,struct classtype,struct classtype,struct classtype,
double,double,int,int, int,int);
int worstofn(struct poptype population, int n) ;
int matchcount(struct classtype,struct classtype,int);
int crowding(struct classtype,struct poptype,int,int);
void statistics(struct poptype);
void ga(struct grecord,struct poptype);
void initrandomnormaldeviate() ;
double randomnormaldeviate();
double rndreal(double,double) ;
double noise(double,double) ;
void skip(FILE *,int);
void advance_random() ;
int flip(double) ;
void randomize();
double randomperc();
int rnd(int, int) ;
void warmup_random(double);
double max(double,double);
double min(double,double);
double avg(double,double);
unsigned halt();
void initmalloc();
void freeall();
void initrepheader(FILE *rep)
{
fprintf(rep,"-----------------------------------\n");
fprintf(rep," 基本分类系统 \n");
fprintf(rep," 同济大学计算机系 \n");
fprintf(rep," 王小平 2000年7月 \n");
fprintf(rep,"-----------------------------------\n");
skip(rep,1);
}
void interactiveheader()
{
initrepheader(stdout);
}
void initialization()
{
interactiveheader();
randomize();
initrandomnormaldeviate();
if((cfile = fopen("c:\\ga\\cfile.txt","r")) == NULL)
{
fprintf(stderr,"Cannot open classifier file \n");
exit(-1);
}
if((efile = fopen("c:\\ga\\efile.txt","r")) == NULL)
{
fprintf(stderr,"Cannot open environment file \n");
exit(-1);
}
if((tfile = fopen("c:\\ga\\tfile.txt","r")) == NULL)
{
fprintf(stderr,"Cannot open timekeeper file \n");
exit(-1);
}
if((rfile = fopen("c:\\ga\\rfile.txt","r")) == NULL)
{
fprintf(stderr,"Cannot open reinforcement file \n");
exit(-1);
}
if((gfile = fopen("c:\\ga\\gfile.txt","r")) == NULL)
{
fprintf(stderr,"Cannot open GA file \n");
exit(-1);
}
if((rep = fopen("c:\\ga\\rep.txt","w")) == NULL)
{
fprintf(stderr,"Cannot write report file \n");
exit(-1);
}
if((pfile = fopen("c:\\ga\\pfile.txt","w")) == NULL)
{
fprintf(stderr,"Cannot write plot file \n");
exit(-1);
}
initmalloc();
initrepheader(rep);
initclassifiers(cfile,population);
initrepclassifiers(rep,population);
initenvironment(efile,environrec);
initrepenvironment(rep,environrec);
clearingrec.bucketbrigadeflag=initaoc();
clearingrec.winner=1; clearingrec.oldwinner=1;
initrepaoc(rep,clearingrec);
initreinforcement(rfile,reinforcementrec);
initrepreinforcement(rep,reinforcementrec);
inittimekeeper(tfile,timekeeprec);
initreptimekeeper(rep,timekeeprec);
initga(gfile,garec,population);
initrepga(rep,garec);
}
void detectors(struct erecord environrec,unsigned *envmessage)
{
int j;
for(j=0;j<=environrec.lsignal-1; j++)
envmessage[j]=environrec.signal[j];
}
void writemessage(FILE *rep, unsigned *mess,int lmessage)
{
int j;
for(j=lmessage-1;j<=0; j--)
fprintf(rep," %d",mess[j]);
}
void reportdetectors(FILE *rep,unsigned *envmessage,int nposition)
{
skip(rep,1);
fprintf(rep,"Environment message: ");
writemessage(rep,envmessage,nposition);
skip(rep,1);
}
void reportheader(FILE *rep)
{
skip(