📄 petrol.c
字号:
#include <stdio.h>#include <stdlib.h>#include <time.h>#include <unistd.h>#include "petrol.h"#include "utils.h"char *appName;static void Initialize(int argc, char **argv, Settings *s, Population *p);static void Usage(void);void main(int argc, char **argv) { Settings s; Population p; /* Setup appName for error routines */ strcreate(&appName, argv[0]); /* Initialize the universe */ Initialize(argc, argv, &s, &p); /* Go for it */ PopulationRun(&s, &p); exit(EXIT_SUCCESS);}void Initialize(int argc, char **argv, Settings *s, Population *p) { extern char *optarg; char c; int seed; /* Set the defaults */ s->verbose = FALSE; s->outputFile = NULL; s->packetSize = PETROL_PACKET_SIZE; p->popSize = PETROL_POP_SIZE; p->probCrossover = PETROL_PROB_CROSSOVER; p->probMutation = PETROL_PROB_MUTATION; p->numGenerations = PETROL_NUM_GEN; p->chromosomeLen = PETROL_CHROMOSOME_LEN; p->maximize = PETROL_MAXIMIZE; /* Set the seed to be a random number */ seed = (int)time(NULL); /* Process the command line arguments */ while ((c = getopt(argc, argv, "b:c:g:l:m:n:o:p:s:v")) != EOF) { switch (c) { case 'b': s->packetSize = atoi(optarg); if (s->packetSize < 1) { error("Initialization", "Block size must be greater than 0."); } break; case 'c': p->probCrossover = atof(optarg); if (p->probCrossover < 0 || p->probCrossover > 1) error("Initialization", "Probability of crossover must be between 0 and 1 inclusive."); break; case 'g': p->numGenerations = atoi(optarg); if (p->numGenerations < 1) error("Initialization", "Number of generations must be greater than 0."); break; case 'l': p->chromosomeLen = atoi(optarg); if (p->chromosomeLen < 1) error("Initialization", "Chromosome length must be greater than 0."); break; case 'm': p->probMutation = atof(optarg); if (p->probMutation < 0 || p->probMutation > 1) error("Initialization", "Probability of mutation must be between 0 and 1 inclusive."); break; case 'n': p->maximize = FALSE; break; case 'o': strcreate(&s->outputFile, optarg); break; case 'p': p->popSize = atoi(optarg); if (p->popSize < 1) error("Initialization", "Population size must be greater than 0."); if (p->popSize % 2) error("Initialization", "Population size must be even."); break; case 's': seed = atoi(optarg); break; case 'v': s->verbose = TRUE; break; default: Usage(); exit(EXIT_FAILURE); } } srandom(seed); ReportSetup(s);}void Usage(void) { fprintf(stderr, "Usage:\n\n%s [-b int] [-c float] [-g int] [-l int] [-m float] [-n] [-o string] [-p int] [-s int] [-v]\n\n", appName); fprintf(stderr, "-b PVM block size (default %d)\n", PETROL_PACKET_SIZE); fprintf(stderr, "-c Probability of crossover (default %g)\n", PETROL_PROB_CROSSOVER); fprintf(stderr, "-g Number of generations (default %d)\n", PETROL_NUM_GEN); fprintf(stderr, "-l Chromosome length (default %d)\n", PETROL_CHROMOSOME_LEN); fprintf(stderr, "-m Probability of mutation (default %g)\n", PETROL_PROB_MUTATION); fprintf(stderr, "-n Minimize (default FALSE)\n"); fprintf(stderr, "-o Output to a file (default stdout)\n"); fprintf(stderr, "-p Population size (default %d)\n", PETROL_POP_SIZE); fprintf(stderr, "-s Random seed (default current time)\n"); fprintf(stderr, "-v Print extra information (verbose)\n");}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -