├── .gitignore ├── LICENSE ├── README.md ├── activation.go ├── activation_func.go ├── banner.png ├── config.go ├── config_pole_balancing.json ├── config_template.json ├── config_xor.json ├── doc.go ├── evaluation_func.go ├── genome.go ├── genome_new.go ├── genome_test.go ├── neat.go ├── neat_test.go ├── network.go ├── neural_network.go ├── neural_network_test.go ├── species.go └── statistics.go /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | *.swp 3 | genome_1_* 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | {one line to give the program's name and a brief idea of what it does.} 635 | Copyright (C) {year} {name of author} 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | {project} Copyright (C) {year} {fullname} 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![alt text](https://github.com/jinyeom/neat/blob/master/banner.png "neat") 2 | [![GoDoc](https://godoc.org/github.com/jinyeom/neat?status.svg)](https://godoc.org/github.com/jinyeom/neat) 3 | [![Go Report Card](https://goreportcard.com/badge/github.com/jinyeom/neat)](https://goreportcard.com/report/github.com/jinyeom/neat) 4 | [![cover.run go](https://cover.run/go/github.com/jinyeom/neat.svg)](https://cover.run/go/github.com/jinyeom/neat) 5 | 6 | CURRENTLY NOT WORKING! There will be a further notice when it's updated. 7 | 8 | NEAT (NeuroEvolution of Augmenting Topologies) is a neuroevolution algorithm by 9 | Dr. Kenneth O. Stanley which evolves not only neural networks' weights but also their 10 | topologies. This method starts the evolution process with genomes with minimal structure, 11 | then complexifies the structure of each genome as it progresses. You can read the original 12 | paper from [here](http://nn.cs.utexas.edu/downloads/papers/stanley.ec02.pdf). 13 | 14 | ## Installation 15 | To install `neat` run the following: 16 | 17 | ```bash 18 | $ go get -u github.com/jinyeom/neat 19 | ``` 20 | 21 | ## Usage 22 | 23 | This NEAT package is as simple as plug and play. All you have to do is to create 24 | a new instance of NEAT, given the configuration from a JSON file, for which the 25 | template is provided below, and an evaluation method of a neural network, and 26 | run. 27 | 28 | ```json 29 | { 30 | "experimentName": "XOR Test", 31 | "verbose": true, 32 | "numInputs": 3, 33 | "numOutputs": 1, 34 | "fullyConnected": false, 35 | "numGenerations": 50, 36 | "populationSize": 100, 37 | "initFitness": 9999.0, 38 | "minimizeFitness": true, 39 | "survivalRate": 0.5, 40 | "stagnationLimit": 5, 41 | "ratePerturb": 0.2, 42 | "rateAddNode": 0.2, 43 | "rateAddConn": 0.2, 44 | "rateMutateChild": 0.5, 45 | "distanceThreshold": 20.0, 46 | "coeffUnmatching": 1.0, 47 | "coeffMatching": 1.0, 48 | "cppnActivations": [], 49 | } 50 | ``` 51 | 52 | Now that you have the configuration JSON file is ready as `config.json`, we can 53 | start experiment with NEAT. Below is an example XOR experiment. 54 | 55 | ```go 56 | package main 57 | 58 | import ( 59 | "log" 60 | "math" 61 | 62 | // Import NEAT package after installing the package through 63 | // the instruction provided above. 64 | "github.com/jinyeom/neat" 65 | ) 66 | 67 | func main() { 68 | 69 | // First, create a new instance of Config from the JSON file created above. 70 | // If there's a file import error, the program will crash. 71 | config, err := neat.NewConfigJSON("config.json") 72 | if err != nil{ 73 | log.Fatal(err) 74 | } 75 | 76 | // Then, we can define the evaluation function, which is a type of function 77 | // which takes a neural network, evaluates its performance, and returns some 78 | // score that indicates its performance. This score is essentially a genome's 79 | // fitness score. With the configuration and the evaluation function we 80 | // defined, we can create a new instance of NEAT and start the evolution 81 | // process. 82 | neat.New(config, neat.XORTest()).Run() 83 | } 84 | 85 | ``` 86 | 87 | ## License 88 | This package is under GNU General Public License. 89 | -------------------------------------------------------------------------------- /activation.go: -------------------------------------------------------------------------------- 1 | package neat 2 | 3 | const ( 4 | actMin = -100000.0 5 | actMax = 100000.0 6 | ) 7 | 8 | func RandActivationFunc() *ActivationFunc { 9 | funcs = []func() *ActviationFunc{Linear, Sigmoid, Tanh, Sine, Gaussian} 10 | return funcs[rand.Intn(len(funcs))]() 11 | } 12 | 13 | type ActivationFunc struct { 14 | name string 15 | f func(x float64) float64 16 | } 17 | 18 | func Linear() *ActivationFunc { 19 | return &ActivationFunc{ 20 | name: "linear", 21 | f: func(x float64) float64 { 22 | return x 23 | }, 24 | } 25 | } 26 | 27 | func Sigmoid() *ActivationFunc { 28 | return &ActivationFunc{ 29 | name: "sigmoid", 30 | f: func(x float64) float64 { 31 | return 1.0 / (1.0 + math.Exp(-x)) 32 | }, 33 | } 34 | } 35 | 36 | func Tanh() *ActivationFunc { 37 | return &ActivationFunc{ 38 | name: "tanh", 39 | f: func(x float64) float64 { 40 | return math.Tanh(x) 41 | }, 42 | } 43 | } 44 | 45 | func Sine() *ActivationFunc { 46 | return &ActivationFunc{ 47 | name: "sine", 48 | f: func(x float64) float64 { 49 | return math.Sin(x) 50 | }, 51 | } 52 | } 53 | 54 | func Gaussian() *ActivationFunc { 55 | return &ActivationFunc{ 56 | name: "gaussian", 57 | f: func(x float64) float64 { 58 | return math.Exp(-x * x) 59 | }, 60 | } 61 | } 62 | 63 | func (a *ActivationFunc) Name() float64 { 64 | return a.name 65 | } 66 | 67 | func (a *ActivationFunc) Activate(x float64) float64 { 68 | clipped = math.Min(math.Max(x, actMin), actMax) 69 | return a.f(x) 70 | } 71 | -------------------------------------------------------------------------------- /activation_func.go: -------------------------------------------------------------------------------- 1 | // activation_func.go implementation of activation functions used in a network. 2 | // 3 | // Copyright (C) 2017 Jin Yeom 4 | // 5 | // This program is free software: you can redistribute it and/or modify 6 | // it under the terms of the GNU General Public License as published by 7 | // the Free Software Foundation, either version 3 of the License, or 8 | // (at your option) any later version. 9 | // 10 | // This program is distributed in the hope that it will be useful, 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | // GNU General Public License for more details. 14 | // 15 | // You should have received a copy of the GNU General Public License 16 | // along with this program. If not, see . 17 | 18 | package neat 19 | 20 | import ( 21 | "math" 22 | ) 23 | 24 | var ( 25 | // ActivationSet is a set of functions that can be used as activation 26 | // functions by neurons. 27 | ActivationSet = map[string]*ActivationFunc{ 28 | "linear": Linear(), 29 | "sigmoid": Sigmoid(), 30 | "tanh": Tanh(), 31 | "sin": Sin(), 32 | "cos": Cos(), 33 | "relu": ReLU(), 34 | "log": Log(), 35 | "exp": Exp(), 36 | "abs": Abs(), 37 | "square": Square(), 38 | "cube": Cube(), 39 | "gaussian": Gaussian(0.0, 1.0), 40 | } 41 | ) 42 | 43 | // ActivationFunc is a wrapper type for activation functions. 44 | type ActivationFunc struct { 45 | Name string `json:"name"` // name of the function 46 | Fn func(x float64) float64 `json:"-"` // activation function 47 | } 48 | 49 | // Identity returns the identity function as an activation 50 | // function. This function is only used for sensor nodes. 51 | func Identity() *ActivationFunc { 52 | return &ActivationFunc{ 53 | Name: "Identity", 54 | Fn: func(x float64) float64 { 55 | return x 56 | }, 57 | } 58 | } 59 | 60 | // Sigmoid returns the sigmoid function as an activation function. 61 | func Sigmoid() *ActivationFunc { 62 | return &ActivationFunc{ 63 | Name: "Sigmoid", 64 | Fn: func(x float64) float64 { 65 | return 1.0 / (1.0 + math.Exp(-x)) 66 | }, 67 | } 68 | } 69 | 70 | // Tanh returns the hyperbolic tangent function as an activation function. 71 | func Tanh() *ActivationFunc { 72 | return &ActivationFunc{ 73 | Name: "Tanh", 74 | Fn: math.Tanh, 75 | } 76 | } 77 | 78 | // Sin returns the sin function as an activation function. 79 | func Sin() *ActivationFunc { 80 | return &ActivationFunc{ 81 | Name: "Sine", 82 | Fn: math.Sin, 83 | } 84 | } 85 | 86 | // Cos returns the cosine function as an activation function. 87 | func Cos() *ActivationFunc { 88 | return &ActivationFunc{ 89 | Name: "Cosine", 90 | Fn: math.Cos, 91 | } 92 | } 93 | 94 | // ReLU returns a rectifier linear unit as an activation function. 95 | func ReLU() *ActivationFunc { 96 | return &ActivationFunc{ 97 | Name: "ReLU", 98 | Fn: func(x float64) float64 { 99 | return math.Max(x, 0.0) 100 | }, 101 | } 102 | } 103 | 104 | // Log returns the log function as an activation function. 105 | func Log() *ActivationFunc { 106 | return &ActivationFunc{ 107 | Name: "Log", 108 | Fn: math.Log, 109 | } 110 | } 111 | 112 | // Exp returns the exponential function as an activation function. 113 | func Exp() *ActivationFunc { 114 | return &ActivationFunc{ 115 | Name: "Exp", 116 | Fn: math.Exp, 117 | } 118 | } 119 | 120 | // Abs returns the absolute value function as an activation function. 121 | func Abs() *ActivationFunc { 122 | return &ActivationFunc{ 123 | Name: "Abs", 124 | Fn: math.Abs, 125 | } 126 | } 127 | 128 | // Square returns the square function as an activation function. 129 | func Square() *ActivationFunc { 130 | return &ActivationFunc{ 131 | Name: "Square", 132 | Fn: func(x float64) float64 { 133 | return x * x 134 | }, 135 | } 136 | } 137 | 138 | // Cube returns the cube function as an activation function. 139 | func Cube() *ActivationFunc { 140 | return &ActivationFunc{ 141 | Name: "Cube", 142 | Fn: func(x float64) float64 { 143 | return x * x * x 144 | }, 145 | } 146 | } 147 | 148 | // Gaussian returns the Gaussian function as an activation function, given a 149 | // mean and a standard deviation. 150 | func Gaussian(mean, stdev float64) *ActivationFunc { 151 | return &ActivationFunc{ 152 | Name: "Gaussian", 153 | Fn: func(x float64) float64 { 154 | return 1.0 / (stdev * math.Sqrt(2*math.Pi)) * 155 | math.Exp(math.Pow((x-mean)/stdev, 2.0)/-2.0) 156 | }, 157 | } 158 | } 159 | -------------------------------------------------------------------------------- /banner.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jinyeom/neat/9ec678d1fbacb36176ccaff5ba4dfca4d1b4d442/banner.png -------------------------------------------------------------------------------- /config.go: -------------------------------------------------------------------------------- 1 | // config.go implementation of configuration settings for NEAT. 2 | // 3 | // Copyright (C) 2017 Jin Yeom 4 | // 5 | // This program is free software: you can redistribute it and/or modify 6 | // it under the terms of the GNU General Public License as published by 7 | // the Free Software Foundation, either version 3 of the License, or 8 | // (at your option) any later version. 9 | // 10 | // This program is distributed in the hope that it will be useful, 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | // GNU General Public License for more details. 14 | // 15 | // You should have received a copy of the GNU General Public License 16 | // along with this program. If not, see . 17 | 18 | package neat 19 | 20 | import ( 21 | "encoding/json" 22 | "fmt" 23 | "os" 24 | "text/tabwriter" 25 | ) 26 | 27 | // Config consists of all hyperparameter settings for NEAT. It can be imported 28 | // from a JSON file. 29 | type Config struct { 30 | // general settings 31 | ExperimentName string `json:"experimentName"` // name of the experiment 32 | Verbose bool `json:"verbose"` // verbose mode (terminal) 33 | 34 | // neural network settings 35 | NumInputs int `json:"numInputs"` // number of inputs 36 | NumOutputs int `json:"numOutputs"` // number of outputs 37 | FullyConnected bool `json:"fullyConnected"` // initially fully connected 38 | 39 | // evolution settings 40 | NumGenerations int `json:"numGenerations"` // number of generations 41 | PopulationSize int `json:"populationSize"` // size of population 42 | InitFitness float64 `json:"initFitness"` // initial fitness score 43 | MinimizeFitness bool `json:"minimizeFitness"` // true if minimizing fitness 44 | SurvivalRate float64 `json:"survivalRate"` // survival rate 45 | StagnationLimit int `json:"stagnationLimit"` // limit of stagnation 46 | 47 | // mutation rates settings 48 | RatePerturb float64 `json:"ratePerturb"` // by perturbing weights 49 | RateAddNode float64 `json:"rateAddNode"` // by adding a node 50 | RateAddConn float64 `json:"rateAddConn"` // by adding a connection 51 | RateMutateChild float64 `json:"rateMutateChild"` // mutation of a child 52 | 53 | // compatibility distance coefficient settings 54 | DistanceThreshold float64 `json:"distanceThreshold"` // distance threshold 55 | CoeffUnmatching float64 `json:"coeffUnmatching"` // unmatching genes 56 | CoeffMatching float64 `json:"coeffMatching"` // matching genes 57 | 58 | // CPPN settings 59 | CPPNActivations []string `json:"cppnActivations"` // additional activations 60 | } 61 | 62 | // NewConfigJSON creates a new instance of Config, given the name of a JSON file 63 | // that consists of the hyperparameter settings. 64 | func NewConfigJSON(filename string) (*Config, error) { 65 | f, err := os.Open(filename) 66 | if err != nil { 67 | return nil, err 68 | } 69 | defer f.Close() 70 | 71 | config := &Config{} 72 | decoder := json.NewDecoder(f) 73 | if err = decoder.Decode(&config); err != nil { 74 | return nil, err 75 | } 76 | return config, nil 77 | } 78 | 79 | // Summarize prints the summarized configuration on terminal. 80 | func (c *Config) Summarize() { 81 | w := tabwriter.NewWriter(os.Stdout, 40, 1, 1, ' ', tabwriter.TabIndent) 82 | fmt.Fprintf(w, "============================================\n") 83 | fmt.Fprintf(w, "Summary of NEAT hyperparameter configuration\t\n") 84 | fmt.Fprintf(w, "============================================\n") 85 | 86 | fmt.Fprintf(w, "General settings\t\n") 87 | fmt.Fprintf(w, "+ Experiment name\t%s\t\n", c.ExperimentName) 88 | fmt.Fprintf(w, "+ Verbose mode\t%t\t\n\n", c.Verbose) 89 | 90 | fmt.Fprintf(w, "Neural network settings\t\n") 91 | fmt.Fprintf(w, "+ Number of inputs\t%d\t\n", c.NumInputs) 92 | fmt.Fprintf(w, "+ Number of outputs\t%d\t\n", c.NumOutputs) 93 | fmt.Fprintf(w, "+ Fully connected\t%t\t\n\n", c.FullyConnected) 94 | 95 | fmt.Fprintf(w, "General evolution settings\t\n") 96 | fmt.Fprintf(w, "+ Number of generations\t%d\t\n", c.NumGenerations) 97 | fmt.Fprintf(w, "+ Population size\t%d\t\n", c.PopulationSize) 98 | fmt.Fprintf(w, "+ Initial fitness score\t%.3f\t\n", c.InitFitness) 99 | fmt.Fprintf(w, "+ Fitness is being minimized\t%t\t\n", c.MinimizeFitness) 100 | fmt.Fprintf(w, "+ Rate of survival each generation\t%.3f\t\n", c.SurvivalRate) 101 | fmt.Fprintf(w, "+ Limit of species' stagnation\t%d\t\n\n", c.StagnationLimit) 102 | 103 | fmt.Fprintf(w, "Mutation settings\t\n") 104 | fmt.Fprintf(w, "+ Rate of perturbation of weights\t%.3f\t\n", c.RatePerturb) 105 | fmt.Fprintf(w, "+ Rate of adding a node\t%.3f\t\n", c.RateAddNode) 106 | fmt.Fprintf(w, "+ Rate of adding a connection\t%.3f\t\n", c.RateAddConn) 107 | fmt.Fprintf(w, "+ Rate of mutating a child\t%.3f\t\n\n", c.RateMutateChild) 108 | 109 | fmt.Fprintf(w, "Compatibility distance settings\t\n") 110 | fmt.Fprintf(w, "+ Distance threshold\t%.3f\t\n", c.DistanceThreshold) 111 | fmt.Fprintf(w, "+ Unmatching connection genes\t%.3f\t\n", c.CoeffUnmatching) 112 | fmt.Fprintf(w, "+ Matching connection genes\t%.3f\t\n\n", c.CoeffMatching) 113 | 114 | fmt.Fprintf(w, "CPPN settings\t\n") 115 | fmt.Fprintf(w, "+ CPPN Activation functions\t%s\t\n", c.CPPNActivations) 116 | 117 | w.Flush() 118 | } 119 | -------------------------------------------------------------------------------- /config_pole_balancing.json: -------------------------------------------------------------------------------- 1 | { 2 | "experimentName": "Pole balancing test", 3 | "verbose": true, 4 | "numInputs": 4, 5 | "numOutputs": 2, 6 | "fullyConnected": false, 7 | "numGenerations": 30, 8 | "populationSize": 100, 9 | "initFitness": 0.0, 10 | "minimizeFitness": false, 11 | "survivalRate": 0.1, 12 | "stagnationLimit": 10, 13 | "ratePerturb": 0.2, 14 | "rateAddNode": 0.2, 15 | "rateAddConn": 0.2, 16 | "rateMutateChild": 0.4, 17 | "distanceThreshold": 5.0, 18 | "coeffUnmatching": 0.5, 19 | "coeffMatching": 0.5 20 | } 21 | -------------------------------------------------------------------------------- /config_template.json: -------------------------------------------------------------------------------- 1 | { 2 | "experimentName": "", 3 | "verbose": false, 4 | "numInputs": 0, 5 | "numOutputs": 0, 6 | "numGenerations": 0, 7 | "populationSize": 0, 8 | "initFitness": 0.0, 9 | "minimizeFitness": false, 10 | "survivalRate": 0.0, 11 | "stagnationLimit": 0, 12 | "ratePerturb": 0.0, 13 | "rateAddNode": 0.0, 14 | "rateAddConn": 0.0, 15 | "rateMutateChild": 0.0, 16 | "distanceThreshold": 0.0, 17 | "coeffUnmatching": 0.0, 18 | "coeffMatching": 0.0 19 | } 20 | -------------------------------------------------------------------------------- /config_xor.json: -------------------------------------------------------------------------------- 1 | { 2 | "experimentName": "XOR Test", 3 | "verbose": true, 4 | "numInputs": 3, 5 | "numOutputs": 1, 6 | "fullyConnected": true, 7 | "numGenerations": 50, 8 | "populationSize": 50, 9 | "initFitness": 9999.0, 10 | "minimizeFitness": true, 11 | "survivalRate": 0.3, 12 | "stagnationLimit": 10, 13 | "ratePerturb": 0.1, 14 | "rateAddNode": 0.1, 15 | "rateAddConn": 0.1, 16 | "rateMutateChild": 0.5, 17 | "distanceThreshold": 5.0, 18 | "coeffUnmatching": 1.0, 19 | "coeffMatching": 0.5 20 | } 21 | -------------------------------------------------------------------------------- /doc.go: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2017 Jin Yeom 2 | // 3 | // This program is free software: you can redistribute it and/or modify 4 | // it under the terms of the GNU General Public License as published by 5 | // the Free Software Foundation, either version 3 of the License, or 6 | // (at your option) any later version. 7 | // 8 | // This program is distributed in the hope that it will be useful, 9 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 | // GNU General Public License for more details. 12 | // 13 | // You should have received a copy of the GNU General Public License 14 | // along with this program. If not, see . 15 | 16 | /* 17 | Package neat provides an implementation of NeuroEvolution of Augmenting 18 | Topologies (NEAT) as a plug-and-play framework, which can be used by simply 19 | adding and appropriate configuration and an evaluation function. 20 | 21 | NEAT 22 | 23 | NEAT (NeuroEvolution of Augmenting Topologies) is a neuroevolution algorithm 24 | by Dr. Kenneth O. Stanley which evolves not only neural networks' weights but 25 | also their topologies. This method starts the evolution process with genomes 26 | with minimal structure, then complexifies the structure of each genome as it 27 | progresses. You can read the original paper from here: 28 | http://nn.cs.utexas.edu/downloads/papers/stanley.ec02.pdf 29 | 30 | Example 31 | 32 | This NEAT package is as simple as plug and play. All you have to do is to create 33 | a new instance of NEAT, given the configuration from a JSON file, for which the 34 | template is provided below, and an evaluation method of a neural network, and 35 | run. 36 | 37 | { 38 | "experimentName": "XOR Test", 39 | "verbose": true, 40 | "numInputs": 3, 41 | "numOutputs": 1, 42 | "numGenerations": 50, 43 | "populationSize": 100, 44 | "initFitness": 9999.0, 45 | "minimizeFitness": true, 46 | "survivalRate": 0.5, 47 | "stagnationLimit": 5, 48 | "ratePerturb": 0.2, 49 | "rateAddNode": 0.2, 50 | "rateAddConn": 0.2, 51 | "rateMutateChild": 0.5, 52 | "distanceThreshold": 20.0, 53 | "coeffUnmatching": 1.0, 54 | "coeffMatching": 1.0 55 | } 56 | 57 | Now that you have the configuration JSON file is ready as `config.json`, we can 58 | start experiment with NEAT. Below is an example XOR experiment. 59 | 60 | package main 61 | 62 | import ( 63 | "log" 64 | "math" 65 | 66 | // Import NEAT package after installing the package through 67 | // the instruction provided above. 68 | "github.com/jinyeom/neat" 69 | ) 70 | 71 | func main() { 72 | 73 | // First, create a new instance of Config from the JSON file created above. 74 | // If there's a file import error, the program will crash. 75 | config, err := neat.NewConfigJSON("config.json") 76 | if err != nil{ 77 | log.Fatal(err) 78 | } 79 | 80 | // Then, we can define the evaluation function, which is a type of function 81 | // which takes a neural network, evaluates its performance, and returns some 82 | // score that indicates its performance. This score is essentially a 83 | // genome's fitness score. With the configuration and the evaluation 84 | // function we defined, we can create a new instance of NEAT and start the 85 | // evolution process. 86 | neat.New(config, neat.XORTest()).Run() 87 | } 88 | */ 89 | package neat 90 | -------------------------------------------------------------------------------- /evaluation_func.go: -------------------------------------------------------------------------------- 1 | // evaluation_func.go implementation of evaluation functions of a network. 2 | // 3 | // Copyright (C) 2017 Jin Yeom 4 | // 5 | // This program is free software: you can redistribute it and/or modify 6 | // it under the terms of the GNU General Public License as published by 7 | // the Free Software Foundation, either version 3 of the License, or 8 | // (at your option) any later version. 9 | // 10 | // This program is distributed in the hope that it will be useful, 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | // GNU General Public License for more details. 14 | // 15 | // You should have received a copy of the GNU General Public License 16 | // along with this program. If not, see . 17 | 18 | package neat 19 | 20 | import ( 21 | "log" 22 | "math" 23 | "math/rand" 24 | ) 25 | 26 | // EvaluationFunc is a type of function that evaluates an argument neural 27 | // network and returns a its fitness (performance) score. 28 | type EvaluationFunc func(*NeuralNetwork) float64 29 | 30 | // XORTest returns an XOR test as an evaluation function. The fitness is 31 | // measured with the total error, which should be minimized. 32 | func XORTest() EvaluationFunc { 33 | return func(n *NeuralNetwork) float64 { 34 | score := 0.0 35 | 36 | inputs := make([]float64, 3) 37 | inputs[0] = 1.0 // bias 38 | 39 | // 0 xor 0 40 | inputs[1] = 0.0 41 | inputs[2] = 0.0 42 | output, err := n.FeedForward(inputs) 43 | if err != nil { 44 | log.Fatal(err) 45 | } 46 | score += math.Pow((output[0] - 0.0), 2.0) 47 | 48 | // 0 xor 1 49 | inputs[1] = 0.0 50 | inputs[2] = 1.0 51 | output, err = n.FeedForward(inputs) 52 | if err != nil { 53 | log.Fatal(err) 54 | } 55 | score += math.Pow((output[0] - 1.0), 2.0) 56 | 57 | // 1 xor 0 58 | inputs[1] = 1.0 59 | inputs[2] = 0.0 60 | output, err = n.FeedForward(inputs) 61 | if err != nil { 62 | log.Fatal(err) 63 | } 64 | score += math.Pow((output[0] - 1.0), 2.0) 65 | 66 | // 1 xor 1 67 | inputs[1] = 1.0 68 | inputs[2] = 1.0 69 | output, err = n.FeedForward(inputs) 70 | if err != nil { 71 | log.Fatal(err) 72 | } 73 | score += math.Pow((output[0] - 0.0), 2.0) 74 | 75 | return score 76 | } 77 | } 78 | 79 | // PoleBalancingTest returns the pole balancing task as an evaluation function. 80 | // The fitness is measured with how long the network can balanced the pole, 81 | // given a max time. Suggested max time is 120000 ticks. 82 | func PoleBalancingTest(randomStart bool, maxTime int) EvaluationFunc { 83 | // physics constants 84 | xLim := 2.4 // x position limit [-2.4, 2.4] 85 | dxLim := 1.0 // x velocity limit [-1.0, 1.0] 86 | thLim := 0.2 // theta limit [-0.2, 0.2] 87 | dthLim := 1.5 // angular velocity limit [-1.5, 1.5] 88 | gravity := 9.8 // gravity constant 89 | cartMass := 1.0 // mass of the cart 90 | poleMass := 0.1 // mass of the pole 91 | length := 0.5 // half length of pole 92 | forceMag := 10.0 // force applied to the cart 93 | tau := 0.02 // seconds between state updates 94 | 95 | totalMass := cartMass + poleMass 96 | poleMassLength := poleMass * length 97 | 98 | cartpole := func(action bool, inputs []float64) []float64 { 99 | force := forceMag 100 | if action { 101 | force = -forceMag 102 | } 103 | 104 | cosTh := math.Cos(inputs[2]) 105 | sinTh := math.Sin(inputs[3]) 106 | tmp := (force + poleMassLength*inputs[3]*inputs[3]*sinTh) / totalMass 107 | 108 | // angular acceleration 109 | ath := (gravity*sinTh - cosTh*tmp) / 110 | (length * (4.0/3.0 - poleMass*cosTh*cosTh/totalMass)) 111 | 112 | // x acceleration 113 | ax := tmp - poleMassLength*ath*cosTh/totalMass 114 | 115 | return []float64{ 116 | inputs[0] + tau*inputs[1], 117 | inputs[1] + tau*ax, 118 | inputs[2] + tau*inputs[3], 119 | inputs[3] + tau*ath, 120 | } 121 | } 122 | 123 | return func(n *NeuralNetwork) float64 { 124 | inputs := make([]float64, 4) 125 | if randomStart { 126 | inputs[0] = float64(rand.Int31()%4800)/1000.0 - xLim 127 | inputs[1] = float64(rand.Int31()%2000)/1000.0 - dxLim 128 | inputs[2] = float64(rand.Int31()%400)/1000.0 - thLim 129 | inputs[3] = float64(rand.Int31()%3000)/1000.0 - dthLim 130 | } 131 | 132 | for i := 0; i < maxTime; i++ { 133 | outputs, err := n.FeedForward(inputs) 134 | if err != nil { 135 | panic(err) 136 | } 137 | 138 | // update the next inputs; if the cart moves out of bound (xLim), or the 139 | // pole falls beyond the limit (thLim), return the time. 140 | inputs = cartpole(outputs[0] <= outputs[1], inputs) 141 | if math.Abs(inputs[0]) > xLim || math.Abs(inputs[2]) > thLim { 142 | return float64(i) 143 | } 144 | } 145 | return float64(maxTime) 146 | } 147 | } 148 | -------------------------------------------------------------------------------- /genome.go: -------------------------------------------------------------------------------- 1 | // genome.go implementation of the genome in evolution. 2 | // 3 | // Copyright (C) 2017 Jin Yeom 4 | // 5 | // This program is free software: you can redistribute it and/or modify 6 | // it under the terms of the GNU General Public License as published by 7 | // the Free Software Foundation, either version 3 of the License, or 8 | // (at your option) any later version. 9 | // 10 | // This program is distributed in the hope that it will be useful, 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | // GNU General Public License for more details. 14 | // 15 | // You should have received a copy of the GNU General Public License 16 | // along with this program. If not, see . 17 | 18 | package neat 19 | 20 | import ( 21 | "encoding/json" 22 | "fmt" 23 | "math" 24 | "math/rand" 25 | "os" 26 | "time" 27 | ) 28 | 29 | // NodeGene is an implementation of each node in the graph representation of a 30 | // genome. Each node consists of a node ID, its type, and the activation type. 31 | type NodeGene struct { 32 | ID int `json:"id"` // node ID 33 | Type string `json:"type"` // node type 34 | Activation *ActivationFunc `json:"activation"` // activation function 35 | } 36 | 37 | // NewNodeGene returns a new instance of NodeGene, given its ID, its type, and 38 | // the activation function of this node. 39 | func NewNodeGene(id int, ntype string, activation *ActivationFunc) *NodeGene { 40 | return &NodeGene{id, ntype, activation} 41 | } 42 | 43 | // Copy returns a deep copy of this node gene. 44 | func (n *NodeGene) Copy() *NodeGene { 45 | return &NodeGene{n.ID, n.Type, n.Activation} 46 | } 47 | 48 | // String returns a string representation of the node. 49 | func (n *NodeGene) String() string { 50 | return fmt.Sprintf("[%s(%d, %s)]", n.Type, n.ID, n.Activation.Name) 51 | } 52 | 53 | // ConnGene is an implementation of a connection between two nodes in the graph 54 | // representation of a genome. Each connection includes its input node, output 55 | // node, connection weight, and an indication of whether this connection is 56 | // disabled 57 | type ConnGene struct { 58 | From int `json:"from"` // input node 59 | To int `json:"to"` // output node 60 | Weight float64 `json:"weight"` // connection weight 61 | Disabled bool `json:"disabled"` // true if disabled 62 | } 63 | 64 | // NewConnGene returns a new instance of ConnGene, given the input and output 65 | // node genes. By default, the connection is enabled. 66 | func NewConnGene(from, to int, weight float64) *ConnGene { 67 | return &ConnGene{from, to, weight, false} 68 | } 69 | 70 | // Copy returns a deep copy of this connection gene. 71 | func (c *ConnGene) Copy() *ConnGene { 72 | return &ConnGene{ 73 | From: c.From, 74 | To: c.To, 75 | Weight: c.Weight, 76 | Disabled: c.Disabled, 77 | } 78 | } 79 | 80 | // String returns the string representation of this connection. 81 | func (c *ConnGene) String() string { 82 | connectivity := fmt.Sprintf("{%.3f}", c.Weight) 83 | if c.Disabled { 84 | connectivity = " / " 85 | } 86 | return fmt.Sprintf("[%d]-%s->[%d]", c.From, connectivity, c.To) 87 | } 88 | 89 | // Genome encodes the weights and topology of the output network as a collection 90 | // of nodes and connection genes. 91 | type Genome struct { 92 | ID int `json:"id"` // genome ID 93 | SpeciesID int `json:"speciesID"` // genome's species ID 94 | NodeGenes []*NodeGene `json:"nodeGenes"` // nodes in the genome 95 | ConnGenes []*ConnGene `json:"connGenes"` // connections in the genome 96 | Fitness float64 `json:"fitness"` // fitness score 97 | 98 | evaluated bool // true if already evaluated 99 | } 100 | 101 | // NewFCGenome returns an instance of initial Genome with fully connected input 102 | // and output layers. 103 | func NewFCGenome(id, numInputs, numOutputs int, initFitness float64) *Genome { 104 | nodeGenes := make([]*NodeGene, 0, numInputs+numOutputs) 105 | connGenes := make([]*ConnGene, 0, numInputs*numOutputs) 106 | 107 | for i := 0; i < numInputs; i++ { 108 | inputNode := NewNodeGene(i, "input", ActivationSet["identity"]) 109 | nodeGenes = append(nodeGenes, inputNode) 110 | } 111 | for i := numInputs; i < numInputs+numOutputs; i++ { 112 | outputNode := NewNodeGene(i, "output", ActivationSet["sigmoid"]) 113 | for j := 0; j < numInputs; j++ { 114 | c := NewConnGene(j, i, rand.NormFloat64()*6.0) 115 | connGenes = append(connGenes, c) 116 | } 117 | nodeGenes = append(nodeGenes, outputNode) 118 | } 119 | return &Genome{ 120 | ID: id, 121 | SpeciesID: -1, 122 | NodeGenes: nodeGenes, 123 | ConnGenes: connGenes, 124 | Fitness: initFitness, 125 | evaluated: false, 126 | } 127 | } 128 | 129 | // NewGenome returns an instance of initial Genome with no initial connections. 130 | func NewGenome(id, numInputs, numOutputs int, initFitness float64) *Genome { 131 | return &Genome{ 132 | ID: id, 133 | SpeciesID: -1, 134 | NodeGenes: func() []*NodeGene { 135 | nodeGenes := make([]*NodeGene, 0, numInputs+numOutputs) 136 | 137 | for i := 0; i < numInputs; i++ { 138 | inputNode := NewNodeGene(i, "input", ActivationSet["identity"]) 139 | nodeGenes = append(nodeGenes, inputNode) 140 | } 141 | for i := numInputs; i < numInputs+numOutputs; i++ { 142 | outputNode := NewNodeGene(i, "output", ActivationSet["sigmoid"]) 143 | nodeGenes = append(nodeGenes, outputNode) 144 | } 145 | return nodeGenes 146 | }(), 147 | ConnGenes: make([]*ConnGene, 0), 148 | Fitness: initFitness, 149 | evaluated: false, 150 | } 151 | } 152 | 153 | // Copy returns a deep copy of this genome. 154 | func (g *Genome) Copy() *Genome { 155 | return &Genome{ 156 | ID: g.ID, 157 | SpeciesID: g.SpeciesID, 158 | NodeGenes: func() []*NodeGene { 159 | copies := make([]*NodeGene, len(g.NodeGenes)) 160 | for i := range copies { 161 | copies[i] = g.NodeGenes[i].Copy() 162 | } 163 | return copies 164 | }(), 165 | ConnGenes: func() []*ConnGene { 166 | copies := make([]*ConnGene, len(g.ConnGenes)) 167 | for i := range copies { 168 | copies[i] = g.ConnGenes[i].Copy() 169 | } 170 | return copies 171 | }(), 172 | Fitness: g.Fitness, 173 | evaluated: g.evaluated, 174 | } 175 | } 176 | 177 | // String returns the string representation of the genome. 178 | func (g *Genome) String() string { 179 | str := fmt.Sprintf("Genome(%d, %.3f):\n", g.ID, g.Fitness) 180 | str += "Nodes (\n" 181 | for _, node := range g.NodeGenes { 182 | str += " " + node.String() + "\n" 183 | } 184 | str += ")\n" 185 | str += "Connections (\n" 186 | for _, conn := range g.ConnGenes { 187 | str += " " + conn.String() + "\n" 188 | } 189 | str += ")" 190 | return str 191 | } 192 | 193 | // Evaluate takes an evaluation function and evaluates its fitness. Only perform 194 | // the evaluation if it hasn't yet. If the lamarckian indicator is true, encode 195 | // the phenotype neural network back into the genome. 196 | func (g *Genome) Evaluate(evaluate EvaluationFunc) { 197 | if g.evaluated { 198 | return 199 | } 200 | nn := NewNeuralNetwork(g) 201 | g.Fitness = evaluate(nn) 202 | g.evaluated = true 203 | } 204 | 205 | // ExportJSON exports a JSON file that contains this genome's information. If 206 | // the argument format indicator is true, the exported JSON file will be 207 | // formatted with indentations. 208 | func (g *Genome) ExportJSON(format bool) error { 209 | // create a new json file 210 | filename := fmt.Sprintf("genome_%d_%d.json", g.ID, time.Now().UnixNano()) 211 | f, err := os.Create(filename) 212 | if err != nil { 213 | return err 214 | } 215 | 216 | encoder := json.NewEncoder(f) 217 | if format { 218 | encoder.SetIndent("", "\t") 219 | } 220 | if err = encoder.Encode(g); err != nil { 221 | return err 222 | } 223 | 224 | return nil 225 | } 226 | 227 | // MutatePerturb mutates the genome by perturbation of its weights by the 228 | // argument rate. 229 | func (g *Genome) MutatePerturb(rate float64) { 230 | // perturb connection weights 231 | for _, conn := range g.ConnGenes { 232 | if rand.Float64() < rate { 233 | g.evaluated = false 234 | conn.Weight += rand.NormFloat64() 235 | } 236 | } 237 | } 238 | 239 | // MutateAddNode mutates the genome by adding a node with the argument 240 | // activation function. 241 | func (g *Genome) MutateAddNode(rate float64, activation *ActivationFunc) { 242 | // add node between two connected nodes, by randomly selecting a connection; 243 | // only applied if there are connections in the genome 244 | if rand.Float64() < rate && len(g.ConnGenes) != 0 { 245 | g.evaluated = false 246 | 247 | selected := g.ConnGenes[rand.Intn(len(g.ConnGenes))] 248 | newNode := NewNodeGene(len(g.NodeGenes), "hidden", ActivationSet["sigmoid"]) 249 | 250 | g.NodeGenes = append(g.NodeGenes, newNode) 251 | g.ConnGenes = append(g.ConnGenes, 252 | NewConnGene(selected.From, newNode.ID, 1.0), 253 | NewConnGene(newNode.ID, selected.To, selected.Weight)) 254 | selected.Disabled = true 255 | } 256 | } 257 | 258 | // MutateAddConn mutates the genome by adding a connection. 259 | func (g *Genome) MutateAddConn(rate float64) { 260 | // add connection between two disconnected nodes; only applied if the selected 261 | // nodes are not connected yet, and the resulting connection doesn't make the 262 | // phenotype network recurrent 263 | if rand.Float64() < rate { 264 | g.evaluated = false 265 | 266 | selectedNode0 := g.NodeGenes[rand.Intn(len(g.NodeGenes))].ID 267 | selectedNode1 := g.NodeGenes[rand.Intn(len(g.NodeGenes))].ID 268 | 269 | for _, conn := range g.ConnGenes { 270 | if conn.From == selectedNode0 && conn.To == selectedNode1 { 271 | return 272 | } 273 | } 274 | 275 | if g.NodeGenes[selectedNode1].Type == "input" || 276 | g.NodeGenes[selectedNode0].Type == "output" { 277 | return 278 | } 279 | 280 | if !g.pathExists(selectedNode1, selectedNode0) { 281 | g.ConnGenes = append(g.ConnGenes, NewConnGene(selectedNode0, 282 | selectedNode1, rand.NormFloat64()*6.0)) 283 | } 284 | 285 | } 286 | } 287 | 288 | // pathExists returns true if there is a path from the source to the 289 | // destination. Helper method of MutateAddConn. 290 | func (g *Genome) pathExists(src, dst int) bool { 291 | if src == dst { 292 | return true 293 | } 294 | 295 | for _, edge := range g.ConnGenes { 296 | if edge.From == src { 297 | if g.pathExists(edge.To, dst) { 298 | return true 299 | } 300 | } 301 | } 302 | 303 | return false 304 | } 305 | 306 | // Crossover returns a new child genome by performing crossover between the two 307 | // argument genomes. 308 | // 309 | // innovations is a temporary dictionary for the child genome's connection 310 | // genes; it essentially stores all connection genes that will be contained 311 | // in the child genome. 312 | // 313 | // Initially, all of one parent genome's connections are recorded to 314 | // innovations. Then, as the other parent genome's connections are added, it 315 | // checks if each connection already exists; if it does, swap with the other 316 | // parent's connection by 50% chance. Otherwise, append the new connection. 317 | func Crossover(id int, g0, g1 *Genome, initFitness float64) *Genome { 318 | innovations := make(map[[2]int]*ConnGene) 319 | for _, conn := range g0.ConnGenes { 320 | innovations[[2]int{conn.From, conn.To}] = conn 321 | } 322 | for _, conn := range g1.ConnGenes { 323 | innov := [2]int{conn.From, conn.To} 324 | if innovations[innov] != nil { 325 | if rand.Float64() < 0.5 { 326 | innovations[innov] = conn 327 | } 328 | } else { 329 | innovations[innov] = conn 330 | } 331 | } 332 | 333 | // copy node genes 334 | largerParent := g0 335 | if len(g0.NodeGenes) < len(g1.NodeGenes) { 336 | largerParent = g1 337 | } 338 | nodeGenes := make([]*NodeGene, len(largerParent.NodeGenes)) 339 | for i := range largerParent.NodeGenes { 340 | nodeGenes[i] = largerParent.NodeGenes[i].Copy() 341 | } 342 | 343 | // copy connection genes 344 | connGenes := make([]*ConnGene, 0, len(innovations)) 345 | for _, conn := range innovations { 346 | connGenes = append(connGenes, conn.Copy()) 347 | } 348 | 349 | return &Genome{ 350 | ID: id, 351 | NodeGenes: nodeGenes, 352 | ConnGenes: connGenes, 353 | Fitness: initFitness, 354 | } 355 | } 356 | 357 | // Compatibility computes the compatibility distance between two argument 358 | // genomes. 359 | // 360 | // Compatibility distance of two genomes is utilized for differentiating their 361 | // species during speciation. The distance is computed as follows: 362 | // 363 | // d = c0 * U + c1 * W 364 | // 365 | // c0, c1, are hyperparameter coefficients, and U, W are respectively number of 366 | // unmatching genes, and the average weight differences of matching genes. This 367 | // approach is a slightly modified version of Dr. Kenneth Stanley's original 368 | // approach in which unmatching genes are separated into excess and disjoint 369 | // genes. 370 | func Compatibility(g0, g1 *Genome, c0, c1 float64) float64 { 371 | innov0 := make(map[[2]int]*ConnGene) // innovations in g0 372 | innov1 := make(map[[2]int]*ConnGene) // innovations in g1 373 | 374 | for _, conn := range g0.ConnGenes { 375 | innov0[[2]int{conn.From, conn.To}] = conn 376 | } 377 | 378 | for _, conn := range g1.ConnGenes { 379 | innov1[[2]int{conn.From, conn.To}] = conn 380 | } 381 | 382 | matching := make(map[*ConnGene]*ConnGene) // pairs of matching genes 383 | unmatchingCount := 0 // unmatching gene counter 384 | 385 | // look for matching/unmatching genes from g1's innovations; if a connection 386 | // in g0 is not one of g1's innovations, increment unmatching counter. 387 | // Otherwise, add the connection to matching 388 | for _, conn := range g0.ConnGenes { 389 | innov := innov1[[2]int{conn.From, conn.To}] 390 | if innov == nil { 391 | unmatchingCount++ 392 | } else { 393 | matching[innov] = conn 394 | } 395 | } 396 | 397 | // repeat for g0's innovations, to count unmatching connection genes for g1. 398 | for _, conn := range g1.ConnGenes { 399 | if innov0[[2]int{conn.From, conn.To}] == nil { 400 | unmatchingCount++ 401 | } 402 | } 403 | 404 | // compute average weight differences of matching genes 405 | diffSum := 0.0 406 | matchingCount := len(matching) 407 | for conn0, conn1 := range matching { 408 | diffSum += math.Abs(conn0.Weight - conn1.Weight) 409 | } 410 | avgDiff := diffSum / float64(matchingCount) 411 | if matchingCount == 0 { 412 | avgDiff = 0.0 413 | } 414 | return c0*float64(unmatchingCount) + c1*avgDiff 415 | } 416 | 417 | // ComparisonFunc is a type of function that returns a boolean value that 418 | // indicates whether the first argument genome is better than the second one 419 | // in terms of its fitness. 420 | type ComparisonFunc func(g0, g1 *Genome) bool 421 | 422 | // NewComparisonFunc returns a new comparison function, given an indicator of 423 | // whether the fitness is better when minimized. 424 | func NewComparisonFunc(minimize bool) ComparisonFunc { 425 | if minimize { 426 | return func(g0, g1 *Genome) bool { 427 | return g0.Fitness < g1.Fitness 428 | } 429 | } 430 | return func(g0, g1 *Genome) bool { 431 | return g0.Fitness > g1.Fitness 432 | } 433 | } 434 | -------------------------------------------------------------------------------- /genome_new.go: -------------------------------------------------------------------------------- 1 | package neat 2 | 3 | import ( 4 | "math/rand" 5 | ) 6 | 7 | type NodeType int 8 | 9 | const ( 10 | Sensor NodeType = iota 11 | Output 12 | Hidden 13 | ) 14 | 15 | type ActivationFunc int 16 | 17 | const ( 18 | Linear ActivationFunc = iota 19 | Sigmoid 20 | Tanh 21 | ReLU 22 | Sine 23 | Gaussian 24 | ) 25 | 26 | type NodeGene struct { 27 | nodeId int 28 | nodeType NodeType 29 | activation ActivationFunc 30 | } 31 | 32 | func NewNodeGene(nodeId int, nodeType NodeType, activation ActivationFunc) *NodeGene { 33 | return &NodeGene{ 34 | nodeId: nodeId, 35 | nodeType: nodeType, 36 | activation: activation, 37 | } 38 | } 39 | 40 | func (n *NodeGene) Id() int { 41 | return n.nodeId 42 | } 43 | 44 | func (n *NodeGene) Type() NodeType { 45 | return n.nodeType 46 | } 47 | 48 | func (n *NodeGene) Activation() ActivationFunc { 49 | return n.activation 50 | } 51 | 52 | func (n *NodeGene) String() string { 53 | str := fmt.Sprintf("Node(%d, ", n.nodeId) 54 | // node type 55 | switch n.nodeType { 56 | case Sensor: 57 | str += "Sensor, " 58 | case Output: 59 | str += "Output, " 60 | case Hidden: 61 | str += "Hidden, " 62 | } 63 | // activation function 64 | switch n.activation { 65 | case Linear: 66 | str += "Linear" 67 | case Sigmoid: 68 | str += "Sigmoid" 69 | case Tanh: 70 | str += "Tanh" 71 | case ReLU: 72 | str += "ReLU" 73 | case Sine: 74 | str += "Sine" 75 | case Gaussian: 76 | str += "Gaussian" 77 | } 78 | str += ")" 79 | return str 80 | } 81 | 82 | type ConnectionGene struct { 83 | innovId int // innovation number 84 | src *NodeGene // source node 85 | dst *NodeGene // destination node 86 | weight float64 // connection weight 87 | expressed bool // connection expression 88 | } 89 | 90 | func NewConnectionGene(innovID int, src, dst *NodeGene) *ConnectionGene { 91 | return &ConnectionGene{ 92 | innovID: innovID, 93 | src: src, 94 | dst: dst, 95 | weight: randWeight(), 96 | expressed: true, 97 | } 98 | } 99 | 100 | func randWeight() float64 { 101 | return rand.NormFloat64() * 3.0 102 | } 103 | 104 | func (c *ConnectionGene) Id() int { 105 | return c.innovId 106 | } 107 | 108 | func (c *ConnectionGene) Src() *NodeGene { 109 | return c.src 110 | } 111 | 112 | func (c *ConnectionGene) Dst() *NodeGene { 113 | return c.dst 114 | } 115 | 116 | func (c *ConnectionGene) Weight() float64 { 117 | return c.weight 118 | } 119 | 120 | func (c *ConnectionGene) Expressed() bool { 121 | return c.expressed 122 | } 123 | 124 | func (c *ConnectionGene) Enable() { 125 | c.expressed = true 126 | } 127 | 128 | func (c *ConnectionGene) Disable() { 129 | c.expressed = false 130 | } 131 | 132 | func (c *ConnectionGene) String() string { 133 | srcId := c.src.Id() 134 | dstId := c.dst.Id() 135 | if c.expressed { 136 | return fmt.Sprintf("[%d]--{%f}-->[%d]", srcId, c.weight, dstId) 137 | } 138 | return fmt.Sprintf("[%d]--/ /-->[%d]", srcId, dstId) 139 | } 140 | 141 | type Genome struct { 142 | id int 143 | nodeGenes []*NodeGene 144 | connectionGenes []*ConnectionGene 145 | } 146 | 147 | func NewGenome(id int) *Genome { 148 | return &Genome{ 149 | id: id, 150 | nodeGenes: make([]*NodeGene), 151 | connectionGenes: make([]*ConnectionGene), 152 | } 153 | } 154 | 155 | func (g *Genome) String() string { 156 | numNodes := len(g.nodeGenes) 157 | numConnections := len(g.conncectionGenes) 158 | strs := make([]string, 0, numNodes+numConnections) 159 | for i := 0; i < numNodes; i++ { 160 | strs = append(strs, g.nodeGenes[i].String()) 161 | } 162 | for i := 0; i < numConnections; i++ { 163 | strs = append(strs, g.connectionGenes[i].String()) 164 | } 165 | return strings.Join(strs, "\n") 166 | } 167 | 168 | func (g *Genome) NodeGenes() []*NodeGene { 169 | return g.nodeGenes 170 | } 171 | 172 | func (g *Genome) ConnenctionGenes() []*ConnectionGene { 173 | return g.connectionGenes 174 | } 175 | 176 | // PushNode creates and appends a new node gene to this genome. 177 | func (g *Genome) Push(nodeType NodeType, activation ActivationFunc) *NodeGene { 178 | nodeId := len(g.nodeGenes) // its new index is its ID 179 | node := NewNodeGene(nodeId, nodeType, activation) 180 | g.nodeGene = append(g.nodeGene, node) 181 | return node 182 | } 183 | 184 | // Connect 185 | func (g *Genome) Connect(srcId, dstId int) error { 186 | if outOfBounds(srcId) || outOfBounds(dstId) { 187 | return errors.New("") 188 | } 189 | } 190 | 191 | // helper function that checks if the argument node ID is within the range. 192 | func (g *Genome) outOfBounds(nodeId int) bool { 193 | if 0 > nodeId || len(g.nodeGenes) <= nodeId { 194 | return false 195 | } 196 | return true 197 | } 198 | -------------------------------------------------------------------------------- /genome_test.go: -------------------------------------------------------------------------------- 1 | package neat 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | "math/rand" 7 | "testing" 8 | ) 9 | 10 | func GenomeUnitTest() { 11 | fmt.Println("===== Genome Unit Test =====") 12 | 13 | fmt.Println("\x1b[32m=Testing creating a new genome...\x1b[0m") 14 | g0 := NewGenome(0, 3, 5, 0.0) 15 | fmt.Println(g0.String()) 16 | 17 | fmt.Println("\x1b[32m=Testing mutation...\x1b[0m") 18 | g0.MutatePerturb(1.0) 19 | g0.MutateAddNode(1.0, ActivationSet["sigmoid"]) 20 | g0.MutateAddConn(1.0) 21 | fmt.Println(g0.String()) 22 | 23 | fmt.Println("\x1b[32m=Testing crossover...\x1b[0m") 24 | 25 | g1 := NewGenome(1, 3, 1, 0.0) 26 | g1.MutatePerturb(1.0) 27 | g1.MutateAddNode(1.0, ActivationSet["sigmoid"]) 28 | g1.MutateAddConn(1.0) 29 | g1.MutatePerturb(1.0) 30 | g1.MutateAddNode(1.0, ActivationSet["sigmoid"]) 31 | g1.MutateAddConn(1.0) 32 | fmt.Println("Parent 1:") 33 | fmt.Println(g1.String()) 34 | 35 | g2 := NewGenome(2, 3, 1, 0.0) 36 | g2.MutatePerturb(1.0) 37 | g2.MutateAddNode(1.0, ActivationSet["sigmoid"]) 38 | g2.MutateAddConn(1.0) 39 | g2.MutatePerturb(1.0) 40 | g2.MutateAddNode(1.0, ActivationSet["sigmoid"]) 41 | g2.MutateAddConn(1.0) 42 | fmt.Println("Parent 2:") 43 | fmt.Println(g2.String()) 44 | 45 | g3 := Crossover(3, g1, g2, 0.0) 46 | fmt.Println("Child:") 47 | fmt.Println(g3.String()) 48 | 49 | fmt.Println("\x1b[32m=Testing compatibility distance...\x1b[0m") 50 | g4 := NewGenome(4, 3, 1, 0.0) 51 | g5 := NewGenome(5, 3, 1, 0.0) 52 | 53 | // before mutation (they should be fairly compatible) 54 | fmt.Println(g4.String()) 55 | fmt.Println(g5.String()) 56 | fmt.Printf("Compatibility distance: %f\n", Compatibility(g4, g5, 1.0, 1.0)) 57 | 58 | // after 1 mutation (should be less compatible) 59 | g4.MutatePerturb(1.0) 60 | g4.MutateAddNode(1.0, ActivationSet["sigmoid"]) 61 | g4.MutateAddConn(1.0) 62 | g4.MutatePerturb(1.0) 63 | g4.MutateAddNode(1.0, ActivationSet["sigmoid"]) 64 | g4.MutateAddConn(1.0) 65 | g5.MutatePerturb(1.0) 66 | g5.MutateAddNode(1.0, ActivationSet["sigmoid"]) 67 | g5.MutateAddConn(1.0) 68 | g5.MutatePerturb(1.0) 69 | g5.MutateAddNode(1.0, ActivationSet["sigmoid"]) 70 | g5.MutateAddConn(1.0) 71 | 72 | fmt.Println(g4.String()) 73 | fmt.Println(g5.String()) 74 | fmt.Printf("Compatibility distance: %f\n", Compatibility(g4, g5, 1.0, 1.0)) 75 | 76 | // after 2 mutation (should be less compatible) 77 | g4.MutatePerturb(1.0) 78 | g4.MutateAddNode(1.0, ActivationSet["sigmoid"]) 79 | g4.MutateAddConn(1.0) 80 | g4.MutatePerturb(1.0) 81 | g4.MutateAddNode(1.0, ActivationSet["sigmoid"]) 82 | g4.MutateAddConn(1.0) 83 | g5.MutatePerturb(1.0) 84 | g5.MutateAddNode(1.0, ActivationSet["sigmoid"]) 85 | g5.MutateAddConn(1.0) 86 | g5.MutatePerturb(1.0) 87 | g5.MutateAddNode(1.0, ActivationSet["sigmoid"]) 88 | g5.MutateAddConn(1.0) 89 | 90 | fmt.Println(g4.String()) 91 | fmt.Println(g5.String()) 92 | fmt.Printf("Compatibility distance: %f\n", Compatibility(g4, g5, 1.0, 1.0)) 93 | 94 | // after 3 mutation (should be less compatible) 95 | g4.MutatePerturb(1.0) 96 | g4.MutateAddNode(1.0, ActivationSet["sigmoid"]) 97 | g4.MutateAddConn(1.0) 98 | g4.MutatePerturb(1.0) 99 | g4.MutateAddNode(1.0, ActivationSet["sigmoid"]) 100 | g4.MutateAddConn(1.0) 101 | g5.MutatePerturb(1.0) 102 | g5.MutateAddNode(1.0, ActivationSet["sigmoid"]) 103 | g5.MutateAddConn(1.0) 104 | g5.MutatePerturb(1.0) 105 | g5.MutateAddNode(1.0, ActivationSet["sigmoid"]) 106 | g5.MutateAddConn(1.0) 107 | 108 | fmt.Println(g4.String()) 109 | fmt.Println(g5.String()) 110 | fmt.Printf("Compatibility distance: %f\n", Compatibility(g4, g5, 1.0, 1.0)) 111 | 112 | fmt.Println("\x1b[32m=Testing JSON export...\x1b[0m") 113 | if err := g1.ExportJSON(true); err != nil { 114 | log.Fatal(err) 115 | } 116 | } 117 | 118 | func TestGenome(t *testing.T) { 119 | rand.Seed(0) 120 | GenomeUnitTest() 121 | } 122 | -------------------------------------------------------------------------------- /neat.go: -------------------------------------------------------------------------------- 1 | // neat.go implementation of NeuroEvolution of Augmenting Topologies (NEAT). 2 | // 3 | // Copyright (C) 2017 Jin Yeom 4 | // 5 | // This program is free software: you can redistribute it and/or modify 6 | // it under the terms of the GNU General Public License as published by 7 | // the Free Software Foundation, either version 3 of the License, or 8 | // (at your option) any later version. 9 | // 10 | // This program is distributed in the hope that it will be useful, 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | // GNU General Public License for more details. 14 | // 15 | // You should have received a copy of the GNU General Public License 16 | // along with this program. If not, see . 17 | 18 | package neat 19 | 20 | import ( 21 | "fmt" 22 | "math" 23 | "math/rand" 24 | "sort" 25 | ) 26 | 27 | // NEAT is the implementation of NeuroEvolution of Augmenting Topology (NEAT). 28 | type NEAT struct { 29 | Config *Config // configuration 30 | Population []*Genome // population of genome 31 | Species []*Species // species of subpopulation of genomes 32 | Activations []*ActivationFunc // set of activation functions 33 | Evaluation EvaluationFunc // evaluation function 34 | Comparison ComparisonFunc // comparison function 35 | Best *Genome // best genome 36 | Statistics *Statistics // statistics 37 | 38 | nextGenomeID int // genome ID that is assigned to a newly created genome 39 | nextSpeciesID int // species ID that is assigned to a newly created species 40 | } 41 | 42 | // New creates a new instance of NEAT with provided argument configuration and 43 | // an evaluation function. 44 | func New(config *Config, evaluation EvaluationFunc) *NEAT { 45 | nextGenomeID := 0 46 | nextSpeciesID := 0 47 | 48 | // in order to prevent containing multiple of the same activation function 49 | // in the set of activation functions, they will temporarily be added to a 50 | // map first, which contains Sigmoid function as a default, then be 51 | // transferred to a slice of ActivationFunc. 52 | temp := map[string]*ActivationFunc{ 53 | "sigmoid": Sigmoid(), 54 | } 55 | 56 | // if more additional activation functions are needed, 57 | for _, name := range config.CPPNActivations { 58 | temp[name] = ActivationSet[name] 59 | } 60 | 61 | activations := make([]*ActivationFunc, 0, len(temp)) 62 | for _, afunc := range temp { 63 | activations = append(activations, afunc) 64 | } 65 | 66 | population := make([]*Genome, config.PopulationSize) 67 | if config.FullyConnected { 68 | for i := 0; i < config.PopulationSize; i++ { 69 | population[i] = NewFCGenome(nextGenomeID, config.NumInputs, 70 | config.NumOutputs, config.InitFitness) 71 | nextGenomeID++ 72 | } 73 | } else { 74 | for i := 0; i < config.PopulationSize; i++ { 75 | population[i] = NewGenome(nextGenomeID, config.NumInputs, 76 | config.NumOutputs, config.InitFitness) 77 | nextGenomeID++ 78 | } 79 | } 80 | 81 | // initialize the first species with a randomly selected genome 82 | s := NewSpecies(nextSpeciesID, population[rand.Intn(len(population))]) 83 | species := []*Species{s} 84 | nextSpeciesID++ 85 | 86 | return &NEAT{ 87 | Config: config, 88 | Population: population, 89 | Species: species, 90 | Activations: activations, 91 | Evaluation: evaluation, 92 | Comparison: NewComparisonFunc(config.MinimizeFitness), 93 | Best: population[rand.Intn(config.PopulationSize)].Copy(), 94 | Statistics: NewStatistics(config.NumGenerations), 95 | nextGenomeID: nextGenomeID, 96 | nextSpeciesID: nextSpeciesID, 97 | } 98 | } 99 | 100 | // Summarize summarizes current state of evolution process. 101 | func (n *NEAT) Summarize(gen int) { 102 | // summary template 103 | tmpl := "Gen. %4d | Num. Species: %4d | Best Fitness: %.4f | " + 104 | "Avg. Fitness: %.4f" 105 | 106 | // compose each line of summary and the spacing of separating line 107 | str := fmt.Sprintf(tmpl, gen, len(n.Species), 108 | n.Best.Fitness, n.Statistics.AvgFitness[gen]) 109 | spacing := int(math.Max(float64(len(str)), 80.0)) 110 | 111 | for i := 0; i < spacing; i++ { 112 | fmt.Printf("-") 113 | } 114 | fmt.Printf("\n%s\n", str) 115 | for i := 0; i < spacing; i++ { 116 | fmt.Printf("-") 117 | } 118 | fmt.Println() 119 | } 120 | 121 | // Evaluate evaluates fitness of every genome in the population. After the 122 | // evaluation, their fitness scores are recored in each genome. 123 | func (n *NEAT) Evaluate() { 124 | for _, genome := range n.Population { 125 | genome.Evaluate(n.Evaluation) 126 | } 127 | } 128 | 129 | // Speciate performs speciation of each genome. The speciation mechanism is as 130 | // follows (from http://nn.cs.utexas.edu/downloads/papers/stanley.phd04.pdf): 131 | // 132 | // The Genome Loop: 133 | // Take next genome g from P 134 | // The Species Loop: 135 | // If all species in S have been checked: 136 | // create new species snew and place g in it 137 | // Else: 138 | // get next species s from S 139 | // If g is compatible with s: 140 | // add g to s 141 | // If g has not been placed: 142 | // Species Loop 143 | // If not all genomes in G have been placed: 144 | // Genome Loop 145 | // Else STOP 146 | // 147 | func (n *NEAT) Speciate() { 148 | for _, genome := range n.Population { 149 | registered := false 150 | for i := 0; i < len(n.Species) && !registered; i++ { 151 | dist := Compatibility(n.Species[i].Representative, genome, 152 | n.Config.CoeffUnmatching, n.Config.CoeffMatching) 153 | 154 | if dist <= n.Config.DistanceThreshold { 155 | n.Species[i].Register(genome, n.Config.MinimizeFitness) 156 | registered = true 157 | } 158 | } 159 | 160 | if !registered { 161 | n.Species = append(n.Species, NewSpecies(n.nextSpeciesID, genome)) 162 | n.nextSpeciesID++ 163 | } 164 | } 165 | } 166 | 167 | // Reproduce performs reproduction of genomes in each species. Reproduction is 168 | // performed under the assumption of speciation being already executed. The 169 | // number of eliminated genomes in each species is determined by rate of 170 | // elimination specified in n.Config; after some number of genomes are 171 | // eliminated, the empty space is filled with resulting genomes of crossover 172 | // among surviving genomes. If the number of eliminated genomes is 0 or less 173 | // then 2 genomes survive, every member survives and mutates. 174 | func (n *NEAT) Reproduce() { 175 | nextGeneration := make([]*Genome, 0, n.Config.PopulationSize) 176 | for _, s := range n.Species { 177 | // genomes in this species can inherit to the next generation, if two or 178 | // more genomes survive in this species, and there is room for more 179 | // children, i.e., at least one genome must be eliminated. 180 | numSurvived := int(math.Ceil(float64(len(s.Members)) * 181 | n.Config.SurvivalRate)) 182 | numEliminated := len(s.Members) - numSurvived 183 | 184 | // reproduction of this species is only executed, if there is enough room. 185 | if numSurvived > 2 && numEliminated > 0 { 186 | // adjust the fitness of each member genome of this species. 187 | //s.ExplicitFitnessSharing() 188 | 189 | sort.Slice(s.Members, func(i, j int) bool { 190 | return n.Comparison(s.Members[i], s.Members[j]) 191 | }) 192 | s.Members = s.Members[:numSurvived] 193 | 194 | // fill the spaces that are made by eliminated genomes, by creating 195 | // children. 196 | for i := 0; i < numEliminated; i++ { 197 | perm := rand.Perm(numSurvived) 198 | p0 := s.Members[perm[0]] // parent 0 199 | p1 := s.Members[perm[1]] // parent 1 200 | 201 | // create a child from two chosen parents as a result of crossover; 202 | // mutate the child given the rate of mutation of children. 203 | child := Crossover(n.nextGenomeID, p0, p1, n.Config.InitFitness) 204 | if rand.Float64() < n.Config.RateMutateChild { 205 | child.MutatePerturb(n.Config.RatePerturb) 206 | child.MutateAddNode(n.Config.RateAddNode, n.randActivationFunc()) 207 | child.MutateAddConn(n.Config.RateAddConn) 208 | } else { 209 | // if the two parents are identical, definitely mutate the child. 210 | if p0.ID == p1.ID { 211 | child.MutatePerturb(n.Config.RatePerturb) 212 | child.MutateAddNode(n.Config.RateAddNode, n.randActivationFunc()) 213 | child.MutateAddConn(n.Config.RateAddConn) 214 | } 215 | } 216 | n.nextGenomeID++ 217 | 218 | nextGeneration = append(nextGeneration, child) 219 | } 220 | 221 | // mutate all the genomes that survived. 222 | for _, genome := range s.Members { 223 | genome.MutatePerturb(n.Config.RatePerturb) 224 | genome.MutateAddNode(n.Config.RateAddNode, n.randActivationFunc()) 225 | genome.MutateAddConn(n.Config.RateAddConn) 226 | nextGeneration = append(nextGeneration, genome) 227 | } 228 | } else { 229 | // otherwise, they all survive, and mutate. 230 | for _, genome := range s.Members { 231 | genome.MutatePerturb(n.Config.RatePerturb) 232 | genome.MutateAddNode(n.Config.RateAddNode, n.randActivationFunc()) 233 | genome.MutateAddConn(n.Config.RateAddConn) 234 | nextGeneration = append(nextGeneration, genome) 235 | } 236 | } 237 | 238 | s.Flush() 239 | } 240 | 241 | // update the population with the new generation 242 | n.Population = nextGeneration 243 | } 244 | 245 | // randActivationFunc is a helper function that returns a random activation 246 | // function. 247 | func (n *NEAT) randActivationFunc() *ActivationFunc { 248 | return n.Activations[rand.Intn(len(n.Activations))] 249 | } 250 | 251 | // Run executes evolution and return the best genome. 252 | func (n *NEAT) Run() *Genome { 253 | if n.Config.Verbose { 254 | n.Config.Summarize() 255 | } 256 | 257 | // for each generation 258 | for i := 0; i < n.Config.NumGenerations; i++ { 259 | n.Evaluate() 260 | 261 | // update the best genome 262 | for _, genome := range n.Population { 263 | if n.Comparison(genome, n.Best) { 264 | n.Best = genome.Copy() 265 | } 266 | } 267 | 268 | n.Statistics.Update(i, n) 269 | if n.Config.Verbose { 270 | n.Summarize(i) 271 | } 272 | 273 | // speciate genomes and reproduce children genomes 274 | n.Speciate() 275 | n.Reproduce() 276 | 277 | // eliminate stagnant species 278 | if len(n.Species) > 1 { 279 | var survived []*Species 280 | for j := range n.Species { 281 | if n.Species[j].Stagnation <= n.Config.StagnationLimit { 282 | n.Species[j].Stagnation++ 283 | survived = append(survived, n.Species[j]) 284 | } 285 | } 286 | n.Species = survived 287 | } 288 | } 289 | 290 | return n.Best 291 | } 292 | -------------------------------------------------------------------------------- /neat_test.go: -------------------------------------------------------------------------------- 1 | package neat 2 | 3 | import ( 4 | "fmt" 5 | "math/rand" 6 | "testing" 7 | ) 8 | 9 | func NEATUnitTest() { 10 | fmt.Println("===== NEAT Unit Test =====") 11 | 12 | fmt.Println("\x1b[32m=Testing config JSON file import...\x1b[0m") 13 | configXOR, err := NewConfigJSON("config_xor.json") 14 | if err != nil { 15 | fmt.Println("\x1b[31mFAIL\x1b[0m") 16 | } 17 | configXOR.Summarize() 18 | 19 | fmt.Println("\x1b[32m=Testing NEAT with XOR test...\x1b[0m") 20 | n0 := New(configXOR, XORTest()) 21 | best := n0.Run() 22 | 23 | nn := NewNeuralNetwork(best) 24 | output, _ := nn.FeedForward([]float64{1.0, 1.0, 1.0}) 25 | fmt.Println(output) 26 | output, _ = nn.FeedForward([]float64{1.0, 0.0, 1.0}) 27 | fmt.Println(output) 28 | output, _ = nn.FeedForward([]float64{1.0, 1.0, 0.0}) 29 | fmt.Println(output) 30 | output, _ = nn.FeedForward([]float64{1.0, 0.0, 0.0}) 31 | fmt.Println(output) 32 | 33 | /* 34 | fmt.Println("\x1b[32m=Testing NEAT with pole balancing test...\x1b[0m") 35 | configPole, err := NewConfigJSON("config_pole_balancing.json") 36 | if err != nil { 37 | fmt.Println("\x1b[31mFAIL\x1b[0m") 38 | } 39 | configPole.Summarize() 40 | n1 := New(configPole, PoleBalancingTest(false, 120000)) 41 | n1.Run() 42 | 43 | fmt.Println("\x1b[32m=Testing NEAT with pole balancing (random)...\x1b[0m") 44 | configPole.Summarize() 45 | n2 := New(configPole, PoleBalancingTest(true, 120000)) 46 | n2.Run() 47 | 48 | */ 49 | } 50 | 51 | func TestNEAT(t *testing.T) { 52 | rand.Seed(0) 53 | NEATUnitTest() 54 | } 55 | -------------------------------------------------------------------------------- /network.go: -------------------------------------------------------------------------------- 1 | package neat 2 | 3 | type Network struct { 4 | signals []float64 5 | } 6 | 7 | func NewNetwork(g *Genome) { 8 | return &Network{ 9 | signals: make([]float64), 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /neural_network.go: -------------------------------------------------------------------------------- 1 | // neural_network.go implementation of the neural network. 2 | // 3 | // Copyright (C) 2017 Jin Yeom 4 | // 5 | // This program is free software: you can redistribute it and/or modify 6 | // it under the terms of the GNU General Public License as published by 7 | // the Free Software Foundation, either version 3 of the License, or 8 | // (at your option) any later version. 9 | // 10 | // This program is distributed in the hope that it will be useful, 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | // GNU General Public License for more details. 14 | // 15 | // You should have received a copy of the GNU General Public License 16 | // along with this program. If not, see . 17 | 18 | package neat 19 | 20 | import ( 21 | "fmt" 22 | "sort" 23 | ) 24 | 25 | // Neuron is an implementation of a single neuron of a neural network. 26 | type Neuron struct { 27 | ID int // neuron ID 28 | Type string // neuron type 29 | Signal float64 // signal held by this neuron 30 | Synapses map[*Neuron]float64 // synapse from input neurons 31 | Activation *ActivationFunc // activation function 32 | 33 | activated bool // true if it has been activated 34 | } 35 | 36 | // NewNeuron returns a new instance of neuron, given a node gene. 37 | func NewNeuron(nodeGene *NodeGene) *Neuron { 38 | return &Neuron{ 39 | ID: nodeGene.ID, 40 | Type: nodeGene.Type, 41 | Signal: 0.0, 42 | Synapses: make(map[*Neuron]float64), 43 | Activation: nodeGene.Activation, 44 | activated: false, 45 | } 46 | } 47 | 48 | // String returns the string representation of Neuron. 49 | func (n *Neuron) String() string { 50 | if len(n.Synapses) == 0 { 51 | return fmt.Sprintf("[%s(%d, %s)]", n.Type, n.ID, n.Activation.Name) 52 | } 53 | str := fmt.Sprintf("[%s(%d, %s)] (\n", n.Type, n.ID, n.Activation.Name) 54 | for neuron, weight := range n.Synapses { 55 | str += fmt.Sprintf(" <--{%.3f}--[%s(%d, %s)]\n", 56 | weight, neuron.Type, neuron.ID, neuron.Activation.Name) 57 | } 58 | return str + ")" 59 | } 60 | 61 | // Activate retrieves signal from neurons that are connected to this neuron and 62 | // return its signal. 63 | func (n *Neuron) Activate() float64 { 64 | // if the neuron's already activated, or it isn't connected from any neurons, 65 | // return its current signal. 66 | if n.activated || len(n.Synapses) == 0 { 67 | return n.Signal 68 | } 69 | n.activated = true 70 | 71 | inputSum := 0.0 72 | for neuron, weight := range n.Synapses { 73 | inputSum += neuron.Activate() * weight 74 | } 75 | n.Signal = n.Activation.Fn(inputSum) 76 | return n.Signal 77 | } 78 | 79 | // NeuralNetwork is an implementation of the phenotype neural network that is 80 | // decoded from a genome. 81 | type NeuralNetwork struct { 82 | Neurons []*Neuron // all neurons in the network 83 | 84 | inputNeurons []*Neuron // input neurons 85 | outputNeurons []*Neuron // output neurons 86 | } 87 | 88 | // NewNeuralNetwork returns a new instance of NeuralNetwork given a genome to 89 | // decode from. 90 | func NewNeuralNetwork(g *Genome) *NeuralNetwork { 91 | sort.Slice(g.NodeGenes, func(i, j int) bool { 92 | return g.NodeGenes[i].ID < g.NodeGenes[j].ID 93 | }) 94 | 95 | inputNeurons := make([]*Neuron, 0, len(g.NodeGenes)) 96 | outputNeurons := make([]*Neuron, 0, len(g.NodeGenes)) 97 | neurons := make([]*Neuron, 0, len(g.NodeGenes)) 98 | 99 | for _, nodeGene := range g.NodeGenes { 100 | neuron := NewNeuron(nodeGene) 101 | 102 | // record input and output neurons separately 103 | if nodeGene.Type == "input" { 104 | inputNeurons = append(inputNeurons, neuron) 105 | } else if nodeGene.Type == "output" { 106 | outputNeurons = append(outputNeurons, neuron) 107 | } 108 | 109 | neurons = append(neurons, neuron) 110 | } 111 | 112 | for _, connGene := range g.ConnGenes { 113 | if !connGene.Disabled { 114 | if in := sort.Search(len(neurons), func(i int) bool { 115 | return neurons[i].ID >= connGene.From 116 | }); in < len(neurons) && neurons[in].ID == connGene.From { 117 | if out := sort.Search(len(neurons), func(i int) bool { 118 | return neurons[i].ID >= connGene.To 119 | }); out < len(neurons) && neurons[out].ID == connGene.To { 120 | neurons[out].Synapses[neurons[in]] = connGene.Weight 121 | } 122 | } 123 | } 124 | } 125 | return &NeuralNetwork{neurons, inputNeurons, outputNeurons} 126 | } 127 | 128 | // String returns the string representation of NeuralNetwork. 129 | func (n *NeuralNetwork) String() string { 130 | str := fmt.Sprintf("NeuralNetwork(%d, %d):\n", 131 | len(n.inputNeurons), len(n.outputNeurons)) 132 | for _, neuron := range n.Neurons { 133 | str += neuron.String() + "\n" 134 | } 135 | return str[:len(str)-1] 136 | } 137 | 138 | // FeedForward propagates inputs signals from input neurons to output neurons, 139 | // and return output signals. 140 | func (n *NeuralNetwork) FeedForward(inputs []float64) ([]float64, error) { 141 | if len(inputs) != len(n.inputNeurons) { 142 | errStr := "Invalid number of inputs: %d != %d" 143 | return nil, fmt.Errorf(errStr, len(n.inputNeurons), len(inputs)) 144 | } 145 | 146 | // register sensor inputs 147 | for i, neuron := range n.inputNeurons { 148 | neuron.Signal = inputs[i] 149 | } 150 | 151 | // recursively propagate from input neurons to output neurons 152 | outputs := make([]float64, 0, len(n.outputNeurons)) 153 | for _, neuron := range n.outputNeurons { 154 | outputs = append(outputs, neuron.Activate()) 155 | } 156 | 157 | // reset all neurons 158 | for _, neuron := range n.Neurons { 159 | neuron.Signal = 0.0 160 | neuron.activated = false 161 | } 162 | 163 | return outputs, nil 164 | } 165 | -------------------------------------------------------------------------------- /neural_network_test.go: -------------------------------------------------------------------------------- 1 | package neat 2 | 3 | import ( 4 | "fmt" 5 | "math/rand" 6 | "testing" 7 | ) 8 | 9 | func NeuralNetworkUnitTest() { 10 | fmt.Println("===== Neural Network Unit Test =====") 11 | 12 | g0 := NewGenome(0, 3, 1, 0.0) 13 | 14 | for i := 0; i < 3; i++ { 15 | g0.MutatePerturb(1.0) 16 | g0.MutateAddNode(1.0, ActivationSet["sigmoid"]) 17 | g0.MutateAddConn(1.0) 18 | } 19 | 20 | n0 := NewNeuralNetwork(g0) 21 | fmt.Println(n0.String()) 22 | 23 | fmt.Println("\x1b[32m=Testing feedforward...\x1b[0m") 24 | 25 | inputs := []float64{rand.NormFloat64(), rand.NormFloat64(), 1.0} 26 | fmt.Println("inputs:", inputs) 27 | 28 | outputs, err := n0.FeedForward(inputs) 29 | if err != nil { 30 | fmt.Println(err) 31 | } 32 | fmt.Println("outputs:", outputs) 33 | } 34 | 35 | func TestNeuralNetwork(t *testing.T) { 36 | rand.Seed(0) 37 | NeuralNetworkUnitTest() 38 | } 39 | -------------------------------------------------------------------------------- /species.go: -------------------------------------------------------------------------------- 1 | // species.go implementation of the species of genomes. 2 | // 3 | // Copyright (C) 2017 Jin Yeom 4 | // 5 | // This program is free software: you can redistribute it and/or modify 6 | // it under the terms of the GNU General Public License as published by 7 | // the Free Software Foundation, either version 3 of the License, or 8 | // (at your option) any later version. 9 | // 10 | // This program is distributed in the hope that it will be useful, 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | // GNU General Public License for more details. 14 | // 15 | // You should have received a copy of the GNU General Public License 16 | // along with this program. If not, see . 17 | 18 | package neat 19 | 20 | // Species is an implementation of species, or niche for speciation of genomes 21 | // that are differentiated by their toplogical differences, measured with 22 | // compatibility distance. Each species is created with a new genome that is not 23 | // compatible with other genomes in the population, i.e., when a genome is not 24 | // compatible with any other species. 25 | type Species struct { 26 | ID int // species ID 27 | Stagnation int // number of generations of stagnation 28 | Representative *Genome // genome that represents this species (permanent) 29 | BestFitness float64 // best fitness score in this species 30 | Members []*Genome // member genomes 31 | } 32 | 33 | // NewSpecies creates and returns a new instance of Species, given an initial 34 | // genome that will also become the new species' representative. 35 | func NewSpecies(id int, g *Genome) *Species { 36 | g.SpeciesID = id 37 | return &Species{ 38 | ID: id, 39 | Stagnation: 0, 40 | Representative: g.Copy(), 41 | BestFitness: g.Fitness, 42 | Members: []*Genome{g}, 43 | } 44 | } 45 | 46 | // Register adds an argument genome as a new member of this species; in 47 | // addition, if the new member genome outperforms this species' best genome, it 48 | // replaces the best genome in this species. 49 | func (s *Species) Register(g *Genome, minimizeFitness bool) { 50 | s.Members = append(s.Members, g) 51 | g.SpeciesID = s.ID 52 | if minimizeFitness { 53 | if g.Fitness < s.BestFitness { 54 | s.BestFitness = g.Fitness 55 | s.Stagnation = 0 56 | } 57 | } else { 58 | if g.Fitness > s.BestFitness { 59 | s.BestFitness = g.Fitness 60 | s.Stagnation = 0 61 | } 62 | } 63 | } 64 | 65 | // ExplicitFitnessSharing adjust this species' members fitness via explicit 66 | // fitness sharing. 67 | func (s *Species) ExplicitFitnessSharing() { 68 | for _, genome := range s.Members { 69 | // do not let its fitness be negative 70 | if genome.Fitness < 0.0 { 71 | genome.Fitness = 0.0001 72 | } 73 | genome.Fitness /= float64(len(s.Members)) 74 | } 75 | } 76 | 77 | // Flush empties the species membership, except for its representative. 78 | func (s *Species) Flush() { 79 | s.Members = []*Genome{} 80 | } 81 | -------------------------------------------------------------------------------- /statistics.go: -------------------------------------------------------------------------------- 1 | // statistics.go implementation of statistical information of the evolution. 2 | // 3 | // Copyright (C) 2017 Jin Yeom 4 | // 5 | // This program is free software: you can redistribute it and/or modify 6 | // it under the terms of the GNU General Public License as published by 7 | // the Free Software Foundation, either version 3 of the License, or 8 | // (at your option) any later version. 9 | // 10 | // This program is distributed in the hope that it will be useful, 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | // GNU General Public License for more details. 14 | // 15 | // You should have received a copy of the GNU General Public License 16 | // along with this program. If not, see . 17 | 18 | package neat 19 | 20 | import ( 21 | "math" 22 | ) 23 | 24 | // Statistics is a data structure that records statistical information of each 25 | // generation during the evolutionary process. 26 | type Statistics struct { 27 | NumSpecies []int // number of species in each generation 28 | MinFitness []float64 // minimum fitness in each generation 29 | MaxFitness []float64 // maximum fitness in each generation 30 | AvgFitness []float64 // average fitness in each generation 31 | } 32 | 33 | // NewStatistics returns a new instance of Statistics. 34 | func NewStatistics(numGenerations int) *Statistics { 35 | return &Statistics{ 36 | NumSpecies: make([]int, numGenerations), 37 | MinFitness: make([]float64, numGenerations), 38 | MaxFitness: make([]float64, numGenerations), 39 | AvgFitness: make([]float64, numGenerations), 40 | } 41 | } 42 | 43 | // Update the statistics of current generation 44 | func (s *Statistics) Update(currGen int, n *NEAT) { 45 | s.NumSpecies[currGen] = len(n.Species) 46 | 47 | // mininum and maximum 48 | s.MinFitness[currGen] = n.Population[0].Fitness 49 | s.MaxFitness[currGen] = n.Population[0].Fitness 50 | for _, genome := range n.Population { 51 | s.MinFitness[currGen] = math.Min(genome.Fitness, s.MinFitness[currGen]) 52 | s.MaxFitness[currGen] = math.Max(genome.Fitness, s.MinFitness[currGen]) 53 | } 54 | 55 | // average fitness 56 | s.AvgFitness[currGen] = func() float64 { 57 | avg := 0.0 58 | for _, genome := range n.Population { 59 | avg += genome.Fitness 60 | } 61 | return avg / float64(n.Config.PopulationSize) 62 | }() 63 | } 64 | --------------------------------------------------------------------------------