* New tree configuration of the project:
.../
... + -- EO
| |
| |
+-- src ----- + -- EDO
| |
| |
+-- test + -- MO
| |
| |
+-- tutorial + -- MOEO
| |
| |
+-- doc + -- SMP
| |
| |
... + -- EOMPI
|
|
+ -- EOSERIAL
Question for current maintainers: ./README: new release?
Also:
* Moving out eompi & eoserial modules (issue #2).
* Correction of the errors when executing "make doc" command.
* Adding a solution for the conflicting headers problem (see the two CMake Cache
Values: PROJECT_TAG & PROJECT_HRS_INSTALL_SUBPATH) (issue #1)
* Header inclusions:
** src: changing absolute paths into relative paths ('#include <...>' -> '#include "..."')
** test, tutorial: changing relative paths into absolute paths ('#include "..."' -> '#include <...>')
* Moving out some scripts from EDO -> to the root
* Add a new script for compilation and installation (see build_gcc_linux_install)
* Compilation with uBLAS library or EDO module: now ok
* Minor modifications on README & INSTALL files
* Comment eompi failed tests with no end
*** TODO: CPack (debian (DEB) & RedHat (RPM) packages) (issues #6 & #7) ***
This commit is contained in:
parent
515bd5943d
commit
490e837f7a
124 changed files with 7688 additions and 16329 deletions
14
tutorial/eo/CMakeLists.txt
Executable file
14
tutorial/eo/CMakeLists.txt
Executable file
|
|
@ -0,0 +1,14 @@
|
|||
######################################################################################
|
||||
### 1) Where must cmake go now ?
|
||||
######################################################################################
|
||||
|
||||
ADD_SUBDIRECTORY(Lesson1)
|
||||
ADD_SUBDIRECTORY(Lesson2)
|
||||
ADD_SUBDIRECTORY(Lesson3)
|
||||
ADD_SUBDIRECTORY(Lesson4)
|
||||
ADD_SUBDIRECTORY(Lesson5)
|
||||
ADD_SUBDIRECTORY(Lesson6)
|
||||
|
||||
ADD_SUBDIRECTORY(app) # deprecated
|
||||
|
||||
######################################################################################
|
||||
502
tutorial/eo/LICENSE
Executable file
502
tutorial/eo/LICENSE
Executable file
|
|
@ -0,0 +1,502 @@
|
|||
GNU LESSER GENERAL PUBLIC LICENSE
|
||||
Version 2.1, February 1999
|
||||
|
||||
Copyright (C) 1991, 1999 Free Software Foundation, Inc.
|
||||
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
[This is the first released version of the Lesser GPL. It also counts
|
||||
as the successor of the GNU Library Public License, version 2, hence
|
||||
the version number 2.1.]
|
||||
|
||||
Preamble
|
||||
|
||||
The licenses for most software are designed to take away your
|
||||
freedom to share and change it. By contrast, the GNU General Public
|
||||
Licenses are intended to guarantee your freedom to share and change
|
||||
free software--to make sure the software is free for all its users.
|
||||
|
||||
This license, the Lesser General Public License, applies to some
|
||||
specially designated software packages--typically libraries--of the
|
||||
Free Software Foundation and other authors who decide to use it. You
|
||||
can use it too, but we suggest you first think carefully about whether
|
||||
this license or the ordinary General Public License is the better
|
||||
strategy to use in any particular case, based on the explanations below.
|
||||
|
||||
When we speak of free software, we are referring to freedom of use,
|
||||
not price. Our General Public Licenses are designed to make sure that
|
||||
you have the freedom to distribute copies of free software (and charge
|
||||
for this service if you wish); that you receive source code or can get
|
||||
it if you want it; that you can change the software and use pieces of
|
||||
it in new free programs; and that you are informed that you can do
|
||||
these things.
|
||||
|
||||
To protect your rights, we need to make restrictions that forbid
|
||||
distributors to deny you these rights or to ask you to surrender these
|
||||
rights. These restrictions translate to certain responsibilities for
|
||||
you if you distribute copies of the library or if you modify it.
|
||||
|
||||
For example, if you distribute copies of the library, whether gratis
|
||||
or for a fee, you must give the recipients all the rights that we gave
|
||||
you. You must make sure that they, too, receive or can get the source
|
||||
code. If you link other code with the library, you must provide
|
||||
complete object files to the recipients, so that they can relink them
|
||||
with the library after making changes to the library and recompiling
|
||||
it. And you must show them these terms so they know their rights.
|
||||
|
||||
We protect your rights with a two-step method: (1) we copyright the
|
||||
library, and (2) we offer you this license, which gives you legal
|
||||
permission to copy, distribute and/or modify the library.
|
||||
|
||||
To protect each distributor, we want to make it very clear that
|
||||
there is no warranty for the free library. Also, if the library is
|
||||
modified by someone else and passed on, the recipients should know
|
||||
that what they have is not the original version, so that the original
|
||||
author's reputation will not be affected by problems that might be
|
||||
introduced by others.
|
||||
|
||||
Finally, software patents pose a constant threat to the existence of
|
||||
any free program. We wish to make sure that a company cannot
|
||||
effectively restrict the users of a free program by obtaining a
|
||||
restrictive license from a patent holder. Therefore, we insist that
|
||||
any patent license obtained for a version of the library must be
|
||||
consistent with the full freedom of use specified in this license.
|
||||
|
||||
Most GNU software, including some libraries, is covered by the
|
||||
ordinary GNU General Public License. This license, the GNU Lesser
|
||||
General Public License, applies to certain designated libraries, and
|
||||
is quite different from the ordinary General Public License. We use
|
||||
this license for certain libraries in order to permit linking those
|
||||
libraries into non-free programs.
|
||||
|
||||
When a program is linked with a library, whether statically or using
|
||||
a shared library, the combination of the two is legally speaking a
|
||||
combined work, a derivative of the original library. The ordinary
|
||||
General Public License therefore permits such linking only if the
|
||||
entire combination fits its criteria of freedom. The Lesser General
|
||||
Public License permits more lax criteria for linking other code with
|
||||
the library.
|
||||
|
||||
We call this license the "Lesser" General Public License because it
|
||||
does Less to protect the user's freedom than the ordinary General
|
||||
Public License. It also provides other free software developers Less
|
||||
of an advantage over competing non-free programs. These disadvantages
|
||||
are the reason we use the ordinary General Public License for many
|
||||
libraries. However, the Lesser license provides advantages in certain
|
||||
special circumstances.
|
||||
|
||||
For example, on rare occasions, there may be a special need to
|
||||
encourage the widest possible use of a certain library, so that it becomes
|
||||
a de-facto standard. To achieve this, non-free programs must be
|
||||
allowed to use the library. A more frequent case is that a free
|
||||
library does the same job as widely used non-free libraries. In this
|
||||
case, there is little to gain by limiting the free library to free
|
||||
software only, so we use the Lesser General Public License.
|
||||
|
||||
In other cases, permission to use a particular library in non-free
|
||||
programs enables a greater number of people to use a large body of
|
||||
free software. For example, permission to use the GNU C Library in
|
||||
non-free programs enables many more people to use the whole GNU
|
||||
operating system, as well as its variant, the GNU/Linux operating
|
||||
system.
|
||||
|
||||
Although the Lesser General Public License is Less protective of the
|
||||
users' freedom, it does ensure that the user of a program that is
|
||||
linked with the Library has the freedom and the wherewithal to run
|
||||
that program using a modified version of the Library.
|
||||
|
||||
The precise terms and conditions for copying, distribution and
|
||||
modification follow. Pay close attention to the difference between a
|
||||
"work based on the library" and a "work that uses the library". The
|
||||
former contains code derived from the library, whereas the latter must
|
||||
be combined with the library in order to run.
|
||||
|
||||
GNU LESSER GENERAL PUBLIC LICENSE
|
||||
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
|
||||
|
||||
0. This License Agreement applies to any software library or other
|
||||
program which contains a notice placed by the copyright holder or
|
||||
other authorized party saying it may be distributed under the terms of
|
||||
this Lesser General Public License (also called "this License").
|
||||
Each licensee is addressed as "you".
|
||||
|
||||
A "library" means a collection of software functions and/or data
|
||||
prepared so as to be conveniently linked with application programs
|
||||
(which use some of those functions and data) to form executables.
|
||||
|
||||
The "Library", below, refers to any such software library or work
|
||||
which has been distributed under these terms. A "work based on the
|
||||
Library" means either the Library or any derivative work under
|
||||
copyright law: that is to say, a work containing the Library or a
|
||||
portion of it, either verbatim or with modifications and/or translated
|
||||
straightforwardly into another language. (Hereinafter, translation is
|
||||
included without limitation in the term "modification".)
|
||||
|
||||
"Source code" for a work means the preferred form of the work for
|
||||
making modifications to it. For a library, complete source code means
|
||||
all the source code for all modules it contains, plus any associated
|
||||
interface definition files, plus the scripts used to control compilation
|
||||
and installation of the library.
|
||||
|
||||
Activities other than copying, distribution and modification are not
|
||||
covered by this License; they are outside its scope. The act of
|
||||
running a program using the Library is not restricted, and output from
|
||||
such a program is covered only if its contents constitute a work based
|
||||
on the Library (independent of the use of the Library in a tool for
|
||||
writing it). Whether that is true depends on what the Library does
|
||||
and what the program that uses the Library does.
|
||||
|
||||
1. You may copy and distribute verbatim copies of the Library's
|
||||
complete source code as you receive it, in any medium, provided that
|
||||
you conspicuously and appropriately publish on each copy an
|
||||
appropriate copyright notice and disclaimer of warranty; keep intact
|
||||
all the notices that refer to this License and to the absence of any
|
||||
warranty; and distribute a copy of this License along with the
|
||||
Library.
|
||||
|
||||
You may charge a fee for the physical act of transferring a copy,
|
||||
and you may at your option offer warranty protection in exchange for a
|
||||
fee.
|
||||
|
||||
2. You may modify your copy or copies of the Library or any portion
|
||||
of it, thus forming a work based on the Library, and copy and
|
||||
distribute such modifications or work under the terms of Section 1
|
||||
above, provided that you also meet all of these conditions:
|
||||
|
||||
a) The modified work must itself be a software library.
|
||||
|
||||
b) You must cause the files modified to carry prominent notices
|
||||
stating that you changed the files and the date of any change.
|
||||
|
||||
c) You must cause the whole of the work to be licensed at no
|
||||
charge to all third parties under the terms of this License.
|
||||
|
||||
d) If a facility in the modified Library refers to a function or a
|
||||
table of data to be supplied by an application program that uses
|
||||
the facility, other than as an argument passed when the facility
|
||||
is invoked, then you must make a good faith effort to ensure that,
|
||||
in the event an application does not supply such function or
|
||||
table, the facility still operates, and performs whatever part of
|
||||
its purpose remains meaningful.
|
||||
|
||||
(For example, a function in a library to compute square roots has
|
||||
a purpose that is entirely well-defined independent of the
|
||||
application. Therefore, Subsection 2d requires that any
|
||||
application-supplied function or table used by this function must
|
||||
be optional: if the application does not supply it, the square
|
||||
root function must still compute square roots.)
|
||||
|
||||
These requirements apply to the modified work as a whole. If
|
||||
identifiable sections of that work are not derived from the Library,
|
||||
and can be reasonably considered independent and separate works in
|
||||
themselves, then this License, and its terms, do not apply to those
|
||||
sections when you distribute them as separate works. But when you
|
||||
distribute the same sections as part of a whole which is a work based
|
||||
on the Library, the distribution of the whole must be on the terms of
|
||||
this License, whose permissions for other licensees extend to the
|
||||
entire whole, and thus to each and every part regardless of who wrote
|
||||
it.
|
||||
|
||||
Thus, it is not the intent of this section to claim rights or contest
|
||||
your rights to work written entirely by you; rather, the intent is to
|
||||
exercise the right to control the distribution of derivative or
|
||||
collective works based on the Library.
|
||||
|
||||
In addition, mere aggregation of another work not based on the Library
|
||||
with the Library (or with a work based on the Library) on a volume of
|
||||
a storage or distribution medium does not bring the other work under
|
||||
the scope of this License.
|
||||
|
||||
3. You may opt to apply the terms of the ordinary GNU General Public
|
||||
License instead of this License to a given copy of the Library. To do
|
||||
this, you must alter all the notices that refer to this License, so
|
||||
that they refer to the ordinary GNU General Public License, version 2,
|
||||
instead of to this License. (If a newer version than version 2 of the
|
||||
ordinary GNU General Public License has appeared, then you can specify
|
||||
that version instead if you wish.) Do not make any other change in
|
||||
these notices.
|
||||
|
||||
Once this change is made in a given copy, it is irreversible for
|
||||
that copy, so the ordinary GNU General Public License applies to all
|
||||
subsequent copies and derivative works made from that copy.
|
||||
|
||||
This option is useful when you wish to copy part of the code of
|
||||
the Library into a program that is not a library.
|
||||
|
||||
4. You may copy and distribute the Library (or a portion or
|
||||
derivative of it, under Section 2) in object code or executable form
|
||||
under the terms of Sections 1 and 2 above provided that you accompany
|
||||
it with the complete corresponding machine-readable source code, which
|
||||
must be distributed under the terms of Sections 1 and 2 above on a
|
||||
medium customarily used for software interchange.
|
||||
|
||||
If distribution of object code is made by offering access to copy
|
||||
from a designated place, then offering equivalent access to copy the
|
||||
source code from the same place satisfies the requirement to
|
||||
distribute the source code, even though third parties are not
|
||||
compelled to copy the source along with the object code.
|
||||
|
||||
5. A program that contains no derivative of any portion of the
|
||||
Library, but is designed to work with the Library by being compiled or
|
||||
linked with it, is called a "work that uses the Library". Such a
|
||||
work, in isolation, is not a derivative work of the Library, and
|
||||
therefore falls outside the scope of this License.
|
||||
|
||||
However, linking a "work that uses the Library" with the Library
|
||||
creates an executable that is a derivative of the Library (because it
|
||||
contains portions of the Library), rather than a "work that uses the
|
||||
library". The executable is therefore covered by this License.
|
||||
Section 6 states terms for distribution of such executables.
|
||||
|
||||
When a "work that uses the Library" uses material from a header file
|
||||
that is part of the Library, the object code for the work may be a
|
||||
derivative work of the Library even though the source code is not.
|
||||
Whether this is true is especially significant if the work can be
|
||||
linked without the Library, or if the work is itself a library. The
|
||||
threshold for this to be true is not precisely defined by law.
|
||||
|
||||
If such an object file uses only numerical parameters, data
|
||||
structure layouts and accessors, and small macros and small inline
|
||||
functions (ten lines or less in length), then the use of the object
|
||||
file is unrestricted, regardless of whether it is legally a derivative
|
||||
work. (Executables containing this object code plus portions of the
|
||||
Library will still fall under Section 6.)
|
||||
|
||||
Otherwise, if the work is a derivative of the Library, you may
|
||||
distribute the object code for the work under the terms of Section 6.
|
||||
Any executables containing that work also fall under Section 6,
|
||||
whether or not they are linked directly with the Library itself.
|
||||
|
||||
6. As an exception to the Sections above, you may also combine or
|
||||
link a "work that uses the Library" with the Library to produce a
|
||||
work containing portions of the Library, and distribute that work
|
||||
under terms of your choice, provided that the terms permit
|
||||
modification of the work for the customer's own use and reverse
|
||||
engineering for debugging such modifications.
|
||||
|
||||
You must give prominent notice with each copy of the work that the
|
||||
Library is used in it and that the Library and its use are covered by
|
||||
this License. You must supply a copy of this License. If the work
|
||||
during execution displays copyright notices, you must include the
|
||||
copyright notice for the Library among them, as well as a reference
|
||||
directing the user to the copy of this License. Also, you must do one
|
||||
of these things:
|
||||
|
||||
a) Accompany the work with the complete corresponding
|
||||
machine-readable source code for the Library including whatever
|
||||
changes were used in the work (which must be distributed under
|
||||
Sections 1 and 2 above); and, if the work is an executable linked
|
||||
with the Library, with the complete machine-readable "work that
|
||||
uses the Library", as object code and/or source code, so that the
|
||||
user can modify the Library and then relink to produce a modified
|
||||
executable containing the modified Library. (It is understood
|
||||
that the user who changes the contents of definitions files in the
|
||||
Library will not necessarily be able to recompile the application
|
||||
to use the modified definitions.)
|
||||
|
||||
b) Use a suitable shared library mechanism for linking with the
|
||||
Library. A suitable mechanism is one that (1) uses at run time a
|
||||
copy of the library already present on the user's computer system,
|
||||
rather than copying library functions into the executable, and (2)
|
||||
will operate properly with a modified version of the library, if
|
||||
the user installs one, as long as the modified version is
|
||||
interface-compatible with the version that the work was made with.
|
||||
|
||||
c) Accompany the work with a written offer, valid for at
|
||||
least three years, to give the same user the materials
|
||||
specified in Subsection 6a, above, for a charge no more
|
||||
than the cost of performing this distribution.
|
||||
|
||||
d) If distribution of the work is made by offering access to copy
|
||||
from a designated place, offer equivalent access to copy the above
|
||||
specified materials from the same place.
|
||||
|
||||
e) Verify that the user has already received a copy of these
|
||||
materials or that you have already sent this user a copy.
|
||||
|
||||
For an executable, the required form of the "work that uses the
|
||||
Library" must include any data and utility programs needed for
|
||||
reproducing the executable from it. However, as a special exception,
|
||||
the materials to be distributed need not include anything that is
|
||||
normally distributed (in either source or binary form) with the major
|
||||
components (compiler, kernel, and so on) of the operating system on
|
||||
which the executable runs, unless that component itself accompanies
|
||||
the executable.
|
||||
|
||||
It may happen that this requirement contradicts the license
|
||||
restrictions of other proprietary libraries that do not normally
|
||||
accompany the operating system. Such a contradiction means you cannot
|
||||
use both them and the Library together in an executable that you
|
||||
distribute.
|
||||
|
||||
7. You may place library facilities that are a work based on the
|
||||
Library side-by-side in a single library together with other library
|
||||
facilities not covered by this License, and distribute such a combined
|
||||
library, provided that the separate distribution of the work based on
|
||||
the Library and of the other library facilities is otherwise
|
||||
permitted, and provided that you do these two things:
|
||||
|
||||
a) Accompany the combined library with a copy of the same work
|
||||
based on the Library, uncombined with any other library
|
||||
facilities. This must be distributed under the terms of the
|
||||
Sections above.
|
||||
|
||||
b) Give prominent notice with the combined library of the fact
|
||||
that part of it is a work based on the Library, and explaining
|
||||
where to find the accompanying uncombined form of the same work.
|
||||
|
||||
8. You may not copy, modify, sublicense, link with, or distribute
|
||||
the Library except as expressly provided under this License. Any
|
||||
attempt otherwise to copy, modify, sublicense, link with, or
|
||||
distribute the Library is void, and will automatically terminate your
|
||||
rights under this License. However, parties who have received copies,
|
||||
or rights, from you under this License will not have their licenses
|
||||
terminated so long as such parties remain in full compliance.
|
||||
|
||||
9. You are not required to accept this License, since you have not
|
||||
signed it. However, nothing else grants you permission to modify or
|
||||
distribute the Library or its derivative works. These actions are
|
||||
prohibited by law if you do not accept this License. Therefore, by
|
||||
modifying or distributing the Library (or any work based on the
|
||||
Library), you indicate your acceptance of this License to do so, and
|
||||
all its terms and conditions for copying, distributing or modifying
|
||||
the Library or works based on it.
|
||||
|
||||
10. Each time you redistribute the Library (or any work based on the
|
||||
Library), the recipient automatically receives a license from the
|
||||
original licensor to copy, distribute, link with or modify the Library
|
||||
subject to these terms and conditions. You may not impose any further
|
||||
restrictions on the recipients' exercise of the rights granted herein.
|
||||
You are not responsible for enforcing compliance by third parties with
|
||||
this License.
|
||||
|
||||
11. If, as a consequence of a court judgment or allegation of patent
|
||||
infringement or for any other reason (not limited to patent issues),
|
||||
conditions are imposed on you (whether by court order, agreement or
|
||||
otherwise) that contradict the conditions of this License, they do not
|
||||
excuse you from the conditions of this License. If you cannot
|
||||
distribute so as to satisfy simultaneously your obligations under this
|
||||
License and any other pertinent obligations, then as a consequence you
|
||||
may not distribute the Library at all. For example, if a patent
|
||||
license would not permit royalty-free redistribution of the Library by
|
||||
all those who receive copies directly or indirectly through you, then
|
||||
the only way you could satisfy both it and this License would be to
|
||||
refrain entirely from distribution of the Library.
|
||||
|
||||
If any portion of this section is held invalid or unenforceable under any
|
||||
particular circumstance, the balance of the section is intended to apply,
|
||||
and the section as a whole is intended to apply in other circumstances.
|
||||
|
||||
It is not the purpose of this section to induce you to infringe any
|
||||
patents or other property right claims or to contest validity of any
|
||||
such claims; this section has the sole purpose of protecting the
|
||||
integrity of the free software distribution system which is
|
||||
implemented by public license practices. Many people have made
|
||||
generous contributions to the wide range of software distributed
|
||||
through that system in reliance on consistent application of that
|
||||
system; it is up to the author/donor to decide if he or she is willing
|
||||
to distribute software through any other system and a licensee cannot
|
||||
impose that choice.
|
||||
|
||||
This section is intended to make thoroughly clear what is believed to
|
||||
be a consequence of the rest of this License.
|
||||
|
||||
12. If the distribution and/or use of the Library is restricted in
|
||||
certain countries either by patents or by copyrighted interfaces, the
|
||||
original copyright holder who places the Library under this License may add
|
||||
an explicit geographical distribution limitation excluding those countries,
|
||||
so that distribution is permitted only in or among countries not thus
|
||||
excluded. In such case, this License incorporates the limitation as if
|
||||
written in the body of this License.
|
||||
|
||||
13. The Free Software Foundation may publish revised and/or new
|
||||
versions of the Lesser General Public License from time to time.
|
||||
Such new versions will be similar in spirit to the present version,
|
||||
but may differ in detail to address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the Library
|
||||
specifies a version number of this License which applies to it and
|
||||
"any later version", you have the option of following the terms and
|
||||
conditions either of that version or of any later version published by
|
||||
the Free Software Foundation. If the Library does not specify a
|
||||
license version number, you may choose any version ever published by
|
||||
the Free Software Foundation.
|
||||
|
||||
14. If you wish to incorporate parts of the Library into other free
|
||||
programs whose distribution conditions are incompatible with these,
|
||||
write to the author to ask for permission. For software which is
|
||||
copyrighted by the Free Software Foundation, write to the Free
|
||||
Software Foundation; we sometimes make exceptions for this. Our
|
||||
decision will be guided by the two goals of preserving the free status
|
||||
of all derivatives of our free software and of promoting the sharing
|
||||
and reuse of software generally.
|
||||
|
||||
NO WARRANTY
|
||||
|
||||
15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO
|
||||
WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW.
|
||||
EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR
|
||||
OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY
|
||||
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
|
||||
LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME
|
||||
THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
|
||||
|
||||
16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN
|
||||
WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY
|
||||
AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU
|
||||
FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR
|
||||
CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
|
||||
LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
|
||||
RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
|
||||
FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
|
||||
SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
|
||||
DAMAGES.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
How to Apply These Terms to Your New Libraries
|
||||
|
||||
If you develop a new library, and you want it to be of the greatest
|
||||
possible use to the public, we recommend making it free software that
|
||||
everyone can redistribute and change. You can do so by permitting
|
||||
redistribution under these terms (or, alternatively, under the terms of the
|
||||
ordinary General Public License).
|
||||
|
||||
To apply these terms, attach the following notices to the library. It is
|
||||
safest to attach them to the start of each source file to most effectively
|
||||
convey the exclusion of warranty; and each file should have at least the
|
||||
"copyright" line and a pointer to where the full notice is found.
|
||||
|
||||
<one line to give the library's name and a brief idea of what it does.>
|
||||
Copyright (C) <year> <name of author>
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
Also add information on how to contact you by electronic and paper mail.
|
||||
|
||||
You should also get your employer (if you work as a programmer) or your
|
||||
school, if any, to sign a "copyright disclaimer" for the library, if
|
||||
necessary. Here is a sample; alter the names:
|
||||
|
||||
Yoyodyne, Inc., hereby disclaims all copyright interest in the
|
||||
library `Frob' (a library for tweaking knobs) written by James Random Hacker.
|
||||
|
||||
<signature of Ty Coon>, 1 April 1990
|
||||
Ty Coon, President of Vice
|
||||
|
||||
That's all there is to it!
|
||||
64
tutorial/eo/Lesson1/CMakeLists.txt
Executable file
64
tutorial/eo/Lesson1/CMakeLists.txt
Executable file
|
|
@ -0,0 +1,64 @@
|
|||
######################################################################################
|
||||
### 1) Include the sources
|
||||
######################################################################################
|
||||
|
||||
#include_directories(${EO_SRC_DIR}/src)
|
||||
#include_directories(${EO_SRC_DIR}/src/ga)
|
||||
#include_directories(${EO_SRC_DIR}/src/utils)
|
||||
|
||||
######################################################################################
|
||||
### 2) Specify where CMake can find the libraries
|
||||
######################################################################################
|
||||
|
||||
if(NOT WIN32 OR CYGWIN)
|
||||
link_directories(${EO_BIN_DIR}/lib)
|
||||
endif(NOT WIN32 OR CYGWIN)
|
||||
|
||||
# especially for Visual Studio
|
||||
if(WIN32 AND NOT CYGWIN)
|
||||
link_directories(${EO_BIN_DIR}\\lib\\${CMAKE_BUILD_TYPE})
|
||||
endif(WIN32 AND NOT CYGWIN)
|
||||
|
||||
######################################################################################
|
||||
### 3) Define your targets
|
||||
######################################################################################
|
||||
|
||||
# no matter what is the OS, hopefully
|
||||
add_executable(FirstBitGA FirstBitGA.cpp)
|
||||
add_executable(FirstRealGA FirstRealGA.cpp)
|
||||
add_executable(exercise1.3 exercise1.3.cpp)
|
||||
|
||||
add_dependencies(FirstBitGA ga eo eoutils)
|
||||
add_dependencies(FirstRealGA ga eo eoutils)
|
||||
add_dependencies(exercise1.3 ga eo eoutils)
|
||||
|
||||
######################################################################################
|
||||
### 4) Optionnal
|
||||
######################################################################################
|
||||
|
||||
set(FIRSTBITGA_VERSION ${GLOBAL_VERSION})
|
||||
set_target_properties(FirstBitGA PROPERTIES VERSION "${FIRSTBITGA_VERSION}")
|
||||
|
||||
set(FIRSTREALGA_VERSION ${GLOBAL_VERSION})
|
||||
set_target_properties(FirstRealGA PROPERTIES VERSION "${FIRSTREALGA_VERSION}")
|
||||
|
||||
set(EXERCICE13_VERSION ${GLOBAL_VERSION})
|
||||
set_target_properties(exercise1.3 PROPERTIES VERSION "${EXERCICE13_VERSION}")
|
||||
|
||||
######################################################################################
|
||||
### 5) Link the librairies for the targets
|
||||
######################################################################################
|
||||
|
||||
target_link_libraries(FirstBitGA ga eo eoutils)
|
||||
target_link_libraries(FirstRealGA ga eo eoutils)
|
||||
target_link_libraries(exercise1.3 ga eo eoutils)
|
||||
|
||||
######################################################################################
|
||||
### 6) Configure project installation paths
|
||||
######################################################################################
|
||||
|
||||
install(TARGETS FirstBitGA RUNTIME DESTINATION share/${PROJECT_TAG}/eo/examples/Lesson1 COMPONENT examples)
|
||||
install(TARGETS FirstRealGA RUNTIME DESTINATION share/${PROJECT_TAG}/eo/examples/Lesson1 COMPONENT examples)
|
||||
install(TARGETS exercise1.3 RUNTIME DESTINATION share/${PROJECT_TAG}/eo/examples/Lesson1 COMPONENT examples)
|
||||
|
||||
######################################################################################
|
||||
167
tutorial/eo/Lesson1/FirstBitGA.cpp
Executable file
167
tutorial/eo/Lesson1/FirstBitGA.cpp
Executable file
|
|
@ -0,0 +1,167 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// FirstBitGA.cpp
|
||||
//-----------------------------------------------------------------------------
|
||||
//*
|
||||
// An instance of a VERY simple Bitstring Genetic Algorithm
|
||||
//
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
#include <stdexcept>
|
||||
#include <iostream>
|
||||
|
||||
#include <paradiseo/eo.h>
|
||||
#include <paradiseo/eo/ga.h>
|
||||
|
||||
// Use functions from namespace std
|
||||
using namespace std;
|
||||
|
||||
// REPRESENTATION
|
||||
//-----------------------------------------------------------------------------
|
||||
// define your individuals
|
||||
typedef eoBit<double> Indi; // A bitstring with fitness double
|
||||
|
||||
// EVAL
|
||||
//-----------------------------------------------------------------------------
|
||||
// a simple fitness function that computes the number of ones of a bitstring
|
||||
// @param _indi A biststring individual
|
||||
|
||||
double binary_value(const Indi & _indi)
|
||||
{
|
||||
double sum = 0;
|
||||
for (unsigned i = 0; i < _indi.size(); i++)
|
||||
sum += _indi[i];
|
||||
return sum;
|
||||
}
|
||||
// GENERAL
|
||||
//-----------------------------------------------------------------------------
|
||||
void main_function(int argc, char **argv)
|
||||
{
|
||||
// PARAMETRES
|
||||
// all parameters are hard-coded!
|
||||
const unsigned int SEED = 42; // seed for random number generator
|
||||
const unsigned int T_SIZE = 3; // size for tournament selection
|
||||
const unsigned int VEC_SIZE = 16; // Number of bits in genotypes
|
||||
const unsigned int POP_SIZE = 100; // Size of population
|
||||
const unsigned int MAX_GEN = 400; // Maximum number of generation before STOP
|
||||
const float CROSS_RATE = 0.8; // Crossover rate
|
||||
const double P_MUT_PER_BIT = 0.01; // probability of bit-flip mutation
|
||||
const float MUT_RATE = 1.0; // mutation rate
|
||||
|
||||
// GENERAL
|
||||
//////////////////////////
|
||||
// Random seed
|
||||
//////////////////////////
|
||||
//reproducible random seed: if you don't change SEED above,
|
||||
// you'll aways get the same result, NOT a random run
|
||||
rng.reseed(SEED);
|
||||
|
||||
// EVAL
|
||||
/////////////////////////////
|
||||
// Fitness function
|
||||
////////////////////////////
|
||||
// Evaluation: from a plain C++ fn to an EvalFunc Object
|
||||
eoEvalFuncPtr<Indi> eval( binary_value );
|
||||
|
||||
// INIT
|
||||
////////////////////////////////
|
||||
// Initilisation of population
|
||||
////////////////////////////////
|
||||
|
||||
// declare the population
|
||||
eoPop<Indi> pop;
|
||||
// fill it!
|
||||
for (unsigned int igeno=0; igeno<POP_SIZE; igeno++)
|
||||
{
|
||||
Indi v; // void individual, to be filled
|
||||
for (unsigned ivar=0; ivar<VEC_SIZE; ivar++)
|
||||
{
|
||||
bool r = rng.flip(); // new value, random in {0,1}
|
||||
v.push_back(r); // append that random value to v
|
||||
}
|
||||
eval(v); // evaluate it
|
||||
pop.push_back(v); // and put it in the population
|
||||
}
|
||||
|
||||
// OUTPUT
|
||||
// sort pop before printing it!
|
||||
pop.sort();
|
||||
// Print (sorted) intial population (raw printout)
|
||||
cout << "Initial Population" << endl;
|
||||
cout << pop;
|
||||
// shuffle - this is a test
|
||||
pop.shuffle();
|
||||
// Print (sorted) intial population (raw printout)
|
||||
cout << "Shuffled Population" << endl;
|
||||
cout << pop;
|
||||
|
||||
// ENGINE
|
||||
/////////////////////////////////////
|
||||
// selection and replacement
|
||||
////////////////////////////////////
|
||||
// SELECT
|
||||
// The robust tournament selection
|
||||
eoDetTournamentSelect<Indi> select(T_SIZE); // T_SIZE in [2,POP_SIZE]
|
||||
|
||||
// REPLACE
|
||||
// The simple GA evolution engine uses generational replacement
|
||||
// so no replacement procedure is needed
|
||||
|
||||
// OPERATORS
|
||||
//////////////////////////////////////
|
||||
// The variation operators
|
||||
//////////////////////////////////////
|
||||
// CROSSOVER
|
||||
// 1-point crossover for bitstring
|
||||
eo1PtBitXover<Indi> xover;
|
||||
// MUTATION
|
||||
// standard bit-flip mutation for bitstring
|
||||
eoBitMutation<Indi> mutation(P_MUT_PER_BIT);
|
||||
|
||||
// STOP
|
||||
// CHECKPOINT
|
||||
//////////////////////////////////////
|
||||
// termination condition
|
||||
/////////////////////////////////////
|
||||
// stop after MAX_GEN generations
|
||||
eoGenContinue<Indi> continuator(MAX_GEN);
|
||||
|
||||
// GENERATION
|
||||
/////////////////////////////////////////
|
||||
// the algorithm
|
||||
////////////////////////////////////////
|
||||
// standard Generational GA requires as parameters
|
||||
// selection, evaluation, crossover and mutation, stopping criterion
|
||||
|
||||
|
||||
eoSGA<Indi> gga(select, xover, CROSS_RATE, mutation, MUT_RATE,
|
||||
eval, continuator);
|
||||
|
||||
// Apply algo to pop - that's it!
|
||||
gga(pop);
|
||||
|
||||
// OUTPUT
|
||||
// Print (sorted) intial population
|
||||
pop.sort();
|
||||
cout << "FINAL Population\n" << pop << endl;
|
||||
// GENERAL
|
||||
}
|
||||
// A main that catches the exceptions
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
|
||||
try
|
||||
{
|
||||
main_function(argc, argv);
|
||||
}
|
||||
catch(exception& e)
|
||||
{
|
||||
cout << "Exception: " << e.what() << '\n';
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
162
tutorial/eo/Lesson1/FirstRealGA.cpp
Executable file
162
tutorial/eo/Lesson1/FirstRealGA.cpp
Executable file
|
|
@ -0,0 +1,162 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// FirstRealGA.cpp
|
||||
//-----------------------------------------------------------------------------
|
||||
//*
|
||||
// An instance of a VERY simple Real-coded Genetic Algorithm
|
||||
//
|
||||
//-----------------------------------------------------------------------------
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
#include <stdexcept>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
#include <paradiseo/eo.h>
|
||||
#include <paradiseo/eo/es.h>
|
||||
|
||||
// Use functions from namespace std
|
||||
using namespace std;
|
||||
|
||||
// REPRESENTATION
|
||||
//-----------------------------------------------------------------------------
|
||||
// define your individuals
|
||||
typedef eoReal<double> Indi;
|
||||
|
||||
// EVAL
|
||||
//-----------------------------------------------------------------------------
|
||||
// a simple fitness function that computes the euclidian norm of a real vector
|
||||
// @param _indi A real-valued individual
|
||||
|
||||
double real_value(const Indi & _indi)
|
||||
{
|
||||
double sum = 0;
|
||||
for (unsigned i = 0; i < _indi.size(); i++)
|
||||
sum += _indi[i]*_indi[i];
|
||||
return (-sum); // maximizing only
|
||||
}
|
||||
// GENERAL
|
||||
//-----------------------------------------------------------------------------
|
||||
void main_function(int argc, char **argv)
|
||||
{
|
||||
// PARAMETRES
|
||||
// all parameters are hard-coded!
|
||||
const unsigned int SEED = 42; // seed for random number generator
|
||||
const unsigned int VEC_SIZE = 8; // Number of object variables in genotypes
|
||||
const unsigned int POP_SIZE = 20; // Size of population
|
||||
const unsigned int T_SIZE = 3; // size for tournament selection
|
||||
const unsigned int MAX_GEN = 500; // Maximum number of generation before STOP
|
||||
const float CROSS_RATE = 0.8; // Crossover rate
|
||||
const double EPSILON = 0.01; // range for real uniform mutation
|
||||
const float MUT_RATE = 0.5; // mutation rate
|
||||
|
||||
// GENERAL
|
||||
//////////////////////////
|
||||
// Random seed
|
||||
//////////////////////////
|
||||
//reproducible random seed: if you don't change SEED above,
|
||||
// you'll aways get the same result, NOT a random run
|
||||
rng.reseed(SEED);
|
||||
|
||||
// EVAL
|
||||
/////////////////////////////
|
||||
// Fitness function
|
||||
////////////////////////////
|
||||
// Evaluation: from a plain C++ fn to an EvalFunc Object
|
||||
eoEvalFuncPtr<Indi> eval( real_value );
|
||||
|
||||
// INIT
|
||||
////////////////////////////////
|
||||
// Initilisation of population
|
||||
////////////////////////////////
|
||||
|
||||
// declare the population
|
||||
eoPop<Indi> pop;
|
||||
// fill it!
|
||||
for (unsigned int igeno=0; igeno<POP_SIZE; igeno++)
|
||||
{
|
||||
Indi v; // void individual, to be filled
|
||||
for (unsigned ivar=0; ivar<VEC_SIZE; ivar++)
|
||||
{
|
||||
double r = 2*rng.uniform() - 1; // new value, random in [-1,1)
|
||||
v.push_back(r); // append that random value to v
|
||||
}
|
||||
eval(v); // evaluate it
|
||||
pop.push_back(v); // and put it in the population
|
||||
}
|
||||
|
||||
// OUTPUT
|
||||
// sort pop before printing it!
|
||||
pop.sort();
|
||||
// Print (sorted) intial population (raw printout)
|
||||
cout << "Initial Population" << endl;
|
||||
cout << pop;
|
||||
|
||||
// ENGINE
|
||||
/////////////////////////////////////
|
||||
// selection and replacement
|
||||
////////////////////////////////////
|
||||
// SELECT
|
||||
// The robust tournament selection
|
||||
eoDetTournamentSelect<Indi> select(T_SIZE); // T_SIZE in [2,POP_SIZE]
|
||||
|
||||
// REPLACE
|
||||
// eoSGA uses generational replacement by default
|
||||
// so no replacement procedure has to be given
|
||||
|
||||
// OPERATORS
|
||||
//////////////////////////////////////
|
||||
// The variation operators
|
||||
//////////////////////////////////////
|
||||
// CROSSOVER
|
||||
// offspring(i) is a linear combination of parent(i)
|
||||
eoSegmentCrossover<Indi> xover;
|
||||
// MUTATION
|
||||
// offspring(i) uniformly chosen in [parent(i)-epsilon, parent(i)+epsilon]
|
||||
eoUniformMutation<Indi> mutation(EPSILON);
|
||||
|
||||
// STOP
|
||||
// CHECKPOINT
|
||||
//////////////////////////////////////
|
||||
// termination condition
|
||||
/////////////////////////////////////
|
||||
// stop after MAX_GEN generations
|
||||
eoGenContinue<Indi> continuator(MAX_GEN);
|
||||
|
||||
// GENERATION
|
||||
/////////////////////////////////////////
|
||||
// the algorithm
|
||||
////////////////////////////////////////
|
||||
// standard Generational GA requires
|
||||
// selection, evaluation, crossover and mutation, stopping criterion
|
||||
|
||||
|
||||
eoSGA<Indi> gga(select, xover, CROSS_RATE, mutation, MUT_RATE,
|
||||
eval, continuator);
|
||||
|
||||
// Apply algo to pop - that's it!
|
||||
gga(pop);
|
||||
|
||||
// OUTPUT
|
||||
// Print (sorted) intial population
|
||||
pop.sort();
|
||||
cout << "FINAL Population\n" << pop << endl;
|
||||
// GENERAL
|
||||
}
|
||||
|
||||
// A main that catches the exceptions
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
try
|
||||
{
|
||||
main_function(argc, argv);
|
||||
}
|
||||
catch(exception& e)
|
||||
{
|
||||
cout << "Exception: " << e.what() << '\n';
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
25
tutorial/eo/Lesson1/Makefile.simple
Executable file
25
tutorial/eo/Lesson1/Makefile.simple
Executable file
|
|
@ -0,0 +1,25 @@
|
|||
# if you use this Makefile as a starting point for another application
|
||||
# you might need to modify the following
|
||||
DIR_EO = ../../src
|
||||
|
||||
.SUFFIXES: .cpp
|
||||
|
||||
# Warning: $(CXX) in Linux (RedHat and Mandrake at least) is g++
|
||||
# However, if you are using this Makefile within xemacs,
|
||||
# and have problems with the interpretation of the output (and its colors)
|
||||
# then you should use c++ instead (make CXX=c++ will do)
|
||||
|
||||
.cpp: ; $(CXX) -DPACKAGE=\"eo\" -DVERSION=\"0.9.3\" -I. -I$(DIR_EO) -Wall -g -pg -o $@ $*.cpp $(DIR_EO)/utils/libeoutils.a $(DIR_EO)/libeo.a
|
||||
|
||||
.cpp.o: ; $(CXX) -DPACKAGE=\"eo\" -DVERSION=\"0.9.3\" -I. -I$(DIR_EO) -Wall -g -c -pg $*.cpp
|
||||
|
||||
firstGA = FirstRealGA FirstBitGA
|
||||
|
||||
ALL = $(firstGA) exercise1.3
|
||||
|
||||
lesson1 : $(firstGA)
|
||||
|
||||
all : $(ALL)
|
||||
|
||||
clean :
|
||||
@/bin/rm $(ALL) *.o *~
|
||||
162
tutorial/eo/Lesson1/exercise1.3.cpp
Executable file
162
tutorial/eo/Lesson1/exercise1.3.cpp
Executable file
|
|
@ -0,0 +1,162 @@
|
|||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// FirstBitGA.cpp
|
||||
//-----------------------------------------------------------------------------
|
||||
//*
|
||||
// An instance of a VERY simple Bitstring Genetic Algorithm
|
||||
//
|
||||
//-----------------------------------------------------------------------------
|
||||
// standard includes
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
|
||||
// the general include for eo
|
||||
#include <paradiseo/eo.h>
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Include the corresponding file
|
||||
#include <paradiseo/eo/ga.h> // bitstring representation & operators
|
||||
// define your individuals
|
||||
typedef eoBit<double> Indi; // A bitstring with fitness double
|
||||
|
||||
using namespace std;
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
/** a simple fitness function that computes the number of ones of a bitstring
|
||||
@param _indi A biststring individual
|
||||
*/
|
||||
|
||||
double binary_value(const Indi & _indi)
|
||||
{
|
||||
double sum = 0;
|
||||
for (unsigned i = 0; i < _indi.size(); i++)
|
||||
sum += _indi[i];
|
||||
return sum;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
void main_function(int argc, char **argv)
|
||||
{
|
||||
const unsigned int SEED = 42; // seed for random number generator
|
||||
const unsigned int VEC_SIZE = 8; // Number of bits in genotypes
|
||||
const unsigned int POP_SIZE = 20; // Size of population
|
||||
const unsigned int MAX_GEN = 500; // Maximum number of generation before STOP
|
||||
const float CROSS_RATE = 0.8; // Crossover rate
|
||||
const double P_MUT_PER_BIT = 0.01; // probability of bit-flip mutation
|
||||
const float MUT_RATE = 1.0; // mutation rate
|
||||
|
||||
//////////////////////////
|
||||
// Random seed
|
||||
//////////////////////////
|
||||
//reproducible random seed: if you don't change SEED above,
|
||||
// you'll aways get the same result, NOT a random run
|
||||
rng.reseed(SEED);
|
||||
|
||||
/////////////////////////////
|
||||
// Fitness function
|
||||
////////////////////////////
|
||||
// Evaluation: from a plain C++ fn to an EvalFunc Object
|
||||
eoEvalFuncPtr<Indi> eval( binary_value );
|
||||
|
||||
////////////////////////////////
|
||||
// Initilisation of population
|
||||
////////////////////////////////
|
||||
|
||||
// declare the population
|
||||
eoPop<Indi> pop;
|
||||
// fill it!
|
||||
for (unsigned int igeno=0; igeno<POP_SIZE; igeno++)
|
||||
{
|
||||
Indi v; // void individual, to be filled
|
||||
for (unsigned ivar=0; ivar<VEC_SIZE; ivar++)
|
||||
{
|
||||
bool r = rng.flip(); // new value, random in {0,1}
|
||||
v.push_back(r); // append that random value to v
|
||||
}
|
||||
eval(v); // evaluate it
|
||||
pop.push_back(v); // and put it in the population
|
||||
}
|
||||
|
||||
// sort pop before printing it!
|
||||
pop.sort();
|
||||
// Print (sorted) intial population (raw printout)
|
||||
cout << "Initial Population" << endl;
|
||||
cout << pop;
|
||||
|
||||
/////////////////////////////////////
|
||||
// selection and replacement
|
||||
////////////////////////////////////
|
||||
|
||||
// solution solution solution: uncomment one of the following,
|
||||
// comment out the eoDetTournament lines
|
||||
|
||||
// The well-known roulette
|
||||
// eoProportionalSelect<Indi> select;
|
||||
|
||||
// could also use stochastic binary tournament selection
|
||||
//
|
||||
// const double RATE = 0.75;
|
||||
// eoStochTournamentSelect<Indi> select(RATE); // RATE in ]0.5,1]
|
||||
// The robust tournament selection
|
||||
const unsigned int T_SIZE = 3; // size for tournament selection
|
||||
eoDetTournamentSelect<Indi> select(T_SIZE); // T_SIZE in [2,POP_SIZE]
|
||||
|
||||
// and of course the random selection
|
||||
// eoRandomSelect<Indi> select;
|
||||
|
||||
// The simple GA evolution engine uses generational replacement
|
||||
// so no replacement procedure is needed
|
||||
|
||||
//////////////////////////////////////
|
||||
// termination condition
|
||||
/////////////////////////////////////
|
||||
// stop after MAX_GEN generations
|
||||
eoGenContinue<Indi> continuator(MAX_GEN);
|
||||
|
||||
|
||||
//////////////////////////////////////
|
||||
// The variation operators
|
||||
//////////////////////////////////////
|
||||
// standard bit-flip mutation for bitstring
|
||||
eoBitMutation<Indi> mutation(P_MUT_PER_BIT);
|
||||
// 1-point mutation for bitstring
|
||||
eo1PtBitXover<Indi> xover;
|
||||
|
||||
/////////////////////////////////////////
|
||||
// the algorithm
|
||||
////////////////////////////////////////
|
||||
// standard Generational GA requires as parameters
|
||||
// selection, evaluation, crossover and mutation, stopping criterion
|
||||
|
||||
|
||||
eoSGA<Indi> gga(select, xover, CROSS_RATE, mutation, MUT_RATE,
|
||||
eval, continuator);
|
||||
|
||||
// Apply algo to pop - that's it!
|
||||
gga(pop);
|
||||
|
||||
// Print (sorted) intial population
|
||||
pop.sort();
|
||||
cout << "FINAL Population\n" << pop << endl;
|
||||
}
|
||||
|
||||
// A main that catches the exceptions
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
|
||||
try
|
||||
{
|
||||
main_function(argc, argv);
|
||||
}
|
||||
catch(exception& e)
|
||||
{
|
||||
cout << "Exception: " << e.what() << '\n';
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
60
tutorial/eo/Lesson2/CMakeLists.txt
Executable file
60
tutorial/eo/Lesson2/CMakeLists.txt
Executable file
|
|
@ -0,0 +1,60 @@
|
|||
######################################################################################
|
||||
### 1) Include the sources
|
||||
######################################################################################
|
||||
|
||||
#include_directories(${EO_SRC_DIR}/src)
|
||||
#include_directories(${EO_SRC_DIR}/src/ga)
|
||||
#include_directories(${EO_SRC_DIR}/src/utils)
|
||||
|
||||
######################################################################################
|
||||
### 2) Specify where CMake can find the libraries
|
||||
######################################################################################
|
||||
|
||||
if(NOT WIN32 OR CYGWIN)
|
||||
link_directories(${EO_BIN_DIR}/lib)
|
||||
endif(NOT WIN32 OR CYGWIN)
|
||||
|
||||
# especially for Visual Studio
|
||||
if(WIN32 AND NOT CYGWIN)
|
||||
link_directories(${EO_BIN_DIR}\\lib\\${CMAKE_BUILD_TYPE})
|
||||
endif(WIN32 AND NOT CYGWIN)
|
||||
|
||||
######################################################################################
|
||||
### 3) Define your targets
|
||||
######################################################################################
|
||||
|
||||
# no matter what is the OS, hopefully
|
||||
add_executable(FirstBitEA FirstBitEA.cpp)
|
||||
add_executable(FirstRealEA FirstRealEA.cpp)
|
||||
add_executable(exercise2.3 exercise2.3.cpp)
|
||||
|
||||
######################################################################################
|
||||
### 4) Optionnal
|
||||
######################################################################################
|
||||
|
||||
set(FIRSTBITEA_VERSION ${GLOBAL_VERSION})
|
||||
set_target_properties(FirstBitEA PROPERTIES VERSION "${FIRSTBITEA_VERSION}")
|
||||
|
||||
set(FIRSTREALEA_VERSION ${GLOBAL_VERSION})
|
||||
set_target_properties(FirstRealEA PROPERTIES VERSION "${FIRSTREALEA_VERSION}")
|
||||
|
||||
set(EXERCICE23_VERSION ${GLOBAL_VERSION})
|
||||
set_target_properties(exercise2.3 PROPERTIES VERSION "${EXERCICE23_VERSION}")
|
||||
|
||||
######################################################################################
|
||||
### 5) Link the librairies for the targets
|
||||
######################################################################################
|
||||
|
||||
target_link_libraries(FirstBitEA ga eo eoutils)
|
||||
target_link_libraries(FirstRealEA ga eo eoutils)
|
||||
target_link_libraries(exercise2.3 ga eo eoutils)
|
||||
|
||||
######################################################################################
|
||||
### 6) Configure project installation paths
|
||||
######################################################################################
|
||||
|
||||
install(TARGETS FirstBitEA RUNTIME DESTINATION share/${PROJECT_TAG}/eo/examples/Lesson2 COMPONENT examples)
|
||||
install(TARGETS FirstRealEA RUNTIME DESTINATION share/${PROJECT_TAG}/eo/examples/Lesson2 COMPONENT examples)
|
||||
install(TARGETS exercise2.3 RUNTIME DESTINATION share/${PROJECT_TAG}/eo/examples/Lesson2 COMPONENT examples)
|
||||
|
||||
######################################################################################
|
||||
193
tutorial/eo/Lesson2/FirstBitEA.cpp
Executable file
193
tutorial/eo/Lesson2/FirstBitEA.cpp
Executable file
|
|
@ -0,0 +1,193 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// FirstBitEA.cpp
|
||||
//-----------------------------------------------------------------------------
|
||||
//*
|
||||
// Still an instance of a VERY simple Bitstring Genetic Algorithm
|
||||
// (see FirstBitGA.cpp) but now with Breeder - and Combined Ops
|
||||
//
|
||||
//-----------------------------------------------------------------------------
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
// standard includes
|
||||
#include <stdexcept> // runtime_error
|
||||
#include <iostream> // cout
|
||||
|
||||
// the general include for eo
|
||||
#include <paradiseo/eo.h>
|
||||
#include <paradiseo/eo/ga.h>
|
||||
|
||||
// Use functions from namespace std
|
||||
using namespace std;
|
||||
|
||||
// REPRESENTATION
|
||||
//-----------------------------------------------------------------------------
|
||||
// define your individuals
|
||||
typedef eoBit<double> Indi; // A bitstring with fitness double
|
||||
|
||||
// EVALFUNC
|
||||
//-----------------------------------------------------------------------------
|
||||
// a simple fitness function that computes the number of ones of a bitstring
|
||||
// Now in a separate file, and declared as binary_value(const vector<bool> &)
|
||||
|
||||
#include "binary_value.h"
|
||||
|
||||
// GENERAL
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
void main_function(int argc, char **argv)
|
||||
{
|
||||
// PARAMETRES
|
||||
const unsigned int SEED = 42; // seed for random number generator
|
||||
const unsigned int T_SIZE = 3; // size for tournament selection
|
||||
const unsigned int VEC_SIZE = 8; // Number of bits in genotypes
|
||||
const unsigned int POP_SIZE = 20; // Size of population
|
||||
|
||||
const unsigned int MAX_GEN = 500; // Maximum number of generation before STOP
|
||||
const unsigned int MIN_GEN = 10; // Minimum number of generation before ...
|
||||
const unsigned int STEADY_GEN = 50; // stop after STEADY_GEN gen. without improvelent
|
||||
|
||||
const double P_CROSS = 0.8; // Crossover probability
|
||||
const double P_MUT = 1.0; // mutation probability
|
||||
|
||||
const double P_MUT_PER_BIT = 0.01; // internal probability for bit-flip mutation
|
||||
// some parameters for chosing among different operators
|
||||
const double onePointRate = 0.5; // rate for 1-pt Xover
|
||||
const double twoPointsRate = 0.5; // rate for 2-pt Xover
|
||||
const double URate = 0.5; // rate for Uniform Xover
|
||||
const double bitFlipRate = 0.5; // rate for bit-flip mutation
|
||||
const double oneBitRate = 0.5; // rate for one-bit mutation
|
||||
|
||||
// GENERAL
|
||||
//////////////////////////
|
||||
// Random seed
|
||||
//////////////////////////
|
||||
//reproducible random seed: if you don't change SEED above,
|
||||
// you'll aways get the same result, NOT a random run
|
||||
rng.reseed(SEED);
|
||||
|
||||
// EVAL
|
||||
/////////////////////////////
|
||||
// Fitness function
|
||||
////////////////////////////
|
||||
// Evaluation: from a plain C++ fn to an EvalFunc Object
|
||||
// you need to give the full description of the function
|
||||
eoEvalFuncPtr<Indi, double, const vector<bool>& > eval( binary_value );
|
||||
|
||||
// INIT
|
||||
////////////////////////////////
|
||||
// Initilisation of population
|
||||
////////////////////////////////
|
||||
|
||||
// based on boolean_generator class (see utils/eoRndGenerators.h)
|
||||
eoUniformGenerator<bool> uGen;
|
||||
eoInitFixedLength<Indi> random(VEC_SIZE, uGen);
|
||||
// Initialization of the population
|
||||
eoPop<Indi> pop(POP_SIZE, random);
|
||||
|
||||
// and evaluate it in one loop
|
||||
apply<Indi>(eval, pop); // STL syntax
|
||||
|
||||
// OUTPUT
|
||||
// sort pop before printing it!
|
||||
pop.sort();
|
||||
// Print (sorted) intial population (raw printout)
|
||||
cout << "Initial Population" << endl;
|
||||
cout << pop;
|
||||
|
||||
// ENGINE
|
||||
/////////////////////////////////////
|
||||
// selection and replacement
|
||||
////////////////////////////////////
|
||||
// SELECT
|
||||
// The robust tournament selection
|
||||
eoDetTournamentSelect<Indi> selectOne(T_SIZE); // T_SIZE in [2,POP_SIZE]
|
||||
// is now encapsulated in a eoSelectPerc (entage)
|
||||
eoSelectPerc<Indi> select(selectOne);// by default rate==1
|
||||
|
||||
// REPLACE
|
||||
// And we now have the full slection/replacement - though with
|
||||
// no replacement (== generational replacement) at the moment :-)
|
||||
eoGenerationalReplacement<Indi> replace;
|
||||
|
||||
// OPERATORS
|
||||
//////////////////////////////////////
|
||||
// The variation operators
|
||||
//////////////////////////////////////
|
||||
// CROSSOVER
|
||||
// 1-point crossover for bitstring
|
||||
eo1PtBitXover<Indi> xover1;
|
||||
// uniform crossover for bitstring
|
||||
eoUBitXover<Indi> xoverU;
|
||||
// 2-pots xover
|
||||
eoNPtsBitXover<Indi> xover2(2);
|
||||
// Combine them with relative rates
|
||||
eoPropCombinedQuadOp<Indi> xover(xover1, onePointRate);
|
||||
xover.add(xoverU, URate);
|
||||
xover.add(xover2, twoPointsRate);
|
||||
|
||||
// MUTATION
|
||||
// standard bit-flip mutation for bitstring
|
||||
eoBitMutation<Indi> mutationBitFlip(P_MUT_PER_BIT);
|
||||
// mutate exactly 1 bit per individual
|
||||
eoDetBitFlip<Indi> mutationOneBit;
|
||||
// Combine them with relative rates
|
||||
eoPropCombinedMonOp<Indi> mutation(mutationBitFlip, bitFlipRate);
|
||||
mutation.add(mutationOneBit, oneBitRate, true);
|
||||
|
||||
// The operators are encapsulated into an eoTRansform object
|
||||
eoSGATransform<Indi> transform(xover, P_CROSS, mutation, P_MUT);
|
||||
|
||||
// STOP
|
||||
// CHECKPOINT
|
||||
//////////////////////////////////////
|
||||
// termination conditions: use more than one
|
||||
/////////////////////////////////////
|
||||
// stop after MAX_GEN generations
|
||||
eoGenContinue<Indi> genCont(MAX_GEN);
|
||||
// do MIN_GEN gen., then stop after STEADY_GEN gen. without improvement
|
||||
eoSteadyFitContinue<Indi> steadyCont(MIN_GEN, STEADY_GEN);
|
||||
// stop when fitness reaches a target (here VEC_SIZE)
|
||||
eoFitContinue<Indi> fitCont(VEC_SIZE);
|
||||
// do stop when one of the above says so
|
||||
eoCombinedContinue<Indi> continuator(genCont);
|
||||
continuator.add(steadyCont);
|
||||
continuator.add(fitCont);
|
||||
|
||||
// GENERATION
|
||||
/////////////////////////////////////////
|
||||
// the algorithm
|
||||
////////////////////////////////////////
|
||||
|
||||
// Easy EA requires
|
||||
// selection, transformation, eval, replacement, and stopping criterion
|
||||
eoEasyEA<Indi> gga(continuator, eval, select, transform, replace);
|
||||
|
||||
// Apply algo to pop - that's it!
|
||||
cout << "\n Here we go\n\n";
|
||||
gga(pop);
|
||||
|
||||
// OUTPUT
|
||||
// Print (sorted) intial population
|
||||
pop.sort();
|
||||
cout << "FINAL Population\n" << pop << endl;
|
||||
// GENERAL
|
||||
}
|
||||
|
||||
// A main that catches the exceptions
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
|
||||
try
|
||||
{
|
||||
main_function(argc, argv);
|
||||
}
|
||||
catch(exception& e)
|
||||
{
|
||||
cout << "Exception: " << e.what() << '\n';
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
192
tutorial/eo/Lesson2/FirstRealEA.cpp
Executable file
192
tutorial/eo/Lesson2/FirstRealEA.cpp
Executable file
|
|
@ -0,0 +1,192 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// FirstRealEA.cpp
|
||||
//-----------------------------------------------------------------------------
|
||||
//*
|
||||
// Still an instance of a VERY simple Real-coded Genetic Algorithm
|
||||
// (see FirstBitGA.cpp) but now with Breeder - and Combined Ops
|
||||
//
|
||||
//-----------------------------------------------------------------------------
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
// standard includes
|
||||
#include <stdexcept> // runtime_error
|
||||
#include <iostream> // cout
|
||||
|
||||
// the general include for eo
|
||||
#include <paradiseo/eo.h>
|
||||
#include <paradiseo/eo/es.h>
|
||||
|
||||
// REPRESENTATION
|
||||
//-----------------------------------------------------------------------------
|
||||
// define your individuals
|
||||
typedef eoReal<double> Indi;
|
||||
|
||||
// Use functions from namespace std
|
||||
using namespace std;
|
||||
|
||||
// EVALFUNC
|
||||
//-----------------------------------------------------------------------------
|
||||
// a simple fitness function that computes the euclidian norm of a real vector
|
||||
// Now in a separate file, and declared as binary_value(const vector<bool> &)
|
||||
|
||||
#include "real_value.h"
|
||||
|
||||
// GENERAL
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
void main_function(int argc, char **argv)
|
||||
{
|
||||
// PARAMETRES
|
||||
const unsigned int SEED = 42; // seed for random number generator
|
||||
const unsigned int T_SIZE = 3; // size for tournament selection
|
||||
const unsigned int VEC_SIZE = 8; // Number of object variables in genotypes
|
||||
const unsigned int POP_SIZE = 20; // Size of population
|
||||
|
||||
const unsigned int MAX_GEN = 500; // Maximum number of generation before STOP
|
||||
const unsigned int MIN_GEN = 10; // Minimum number of generation before ...
|
||||
const unsigned int STEADY_GEN = 50; // stop after STEADY_GEN gen. without improvelent
|
||||
|
||||
const float P_CROSS = 0.8; // Crossover probability
|
||||
const float P_MUT = 0.5; // mutation probability
|
||||
|
||||
const double EPSILON = 0.01; // range for real uniform mutation
|
||||
double SIGMA = 0.3; // std dev. for normal mutation
|
||||
// some parameters for chosing among different operators
|
||||
const double hypercubeRate = 0.5; // relative weight for hypercube Xover
|
||||
const double segmentRate = 0.5; // relative weight for segment Xover
|
||||
const double uniformMutRate = 0.5; // relative weight for uniform mutation
|
||||
const double detMutRate = 0.5; // relative weight for det-uniform mutation
|
||||
const double normalMutRate = 0.5; // relative weight for normal mutation
|
||||
|
||||
// GENERAL
|
||||
//////////////////////////
|
||||
// Random seed
|
||||
//////////////////////////
|
||||
//reproducible random seed: if you don't change SEED above,
|
||||
// you'll aways get the same result, NOT a random run
|
||||
rng.reseed(SEED);
|
||||
|
||||
// EVAL
|
||||
/////////////////////////////
|
||||
// Fitness function
|
||||
////////////////////////////
|
||||
// Evaluation: from a plain C++ fn to an EvalFunc Object
|
||||
// you need to give the full description of the function
|
||||
eoEvalFuncPtr<Indi, double, const vector<double>& > eval( real_value );
|
||||
|
||||
// INIT
|
||||
////////////////////////////////
|
||||
// Initilisation of population
|
||||
////////////////////////////////
|
||||
// based on a uniform generator
|
||||
eoUniformGenerator<double> uGen(-1.0, 1.0);
|
||||
eoInitFixedLength<Indi> random(VEC_SIZE, uGen);
|
||||
// Initialization of the population
|
||||
eoPop<Indi> pop(POP_SIZE, random);
|
||||
|
||||
// and evaluate it in one loop
|
||||
apply<Indi>(eval, pop); // STL syntax
|
||||
|
||||
// OUTPUT
|
||||
// sort pop before printing it!
|
||||
pop.sort();
|
||||
// Print (sorted) intial population (raw printout)
|
||||
cout << "Initial Population" << endl;
|
||||
cout << pop;
|
||||
|
||||
// ENGINE
|
||||
/////////////////////////////////////
|
||||
// selection and replacement
|
||||
////////////////////////////////////
|
||||
// SELECT
|
||||
// The robust tournament selection
|
||||
eoDetTournamentSelect<Indi> selectOne(T_SIZE);
|
||||
// is now encapsulated in a eoSelectPerc (entage)
|
||||
eoSelectPerc<Indi> select(selectOne);// by default rate==1
|
||||
|
||||
// REPLACE
|
||||
// And we now have the full slection/replacement - though with
|
||||
// no replacement (== generational replacement) at the moment :-)
|
||||
eoGenerationalReplacement<Indi> replace;
|
||||
|
||||
// OPERATORS
|
||||
//////////////////////////////////////
|
||||
// The variation operators
|
||||
//////////////////////////////////////
|
||||
// CROSSOVER
|
||||
// uniform chooce on segment made by the parents
|
||||
eoSegmentCrossover<Indi> xoverS;
|
||||
// uniform choice in hypercube built by the parents
|
||||
eoHypercubeCrossover<Indi> xoverA;
|
||||
// Combine them with relative weights
|
||||
eoPropCombinedQuadOp<Indi> xover(xoverS, segmentRate);
|
||||
xover.add(xoverA, hypercubeRate);
|
||||
|
||||
// MUTATION
|
||||
// offspring(i) uniformly chosen in [parent(i)-epsilon, parent(i)+epsilon]
|
||||
eoUniformMutation<Indi> mutationU(EPSILON);
|
||||
// k (=1) coordinates of parents are uniformly modified
|
||||
eoDetUniformMutation<Indi> mutationD(EPSILON);
|
||||
// all coordinates of parents are normally modified (stDev SIGMA)
|
||||
eoNormalMutation<Indi> mutationN(SIGMA);
|
||||
// Combine them with relative weights
|
||||
eoPropCombinedMonOp<Indi> mutation(mutationU, uniformMutRate);
|
||||
mutation.add(mutationD, detMutRate);
|
||||
mutation.add(mutationN, normalMutRate, true);
|
||||
|
||||
// STOP
|
||||
// CHECKPOINT
|
||||
//////////////////////////////////////
|
||||
// termination conditions: use more than one
|
||||
/////////////////////////////////////
|
||||
// stop after MAX_GEN generations
|
||||
eoGenContinue<Indi> genCont(MAX_GEN);
|
||||
// do MIN_GEN gen., then stop after STEADY_GEN gen. without improvement
|
||||
eoSteadyFitContinue<Indi> steadyCont(MIN_GEN, STEADY_GEN);
|
||||
// stop when fitness reaches a target (here VEC_SIZE)
|
||||
eoFitContinue<Indi> fitCont(0);
|
||||
// do stop when one of the above says so
|
||||
eoCombinedContinue<Indi> continuator(genCont);
|
||||
continuator.add(steadyCont);
|
||||
continuator.add(fitCont);
|
||||
|
||||
// The operators are encapsulated into an eoTRansform object
|
||||
eoSGATransform<Indi> transform(xover, P_CROSS, mutation, P_MUT);
|
||||
|
||||
// GENERATION
|
||||
/////////////////////////////////////////
|
||||
// the algorithm
|
||||
////////////////////////////////////////
|
||||
|
||||
// Easy EA requires
|
||||
// selection, transformation, eval, replacement, and stopping criterion
|
||||
eoEasyEA<Indi> gga(continuator, eval, select, transform, replace);
|
||||
|
||||
// Apply algo to pop - that's it!
|
||||
cout << "\n Here we go\n\n";
|
||||
gga(pop);
|
||||
|
||||
// OUTPUT
|
||||
// Print (sorted) intial population
|
||||
pop.sort();
|
||||
cout << "FINAL Population\n" << pop << endl;
|
||||
// GENERAL
|
||||
}
|
||||
|
||||
// A main that catches the exceptions
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
try
|
||||
{
|
||||
main_function(argc, argv);
|
||||
}
|
||||
catch(exception& e)
|
||||
{
|
||||
cout << "Exception: " << e.what() << '\n';
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
34
tutorial/eo/Lesson2/Makefile.simple
Executable file
34
tutorial/eo/Lesson2/Makefile.simple
Executable file
|
|
@ -0,0 +1,34 @@
|
|||
### This Makefile is part of the tutorial of the EO library
|
||||
# Unlike other Makefiles in EO, it is not using the automake/autoconf
|
||||
# so that it stays easy to understant (you are in the tutorial, remember!)
|
||||
# MS, Oct. 2002
|
||||
|
||||
# if you use this Makefile as a starting point for another application
|
||||
# you might need to modify the following
|
||||
DIR_EO = ../../src
|
||||
|
||||
.SUFFIXES: .cpp
|
||||
|
||||
# Warning: $(CXX) in Linux (RedHat and Mandrake at least) is g++
|
||||
# However, if you are using this Makefile within xemacs,
|
||||
# and have problems with the interpretation of the output (and its colors)
|
||||
# then you should use c++ instead (make CXX=c++ will do)
|
||||
|
||||
.cpp: ; $(CXX) -DPACKAGE=\"eo\" -DVERSION=\"0.9.3\" -I. -I$(DIR_EO) -Wall -g -pg -o $@ $*.cpp $(DIR_EO)/utils/libeoutils.a $(DIR_EO)/libeo.a
|
||||
|
||||
.cpp.o: ; $(CXX) -DPACKAGE=\"eo\" -DVERSION=\"0.9.3\" -I. -I$(DIR_EO) -Wall -g -c -pg $*.cpp
|
||||
|
||||
firstEA = FirstRealEA FirstBitEA
|
||||
|
||||
ALL = $(firstEA) exercise2.3
|
||||
|
||||
lesson2 : $(firstEA)
|
||||
|
||||
all : $(ALL)
|
||||
|
||||
clean :
|
||||
@/bin/rm $(ALL) *.o *~
|
||||
|
||||
FirstRealEA : real_value.h
|
||||
|
||||
FirstBitEA : binary_value.h
|
||||
16
tutorial/eo/Lesson2/binary_value.h
Executable file
16
tutorial/eo/Lesson2/binary_value.h
Executable file
|
|
@ -0,0 +1,16 @@
|
|||
#include <paradiseo/eo.h>
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
/** Just a simple function that takes binary value of a chromosome and sets
|
||||
the fitnes.
|
||||
@param _chrom A binary chromosome
|
||||
*/
|
||||
// INIT
|
||||
double binary_value(const std::vector<bool>& _chrom)
|
||||
{
|
||||
double sum = 0;
|
||||
for (unsigned i = 0; i < _chrom.size(); i++)
|
||||
sum += _chrom[i];
|
||||
return sum;
|
||||
}
|
||||
197
tutorial/eo/Lesson2/exercise2.3.cpp
Executable file
197
tutorial/eo/Lesson2/exercise2.3.cpp
Executable file
|
|
@ -0,0 +1,197 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// FirstBitEA.cpp
|
||||
//-----------------------------------------------------------------------------
|
||||
//*
|
||||
// Still an instance of a VERY simple Bitstring Genetic Algorithm
|
||||
// (see FirstBitGA.cpp) but now with Breeder - and Combined Ops
|
||||
//
|
||||
//-----------------------------------------------------------------------------
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
// standard includes
|
||||
#include <stdexcept> // runtime_error
|
||||
#include <iostream> // cout
|
||||
|
||||
// the general include for eo
|
||||
#include <paradiseo/eo.h>
|
||||
|
||||
// REPRESENTATION
|
||||
//-----------------------------------------------------------------------------
|
||||
// Include the corresponding file
|
||||
#include <paradiseo/eo/ga.h> // bitstring representation & operators
|
||||
// define your individuals
|
||||
typedef eoBit<double> Indi; // A bitstring with fitness double
|
||||
|
||||
// EVAL
|
||||
//-----------------------------------------------------------------------------
|
||||
// a simple fitness function that computes the number of ones of a bitstring
|
||||
// Now in a separate file, and declared as binary_value(const vector<bool> &)
|
||||
|
||||
#include "binary_value.h"
|
||||
|
||||
// GENERAL
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
using namespace std;
|
||||
|
||||
void main_function(int argc, char **argv)
|
||||
{
|
||||
// PARAMETRES
|
||||
const unsigned int SEED = 42; // seed for random number generator
|
||||
const unsigned int T_SIZE = 3; // size for tournament selection
|
||||
const unsigned int VEC_SIZE = 20; // Number of bits in genotypes
|
||||
const unsigned int POP_SIZE = 20; // Size of population
|
||||
|
||||
const unsigned int MAX_GEN = 500; // Maximum number of generation before STOP
|
||||
const unsigned int MIN_GEN = 10; // Minimum number of generation before ...
|
||||
const unsigned int STEADY_GEN = 50; // stop after STEADY_GEN gen. without improvelent
|
||||
|
||||
const double P_CROSS = 0.8; // Crossover probability
|
||||
const double P_MUT = 1.0; // mutation probability
|
||||
|
||||
const double P_MUT_PER_BIT = 0.01; // internal probability for bit-flip mutation
|
||||
// some parameters for chosing among different operators
|
||||
const double onePointRate = 0.5; // rate for 1-pt Xover
|
||||
const double twoPointsRate = 0.5; // rate for 2-pt Xover
|
||||
const double URate = 0.5; // rate for Uniform Xover
|
||||
const double bitFlipRate = 0.5; // rate for bit-flip mutation
|
||||
const double oneBitRate = 0.5; // rate for one-bit mutation
|
||||
|
||||
// GENERAL
|
||||
//////////////////////////
|
||||
// Random seed
|
||||
//////////////////////////
|
||||
//reproducible random seed: if you don't change SEED above,
|
||||
// you'll aways get the same result, NOT a random run
|
||||
rng.reseed(SEED);
|
||||
|
||||
// EVAL
|
||||
/////////////////////////////
|
||||
// Fitness function
|
||||
////////////////////////////
|
||||
// Evaluation: from a plain C++ fn to an EvalFunc Object
|
||||
// you need to give the full description of the function
|
||||
eoEvalFuncPtr<Indi, double, const vector<bool>& > eval( binary_value );
|
||||
|
||||
// INIT
|
||||
////////////////////////////////
|
||||
// Initilisation of population
|
||||
////////////////////////////////
|
||||
|
||||
// based on boolean_generator class (see utils/rnd_generator.h)
|
||||
eoUniformGenerator<bool> uGen;
|
||||
eoInitFixedLength<Indi> random(VEC_SIZE, uGen);
|
||||
// Initialization of the population
|
||||
eoPop<Indi> pop(POP_SIZE, random);
|
||||
|
||||
// and evaluate it in one loop
|
||||
apply<Indi>(eval, pop); // STL syntax
|
||||
|
||||
// OUTPUT
|
||||
// sort pop before printing it!
|
||||
pop.sort();
|
||||
// Print (sorted) intial population (raw printout)
|
||||
cout << "Initial Population" << endl;
|
||||
cout << pop;
|
||||
|
||||
// ENGINE
|
||||
/////////////////////////////////////
|
||||
// selection and replacement
|
||||
////////////////////////////////////
|
||||
// SELECT
|
||||
// The robust tournament selection
|
||||
eoDetTournamentSelect<Indi> selectOne(T_SIZE); // T_SIZE in [2,POP_SIZE]
|
||||
// solution solution solution solution solution solution solution
|
||||
// modify the nb offspring / rate in the constructor. 2 ways:
|
||||
// second arg treated as integer
|
||||
eoSelectMany<Indi> select(selectOne,2, eo_is_an_integer);
|
||||
// second arg treated as a rate (default behavior)
|
||||
// eoSelectMany<Indi> select(selectOne,0.1);
|
||||
|
||||
// REPLACE
|
||||
// solution solution solution solution solution solution solution
|
||||
// eoCommaReplacement keeps the best among offspring
|
||||
// eoPlusReplacement keeps the best among parents + offspring
|
||||
// eoCommaReplacement<Indi> replace;
|
||||
eoPlusReplacement<Indi> replace;
|
||||
|
||||
// OPERATORS
|
||||
//////////////////////////////////////
|
||||
// The variation operators
|
||||
//////////////////////////////////////
|
||||
// CROSSOVER
|
||||
// 1-point crossover for bitstring
|
||||
eo1PtBitXover<Indi> xover1;
|
||||
// uniform crossover for bitstring
|
||||
eoUBitXover<Indi> xoverU;
|
||||
// 2-pots xover
|
||||
eoNPtsBitXover<Indi> xover2(2);
|
||||
// Combine them with relative rates
|
||||
eoPropCombinedQuadOp<Indi> xover(xover1, onePointRate);
|
||||
xover.add(xoverU, URate);
|
||||
xover.add(xover2, twoPointsRate);
|
||||
|
||||
// MUTATION
|
||||
// standard bit-flip mutation for bitstring
|
||||
eoBitMutation<Indi> mutationBitFlip(P_MUT_PER_BIT);
|
||||
// mutate exactly 1 bit per individual
|
||||
eoDetBitFlip<Indi> mutationOneBit;
|
||||
// Combine them with relative rates
|
||||
eoPropCombinedMonOp<Indi> mutation(mutationBitFlip, bitFlipRate);
|
||||
mutation.add(mutationOneBit, oneBitRate, true);
|
||||
|
||||
// The operators are encapsulated into an eoTRansform object
|
||||
eoSGATransform<Indi> transform(xover, P_CROSS, mutation, P_MUT);
|
||||
|
||||
// STOP
|
||||
// CHECKPOINT
|
||||
//////////////////////////////////////
|
||||
// termination conditions: use more than one
|
||||
/////////////////////////////////////
|
||||
// stop after MAX_GEN generations
|
||||
eoGenContinue<Indi> genCont(MAX_GEN);
|
||||
// do MIN_GEN gen., then stop after STEADY_GEN gen. without improvement
|
||||
eoSteadyFitContinue<Indi> steadyCont(MIN_GEN, STEADY_GEN);
|
||||
// stop when fitness reaches a target (here VEC_SIZE)
|
||||
eoFitContinue<Indi> fitCont(VEC_SIZE);
|
||||
// do stop when one of the above says so
|
||||
eoCombinedContinue<Indi> continuator(genCont);
|
||||
continuator.add(steadyCont);
|
||||
continuator.add(fitCont);
|
||||
|
||||
// GENERATION
|
||||
/////////////////////////////////////////
|
||||
// the algorithm
|
||||
////////////////////////////////////////
|
||||
|
||||
// Easy EA requires
|
||||
// selection, transformation, eval, replacement, and stopping criterion
|
||||
eoEasyEA<Indi> gga(continuator, eval, select, transform, replace);
|
||||
|
||||
// Apply algo to pop - that's it!
|
||||
gga(pop);
|
||||
|
||||
// OUTPUT
|
||||
// Print (sorted) intial population
|
||||
pop.sort();
|
||||
cout << "FINAL Population\n" << pop << endl;
|
||||
// GENERAL
|
||||
}
|
||||
|
||||
// A main that catches the exceptions
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
try
|
||||
{
|
||||
main_function(argc, argv);
|
||||
}
|
||||
catch(exception& e)
|
||||
{
|
||||
cout << "Exception: " << e.what() << '\n';
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
17
tutorial/eo/Lesson2/real_value.h
Executable file
17
tutorial/eo/Lesson2/real_value.h
Executable file
|
|
@ -0,0 +1,17 @@
|
|||
#include <vector>
|
||||
//-----------------------------------------------------------------------------
|
||||
/** Just a simple function that takes an vector<double> and sets the fitnes
|
||||
to the sphere function. Please use doubles not float!!!
|
||||
@param _ind A floatingpoint vector
|
||||
*/
|
||||
|
||||
// INIT
|
||||
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 -sum;
|
||||
}
|
||||
64
tutorial/eo/Lesson3/CMakeLists.txt
Executable file
64
tutorial/eo/Lesson3/CMakeLists.txt
Executable file
|
|
@ -0,0 +1,64 @@
|
|||
######################################################################################
|
||||
### 1) Include the sources
|
||||
######################################################################################
|
||||
|
||||
#include_directories(${EO_SRC_DIR}/src)
|
||||
#include_directories(${EO_SRC_DIR}/src/ga)
|
||||
#include_directories(${EO_SRC_DIR}/src/utils)
|
||||
|
||||
######################################################################################
|
||||
### 2) Specify where CMake can find the libraries
|
||||
######################################################################################
|
||||
|
||||
if(NOT WIN32 OR CYGWIN)
|
||||
link_directories(${EO_BIN_DIR}/lib)
|
||||
endif(NOT WIN32 OR CYGWIN)
|
||||
|
||||
# especially for Visual Studio
|
||||
if(WIN32 AND NOT CYGWIN)
|
||||
link_directories(${EO_BIN_DIR}\\lib\\${CMAKE_BUILD_TYPE})
|
||||
endif(WIN32 AND NOT CYGWIN)
|
||||
|
||||
######################################################################################
|
||||
### 3) Define your targets
|
||||
######################################################################################
|
||||
|
||||
# no matter what is the OS, hopefully
|
||||
add_executable(SecondBitEA SecondBitEA.cpp)
|
||||
add_executable(SecondRealEA SecondRealEA.cpp)
|
||||
add_executable(exercise3.1 exercise3.1.cpp)
|
||||
|
||||
add_dependencies(SecondBitEA ga eoutils eo)
|
||||
add_dependencies(SecondRealEA ga eoutils eo)
|
||||
add_dependencies(exercise3.1 ga eoutils eo)
|
||||
|
||||
######################################################################################
|
||||
### 4) Optionnal
|
||||
######################################################################################
|
||||
|
||||
set(SECONDBITEA_VERSION ${GLOBAL_VERSION})
|
||||
set_target_properties(SecondBitEA PROPERTIES VERSION "${SECONDBITEA_VERSION}")
|
||||
|
||||
set(SECONDREALEA_VERSION ${GLOBAL_VERSION})
|
||||
set_target_properties(SecondRealEA PROPERTIES VERSION "${SECONDREALEA_VERSION}")
|
||||
|
||||
set(EXERCICE31_VERSION ${GLOBAL_VERSION})
|
||||
set_target_properties(exercise3.1 PROPERTIES VERSION "${EXERCICE31_VERSION}")
|
||||
|
||||
######################################################################################
|
||||
### 5) Link the librairies for the targets
|
||||
######################################################################################
|
||||
|
||||
target_link_libraries(SecondBitEA ga eoutils eo)
|
||||
target_link_libraries(SecondRealEA ga eoutils eo)
|
||||
target_link_libraries(exercise3.1 ga eoutils eo)
|
||||
|
||||
######################################################################################
|
||||
### 6) Configure project installation paths
|
||||
######################################################################################
|
||||
|
||||
install(TARGETS SecondBitEA RUNTIME DESTINATION share/${PROJECT_TAG}/eo/examples/Lesson3 COMPONENT examples)
|
||||
install(TARGETS SecondRealEA RUNTIME DESTINATION share/${PROJECT_TAG}/eo/examples/Lesson3 COMPONENT examples)
|
||||
install(TARGETS exercise3.1 RUNTIME DESTINATION share/${PROJECT_TAG}/eo/examples/Lesson3 COMPONENT examples)
|
||||
|
||||
######################################################################################
|
||||
33
tutorial/eo/Lesson3/Makefile.simple
Executable file
33
tutorial/eo/Lesson3/Makefile.simple
Executable file
|
|
@ -0,0 +1,33 @@
|
|||
### This Makefile is part of the tutorial of the EO library
|
||||
# Unlike other Makefiles in EO, it is not using the automake/autoconf
|
||||
# so that it stays easy to understant (you are in the tutorial, remember!)
|
||||
# MS, Oct. 2002
|
||||
|
||||
# if you use this Makefile as a starting point for another application
|
||||
# you might need to modify the following
|
||||
DIR_EO = ../../src
|
||||
|
||||
.SUFFIXES: .cpp
|
||||
|
||||
# Warning: $(CXX) in Linux (RedHat and Mandrake at least) is g++
|
||||
# However, if you are using this Makefile within xemacs,
|
||||
# and have problems with the interpretation of the output (and its colors)
|
||||
# then you should use c++ instead (make CXX=c++ will do)
|
||||
|
||||
.cpp: ; $(CXX) -DPACKAGE=\"eo\" -DVERSION=\"0.9.3\" -I. -I$(DIR_EO) -Wall -g -pg -o $@ $*.cpp $(DIR_EO)/utils/libeoutils.a $(DIR_EO)/libeo.a
|
||||
|
||||
.cpp.o: ; $(CXX) -DPACKAGE=\"eo\" -DVERSION=\"0.9.3\" -I. -I$(DIR_EO) -Wall -g -c -pg $*.cpp
|
||||
|
||||
secondEA = SecondBitEA SecondRealEA
|
||||
|
||||
ALL = $(secondEA) exercise3.1
|
||||
|
||||
lesson3 : $(secondEA)
|
||||
|
||||
all : $(ALL)
|
||||
|
||||
SecondBitEA : binary_value.h
|
||||
SecondRealEA : real_value.h
|
||||
|
||||
clean :
|
||||
@/bin/rm $(ALL) *.o *.sav *.xg *.status *~
|
||||
348
tutorial/eo/Lesson3/SecondBitEA.cpp
Executable file
348
tutorial/eo/Lesson3/SecondBitEA.cpp
Executable file
|
|
@ -0,0 +1,348 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// SecondGA.cpp
|
||||
//-----------------------------------------------------------------------------
|
||||
//*
|
||||
// Same code than FirstBitEA as far as Evolutionary Computation is concerned
|
||||
// but now you learn to enter the parameters in a more flexible way
|
||||
// and to twidle the output to your preferences!
|
||||
//-----------------------------------------------------------------------------
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
// standard includes
|
||||
#include <fstream>
|
||||
#include <iostream> // cout
|
||||
#include <stdexcept> // runtime_error
|
||||
|
||||
// the general include for eo
|
||||
#include <paradiseo/eo.h>
|
||||
#include <paradiseo/eo/ga.h>
|
||||
|
||||
// Use functions from namespace std
|
||||
using namespace std;
|
||||
|
||||
// EVAL
|
||||
#include "binary_value.h"
|
||||
|
||||
// REPRESENTATION
|
||||
//-----------------------------------------------------------------------------
|
||||
// define your genotype and fitness types
|
||||
typedef eoBit<double> Indi;
|
||||
|
||||
// the main_function: nothing changed(!), except variable initialization
|
||||
void main_function(int argc, char **argv)
|
||||
{
|
||||
// PARAMETRES
|
||||
//-----------------------------------------------------------------------------
|
||||
// instead of having all values of useful parameters as constants, read them:
|
||||
// either on the command line (--option=value or -o=value)
|
||||
// or in a parameter file (same syntax, order independent,
|
||||
// # = usual comment character
|
||||
// or in the environment (TODO)
|
||||
|
||||
// First define a parser from the command-line arguments
|
||||
eoParser parser(argc, argv);
|
||||
|
||||
// For each parameter, define Parameter, read it through the parser,
|
||||
// and assign the value to the variable
|
||||
|
||||
eoValueParam<uint32_t> seedParam(time(0), "seed", "Random number seed", 'S');
|
||||
parser.processParam( seedParam );
|
||||
unsigned seed = seedParam.value();
|
||||
|
||||
// description of genotype
|
||||
eoValueParam<unsigned int> vecSizeParam(8, "vecSize", "Genotype size",'V');
|
||||
parser.processParam( vecSizeParam, "Representation" );
|
||||
unsigned vecSize = vecSizeParam.value();
|
||||
|
||||
// parameters for evolution engine
|
||||
eoValueParam<unsigned int> popSizeParam(10, "popSize", "Population size",'P');
|
||||
parser.processParam( popSizeParam, "Evolution engine" );
|
||||
unsigned popSize = popSizeParam.value();
|
||||
|
||||
eoValueParam<unsigned int> tSizeParam(10, "tSize", "Tournament size",'T');
|
||||
parser.processParam( tSizeParam, "Evolution Engine" );
|
||||
unsigned tSize = tSizeParam.value();
|
||||
|
||||
// init and stop
|
||||
eoValueParam<string> loadNameParam("", "Load","A save file to restart from",'L');
|
||||
parser.processParam( loadNameParam, "Persistence" );
|
||||
string loadName = loadNameParam.value();
|
||||
|
||||
eoValueParam<unsigned int> maxGenParam(100, "maxGen", "Maximum number of generations",'G');
|
||||
parser.processParam( maxGenParam, "Stopping criterion" );
|
||||
unsigned maxGen = maxGenParam.value();
|
||||
|
||||
eoValueParam<unsigned int> minGenParam(100, "minGen", "Minimum number of generations",'g');
|
||||
parser.processParam( minGenParam, "Stopping criterion" );
|
||||
unsigned minGen = minGenParam.value();
|
||||
|
||||
eoValueParam<unsigned int> steadyGenParam(100, "steadyGen", "Number of generations with no improvement",'s');
|
||||
parser.processParam( steadyGenParam, "Stopping criterion" );
|
||||
unsigned steadyGen = steadyGenParam.value();
|
||||
|
||||
// operators probabilities at the algorithm level
|
||||
eoValueParam<double> pCrossParam(0.6, "pCross", "Probability of Crossover", 'C');
|
||||
parser.processParam( pCrossParam, "Genetic Operators" );
|
||||
double pCross = pCrossParam.value();
|
||||
|
||||
eoValueParam<double> pMutParam(0.1, "pMut", "Probability of Mutation", 'M');
|
||||
parser.processParam( pMutParam, "Genetic Operators" );
|
||||
double pMut = pMutParam.value();
|
||||
|
||||
// relative rates for crossovers
|
||||
eoValueParam<double> onePointRateParam(1, "onePointRate", "Relative rate for one point crossover", '1');
|
||||
parser.processParam( onePointRateParam, "Genetic Operators" );
|
||||
double onePointRate = onePointRateParam.value();
|
||||
|
||||
eoValueParam<double> twoPointsRateParam(1, "twoPointRate", "Relative rate for two point crossover", '2');
|
||||
parser.processParam( twoPointsRateParam, "Genetic Operators" );
|
||||
double twoPointsRate = twoPointsRateParam.value();
|
||||
|
||||
eoValueParam<double> uRateParam(2, "uRate", "Relative rate for uniform crossover", 'U');
|
||||
parser.processParam( uRateParam, "Genetic Operators" );
|
||||
double URate = uRateParam.value();
|
||||
|
||||
// relative rates and private parameters for mutations;
|
||||
eoValueParam<double> pMutPerBitParam(0.01, "pMutPerBit", "Probability of flipping 1 bit in bit-flip mutation", 'b');
|
||||
parser.processParam( pMutPerBitParam, "Genetic Operators" );
|
||||
double pMutPerBit = pMutPerBitParam.value();
|
||||
|
||||
eoValueParam<double> bitFlipRateParam(0.01, "bitFlipRate", "Relative rate for bit-flip mutation", 'B');
|
||||
parser.processParam( bitFlipRateParam, "Genetic Operators" );
|
||||
double bitFlipRate = bitFlipRateParam.value();
|
||||
|
||||
eoValueParam<double> oneBitRateParam(0.01, "oneBitRate", "Relative rate for deterministic bit-flip mutation", 'D');
|
||||
parser.processParam( oneBitRateParam, "Genetic Operators" );
|
||||
double oneBitRate = oneBitRateParam.value();
|
||||
|
||||
// the name of the "status" file where all actual parameter values will be saved
|
||||
string str_status = parser.ProgramName() + ".status"; // default value
|
||||
eoValueParam<string> statusParam(str_status.c_str(), "status","Status file",'S');
|
||||
parser.processParam( statusParam, "Persistence" );
|
||||
|
||||
// do the following AFTER ALL PARAMETERS HAVE BEEN PROCESSED
|
||||
// i.e. in case you need parameters somewhere else, postpone these
|
||||
if (parser.userNeedsHelp())
|
||||
{
|
||||
parser.printHelp(cout);
|
||||
exit(1);
|
||||
}
|
||||
if (statusParam.value() != "")
|
||||
{
|
||||
ofstream os(statusParam.value().c_str());
|
||||
os << parser; // and you can use that file as parameter file
|
||||
}
|
||||
|
||||
// EVAL
|
||||
/////////////////////////////
|
||||
// Fitness function
|
||||
////////////////////////////
|
||||
// Evaluation: from a plain C++ fn to an EvalFunc Object ...
|
||||
eoEvalFuncPtr<Indi, double, const vector<bool>& > plainEval( binary_value );
|
||||
// ... to an object that counts the nb of actual evaluations
|
||||
eoEvalFuncCounter<Indi> eval(plainEval);
|
||||
|
||||
// INIT
|
||||
////////////////////////////////
|
||||
// Initilisation of population
|
||||
////////////////////////////////
|
||||
// Either load or initialize
|
||||
// create an empty pop
|
||||
eoPop<Indi> pop;
|
||||
// create a state for reading
|
||||
eoState inState; // a state for loading - WITHOUT the parser
|
||||
// register the rng and the pop in the state, so they can be loaded,
|
||||
// and the present run will be the exact conitnuation of the saved run
|
||||
// eventually with different parameters
|
||||
inState.registerObject(rng);
|
||||
inState.registerObject(pop);
|
||||
|
||||
if (loadName != "")
|
||||
{
|
||||
inState.load(loadName); // load the pop and the rng
|
||||
// the fitness is read in the file:
|
||||
// do only evaluate the pop if the fitness has changed
|
||||
}
|
||||
else
|
||||
{
|
||||
rng.reseed(seed);
|
||||
// a Indi random initializer
|
||||
// based on boolean_generator class (see utils/rnd_generator.h)
|
||||
eoUniformGenerator<bool> uGen;
|
||||
eoInitFixedLength<Indi> random(vecSize, uGen);
|
||||
|
||||
// Init pop from the randomizer: need to use the append function
|
||||
pop.append(popSize, random);
|
||||
// and evaluate pop (STL syntax)
|
||||
apply<Indi>(eval, pop);
|
||||
} // end of initializatio of the population
|
||||
|
||||
// OUTPUT
|
||||
// sort pop for pretty printout
|
||||
pop.sort();
|
||||
// Print (sorted) intial population (raw printout)
|
||||
cout << "Initial Population" << endl << pop << endl;
|
||||
|
||||
// ENGINE
|
||||
/////////////////////////////////////
|
||||
// selection and replacement
|
||||
////////////////////////////////////
|
||||
// SELECT
|
||||
// The robust tournament selection
|
||||
eoDetTournamentSelect<Indi> selectOne(tSize); // tSize in [2,POPSIZE]
|
||||
// is now encapsulated in a eoSelectPerc (stands for Percentage)
|
||||
eoSelectPerc<Indi> select(selectOne);
|
||||
|
||||
// REPLACE
|
||||
// And we now have the full slection/replacement - though with
|
||||
// the same generational replacement at the moment :-)
|
||||
eoGenerationalReplacement<Indi> replace;
|
||||
|
||||
// OPERATORS
|
||||
//////////////////////////////////////
|
||||
// The variation operators
|
||||
//////////////////////////////////////
|
||||
// CROSSOVER
|
||||
// 1-point crossover for bitstring
|
||||
eo1PtBitXover<Indi> xover1;
|
||||
// uniform crossover for bitstring
|
||||
eoUBitXover<Indi> xoverU;
|
||||
// 2-pots xover
|
||||
eoNPtsBitXover<Indi> xover2(2);
|
||||
// Combine them with relative rates
|
||||
eoPropCombinedQuadOp<Indi> xover(xover1, onePointRate);
|
||||
xover.add(xoverU, URate);
|
||||
xover.add(xover2, twoPointsRate);
|
||||
|
||||
// MUTATION
|
||||
// standard bit-flip mutation for bitstring
|
||||
eoBitMutation<Indi> mutationBitFlip(pMutPerBit);
|
||||
// mutate exactly 1 bit per individual
|
||||
eoDetBitFlip<Indi> mutationOneBit;
|
||||
// Combine them with relative rates
|
||||
eoPropCombinedMonOp<Indi> mutation(mutationBitFlip, bitFlipRate);
|
||||
mutation.add(mutationOneBit, oneBitRate, true);
|
||||
|
||||
// The operators are encapsulated into an eoTRansform object
|
||||
eoSGATransform<Indi> transform(xover, pCross, mutation, pMut);
|
||||
|
||||
// STOP
|
||||
//////////////////////////////////////
|
||||
// termination condition see FirstBitEA.cpp
|
||||
/////////////////////////////////////
|
||||
eoGenContinue<Indi> genCont(maxGen);
|
||||
eoSteadyFitContinue<Indi> steadyCont(minGen, steadyGen);
|
||||
eoFitContinue<Indi> fitCont(vecSize);
|
||||
eoCombinedContinue<Indi> continuator(genCont);
|
||||
continuator.add(steadyCont);
|
||||
continuator.add(fitCont);
|
||||
|
||||
|
||||
// CHECKPOINT
|
||||
// but now you want to make many different things every generation
|
||||
// (e.g. statistics, plots, ...).
|
||||
// the class eoCheckPoint is dedicated to just that:
|
||||
|
||||
// Declare a checkpoint (from a continuator: an eoCheckPoint
|
||||
// IS AN eoContinue and will be called in the loop of all algorithms)
|
||||
eoCheckPoint<Indi> checkpoint(continuator);
|
||||
|
||||
// Create a counter parameter
|
||||
eoValueParam<unsigned> generationCounter(0, "Gen.");
|
||||
|
||||
// Create an incrementor (sub-class of eoUpdater). Note that the
|
||||
// parameter's value is passed by reference,
|
||||
// so every time the incrementer is updated (every generation),
|
||||
// the data in generationCounter will change.
|
||||
eoIncrementor<unsigned> increment(generationCounter.value());
|
||||
|
||||
// Add it to the checkpoint,
|
||||
// so the counter is updated (here, incremented) every generation
|
||||
checkpoint.add(increment);
|
||||
|
||||
// now some statistics on the population:
|
||||
// Best fitness in population
|
||||
eoBestFitnessStat<Indi> bestStat;
|
||||
// Second moment stats: average and stdev
|
||||
eoSecondMomentStats<Indi> SecondStat;
|
||||
|
||||
// Add them to the checkpoint to get them called at the appropriate time
|
||||
checkpoint.add(bestStat);
|
||||
checkpoint.add(SecondStat);
|
||||
|
||||
// The Stdout monitor will print parameters to the screen ...
|
||||
eoStdoutMonitor monitor(false);
|
||||
|
||||
// when called by the checkpoint (i.e. at every generation)
|
||||
checkpoint.add(monitor);
|
||||
|
||||
// the monitor will output a series of parameters: add them
|
||||
monitor.add(generationCounter);
|
||||
monitor.add(eval); // because now eval is an eoEvalFuncCounter!
|
||||
monitor.add(bestStat);
|
||||
monitor.add(SecondStat);
|
||||
|
||||
// A file monitor: will print parameters to ... a File, yes, you got it!
|
||||
eoFileMonitor fileMonitor("stats.xg", " ");
|
||||
|
||||
// the checkpoint mechanism can handle multiple monitors
|
||||
checkpoint.add(fileMonitor);
|
||||
|
||||
// the fileMonitor can monitor parameters, too, but you must tell it!
|
||||
fileMonitor.add(generationCounter);
|
||||
fileMonitor.add(bestStat);
|
||||
fileMonitor.add(SecondStat);
|
||||
|
||||
// Last type of item the eoCheckpoint can handle: state savers:
|
||||
eoState outState;
|
||||
// Register the algorithm into the state (so it has something to save!!)
|
||||
outState.registerObject(parser);
|
||||
outState.registerObject(pop);
|
||||
outState.registerObject(rng);
|
||||
|
||||
// and feed the state to state savers
|
||||
// save state every 100th generation
|
||||
eoCountedStateSaver stateSaver1(20, outState, "generation");
|
||||
// save state every 1 seconds
|
||||
eoTimedStateSaver stateSaver2(1, outState, "time");
|
||||
|
||||
// Don't forget to add the two savers to the checkpoint
|
||||
checkpoint.add(stateSaver1);
|
||||
checkpoint.add(stateSaver2);
|
||||
// and that's it for the (control and) output
|
||||
|
||||
// GENERATION
|
||||
/////////////////////////////////////////
|
||||
// the algorithm
|
||||
////////////////////////////////////////
|
||||
|
||||
// Easy EA requires
|
||||
// stopping criterion, eval, selection, transformation, replacement
|
||||
eoEasyEA<Indi> gga(checkpoint, eval, select, transform, replace);
|
||||
|
||||
// Apply algo to pop - that's it!
|
||||
gga(pop);
|
||||
|
||||
// OUTPUT
|
||||
// Print (sorted) intial population
|
||||
pop.sort();
|
||||
cout << "FINAL Population\n" << pop << endl;
|
||||
// GENERAL
|
||||
}
|
||||
|
||||
// A main that catches the exceptions
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
try
|
||||
{
|
||||
main_function(argc, argv);
|
||||
}
|
||||
catch(exception& e)
|
||||
{
|
||||
cout << "Exception: " << e.what() << '\n';
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
329
tutorial/eo/Lesson3/SecondRealEA.cpp
Executable file
329
tutorial/eo/Lesson3/SecondRealEA.cpp
Executable file
|
|
@ -0,0 +1,329 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// SecondRealEA.cpp
|
||||
//-----------------------------------------------------------------------------
|
||||
//*
|
||||
// Same code than FirstBitEA as far as Evolutionary Computation is concerned
|
||||
// but now you learn to enter the parameters in a more flexible way
|
||||
// (also slightly different than in SecondBitEA.cpp)
|
||||
// and to twidle the output to your preferences (as in SecondBitEA.cpp)
|
||||
//
|
||||
//-----------------------------------------------------------------------------
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
// standard includes
|
||||
#include <fstream>
|
||||
#include <iostream> // cout
|
||||
#include <stdexcept> // runtime_error
|
||||
|
||||
// the general include for eo
|
||||
#include <paradiseo/eo.h>
|
||||
#include <paradiseo/eo/es.h>
|
||||
|
||||
// REPRESENTATION
|
||||
//-----------------------------------------------------------------------------
|
||||
// define your individuals
|
||||
typedef eoReal<eoMinimizingFitness> Indi;
|
||||
|
||||
// Use functions from namespace std
|
||||
using namespace std;
|
||||
|
||||
// EVALFUNC
|
||||
//-----------------------------------------------------------------------------
|
||||
// a simple fitness function that computes the euclidian norm of a real vector
|
||||
// Now in a separate file, and declared as binary_value(const vector<bool> &)
|
||||
|
||||
#include "real_value.h"
|
||||
|
||||
// GENERAL
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
void main_function(int argc, char **argv)
|
||||
{
|
||||
// PARAMETRES
|
||||
//-----------------------------------------------------------------------------
|
||||
// instead of having all values of useful parameters as constants, read them:
|
||||
// either on the command line (--option=value or -o=value)
|
||||
// or in a parameter file (same syntax, order independent,
|
||||
// # = usual comment character
|
||||
// or in the environment (TODO)
|
||||
|
||||
// First define a parser from the command-line arguments
|
||||
eoParser parser(argc, argv);
|
||||
|
||||
// For each parameter, you can in on single line
|
||||
// define the parameter, read it through the parser, and assign it
|
||||
|
||||
unsigned seed = parser.createParam(unsigned(time(0)), "seed", "Random number seed", 'S').value(); // will be in default section General
|
||||
|
||||
// description of genotype
|
||||
unsigned vecSize = parser.createParam(unsigned(8), "vecSize", "Genotype size",'V', "Representation" ).value();
|
||||
|
||||
// parameters for evolution engine
|
||||
unsigned popSize = parser.createParam(unsigned(10), "popSize", "Population size",'P', "Evolution engine" ).value();
|
||||
|
||||
unsigned tSize = parser.createParam(unsigned(2), "tSize", "Tournament size",'T', "Evolution Engine" ).value();
|
||||
|
||||
// init and stop
|
||||
string loadName = parser.createParam(string(""), "Load","A save file to restart from",'L', "Persistence" ).value();
|
||||
|
||||
unsigned maxGen = parser.createParam(unsigned(100), "maxGen", "Maximum number of generations",'G', "Stopping criterion" ).value();
|
||||
|
||||
unsigned minGen = parser.createParam(unsigned(100), "minGen", "Minimum number of generations",'g', "Stopping criterion" ).value();
|
||||
|
||||
unsigned steadyGen = parser.createParam(unsigned(100), "steadyGen", "Number of generations with no improvement",'s', "Stopping criterion" ).value();
|
||||
|
||||
// operators probabilities at the algorithm level
|
||||
double pCross = parser.createParam(double(0.6), "pCross", "Probability of Crossover", 'C', "Genetic Operators" ).value();
|
||||
|
||||
double pMut = parser.createParam(double(0.1), "pMut", "Probability of Mutation", 'M', "Genetic Operators" ).value();
|
||||
|
||||
// relative rates for crossovers
|
||||
double hypercubeRate = parser.createParam(double(1), "hypercubeRate", "Relative rate for hypercube crossover", '\0', "Genetic Operators" ).value();
|
||||
|
||||
double segmentRate = parser.createParam(double(1), "segmentRate", "Relative rate for segment crossover", '\0', "Genetic Operators" ).value();
|
||||
|
||||
// internal parameters for the mutations
|
||||
double EPSILON = parser.createParam(double(0.01), "EPSILON", "Width for uniform mutation", '\0', "Genetic Operators" ).value();
|
||||
|
||||
double SIGMA = parser.createParam(double(0.3), "SIGMA", "Sigma for normal mutation", '\0', "Genetic Operators" ).value();
|
||||
|
||||
// relative rates for mutations
|
||||
double uniformMutRate = parser.createParam(double(1), "uniformMutRate", "Relative rate for uniform mutation", '\0', "Genetic Operators" ).value();
|
||||
|
||||
double detMutRate = parser.createParam(double(1), "detMutRate", "Relative rate for det-uniform mutation", '\0', "Genetic Operators" ).value();
|
||||
|
||||
double normalMutRate = parser.createParam(double(1), "normalMutRate", "Relative rate for normal mutation", '\0', "Genetic Operators" ).value();
|
||||
|
||||
// the name of the "status" file where all actual parameter values will be saved
|
||||
string str_status = parser.ProgramName() + ".status"; // default value
|
||||
string statusName = parser.createParam(str_status, "status","Status file",'S', "Persistence" ).value();
|
||||
|
||||
// do the following AFTER ALL PARAMETERS HAVE BEEN PROCESSED
|
||||
// i.e. in case you need parameters somewhere else, postpone these
|
||||
if (parser.userNeedsHelp())
|
||||
{
|
||||
parser.printHelp(cout);
|
||||
exit(1);
|
||||
}
|
||||
if (statusName != "")
|
||||
{
|
||||
ofstream os(statusName.c_str());
|
||||
os << parser; // and you can use that file as parameter file
|
||||
}
|
||||
|
||||
// EVAL
|
||||
/////////////////////////////
|
||||
// Fitness function
|
||||
////////////////////////////
|
||||
// Evaluation: from a plain C++ fn to an EvalFunc Object
|
||||
// you need to give the full description of the function
|
||||
eoEvalFuncPtr<Indi, double, const vector<double>& > plainEval( real_value );
|
||||
// ... to an object that counts the nb of actual evaluations
|
||||
eoEvalFuncCounter<Indi> eval(plainEval);
|
||||
|
||||
// INIT
|
||||
////////////////////////////////
|
||||
// Initilisation of population
|
||||
////////////////////////////////
|
||||
// Either load or initialize
|
||||
// create an empty pop
|
||||
eoPop<Indi> pop;
|
||||
// create a state for reading
|
||||
eoState inState; // a state for loading - WITHOUT the parser
|
||||
// register the rng and the pop in the state, so they can be loaded,
|
||||
// and the present run will be the exact conitnuation of the saved run
|
||||
// eventually with different parameters
|
||||
inState.registerObject(rng);
|
||||
inState.registerObject(pop);
|
||||
|
||||
if (loadName != "")
|
||||
{
|
||||
inState.load(loadName); // load the pop and the rng
|
||||
// the fitness is read in the file:
|
||||
// do only evaluate the pop if the fitness has changed
|
||||
}
|
||||
else
|
||||
{
|
||||
rng.reseed(seed);
|
||||
// a Indi random initializer
|
||||
// based on boolean_generator class (see utils/rnd_generator.h)
|
||||
eoUniformGenerator<double> uGen(-1.0, 1.0);
|
||||
eoInitFixedLength<Indi> random(vecSize, uGen);
|
||||
|
||||
// Init pop from the randomizer: need to use the append function
|
||||
pop.append(popSize, random);
|
||||
// and evaluate pop (STL syntax)
|
||||
apply<Indi>(eval, pop);
|
||||
} // end of initializatio of the population
|
||||
|
||||
// OUTPUT
|
||||
// sort pop before printing it!
|
||||
pop.sort();
|
||||
// Print (sorted) intial population (raw printout)
|
||||
cout << "Initial Population" << endl;
|
||||
cout << pop;
|
||||
|
||||
// ENGINE
|
||||
/////////////////////////////////////
|
||||
// selection and replacement
|
||||
////////////////////////////////////
|
||||
// SELECT
|
||||
// The robust tournament selection
|
||||
eoDetTournamentSelect<Indi> selectOne(tSize);
|
||||
// is now encapsulated in a eoSelectPerc (entage)
|
||||
eoSelectPerc<Indi> select(selectOne);// by default rate==1
|
||||
|
||||
// REPLACE
|
||||
// And we now have the full slection/replacement - though with
|
||||
// no replacement (== generational replacement) at the moment :-)
|
||||
eoGenerationalReplacement<Indi> replace;
|
||||
|
||||
// OPERATORS
|
||||
//////////////////////////////////////
|
||||
// The variation operators
|
||||
//////////////////////////////////////
|
||||
// CROSSOVER
|
||||
// uniform chooce on segment made by the parents
|
||||
eoSegmentCrossover<Indi> xoverS;
|
||||
// uniform choice in hypercube built by the parents
|
||||
eoHypercubeCrossover<Indi> xoverA;
|
||||
// Combine them with relative weights
|
||||
eoPropCombinedQuadOp<Indi> xover(xoverS, segmentRate);
|
||||
xover.add(xoverA, hypercubeRate);
|
||||
|
||||
// MUTATION
|
||||
// offspring(i) uniformly chosen in [parent(i)-epsilon, parent(i)+epsilon]
|
||||
eoUniformMutation<Indi> mutationU(EPSILON);
|
||||
// k (=1) coordinates of parents are uniformly modified
|
||||
eoDetUniformMutation<Indi> mutationD(EPSILON);
|
||||
// all coordinates of parents are normally modified (stDev SIGMA)
|
||||
eoNormalMutation<Indi> mutationN(SIGMA);
|
||||
// Combine them with relative weights
|
||||
eoPropCombinedMonOp<Indi> mutation(mutationU, uniformMutRate);
|
||||
mutation.add(mutationD, detMutRate);
|
||||
mutation.add(mutationN, normalMutRate, true);
|
||||
|
||||
// The operators are encapsulated into an eoTRansform object
|
||||
eoSGATransform<Indi> transform(xover, pCross, mutation, pMut);
|
||||
|
||||
// STOP
|
||||
//////////////////////////////////////
|
||||
// termination condition see FirstBitEA.cpp
|
||||
/////////////////////////////////////
|
||||
eoGenContinue<Indi> genCont(maxGen);
|
||||
eoSteadyFitContinue<Indi> steadyCont(minGen, steadyGen);
|
||||
eoFitContinue<Indi> fitCont(0);
|
||||
eoCombinedContinue<Indi> continuator(genCont);
|
||||
continuator.add(steadyCont);
|
||||
continuator.add(fitCont);
|
||||
|
||||
|
||||
// CHECKPOINT
|
||||
// but now you want to make many different things every generation
|
||||
// (e.g. statistics, plots, ...).
|
||||
// the class eoCheckPoint is dedicated to just that:
|
||||
|
||||
// Declare a checkpoint (from a continuator: an eoCheckPoint
|
||||
// IS AN eoContinue and will be called in the loop of all algorithms)
|
||||
eoCheckPoint<Indi> checkpoint(continuator);
|
||||
|
||||
// Create a counter parameter
|
||||
eoValueParam<unsigned> generationCounter(0, "Gen.");
|
||||
|
||||
// Create an incrementor (sub-class of eoUpdater). Note that the
|
||||
// parameter's value is passed by reference,
|
||||
// so every time the incrementer is updated (every generation),
|
||||
// the data in generationCounter will change.
|
||||
eoIncrementor<unsigned> increment(generationCounter.value());
|
||||
|
||||
// Add it to the checkpoint,
|
||||
// so the counter is updated (here, incremented) every generation
|
||||
checkpoint.add(increment);
|
||||
|
||||
// now some statistics on the population:
|
||||
// Best fitness in population
|
||||
eoBestFitnessStat<Indi> bestStat;
|
||||
// Second moment stats: average and stdev
|
||||
eoSecondMomentStats<Indi> SecondStat;
|
||||
|
||||
// Add them to the checkpoint to get them called at the appropriate time
|
||||
checkpoint.add(bestStat);
|
||||
checkpoint.add(SecondStat);
|
||||
|
||||
// The Stdout monitor will print parameters to the screen ...
|
||||
eoStdoutMonitor monitor(false);
|
||||
|
||||
// when called by the checkpoint (i.e. at every generation)
|
||||
checkpoint.add(monitor);
|
||||
|
||||
// the monitor will output a series of parameters: add them
|
||||
monitor.add(generationCounter);
|
||||
monitor.add(eval); // because now eval is an eoEvalFuncCounter!
|
||||
monitor.add(bestStat);
|
||||
monitor.add(SecondStat);
|
||||
|
||||
// A file monitor: will print parameters to ... a File, yes, you got it!
|
||||
eoFileMonitor fileMonitor("stats.xg", " ");
|
||||
|
||||
// the checkpoint mechanism can handle multiple monitors
|
||||
checkpoint.add(fileMonitor);
|
||||
|
||||
// the fileMonitor can monitor parameters, too, but you must tell it!
|
||||
fileMonitor.add(generationCounter);
|
||||
fileMonitor.add(bestStat);
|
||||
fileMonitor.add(SecondStat);
|
||||
|
||||
// Last type of item the eoCheckpoint can handle: state savers:
|
||||
eoState outState;
|
||||
// Register the algorithm into the state (so it has something to save!!)
|
||||
outState.registerObject(parser);
|
||||
outState.registerObject(pop);
|
||||
outState.registerObject(rng);
|
||||
|
||||
// and feed the state to state savers
|
||||
// save state every 100th generation
|
||||
eoCountedStateSaver stateSaver1(20, outState, "generation");
|
||||
// save state every 1 seconds
|
||||
eoTimedStateSaver stateSaver2(1, outState, "time");
|
||||
|
||||
// Don't forget to add the two savers to the checkpoint
|
||||
checkpoint.add(stateSaver1);
|
||||
checkpoint.add(stateSaver2);
|
||||
// and that's it for the (control and) output
|
||||
|
||||
// GENERATION
|
||||
/////////////////////////////////////////
|
||||
// the algorithm
|
||||
////////////////////////////////////////
|
||||
|
||||
// Easy EA requires
|
||||
// stopping criterion, eval, selection, transformation, replacement
|
||||
eoEasyEA<Indi> gga(checkpoint, eval, select, transform, replace);
|
||||
|
||||
// Apply algo to pop - that's it!
|
||||
gga(pop);
|
||||
|
||||
// OUTPUT
|
||||
// Print (sorted) intial population
|
||||
pop.sort();
|
||||
cout << "FINAL Population\n" << pop << endl;
|
||||
// GENERAL
|
||||
}
|
||||
|
||||
// A main that catches the exceptions
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
try
|
||||
{
|
||||
main_function(argc, argv);
|
||||
}
|
||||
catch(exception& e)
|
||||
{
|
||||
cout << "Exception: " << e.what() << '\n';
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
16
tutorial/eo/Lesson3/binary_value.h
Executable file
16
tutorial/eo/Lesson3/binary_value.h
Executable file
|
|
@ -0,0 +1,16 @@
|
|||
#include <paradiseo/eo.h>
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
/** Just a simple function that takes binary value of a chromosome and sets
|
||||
the fitnes.
|
||||
@param _chrom A binary chromosome
|
||||
*/
|
||||
// INIT
|
||||
double binary_value(const std::vector<bool>& _chrom)
|
||||
{
|
||||
double sum = 0;
|
||||
for (unsigned i = 0; i < _chrom.size(); i++)
|
||||
sum += _chrom[i];
|
||||
return sum;
|
||||
}
|
||||
404
tutorial/eo/Lesson3/exercise3.1.cpp
Executable file
404
tutorial/eo/Lesson3/exercise3.1.cpp
Executable file
|
|
@ -0,0 +1,404 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// SecondBitGA.cpp
|
||||
//-----------------------------------------------------------------------------
|
||||
//*
|
||||
// Same code than FirstBitEA as far as Evolutionary Computation is concerned
|
||||
// but now you learn to enter the parameters in a more flexible way
|
||||
// and to twidle the output to your preferences!
|
||||
//-----------------------------------------------------------------------------
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
// standard includes
|
||||
#include <fstream>
|
||||
#include <iostream> // cout
|
||||
#include <stdexcept> // runtime_error
|
||||
|
||||
// the general include for eo
|
||||
#include <paradiseo/eo.h>
|
||||
|
||||
// EVAL
|
||||
#include "binary_value.h"
|
||||
|
||||
// REPRESENTATION
|
||||
//-----------------------------------------------------------------------------
|
||||
// Include the corresponding file
|
||||
#include <paradiseo/eo/ga.h> // bitstring representation & operators
|
||||
// define your genotype and fitness types
|
||||
typedef eoBit<eoMinimizingFitness> Indi;
|
||||
|
||||
using namespace std;
|
||||
|
||||
// the main_function: nothing changed(!), except variable initialization
|
||||
void main_function(int argc, char **argv)
|
||||
{
|
||||
// PARAMETRES
|
||||
//-----------------------------------------------------------------------------
|
||||
// instead of having all values of useful parameters as constants, read them:
|
||||
// either on the command line (--option=value or -o=value)
|
||||
// or in a parameter file (same syntax, order independent,
|
||||
// # = usual comment character
|
||||
// or in the environment (TODO)
|
||||
|
||||
// First define a parser from the command-line arguments
|
||||
eoParser parser(argc, argv);
|
||||
|
||||
// For each parameter, define Parameter, read it through the parser,
|
||||
// and assign the value to the variable
|
||||
|
||||
eoValueParam<uint32_t> seedParam(time(0), "seed", "Random number seed", 'S');
|
||||
parser.processParam( seedParam );
|
||||
unsigned seed = seedParam.value();
|
||||
|
||||
// description of genotype
|
||||
eoValueParam<unsigned int> vecSizeParam(100, "vecSize", "Genotype size",'V');
|
||||
parser.processParam( vecSizeParam, "Representation" );
|
||||
unsigned vecSize = vecSizeParam.value();
|
||||
|
||||
// parameters for evolution engine
|
||||
eoValueParam<unsigned int> popSizeParam(100, "popSize", "Population size",'P');
|
||||
parser.processParam( popSizeParam, "Evolution engine" );
|
||||
unsigned popSize = popSizeParam.value();
|
||||
|
||||
eoValueParam<unsigned int> tSizeParam(10, "tSize", "Tournament size",'T');
|
||||
parser.processParam( tSizeParam, "Evolution Engine" );
|
||||
unsigned tSize = tSizeParam.value();
|
||||
|
||||
// init and stop
|
||||
eoValueParam<string> loadNameParam("", "Load","A save file to restart from",'L');
|
||||
parser.processParam( loadNameParam, "Persistence" );
|
||||
string loadName = loadNameParam.value();
|
||||
|
||||
eoValueParam<unsigned int> maxGenParam(500, "maxGen", "Maximum number of generations",'G');
|
||||
parser.processParam( maxGenParam, "Stopping criterion" );
|
||||
unsigned maxGen = maxGenParam.value();
|
||||
|
||||
eoValueParam<unsigned int> minGenParam(500, "minGen", "Minimum number of generations",'g');
|
||||
parser.processParam( minGenParam, "Stopping criterion" );
|
||||
unsigned minGen = minGenParam.value();
|
||||
|
||||
eoValueParam<unsigned int> steadyGenParam(100, "steadyGen", "Number of generations with no improvement",'s');
|
||||
parser.processParam( steadyGenParam, "Stopping criterion" );
|
||||
unsigned steadyGen = steadyGenParam.value();
|
||||
|
||||
// operators probabilities at the algorithm level
|
||||
eoValueParam<double> pCrossParam(0.6, "pCross", "Probability of Crossover", 'C');
|
||||
parser.processParam( pCrossParam, "Genetic Operators" );
|
||||
double pCross = pCrossParam.value();
|
||||
|
||||
eoValueParam<double> pMutParam(0.1, "pMut", "Probability of Mutation", 'M');
|
||||
parser.processParam( pMutParam, "Genetic Operators" );
|
||||
double pMut = pMutParam.value();
|
||||
|
||||
// relative rates for crossovers
|
||||
eoValueParam<double> onePointRateParam(1, "onePointRate", "Relative rate for one point crossover", '1');
|
||||
parser.processParam( onePointRateParam, "Genetic Operators" );
|
||||
double onePointRate = onePointRateParam.value();
|
||||
|
||||
eoValueParam<double> twoPointsRateParam(1, "twoPointRate", "Relative rate for two point crossover", '2');
|
||||
parser.processParam( twoPointsRateParam, "Genetic Operators" );
|
||||
double twoPointsRate = twoPointsRateParam.value();
|
||||
|
||||
eoValueParam<double> uRateParam(2, "uRate", "Relative rate for uniform crossover", 'U');
|
||||
parser.processParam( uRateParam, "Genetic Operators" );
|
||||
double URate = uRateParam.value();
|
||||
|
||||
// relative rates and private parameters for mutations;
|
||||
eoValueParam<double> pMutPerBitParam(0.01, "pMutPerBit", "Probability of flipping 1 bit in bit-flip mutation", 'b');
|
||||
parser.processParam( pMutPerBitParam, "Genetic Operators" );
|
||||
double pMutPerBit = pMutPerBitParam.value();
|
||||
|
||||
eoValueParam<double> bitFlipRateParam(0.01, "bitFlipRate", "Relative rate for bit-flip mutation", 'B');
|
||||
parser.processParam( bitFlipRateParam, "Genetic Operators" );
|
||||
double bitFlipRate = bitFlipRateParam.value();
|
||||
|
||||
eoValueParam<double> oneBitRateParam(0.01, "oneBitRate", "Relative rate for deterministic bit-flip mutation", 'D');
|
||||
parser.processParam( oneBitRateParam, "Genetic Operators" );
|
||||
double oneBitRate = oneBitRateParam.value();
|
||||
|
||||
// the name of the "status" file where all actual parameter values will be saved
|
||||
string str_status = parser.ProgramName() + ".status"; // default value
|
||||
eoValueParam<string> statusParam(str_status.c_str(), "status","Status file",'S');
|
||||
parser.processParam( statusParam, "Persistence" );
|
||||
|
||||
// do the following AFTER ALL PARAMETERS HAVE BEEN PROCESSED
|
||||
// i.e. in case you need parameters somewhere else, postpone these
|
||||
if (parser.userNeedsHelp())
|
||||
{
|
||||
parser.printHelp(cout);
|
||||
exit(1);
|
||||
}
|
||||
if (statusParam.value() != "")
|
||||
{
|
||||
ofstream os(statusParam.value().c_str());
|
||||
os << parser; // and you can use that file as parameter file
|
||||
}
|
||||
|
||||
// EVAL
|
||||
/////////////////////////////
|
||||
// Fitness function
|
||||
////////////////////////////
|
||||
// Evaluation: from a plain C++ fn to an EvalFunc Object ...
|
||||
eoEvalFuncPtr<Indi, double, const vector<bool>& > plainEval( binary_value );
|
||||
// ... to an object that counts the nb of actual evaluations
|
||||
eoEvalFuncCounter<Indi> eval(plainEval);
|
||||
|
||||
// INIT
|
||||
////////////////////////////////
|
||||
// Initilisation of population
|
||||
////////////////////////////////
|
||||
// Either load or initialize
|
||||
// create an empty pop
|
||||
eoPop<Indi> pop;
|
||||
// create a state for reading
|
||||
eoState inState; // a state for loading - WITHOUT the parser
|
||||
// register the rng and the pop in the state, so they can be loaded,
|
||||
// and the present run will be the exact conitnuation of the saved run
|
||||
// eventually with different parameters
|
||||
inState.registerObject(rng);
|
||||
inState.registerObject(pop);
|
||||
|
||||
if (loadName != "")
|
||||
{
|
||||
inState.load(loadName); // load the pop and the rng
|
||||
// the fitness is read in the file:
|
||||
// do only evaluate the pop if the fitness has changed
|
||||
}
|
||||
else
|
||||
{
|
||||
rng.reseed(seed);
|
||||
// a Indi random initializer
|
||||
// based on boolean_generator class (see utils/rnd_generator.h)
|
||||
eoUniformGenerator<bool> uGen;
|
||||
eoInitFixedLength<Indi> random(vecSize, uGen);
|
||||
|
||||
// Init pop from the randomizer: need to use the append function
|
||||
pop.append(popSize, random);
|
||||
// and evaluate pop (STL syntax)
|
||||
apply<Indi>(eval, pop);
|
||||
} // end of initializatio of the population
|
||||
|
||||
// OUTPUT
|
||||
// sort pop for pretty printout
|
||||
// pop.sort();
|
||||
// Print (sorted) intial population (raw printout)
|
||||
cout << "Initial Population" << endl << pop ;
|
||||
cout << "and best is " << pop.best_element() << "\n\n";
|
||||
cout << "and worse is " << pop.worse_element() << "\n\n";
|
||||
// ENGINE
|
||||
/////////////////////////////////////
|
||||
// selection and replacement
|
||||
////////////////////////////////////
|
||||
// SELECT
|
||||
// The robust tournament selection
|
||||
eoDetTournamentSelect<Indi> selectOne(tSize); // tSize in [2,POPSIZE]
|
||||
// is now encapsulated in a eoSelectPerc (entage)
|
||||
eoSelectPerc<Indi> select(selectOne);// by default rate==1
|
||||
|
||||
// REPLACE
|
||||
// And we now have the full slection/replacement - though with
|
||||
// generational replacement at the moment :-)
|
||||
eoGenerationalReplacement<Indi> replace;
|
||||
// want to add (weak) elitism? easy!
|
||||
// rename the eoGenerationalReplacement replace_main,
|
||||
// then encapsulate it in the elitist replacement
|
||||
// eoWeakElitistReplacement<Indi> replace(replace_main);
|
||||
|
||||
// OPERATORS
|
||||
//////////////////////////////////////
|
||||
// The variation operators
|
||||
//////////////////////////////////////
|
||||
// CROSSOVER
|
||||
// 1-point crossover for bitstring
|
||||
eo1PtBitXover<Indi> xover1;
|
||||
// uniform crossover for bitstring
|
||||
eoUBitXover<Indi> xoverU;
|
||||
// 2-pots xover
|
||||
eoNPtsBitXover<Indi> xover2(2);
|
||||
// Combine them with relative rates
|
||||
eoPropCombinedQuadOp<Indi> xover(xover1, onePointRate);
|
||||
xover.add(xoverU, URate);
|
||||
xover.add(xover2, twoPointsRate);
|
||||
|
||||
// MUTATION
|
||||
// standard bit-flip mutation for bitstring
|
||||
eoBitMutation<Indi> mutationBitFlip(pMutPerBit);
|
||||
// mutate exactly 1 bit per individual
|
||||
eoDetBitFlip<Indi> mutationOneBit;
|
||||
// Combine them with relative rates
|
||||
eoPropCombinedMonOp<Indi> mutation(mutationBitFlip, bitFlipRate);
|
||||
mutation.add(mutationOneBit, oneBitRate, true);
|
||||
|
||||
// The operators are encapsulated into an eoTRansform object
|
||||
eoSGATransform<Indi> transform(xover, pCross, mutation, pMut);
|
||||
|
||||
// STOP
|
||||
//////////////////////////////////////
|
||||
// termination condition see FirstBitEA.cpp
|
||||
/////////////////////////////////////
|
||||
eoGenContinue<Indi> genCont(maxGen);
|
||||
eoSteadyFitContinue<Indi> steadyCont(minGen, steadyGen);
|
||||
// eoFitContinue<Indi> fitCont(vecSize); // remove if minimizing :-)
|
||||
eoCombinedContinue<Indi> continuator(genCont);
|
||||
continuator.add(steadyCont);
|
||||
// continuator.add(fitCont);
|
||||
// Ctrl C signal handling: don't know if that works in MSC ...
|
||||
#ifndef _MSC_VER
|
||||
eoCtrlCContinue<Indi> ctrlC;
|
||||
continuator.add(ctrlC);
|
||||
#endif
|
||||
|
||||
// CHECKPOINT
|
||||
// but now you want to make many different things every generation
|
||||
// (e.g. statistics, plots, ...).
|
||||
// the class eoCheckPoint is dedicated to just that:
|
||||
|
||||
// Declare a checkpoint (from a continuator: an eoCheckPoint
|
||||
// IS AN eoContinue and will be called in the loop of all algorithms)
|
||||
eoCheckPoint<Indi> checkpoint(continuator);
|
||||
|
||||
// Create a counter parameter
|
||||
eoValueParam<unsigned> generationCounter(0, "Gen.");
|
||||
|
||||
// Create an incrementor (sub-class of eoUpdater). Note that the
|
||||
// parameter's value is passed by reference,
|
||||
// so every time the incrementer is updated (every generation),
|
||||
// the data in generationCounter will change.
|
||||
eoIncrementor<unsigned> increment(generationCounter.value());
|
||||
|
||||
// Add it to the checkpoint,
|
||||
// so the counter is updated (here, incremented) every generation
|
||||
checkpoint.add(increment);
|
||||
|
||||
// now some statistics on the population:
|
||||
// Best fitness in population
|
||||
eoBestFitnessStat<Indi> bestStat;
|
||||
eoAverageStat<Indi> averageStat;
|
||||
// Second moment stats: average and stdev
|
||||
eoSecondMomentStats<Indi> SecondStat;
|
||||
// the Fitness Distance Correlation
|
||||
// need first an object to compute the distances
|
||||
eoQuadDistance<Indi> dist; // Hamming distance
|
||||
eoFDCStat<Indi> fdcStat(dist);
|
||||
|
||||
// Add them to the checkpoint to get them called at the appropriate time
|
||||
checkpoint.add(bestStat);
|
||||
checkpoint.add(averageStat);
|
||||
checkpoint.add(SecondStat);
|
||||
checkpoint.add(fdcStat);
|
||||
|
||||
// The Stdout monitor will print parameters to the screen ...
|
||||
eoStdoutMonitor monitor(false);
|
||||
|
||||
// when called by the checkpoint (i.e. at every generation)
|
||||
checkpoint.add(monitor);
|
||||
|
||||
// the monitor will output a series of parameters: add them
|
||||
monitor.add(generationCounter);
|
||||
monitor.add(eval); // because now eval is an eoEvalFuncCounter!
|
||||
monitor.add(bestStat);
|
||||
monitor.add(SecondStat);
|
||||
monitor.add(fdcStat);
|
||||
|
||||
// test de eoPopStat and/or eoSortedPopStat.
|
||||
// Dumps the whole pop every 10 gen.
|
||||
// eoSortedPopStat<Indi> popStat(10, "Dump of whole population");
|
||||
// eoPopStat<Indi> popStat(10, "Dump of whole population");
|
||||
// checkpoint.add(popStat);
|
||||
// monitor.add(popStat);
|
||||
|
||||
// A file monitor: will print parameters to ... a File, yes, you got it!
|
||||
eoFileMonitor fileMonitor("stats.xg", " ");
|
||||
|
||||
// the checkpoint mechanism can handle monitors
|
||||
checkpoint.add(fileMonitor);
|
||||
|
||||
// the fileMonitor can monitor parameters, too, but you must tell it!
|
||||
fileMonitor.add(generationCounter);
|
||||
fileMonitor.add(bestStat);
|
||||
fileMonitor.add(SecondStat);
|
||||
|
||||
#ifndef _MSC_VER
|
||||
// and an eoGnuplot1DMonitor will 1-print to a file, and 2- plot on screen
|
||||
eoGnuplot1DMonitor gnuMonitor("best_average.xg",minimizing_fitness<Indi>());
|
||||
// the checkpoint mechanism can handle multiple monitors
|
||||
checkpoint.add(gnuMonitor);
|
||||
// the gnuMonitor can monitor parameters, too, but you must tell it!
|
||||
gnuMonitor.add(eval);
|
||||
gnuMonitor.add(bestStat);
|
||||
gnuMonitor.add(averageStat);
|
||||
|
||||
// send a scaling command to gnuplot
|
||||
gnuMonitor.gnuplotCommand("set yrange [0:500]");
|
||||
|
||||
// a specific plot monitor for FDC
|
||||
// first into a file (it adds everything ti itself
|
||||
eoFDCFileSnapshot<Indi> fdcFileSnapshot(fdcStat);
|
||||
// then to a Gnuplot monitor
|
||||
eoGnuplot1DSnapshot fdcGnuplot(fdcFileSnapshot);
|
||||
// and of coruse add them to the checkPoint
|
||||
checkpoint.add(fdcFileSnapshot);
|
||||
checkpoint.add(fdcGnuplot);
|
||||
|
||||
// want to see how the fitness is spread?
|
||||
eoScalarFitnessStat<Indi> fitStat;
|
||||
checkpoint.add(fitStat);
|
||||
// a gnuplot-based monitor for snapshots: needs a dir name
|
||||
// where to store the files
|
||||
eoGnuplot1DSnapshot fitSnapshot("Fitnesses");
|
||||
// add any stat that is a vector<double> to it
|
||||
fitSnapshot.add(fitStat);
|
||||
// and of course add it to the checkpoint
|
||||
checkpoint.add(fitSnapshot);
|
||||
#endif
|
||||
// Last type of item the eoCheckpoint can handle: state savers:
|
||||
eoState outState;
|
||||
// Register the algorithm into the state (so it has something to save!!)
|
||||
outState.registerObject(rng);
|
||||
outState.registerObject(pop);
|
||||
|
||||
// and feed the state to state savers
|
||||
// save state every 100th generation
|
||||
eoCountedStateSaver stateSaver1(100, outState, "generation");
|
||||
// save state every 1 seconds
|
||||
eoTimedStateSaver stateSaver2(1, outState, "time");
|
||||
|
||||
// Don't forget to add the two savers to the checkpoint
|
||||
checkpoint.add(stateSaver1);
|
||||
checkpoint.add(stateSaver2);
|
||||
// and that's it for the (control and) output
|
||||
|
||||
// GENERATION
|
||||
/////////////////////////////////////////
|
||||
// the algorithm
|
||||
////////////////////////////////////////
|
||||
|
||||
// Easy EA requires
|
||||
// selection, transformation, eval, replacement, and stopping criterion
|
||||
eoEasyEA<Indi> gga(checkpoint, eval, select, transform, replace);
|
||||
|
||||
// Apply algo to pop - that's it!
|
||||
gga(pop);
|
||||
|
||||
// OUTPUT
|
||||
// Print (sorted) intial population
|
||||
pop.sort();
|
||||
cout << "FINAL Population\n" << pop << endl;
|
||||
// GENERAL
|
||||
}
|
||||
|
||||
// A main that catches the exceptions
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
try
|
||||
{
|
||||
main_function(argc, argv);
|
||||
}
|
||||
catch(exception& e)
|
||||
{
|
||||
cout << "Exception: " << e.what() << '\n';
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue