00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "EGlib.h"
00026
00027
00028
00029 static void sim_usage (char **argv)
00030 {
00031 fprintf (stdout, "Generate demand from a variable rate poisson process\n"
00032 "as described by the given file, for the prescribed time length\n");
00033 fprintf (stdout, "Usage: %s [options]\n", argv[0]);
00034 fprintf (stdout, "Options:\n");
00035 fprintf (stdout, " -s n initial seed for the RNG (you can give up to six seeds)\n");
00036 fprintf (stdout, " -t n time to run the simulation.\n");
00037 fprintf (stdout, " -f n file name.\n");
00038 }
00039
00040
00041
00042
00043 static int n_seeds = 0;
00044 static unsigned long seed[6] = {12345,12345,12345,12345,12345,12345};
00045 char const*file_name = 0;
00046 static double max_time = 1440;
00047 FILE* input;
00048
00049
00050
00051
00052 static inline int sim_parseargs (int argc,
00053 char **argv)
00054 {
00055 int c;
00056 while ((c = getopt (argc, argv, "f:t:s:")) != EOF)
00057 {
00058 switch (c)
00059 {
00060 case 'f':
00061 file_name = optarg;
00062 break;
00063 case 't':
00064 max_time = strtod (optarg,0);
00065 break;
00066 case 's':
00067 if(n_seeds < 6)
00068 {
00069 seed[n_seeds++] = (unsigned long) atol(optarg);
00070 }
00071 else
00072 {
00073 fprintf(stderr,"Seed %s ignored\n",optarg);
00074 }
00075 break;
00076 default:
00077 sim_usage (argv);
00078 return 1;
00079 }
00080 }
00081
00082 if (!file_name)
00083 {
00084 sim_usage (argv);
00085 return 1;
00086 }
00087 input = fopen(file_name,"r");
00088 if(!input)
00089 {
00090 fprintf(stderr, "fopen failed with code error %d (%s)\n", errno,
00091 strerror(errno));
00092 sim_usage (argv);
00093 return 1;
00094 }
00095
00096 if(EGrandSetGlobalSeed(seed))
00097 {
00098 sim_usage (argv);
00099 return 1;
00100 }
00101
00102 fprintf (stderr, "\tFile %s\n\tMax_time %lf\n\tSeed [%lu,%lu,%lu,%lu,%lu,%lu]\n",
00103 file_name, max_time, seed[0], seed[1], seed[2], seed[3], seed[4], seed[5]);
00104 return 0;
00105 }
00106
00107
00108
00109
00110
00111
00112 int main (int argc,char**argv)
00113 {
00114 int rval = sim_parseargs(argc,argv);
00115 EGsimVRPoisson_t data;
00116 double cur_t=0.0,t;
00117 EGlib_info();
00118 EGlib_version();
00119
00120 EGsigSet(rval,CLEANUP);
00121 EGsetLimits(3600.0,4294967295UL);
00122 CHECKRVAL(rval);
00123
00124
00125 EGsimVRPInit(&data);
00126 rval = EGsimVRPLoadFile(&data,input);
00127 CHECKRVAL(rval);
00128 fclose(input);
00129 while(cur_t < max_time)
00130 {
00131 t = EGsimVRPoisson(cur_t,&data);
00132 cur_t += t;
00133 fprintf(stdout,"%.3lf\n",cur_t);
00134 }
00135 CLEANUP:
00136 EGsimVRPClear(&data);
00137 return rval;
00138 }
00139