Difference between revisions of "Tmp"

From assela Pathirana
Jump to navigationJump to search
 
(91 intermediate revisions by the same user not shown)
Line 1: Line 1:
=Structure of a program=
=Putting it all together -- Optimization and EPAnet=
{{box|This section contains some advanced stuff. Don't be discouraged if you don't understand all and difficult to complete it without the help of somebody else.}}
==Prerequisites==
# You should have completed the C++ programming primer preceeding this section.
# You have exposure to the EPAnet 2 software (graphical version is adequate).
# A basic understanding of Genetic Algorithms (Follow these links: [http://en.wikipedia.org/wiki/Genetic_Algorithm],[http://cs.felk.cvut.cz/~xobitko/ga/],[http://math.hws.edu/xJava/GA/] and spend some time if not.)
In this lesson, we push the abilities we have gained to the limit! We link two code libraries, namely, Evolving objects -- a versatile genetic algorithm code and EPAnet (toolkit) a pipe network calculation model by writing some relatively simple code and create a running program. The following section explains the problem we have selected to solve (please note that the problem itself is of secondary importance here, what we are trying to do is to hone the skills we have gained and build confidence to attack bigger coding projects).


Probably the best way to start learning a programming language is by writing a program. Therefore, here is our first program:
You can download the set of files needed for this exercise from : [[Image:cbcpp_EO.rar|EO.rar]].


<enscript lang=c>
==Problem==
#include <stdio.h>
[[Image:cbcpp_pipe_network1.jpg|The pipe network.|thumb|The problem network.]]
The figure on the left shows a water supply network with a reservoir, seven pipe segments conveying water and five junctions that have different demands. We want to compute the most economical pipe diameters for each segment while maintaining a minimum pressure head of 10 m at all junctions. You can open the [[image:cbcpp_pipe_network1.inp|input file]] in Epanet 2 software to view the network.  


main(){
Our plan is to use a Genetic Algorithm to optimize the pipe diameters. For this we need to connect Epanet Software to a genetic algorithm code.
printf("hello world.\n");
 
==Plan==
We will use the Epanet Toolkit, a programming interface designed to run Epanet 2.0 programmatically without the standard graphical interface. For the Genetic Algorithm part, we'll use Evolving objects a free and open source  package for evolutionary computations.
 
Evolving objects can be downloaded from [http://eodev.sourceforge.net/ eodev]] website. Epanet toolkit can be downloaded from [[http://www.epa.gov/nrmrl/wswrd/epanet.html US-EPA]]. However, for this exercise you may use the already downloaded versions of these tools. Download the file [[Image:cbcpp_EO.zip|cbcpp_EO.zip]] into your computer and extract it to a new folder (say <tt>E:\GA\EO</tt>). This will create four subfolders,
<pre>
code
data
eo-1.0
epanet_tools
</pre>
We use code folder to keep all the code we write (not the code libraries we use!). It already have the files:
<pre>
RealEA.cpp
real_value.h
</pre>
<tt>RealEA.cpp</tt> is a c++ program that can be used to access the EO Genetic algorithm functions. <tt>real_value.h</tt> has the <tt>cost function</tt> for the GA. We'll learn more about this later.
 
<tt>data</tt> folder where we shall keep all our data, has the water distribution network file.
 
<tt>eo-1.0</tt> and <tt>epanet_tools</tt> have the evolving objects and epanet toolkit code libraries.
 
==Solution==
We shall attack our problem in a piecemeal fashion, in the steps given below:
# Get EO to solve a small GA problem.
# Replace the cost function with our own.
# Use epanet_toolkit to run Epanet on our water supply network and use the results to evaluate a cost function.
 
Usually this step-by-step approach is less error-prone than attempting the whole task once.
 
==Running a GA==
Create a new folder called <tt>projects</tt> under the same folder that has sub-folders of <tt>code</tt>, <tt>data</tt>, etc. This is where we shall keep the visual studio express edition related (platform specific) stuff. Open visual C++ and reate a new empty project EPGA in that folder. Add the following files in the code folder to the project.
<pre>
RealEA.cpp
real_value.h
</pre>
 
When you try to compile the project, you will get the error message on the line:
<source lang=cpp>
#include <eo>
</source>
Indicating that the compiler can not include '<tt>eo</tt>'. To remedy this, we should include the path to the include file <tt>eo</tt> ("<tt>..\..\eo-1.0.1\src</tt>"). This can be done by additing it to: <tt>Edit-><project>Properties->C/C++->General->Additional include directories</tt>.
 
At this stage RealEA.cpp should comile successfully, but would cause errors at linking stage. The error messages would look like
<pre>
unresolved external symbol "public: virtual __thiscall eoFunctorStore::~eoFunctorStore(void)"
(??1eoFunctorStore@@UAE@XZ) referenced in function
"public: virtual void * __thiscall eoFunctorStore::`scalar deleting destructor'(unsigned int)"
(??_GeoFunctorStore@@UAEPAXI@Z)
</pre>
 
The reason for this is
#The compiler knows about eo library (by <tt>#include <eo></tt>), but
#the real library objects of eo needed for linking are missing
 
To rectify this, we should let the linker access to the necessary libraries. Before doing this we have to make a detour. First save your project/solution and close it.
===Building EO libraries===
The eo code  available for download does not have the libraries for windows built in, but they have the complete source and tools needed to build them. There is a visual C++ solution called <tt>eo.sln</tt> in the sub-folder <tt>win</tt>. Just build this solution (Project->Build solution). This will create the necessary libraries in the win.
 
One more point: It is beneficial to build the '''release''' version (not the '''debug''' version) of the libraries for performance reasons.
 
After this you will have the following libraries in the folder <tt>win\lib\release</tt>.
<pre>
eo.lib  eoes.lib  eoga.lib  eoutils.lib
</pre>
[[Image:cbcpp_linker-depend.jpg|thumb|Adding linker dependancies.]]
Now open your project again. In add the above four files as dependancies for the linker.
(<tt>Edit-><project>Properties->Linker->General->Additional Dependancies</tt>).
 
Then let the linker know where these are:
(<tt>Edit-><project>Properties->Linker->General->Additional Library Directories</tt>). Add something like <tt>..\..\eo-1.0.1\win\lib\release</tt>.
 
At this stage, you should be able to compile the project successfully. <tt>Debug->Start without Debugging</tt> should run the program, albeit with not-so-meaningful-at-the-moment results.
 
 
 
==Enable debugging==
If you try to debug your code at this stage, visual c++ 2005 will complain (as of 01:07, 9 April 2007 (JST)) that the binaries were not built with debug information. There is a [[how to enable debug|separate article]] on how to enable debugging in visual studio 2005.
 
==EO In-Action==
Now is a good time to have an idea about how our GA code works. Don't worry if you can not understand everything -- what is important is to have a general idea of how things work!
 
The actual code-in-action is very short, indeed. I have changed the comments a little bit.
<source lang=cpp>
  /* Many instructions to this program can be given on the command line.
    The following code understands what you have specified as command line arguments.
  */
  eoParser parser(argc, argv);  // for user-parameter reading
  eoState state;    // keeps all things allocated
 
  typedef eoReal<eoMinimizingFitness> EOT;
 
  // The evaluation fn - encapsulated into an eval counter for output
  eoEvalFuncPtr<EOT, double, const std::vector<double>&>
              mainEval( real_value );
  eoEvalFuncCounter<EOT> eval(mainEval);
  // the genotype - through a genotype initializer
  eoRealInitBounded<EOT>& init = make_genotype(parser, state, EOT());
  // Build the variation operator (any seq/prop construct)
  eoGenOp<EOT>& op = make_op(parser, state, init);
  // initialize the population - and evaluate
  // yes, this is representation indepedent once you have an eoInit
  eoPop<EOT>& pop  = make_pop(parser, state, init);
  // stopping criteria
  eoContinue<EOT> & term = make_continue(parser, state, eval);
  // output
  eoCheckPoint<EOT> & checkpoint = make_checkpoint(parser, state, eval, term);
  // algorithm (need the operator!)
  eoAlgo<EOT>& ea = make_algo_scalar(parser, state, eval, checkpoint, op);
  make_help(parser); //print help, if something is missing or user gives /h
  // evaluate intial population AFTER help and status in case it takes time
  apply<EOT>(eval, pop);
  // print it out
  cout << "Initial Population\n";
  pop.sortedPrintOn(cout);
  cout << endl;
 
  run_ea(ea, pop); // run the ea
 
  cout << "Final Population\n";
  pop.sortedPrintOn(cout);
  cout << endl;
</source>
 
You can get away without understanding a single line of code above!! The only critical part is the following function, specified in the header file <tt>real_value.h</tt>. In order to adopt these versatile algorithms to solve a problem of our choosing, only changing this header file is adequate.
 
==Fitness function==
<source lang=cpp>
double real_value(const std::vector<double>& _ind)
{
  double sum = 0;
  for (unsigned i = 0; i < _ind.size(); i++)
      sum += _ind[i] * _ind[i];
  return sqrt(sum);
}
}
</enscript>
</source>
<enscript lang=c>
The eo library expects this function to be present and uses to evaluate the individuals in the population for fitness.
// line comment
 
What happens here?
# Our genotype vector has (say) <tt>n</tt> number of items. The fitness function simply squares each of these and add them up and returns the square root. No rocket science here! Just, the rule here is <tt>larger the individuals -- better the fitness</tt>!! 
# The GA code does the rest,
 
Try running the algorithm. It will go on decreasing the cost function and exit at 100th generation. The default is 100 generations, in a moment, we'll get into the details on how to change the default behavior.
 
==Providing arguments==
 
Once you run the above code once successfully, check the folder that consists the executable file (<tt>project\EPGA\Debug</tt> folder). There should be a file <tt>EPGA.exe.status</tt> EO library creates this file, when you run the program. It lists all the changes you can make at the runtime to the GA. (Meaning, you don't have to recompile the program -- just indicate the parameters.) There are two ways of specifying parameters -- first is to list them in the command line. For example try the following in the dos prompt.
<source lang=bash>
EPGA.exe --help
</source>
It  will just print a long help message.  Then try
<source lang=bash>
EPGA.exe  --maxGen=1000
</source>
It will run a GA like before, but will stop only after 1000 generations.
<source lang=bash>
EPGA.exe  --maxGen=0 --steadyGen=20
</source>
will run the GA until 20 generations without any improvement.
 
So, you get the idea.
 
Now try this. First copy the file <tt> EPGA.exe.status</tt> to <tt>args.txt</tt> and edit the latter with notepad.
<source lang=dos>
> copy EPGA.exe.status args.txt
> notepad args.txt
</source>
Notice that this file has all the arguments that can be given at the command line. <tt>#</tt> in a line means the rest that follows is a comment. Note that almost all the argments are commented. Now make sure only the following items are uncommented.
<source lang=bash>
--vecSize=3 
--maxGen=0                           
--steadyGen=20 
</source>
 
Now, run the program with the following command.
<source lang=dos>
EPGA.exe @args.txt
</source>
Notice that this is equivalent to running
<source lang=dos>
EPGA.exe --vecSize=3  --maxGen=0  --steadyGen=20
</source>


/* block comment */
===Within Visual Studio===
</enscript>
To make things a bit easier for us, lets do the following. Move the file args.txt to the data folder.
<source lang=dos>
> move args.txt ..\..\data\.
</source>


{| class="codebox"
Then in your project in visual C++, add the file data.txt to <tt>resource files</tt> tab, in <tt>solutions explorer</tt>.  (This step is optional)
<enscript lang=c>
// my first program in C++


Finally, add the following
<pre>
@..\..\data\args.txt
</pre>
to the <tt>Command argments</tt> <tt>Configuration properties->Debugging</tt>.
Now within visual C++ itself, you can just modify the file <tt>args.txt</tt> to suit your needs, save it and re-run our program.
==Writing our own cost function==
Now let's turn into our original problem. We need to minimize the diameter of seven pipes while maintaining reasonable (<tt>>10m</tt>) pressure at all supply nodes. Lets forget the pressure part for a moment and concentrate on minimizing the diameter. (The answer here is obvious, if pressure is no concern, then the 'best' diameter is zero!! -- but let's live with this silly notion for the moment.)
Do the following changes:
* First we change the args.txt file to have
<source lang=bash>
--vecSize=7 # we have seven pipes, each diameter can be represented by a single real value.
--initBounds=7[0,500]            # -B : Bounds for variables -- pipe diameter within 0-500mm
--objectBounds=7[0,500]            # -B : Bounds for variables -- pipe diameter within 0-500mm
</source>
* Change the cost function <tt>real_value.h</tt> to the following:
<source lang=cpp>
#include <vector>
#include <iostream>
#include <iostream>
using namespace std;
using namespace std;
double real_value(const std::vector<double>& _ind)
{
  //GA returns diameter as ind_
  double length=1000;
  double factor=1.; //some factor so that cost=length=diameter*factor (lets keep things simple!)
  double dia,cost = 0;
  for (unsigned i = 0; i < _ind.size(); i++){
      cost+=_ind[i]*length*factor;
  }
  return cost/10000;
}
</source>


int main ()
If everything is all right, you will have some large initial cost, and a very small final cost.
 
 
Now that we have customized the GA to represent our silly optimization problem, it is but a small step to do the real job!
 
==In comes EPANet 2==
 
With the provided EPANET toolkit (in <tt>epanet_tools</tt> folder) there is a help file:  <tt>TOOLKIT.HLP</tt>. This is going to be our standard reference to various calls to epanet program via toolkit.
 
Let's do most of the changes in the <tt>real_value.h</tt> file and try to keep changes in <tt>RealEA.cpp</tt> to a minimum.
 
We will focus on several toolkit functions:
===ENOpen and ENClose===
;Declaration: <source lang=cpp>int ENopen( char* f1, char* f2, char* f3)</source>
;Description: Opens the Toolkit to analyze a particular distribution system.
 
;Declaration: <source lang=cpp>int Enclose(void)</source>
;Description:Closes down the Toolkit system (including all files being processed).
 
===ENSolveH===
;Declaration: <source lang=cpp>int ENsolveH( void )</source>
;Description: Runs a complete hydraulic simulation with results for all time periods written to the binary Hydraulics file.
 
===ENGetNodeValue===
;Declaration: <source lang=cpp>int  ENgetnodevalue( int index, int paramcode, float* value )</source>
;Description:Retrieves the value of a specific link parameter.
 
===ENSetLinkValue===
;Declaration: <source lang=cpp>int  ENsetlinkvalue( int index, int paramcode, float value )</source>
;Description:Sets the value of a parameter for a specific link.
 
==Call EPANet 2==
Do the following changes in RealEA.cpp.
<source lang=cpp>
  try
  {
  // first initialize the Epanet
  epanet_init(); // <-- new addition A function call to initialize epanet. we have to write this function.
 
  eoParser parser(argc, argv);  // for user-parameter reading
...
  // close Epanet
  epanet_close(); // <-- new addition A function call to close epanet. we have to write this function.
 
  }
  catch(exception& e)
</source>
 
Now let's do the major modifications in real_value.h
 
As the first stage:
<source lang=cpp>
#include <vector>
#include <iostream>
#include "epanet2.h"
using namespace std;
double dia_cost_factor=1.; //some factor so that cost=length=diameter*factor (lets keep things simple!)
 
/** A function that computes the cost. This is what the GA use to evaluate its populations */
double real_value(const std::vector<double>& _ind)
{
{
   cout << "Hello World!";
   //GA returns diameter as ind_
  return 0;
  double length=1000; /* All pipe lengths are equal */
 
  double dia,cost = 0;
  for (unsigned i = 0; i < _ind.size(); i++){
      cost+=_ind[i]*length*dia_cost_factor;
  }
  return cost/10000;
}
}
<\enscript>
| class="result" |
Hello World!
|}


The first panel shows the source code for our first program. The second one shows the result of the program once compiled and executed. The way to edit and compile a program depends on the compiler you are using. Depending on whether it has a Development Interface or not and on its version. Consult the compilers section and the manual or help included with your compiler if you have doubts on how to compile a C++ console program.
/* We open the epanet system with the necessary input file.  
A bit of hard coding here. But, lets live with that for the moment. */
void epanet_init(){
int ret;
char file[500]="../../data/network.inp";
char rept[500]="../../data/network.rep";


The previous program is the typical program that programmer apprentices write for the first time, and its result is the printing on screen of the "Hello World!" sentence. It is one of the simplest programs that can be written in C++, but it already contains the fundamental components that every C++ program has. We are going to look line by line at the code we have just written:
ret=ENopen(file,rept,"");
cout << "At opening Epanet retured : "<<ret<<'\n';


; <tt>// my first program in C++</tt>
}
: This is a comment line. All lines beginning with two slash signs (<tt>//</tt>) are considered comments and do not have any effect on the behavior of the program. The programmer can use them to include short explanations or observations within the source code itself. In this case, the line is a brief description of what our program is. <br />
/* Close the epanet system */
; <tt><nowiki>#include <iostream></nowiki></tt>
void epanet_close(){
: Lines beginning with a pound sign (<tt><nowiki>#</nowiki></tt>) are directives for the preprocessor. They are not regular code lines with expressions but indications for the compiler's preprocessor. In this case the directive <tt><nowiki>#include <iostream></nowiki></tt> tells the preprocessor to include the iostream standard file. This specific file (iostream) includes the declarations of the basic standard input-output library in C++, and it is included because its functionality is going to be used later in the program. <br />
int ret;
; <tt>using namespace std;</tt>
    ret=ENclose();
: All the elements of the standard C++ library are declared within what is called a namespace, the namespace with the name ''std''. So in order to access its functionality we declare with this expression that we will be using these entities. This line is very frequent in C++ programs that use the standard library, and in fact it will be included in most of the source codes included in these tutorials.<br />
cout << "At closing Epanet retured : "<<ret<<'\n';
; <tt>int main ()</tt>
: This line corresponds to the beginning of the definition of the main function. The main function is the point by where all C++ programs start their execution, independently of its location within the source code. It does not matter whether there are other functions with other names defined before or after it - the instructions contained within this function's definition will always be the first ones to be executed in any C++ program. For that same reason, it is essential that all C++ programs have a main function.The word main is followed in the code by a pair of parentheses (<tt>()</tt>). That is because it is a function declaration: In C++, what differentiates a function declaration from other types of expressions are these parentheses that follow its name. Optionally, these parentheses may enclose a list of parameters within them.Right after these parentheses we can find the body of the main function enclosed in braces (<tt>{}</tt>). What is contained within these braces is what the function does when it is executed.<br />
; <tt>cout << "Hello World";</tt>
: This line is a C++ statement. A statement is a simple or compound expression that can actually produce some effect. In fact, this statement performs the only action that generates a visible effect in our first program.<tt>cout</tt> represents the standard output stream in C++, and the meaning of the entire statement is to insert a sequence of characters (in this case the <tt>Hello World</tt> sequence of characters) into the standard output stream (which usually is the screen).<tt>cout</tt> is declared in the <tt>iostream</tt> standard file within the <tt>std</tt> namespace, so that's why we needed to include that specific file and to declare that we were going to use this specific namespace earlier in our code.Notice that the statement ends with a semicolon character (<tt><nowiki>;</nowiki></tt>). This character is used to mark the end of the statement and in fact it must be included at the end of all expression statements in all C++ programs (one of the most common syntax errors is indeed to forget to include some semicolon after a statement).<br />
; <tt>return 0;</tt>
: The return statement causes the main function to finish. return may be followed by a return code (in our example is followed by the return code ). A return code of 0 for the main function is generally interpreted as the program worked as expected without any errors during its execution. This is the most usual way to end a C++ program.<br />


You may have noticed that not all the lines of this program perform actions when the code is executed. There were lines containing only comments (those beginning by <tt>//</tt>). There were lines with directives for the compiler's preprocessor (those beginning by <tt><nowiki>#</nowiki></tt>). Then there were lines that began the declaration of a function (in this case, the main function) and, finally lines with statements (like the insertion into <tt>cout</tt>), which were all included within the block delimited by the braces (<tt>{}</tt>) of the main function.
}
</source>


The program has been structured in different lines in order to be more readable, but in C++, we do not have strict rules on how to separate instructions in different lines. For example, instead of


{| class="snippet"
To run the above you will have to
| class="code" |
# Add <tt>..\..\epanet_tools</tt> to additional include directories.
<span class="kw">int</span> main ()
# Add <tt>epanet2vc.lib</tt> to additional dependencies.
{
# Add <tt>..\..\epanet_tools</tt> to additional library directories (At this stage the application should compile, but will give a runtime error.)
  cout << <span class="str">" Hello World "</span><nowiki>;
# Add <tt>PATH=%PATH%;..\..\epanet_tools</tt> to debugging environment. (Project <tt>properties->Debugging->Environment</tt>)
  </nowiki><span class="kw">return</span> 0;
}
|}


We could have written:


{| class="snippet"
Run the application at this stage to make sure that the two epanet calls return zero (error free call signal).
| class="code" |
<span class="kw">int</span> main () { cout << <span class="str">"Hello World"</span><nowiki>; </nowiki><span class="kw">return</span> 0; }
|}


All in just one line and this would have had exactly the same meaning as the previous code.
Then add a function pressure_cost to real_value.h to compute the 'cost' of pressure deficiency. (Something like the one below)
<source lang=cpp>
/* Returns the pressure cost (penalty for pressure violations at demand nodes) based on epanet runs.
Prerequisites: The epanet system should be initialized before calling this function for the first time. */
double pressure_cost(vector<double> _ind){
int ret;
double cost;
for(unsigned int i=0;i<npipes;i++){
int index=-1;
ret=ENgetlinkindex(pipes[i],&index);
//cout << "At opening Epanet retured : "<<ret<<'\n';
ret=ENsetlinkvalue(index,EN_DIAMETER,_ind[i]);
//cout << "At opening Epanet retured : "<<ret<<'\n';
}
//now run the simulation
ret=ENsolveH();
//cout << "At solve Epanet retured : "<<ret<<'\n';
cost=0;
    //read the pressure values
for(unsigned int i=0;i<nnodes;i++){
int index=-1;
ret=ENgetnodeindex(nodes[i],&index);
float value;
//cout << "At ENgetnodeindex Epanet retured : "<<ret<<'\n';
ret=ENgetnodevalue(index,EN_PRESSURE,&value);
//cout << "At ENgetnodevalue Epanet retured : "<<ret<<'\n';
if(value<10){
cost+=pressue_cost_factor*(10-value); // if p<10m, set a proportional penalty.  
}
}
//cout << "At ENcloseH Epanet retured : "<<ret<<'\n';
return cost;
   
}
</source>


In C++, the separation between statements is specified with an ending semicolon (<tt><nowiki>;</nowiki></tt>) at the end of each one, so the separation in different code lines does not matter at all for this purpose. We can write many statements per line or write a single statement that takes many code lines. The division of code in different lines serves only to make it more legible and schematic for the humans that may read it.
The value of variable <tt>pressure_cost_factor</tt> should be carefully considered (against that of <tt>dia_cost_factor</tt>).  


Let us add an additional instruction to our first program:
Finally modify <tt> real_value</tt> function so that it will call the above pressure_cost function and add the cost to the total cost.


{| class="codebox"
At this stage you have a complete living-breathing program that join the power to evolving objects with Epanet.
| class="code" |
<span class="comm">// my second program in C++</span>
<span class="prep"><nowiki>#include <iostream></nowiki></span>
<span class="kw">using</span> <span class="kw">namespace</span> std;
<span class="kw">int</span> main ()
{
  cout << <span class="str">"Hello World! "</span><nowiki>;
  cout << </nowiki><span class="str">"I'm a C++ program"</span><nowiki>;
  </nowiki><span class="kw">return</span> 0;
}
| class="result" |
Hello World! I'm a C++ program
|}


In this case, we performed two insertions into cout in two different statements. Once again, the separation in different lines of code has been done just to give greater readability to the program, since main could have been perfectly valid defined this way:
==A touch of sophistication -- let's get rid of hard coding==
{{Wbox|This section is here mostly for the sake of completion. Ignore this section if you don't have time or inclination.}}
We can change the behavior of the GA without recompiling the code, thanks to the sophistication of the design of EO library. However, we have given up some of this flexibility in the way we have designed our cost function. We have hard coded a number of items:
# Name of the network file.
# Number of pipes in the network.
# Number of nodes.
# IDs of pipes and nodes.


{| class="snippet"
It is possible to make our program quite flexible in these aspects also. But it needs a bit of work. Let's see how it can be done. The first stage is to change the <tt>real_value.h</tt> so that instead of hard coded values, it can take values stored in variables. Then in the <tt>main</tt> function (<tt>RealEA.cpp</tt>) add the code necessary to read these from a text file supplied by user.
| class="code" |
<span class="kw">int</span> main () { cout << <span class="str">" Hello World! "</span><nowiki>; cout << </nowiki><span class="str">" I'm a C++ program "</span><nowiki>; </nowiki><span class="kw">return</span> 0; }
|}


We were also free to divide the code into more lines if we considered it more convenient:
Lets define the text file format as follows:
<pre>
<network file name>
<file name for the report to write into>
<no of pipes>
<PIPE_ID1>
...
<no of nodes>
<NODE_ID1>
...
</pre>
That means something like the file below
<pre>
..\..\data\network.inp
..\..\data\network.rpt
7
P1
P2
P3
P4
P5
P6
P7
5
J1
J2
J3
J4
J5
</pre>


{| class="snippet"
Then the modified program is as follows:
| class="code" |
<span class="kw">int</span> main ()
{
  cout <<
    <span class="str">"Hello World!"</span><nowiki>;
  cout
    << </nowiki><span class="str">"I'm a C++ program"</span><nowiki>;
  </nowiki><span class="kw">return</span> 0;
}
|}


And the result would again have been exactly the same as in the previous examples.
;<tt>real_value.h</tt>:
<source lang=cpp>
#include <vector>
#include <iostream>
#include "epanet2.h"
#include <fstream>
using namespace std;
#define MAX_PATH_LEN 500
#define MAX_LABEL_LEN 25
double pressure_cost(vector<double> _ind);
int npipes=7;


Preprocessor directives (those that begin by <tt><nowiki>#</nowiki></tt>) are out of this general rule since they are not statements. They are lines read and discarded by the preprocessor and do not produce any code by themselves. Preprocessor directives must be specified in their own line and do not have to end with a semicolon (<tt><nowiki>;</nowiki></tt>).
int nnodes=5;


===Comments===
char file[MAX_PATH_LEN];
char rept[MAX_PATH_LEN];
vector<string> nodes; // notice, now we use vectors instead of traditional arrays.
vector<string> pipes;


Comments are parts of the source code disregarded by the compiler. They simply do nothing. Their purpose is only to allow the programmer to insert notes or descriptions embedded within the source code.
  double dia_cost_factor=1.; //some factor so that cost=length=diameter*factor (lets keep things simple!)
  double pressue_cost_factor=1000000; //multiplier to 'map' pressue defficiency to cost.  
                                      // cost=(pressuredefficiency)*pmult


C++ supports two ways to insert comments:


{| class="snippet"
  /** read the text file specified by filename argument and obtain epanet related parameters */
| class="code" |
  void parse_epanet_para(char* filename){
<span class="comm">// line comment</span>
  cout << "I read epanet related data from "<<filename<<"\n"; // inform the user
  //open the file
<span class="comm">/* block comment */</span>
  ifstream myfile (filename);
|}
  if(!myfile.is_open()){ // this is important.
  cout << "I can not open the file:"<<filename <<" I quit!!\n";
  exit(1);
  }
  myfile >> file; //read the name of the file
  myfile >> rept; //read the name of the (new) report file
  myfile >> npipes; //number of pipes
  for(int i=0;i<npipes;i++){ // read those pipe ids
  char tmp[MAX_LABEL_LEN];
  myfile >>  tmp;
  pipes.push_back(tmp);
  }
myfile >> nnodes; //number of junctions
  for(int i=0;i<nnodes;i++){//those ids
  char tmp[MAX_LABEL_LEN];
  myfile >>  tmp;
  nodes.push_back(tmp);
  }
  }


The first of them, known as line comment, discards everything from where the pair of slash signs (<tt>//</tt>) is found up to the end of that same line. The second one, known as block comment, discards everything between the <tt>/*</tt> characters and the first appearance of the <tt><nowiki>*/</nowiki></tt> characters, with the possibility of including more than one line.<br />We are going to add comments to our second program:
double real_value(const std::vector<double>& _ind)
{
  // check for sanity
if(_ind.size()!=npipes){
//raise hell
cout << "Bloody murder!\n";
cout << "Number of pipes and chromosome size mismatch!\n";
exit(5);
}
  //GA returns diameter as ind_
  double length=1000;


{| class="codebox"
  double dia,cost = 0;
| class="code" |
 
<span class="comm">/* my second program in C++
  cost=pressure_cost(_ind);
    with more comments */</span>
  for (unsigned i = 0; i < _ind.size(); i++){
      cost+=_ind[i]*length*dia_cost_factor;
<span class="prep"><nowiki>#include <iostream></nowiki></span>
  }
  return cost/10000;
<span class="kw">using</span> <span class="kw">namespace</span> std;
}
 
<span class="kw">int</span> main ()
double pressure_cost(vector<double> _ind){
{
int ret;
  cout << <span class="str">"Hello World! "</span><nowiki>;     </nowiki><span class="comm">// prints Hello World!</span>
double cost;
  cout << <span class="str">"I'm a C++ program"</span><nowiki>; </nowiki><span class="comm">// prints I'm a C++ program</span>
for(unsigned int i=0;i<npipes;i++){
int index=-1;
  <span class="kw">return</span> 0;
char tmp[MAX_LABEL_LEN]; // this gimmick here is to convet a c++ string to a c style char*
}
strcpy(tmp,pipes[i].c_str()); // because epanet is writtin in old c, which does not accept strings.
| class="result" |
ret=ENgetlinkindex(tmp,&index);
Hello World! I'm a C++ program
//cout << "At opening Epanet retured : "<<ret<<'\n';
|}
ret=ENsetlinkvalue(index,EN_DIAMETER,_ind[i]);
//cout << "At opening Epanet retured : "<<ret<<'\n';
}
//now run the simulation
ret=ENsolveH();
//cout << "At solve Epanet retured : "<<ret<<'\n';
cost=0;
    //read the pressure values
for(unsigned int i=0;i<nnodes;i++){
int index=-1;
char tmp[MAX_LABEL_LEN]; // convert c++ string to c style char*  
strcpy(tmp,nodes[i].c_str());
ret=ENgetnodeindex(tmp,&index);
float value;
//cout << "At ENgetnodeindex Epanet retured : "<<ret<<'\n';
ret=ENgetnodevalue(index,EN_PRESSURE,&value);
//cout << "At ENgetnodevalue Epanet retured : "<<ret<<'\n';
if(value<10){
cost+=pressue_cost_factor*(10-value);
}
}
//cout << "At ENcloseH Epanet retured : "<<ret<<'\n';
return cost;
   
}
 
void epanet_init(){
int ret;
 
 
ret=ENopen(file,rept,"");
cout << "At opening Epanet retured : "<<ret<<'\n';
 
}
 
void epanet_close(){
int ret;
    ret=ENclose();
cout << "At closing Epanet retured : "<<ret<<'\n';
 
}
</source>
 
;<tt>RealEA.cpp</tt>:
<source lang=cpp>
#define _CRT_SECURE_NO_DEPRECATE
//above is to get rid of deprecation warnings of Microsoft compiler. Needed because we use strcpy() function.
#include <iostream>
#include <es/make_real.h>
#include "real_value.h"
#include <apply.h>
 
using namespace std;
typedef eoReal<eoMinimizingFitness> EOT;
void print_values(eoPop<EOT> pop);
int main_function(int argc, char* argv[]);
 
/** Notice that we have moved everything that was previously in main() to
main_function.
Now before GA related stuff is handled (by main_function),
We process the command argument list. Unlike the previous case, now the first argument, i.e.
the filename of the EPAnet related parameters, is mandatory.
Then we copy the rest of the arguments in argv to a new array argv_ and pass it to main_function.
From the GA viewpoint, nothing has changed. It receives a argument array. If there are no arguments in it, GA will
run with default parameters. Otherwise it will parse the argument array.
The first command line argument is separately passed parse_epanet_para function.
*/
int main(int argc,char *argv[]){
      /* argv[0] is always the name of the program. So, to run properly the program should have
      length of argv (i.e. argc) >=2
      If this is not the case, provide some help. */
if(argc<2){// no arguments provided at the command line
cout << "Usage: argv[0] <epanet_related_datafile> <EO related arguments ...>\n";
cout << "Format of epanet_related_datafile as follows.\n";
cout << "<network file name>\n";
cout << "<file name for the report to write into>\n";
cout << "<no of pipes>\n";
cout << "<PIPE_ID1>\n";
cout << "...\n";
cout << "<no of nodes>\n";
cout << "<NODE_ID1>\n";
cout << "...\n";
exit(1);
}
char* filename=argv[1]; // seperately copy argv[1] (first argument) to variable filename
char* argv_[MAX_PATH_LEN];
argv_[0]=argv[0];      // argv[0] is the calling program name, copy this as is.
for(int i=1;i<argc-1;i++){ // then copy the rest (argv[2], argv[3], ...) of arguments to new array
cerr << argv[i+1];
argv_[i]=argv[i+1];
}
argc--; // argc should be one less than before
//now parse the parameter file stright away!
parse_epanet_para(filename);
return main_function(argc,argv_); // now call main_function with new argc, argv_ pair.
}
int main_function(int argc, char* argv[])
{
 
  try
  {
  // first initialize the Epanet
  epanet_init();
 
  eoParser parser(argc, argv);  // for user-parameter reading
  eoState state; 
  eoEvalFuncPtr<EOT, double, const std::vector<double>&>
              mainEval( real_value );
  eoEvalFuncCounter<EOT> eval(mainEval);
  eoRealInitBounded<EOT>& init = make_genotype(parser, state, EOT());
  // Build the variation operator (any seq/prop construct)
  eoGenOp<EOT>& op = make_op(parser, state, init);
  // initialize the population - and evaluate
  // yes, this is representation indepedent once you have an eoInit
  eoPop<EOT>& pop  = make_pop(parser, state, init);
  // stopping criteria
  eoContinue<EOT> & term = make_continue(parser, state, eval);
  // output
  eoCheckPoint<EOT> & checkpoint = make_checkpoint(parser, state, eval, term);
  // algorithm (need the operator!)
  eoAlgo<EOT>& ea = make_algo_scalar(parser, state, eval, checkpoint, op);
  // to be called AFTER all parameters have been read!!!
  make_help(parser);
 
  // evaluate intial population AFTER help and status in case it takes time
  apply<EOT>(eval, pop);
  // print it out
  cout << "Initial Population\n";
  pop.sortedPrintOn(cout);
  cout << endl;
 
  cin.get();
  run_ea(ea, pop); // run the ea
 
  cout << "Final Population\n";
  pop.sortedPrintOn(cout);
  cout << endl;
 
  // close Epanet
  epanet_close();
 
  }
  catch(exception& e)
  {
    cout << e.what() << endl;
  }
return 1;
}


If you include comments within the source code of your programs without using the comment characters combinations <tt>//</tt>, <tt>/*</tt> or <tt><nowiki>*/</nowiki></tt>, the compiler will take them as if they were C++ expressions, most likely causing one or several error messages when you compile it.<br />
</source>

Latest revision as of 15:13, 9 April 2007

Putting it all together -- Optimization and EPAnet

Green info.gif

This section contains some advanced stuff. Don't be discouraged if you don't understand all and difficult to complete it without the help of somebody else.

Prerequisites

  1. You should have completed the C++ programming primer preceeding this section.
  2. You have exposure to the EPAnet 2 software (graphical version is adequate).
  3. A basic understanding of Genetic Algorithms (Follow these links: [1],[2],[3] and spend some time if not.)

In this lesson, we push the abilities we have gained to the limit! We link two code libraries, namely, Evolving objects -- a versatile genetic algorithm code and EPAnet (toolkit) a pipe network calculation model by writing some relatively simple code and create a running program. The following section explains the problem we have selected to solve (please note that the problem itself is of secondary importance here, what we are trying to do is to hone the skills we have gained and build confidence to attack bigger coding projects).

You can download the set of files needed for this exercise from : EO.rar.

Problem

The problem network.

The figure on the left shows a water supply network with a reservoir, seven pipe segments conveying water and five junctions that have different demands. We want to compute the most economical pipe diameters for each segment while maintaining a minimum pressure head of 10 m at all junctions. You can open the File:Cbcpp pipe network1.inp in Epanet 2 software to view the network.

Our plan is to use a Genetic Algorithm to optimize the pipe diameters. For this we need to connect Epanet Software to a genetic algorithm code.

Plan

We will use the Epanet Toolkit, a programming interface designed to run Epanet 2.0 programmatically without the standard graphical interface. For the Genetic Algorithm part, we'll use Evolving objects a free and open source package for evolutionary computations.

Evolving objects can be downloaded from eodev] website. Epanet toolkit can be downloaded from [US-EPA]. However, for this exercise you may use the already downloaded versions of these tools. Download the file cbcpp_EO.zip into your computer and extract it to a new folder (say E:\GA\EO). This will create four subfolders,

code
data
eo-1.0
epanet_tools

We use code folder to keep all the code we write (not the code libraries we use!). It already have the files:

RealEA.cpp
real_value.h

RealEA.cpp is a c++ program that can be used to access the EO Genetic algorithm functions. real_value.h has the cost function for the GA. We'll learn more about this later.

data folder where we shall keep all our data, has the water distribution network file.

eo-1.0 and epanet_tools have the evolving objects and epanet toolkit code libraries.

Solution

We shall attack our problem in a piecemeal fashion, in the steps given below:

  1. Get EO to solve a small GA problem.
  2. Replace the cost function with our own.
  3. Use epanet_toolkit to run Epanet on our water supply network and use the results to evaluate a cost function.

Usually this step-by-step approach is less error-prone than attempting the whole task once.

Running a GA

Create a new folder called projects under the same folder that has sub-folders of code, data, etc. This is where we shall keep the visual studio express edition related (platform specific) stuff. Open visual C++ and reate a new empty project EPGA in that folder. Add the following files in the code folder to the project.

 
RealEA.cpp
real_value.h

When you try to compile the project, you will get the error message on the line:

#include <eo>

Indicating that the compiler can not include 'eo'. To remedy this, we should include the path to the include file eo ("..\..\eo-1.0.1\src"). This can be done by additing it to: Edit-><project>Properties->C/C++->General->Additional include directories.

At this stage RealEA.cpp should comile successfully, but would cause errors at linking stage. The error messages would look like

unresolved external symbol "public: virtual __thiscall eoFunctorStore::~eoFunctorStore(void)" 
(??1eoFunctorStore@@UAE@XZ) referenced in function 
"public: virtual void * __thiscall eoFunctorStore::`scalar deleting destructor'(unsigned int)" 
(??_GeoFunctorStore@@UAEPAXI@Z)

The reason for this is

  1. The compiler knows about eo library (by #include <eo>), but
  2. the real library objects of eo needed for linking are missing

To rectify this, we should let the linker access to the necessary libraries. Before doing this we have to make a detour. First save your project/solution and close it.

Building EO libraries

The eo code available for download does not have the libraries for windows built in, but they have the complete source and tools needed to build them. There is a visual C++ solution called eo.sln in the sub-folder win. Just build this solution (Project->Build solution). This will create the necessary libraries in the win.

One more point: It is beneficial to build the release version (not the debug version) of the libraries for performance reasons.

After this you will have the following libraries in the folder win\lib\release.

eo.lib  eoes.lib  eoga.lib  eoutils.lib
Adding linker dependancies.

Now open your project again. In add the above four files as dependancies for the linker. (Edit-><project>Properties->Linker->General->Additional Dependancies).

Then let the linker know where these are: (Edit-><project>Properties->Linker->General->Additional Library Directories). Add something like ..\..\eo-1.0.1\win\lib\release.

At this stage, you should be able to compile the project successfully. Debug->Start without Debugging should run the program, albeit with not-so-meaningful-at-the-moment results.


Enable debugging

If you try to debug your code at this stage, visual c++ 2005 will complain (as of 01:07, 9 April 2007 (JST)) that the binaries were not built with debug information. There is a separate article on how to enable debugging in visual studio 2005.

EO In-Action

Now is a good time to have an idea about how our GA code works. Don't worry if you can not understand everything -- what is important is to have a general idea of how things work!

The actual code-in-action is very short, indeed. I have changed the comments a little bit.

  /* Many instructions to this program can be given on the command line. 
     The following code understands what you have specified as command line arguments. 
  */
  eoParser parser(argc, argv);  // for user-parameter reading
  eoState state;    // keeps all things allocated
  
  typedef eoReal<eoMinimizingFitness> EOT;

  // The evaluation fn - encapsulated into an eval counter for output 
  eoEvalFuncPtr<EOT, double, const std::vector<double>&> 
               mainEval( real_value );
  eoEvalFuncCounter<EOT> eval(mainEval);
  // the genotype - through a genotype initializer
  eoRealInitBounded<EOT>& init = make_genotype(parser, state, EOT());
  // Build the variation operator (any seq/prop construct)
  eoGenOp<EOT>& op = make_op(parser, state, init);
  // initialize the population - and evaluate
  // yes, this is representation indepedent once you have an eoInit
  eoPop<EOT>& pop   = make_pop(parser, state, init);
  // stopping criteria
  eoContinue<EOT> & term = make_continue(parser, state, eval);
  // output
  eoCheckPoint<EOT> & checkpoint = make_checkpoint(parser, state, eval, term);
  // algorithm (need the operator!)
  eoAlgo<EOT>& ea = make_algo_scalar(parser, state, eval, checkpoint, op);
  make_help(parser); //print help, if something is missing or user gives /h 
  // evaluate intial population AFTER help and status in case it takes time
  apply<EOT>(eval, pop);
  // print it out
  cout << "Initial Population\n";
  pop.sortedPrintOn(cout);
  cout << endl;

  run_ea(ea, pop); // run the ea

  cout << "Final Population\n";
  pop.sortedPrintOn(cout);
  cout << endl;

You can get away without understanding a single line of code above!! The only critical part is the following function, specified in the header file real_value.h. In order to adopt these versatile algorithms to solve a problem of our choosing, only changing this header file is adequate.

Fitness function

double real_value(const std::vector<double>& _ind)
{
  double sum = 0;
  for (unsigned i = 0; i < _ind.size(); i++)
      sum += _ind[i] * _ind[i];
  return sqrt(sum);
}

The eo library expects this function to be present and uses to evaluate the individuals in the population for fitness.

What happens here?

  1. Our genotype vector has (say) n number of items. The fitness function simply squares each of these and add them up and returns the square root. No rocket science here! Just, the rule here is larger the individuals -- better the fitness!!
  2. The GA code does the rest,

Try running the algorithm. It will go on decreasing the cost function and exit at 100th generation. The default is 100 generations, in a moment, we'll get into the details on how to change the default behavior.

Providing arguments

Once you run the above code once successfully, check the folder that consists the executable file (project\EPGA\Debug folder). There should be a file EPGA.exe.status EO library creates this file, when you run the program. It lists all the changes you can make at the runtime to the GA. (Meaning, you don't have to recompile the program -- just indicate the parameters.) There are two ways of specifying parameters -- first is to list them in the command line. For example try the following in the dos prompt.

EPGA.exe --help

It will just print a long help message. Then try

EPGA.exe  --maxGen=1000

It will run a GA like before, but will stop only after 1000 generations.

EPGA.exe  --maxGen=0 --steadyGen=20

will run the GA until 20 generations without any improvement.

So, you get the idea.

Now try this. First copy the file EPGA.exe.status to args.txt and edit the latter with notepad.

> copy EPGA.exe.status args.txt
> notepad args.txt

Notice that this file has all the arguments that can be given at the command line. # in a line means the rest that follows is a comment. Note that almost all the argments are commented. Now make sure only the following items are uncommented.

--vecSize=3  
--maxGen=0                             
--steadyGen=20

Now, run the program with the following command.

EPGA.exe @args.txt

Notice that this is equivalent to running

EPGA.exe --vecSize=3  --maxGen=0  --steadyGen=20

Within Visual Studio

To make things a bit easier for us, lets do the following. Move the file args.txt to the data folder.

> move args.txt ..\..\data\.

Then in your project in visual C++, add the file data.txt to resource files tab, in solutions explorer. (This step is optional)

Finally, add the following

@..\..\data\args.txt

to the Command argments Configuration properties->Debugging.

Now within visual C++ itself, you can just modify the file args.txt to suit your needs, save it and re-run our program.

Writing our own cost function

Now let's turn into our original problem. We need to minimize the diameter of seven pipes while maintaining reasonable (>10m) pressure at all supply nodes. Lets forget the pressure part for a moment and concentrate on minimizing the diameter. (The answer here is obvious, if pressure is no concern, then the 'best' diameter is zero!! -- but let's live with this silly notion for the moment.)

Do the following changes:

  • First we change the args.txt file to have
--vecSize=7 # we have seven pipes, each diameter can be represented by a single real value. 
--initBounds=7[0,500]             # -B : Bounds for variables -- pipe diameter within 0-500mm
--objectBounds=7[0,500]             # -B : Bounds for variables -- pipe diameter within 0-500mm
  • Change the cost function real_value.h to the following:
#include <vector>
#include <iostream>
using namespace std;
double real_value(const std::vector<double>& _ind)
{
  //GA returns diameter as ind_
  double length=1000;
  double factor=1.; //some factor so that cost=length=diameter*factor (lets keep things simple!)
  double dia,cost = 0;
  for (unsigned i = 0; i < _ind.size(); i++){
      cost+=_ind[i]*length*factor;
  }
  return cost/10000;
}

If everything is all right, you will have some large initial cost, and a very small final cost.


Now that we have customized the GA to represent our silly optimization problem, it is but a small step to do the real job!

In comes EPANet 2

With the provided EPANET toolkit (in epanet_tools folder) there is a help file: TOOLKIT.HLP. This is going to be our standard reference to various calls to epanet program via toolkit.

Let's do most of the changes in the real_value.h file and try to keep changes in RealEA.cpp to a minimum.

We will focus on several toolkit functions:

ENOpen and ENClose

Declaration
int ENopen( char* f1, char* f2, char* f3)
Description
Opens the Toolkit to analyze a particular distribution system.
Declaration
int Enclose(void)
Description
Closes down the Toolkit system (including all files being processed).

ENSolveH

Declaration
int ENsolveH( void )
Description
Runs a complete hydraulic simulation with results for all time periods written to the binary Hydraulics file.

ENGetNodeValue

Declaration
int  ENgetnodevalue( int index, int paramcode, float* value )
Description
Retrieves the value of a specific link parameter.

ENSetLinkValue

Declaration
int  ENsetlinkvalue( int index, int paramcode, float value )
Description
Sets the value of a parameter for a specific link.

Call EPANet 2

Do the following changes in RealEA.cpp.

  try
  {
  // first initialize the Epanet
  epanet_init(); // <-- new addition A function call to initialize epanet. we have to write this function. 

  eoParser parser(argc, argv);  // for user-parameter reading
...
  // close Epanet
  epanet_close(); // <-- new addition A function call to close epanet. we have to write this function. 

  }
  catch(exception& e)

Now let's do the major modifications in real_value.h

As the first stage:

#include <vector>
#include <iostream>
#include "epanet2.h"
using namespace std;
double dia_cost_factor=1.; //some factor so that cost=length=diameter*factor (lets keep things simple!)

/** A function that computes the cost. This is what the GA use to evaluate its populations */
double real_value(const std::vector<double>& _ind)
{
  //GA returns diameter as ind_
  double length=1000; /* All pipe lengths are equal */

  double dia,cost = 0;
  for (unsigned i = 0; i < _ind.size(); i++){
      cost+=_ind[i]*length*dia_cost_factor;
  }
  return cost/10000;
}

/* We open the epanet system with the necessary input file. 
A bit of hard coding here. But, lets live with that for the moment. */
void epanet_init(){
	int ret;
	char file[500]="../../data/network.inp";
	char rept[500]="../../data/network.rep";

	ret=ENopen(file,rept,"");
	cout << "At opening Epanet retured : "<<ret<<'\n';

}
/* Close the epanet system */
void epanet_close(){
	int ret;
    ret=ENclose();
	cout << "At closing Epanet retured : "<<ret<<'\n';

}


To run the above you will have to

  1. Add ..\..\epanet_tools to additional include directories.
  2. Add epanet2vc.lib to additional dependencies.
  3. Add ..\..\epanet_tools to additional library directories (At this stage the application should compile, but will give a runtime error.)
  4. Add PATH=%PATH%;..\..\epanet_tools to debugging environment. (Project properties->Debugging->Environment)


Run the application at this stage to make sure that the two epanet calls return zero (error free call signal).

Then add a function pressure_cost to real_value.h to compute the 'cost' of pressure deficiency. (Something like the one below)

/* Returns the pressure cost (penalty for pressure violations at demand nodes) based on epanet runs.
Prerequisites: The epanet system should be initialized before calling this function for the first time. */
double pressure_cost(vector<double> _ind){
	int ret;
	double cost;
	for(unsigned int i=0;i<npipes;i++){
		int index=-1;
		ret=ENgetlinkindex(pipes[i],&index);
		//cout << "At opening Epanet retured : "<<ret<<'\n';
		ret=ENsetlinkvalue(index,EN_DIAMETER,_ind[i]);
		//cout << "At opening Epanet retured : "<<ret<<'\n';
	}
	//now run the simulation
		ret=ENsolveH();
		//cout << "At solve Epanet retured : "<<ret<<'\n';
		cost=0;
    //read the pressure values
	for(unsigned int i=0;i<nnodes;i++){
		int index=-1;
		ret=ENgetnodeindex(nodes[i],&index);
		float value;
		//cout << "At ENgetnodeindex Epanet retured : "<<ret<<'\n';
		ret=ENgetnodevalue(index,EN_PRESSURE,&value);
		//cout << "At ENgetnodevalue Epanet retured : "<<ret<<'\n';
		if(value<10){
			cost+=pressue_cost_factor*(10-value); // if p<10m, set a proportional penalty. 
		}
	}
	
	//cout << "At ENcloseH Epanet retured : "<<ret<<'\n';
	return cost;
		
	    
}

The value of variable pressure_cost_factor should be carefully considered (against that of dia_cost_factor).

Finally modify real_value function so that it will call the above pressure_cost function and add the cost to the total cost.

At this stage you have a complete living-breathing program that join the power to evolving objects with Epanet.

A touch of sophistication -- let's get rid of hard coding

Red warning.gif

This section is here mostly for the sake of completion. Ignore this section if you don't have time or inclination.

We can change the behavior of the GA without recompiling the code, thanks to the sophistication of the design of EO library. However, we have given up some of this flexibility in the way we have designed our cost function. We have hard coded a number of items:

  1. Name of the network file.
  2. Number of pipes in the network.
  3. Number of nodes.
  4. IDs of pipes and nodes.

It is possible to make our program quite flexible in these aspects also. But it needs a bit of work. Let's see how it can be done. The first stage is to change the real_value.h so that instead of hard coded values, it can take values stored in variables. Then in the main function (RealEA.cpp) add the code necessary to read these from a text file supplied by user.

Lets define the text file format as follows:

<network file name>
<file name for the report to write into>
<no of pipes>
<PIPE_ID1>
...
<no of nodes>
<NODE_ID1>
...

That means something like the file below

..\..\data\network.inp
..\..\data\network.rpt
7
P1
P2
P3
P4
P5
P6
P7
5
J1
J2
J3
J4
J5

Then the modified program is as follows:

real_value.h
#include <vector>
#include <iostream>
#include "epanet2.h"
#include <fstream>
using namespace std;
#define MAX_PATH_LEN 500
#define MAX_LABEL_LEN 25
double pressure_cost(vector<double> _ind);
int npipes=7;

int nnodes=5;

char file[MAX_PATH_LEN];
char rept[MAX_PATH_LEN];
vector<string> nodes; // notice, now we use vectors instead of traditional arrays. 
vector<string> pipes;

  double dia_cost_factor=1.; //some factor so that cost=length=diameter*factor (lets keep things simple!)
  double pressue_cost_factor=1000000; //multiplier to 'map' pressue defficiency to cost. 
                                      // cost=(pressuredefficiency)*pmult


  /** read the text file specified by filename argument and obtain epanet related parameters */
  void parse_epanet_para(char* filename){
	  cout << "I read epanet related data from "<<filename<<"\n"; // inform the user
	  //open the file
	  ifstream myfile (filename);
	   if(!myfile.is_open()){ // this is important. 
		   cout << "I can not open the file:"<<filename <<" I quit!!\n";
		   exit(1);
	   }
	   myfile >> file; //read the name of the file
	   myfile >> rept; //read the name of the (new) report file
	   myfile >> npipes; //number of pipes
	   for(int i=0;i<npipes;i++){ // read those pipe ids
		   char tmp[MAX_LABEL_LEN];
		   myfile >>  tmp;
		   pipes.push_back(tmp);
	   }
		myfile >> nnodes; //number of junctions
	   for(int i=0;i<nnodes;i++){//those ids
		   char tmp[MAX_LABEL_LEN];
		   myfile >>  tmp;
		   nodes.push_back(tmp);
	   }
  }

double real_value(const std::vector<double>& _ind)
{
  // check for sanity 
	if(_ind.size()!=npipes){
		//raise hell
		cout << "Bloody murder!\n";
		cout << "Number of pipes and chromosome size mismatch!\n";
		exit(5);
	}
  //GA returns diameter as ind_
  double length=1000;

  double dia,cost = 0;

  cost=pressure_cost(_ind);
  for (unsigned i = 0; i < _ind.size(); i++){
      cost+=_ind[i]*length*dia_cost_factor;
  }
  return cost/10000;
}

double pressure_cost(vector<double> _ind){
	int ret;
	double cost;
	for(unsigned int i=0;i<npipes;i++){
		int index=-1;
		char tmp[MAX_LABEL_LEN]; // this gimmick here is to convet a c++ string to a c style char*
		strcpy(tmp,pipes[i].c_str()); // because epanet is writtin in old c, which does not accept strings.
		ret=ENgetlinkindex(tmp,&index);
		//cout << "At opening Epanet retured : "<<ret<<'\n';
		ret=ENsetlinkvalue(index,EN_DIAMETER,_ind[i]);
		//cout << "At opening Epanet retured : "<<ret<<'\n';
	}
	//now run the simulation
		ret=ENsolveH();
		//cout << "At solve Epanet retured : "<<ret<<'\n';
		cost=0;
    //read the pressure values
	for(unsigned int i=0;i<nnodes;i++){
		int index=-1;
		char tmp[MAX_LABEL_LEN]; // convert c++ string to c style char* 
		strcpy(tmp,nodes[i].c_str());
		ret=ENgetnodeindex(tmp,&index);
		float value;
		//cout << "At ENgetnodeindex Epanet retured : "<<ret<<'\n';
		ret=ENgetnodevalue(index,EN_PRESSURE,&value);
		//cout << "At ENgetnodevalue Epanet retured : "<<ret<<'\n';
		if(value<10){
			cost+=pressue_cost_factor*(10-value);
		}
	}
	
	//cout << "At ENcloseH Epanet retured : "<<ret<<'\n';
	return cost;
		
	    
}

void epanet_init(){
	int ret;


	ret=ENopen(file,rept,"");
	cout << "At opening Epanet retured : "<<ret<<'\n';

}

void epanet_close(){
	int ret;
    ret=ENclose();
	cout << "At closing Epanet retured : "<<ret<<'\n';

}
RealEA.cpp
#define _CRT_SECURE_NO_DEPRECATE
//above is to get rid of deprecation warnings of Microsoft compiler. Needed because we use strcpy() function. 
#include <iostream>
#include <es/make_real.h>
#include "real_value.h"
#include <apply.h>

using namespace std;
typedef eoReal<eoMinimizingFitness> EOT;
void print_values(eoPop<EOT> pop);
int main_function(int argc, char* argv[]);

/** Notice that we have moved everything that was previously in main() to 
main_function. 
Now before GA related stuff is handled (by main_function), 
We process the command argument list. Unlike the previous case, now the first argument, i.e. 
the filename of the EPAnet related parameters, is mandatory. 
Then we copy the rest of the arguments in argv to a new array argv_ and pass it to main_function. 
From the GA viewpoint, nothing has changed. It receives a argument array. If there are no arguments in it, GA will 
run with default parameters. Otherwise it will parse the argument array. 
The first command line argument is separately passed parse_epanet_para function.
*/
int main(int argc,char *argv[]){
       /* argv[0] is always the name of the program. So, to run properly the program should have 
       length of argv (i.e. argc) >=2 
       If this is not the case, provide some help. */
	if(argc<2){// no arguments provided at the command line
		cout << "Usage: argv[0] <epanet_related_datafile> <EO related arguments ...>\n";
		cout << "Format of epanet_related_datafile as follows.\n";
		cout << "<network file name>\n";
		cout << "<file name for the report to write into>\n";
		cout << "<no of pipes>\n";
		cout << "<PIPE_ID1>\n";
		cout << "...\n";
		cout << "<no of nodes>\n";
		cout << "<NODE_ID1>\n";
		cout << "...\n";
		exit(1);
	}
	char* filename=argv[1]; // seperately copy argv[1] (first argument) to variable filename
	char* argv_[MAX_PATH_LEN]; 
	argv_[0]=argv[0];       // argv[0] is the calling program name, copy this as is. 
	for(int i=1;i<argc-1;i++){ // then copy the rest (argv[2], argv[3], ...) of arguments to new array
		cerr << argv[i+1];
		argv_[i]=argv[i+1];
	}
	argc--; // argc should be one less than before
	//now parse the parameter file stright away! 
	parse_epanet_para(filename);
	return main_function(argc,argv_); // now call main_function with new argc, argv_ pair. 
}
int main_function(int argc, char* argv[])
{

  try
  {
  // first initialize the Epanet
  epanet_init();

  eoParser parser(argc, argv);  // for user-parameter reading
  eoState state;   
  eoEvalFuncPtr<EOT, double, const std::vector<double>&> 
               mainEval( real_value );
  eoEvalFuncCounter<EOT> eval(mainEval);
  eoRealInitBounded<EOT>& init = make_genotype(parser, state, EOT());
  // Build the variation operator (any seq/prop construct)
  eoGenOp<EOT>& op = make_op(parser, state, init);
  // initialize the population - and evaluate
  // yes, this is representation indepedent once you have an eoInit
  eoPop<EOT>& pop   = make_pop(parser, state, init);
  // stopping criteria
  eoContinue<EOT> & term = make_continue(parser, state, eval);
  // output
  eoCheckPoint<EOT> & checkpoint = make_checkpoint(parser, state, eval, term);
  // algorithm (need the operator!)
  eoAlgo<EOT>& ea = make_algo_scalar(parser, state, eval, checkpoint, op);
  // to be called AFTER all parameters have been read!!!
  make_help(parser);

  // evaluate intial population AFTER help and status in case it takes time
  apply<EOT>(eval, pop);
  // print it out
  cout << "Initial Population\n";
  pop.sortedPrintOn(cout);
  cout << endl;

  cin.get();
  run_ea(ea, pop); // run the ea

  cout << "Final Population\n";
  pop.sortedPrintOn(cout);
  cout << endl;

  // close Epanet
  epanet_close();

  }
  catch(exception& e)
  {
    cout << e.what() << endl;
  }
	return 1;
}