ga/000755 000767 000000 00000000000 11737425143 012555 5ustar00slackydebwheel000000 000000 ga/COPYING000644 000767 000000 00000043077 11737424747 013634 0ustar00slackydebwheel000000 000000 GNU GENERAL PUBLIC LICENSE Version 2, June 1991 Copyright (C) 1989, 1991 Free Software Foundation, Inc. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This General Public License applies to most of the Free Software Foundation's software and to any other program whose authors commit to using it. (Some other Free Software Foundation software is covered by the GNU Library General Public License instead.) You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. Finally, any free program is threatened constantly by software patents. We wish to avoid the danger that redistributors of a free program will individually obtain patent licenses, in effect making the program proprietary. To prevent this, we have made it clear that any patent must be licensed for everyone's free use or not licensed at all. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any derivative work under copyright law: that is to say, a work containing the Program or a portion of it, either verbatim or with modifications and/or translated into another language. (Hereinafter, translation is included without limitation in the term "modification".) Each licensee is addressed as "you". Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running the Program is not restricted, and the output from the Program is covered only if its contents constitute a work based on the Program (independent of having been made by running the Program). Whether that is true depends on what the Program does. 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and give any other recipients of the Program a copy of this License along with the Program. You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 2. You may modify your copy or copies of the Program or any portion of it, thus forming a work based on the Program, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: a) You must cause the modified files to carry prominent notices stating that you changed the files and the date of any change. b) You must cause any work that you distribute or publish, that in whole or in part contains or is derived from the Program or any part thereof, to be licensed as a whole at no charge to all third parties under the terms of this License. c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the most ordinary way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this License. (Exception: if the Program itself is interactive but does not normally print such an announcement, your work based on the Program is not required to print an announcement.) These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Program, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Program, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Program. In addition, mere aggregation of another work not based on the Program with the Program (or with a work based on the Program) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 3. You may copy and distribute the Program (or a work based on it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you also do one of the following: a) Accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, b) Accompany it with a written offer, valid for at least three years, to give any third party, for a charge no more than your cost of physically performing source distribution, a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, c) Accompany it with the information you received as to the offer to distribute corresponding source code. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form with such an offer, in accord with Subsection b above.) The source code for a work means the preferred form of the work for making modifications to it. For an executable work, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the executable. However, as a special exception, the source code distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. If distribution of executable or object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place counts as distribution of the source code, even though third parties are not compelled to copy the source along with the object code. 4. You may not copy, modify, sublicense, or distribute the Program except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense or distribute the Program is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 5. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Program or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Program (or any work based on the Program), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Program or works based on it. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties to this License. 7. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Program at all. For example, if a patent license would not permit royalty-free redistribution of the Program by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Program. If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply and the section as a whole is intended to apply in other circumstances. It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system, which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 8. If the distribution and/or use of the Program is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Program under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 9. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of this License, you may choose any version ever published by the Free Software Foundation. 10. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, see . Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) year name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here is a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (which makes passes at compilers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice This General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Library General Public License instead of this License. ga/doc/000755 000767 000000 00000000000 11737424747 013333 5ustar00slackydebwheel000000 000000 ga/INDEX000644 000767 000000 00000000402 11737424747 013354 0ustar00slackydebwheel000000 000000 ga >> Genetic Algorithm Genetic Algorithm ga gaoptimset Creation gacreationuniform Fitness Scaling fitscalingrank Selection selectionstochunif Crossover crossoverscattered Mutation mutationgaussian Utility rastriginsfcn Miscellaneous test_ga ga/inst/000755 000767 000000 00000000000 11737424747 013543 5ustar00slackydebwheel000000 000000 ga/NEWS000644 000767 000000 00000001610 11737424747 013263 0ustar00slackydebwheel000000 000000 Summary of important user-visible changes for releases of the ga package =============================================================================== ga-0.10.0 Release Date: 2012-04-06 Release Manager: Luca Favatella =============================================================================== ** Remove dependency on the "communications" package and require Octave version 3.4.0 (or better). The "communications" package was used only for its "randint" function; now the "randi" function, introduced in Octave 3.4.0, is used instead. ** Reorganize the unit test suite. All available tests can be executed by running "test_ga". Fix bug #3287917 (Debian bug #622929). ** Rename the package from "Genetic Algorithm and Direct Search" to "Genetic Algorithm" as only the Genetic Algorithm is implemented. Direct Search will not be implemented in the foreseeable future. ga/inst/__ga_crossoverfcn__.m000644 000767 000000 00000002433 11737424747 017702 0ustar00slackydebwheel000000 000000 ## Copyright (C) 2008 Luca Favatella ## ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with this program; If not, see . ## Author: Luca Favatella ## Version: 1.3.3 function xoverKids = __ga_crossoverfcn__ (parents, options, nvars, FitnessFcn, unused, thisPopulation) ## preconditions nc_parents = columns (parents); if (rem (nc_parents, 2) != 0) error ("'parents' must have an even number of columns"); endif xoverKids(1:(nc_parents / 2), 1:nvars) = options.CrossoverFcn \ (parents(1, 1:nc_parents), options, nvars, FitnessFcn, unused, thisPopulation(:, 1:nvars)); endfunctionga/inst/__ga_initial_population__.m000644 000767 000000 00000007237 11737424747 021100 0ustar00slackydebwheel000000 000000 ## Copyright (C) 2008, 2010 Luca Favatella ## ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with this program; If not, see . ## -*- texinfo -*- ## @deftypefn{Function File} {@var{Population} =} __ga_initial_population__ (@var{GenomeLength}, @var{FitnessFcn}, @var{options}) ## Create an initial population. ## ## @seealso{__ga_problem__} ## @end deftypefn ## Author: Luca Favatella ## Version: 3.3 #TODO consider PopulationSize as a #vector for multiple subpopolations function Population = \ __ga_initial_population__ (GenomeLength, FitnessFcn, options) if (isempty (options.InitialPopulation)) Population(1:options.PopulationSize, 1:GenomeLength) = \ options.CreationFcn (GenomeLength, FitnessFcn, options); else if (columns (options.InitialPopulation) != GenomeLength) ## columns (InitialPopulation) > 0 error ("nonempty 'InitialPopulation' must have 'GenomeLength' columns"); else ## rows (InitialPopulation) > 0 nrIP = rows (options.InitialPopulation); if (nrIP > options.PopulationSize) error ("nonempty 'InitialPopulation' must have no more than \ 'PopulationSize' rows"); elseif (nrIP == options.PopulationSize) Population(1:options.PopulationSize, 1:GenomeLength) = \ options.InitialPopulation; else # rows (InitialPopulation) < PopulationSize ## create a complete new population, and then select only needed ## individuals (creating only a partial population is difficult) CreatedPopulation(1:options.PopulationSize, 1:GenomeLength) = \ options.CreationFcn (GenomeLength, FitnessFcn, options); Population(1:options.PopulationSize, 1:GenomeLength) = vertcat \ (options.InitialPopulation(1:nrIP, 1:GenomeLength), CreatedPopulation(1:(options.PopulationSize - nrIP), 1:GenomeLength)); endif endif endif endfunction %!test %! GenomeLength = 2; %! options = gaoptimset (); %! Population = __ga_initial_population__ (GenomeLength, @rastriginsfcn, options); %! assert (size (Population), [options.PopulationSize, GenomeLength]); %!test %! GenomeLength = 2; %! options = gaoptimset ("InitialPopulation", [1, 2; 3, 4; 5, 6]); %! Population = __ga_initial_population__ (GenomeLength, @rastriginsfcn, options); %! assert (size (Population), [options.PopulationSize, GenomeLength]); ## nonempty 'InitialPopulation' must have 'GenomeLength' columns %!error %! GenomeLength = 2; %! options = gaoptimset ("InitialPopulation", [1; 3; 5]); %! __ga_initial_population__ (GenomeLength, @rastriginsfcn, options); %!error %! GenomeLength = 2; %! options = gaoptimset ("InitialPopulation", [1, 1, 1; 3, 3, 3; 5, 5, 5]); %! __ga_initial_population__ (GenomeLength, @rastriginsfcn, options); ## nonempty 'InitialPopulation' must have no more than 'PopulationSize' rows %!error %! GenomeLength = 2; %! options = gaoptimset ("InitialPopulation", [1, 2; 3, 4; 5, 6], "PopulationSize", 2); %! __ga_initial_population__ (GenomeLength, @rastriginsfcn, options); ga/inst/__ga_mutationfcn__.m000644 000767 000000 00000002267 11737424747 017522 0ustar00slackydebwheel000000 000000 ## Copyright (C) 2008 Luca Favatella ## ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with this program; If not, see . ## Author: Luca Favatella ## Version: 1.3 function mutationChildren = \ __ga_mutationfcn__ (parents, options, nvars, FitnessFcn, state, thisScore, thisPopulation) mutationChildren(1:(columns (parents)), 1:nvars) = \ options.MutationFcn{1, 1} (parents(1, :), options, nvars, FitnessFcn, state, thisScore, thisPopulation(:, 1:nvars)); endfunctionga/inst/__ga_popinitrange__.m000644 000767 000000 00000002141 11737424747 017661 0ustar00slackydebwheel000000 000000 ## Copyright (C) 2008 Luca Favatella ## ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with this program; If not, see . ## Author: Luca Favatella ## Version: 2.1 function [LB UB] = __ga_popinitrange__ (PopInitRange, nvars) switch (columns (PopInitRange)) case 1 tmpPIR(1:2, 1:nvars) = PopInitRange(1:2, 1) * ones (1, nvars); case nvars tmpPIR(1:2, 1:nvars) = PopInitRange(1:2, 1:nvars); endswitch LB(1, 1:nvars) = tmpPIR(1, 1:nvars); UB(1, 1:nvars) = tmpPIR(2, 1:nvars); endfunctionga/inst/__ga_problem__.m000644 000767 000000 00000010240 11737424747 016621 0ustar00slackydebwheel000000 000000 ## Copyright (C) 2008 Luca Favatella ## ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with this program; If not, see . ## Author: Luca Favatella ## Version: 5.9 function [x fval exitflag output population scores] = __ga_problem__ (problem) ## first instruction state.StartTime = time (); ## second instruction output = struct ("randstate", rand ("state"), "randnstate", randn ("state")); ## instructions not to be executed at each generation state.Population(1:problem.options.PopulationSize, 1:problem.nvars) = \ __ga_initial_population__ (problem.nvars, problem.fitnessfcn, problem.options); state.Generation = 0; private_state = __ga_problem_private_state__ (problem.options); state.Selection = __ga_problem_state_selection__ (private_state, problem.options); ## instructions to be executed at each generation state = __ga_problem_update_state_at_each_generation__ (state, problem, private_state); NextPopulation = zeros (problem.options.PopulationSize, problem.nvars); while (! __ga_stop__ (problem, state)) ## fix a generation ## elite if (private_state.ReproductionCount.elite > 0) [trash IndexSortedScores] = sort (state.Score); NextPopulation(state.Selection.elite, 1:problem.nvars) = \ state.Population \ (IndexSortedScores(1:private_state.ReproductionCount.elite, 1), 1:problem.nvars); endif ## selection for crossover and mutation parents(1, 1:private_state.nParents) = __ga_selectionfcn__ \ (state.Expectation, private_state.nParents, problem.options); ## crossover if (private_state.ReproductionCount.crossover > 0) NextPopulation(state.Selection.crossover, 1:problem.nvars) = \ __ga_crossoverfcn__ \ (parents(1, private_state.parentsSelection.crossover), problem.options, problem.nvars, problem.fitnessfcn, false, ## unused state.Population); endif ## mutation if (private_state.ReproductionCount.mutation > 0) NextPopulation(state.Selection.mutation, 1:problem.nvars) = \ __ga_mutationfcn__ \ (parents(1, private_state.parentsSelection.mutation), problem.options, problem.nvars, problem.fitnessfcn, state, state.Score, state.Population); endif ## update state structure state.Population(1:problem.options.PopulationSize, 1:problem.nvars) = NextPopulation; state.Generation++; state = __ga_problem_update_state_at_each_generation__ (state, problem, private_state); endwhile [x fval exitflag output population scores] = \ __ga_problem_return_variables__ (state, problem); endfunction #state structure fields #DONE state.Population #DONE state.Score #DONE state.Generation #DONE state.StartTime #state.StopFlag #DONE state.Selection #DONE state.Expectation #DONE state.Best #state.LastImprovement #state.LastImprovementTime #state.NonlinIneq #state.NonlinEqga/inst/__ga_problem_private_state__.m000644 000767 000000 00000004142 11737424747 021557 0ustar00slackydebwheel000000 000000 ## Copyright (C) 2008 Luca Favatella ## ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with this program; If not, see . ## Author: Luca Favatella ## Version: 1.1.1 function private_state = __ga_problem_private_state__ (options) private_state.ReproductionCount.elite = options.EliteCount; private_state.ReproductionCount.crossover = \ fix (options.CrossoverFraction * (options.PopulationSize - options.EliteCount)); private_state.ReproductionCount.mutation = \ options.PopulationSize - \ (private_state.ReproductionCount.elite + private_state.ReproductionCount.crossover); assert (private_state.ReproductionCount.elite + private_state.ReproductionCount.crossover + private_state.ReproductionCount.mutation, options.PopulationSize); ## DEBUG private_state.parentsCount.crossover = \ 2 * private_state.ReproductionCount.crossover; private_state.parentsCount.mutation = \ 1 * private_state.ReproductionCount.mutation; private_state.nParents = \ private_state.parentsCount.crossover + \ private_state.parentsCount.mutation; private_state.parentsSelection.crossover = \ 1:private_state.parentsCount.crossover; private_state.parentsSelection.mutation = \ private_state.parentsCount.crossover + \ (1:private_state.parentsCount.mutation); assert (length (private_state.parentsSelection.crossover) + length (private_state.parentsSelection.mutation), private_state.nParents); ## DEBUG endfunctionga/inst/__ga_problem_return_variables__.m000644 000767 000000 00000002642 11737424747 022257 0ustar00slackydebwheel000000 000000 ## Copyright (C) 2008 Luca Favatella ## ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with this program; If not, see . ## Author: Luca Favatella ## Version: 1.1 function [x fval exitflag output population scores] = \ __ga_problem_return_variables__ (state, problem) [trash IndexMinScore] = min (state.Score); x = state.Population(IndexMinScore, 1:problem.nvars); fval = problem.fitnessfcn (x); #TODO exitflag ## output.randstate and output.randnstate must be assigned at the ## start of the algorithm output.generations = state.Generation; #TODO output.funccount #TODO output.message #TODO output.maxconstraint population = state.Population; scores = state.Score; endfunctionga/inst/__ga_problem_state_selection__.m000644 000767 000000 00000002560 11737424747 022074 0ustar00slackydebwheel000000 000000 ## Copyright (C) 2008 Luca Favatella ## ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with this program; If not, see . ## Author: Luca Favatella ## Version: 1.1.1 function Selection = __ga_problem_state_selection__ (private_state, options) Selection.elite = 1:private_state.ReproductionCount.elite; Selection.crossover = \ private_state.ReproductionCount.elite + \ (1:private_state.ReproductionCount.crossover); Selection.mutation = \ private_state.ReproductionCount.elite + \ private_state.ReproductionCount.crossover + \ (1:private_state.ReproductionCount.mutation); #assert (length (Selection.elite) + # length (Selection.crossover) + # length (Selection.mutation), # options.PopulationSize); ##DEBUG endfunctionga/inst/__ga_problem_update_state_at_each_generation__.m000644 000767 000000 00000004233 11737424747 025247 0ustar00slackydebwheel000000 000000 ## Copyright (C) 2008, 2010 Luca Favatella ## ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with this program; If not, see . ## Author: Luca Favatella ## Version: 1.3.1 function state = \ __ga_problem_update_state_at_each_generation__ (state, problem, private_state) if ((state.Generation > 0) || isempty (problem.options.InitialScores)) state.Score(1:problem.options.PopulationSize, 1) = \ __ga_scores__ (problem, state.Population); else ## (Generation == 0) && (! isempty (InitialScores)) nrIS = rows (problem.options.InitialScores); #assert (rows (problem.options.InitialPopulation) <= problem.options.PopulationSize); ## DEBUG if (nrIS <= rows (problem.options.InitialPopulation)) missing_rows = (nrIS+1):problem.options.PopulationSize; state.Score(1:problem.options.PopulationSize, 1) = \ [problem.options.InitialScores(:, 1); (__ga_scores__ (problem, state.Population(missing_rows, :))) ]; else error ("rows (InitialScores) > rows (InitialPopulation)"); endif endif state.Expectation(1, 1:problem.options.PopulationSize) = \ problem.options.FitnessScalingFcn (state.Score, private_state.nParents); state.Best(state.Generation + 1, 1) = min (state.Score); endfunction %!error %! state.Generation = 0; %! problem = struct ("fitnessfcn", @rastriginsfcn, "nvars", 2, "options", gaoptimset ("Generations", 10, "InitialScores", [0; 0; 0])); %! unused = 0; %! __ga_problem_update_state_at_each_generation__ (state, problem, unused);ga/inst/__ga_scores__.m000644 000767 000000 00000004004 11737424747 016460 0ustar00slackydebwheel000000 000000 ## Copyright (C) 2008, 2010 Luca Favatella ## ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with this program; If not, see . ## Author: Luca Favatella ## Version: 5.7 function Scores = __ga_scores__ (problem, Population) [nrP ncP] = size (Population); switch problem.options.Vectorized case "on" ## using vectorized evaluation switch problem.options.UseParallel case "always" warning ("'Vectorized' option is 'on': ignoring 'UseParallel' option, even if it is 'always'"); case "never" ## Nothing. otherwise warning ("'Vectorized' option is 'on': ignoring invalid 'UseParallel' option value (it should be 'always' or 'never')"); endswitch Scores = (problem.fitnessfcn (Population))(1:nrP, 1); case "off" ## not using vectorized evaluation switch problem.options.UseParallel case "always" ## using parallel evaluation error ("TODO: implement parallel evaluation of objective function"); case "never" ## using serial evaluation (i.e. loop) tmp = zeros (nrP, 1); for index = 1:nrP tmp(index, 1) = problem.fitnessfcn (Population(index, 1:ncP)); endfor Scores = tmp(1:nrP, 1); otherwise error ("'UseParallel' option must be 'always' or 'never'"); endswitch otherwise error ("'Vectorized' option must be 'on' or 'off'"); endswitch endfunction ga/inst/__ga_selectionfcn__.m000644 000767 000000 00000001670 11737424747 017644 0ustar00slackydebwheel000000 000000 ## Copyright (C) 2008 Luca Favatella ## ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with this program; If not, see . ## Author: Luca Favatella ## Version: 1.2 function parents = __ga_selectionfcn__ (expectation, nParents, options) parents(1, 1:nParents) = \ options.SelectionFcn (expectation(1, :), nParents, options); endfunctionga/inst/__ga_stop__.m000644 000767 000000 00000002530 11737424747 016151 0ustar00slackydebwheel000000 000000 ## Copyright (C) 2008 Luca Favatella ## ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with this program; If not, see . ## -*- texinfo -*- ## @deftypefn{Function File} {@var{stop} =} __ga_stop__ (@var{problem}, @var{state}) ## Determine whether the genetic algorithm should stop. ## ## @seealso{__ga_problem__} ## @end deftypefn ## Author: Luca Favatella ## Version: 5.1 function stop = __ga_stop__ (problem, state) Generations = \ (state.Generation >= problem.options.Generations); TimeLimit = \ ((time () - state.StartTime) >= problem.options.TimeLimit); FitnessLimit = \ (state.Best(state.Generation + 1, 1) <= problem.options.FitnessLimit); stop = (Generations || TimeLimit || FitnessLimit); endfunctionga/inst/__gaoptimset_default_options__.m000644 000767 000000 00000005307 11737424747 022155 0ustar00slackydebwheel000000 000000 ## Copyright (C) 2008, 2009, 2010 Luca Favatella ## ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with this program; If not, see . ## Author: Luca Favatella ## Version: 1.2.3 function default_options = __gaoptimset_default_options__ () default_options.CreationFcn = @gacreationuniform; default_options.CrossoverFcn = @crossoverscattered; default_options.CrossoverFraction = 0.8; #default_options.Display = "final"; #default_options.DistanceMeasureFcn gamultiobj default_options.EliteCount = 2; default_options.FitnessLimit = -Inf; default_options.FitnessScalingFcn = @fitscalingrank; default_options.Generations = 100; #default_options.HybridFcn = []; #default_options.InitialPenalty = 10; default_options.InitialPopulation = []; default_options.InitialScores = []; #default_options.MigrationDirection = "forward"; #default_options.MigrationFraction = 0.2; #default_options.MigrationInterval = 20; default_options.MutationFcn = {@mutationgaussian, 1, 1}; #default_options.OutputFcns = []; #default_options.ParetoFraction = 0.35; #default_options.PenaltyFactor = 100; #default_options.PlotFcns = []; #default_options.PlotInterval = 1; default_options.PopInitRange = [0; 1]; default_options.PopulationSize = 20; #default_options.PopulationType = "doubleVector"; default_options.SelectionFcn = @selectionstochunif; #default_options.StallGenLimit = 50; #default_options.StallTimeLimit = Inf; default_options.TimeLimit = Inf; #default_options.TolCon = 1e-6; #default_options.TolFun = 1e-6; default_options.UseParallel = "never"; default_options.Vectorized = "off"; endfunctionga/inst/crossoverscattered.m000644 000767 000000 00000003220 11737424747 017642 0ustar00slackydebwheel000000 000000 ## Copyright (C) 2008, 2009, 2011 Luca Favatella ## ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with this program; If not, see . ## Author: Luca Favatella ## Version: 7.0 function xoverKids = crossoverscattered (parents, options, nvars, FitnessFcn, unused, thisPopulation) ## simplified example (nvars == 4) ## p1 = [varA varB varC varD] ## p2 = [var1 var2 var3 var4] ## b = [1 1 0 1] ## child = [varA varB var3 varD] nc_parents = columns (parents); n_children = nc_parents / 2; p1(1:n_children, 1:nvars) = \ thisPopulation(parents(1, 1:n_children), 1:nvars); p2(1:n_children, 1:nvars) = \ thisPopulation(parents(1, n_children + (1:n_children)), 1:nvars); b(1:n_children, 1:nvars) = randi (1, n_children, nvars); ## TODO: test randi xoverKids(1:n_children, 1:nvars) = \ b .* p1 + (ones (n_children, nvars) - b) .* p2; endfunction ## number of input arguments # TODO ## number of output arguments # TODO ## type of arguments # TODO # TODO ga/inst/fitscalingrank.m000644 000767 000000 00000003564 11737424747 016730 0ustar00slackydebwheel000000 000000 ## Copyright (C) 2008, 2009 Luca Favatella ## ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with this program; If not, see . ## Author: Luca Favatella ## Version: 1.3.3 function expectation = fitscalingrank (scores, nParents) nr_scores = rows (scores); r(1, 1:nr_scores) = ranks (scores(1:nr_scores, 1)); #TODO #ranks ([7,2,2]) == [3.0,1.5,1.5] #is [3,1,2] (or [3,2,1]) useful? expectation_wo_nParents(1, 1:nr_scores) = arrayfun (@(n) 1 / sqrt (n), r); expectation(1, 1:nr_scores) = \ (nParents / sum (expectation_wo_nParents)) * \ expectation_wo_nParents; endfunction ## number of input arguments # TODO ## number of output arguments # TODO ## type of arguments # TODO # TODO %!shared scores, nParents, expectation %! scores = rand (20, 1); %! nParents = 32; %! expectation = fitscalingrank (scores, nParents); %!assert (sum (expectation), nParents, 1e-9); %!test %! [trash index_min_scores] = min (scores); %! [trash index_max_expectation] = max (expectation); %! assert (index_min_scores, index_max_expectation); %!test %! [trash index_max_scores] = max (scores); %! [trash index_min_expectation] = min (expectation); %! assert (index_max_scores, index_min_expectation);ga/inst/ga.m000644 000767 000000 00000035521 11737424747 014316 0ustar00slackydebwheel000000 000000 ## Copyright (C) 2008, 2010, 2012 Luca Favatella ## ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with this program; If not, see . ## -*- texinfo -*- ## @deftypefn{Function File} {@var{x} =} ga (@var{fitnessfcn}, @var{nvars}) ## @deftypefnx{Function File} {@var{x} =} ga (@var{fitnessfcn}, @var{nvars}, @var{A}, @var{b}) ## @deftypefnx{Function File} {@var{x} =} ga (@var{fitnessfcn}, @var{nvars}, @var{A}, @var{b}, @var{Aeq}, @var{beq}) ## @deftypefnx{Function File} {@var{x} =} ga (@var{fitnessfcn}, @var{nvars}, @var{A}, @var{b}, @var{Aeq}, @var{beq}, @var{LB}, @var{UB}) ## @deftypefnx{Function File} {@var{x} =} ga (@var{fitnessfcn}, @var{nvars}, @var{A}, @var{b}, @var{Aeq}, @var{beq}, @var{LB}, @var{UB}, @var{nonlcon}) ## @deftypefnx{Function File} {@var{x} =} ga (@var{fitnessfcn}, @var{nvars}, @var{A}, @var{b}, @var{Aeq}, @var{beq}, @var{LB}, @var{UB}, @var{nonlcon}, @var{options}) ## @deftypefnx{Function File} {@var{x} =} ga (@var{problem}) ## @deftypefnx{Function File} {[@var{x}, @var{fval}] =} ga (@dots{}) ## @deftypefnx{Function File} {[@var{x}, @var{fval}, @var{exitflag}] =} ga (@dots{}) ## @deftypefnx{Function File} {[@var{x}, @var{fval}, @var{exitflag}, @var{output}] =} ga (@dots{}) ## @deftypefnx{Function File} {[@var{x}, @var{fval}, @var{exitflag}, @var{output}, @var{population}] =} ga (@dots{}) ## @deftypefnx{Function File} {[@var{x}, @var{fval}, @var{exitflag}, @var{output}, @var{population}, @var{scores}] =} ga (@dots{}) ## Find minimum of function using genetic algorithm. ## ## @strong{Inputs} ## @table @var ## @item fitnessfcn ## The objective function to minimize. It accepts a vector @var{x} of ## size 1-by-@var{nvars}, and returns a scalar evaluated at @var{x}. ## @item nvars ## The dimension (number of design variables) of @var{fitnessfcn}. ## @item options ## The structure of the optimization parameters; can be created using ## the @code{gaoptimset} function. If not specified, @code{ga} minimizes ## with the default optimization parameters. ## @item problem ## A structure containing the following fields: ## @itemize @bullet ## @item @code{fitnessfcn} ## @item @code{nvars} ## @item @code{Aineq} ## @item @code{Bineq} ## @item @code{Aeq} ## @item @code{Beq} ## @item @code{lb} ## @item @code{ub} ## @item @code{nonlcon} ## @item @code{randstate} ## @item @code{randnstate} ## @item @code{solver} ## @item @code{options} ## @end itemize ## @end table ## ## @strong{Outputs} ## @table @var ## @item x ## The local unconstrained found minimum to the objective function, ## @var{fitnessfcn}. ## @item fval ## The value of the fitness function at @var{x}. ## @end table ## ## @seealso{gaoptimset} ## @end deftypefn ## Author: Luca Favatella ## Version: 6.0.1 function [x fval exitflag output population scores] = \ ga (fitnessfcn_or_problem, nvars, A = [], b = [], Aeq = [], beq = [], LB = [], UB = [], nonlcon = [], options = gaoptimset ()) if ((nargout > 6) || (nargin < 1) || (nargin == 3) || (nargin == 5) || (nargin == 7) || (nargin > 10)) print_usage (); else ## retrieve the problem structure if (nargin == 1) problem = fitnessfcn_or_problem; else problem.fitnessfcn = fitnessfcn_or_problem; problem.nvars = nvars; problem.Aineq = A; problem.Bineq = b; problem.Aeq = Aeq; problem.Beq = beq; problem.lb = LB; problem.ub = UB; problem.nonlcon = nonlcon; problem.randstate = rand ("state"); problem.randnstate = randn ("state"); problem.solver = "ga"; problem.options = options; endif ## call the function that manages the problem structure [x fval exitflag output population scores] = __ga_problem__ (problem); endif endfunction ## number of input arguments %!shared f, nvars %! f = @rastriginsfcn; %! nvars = 2; %!error x = ga () %!error x = ga (f) %!error x = ga (f, nvars, []) %!error x = ga (f, nvars, [], [], []) %!error x = ga (f, nvars, [], [], [], [], []) %!error x = ga (f, nvars, [], [], [], [], [], [], @(x) [[], []], gaoptimset (), []) ## number of output arguments # TODO ## type of arguments %!function f = ff (nvars) %! f = @(x) sum (x(:, 1:nvars) .** 2, 2); %!error x = ga (ff (3), 2); # TODO # TODO: test that each field in the user-specified "problem" structure is checked ## flawless execution with right arguments %!shared f, nvars %! f = @rastriginsfcn; %! nvars = 2; %!function [C, Ceq] = nonlcon (x) %! C = []; %! Ceq = []; %!test x = ga (f, nvars); %!test x = ga (f, nvars, [], []); %!test x = ga (f, nvars, ones (3, nvars), ones (3, 1)); %!test x = ga (f, nvars, [], [], [], []); %!test x = ga (f, nvars, [], [], ones (4, nvars), ones (4, 1)); %!test x = ga (f, nvars, [], [], [], [], [], []); %!test x = ga (f, nvars, [], [], [], [], - Inf (1, nvars), Inf (1, nvars)); %!test x = ga (f, nvars, [], [], [], [], - ones (1, nvars), ones (1, nvars)); %!test x = ga (f, nvars, [], [], [], [], [], [], @(x) [[], []]); %!test x = ga (f, nvars, [], [], [], [], [], [], @nonlcon); %!test x = ga (f, nvars, [], [], [], [], [], [], @(x) [[], []], gaoptimset ()); %!test # TODO: convert to error after implementing private ga-specific createOptimProblem. All fields in the user-specified structure should be checked %! problem = struct ("fitnessfcn", @rastriginsfcn, %! "nvars", 2, %! "options", gaoptimset ()); %! x = ga (problem); ## flawless execution with any nvars %!function f = ff (nvars) %! f = @(x) sum (x(:, 1:nvars) .** 2, 2); %!test %! nvars = 1; %! x = ga (ff (nvars), nvars); %!test %! nvars = 2; %! x = ga (ff (nvars), nvars); %!test %! nvars = 3; %! x = ga (ff (nvars), nvars); ## flawless execution with any supported optimization parameter ## different from the default value %!shared f, nvars, default_options %! f = @rastriginsfcn; %! nvars = 2; %! default_options = gaoptimset (); %!function [C, Ceq] = nonlcon (x) %! C = []; %! Ceq = []; %!test %! x = ga (f, nvars, [], [], [], [], [], [], @nonlcon, default_options); %!test # TODO: use non-default value %! options = gaoptimset ("CreationFcn", @gacreationuniform); %! x = ga (f, nvars, [], [], [], [], [], [], @nonlcon, options); %!test # TODO: use non-default value %! options = gaoptimset ("CrossoverFcn", @crossoverscattered); %! x = ga (f, nvars, [], [], [], [], [], [], @nonlcon, options); %!test %! options = gaoptimset ("CrossoverFraction", rand); %! x = ga (f, nvars, [], [], [], [], [], [], @nonlcon, options); %!test %! ps = getfield (default_options, "PopulationSize"); %! options = gaoptimset ("EliteCount", randi ([0, ps])); %! x = ga (f, nvars, [], [], [], [], [], [], @nonlcon, options); %!test %! options = gaoptimset ("FitnessLimit", 1e-7); %! x = ga (f, nvars, [], [], [], [], [], [], @nonlcon, options); %!test # TODO: use non-default value %! options = gaoptimset ("FitnessScalingFcn", @fitscalingrank); %! x = ga (f, nvars, [], [], [], [], [], [], @nonlcon, options); %!test %! g = getfield (default_options, "Generations"); %! options = gaoptimset ("Generations", g + 1); %! x = ga (f, nvars, [], [], [], [], [], [], @nonlcon, options); %!test %! ps = getfield (default_options, "PopulationSize"); %! ## Initial population can be partial %! options_w_full_ip = \ %! gaoptimset ("InitialPopulation", rand (ps, nvars)); %! partial_ip = randi ([0, ps - 1]); %! options_w_partial_ip = \ %! gaoptimset ("InitialPopulation", rand (partial_ip, nvars)); %! x = ga (f, nvars, [], [], [], [], [], [], @nonlcon, options_w_full_ip); %! x = ga (f, nvars, [], [], [], [], [], [], @nonlcon, options_w_partial_ip); %!test %! ps = getfield (default_options, "PopulationSize"); %! ## Initial scores needs initial population %! %! options_w_full_ip_full_is = \ %! gaoptimset ("InitialPopulation", rand (ps, nvars), %! "InitialScores", rand (ps, 1 )); %! partial_ip = randi ([2, ps - 1]); %! options_w_partial_ip_full_is = \ %! gaoptimset ("InitialPopulation", rand (partial_ip, nvars), %! "InitialScores", rand (partial_ip, 1 )); %! %! ## Initial scores can be partial %! partial_is_when_full_ip = randi ([1, ps - 1]); %! partial_is_when_partial_ip = randi ([1, partial_ip - 1]); %! options_w_full_ip_partial_is = \ %! gaoptimset ("InitialPopulation", rand (ps, nvars), %! "InitialScores", rand (partial_is_when_full_ip, 1 )); %! options_w_partial_ip_partial_is = \ %! gaoptimset ("InitialPopulation", rand (partial_ip, nvars), %! "InitialScores", rand (partial_is_when_partial_ip, 1 )); %! %! x = ga (f, nvars, [], [], [], [], [], [], @nonlcon, %! options_w_full_ip_full_is); %! x = ga (f, nvars, [], [], [], [], [], [], @nonlcon, %! options_w_partial_ip_full_is); %! x = ga (f, nvars, [], [], [], [], [], [], @nonlcon, %! options_w_full_ip_partial_is); %! x = ga (f, nvars, [], [], [], [], [], [], @nonlcon, %! options_w_partial_ip_partial_is); %!test # TODO: use non-default value %! options = gaoptimset ("MutationFcn", {@mutationgaussian, 1, 1}); %! x = ga (f, nvars, [], [], [], [], [], [], @nonlcon, options); %!test %! options = gaoptimset ("PopInitRange", [-2; 2]); %! x = ga (f, nvars, [], [], [], [], [], [], @nonlcon, options); %!test %! options = gaoptimset ("PopulationSize", 200); %! x = ga (f, nvars, [], [], [], [], [], [], @nonlcon, options); %!test # TODO: use non-default value %! options = gaoptimset ("SelectionFcn", @selectionstochunif); %! x = ga (f, nvars, [], [], [], [], [], [], @nonlcon, options); %!test # TODO: use non-default value %! options = gaoptimset ("TimeLimit", Inf); %! x = ga (f, nvars, [], [], [], [], [], [], @nonlcon, options); %!error # TODO: this should become test %! options = gaoptimset ("UseParallel", "always"); %! x = ga (f, nvars, [], [], [], [], [], [], @nonlcon, options); %!test %! options = gaoptimset ("Vectorized", "on"); %! x = ga (f, nvars, [], [], [], [], [], [], @nonlcon, options); ## error with conflicting optimization parameters: population size et al. %!shared f, nvars %! f = @rastriginsfcn; %! nvars = 2; %!function [C, Ceq] = nonlcon (x) %! C = []; %! Ceq = []; %!error # Elite count cannot be greater than the population size %! ps = 3; %! bad_options = gaoptimset ("PopulationSize", ps, %! "EliteCount", ps + 1); %! x = ga (f, nvars, [], [], [], [], [], [], @nonlcon, bad_options); %!error # The number of individuals in the initial population cannot be greater of the population size %! ps = 3; %! bad_options = gaoptimset ("PopulationSize", ps, %! "InitialPopulation", zeros (ps + 1, nvars)); %! x = ga (f, nvars, [], [], [], [], [], [], @nonlcon, bad_options); %!error # Initial scores cannot be specified without specifying the initial population too %! bad_options = gaoptimset ("InitialScores", zeros (3, 1)); %! x = ga (f, nvars, [], [], [], [], [], [], @nonlcon, bad_options); %!error # The number of initial scores specified cannot be greater of the number of individuals in the initial population %! ip = 3; %! bad_options = gaoptimset ("InitialPopulation", zeros (ip, nvars), %! "InitialScores", zeros (ip + 1, 1)); %! x = ga (f, nvars, [], [], [], [], [], [], @nonlcon, bad_options); ## error with vectorized evaluation of objective function. Vectorized ## objective functions are better because can be evaluated both as ## serial and vectorized. %!shared nvars %! nvars = 2; %!function [C, Ceq] = nonlcon (x) %! C = []; %! Ceq = []; %!function f = ff (nvars) %! f = @(x) sum (x(:, 1:nvars) .** 2, 2); %!function f_not_vectorized = ff_not_vectorized (nvars) %! f_not_vectorized = @(x) sum (x(1:nvars) .** 2); %!test # A non-vectorized objective function works when no vectorization is required %! f = ff_not_vectorized (nvars); %! options = gaoptimset ("Vectorized", "off"); %! x = ga (f, nvars, [], [], [], [], [], [], @nonlcon, options); %!error # A non-vectorized objective function does not work when vectorization is required %! f = ff_not_vectorized (nvars); %! options = gaoptimset ("Vectorized", "on"); %! x = ga (f, nvars, [], [], [], [], [], [], @nonlcon, options); %!test # A vectorized objective function works when no vectorization is required %! f = ff (nvars); %! options = gaoptimset ("Vectorized", "off"); %! x = ga (f, nvars, [], [], [], [], [], [], @nonlcon, options); %!test # A vectorized objective function works when vectorization is required %! f = ff (nvars); %! options = gaoptimset ("Vectorized", "on"); %! x = ga (f, nvars, [], [], [], [], [], [], @nonlcon, options); ## error with conflicting optimization parameters: parallel and ## vectorized evaluation of objective function %!shared f, nvars %! f = @rastriginsfcn; %! nvars = 2; %!function [C, Ceq] = nonlcon (x) %! C = []; %! Ceq = []; %!test %! options = gaoptimset ("UseParallel", "never", %! "Vectorized", "off"); %! x = ga (f, nvars, [], [], [], [], [], [], @nonlcon, options); %!error # TODO: this should become test %! options = gaoptimset ("UseParallel", "always", %! "Vectorized", "off"); %! x = ga (f, nvars, [], [], [], [], [], [], @nonlcon, options); %!error %! bad_options = gaoptimset ("UseParallel", "garbage", %! "Vectorized", "off"); %! x = ga (f, nvars, [], [], [], [], [], [], @nonlcon, bad_options); %!test %! options = gaoptimset ("UseParallel", "never", %! "Vectorized", "on"); %! x = ga (f, nvars, [], [], [], [], [], [], @nonlcon, options); %!warning %! bad_options = gaoptimset ("UseParallel", "always", %! "Vectorized", "on"); %! x = ga (f, nvars, [], [], [], [], [], [], @nonlcon, bad_options); %!warning %! bad_options = gaoptimset ("UseParallel", "garbage", %! "Vectorized", "on"); %! x = ga (f, nvars, [], [], [], [], [], [], @nonlcon, bad_options); %!error %! bad_options = gaoptimset ("UseParallel", "never", %! "Vectorized", "garbage"); %! x = ga (f, nvars, [], [], [], [], [], [], @nonlcon, bad_options); %!error %! bad_options = gaoptimset ("UseParallel", "always", %! "Vectorized", "garbage"); %! x = ga (f, nvars, [], [], [], [], [], [], @nonlcon, bad_options); %!error %! bad_options = gaoptimset ("UseParallel", "garbage", %! "Vectorized", "garbage"); %! x = ga (f, nvars, [], [], [], [], [], [], @nonlcon, bad_options); ga/inst/gacreationuniform.m000644 000767 000000 00000003726 11737424747 017445 0ustar00slackydebwheel000000 000000 ## Copyright (C) 2008 Luca Favatella ## ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with this program; If not, see . ## -*- texinfo -*- ## @deftypefn{Function File} {@var{Population} =} gacreationuniform (@var{GenomeLength}, @var{FitnessFcn}, @var{options}) ## Create a random initial population with a uniform distribution. ## ## @strong{Inputs} ## @table @var ## @item GenomeLength ## The number of indipendent variables for the fitness function. ## @item FitnessFcn ## The fitness function. ## @item options ## The options structure. ## @end table ## ## @strong{Outputs} ## @table @var ## @item Population ## The initial population for the genetic algorithm. ## @end table ## ## @seealso{ga, gaoptimset} ## @end deftypefn ## Author: Luca Favatella ## Version: 4.9 function Population = gacreationuniform (GenomeLength, FitnessFcn, options) [LB(1, 1:GenomeLength) UB(1, 1:GenomeLength)] = \ __ga_popinitrange__ (options.PopInitRange, GenomeLength); ## pseudocode ## ## Population = Delta * RandomPopulationBetween0And1 + Offset Population(1:options.PopulationSize, 1:GenomeLength) = \ ((ones (options.PopulationSize, 1) * (UB - LB)) .* \ rand (options.PopulationSize, GenomeLength)) + \ (ones (options.PopulationSize, 1) * LB); endfunction ## number of input arguments # TODO ## number of output arguments # TODO ## type of arguments # TODO # TODO ga/inst/gaoptimset.m000644 000767 000000 00000007737 11737424747 016113 0ustar00slackydebwheel000000 000000 ## Copyright (C) 2008, 2009, 2010 Luca Favatella ## ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with this program; If not, see . ## -*- texinfo -*- ## @deftypefn{Function File} {@var{options} =} gaoptimset ## @deftypefnx{Function File} {@var{options} =} gaoptimset ('@var{param1}', @var{value1}, '@var{param2}', @var{value2}, @dots{}) ## Create genetic algorithm options structure. ## ## @strong{Inputs} ## @table @var ## @item param ## Parameter to set. Unspecified parameters are set to their default ## values; specifying no parameters is allowed. ## @item value ## Value of @var{param}. ## @end table ## ## @strong{Outputs} ## @table @var ## @item options ## Structure containing the options, or parameters, for the genetic algorithm. ## @end table ## ## @strong{Options} ## @table @code ## @item CreationFcn ## @item CrossoverFcn ## @item CrossoverFraction ## @item EliteCount ## @item FitnessLimit ## @item FitnessScalingFcn ## @item Generations ## @item InitialPopulation ## Can be partial. ## @item InitialScores ## column vector | [] (default) . Can be partial. ## @item MutationFcn ## @item PopInitRange ## @item PopulationSize ## @item SelectionFcn ## @item TimeLimit ## @item UseParallel ## "always" | "never" (default) . Parallel evaluation of objective function. TODO: parallel evaluation of nonlinear constraints ## @item Vectorized ## "on" | "off" (default) . Vectorized evaluation of objective function. TODO: vectorized evaluation of nonlinear constraints ## @end table ## ## @seealso{ga} ## @end deftypefn ## Author: Luca Favatella ## Version: 4.4.7 function options = gaoptimset (varargin) if ((nargout != 1) || (mod (length (varargin), 2) == 1)) print_usage (); else ## initialize the return variable to a structure with all parameters ## set to their default value options = __gaoptimset_default_options__ (); ## set custom options for i = 1:2:length (varargin) param = varargin{i}; value = varargin{i + 1}; if (! isfield (options, param)) error ("wrong parameter"); else options = setfield (options, param, value); endif endfor endif endfunction ## number of input arguments %!error options = gaoptimset ("odd number of arguments") %!error options = gaoptimset ("Generations", 123, "odd number of arguments") ## number of output arguments %!error gaoptimset ("Generations", 123) %!error [a, b] = gaoptimset ("Generations", 123) ## type of arguments # TODO %!#error options = gaoptimset ("Vectorized", "bad value") # TODO: fix %!#error options = gaoptimset ("UseParallel", "bad value") # TODO: fix # TODO: structure/add tests below %!assert (getfield (gaoptimset ("Generations", 123), "Generations"), 123) %!test %! options = gaoptimset ("EliteCount", 1, %! "FitnessLimit", 1e-7, %! "Generations", 1000, %! "PopInitRange", [-5; 5], %! "PopulationSize", 200); %! %! ## "CrossoverFraction" is not specified, so gaoptimset should put the default value: testing this too %! assert ([(getfield (options, "CrossoverFraction")); %! (getfield (options, "EliteCount")); %! (getfield (options, "FitnessLimit")); %! (getfield (options, "Generations")); %! (getfield (options, "PopInitRange")); %! (getfield (options, "PopulationSize"))], %! [0.8; 1; 1e-7; 1000; [-5; 5]; 200]) ga/inst/mutationgaussian.m000644 000767 000000 00000003741 11737424747 017321 0ustar00slackydebwheel000000 000000 ## Copyright (C) 2008 Luca Favatella ## ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with this program; If not, see . ## Author: Luca Favatella ## Version: 1.7.1 function mutationChildren = \ mutationgaussian (parents, options, nvars, FitnessFcn, state, thisScore, thisPopulation) [LB(1, 1:nvars) UB(1, 1:nvars)] = \ __ga_popinitrange__ (options.PopInitRange, nvars); ## start mutationgaussian logic Scale = options.MutationFcn{1, 2}; #assert (size (Scale), [1 1]); ## DEBUG Shrink = options.MutationFcn{1, 3}; #assert (size (Shrink), [1 1]); ## DEBUG ## initial standard deviation (i.e. when state.Generation == 0) tmp_std = Scale * (UB - LB); ## vector = scalar * vector ## recursively compute current standard deviation for k = 1:state.Generation tmp_std(1, 1:nvars) = (1 - Shrink * (k / options.Generations)) * tmp_std; endfor current_std(1, 1:nvars) = tmp_std; nc_parents = columns (parents); expanded_current_std(1:nc_parents, 1:nvars) = \ ones (nc_parents, 1) * current_std; ## finally add random numbers mutationChildren(1:nc_parents, 1:nvars) = \ thisPopulation(parents(1, 1:nc_parents), 1:nvars) + \ expanded_current_std .* randn (nc_parents, nvars); endfunction ## number of input arguments # TODO ## number of output arguments # TODO ## type of arguments # TODO # TODO ga/inst/rastriginsfcn.m000644 000767 000000 00000003243 11737424747 016577 0ustar00slackydebwheel000000 000000 ## Copyright (C) 2008, 2009, 2010, 2012 Luca Favatella ## ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with this program; If not, see . ## -*- texinfo -*- ## @deftypefn{Function File} {@var{y} =} rastriginsfcn (@var{x}) ## Rastrigin's function. ## @end deftypefn ## Author: Luca Favatella ## Version: 2.0 function retval = rastriginsfcn (x) if ((nargout != 1) || (nargin != 1) || (columns (x) != 2)) print_usage (); else x1 = x(:, 1); x2 = x(:, 2); retval = 20 + (x1 .** 2) + (x2 .** 2) - 10 .* (cos (2 .* pi .* x1) + cos (2 .* pi .* x2)); endif endfunction ## number of input arguments %!error y = rastriginsfcn () %!error y = rastriginsfcn ([0, 0], "other argument") ## number of output arguments %!error [y1, y2] = rastriginsfcn ([0, 0]) ## type of arguments %!error y = rastriginsfcn ([0; 0]) %!error y = rastriginsfcn (zeros (2, 3)) # TODO: document size of x %!assert (rastriginsfcn ([0, 0]), 0) %!assert (rastriginsfcn ([0, 0; 0, 0]), [0; 0]) %!assert (rastriginsfcn (zeros (3, 2)), [0; 0; 0]) ga/inst/selectionstochunif.m000644 000767 000000 00000003122 11737424747 017627 0ustar00slackydebwheel000000 000000 ## Copyright (C) 2008 Luca Favatella ## ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with this program; If not, see . ## Author: Luca Favatella ## Version: 1.3 function parents = selectionstochunif (expectation, nParents, options) nc_expectation = columns (expectation); line(1, 1:nc_expectation) = cumsum (expectation(1, 1:nc_expectation)); max_step_size = line(1, nc_expectation); step_size = max_step_size * rand (); steps(1, 1:nParents) = rem (step_size * (1:nParents), max_step_size); for index_steps = 1:nParents ## fix an entry of the steps (or parents) vector #assert (steps(1, index_steps) < max_step_size); ## DEBUG index_line = 1; while (steps(1, index_steps) >= line(1, index_line)) #assert ((index_line >= 1) && (index_line < nc_expectation)); ## DEBUG index_line++; endwhile parents(1, index_steps) = index_line; endfor endfunction ## number of input arguments # TODO ## number of output arguments # TODO ## type of arguments # TODO # TODO ga/inst/test_ga.m000644 000767 000000 00000002400 11737424747 015343 0ustar00slackydebwheel000000 000000 ## Copyright (C) 2012 Luca Favatella ## ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with this program; If not, see . ## -*- texinfo -*- ## @deftypefn{Script File} {} test_ga ## Execute all available tests at once. ## @end deftypefn ## Author: Luca Favatella ## Created: March 2012 ## Version: 0.3 ## Creation test gacreationuniform ## Fitness Scaling test fitscalingrank ## Selection test selectionstochunif ## Crossover test crossoverscattered ## Mutation test mutationgaussian ## Utility test rastriginsfcn ## Genetic Algorithm test gaoptimset test ga ## Private functions test __ga_initial_population__ test __ga_problem_update_state_at_each_generation__ ga/doc/README000644 000767 000000 00000000461 11737424747 014214 0ustar00slackydebwheel000000 000000 STATUS (BY EXAMPLES) See tests of the ga function. TODO create a demo write a proper README linear constraints (needs mutationadaptfeasible and perhaps LMI) nonlinear constraints subpopulations and migrations bitstring support plot functions more texinfo handle TODOs in the code integer constraints ga/DESCRIPTION0000644000175000017500000000046411737651143015467 0ustar00carandraugcarandraug00000000000000Name: ga Version: 0.10.0 Date: 2012-04-06 Author: Luca Favatella Maintainer: Luca Favatella Title: Genetic Algorithm Description: Genetic optimization code Categories: Optimization Depends: octave (>= 3.4.0) Autoload: yes License: GPLv2+ Url: http://octave.sf.net