⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main_genetic.cpp

📁 uploading the file , the system will delete the file when time expires
💻 CPP
字号:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/timeb.h>
#include <string.h>

#include "rand32.h"
#include "metagenetic.h"

const int  nSwitches = 10;
const char * switches[] = {"-i","-o","-np","-ng","-npg","-ngg","-rc","-rm","-s","-v",NULL};

int  getSwitchNum(char * arg);
void fatal();

int main(int argc, char * argv[]) {

	struct timeb starttime; // will hold algorithm start time
	struct timeb stoptime;  // will hold algorithm stop time

	/* Do argument processing */
	double Rc         = 1.0;
	double Rm         = 0.2;
	int    Np         = 20;
	int    Ng         = 10;
	int    Np_gen     = 10;
	int    Ng_gen     = 10;
	int    verbosity  = 0;
	FILE * outfile    = stdout;
	unsigned int seed = time(NULL);
	char * filename   = NULL; // input file name is only mandatory parameter
	char * outname    = "(terminal)";

	int switchnum;

	/* Process the command-line arguments */
	for(int i=1;i<argc;i++) { 
		switchnum = getSwitchNum(argv[i]);
		switch(switchnum) {
		case 0:
			// -i
			// next arg should be the name of the input file
			if(argv[i+1]!=NULL) {
				filename = argv[i+1];
			} else {
				fprintf(stderr,"ERR: No argument specified to switch -i\n");
				fatal();
				return -1;
			}
			i++;
			break;
		case 1:
			// -o
			// next arg should be the name of the output file
			if(argv[i+1]!=NULL) {
				outfile = fopen(argv[i+1],"r");
				if(outfile!=NULL) {
					fprintf(stderr,"ERR: File %s exists. Cannot use for output.\n",argv[i+1]);
					return -1;
				} 
				outfile = fopen(argv[i+1],"w");
				if(outfile==NULL) {
					fprintf(stderr,"ERR: Unable to open file %s for writing.\n",argv[i+1]);
					return -1;
				}

				outname = argv[i+1];
				
			} else {
				fprintf(stderr,"ERR: No argument specified to switch -o\n");
				fatal();
				return -1;	
			}
			i++;
			break;
		case 2:
			// -np
			// next arg should be an integer
			if(argv[i+1]!=NULL) {
				Np = atoi(argv[i+1]);
				if(Np<1) {
					fprintf(stderr,"ERR: Parameter Np out of bounds: %s\n",argv[i+1]);
					return -1;
				}
			} else {
				fprintf(stderr,"ERR: No argument specified to switch -np\n");
				fatal();
				return -1;
			}
			i++;
			break;
		case 3:
			// -ng
			// next arg should be an integer
			if(argv[i+1]!=NULL) {
				Ng = atoi(argv[i+1]);
				if(Ng<1) {
					fprintf(stderr,"ERR: Parameter Ng out of bounds: %s\n",argv[i+1]);
					return -1;
				}
			} else {
				fprintf(stderr,"ERR: No argument specified to switch -ng\n");
				fatal();
				return -1;
			}
			i++;
			break;
		case 4:
			// -npg
			// next arg should be an integer
			if(argv[i+1]!=NULL) {
				Np_gen = atoi(argv[i+1]);
				if(Np_gen<1) {
					fprintf(stderr,"ERR: Parameter Np_g out of bounds: %s\n",argv[i+1]);
					return -1;
				}
			} else {
				fprintf(stderr,"ERR: No argument specified to switch -npg\n");
				fatal();
				return -1;
			}
			i++;
			break;
		case 5:
			// -ngg
			// next arg should be an integer
			if(argv[i+1]!=NULL) {
				Ng_gen = atoi(argv[i+1]);
				if(Ng_gen<1) {
					fprintf(stderr,"ERR: Parameter Ng_g out of bounds: %s\n",argv[i+1]);
					return -1;
				}
			} else {
				fprintf(stderr,"ERR: No argument specified to switch -ngg\n");
				fatal();
				return -1;
			}
			i++;
			break;
		case 6:
			// -rc
			// next arg should be a double
			if(argv[i+1]!=NULL) {
				Rc = strtod(argv[i+1],NULL);
				if(Rc<0.0||Rc>1.0) {
					fprintf(stderr,"ERR: Parameter Rc out of bounds: %s\n",argv[i+1]);
					return -1;
				}
			} else {
				fprintf(stderr,"ERR: No argument specified to switch -rc\n");
				fatal();
				return -1;
			}
			i++;
			break;
		case 7:
			// -rm
			// next arg should be a double
			if(argv[i+1]!=NULL) {
				Rm = strtod(argv[i+1],NULL);
				if(Rm<0.0||Rm>1.0) {
					fprintf(stderr,"ERR: Parameter Rm out of bounds: %s\n",argv[i+1]);
					return -1;
				}
			} else {
				fprintf(stderr,"ERR: No argument specified to switch -rm\n");
				fatal();
				return -1;
			}
			i++;
			break;
		case 8:
			// -s
			// next arg should be an integer
			if(argv[i+1]!=NULL) {
				seed = atoi(argv[i+1]);
				if(seed<0) {
					fprintf(stderr,"ERR: Parameter Seed out of bounds: %s\n",argv[i+1]);
					return -1;
				}
			} else {
				fprintf(stderr,"ERR: No argument specified to switch -s\n");
				fatal();
				return -1;
			}
			i++;
			break;
		case 9:
			// -v
			// next arg should be an integer
			if(argv[i+1]!=NULL) {
				verbosity = atoi(argv[i+1]);
				if(verbosity<0||verbosity>10) {
					fprintf(stderr,"ERR: Parameter Verbosity out of bounds: %s\n",argv[i+1]);
					return -1;
				}
			} else {
				fprintf(stderr,"ERR: No argument specified to switch -v\n");
				fatal();
				return -1;
			}
			i++;
			break;
		default:
			fprintf(stderr,"Unrecognized switch: %s\n",argv[i]);
			fatal();
			return -1;
		}
	}

	if(filename==NULL) {
		fatal();
		return -1;
	}

	Netlist nl;

	if(!ReadFromFile(filename,&nl)) {
		fprintf(stderr,"Error occurred when reading file: %s\n",filename);
		return -1;
	}

	// print the values of the arguments
	fprintf(outfile,"Using the following parameters:\n");
	fprintf(outfile,"Input file :  %s\n",filename);
	fprintf(outfile,"Output file:  %s\n",outname);
	fprintf(outfile,"Np (META)  :  %i\n",Np);
	fprintf(outfile,"Ng (META)  :  %i\n",Ng);
	fprintf(outfile,"Np (GEN)   :  %i\n",Np_gen);
	fprintf(outfile,"Ng (GEN)   :  %i\n",Ng_gen);
	fprintf(outfile,"Rc         :  %f\n",Rc);
	fprintf(outfile,"Rm         :  %f\n",Rm);
	fprintf(outfile,"Random Seed:  %i\n",seed);
	fprintf(outfile,"Verbosity  :  %i\n\n",verbosity);

	// seed the random number generator
	srand(seed);

	/* Call MetaGenetic */
	fprintf(outfile,"*** CALLING METAGENTIC() ***\n");
	// Clock-in the start-time
	ftime( &starttime );

	MetaGenetic(&nl,Rc,Rm,Np,Ng,Np_gen,Ng_gen,outfile,verbosity);

	// Clock-in stop time
	ftime( &stoptime );

	fprintf(outfile,"*** METAGENETIC() HAS RETURNED ***\n");

	// Prepare and print (to stdout, the screen) the running
	// time of the program

	int seconds = stoptime.time - starttime.time;
	int milliseconds = stoptime.millitm - starttime.millitm;
	if(milliseconds<0) {
		milliseconds+= 1000;
		seconds--;
	}

	// print running time

	fprintf(outfile, "\nRunning time of MetaGenetic(): %i seconds\t%i milliseconds.\n",
            seconds, milliseconds );

	/* closeup */

	fcloseall();
	return 0;

}

int getSwitchNum(char * arg) {
	for(int i=0;i<nSwitches;i++) {
		if(strcmp(switches[i],arg)==0) {
			return i;
		}
	}

	return -1;
}

void fatal() {
	fprintf(stderr,"Invalid syntax.\n");
	fprintf(stderr,"Correct usage is:\n");
	fprintf(stderr,"genetic [switch] [switch argument] [switch] [switch argument] ...\n\n");
	fprintf(stderr,"The -i switch and argument are mandatory.\n\n");
	fprintf(stderr,"Switch   Argument                       Bounds        Default\n");
	fprintf(stderr,"------   --------                       ------        -------\n");
	fprintf(stderr," -i      Input file name                <string>      NONE\n");
	fprintf(stderr," -o      Output file name               <string>      stdout\n");
	fprintf(stderr," -np     Meta-Genetic population size   [1,infinity)  20\n");
	fprintf(stderr," -ng     Meta-Genetic generations       [1,infinity)  10\n");
	fprintf(stderr," -npg    Genetic population size        [1,infinity)  10\n");
	fprintf(stderr," -ngg    Genetic generations            [1,infinity)  10\n");
	fprintf(stderr," -rc     Meta-Genetic crossover rate    [0.0,1.0]     1.0\n");
	fprintf(stderr," -rm     Meta-Genetic mutation rate     [0.0,1.0]     0.2\n");
	fprintf(stderr," -s      Random Seed                    [0,infinity)  (=time())\n");
	fprintf(stderr," -v      Verbosity                      [0,10]        0\n\n");

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -