Optimization with genetic algorithm


I’ve some problems in the application of genetic algorithm for network emitter calibration. I’m a PhD researcher and I’m working on emitter calibration for large networks with epanet and Matlab r2015b (x86). After some iterations (depending on population size of algorithm) the file epanet2.dll shows problems. I mean that after some iteration I have this error “error 305 cannot open hydraulics file” so I must stop the simulation, close matlab and copy/replace the .dll file.
Can someone help me?
(I’m working with 32bit epanet-toolkit and 32bit Matlab on Windows10 64bit)


1 Like

I believe the problem is caused by EPANET’s use of the C library function tmpnam() to create a new temporary hydraulics file name for each iteration of your genetic algorithm. The tmpnam() function generates a different string each time it is called, up to TMP_MAX times. If it is called more than TMP_MAX times, the behavior is implementation defined.TMP_MAX is defined in the C library header stdio.h which is 32767 for Microsoft C.

I can suggest two possible work-arounds you could try:

  1. Request that EPANET save the hydraulic results from each run to a file whose name you supply, even though you won’t be making use of the file. This can be done either in the input file you use for your analysis, by adding a line: HYDRAULICS SAVE filename to the [OPTIONS] section where filename is the full path name of the file you want to use. Or you could call ENsavehydfile(filename) prior to starting your genetic algorithm.
  2. Instead of calling ENsolveH() to evaluate your hydraulics at each iteration, use the call sequence:
    do {
        // retrieve hydraulic results here
    } while (tstep > 0);

and prior to starting your genetic algorithm call ENopenH(). This option will be the more efficient alternative.

Lew Rossman


The C library limit of 32767 is 2^15 - 1, or the largest signed 16 bit integer. You would think the C library would have been updated over the years to allow for a larger TMP_MAX. I have also run into this issue doing Monte Carlo simulations of different damage states of a network. The problem with ENintH-NrunH-ENnextH is that th network is loaded and you cannot add or delete components. This is an important feature if the optimization is on the network topology. I know that people have been workin for the 2.2 release to have the abbility to add and delete compoenets, but I do wonder how much time that will take computationally. The inp file is a simple text file that can be parsed into a set of lists and tables that can easily be modified written back to a inp file. I did have the benefit of my results being independant, so I could just run batches smaller than the TMP_MAX limit and combine them after the fact.

I cannot get the line: HYDRAULICS SAVE filename to the [OPTIONS] section to work. I keep getting a 201 error code. When specifying the filename how is it supposed to be formatted. Typically I’ll make most modifications in the GUI and export the updated inp file, however I cannot find where to add the HYDRAULICS SAVE line to the options.

Is it possible to modify the stdio.h TMP_MAX value so it is greater than 32767?

Hello. I have the same problem in Epanettools. How I can resolve this problem in python?