eg_simula2.ex.c

Go to the documentation of this file.
00001 /* EGlib "Efficient General Library" provides some basic structures and
00002  * algorithms commons in many optimization algorithms.
00003  *
00004  * Copyright (C) 2005 Daniel Espinoza and Marcos Goycoolea.
00005  * 
00006  * This library is free software; you can redistribute it and/or modify it
00007  * under the terms of the GNU Lesser General Public License as published by the
00008  * Free Software Foundation; either version 2.1 of the License, or (at your
00009  * option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful, but 
00012  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
00013  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
00014  * License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public License
00017  * along with this library; if not, write to the Free Software Foundation,
00018  * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
00019  * */
00020 /* ========================================================================= */
00021 /** @file
00022  * @ingroup EGsimula */
00023 /** @addtogroup EGsimula */
00024 /** @{ */
00025 #include "EGlib.h"
00026 
00027 /* ========================================================================= */
00028 /** @brief display usage of this program */
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 /** @name Global Variables */
00042 /*@{*/
00043 static int n_seeds = 0; /**< number of readed seeds */
00044 static unsigned long seed[6] = {12345,12345,12345,12345,12345,12345}; /**< default seed to use */
00045 char const*file_name = 0; /**< pointer to the file name to use */
00046 static double max_time = 1440;/**< maximum time for the simulation */
00047 FILE* input;/**< input stream */
00048 /*@}*/
00049 
00050 /* ========================================================================= */
00051 /** @brief parse input */
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   /* test that we have an input file */
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   /* set the seed */
00096   if(EGrandSetGlobalSeed(seed))
00097   {
00098     sim_usage (argv);
00099     return 1;
00100   }
00101   /* report options */
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 /** @brief this program read a generic network from a file, a given (set 
00110  * of) seed(s),  and a maximum simulation time, and output the overall 
00111  * statistics for the complete simulation */
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   /* set signal and limits */
00120   EGsigSet(rval,CLEANUP);
00121   EGsetLimits(3600.0,4294967295UL);
00122   CHECKRVAL(rval);
00123   /* now we initialize structures, read the file, set the maximum time ,
00124    * simulate, clean-up memory and exit */
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 /** @} */