├── DOC
└── GAToolbox Documentation.pdf
├── LICENSE
├── README.md
├── Test_fns
├── TEST_FNS.PS
├── demoga1.m
├── mpga.m
├── objbran.m
├── objdopi.m
├── objeaso.m
├── objfun1.m
├── objfun1a.m
├── objfun1b.m
├── objfun2.m
├── objfun6.m
├── objfun7.m
├── objfun8.m
├── objfun9.m
├── objgold.m
├── objharv.m
├── objlinq.m
├── objlinq2.m
├── objpush.m
├── objsixh.m
├── resplot.m
├── sga.m
├── simdopi1.m
├── simdopi2.m
├── simlinq1.m
├── simlinq2.m
└── simobjp.m
├── bs2rv.m
├── contents.m
├── crtbase.m
├── crtbp.m
├── crtrp.m
├── migrate.m
├── mpga.m
├── mut.m
├── mutate.m
├── mutbga.m
├── objfun1.m
├── objharv.m
├── ranking.m
├── recdis.m
├── recint.m
├── reclin.m
├── recmut.m
├── recombin.m
├── reins.m
├── rep.m
├── resplot.m
├── rws.m
├── scaling.m
├── select.m
├── sga.m
├── sus.m
├── xovdp.m
├── xovdprs.m
├── xovmp.m
├── xovsh.m
├── xovshrs.m
├── xovsp.m
└── xovsprs.m
/DOC/GAToolbox Documentation.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/UoS-CODeM/GA-Toolbox/8ca5c5cf806bea948f062832eb52ebf71d9afb7d/DOC/GAToolbox Documentation.pdf
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 2, June 1991
3 |
4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
6 | Everyone is permitted to copy and distribute verbatim copies
7 | of this license document, but changing it is not allowed.
8 |
9 | Preamble
10 |
11 | The licenses for most software are designed to take away your
12 | freedom to share and change it. By contrast, the GNU General Public
13 | License is intended to guarantee your freedom to share and change free
14 | software--to make sure the software is free for all its users. This
15 | General Public License applies to most of the Free Software
16 | Foundation's software and to any other program whose authors commit to
17 | using it. (Some other Free Software Foundation software is covered by
18 | the GNU Lesser General Public License instead.) You can apply it to
19 | your programs, too.
20 |
21 | When we speak of free software, we are referring to freedom, not
22 | price. Our General Public Licenses are designed to make sure that you
23 | have the freedom to distribute copies of free software (and charge for
24 | this service if you wish), that you receive source code or can get it
25 | if you want it, that you can change the software or use pieces of it
26 | in new free programs; and that you know you can do these things.
27 |
28 | To protect your rights, we need to make restrictions that forbid
29 | anyone to deny you these rights or to ask you to surrender the rights.
30 | These restrictions translate to certain responsibilities for you if you
31 | distribute copies of the software, or if you modify it.
32 |
33 | For example, if you distribute copies of such a program, whether
34 | gratis or for a fee, you must give the recipients all the rights that
35 | you have. You must make sure that they, too, receive or can get the
36 | source code. And you must show them these terms so they know their
37 | rights.
38 |
39 | We protect your rights with two steps: (1) copyright the software, and
40 | (2) offer you this license which gives you legal permission to copy,
41 | distribute and/or modify the software.
42 |
43 | Also, for each author's protection and ours, we want to make certain
44 | that everyone understands that there is no warranty for this free
45 | software. If the software is modified by someone else and passed on, we
46 | want its recipients to know that what they have is not the original, so
47 | that any problems introduced by others will not reflect on the original
48 | authors' reputations.
49 |
50 | Finally, any free program is threatened constantly by software
51 | patents. We wish to avoid the danger that redistributors of a free
52 | program will individually obtain patent licenses, in effect making the
53 | program proprietary. To prevent this, we have made it clear that any
54 | patent must be licensed for everyone's free use or not licensed at all.
55 |
56 | The precise terms and conditions for copying, distribution and
57 | modification follow.
58 |
59 | GNU GENERAL PUBLIC LICENSE
60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
61 |
62 | 0. This License applies to any program or other work which contains
63 | a notice placed by the copyright holder saying it may be distributed
64 | under the terms of this General Public License. The "Program", below,
65 | refers to any such program or work, and a "work based on the Program"
66 | means either the Program or any derivative work under copyright law:
67 | that is to say, a work containing the Program or a portion of it,
68 | either verbatim or with modifications and/or translated into another
69 | language. (Hereinafter, translation is included without limitation in
70 | the term "modification".) Each licensee is addressed as "you".
71 |
72 | Activities other than copying, distribution and modification are not
73 | covered by this License; they are outside its scope. The act of
74 | running the Program is not restricted, and the output from the Program
75 | is covered only if its contents constitute a work based on the
76 | Program (independent of having been made by running the Program).
77 | Whether that is true depends on what the Program does.
78 |
79 | 1. You may copy and distribute verbatim copies of the Program's
80 | source code as you receive it, in any medium, provided that you
81 | conspicuously and appropriately publish on each copy an appropriate
82 | copyright notice and disclaimer of warranty; keep intact all the
83 | notices that refer to this License and to the absence of any warranty;
84 | and give any other recipients of the Program a copy of this License
85 | along with the Program.
86 |
87 | You may charge a fee for the physical act of transferring a copy, and
88 | you may at your option offer warranty protection in exchange for a fee.
89 |
90 | 2. You may modify your copy or copies of the Program or any portion
91 | of it, thus forming a work based on the Program, and copy and
92 | distribute such modifications or work under the terms of Section 1
93 | above, provided that you also meet all of these conditions:
94 |
95 | a) You must cause the modified files to carry prominent notices
96 | stating that you changed the files and the date of any change.
97 |
98 | b) You must cause any work that you distribute or publish, that in
99 | whole or in part contains or is derived from the Program or any
100 | part thereof, to be licensed as a whole at no charge to all third
101 | parties under the terms of this License.
102 |
103 | c) If the modified program normally reads commands interactively
104 | when run, you must cause it, when started running for such
105 | interactive use in the most ordinary way, to print or display an
106 | announcement including an appropriate copyright notice and a
107 | notice that there is no warranty (or else, saying that you provide
108 | a warranty) and that users may redistribute the program under
109 | these conditions, and telling the user how to view a copy of this
110 | License. (Exception: if the Program itself is interactive but
111 | does not normally print such an announcement, your work based on
112 | the Program is not required to print an announcement.)
113 |
114 | These requirements apply to the modified work as a whole. If
115 | identifiable sections of that work are not derived from the Program,
116 | and can be reasonably considered independent and separate works in
117 | themselves, then this License, and its terms, do not apply to those
118 | sections when you distribute them as separate works. But when you
119 | distribute the same sections as part of a whole which is a work based
120 | on the Program, the distribution of the whole must be on the terms of
121 | this License, whose permissions for other licensees extend to the
122 | entire whole, and thus to each and every part regardless of who wrote it.
123 |
124 | Thus, it is not the intent of this section to claim rights or contest
125 | your rights to work written entirely by you; rather, the intent is to
126 | exercise the right to control the distribution of derivative or
127 | collective works based on the Program.
128 |
129 | In addition, mere aggregation of another work not based on the Program
130 | with the Program (or with a work based on the Program) on a volume of
131 | a storage or distribution medium does not bring the other work under
132 | the scope of this License.
133 |
134 | 3. You may copy and distribute the Program (or a work based on it,
135 | under Section 2) in object code or executable form under the terms of
136 | Sections 1 and 2 above provided that you also do one of the following:
137 |
138 | a) Accompany it with the complete corresponding machine-readable
139 | source code, which must be distributed under the terms of Sections
140 | 1 and 2 above on a medium customarily used for software interchange; or,
141 |
142 | b) Accompany it with a written offer, valid for at least three
143 | years, to give any third party, for a charge no more than your
144 | cost of physically performing source distribution, a complete
145 | machine-readable copy of the corresponding source code, to be
146 | distributed under the terms of Sections 1 and 2 above on a medium
147 | customarily used for software interchange; or,
148 |
149 | c) Accompany it with the information you received as to the offer
150 | to distribute corresponding source code. (This alternative is
151 | allowed only for noncommercial distribution and only if you
152 | received the program in object code or executable form with such
153 | an offer, in accord with Subsection b above.)
154 |
155 | The source code for a work means the preferred form of the work for
156 | making modifications to it. For an executable work, complete source
157 | code means all the source code for all modules it contains, plus any
158 | associated interface definition files, plus the scripts used to
159 | control compilation and installation of the executable. However, as a
160 | special exception, the source code distributed need not include
161 | anything that is normally distributed (in either source or binary
162 | form) with the major components (compiler, kernel, and so on) of the
163 | operating system on which the executable runs, unless that component
164 | itself accompanies the executable.
165 |
166 | If distribution of executable or object code is made by offering
167 | access to copy from a designated place, then offering equivalent
168 | access to copy the source code from the same place counts as
169 | distribution of the source code, even though third parties are not
170 | compelled to copy the source along with the object code.
171 |
172 | 4. You may not copy, modify, sublicense, or distribute the Program
173 | except as expressly provided under this License. Any attempt
174 | otherwise to copy, modify, sublicense or distribute the Program is
175 | void, and will automatically terminate your rights under this License.
176 | However, parties who have received copies, or rights, from you under
177 | this License will not have their licenses terminated so long as such
178 | parties remain in full compliance.
179 |
180 | 5. You are not required to accept this License, since you have not
181 | signed it. However, nothing else grants you permission to modify or
182 | distribute the Program or its derivative works. These actions are
183 | prohibited by law if you do not accept this License. Therefore, by
184 | modifying or distributing the Program (or any work based on the
185 | Program), you indicate your acceptance of this License to do so, and
186 | all its terms and conditions for copying, distributing or modifying
187 | the Program or works based on it.
188 |
189 | 6. Each time you redistribute the Program (or any work based on the
190 | Program), the recipient automatically receives a license from the
191 | original licensor to copy, distribute or modify the Program subject to
192 | these terms and conditions. You may not impose any further
193 | restrictions on the recipients' exercise of the rights granted herein.
194 | You are not responsible for enforcing compliance by third parties to
195 | this License.
196 |
197 | 7. If, as a consequence of a court judgment or allegation of patent
198 | infringement or for any other reason (not limited to patent issues),
199 | conditions are imposed on you (whether by court order, agreement or
200 | otherwise) that contradict the conditions of this License, they do not
201 | excuse you from the conditions of this License. If you cannot
202 | distribute so as to satisfy simultaneously your obligations under this
203 | License and any other pertinent obligations, then as a consequence you
204 | may not distribute the Program at all. For example, if a patent
205 | license would not permit royalty-free redistribution of the Program by
206 | all those who receive copies directly or indirectly through you, then
207 | the only way you could satisfy both it and this License would be to
208 | refrain entirely from distribution of the Program.
209 |
210 | If any portion of this section is held invalid or unenforceable under
211 | any particular circumstance, the balance of the section is intended to
212 | apply and the section as a whole is intended to apply in other
213 | circumstances.
214 |
215 | It is not the purpose of this section to induce you to infringe any
216 | patents or other property right claims or to contest validity of any
217 | such claims; this section has the sole purpose of protecting the
218 | integrity of the free software distribution system, which is
219 | implemented by public license practices. Many people have made
220 | generous contributions to the wide range of software distributed
221 | through that system in reliance on consistent application of that
222 | system; it is up to the author/donor to decide if he or she is willing
223 | to distribute software through any other system and a licensee cannot
224 | impose that choice.
225 |
226 | This section is intended to make thoroughly clear what is believed to
227 | be a consequence of the rest of this License.
228 |
229 | 8. If the distribution and/or use of the Program is restricted in
230 | certain countries either by patents or by copyrighted interfaces, the
231 | original copyright holder who places the Program under this License
232 | may add an explicit geographical distribution limitation excluding
233 | those countries, so that distribution is permitted only in or among
234 | countries not thus excluded. In such case, this License incorporates
235 | the limitation as if written in the body of this License.
236 |
237 | 9. The Free Software Foundation may publish revised and/or new versions
238 | of the General Public License from time to time. Such new versions will
239 | be similar in spirit to the present version, but may differ in detail to
240 | address new problems or concerns.
241 |
242 | Each version is given a distinguishing version number. If the Program
243 | specifies a version number of this License which applies to it and "any
244 | later version", you have the option of following the terms and conditions
245 | either of that version or of any later version published by the Free
246 | Software Foundation. If the Program does not specify a version number of
247 | this License, you may choose any version ever published by the Free Software
248 | Foundation.
249 |
250 | 10. If you wish to incorporate parts of the Program into other free
251 | programs whose distribution conditions are different, write to the author
252 | to ask for permission. For software which is copyrighted by the Free
253 | Software Foundation, write to the Free Software Foundation; we sometimes
254 | make exceptions for this. Our decision will be guided by the two goals
255 | of preserving the free status of all derivatives of our free software and
256 | of promoting the sharing and reuse of software generally.
257 |
258 | NO WARRANTY
259 |
260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
268 | REPAIR OR CORRECTION.
269 |
270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
278 | POSSIBILITY OF SUCH DAMAGES.
279 |
280 | END OF TERMS AND CONDITIONS
281 |
282 | How to Apply These Terms to Your New Programs
283 |
284 | If you develop a new program, and you want it to be of the greatest
285 | possible use to the public, the best way to achieve this is to make it
286 | free software which everyone can redistribute and change under these terms.
287 |
288 | To do so, attach the following notices to the program. It is safest
289 | to attach them to the start of each source file to most effectively
290 | convey the exclusion of warranty; and each file should have at least
291 | the "copyright" line and a pointer to where the full notice is found.
292 |
293 | {description}
294 | Copyright (C) {year} {fullname}
295 |
296 | This program is free software; you can redistribute it and/or modify
297 | it under the terms of the GNU General Public License as published by
298 | the Free Software Foundation; either version 2 of the License, or
299 | (at your option) any later version.
300 |
301 | This program is distributed in the hope that it will be useful,
302 | but WITHOUT ANY WARRANTY; without even the implied warranty of
303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
304 | GNU General Public License for more details.
305 |
306 | You should have received a copy of the GNU General Public License along
307 | with this program; if not, write to the Free Software Foundation, Inc.,
308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
309 |
310 | Also add information on how to contact you by electronic and paper mail.
311 |
312 | If the program is interactive, make it output a short notice like this
313 | when it starts in an interactive mode:
314 |
315 | Gnomovision version 69, Copyright (C) year name of author
316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
317 | This is free software, and you are welcome to redistribute it
318 | under certain conditions; type `show c' for details.
319 |
320 | The hypothetical commands `show w' and `show c' should show the appropriate
321 | parts of the General Public License. Of course, the commands you use may
322 | be called something other than `show w' and `show c'; they could even be
323 | mouse-clicks or menu items--whatever suits your program.
324 |
325 | You should also get your employer (if you work as a programmer) or your
326 | school, if any, to sign a "copyright disclaimer" for the program, if
327 | necessary. Here is a sample; alter the names:
328 |
329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program
330 | `Gnomovision' (which makes passes at compilers) written by James Hacker.
331 |
332 | {signature of Ty Coon}, 1 April 1989
333 | Ty Coon, President of Vice
334 |
335 | This General Public License does not permit incorporating your program into
336 | proprietary programs. If your program is a subroutine library, you may
337 | consider it more useful to permit linking proprietary applications with the
338 | library. If this is what you want to do, use the GNU Lesser General
339 | Public License instead of this License.
340 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ----------------------------------------------------------------------
2 |
3 | Genetic Algorithm Toolbox for MATLAB, v1.2
4 | ==========================================
5 |
6 | Thank you for requesting a copy of the Genetic Algorithm Toolbox.
7 |
8 | The Genetic Algorithm Toolbox for MATLAB was developed at the
9 | Department of Automatic Control and Systems Engineering of The
10 | University of Sheffield, UK, in order to make GA's accessible to the
11 | control engineer within the framework of a existing computer-aided
12 | control system design package. The toolbox was written with the
13 | support of a UK SERC grant, and the final version (v1.2) was
14 | completed in 1994.
15 |
16 | The Toolbox was originally developed for MATLAB v4.2. It has also
17 | been successfully used with subsequent versions up to and including
18 | MATLAB 7.
19 |
20 | For a more detailed introduction to the capabilities and use of the
21 | GA Toolbox, please refer to the two introductory papers and the
22 | Toolbox User's Guide, all of which are available at the GA Toolbox
23 | homepage at http://codem.group.shef.ac.uk/index.php/ga-toolbox.
24 |
25 | The GA Toolbox is copyright the original authors and The University
26 | of Sheffield, and is published here under the GNU General Public
27 | License. (See http://www.fsf.org/licenses/licenses.html)
28 |
29 | We would be interested to hear of your experiences with, criticisms
30 | of, and enhancements to, the GA Toolbox. Please direct all such
31 | correspondence to ga-toolbox@acse.sheffield.ac.uk.
32 |
33 | ----------------------------------------------------------------------
34 |
--------------------------------------------------------------------------------
/Test_fns/demoga1.m:
--------------------------------------------------------------------------------
1 | % DEMOGA1.M (DEMO of Genetic Algorithms 1)
2 | %
3 | % This function displays a number of figures. Select a number of these
4 | % figures with the mouse by clicking in the area of the shape. These
5 | % figures are the parents for the next generation. The offspring are
6 | % created by recombination and mutation. The user is the selector.
7 | %
8 | % The shape of the figures is defined by Points X- and Y-values each.
9 | % These points are ploted by patch(), each figure in a rectangle.
10 | % The X- and Y-values are the variables of a individual.
11 | %
12 | % Aim of this demo is, to "construct" a special figure, e.g. a square
13 | % or a star or ..., imagine, what you want and select appropriate points.
14 | %
15 | % Parameters inside function for changing:
16 | % Num - Number of figures (4, 9, 16, 25, ...)
17 | % Points - Number of Points per figure (3, 4, 5...), 5 recommended
18 | % MAXGEN - Number of generations (more than 30 recommended)
19 | %
20 | % Syntax: demoga1();
21 | %
22 | % Input parameters:
23 | % no input parameter
24 | % Output parameter:
25 | % no output parameter
26 | %
27 | % Author: Hartmut Pohlheim
28 | % History: 24.03.94 file created
29 | % 13.01.03 tested under MATLAB v6 by Alex Shenfield
30 |
31 | function demoga1();
32 |
33 | Num = 16; % Number of figures
34 | Points = 6; % Number of points per figure
35 | MAXGEN = 30; % Number of generations
36 | FieldDR = rep([0; 1], [1 Points]); % Fielddescription for mutation
37 |
38 | % Function needs as many rows as columns, every place has to be filled
39 | if sqrt(Num) ~= ceil(sqrt(Num)),
40 | error('sqrt(Num) must be an integer');
41 | end
42 |
43 | ColRowNum = ceil(sqrt(Num)); % Number of columns and rows
44 | Shrink=Num/ColRowNum; % Value for shrinking of area
45 | SelectNumber = ColRowNum; % Number of parents by selection
46 |
47 | Cols = 1:1:ColRowNum; % Index of individuals in first columns
48 | Rows = 1:ColRowNum:Num; % Index of individuals in first row
49 |
50 | h = figure; % Open a new figure for output
51 | axes('Position',[0 0 1 1]); % Set axes to whole figure
52 | set(gca, 'xcolor',[0 0 0]); % Make X-Axis invisible
53 | set(gca, 'ycolor',[0 0 0]); % Make Y-Axis invisible
54 | axis('ij'); % Position start in left-upper corner
55 | axis(axis); % Freeze axes
56 |
57 | XStart = (Cols(1:ColRowNum-1)/Shrink); % Calculate X-start- and Y-endvalues of lines
58 | XLine1 = [XStart; XStart]; % between figures
59 | YLine1 = [zeros(1,ColRowNum-1); ones(1,ColRowNum-1)]; % X-end- and Y-startvalues
60 | XLine = [XLine1 YLine1]; YLine = [YLine1 XLine1]; % Assemble line vectors
61 |
62 | MatX=rand(Points,Num); % Create X-values of figures at random
63 | MatY=rand(Points,Num); % Create Y-values of figures at random
64 |
65 | for igen = 1:MAXGEN, % Loop over all generations
66 |
67 | % Recombine individuals
68 | MatXYOff = recombin('recint', [MatX MatY]',NaN, 2);
69 |
70 | % Mutate individuals
71 | MatXYOff = mutbga(MatXYOff, FieldDR, 1/Points);
72 |
73 | MatXYOff = MatXYOff'; % Invert matrix of individuals
74 | MatX = MatXYOff(:,1:Num); % Select X- and Y-values
75 | MatY = MatXYOff(:,size(MatXYOff,2)/2+1:size(MatXYOff,2)/2+Num);
76 |
77 | PolyMatX = MatX / Shrink; % Shrink X-values for fitting in small area
78 | PolyMatY = MatY / Shrink; % Shrink Y-values for fitting in small area
79 |
80 | % Add a value to the X-value for placing individual in the appropriate column
81 | for irun = 1:ColRowNum-1,
82 | PolyMatX(:,Rows+irun)=PolyMatX(:,Rows+irun)+(irun)/Shrink;
83 | end
84 |
85 | % Add a value to the Y-value for placing individual in the appropriate row
86 | for irun = 1:ColRowNum-1,
87 | PolyMatY(:,Cols+(irun*ColRowNum))= ...
88 | PolyMatY(:,Cols+(irun*ColRowNum))+(irun)/Shrink;
89 | end
90 |
91 | cla; % Clear axes, removes all earlier figures
92 | patch(PolyMatX,PolyMatY,'b'); % Plot shape of figures
93 | lh = line(XLine, YLine); % Plot lines between figures
94 | set(lh,'Color',[.6 .6 .6]); % Set linecolor to grey
95 |
96 | xclick = []; yclick = []; % Reset vectors for storing click points
97 | for isel = 1:SelectNumber, % Loop for as many points as individuals to select
98 | set(gcf,'Name',[ ' Select ' int2str(isel) '. figure (of ' ...
99 | int2str(SelectNumber) ') with mouseclick (Generation ' ...
100 | int2str(igen) ' of ' int2str(MAXGEN) ')']);
101 | [xclick1, yclick1] = ginput(1); % get position of mouse click
102 | xclick = [xclick; xclick1]; yclick = [yclick; yclick1]; % add position to vectors
103 | end
104 |
105 | xpos = ceil(xclick * Shrink); % Calculate column of mouse click
106 | ypos = ceil(yclick * Shrink); % Calculate row of mouse click
107 |
108 | SelNumber = xpos + ColRowNum * (ypos-1); % Calculate numbers of selected figures
109 |
110 | % select X- and Y-values of individuals and repeat them
111 | MatX = rep(MatX(:,SelNumber),[1 ceil(Num/SelectNumber)]);
112 | MatY = rep(MatY(:,SelNumber),[1 ceil(Num/SelectNumber)]);
113 |
114 | end
115 |
116 |
117 | % End of function
--------------------------------------------------------------------------------
/Test_fns/mpga.m:
--------------------------------------------------------------------------------
1 | % MPGA.M (Multi Population Genetic Algorithm)
2 | %
3 | % This script implements the Multi Population Genetic Algorithm.
4 | % Real valued representation for the individuals is used.
5 | %
6 | % Author: Hartmut Pohlheim
7 | % History: 23.03.94 file created
8 | % 13.01.03 tested under MATLAB v6 by Alex Shenfield
9 |
10 | GGAP = .8; % Generation gap, how many new individuals are created
11 | INSR = .9; % Insertion rate, how many of the offspring are inserted
12 | XOVR = 1; % Crossover rate
13 | SP = 2; % Selective Pressure
14 | MUTR = 1; % Mutation rate; only a factor;
15 | MIGR = 0.2; % Migration rate between subpopulations
16 | MIGGEN = 20; % Number of generations between migration (isolation time)
17 |
18 | TERMEXACT = 1e-4; % Value for termination if minimum reached
19 |
20 | SEL_F = 'sus'; % Name of selection function
21 | XOV_F = 'recdis'; % Name of recombination function for individuals
22 | MUT_F = 'mutbga'; % Name of mutation function
23 | OBJ_F = 'objharv'; % Name of function for objective values
24 |
25 | % Get boundaries of objective function
26 | FieldDR = feval(OBJ_F,[],1);
27 |
28 | % compute SUBPOP, NIND depending on number of variables (defined in objective function)
29 | NVAR = size(FieldDR,2); % Get number of variables from objective function
30 | SUBPOP = 2 * floor(sqrt(NVAR)); % Number of subpopulations
31 | NIND = 20 + 5 * floor(NVAR/50); % Number of individuals per subpopulations
32 | MAXGEN = 300 * floor(sqrt(NVAR)); % Max number of generations
33 | MUTR = MUTR / NVAR; % Mutation rate depending on NVAR
34 |
35 | % Get value of minimum, defined in objective function
36 | GlobalMin = feval(OBJ_F,[],3);
37 |
38 | % Get title of objective function, defined in objective function
39 | FigTitle = [feval(OBJ_F,[],2) ' (' int2str(SUBPOP) ':' int2str(MAXGEN) ') '];
40 |
41 | % Clear Best and storing matrix
42 | % Initialise Matrix for storing best results
43 | Best = NaN * ones(MAXGEN,3);
44 | Best(:,3) = zeros(size(Best,1),1);
45 | % Matrix for storing best individuals
46 | IndAll = [];
47 |
48 | % Create real population
49 | Chrom = crtrp(SUBPOP*NIND,FieldDR);
50 |
51 | % reset count variables
52 | gen = 0;
53 | termopt = 0;
54 |
55 | % Calculate objective function for population
56 | ObjV = feval(OBJ_F,Chrom);
57 | % count number of objective function evaluations
58 | Best(gen+1,3) = Best(gen+1,3) + NIND;
59 |
60 | % Iterate subpopulation till termination or MAXGEN
61 | while ((gen < MAXGEN) & (termopt == 0)),
62 |
63 | % Save the best and average objective values and the best individual
64 | [Best(gen+1,1),ix] = min(ObjV);
65 | Best(gen+1,2) = mean(ObjV);
66 | IndAll = [IndAll; Chrom(ix,:)];
67 |
68 | % Fitness assignment to whole population
69 | FitnV = ranking(ObjV,[2 0],SUBPOP);
70 |
71 | % Select individuals from population
72 | SelCh = select(SEL_F, Chrom, FitnV, GGAP, SUBPOP);
73 |
74 | % Recombine selected individuals
75 | SelCh=recombin(XOV_F, SelCh, XOVR, SUBPOP);
76 |
77 | % Mutate offspring
78 | SelCh=mutate(MUT_F, SelCh, FieldDR, [MUTR], SUBPOP);
79 |
80 | % Calculate objective function for offspring
81 | ObjVOff = feval(OBJ_F,SelCh);
82 | Best(gen+1,3) = Best(gen+1,3) + size(SelCh,1);
83 |
84 | % Insert best offspring in population replacing worst parents
85 | [Chrom, ObjV] = reins(Chrom, SelCh, SUBPOP, [1 INSR], ObjV, ObjVOff);
86 |
87 | gen=gen+1;
88 |
89 | % Plot some results, rename title of figure for graphic output
90 | if ((rem(gen,20) == 1) | (rem(gen,MAXGEN) == 0) | (termopt == 1)),
91 | set(gcf,'Name',[FigTitle ' in ' int2str(gen)]);
92 | resplot(Chrom(1:2:size(Chrom,1),:),...
93 | IndAll(max(1,gen-39):size(IndAll,1),:),...
94 | [ObjV; GlobalMin], Best(max(1,gen-19):gen,[1 2]), gen);
95 | end
96 |
97 | % Check, if best objective value near GlobalMin -> termination criterion
98 | % compute difference between GlobalMin and best objective value
99 | ActualMin = abs(min(ObjV) - GlobalMin);
100 | % if ActualMin smaller than TERMEXACT --> termination
101 | if ((ActualMin < (TERMEXACT * abs(GlobalMin))) | (ActualMin < TERMEXACT))
102 | termopt = 1;
103 | end
104 |
105 | % migrate individuals between subpopulations
106 | if ((termopt ~= 1) & (rem(gen,MIGGEN) == 0))
107 | [Chrom, ObjV] = migrate(Chrom, SUBPOP, [MIGR, 1, 0], ObjV);
108 | end
109 |
110 | end
111 |
112 |
113 | % Results
114 | % add number of objective function evaluations
115 | Results = cumsum(Best(1:gen,3));
116 | % number of function evaluation, mean and best results
117 | Results = [Results Best(1:gen,2) Best(1:gen,1)];
118 |
119 | % Plot Results and show best individuals => optimum
120 | figure('Name',['Results of ' FigTitle]);
121 | subplot(2,1,1), plot(Results(:,1),Results(:,2),'-',Results(:,1),Results(:,3),':');
122 | subplot(2,1,2), plot(IndAll(gen-4:gen,:)');
123 |
124 |
125 | % End of script
--------------------------------------------------------------------------------
/Test_fns/objbran.m:
--------------------------------------------------------------------------------
1 | % OBJBRAN.M (OBJective function for BRANin RCOS function)
2 | %
3 | % This function implements the BRANIN RCOS function.
4 | %
5 | % Syntax: ObjVal = objbran(Chrom,rtn_type)
6 | %
7 | % Input parameters:
8 | % Chrom - Matrix containing the chromosomes of the current
9 | % population. Each row corresponds to one individual's
10 | % string representation.
11 | % if Chrom == [], then special values will be returned
12 | % rtn_type - if Chrom == [] and
13 | % rtn_type == 1 (or []) return boundaries
14 | % rtn_type == 2 return title
15 | % rtn_type == 3 return value of global minimum
16 | %
17 | % Output parameters:
18 | % ObjVal - Column vector containing the objective values of the
19 | % individuals in the current population.
20 | % if called with Chrom == [], then ObjVal contains
21 | % rtn_type == 1, matrix with the boundaries of the function
22 | % rtn_type == 2, text for the title of the graphic output
23 | % rtn_type == 3, value of global minimum
24 | %
25 | % Author: Hartmut Pohlheim
26 | % History: 25.11.93 file created
27 | % 27.11.93 text of title and rtn_type added
28 | % 16.12.93 rtn_type == 3, return value of global minimum
29 | % 01.03.94 name changed in obj*
30 | % 13.01.03 updated for MATLAB v6 by Alex Shenfield
31 |
32 | function ObjVal = objbran(Chrom,rtn_type);
33 |
34 | % Compute population parameters
35 | [Nind,Nvar] = size(Chrom);
36 |
37 | % Check size of Chrom and do the appropriate thing
38 | % if Chrom is []
39 | if Nind == 0
40 | % return text of title for graphic output
41 | if rtn_type == 2
42 | ObjVal = 'BRANINs RCOS function';
43 | % return value of global minimum
44 | elseif rtn_type == 3
45 | ObjVal = 0.397887;
46 | % define size of boundary-matrix and values
47 | else
48 | % x1 x2
49 | ObjVal = [-5 0; % lower bounds
50 | 10 15]; % upper bounds
51 | end
52 | % if two variables, compute values of function
53 | elseif Nvar == 2
54 | % BRANIN's RCOS function
55 | % -5 <= x1 <= 10 ; 0 <= x2 <= 15
56 | % global minimum at (x1,x2)=(-pi,12.275), (pi,2.275), and
57 | % (9.42478,2.475) ; fmin=0.397887
58 | x1 = Chrom(:,1);
59 | x2 = Chrom(:,2);
60 | ObjVal = 1*(x2-(5.1/(4*pi^2))*x1.^2+(5/pi)*x1-6).^2+10*(1-(1/(8*pi))).*cos(x1)+10;
61 | % otherwise error, wrong format of Chrom
62 | else
63 | error('size of matrix Chrom is not correct for function evaluation');
64 | end
65 |
66 | % End of function
--------------------------------------------------------------------------------
/Test_fns/objdopi.m:
--------------------------------------------------------------------------------
1 | % OBJDOPI.M (OBJective function for DOuble Integrator)
2 | %
3 | % This function implements the Double Integrator.
4 | %
5 | % Syntax: ObjVal = objdopi(Chrom,rtn_type)
6 | %
7 | % Input parameters:
8 | % Chrom - Matrix containing the chromosomes of the current
9 | % population. Each row corresponds to one individual's
10 | % string representation.
11 | % if Chrom == [], then special values will be returned
12 | % rtn_type - if Chrom == [] and
13 | % rtn_type == 1 (or []) return boundaries
14 | % rtn_type == 2 return title
15 | % rtn_type == 3 return value of global minimum
16 | %
17 | % Output parameters:
18 | % ObjVal - Column vector containing the objective values of the
19 | % individuals in the current population.
20 | % if called with Chrom == [], then ObjVal contains
21 | % rtn_type == 1, matrix with the boundaries of the function
22 | % rtn_type == 2, text for the title of the graphic output
23 | % rtn_type == 3, value of global minimum
24 | %
25 | %
26 | % Author: Hartmut Pohlheim
27 | % History: 17.12.93 file created (copy of valfun7.m)
28 | % 19.12.93 Dim reintroduced
29 | % Dim and STEPSIMU independend from each other, rk23
30 | % can compute control between times
31 | % 01.03.94 name changed in obj*
32 | % 05.04.94 trapz used
33 | % 26.01.03 switch changed to rtn_type for compatability with MATLAB v6
34 | % by Alex Shenfield
35 |
36 | function [ObjVal,t,x] = objdopi(Chrom,rtn_type);
37 |
38 | % Define used method
39 | method = 1; % 1 - sim: simulink model
40 | % 2 - ode: ordinary differential equations
41 | % 3 - con: transfer function to state space
42 |
43 | % Dimension of objective function
44 | Dim = 20;
45 | TSTART = 0;
46 | TEND = 1;
47 | STEPSIMU = min(0.1,abs((TEND-TSTART)/(Dim-1)));
48 | TIMEVEC = linspace(TSTART,TEND,Dim)';
49 |
50 | % initial conditions
51 | XINIT = [ 0; -1];
52 |
53 | % end conditions
54 | XEND = [ 0; 0];
55 |
56 | % weights for control and end
57 | XENDWEIGHT = 12 * [1; 1]; % XEND(1); XEND(2)
58 | UWEIGHT = [0.5]; % Control vector
59 |
60 | % Compute population parameters
61 | [Nind,Nvar] = size(Chrom);
62 |
63 | % Check size of Chrom and do the appropriate thing
64 | % if Chrom is [], then
65 | if Nind == 0
66 | % return text of title for graphic output
67 | if rtn_type == 2
68 | if method == 2, ObjVal = ['Double Integrator (ode)-' int2str(Dim)];
69 | elseif method == 3, ObjVal = ['Double Integrator (con)-' int2str(Dim)];
70 | else ObjVal = ['Double Integrator (sim)-' int2str(Dim)];
71 | end
72 | % return value of global minimum
73 | elseif rtn_type == 3
74 | ObjVal = 2; % UWEIGHT * 3 * (TEND - TSTART);
75 | % define size of boundary-matrix and values
76 | else
77 | % lower and upper bound, identical for all n variables
78 | ObjVal1 = [-15; 15];
79 | ObjVal = rep(ObjVal1,[1 Dim]);
80 | end
81 | % if Dim variables, compute values of function
82 | elseif Nvar == Dim
83 | if method == 3, % Convert transfer function to state space system
84 | [Ai2, Bi2, Ci2, Di2] = tf2ss(1, [1 0 0]);
85 | t = TIMEVEC;
86 | end
87 | ObjVal = zeros(Nind,1);
88 | for indrun = 1:Nind
89 | steuerung = [TIMEVEC [Chrom(indrun,:)]'];
90 | if method == 2,
91 | [t, x] = rk23('simdopi2',[TSTART TEND],XINIT,[1e-3;STEPSIMU;STEPSIMU],steuerung);
92 | elseif method == 3,
93 | [y, x] = lsim(Ai2, Bi2, Ci2, Di2, Chrom(indrun,:),TIMEVEC, XINIT);
94 | else
95 | [t, x] = rk23('simdopi1',[TSTART TEND],[],[1e-3;STEPSIMU;STEPSIMU],steuerung);
96 | end
97 | % Calculate objective function, endvalues, trapez-integration for control vector
98 | ObjVal(indrun) = sum(XENDWEIGHT .* abs( x(size(x,1),:)' - XEND )) + ...
99 | (UWEIGHT / (Dim-1) * trapz(Chrom(indrun,:).^2));
100 | end
101 | % otherwise error, wrong format of Chrom
102 | else
103 | error('size of matrix Chrom is not correct for function evaluation');
104 | end
105 |
106 |
107 | % End of function
108 |
109 |
--------------------------------------------------------------------------------
/Test_fns/objeaso.m:
--------------------------------------------------------------------------------
1 | % OBJEASO.M (OBJective function for EASom function)
2 | %
3 | % This function implements the EASOM function.
4 | %
5 | % Syntax: ObjVal = objeaso(Chrom,rtn_type)
6 | %
7 | % Input parameters:
8 | % Chrom - Matrix containing the chromosomes of the current
9 | % population. Each row corresponds to one individual's
10 | % string representation.
11 | % if Chrom == [], then special values will be returned
12 | % rtn_type - if Chrom == [] and
13 | % rtn_type == 1 (or []) return boundaries
14 | % rtn_type == 2 return title
15 | % rtn_type == 3 return value of global minimum
16 | %
17 | % Output parameters:
18 | % ObjVal - Column vector containing the objective values of the
19 | % individuals in the current population.
20 | % if called with Chrom == [], then ObjVal contains
21 | % rtn_type == 1, matrix with the boundaries of the function
22 | % rtn_type == 2, text for the title of the graphic output
23 | % rtn_type == 3, value of global minimum
24 | %
25 | % Author: Hartmut Pohlheim
26 | % History: 25.11.93 file created
27 | % 27.11.93 text of title and rtn_type added
28 | % 16.12.93 rtn_type == 3, return value of global minimum
29 | % 01.03.94 name changed in obj*
30 | % 13.01.03 updated for MATLAB v6 by Alex Shenfield
31 |
32 | function ObjVal = objeaso(Chrom,rtn_type);
33 |
34 | % Compute population parameters
35 | [Nind,Nvar] = size(Chrom);
36 |
37 | % Check size of Chrom and do the appropriate thing
38 | % if Chrom is []
39 | if Nind == 0
40 | % return text of title for graphic output
41 | if rtn_type == 2
42 | ObjVal = 'EASOMs function';
43 | % return value of global minimum
44 | elseif rtn_type == 3
45 | ObjVal = -1;
46 | % define size of boundary-matrix and values
47 | else
48 | % x1 x2
49 | ObjVal = [-100 -100; % lower bounds
50 | 100 100]; % upper bounds
51 | end
52 | % if two variables, compute values of function
53 | elseif Nvar == 2
54 | % EASOM's function
55 | % -100(-5) <= x1 <= 100(5) ; -100(-5) <= x2 <= 100(5)
56 | % global minimum at (x1,x2)=(pi,pi) ; fmin=-1
57 | x1 = Chrom(:,1);
58 | x2 = Chrom(:,2);
59 | ObjVal = -cos(x1).*cos(x2).*exp(-((x1-pi).^2+(x2-pi).^2));
60 | % otherwise error, wrong format of Chrom
61 | else
62 | error('size of matrix Chrom is not correct for function evaluation');
63 | end
64 |
65 | % End of function
--------------------------------------------------------------------------------
/Test_fns/objfun1.m:
--------------------------------------------------------------------------------
1 | % OBJFUN1.M (OBJective function for de jong's FUNction 1)
2 | %
3 | % This function implements the DE JONG function 1.
4 | %
5 | % Syntax: ObjVal = objfun1(Chrom,rtn_type)
6 | %
7 | % Input parameters:
8 | % Chrom - Matrix containing the chromosomes of the current
9 | % population. Each row corresponds to one individual's
10 | % string representation.
11 | % if Chrom == [], then special values will be returned
12 | % rtn_type - if Chrom == [] and
13 | % rtn_type == 1 (or []) return boundaries
14 | % rtn_type == 2 return title
15 | % rtn_type == 3 return value of global minimum
16 | %
17 | % Output parameters:
18 | % ObjVal - Column vector containing the objective values of the
19 | % individuals in the current population.
20 | % if called with Chrom == [], then ObjVal contains
21 | % rtn_type == 1, matrix with the boundaries of the function
22 | % rtn_type == 2, text for the title of the graphic output
23 | % rtn_type == 3, value of global minimum
24 | %
25 | % Author: Hartmut Pohlheim
26 | % History: 26.11.93 file created
27 | % 27.11.93 text of title and rtn_type added
28 | % 30.11.93 show Dim in figure titel
29 | % 16.12.93 rtn_type == 3, return value of global minimum
30 | % 01.03.94 name changed in obj*
31 | % 13.01.03 updated for MATLAB v6 by Alex Shenfield
32 |
33 | function ObjVal = objfun1(Chrom,rtn_type);
34 |
35 | % Dimension of objective function
36 | Dim = 20;
37 |
38 | % Compute population parameters
39 | [Nind,Nvar] = size(Chrom);
40 |
41 | % Check size of Chrom and do the appropriate thing
42 | % if Chrom is [], then define size of boundary-matrix and values
43 | if Nind == 0
44 | % return text of title for graphic output
45 | if rtn_type == 2
46 | ObjVal = ['DE JONG function 1-' int2str(Dim)];
47 | % return value of global minimum
48 | elseif rtn_type == 3
49 | ObjVal = 0;
50 | % define size of boundary-matrix and values
51 | else
52 | % lower and upper bound, identical for all n variables
53 | ObjVal = 100*[-5.12; 5.12];
54 | ObjVal = ObjVal(1:2,ones(Dim,1));
55 | end
56 | % if Dim variables, compute values of function
57 | elseif Nvar == Dim
58 | % function 1, sum of xi^2 for i = 1:Dim (Dim=30)
59 | % n = Dim, -5.12 <= xi <= 5.12
60 | % global minimum at (xi)=(0) ; fmin=0
61 | ObjVal = sum((Chrom .* Chrom)')';
62 | % ObjVal = diag(Chrom * Chrom'); % both lines produce the same
63 | % otherwise error, wrong format of Chrom
64 | else
65 | error('size of matrix Chrom is not correct for function evaluation');
66 | end
67 |
68 |
69 | % End of function
--------------------------------------------------------------------------------
/Test_fns/objfun1a.m:
--------------------------------------------------------------------------------
1 | % OBJFUN1A.M (OBJective function for axis parallel hyper-ellipsoid)
2 | %
3 | % This function implements the axis parallel hyper-ellipsoid.
4 | %
5 | % Syntax: ObjVal = objfun1a(Chrom,rtn_type)
6 | %
7 | % Input parameters:
8 | % Chrom - Matrix containing the chromosomes of the current
9 | % population. Each row corresponds to one individual's
10 | % string representation.
11 | % if Chrom == [], then special values will be returned
12 | % rtn_type - if Chrom == [] and
13 | % rtn_type == 1 (or []) return boundaries
14 | % rtn_type == 2 return title
15 | % rtn_type == 3 return value of global minimum
16 | %
17 | % Output parameters:
18 | % ObjVal - Column vector containing the objective values of the
19 | % individuals in the current population.
20 | % if called with Chrom == [], then ObjVal contains
21 | % rtn_type == 1, matrix with the boundaries of the function
22 | % rtn_type == 2, text for the title of the graphic output
23 | % rtn_type == 3, value of global minimum
24 | %
25 | % Author: Hartmut Pohlheim
26 | % History: 07.04.94 file created
27 | % 13.01.03 updated for MATLAB v6 by Alex Shenfield
28 |
29 | function ObjVal = objfun1a(Chrom,rtn_type);
30 |
31 | % Dimension of objective function
32 | Dim = 10;
33 |
34 | % Compute population parameters
35 | [Nind,Nvar] = size(Chrom);
36 |
37 | % Check size of Chrom and do the appropriate thing
38 | % if Chrom is [], then define size of boundary-matrix and values
39 | if Nind == 0
40 | % return text of title for graphic output
41 | if rtn_type == 2
42 | ObjVal = ['Axis Parallel Hyper-Ellipsoid 1a-' int2str(Dim)];
43 | % return value of global minimum
44 | elseif rtn_type == 3
45 | ObjVal = 0;
46 | % define size of boundary-matrix and values
47 | else
48 | % lower and upper bound, identical for all n variables
49 | ObjVal = 100*[-5.12; 5.12];
50 | ObjVal = ObjVal(1:2,ones(Dim,1));
51 | end
52 | % if Dim variables, compute values of function
53 | elseif Nvar == Dim
54 | % function 1a, sum of i * xi^2 for i = 1:Dim (Dim=30)
55 | % n = Dim, -5.12 <= xi <= 5.12
56 | % global minimum at (xi)=(0) ; fmin=0
57 | nummer = rep(1:Dim,[Nind 1]);
58 | ObjVal = sum((nummer .* (Chrom .* Chrom))')';
59 | % ObjVal = diag((nummer .* (Chrom * Chrom))'); % both lines produce the same
60 | % otherwise error, wrong format of Chrom
61 | else
62 | error('size of matrix Chrom is not correct for function evaluation');
63 | end
64 |
65 |
66 | % End of function
--------------------------------------------------------------------------------
/Test_fns/objfun1b.m:
--------------------------------------------------------------------------------
1 | % OBJFUN1B.M (OBJective function for rotated hyper-ellipsoid)
2 | %
3 | % This function implements the rotated hyper-ellipsoid.
4 | %
5 | % Syntax: ObjVal = objfun1b(Chrom,rtn_type)
6 | %
7 | % Input parameters:
8 | % Chrom - Matrix containing the chromosomes of the current
9 | % population. Each row corresponds to one individual's
10 | % string representation.
11 | % if Chrom == [], then special values will be returned
12 | % rtn_type - if Chrom == [] and
13 | % rtn_type == 1 (or []) return boundaries
14 | % rtn_type == 2 return title
15 | % rtn_type == 3 return value of global minimum
16 | %
17 | % Output parameters:
18 | % ObjVal - Column vector containing the objective values of the
19 | % individuals in the current population.
20 | % if called with Chrom == [], then ObjVal contains
21 | % rtn_type == 1, matrix with the boundaries of the function
22 | % rtn_type == 2, text for the title of the graphic output
23 | % rtn_type == 3, value of global minimum
24 | %
25 | % Author: Hartmut Pohlheim
26 | % History: 07.04.94 file created
27 | % 13.01.03 updated for MATLAB v6 by Alex Shenfield
28 |
29 | function ObjVal = objfun1b(Chrom,rtn_type);
30 |
31 | % Dimension of objective function
32 | Dim = 10;
33 |
34 | % Compute population parameters
35 | [Nind,Nvar] = size(Chrom);
36 |
37 | % Check size of Chrom and do the appropriate thing
38 | % if Chrom is [], then define size of boundary-matrix and values
39 | if Nind == 0
40 | % return text of title for graphic output
41 | if rtn_type == 2
42 | ObjVal = ['Rotated Hyper-Ellipsoid 1b-' int2str(Dim)];
43 | % return value of global minimum
44 | elseif rtn_type == 3
45 | ObjVal = 0;
46 | % define size of boundary-matrix and values
47 | else
48 | % lower and upper bound, identical for all n variables
49 | ObjVal = [-65; 65];
50 | ObjVal = ObjVal(1:2,ones(Dim,1));
51 | end
52 | % if Dim variables, compute values of function
53 | elseif Nvar == Dim
54 | % function 1b, sum over i of ( sum of xj )^2 for i = 1:Dim, j = 1:i (Dim=30)
55 | % n = Dim, -65 <= xj <= 65
56 | % global minimum at (xi)=(0) ; fmin=0
57 | ObjVal = sum(cumsum(Chrom').^2)';
58 | % otherwise error, wrong format of Chrom
59 | else
60 | error('size of matrix Chrom is not correct for function evaluation');
61 | end
62 |
63 |
64 | % End of function
--------------------------------------------------------------------------------
/Test_fns/objfun2.m:
--------------------------------------------------------------------------------
1 | % OBJFUN2.M (OBJective function for rosenbrock's FUNction)
2 | %
3 | % This function implements the ROSENBROCK valley (DE JONG's Function 2).
4 | %
5 | % Syntax: ObjVal = objfun2(Chrom,rtn_type)
6 | %
7 | % Input parameters:
8 | % Chrom - Matrix containing the chromosomes of the current
9 | % population. Each row corresponds to one
10 | % individual's string representation.
11 | % if called with Chrom == [], then boundaries of
12 | % the function or title for figure will be returned
13 | % rtn_type - if Chrom == [] and rtn_type == 1 (or []) then return
14 | % boundaries, if rtn_type == 2 return title
15 | %
16 | % Output parameters:
17 | % ObjVal - Column vector containing the objective values of the
18 | % individuals in the current population.
19 | % if called with Chrom == [], then ObjVal contains
20 | % the matrix with the boundaries of the function or
21 | % the Text for the title of the graphic output
22 | %
23 | % Author: Hartmut Pohlheim
24 | % History: 26.01.94 file created
25 | % 01.03.94 name changed in obj*
26 | % 13.01.03 updated for MATLAB v6 by Alex Shenfield
27 |
28 | function ObjVal = objfun2(Chrom,rtn_type);
29 |
30 | % Dimension of objective function
31 | Dim = 2;
32 |
33 | % Compute population parameters
34 | [Nind,Nvar] = size(Chrom);
35 |
36 | % Check size of Chrom and do the appropriate thing
37 | % if Chrom is [], then define size of boundary-matrix and values
38 | if Nind == 0
39 | % return text of title for graphic output
40 | if rtn_type == 2
41 | ObjVal = ['ROSENBROCKs function 2-' int2str(Dim)];
42 | % return value of global minimum
43 | elseif rtn_type == 3
44 | ObjVal = 0;
45 | % define size of boundary-matrix and values
46 | else
47 | % lower and upper bound, identical for all n variables
48 | ObjVal = [-2; 2];
49 | ObjVal = ObjVal(1:2,ones(Dim,1));
50 | end
51 | % if Dim variables, compute values of function
52 | elseif Nvar == Dim
53 | % function 11, sum of 100* (x(i+1) -xi^2)^2+(1-xi)^2 for i = 1:Dim (Dim=10)
54 | % n = Dim, -10 <= xi <= 10
55 | % global minimum at (xi)=(1) ; fmin=0
56 | Mat1 = Chrom(:,1:Nvar-1);
57 | Mat2 = Chrom(:,2:Nvar);
58 | if Dim == 2
59 | ObjVal = 100*(Mat2-Mat1.^2).^2+(1-Mat1).^2;
60 | else
61 | ObjVal = sum((100*(Mat2-Mat1.^2).^2+(1-Mat1).^2)')';
62 | end
63 | % otherwise error, wrong format of Chrom
64 | else
65 | error('size of matrix Chrom is not correct for function evaluation');
66 | end
67 |
68 |
69 | % End of function
--------------------------------------------------------------------------------
/Test_fns/objfun6.m:
--------------------------------------------------------------------------------
1 | % OBJFUN6.M (OBJective function for rastrigins FUNction 6)
2 | %
3 | % This function implements the RASTRIGIN function 6.
4 | %
5 | % Syntax: ObjVal = objfun6(Chrom,rtn_type)
6 | %
7 | % Input parameters:
8 | % Chrom - Matrix containing the chromosomes of the current
9 | % population. Each row corresponds to one individual's
10 | % string representation.
11 | % if Chrom == [], then special values will be returned
12 | % rtn_type - if Chrom == [] and
13 | % rtn_type == 1 (or []) return boundaries
14 | % rtn_type == 2 return title
15 | % rtn_type == 3 return value of global minimum
16 | %
17 | % Output parameters:
18 | % ObjVal - Column vector containing the objective values of the
19 | % individuals in the current population.
20 | % if called with Chrom == [], then ObjVal contains
21 | % rtn_type == 1, matrix with the boundaries of the function
22 | % rtn_type == 2, text for the title of the graphic output
23 | % rtn_type == 3, value of global minimum
24 | %
25 | % Author: Hartmut Pohlheim
26 | % History: 26.11.93 file created
27 | % 27.11.93 text of title and rtn_type added
28 | % 30.11.93 show Dim in figure titel
29 | % 16.12.93 rtn_type == 3, return value of global minimum
30 | % 01.03.94 name changed in obj*
31 | % 13.01.03 updated for MATLAB v6 by Alex Shenfield
32 |
33 | function ObjVal = objfun6(Chrom,rtn_type);
34 |
35 | % Dimension of objective function
36 | Dim = 20;
37 |
38 | % Compute population parameters
39 | [Nind,Nvar] = size(Chrom);
40 |
41 | % Check size of Chrom and do the appropriate thing
42 | % if Chrom is [], then define size of boundary-matrix and values
43 | if Nind == 0
44 | % return text of title for graphic output
45 | if rtn_type == 2
46 | ObjVal = ['RASTRIGINs function 6-' int2str(Dim)];
47 | % return value of global minimum
48 | elseif rtn_type == 3
49 | ObjVal = 0;
50 | % define size of boundary-matrix and values
51 | else
52 | % lower and upper bound, identical for all n variables
53 | ObjVal = [-5.12; 5.12];
54 | ObjVal = ObjVal(1:2,ones(Dim,1));
55 | end
56 | % if Dim variables, compute values of function
57 | elseif Nvar == Dim
58 | % function 6, Dim*A + sum of (xi^2 - A*cos(Omega*xi)) for i = 1:Dim (Dim=20)
59 | % n = Dim, -5.12 <= xi <= 5.12
60 | % global minimum at (xi)=(0) ; fmin=0
61 | A = 10;
62 | Omega = 2 * pi;
63 | ObjVal = Dim * A + sum(((Chrom .* Chrom) - A * cos(Omega * Chrom))')';
64 | % ObjVal = diag(Chrom * Chrom'); % both lines produce the same
65 | % otherwise error, wrong format of Chrom
66 | else
67 | error('size of matrix Chrom is not correct for function evaluation');
68 | end
69 |
70 |
71 | % End of function
--------------------------------------------------------------------------------
/Test_fns/objfun7.m:
--------------------------------------------------------------------------------
1 | % OBJFUN7.M (OBJective function for schwefel's FUNction)
2 | %
3 | % This function implements the SCHWEFEL function 7.
4 | %
5 | % Syntax: ObjVal = objfun7(Chrom,rtn_type)
6 | %
7 | % Input parameters:
8 | % Chrom - Matrix containing the chromosomes of the current
9 | % population. Each row corresponds to one
10 | % individual's string representation.
11 | % if called with Chrom == [], then boundaries of
12 | % the function or title for figure will be returned
13 | % rtn_type - if Chrom == [] and rtn_type == 1 (or []) then return
14 | % boundaries, if rtn_type == 2 return title
15 | %
16 | % Output parameters:
17 | % ObjVal - Column vector containing the objective values of the
18 | % individuals in the current population.
19 | % if called with Chrom == [], then ObjVal contains
20 | % the matrix with the boundaries of the function or
21 | % the Text for the title of the graphic output
22 | %
23 | %
24 | % Author: Hartmut Pohlheim
25 | % History: 27.11.93 file created
26 | % 30.11.93 show Dim in figure title
27 | % 01.03.94 name changed in obj*
28 | % 14.01.03 updated for MATLAB v6 by Alex Shenfield
29 |
30 | function ObjVal = objfun7(Chrom,rtn_type);
31 |
32 | % Dimension of objective function
33 | Dim = 20;
34 |
35 | % Compute population parameters
36 | [Nind,Nvar] = size(Chrom);
37 |
38 | % Check size of Chrom and do the appropriate thing
39 | % if Chrom is [], then define size of boundary-matrix and values
40 | if Nind == 0
41 | % return text of title for graphic output
42 | if rtn_type == 2
43 | ObjVal = ['SCHWEFELs function 7-' int2str(Dim)];
44 | % return value of global minimum
45 | elseif rtn_type == 3
46 | xmin = 420.9687;
47 | ObjVal = Dim * (-xmin * sin(sqrt(abs(xmin))));
48 | % define size of boundary-matrix and values
49 | else
50 | % lower and upper bound, identical for all n variables
51 | ObjVal = [-500; 500];
52 | ObjVal = ObjVal(1:2,ones(Dim,1));
53 | end
54 | % if Dim variables, compute values of function
55 | elseif Nvar == Dim
56 | % function 7, sum of -xi*sin(sqrt(abs(xi))) for i = 1:Dim (Dim=10)
57 | % n = Dim, -500 <= xi <= 500
58 | % global minimum at (xi)=(420.9687) ; fmin=?
59 | ObjVal = sum((-Chrom .* sin(sqrt(abs(Chrom))))')';
60 | % otherwise error, wrong format of Chrom
61 | else
62 | error('size of matrix Chrom is not correct for function evaluation');
63 | end
64 |
65 |
66 | % End of function
--------------------------------------------------------------------------------
/Test_fns/objfun8.m:
--------------------------------------------------------------------------------
1 | % OBJFUN8.M (OBJective function for griewangk's FUNction)
2 | %
3 | % This function implements the GRIEWANGK function 8.
4 | %
5 | % Syntax: ObjVal = objfun8(Chrom,rtn_type)
6 | %
7 | % Input parameters:
8 | % Chrom - Matrix containing the chromosomes of the current
9 | % population. Each row corresponds to one individual's
10 | % string representation.
11 | % if Chrom == [], then special values will be returned
12 | % rtn_type - if Chrom == [] and
13 | % rtn_type == 1 (or []) return boundaries
14 | % rtn_type == 2 return title
15 | % rtn_type == 3 return value of global minimum
16 | %
17 | % Output parameters:
18 | % ObjVal - Column vector containing the objective values of the
19 | % individuals in the current population.
20 | % if called with Chrom == [], then ObjVal contains
21 | % rtn_type == 1, matrix with the boundaries of the function
22 | % rtn_type == 2, text for the title of the graphic output
23 | % rtn_type == 3, value of global minimum
24 | %
25 | %
26 | % Author: Hartmut Pohlheim
27 | % History: 12.12.93 file created (copy of valfun7.m)
28 | % 16.12.93 rtn_type == 3, return value of global minimum
29 | % 27.01.94 20* in formula, correction ??
30 | % 01.03.94 name changed in obj*
31 | % 14.01.03 updated for MATLAB v6 by Alex Shenfield
32 |
33 | function ObjVal = objfun8(Chrom,rtn_type);
34 |
35 | % Dimension of objective function
36 | Dim = 10;
37 |
38 | % Compute population parameters
39 | [Nind,Nvar] = size(Chrom);
40 |
41 | % Check size of Chrom and do the appropriate thing
42 | % if Chrom is [], then define size of boundary-matrix and values
43 | if Nind == 0
44 | % return text of title for graphic output
45 | if rtn_type == 2
46 | ObjVal = ['GRIEWANGKs function 8-' int2str(Dim)];
47 | % return value of global minimum
48 | elseif rtn_type == 3
49 | ObjVal = 0;
50 | % define size of boundary-matrix and values
51 | else
52 | % lower and upper bound, identical for all n variables
53 | ObjVal = [-600; 600];
54 | ObjVal = ObjVal(1:2,ones(Dim,1));
55 | end
56 | % if Dim variables, compute values of function
57 | elseif Nvar == Dim
58 | % function 8, sum(xi^2/4000) - 20*prod(cos(xi/sqrt(i))) + 1 for i = 1:Dim (Dim=10)
59 | % n = Dim, -600 <= xi <= 600
60 | % global minimum at (xi)=(0) ; fmin=0
61 | % nummer = 1:Dim;
62 | nummer = rep(1:Dim,[Nind 1]);
63 | ObjVal = sum(((Chrom.^2) / 4000)')' - prod(cos(Chrom ./ sqrt(nummer))')' + 1;
64 | % otherwise error, wrong format of Chrom
65 | else
66 | error('size of matrix Chrom is not correct for function evaluation');
67 | end
68 |
69 |
70 | % End of function
--------------------------------------------------------------------------------
/Test_fns/objfun9.m:
--------------------------------------------------------------------------------
1 | % OBJFUN9.M (OBJective function for sum of different power FUNction 9)
2 | %
3 | % This function implements the sum of different power.
4 | %
5 | % Syntax: ObjVal = objfun9(Chrom,rtn_type)
6 | %
7 | % Input parameters:
8 | % Chrom - Matrix containing the chromosomes of the current
9 | % population. Each row corresponds to one individual's
10 | % string representation.
11 | % if Chrom == [], then special values will be returned
12 | % rtn_type - if Chrom == [] and
13 | % rtn_type == 1 (or []) return boundaries
14 | % rtn_type == 2 return title
15 | % rtn_type == 3 return value of global minimum
16 | %
17 | % Output parameters:
18 | % ObjVal - Column vector containing the objective values of the
19 | % individuals in the current population.
20 | % if called with Chrom == [], then ObjVal contains
21 | % rtn_type == 1, matrix with the boundaries of the function
22 | % rtn_type == 2, text for the title of the graphic output
23 | % rtn_type == 3, value of global minimum
24 | %
25 | %
26 | % Author: Hartmut Pohlheim
27 | % History: 07.04.94 file created
28 | % 14.01.03 updated for MATLAB v6 by Alex Shenfield
29 |
30 | function ObjVal = objfun9(Chrom,rtn_type);
31 |
32 | % Dimension of objective function
33 | Dim = 10;
34 |
35 | % Compute population parameters
36 | [Nind,Nvar] = size(Chrom);
37 |
38 | % Check size of Chrom and do the appropriate thing
39 | % if Chrom is [], then define size of boundary-matrix and values
40 | if Nind == 0
41 | % return text of title for graphic output
42 | if rtn_type == 2
43 | ObjVal = ['Sum of different Power 9-' int2str(Dim)];
44 | % return value of global minimum
45 | elseif rtn_type == 3
46 | ObjVal = 0;
47 | % define size of boundary-matrix and values
48 | else
49 | % lower and upper bound, identical for all n variables
50 | ObjVal = [-1; 1];
51 | ObjVal = ObjVal(1:2,ones(Dim,1));
52 | end
53 | % if Dim variables, compute values of function
54 | elseif Nvar == Dim
55 | % function 9, sum of abs(xi)^(i+1) for i = 1:Dim (Dim=30)
56 | % n = Dim, -1 <= xi <= 1
57 | % global minimum at (xi)=(0) ; fmin=0
58 | nummer = rep(1:Dim,[Nind 1]);
59 | ObjVal = sum((abs(Chrom).^(nummer+1))')';
60 | % otherwise error, wrong format of Chrom
61 | else
62 | error('size of matrix Chrom is not correct for function evaluation');
63 | end
64 |
65 |
66 | % End of function
--------------------------------------------------------------------------------
/Test_fns/objgold.m:
--------------------------------------------------------------------------------
1 | % OBJGOLD.M (OBJective function for GOLDstein-price function)
2 | %
3 | % This function implements the GOLDSTEIN-PRICE function.
4 | %
5 | % Syntax: ObjVal = objgold(Chrom,rtn_type)
6 | %
7 | % Input parameters:
8 | % Chrom - Matrix containing the chromosomes of the current
9 | % population. Each row corresponds to one individual's
10 | % string representation.
11 | % if Chrom == [], then special values will be returned
12 | % rtn_type - if Chrom == [] and
13 | % rtn_type == 1 (or []) return boundaries
14 | % rtn_type == 2 return title
15 | % rtn_type == 3 return value of global minimum
16 | %
17 | % Output parameters:
18 | % ObjVal - Column vector containing the objective values of the
19 | % individuals in the current population.
20 | % if called with Chrom == [], then ObjVal contains
21 | % rtn_type == 1, matrix with the boundaries of the function
22 | % rtn_type == 2, text for the title of the graphic output
23 | % rtn_type == 3, value of global minimum
24 | %
25 | %
26 | % Author: Hartmut Pohlheim
27 | % History: 25.11.93 file created
28 | % 27.11.93 text of title and rtn_type added
29 | % 16.12.93 rtn_type == 3, return value of global minimum
30 | % 01.03.94 name changed in obj*
31 | % 14.01.03 updated for MATLAB v6 by Alex Shenfield
32 |
33 | function ObjVal = objgold(Chrom,rtn_type);
34 |
35 | % Compute population parameters
36 | [Nind,Nvar] = size(Chrom);
37 |
38 | % Check size of Chrom and do the appropriate thing
39 | % if Chrom is [], then define size of boundary-matrix and values
40 | if Nind == 0
41 | % return text of title for graphic output
42 | if rtn_type == 2
43 | ObjVal = 'GOLDSTEIN-PRICE function';
44 | % return value of global minimum
45 | elseif rtn_type == 3
46 | ObjVal = 3;
47 | % define size of boundary-matrix and values
48 | else
49 | brd = 3;
50 | % x1 x2
51 | ObjVal = [-brd -brd; % lower bounds
52 | brd brd]; % upper bounds
53 | end
54 | % if two variables, compute values of function
55 | elseif Nvar == 2
56 | % GOLDSTEIN-PRICE function
57 | % -2 <= x1 <= 2 ; -2 <= x2 <= 2 (or -10 <= xi <= 10)
58 | % global minimum at (x1,x2)=(0,-1) ; fmin=3
59 | x1 = Chrom(:,1);
60 | x2 = Chrom(:,2);
61 | ObjVal = ((1+(x1+x2+1).^2.*(19-14*x1+3*x1.^2-14*x2+6*x1.*x2+3*x2.^2))...
62 | .*(30+(2*x1-3*x2).^2.*(18-32*x1+12*x1.^2+48*x2-36*x1.*x2+27*x2.^2)));
63 | % otherwise error, wrong format of Chrom
64 | else
65 | error('size of matrix Chrom is not correct for function evaluation');
66 | end
67 |
68 |
69 | % End of function
--------------------------------------------------------------------------------
/Test_fns/objharv.m:
--------------------------------------------------------------------------------
1 | % OBJHARV.M (OBJective function for HARVest problem)
2 | %
3 | % This function implements the HARVEST PROBLEM.
4 | %
5 | % Syntax: ObjVal = objharv(Chrom,rtn_type)
6 | %
7 | % Input parameters:
8 | % Chrom - Matrix containing the chromosomes of the current
9 | % population. Each row corresponds to one individual's
10 | % string representation.
11 | % if Chrom == [], then special values will be returned
12 | % rtn_type - if Chrom == [] and
13 | % rtn_type == 1 (or []) return boundaries
14 | % rtn_type == 2 return title
15 | % rtn_type == 3 return value of global minimum
16 | %
17 | % Output parameters:
18 | % ObjVal - Column vector containing the objective values of the
19 | % individuals in the current population.
20 | % if called with Chrom == [], then ObjVal contains
21 | % rtn_type == 1, matrix with the boundaries of the function
22 | % rtn_type == 2, text for the title of the graphic output
23 | % rtn_type == 3, value of global minimum
24 | %
25 | %
26 | % Author: Hartmut Pohlheim
27 | % History: 18.02.94 file created (copy of vallinq.m)
28 | % 01.03.94 name changed in obj*
29 | % 14.01.03 updated for MATLAB v6 by Alex Shenfield
30 |
31 | function ObjVal = objharv(Chrom,rtn_type);
32 |
33 | % global gen;
34 |
35 | % Dimension of objective function
36 | Dim = 20;
37 |
38 | % values from MICHALEWICZ
39 | a = 1.1;
40 | x0 = 100;
41 | xend = x0;
42 | XENDWEIGHT = 0.4/(Dim^0.6);
43 |
44 | % Compute population parameters
45 | [Nind,Nvar] = size(Chrom);
46 |
47 | % Check size of Chrom and do the appropriate thing
48 | % if Chrom is [], then define size of boundary-matrix and values
49 | if Nind == 0
50 | % return text of title for graphic output
51 | if rtn_type == 2
52 | ObjVal = ['HARVEST PROBLEM-' int2str(Dim)];
53 | % return value of global minimum
54 | elseif rtn_type == 3
55 | ObjVal = -sqrt(x0*(a^Dim-1)^2/(a^(Dim-1)*(a-1)));
56 | % define size of boundary-matrix and values
57 | else
58 | % lower and upper bound, identical for all n variables
59 | ObjVal1 = [0; 10*Dim];
60 | ObjVal = rep(ObjVal1,[1 Dim]);
61 | end
62 | % if Dim variables, compute values of function
63 | elseif Nvar == Dim
64 | ObjVal = zeros(Nind,1);
65 | X = rep(x0,[Nind 1]);
66 | for irun = 1:Nvar,
67 | X = a*X - Chrom(:,irun);
68 | end
69 | X;
70 | ObjVal = -(sum(sqrt(Chrom)')' - XENDWEIGHT * abs(X-x0));
71 | % otherwise error, wrong format of Chrom
72 | else
73 | error('size of matrix Chrom is not correct for function evaluation');
74 | end
75 |
76 |
77 | % End of function
--------------------------------------------------------------------------------
/Test_fns/objlinq.m:
--------------------------------------------------------------------------------
1 | % OBJLINQ.M (OBJective function for LINear Quadratic problem)
2 | %
3 | % This function implements the discret LINEAR-QUADRATIC PROBLEM.
4 | %
5 | % Syntax: ObjVal = objlinq(Chrom,rtn_type)
6 | %
7 | % Input parameters:
8 | % Chrom - Matrix containing the chromosomes of the current
9 | % population. Each row corresponds to one individual's
10 | % string representation.
11 | % if Chrom == [], then special values will be returned
12 | % rtn_type - if Chrom == [] and
13 | % rtn_type == 1 (or []) return boundaries
14 | % rtn_type == 2 return title
15 | % rtn_type == 3 return value of global minimum
16 | %
17 | % Output parameters:
18 | % ObjVal - Column vector containing the objective values of the
19 | % individuals in the current population.
20 | % if called with Chrom == [], then ObjVal contains
21 | % rtn_type == 1, matrix with the boundaries of the function
22 | % rtn_type == 2, text for the title of the graphic output
23 | % rtn_type == 3, value of global minimum
24 | %
25 | %
26 | % Author: Hartmut Pohlheim
27 | % History: 18.02.94 file created (copy of valfun7.m)
28 | % 01.03.94 name changed in obj*
29 | % 14.01.03 updated for MATLAB v6 by Alex Shenfield
30 |
31 | function ObjVal = objlinq(Chrom,rtn_type);
32 |
33 | % Dimension of objective function
34 | Dim = 45;
35 |
36 | % values from MICHALEWICZ
37 | x0 = 100; % start of X
38 | var = 1; % 1 - 10 possible
39 | Para = [ 1 1 1 1 1 16180.3399;
40 | 10 1 1 1 1 109160.7978;
41 | 1000 1 1 1 1 10009990.0200;
42 | 1 10 1 1 1 37015.6212;
43 | 1 1000 1 1 1 287569.3725;
44 | 1 1 0 1 1 16180.3399;
45 | 1 1 1000 1 1 16180.3399;
46 | 1 1 1 0.01 1 10000.5000;
47 | 1 1 1 1 0.01 431004.0987;
48 | 1 1 1 1 100 10000.9999];
49 | s = Para(var,1); r = Para(var,2); q = Para(var,3);
50 | a = Para(var,4); b = Para(var,5); GlobalMinimum = Para(var,6);
51 |
52 | % Compute population parameters
53 | [Nind,Nvar] = size(Chrom);
54 |
55 | % Check size of Chrom and do the appropriate thing
56 | % if Chrom is [], then define size of boundary-matrix and values
57 | if Nind == 0
58 | % return text of title for graphic output
59 | if rtn_type == 2
60 | ObjVal = ['Linear-quadratic problem (dis)-' int2str(Dim)];
61 | % return value of global minimum
62 | elseif rtn_type == 3
63 | ObjVal = GlobalMinimum;
64 | % define size of boundary-matrix and values
65 | else
66 | % lower and upper bound, identical for all n variables
67 | ObjVal1 = [-100 -70 -50; 20 20 20];
68 | ObjVal = [ObjVal1 rep([-30;20],[1 Dim-3])];
69 | end
70 | % if Dim variables, compute values of function
71 | elseif Nvar == Dim
72 | ObjVal = zeros(Nind,1);
73 | X = zeros(Nind,Nvar+1);
74 | X(:,1) = rep(x0,[Nind 1]);
75 | for irun = 1:Nvar,
76 | X(:,irun+1) = a*X(:,irun) + b*Chrom(:,irun);
77 | end
78 | ObjVal = q * X(:,Nvar+1).^2 + sum((s * X(:,1:Nvar).^2 + r * Chrom.^2)')';
79 | % otherwise error, wrong format of Chrom
80 | else
81 | error('size of matrix Chrom is not correct for function evaluation');
82 | end
83 |
84 |
85 | % End of function
--------------------------------------------------------------------------------
/Test_fns/objlinq2.m:
--------------------------------------------------------------------------------
1 | % OBJLINQ2.M (OBJective function for LINear Quadratic problem)
2 | %
3 | % This function implements the continuous LINear Quadratic problem.
4 | %
5 | % Syntax: ObjVal = objlinq2(Chrom,rtn_type)
6 | %
7 | % Input parameters:
8 | % Chrom - Matrix containing the chromosomes of the current
9 | % population. Each row corresponds to one individual's
10 | % string representation.
11 | % if Chrom == [], then special values will be returned
12 | % rtn_type - if Chrom == [] and
13 | % rtn_type == 1 (or []) return boundaries
14 | % rtn_type == 2 return title
15 | % rtn_type == 3 return value of global minimum
16 | %
17 | % Output parameters:
18 | % ObjVal - Column vector containing the objective values of the
19 | % individuals in the current population.
20 | % if called with Chrom == [], then ObjVal contains
21 | % rtn_type == 1, matrix with the boundaries of the function
22 | % rtn_type == 2, text for the title of the graphic output
23 | % rtn_type == 3, value of global minimum
24 | %
25 | %
26 | % Author: Hartmut Pohlheim
27 | % History: 03.03.94 file created
28 | % 06.04.94 all linq (sim, ode, con) in 1 file
29 | % 26.01.03 switch changed to rtn_type for compatability with MATLAB v6
30 | % by Alex Shenfield
31 |
32 | function [ObjVal,t,x] = objlinq2(Chrom,rtn_type);
33 |
34 | % Define used method
35 | method = 1; % 1 - sim: simulink model
36 | % 2 - ode: ordinary differential equations
37 | % 3 - con: transfer function to state space
38 |
39 | % Dimension of objective function
40 | Dim = 50;
41 | TSTART = 0;
42 | TEND = 1;
43 | STEPSIMU = min(0.1,abs((TEND-TSTART)/(Dim-1)));
44 | TIMEVEC = linspace(TSTART,TEND,Dim)';
45 |
46 | % initial conditions
47 | XINIT = [100];
48 |
49 | % end conditions
50 | XEND = [0];
51 |
52 | % weights for control and end
53 | XENDWEIGHT = [20]; % XEND
54 | XWEIGHT = [2]; % State vector
55 | UWEIGHT = [1]; % Control vector
56 |
57 | % Compute population parameters
58 | [Nind,Nvar] = size(Chrom);
59 |
60 | % Check size of Chrom and do the appropriate thing
61 | % if Chrom is [], then
62 | if Nind == 0
63 | % return text of title for graphic output
64 | if rtn_type == 2
65 | if method == 2, ObjVal = ['Linear-quadratic problem (ode)-' int2str(Dim)];
66 | elseif method == 3, ObjVal = ['Linear-quadratic problem (con)-' int2str(Dim)];
67 | else ObjVal = ['Linear-quadratic problem (sim)-' int2str(Dim)];
68 | end
69 | % return value of global minimum
70 | elseif rtn_type == 3
71 | ObjVal = 16180.3399;
72 | % define size of boundary-matrix and values
73 | else
74 | % lower and upper bound, identical for all n variables
75 | ObjVal = rep([-600; 0],[1 Dim]);
76 | end
77 | % if Dim variables, compute values of function
78 | elseif Nvar == Dim
79 | if method == 3, % Convert transfer function to state space system
80 | [NC DC]=cloop(1, [1 0], +1);
81 | [Ai2 Bi2 Ci2 Di2] = tf2ss(NC, DC);
82 | t = TIMEVEC;
83 | end
84 | ObjVal = zeros(Nind,1);
85 | for indrun = 1:Nind
86 | steuerung = [TIMEVEC Chrom(indrun,:)'];
87 | if method == 2,
88 | [t x] = linsim('simlinq2',[TSTART TEND],[],[1e-3;STEPSIMU;STEPSIMU],steuerung);
89 | elseif method == 3,
90 | [y x] = lsim(Ai2, Bi2, Ci2, Di2, Chrom(indrun,:),TIMEVEC, XINIT);
91 | else
92 | [t x] = linsim('simlinq1',[TSTART TEND],[],[1e-3;STEPSIMU;STEPSIMU],steuerung);
93 | end
94 | % Calculate objective function, endvalues, trapez-integration for control vector
95 | ObjVal(indrun) = (XENDWEIGHT * ( x(size(x,1),:)^2 )) + ...
96 | (UWEIGHT / (Dim-1) * trapz(Chrom(indrun,:).^2)) + ...
97 | (XWEIGHT / size(x,1) * sum(x.^2));
98 | end
99 | % otherwise error, wrong format of Chrom
100 | else
101 | error('size of matrix Chrom is not correct for function evaluation');
102 | end
103 |
104 | % End of function
--------------------------------------------------------------------------------
/Test_fns/objpush.m:
--------------------------------------------------------------------------------
1 | % OBJPUSH.M (OBJective function for PUSH-cart problem)
2 | %
3 | % This function implements the PUSH-CART PROBLEM.
4 | %
5 | % Syntax: ObjVal = objpush(Chrom,rtn_type)
6 | %
7 | % Input parameters:
8 | % Chrom - Matrix containing the chromosomes of the current
9 | % population. Each row corresponds to one individual's
10 | % string representation.
11 | % if Chrom == [], then special values will be returned
12 | % rtn_type - if Chrom == [] and
13 | % rtn_type == 1 (or []) return boundaries
14 | % rtn_type == 2 return title
15 | % rtn_type == 3 return value of global minimum
16 | %
17 | % Output parameters:
18 | % ObjVal - Column vector containing the objective values of the
19 | % individuals in the current population.
20 | % if called with Chrom == [], then ObjVal contains
21 | % rtn_type == 1, matrix with the boundaries of the function
22 | % rtn_type == 2, text for the title of the graphic output
23 | % rtn_type == 3, value of global minimum
24 | %
25 | %
26 | % Author: Hartmut Pohlheim
27 | % History: 19.02.94 file created (copy of valharv.m)
28 | % 01.03.94 name changed in obj*
29 | % 15.01.03 updated for MATLAB v6 by Alex Shenfield
30 |
31 | function ObjVal = objpush(Chrom,rtn_type);
32 |
33 | % Dimension of objective function
34 | Dim = 20;
35 |
36 | % values from MICHALEWICZ
37 | x0 = [0 0];
38 |
39 | % Compute population parameters
40 | [Nind,Nvar] = size(Chrom);
41 |
42 | % Check size of Chrom and do the appropriate thing
43 | % if Chrom is [], then define size of boundary-matrix and values
44 | if Nind == 0
45 | % return text of title for graphic output
46 | if rtn_type == 2
47 | ObjVal = ['PUSH-CART PROBLEM-' int2str(Dim)];
48 | % return value of global minimum
49 | elseif rtn_type == 3
50 | ObjVal = -(1/3 - ((3*Dim-1)/(6*Dim^2)) - (1/(2*Dim^3))*sum((1:Dim-1).^2));
51 | % define size of boundary-matrix and values
52 | else
53 | % lower and upper bound, identical for all n variables
54 | ObjVal = [0; 5];
55 | ObjVal = rep(ObjVal,[1 Dim]);
56 | end
57 | % if Dim variables, compute values of function
58 | elseif Nvar == Dim
59 | ObjVal = zeros(Nind,1);
60 | X = rep(x0,[Nind 1]);
61 | for irun = 1:Nvar,
62 | Xsave = X;
63 | X(:,1) = Xsave(:,2);
64 | X(:,2) = 2 * X(:,2) - Xsave(:,1) + (1/Dim^2) * Chrom(:,irun);
65 | end
66 | X;
67 | ObjVal = -(X(:,1) - (1/(2*Dim)) * sum((Chrom.^2)')');
68 | % otherwise error, wrong format of Chrom
69 | else
70 | error('size of matrix Chrom is not correct for function evaluation');
71 | end
72 |
73 |
74 | % End of function
--------------------------------------------------------------------------------
/Test_fns/objsixh.m:
--------------------------------------------------------------------------------
1 | % OBJSIXH.M (OBJective function for SIX Hump camelback function)
2 | %
3 | % This function implements the six hump camelback function.
4 | %
5 | % Syntax: ObjVal = objsixh(Chrom,rtn_type)
6 | %
7 | % Input parameters:
8 | % Chrom - Matrix containing the chromosomes of the current
9 | % population. Each row corresponds to one individual's
10 | % string representation.
11 | % if Chrom == [], then special values will be returned
12 | % rtn_type - if Chrom == [] and
13 | % rtn_type == 1 (or []) return boundaries
14 | % rtn_type == 2 return title
15 | % rtn_type == 3 return value of global minimum
16 | %
17 | % Output parameters:
18 | % ObjVal - Column vector containing the objective values of the
19 | % individuals in the current population.
20 | % if called with Chrom == [], then ObjVal contains
21 | % rtn_type == 1, matrix with the boundaries of the function
22 | % rtn_type == 2, text for the title of the graphic output
23 | % rtn_type == 3, value of global minimum
24 | %
25 | %
26 | % Author: Hartmut Pohlheim
27 | % History: 25.11.93 file created
28 | % 27.11.93 text of title and rtn_type added
29 | % 16.12.93 rtn_type == 3, return value of global minimum
30 | % 01.03.94 name changed in obj*
31 | % 15.01.03 updated for MATLAB v6 by Alex Shenfield
32 |
33 | function ObjVal = objsixh(Chrom,rtn_type);
34 |
35 | % Compute population parameters
36 | [Nind,Nvar] = size(Chrom);
37 |
38 | % Check size of Chrom and do the appropriate thing
39 | % if Chrom is [], then define size of boundary-matrix and values
40 | if Nind == 0
41 | % return text of title for graphic output
42 | if rtn_type == 2
43 | ObjVal = 'six-hump camelback function';
44 | % return value of global minimum
45 | elseif rtn_type == 3
46 | ObjVal = -1.0316;
47 | % define size of boundary-matrix and values
48 | else
49 | % x1 x2
50 | ObjVal = [-3 -2; % lower bounds
51 | 3 2]; % upper bounds
52 | end
53 | % if two variables, compute values of function
54 | elseif Nvar == 2
55 | % six-hump camelback function
56 | % -3 <= x1 <= 3 ; -2 <= x2 <= 2
57 | % global minimum at (x1,x2)=(-0.0898,0.7126),(0.0898,-0.7126) ; fmin=-1.0316
58 | x1 = Chrom(:,1);
59 | x2 = Chrom(:,2);
60 | ObjVal = (4-2.1*x1.^2+1/3*x1.^4).*x1.^2+x1.*x2+(-4+4*x2.^2).*x2.^2;
61 | % otherwise error, wrong format of Chrom
62 | else
63 | error('size of matrix Chrom is not correct for function evaluation');
64 | end
65 |
66 | % End of function
--------------------------------------------------------------------------------
/Test_fns/resplot.m:
--------------------------------------------------------------------------------
1 | % RESPLOT.M (RESult PLOTing)
2 | %
3 | % This function plots some of the results during computation.
4 | %
5 | % Syntax: resplot(Chrom,IndAll,ObjV,Best,gen)
6 | %
7 | % Input parameters:
8 | % Chrom - Matrix containing the chromosomes of the current
9 | % population. Each line corresponds to one individual.
10 | % IndAll - Matrix containing the best individual (variables) of each
11 | % generation. Each line corresponds to one individual.
12 | % ObjV - Vector containing objective values of the current
13 | % generation
14 | % Best - Matrix containing the best and average Objective values of each
15 | % generation, [best value per generation,average value per generation]
16 | % gen - Scalar containing the number of the current generation
17 | %
18 | % Output parameter:
19 | % no output parameter
20 | %
21 | % Author: Hartmut Pohlheim
22 | % History: 27.11.93 file created
23 | % 29.11.93 decision, if plot or not deleted
24 | % yscale not log
25 | % 15.12.93 MutMatrix as parameter and plot added
26 | % 16.03.94 function cleaned, MutMatrix removed, IndAll added
27 | % 15.01.03 tested under MATLAB v6 by Alex Shenfield
28 |
29 | function resplot(Chrom,IndAll,ObjV,Best,gen);
30 |
31 | % plot of best and mean value per generation
32 | subplot(2,2,1), plot(Best);
33 | title('Best and mean objective value');
34 | xlabel('generation'), ylabel('objective value');
35 |
36 | % plot of best individuals in all generations
37 | subplot(2,2,2), plot(IndAll);
38 | title(['Best individuals']);
39 | xlabel('generation'), ylabel('value of variable');
40 |
41 | % plot of variables of all individuals in current generation
42 | subplot(2,2,3), plot(Chrom');
43 | title(['All individuals in gen ',num2str(gen)]);
44 | xlabel('number of variable'), ylabel('value of variable');
45 |
46 | % plot of all objective values in current generation
47 | subplot(2,2,4), plot(ObjV,'y.');
48 | title(['All objective values']);
49 | xlabel('number of individual'), ylabel('objective value');
50 |
51 | drawnow;
52 |
53 |
54 | % End of function
--------------------------------------------------------------------------------
/Test_fns/sga.m:
--------------------------------------------------------------------------------
1 | % SGA.M (Simple Genetic Algorithm)
2 | %
3 | % This script implements the Simple Genetic Algorithm.
4 | % Binary representation for the individuals is used.
5 | %
6 | % Author: Hartmut Pohlheim
7 | % History: 23.03.94 file created
8 | % 15.01.03 tested under MATLAB v6 by Alex Shenfield
9 |
10 | NIND = 20; % Number of individuals per subpopulations
11 | MAXGEN = 300; % max Number of generations
12 | GGAP = .8; % Generation gap, how many new individuals are created
13 | SEL_F = 'sus'; % Name of selection function
14 | XOV_F = 'xovsp'; % Name of recombination function for individuals
15 | MUT_F = 'mut'; % Name of mutation function for individuals
16 | OBJ_F = 'objfun1'; % Name of function for objective values
17 |
18 | % Get boundaries of objective function
19 | FieldDR = feval(OBJ_F,[],1);
20 |
21 | % Number of variables of objective function, in OBJ_F defined
22 | NVAR = size(FieldDR,2);
23 |
24 | % Build fielddescription matrix
25 | PRECI = 20; % Precisicion of binary representation
26 | FieldDD = [rep([PRECI],[1, NVAR]);...
27 | FieldDR;...
28 | rep([1; 0; 1 ;1], [1, NVAR])];
29 |
30 | % Create population
31 | Chrom = crtbp(NIND, NVAR*PRECI);
32 |
33 | % reset count variables
34 | gen = 0;
35 | Best = NaN*ones(MAXGEN,1);
36 |
37 | % Iterate population
38 | while gen < MAXGEN,
39 |
40 | % Calculate objective function for population
41 | ObjV = feval(OBJ_F,bs2rv(Chrom, FieldDD));
42 | Best(gen+1) = min(ObjV);
43 | plot(log10(Best),'ro');
44 | drawnow;
45 |
46 | % Fitness assignement to whole population
47 | FitnV = ranking(ObjV);
48 |
49 | % Select individuals from population
50 | SelCh = select(SEL_F, Chrom, FitnV, GGAP);
51 |
52 | % Recombine selected individuals (crossover)
53 | SelCh=recombin(XOV_F, SelCh);
54 |
55 | % Mutate offspring
56 | SelCh=mutate(MUT_F, SelCh);
57 |
58 | % Insert offspring in population replacing parents
59 | Chrom = reins(Chrom, SelCh);
60 |
61 | gen=gen+1;
62 |
63 | end
64 |
65 | % End of script
--------------------------------------------------------------------------------
/Test_fns/simdopi1.m:
--------------------------------------------------------------------------------
1 | function [ret,x0,str]=simdopi1(t,x,u,flag);
2 | %SIMDOPI1 is the M-file description of the SIMULINK system named SIMDOPI1.
3 | % The block-diagram can be displayed by typing: SIMDOPI1.
4 | %
5 | % SYS=SIMDOPI1(T,X,U,FLAG) returns depending on FLAG certain
6 | % system values given time point, T, current state vector, X,
7 | % and input vector, U.
8 | % FLAG is used to indicate the type of output to be returned in SYS.
9 | %
10 | % Setting FLAG=1 causes SIMDOPI1 to return state derivatives, FLAG=2
11 | % discrete states, FLAG=3 system outputs and FLAG=4 next sample
12 | % time. For more information and other options see SFUNC.
13 | %
14 | % Calling SIMDOPI1 with a FLAG of zero:
15 | % [SIZES]=SIMDOPI1([],[],[],0), returns a vector, SIZES, which
16 | % contains the sizes of the state vector and other parameters.
17 | % SIZES(1) number of states
18 | % SIZES(2) number of discrete states
19 | % SIZES(3) number of outputs
20 | % SIZES(4) number of inputs.
21 | % For the definition of other parameters in SIZES, see SFUNC.
22 | % See also, TRIM, LINMOD, LINSIM, EULER, RK23, RK45, ADAMS, GEAR.
23 |
24 | % Note: This M-file is only used for saving graphical information;
25 | % after the model is loaded into memory an internal model
26 | % representation is used.
27 |
28 | % the system will take on the name of this mfile:
29 | sys = mfilename;
30 | new_system(sys)
31 | simver(1.2)
32 | if(0 == (nargin + nargout))
33 | set_param(sys,'Location',[100,100,600,400])
34 | open_system(sys)
35 | end;
36 | set_param(sys,'algorithm', 'RK-45')
37 | set_param(sys,'Start time', '0.0')
38 | set_param(sys,'Stop time', '1')
39 | set_param(sys,'Min step size', '0.001')
40 | set_param(sys,'Max step size', '0.01')
41 | set_param(sys,'Relative error','1e-3')
42 | set_param(sys,'Return vars', '')
43 |
44 | add_block('built-in/Inport',[sys,'/','Inport'])
45 | set_param([sys,'/','Inport'],...
46 | 'position',[65,95,85,115])
47 |
48 | add_block('built-in/Note',[sys,'/','Doppelintegrator'])
49 | set_param([sys,'/','Doppelintegrator'],...
50 | 'position',[225,10,230,15])
51 |
52 | add_block('built-in/Note',[sys,'/','Steuerung'])
53 | set_param([sys,'/','Steuerung'],...
54 | 'position',[75,65,80,70])
55 |
56 | add_block('built-in/Integrator',[sys,'/','Integrator1'])
57 | set_param([sys,'/','Integrator1'],...
58 | 'position',[175,95,195,115])
59 |
60 | add_block('built-in/Integrator',[sys,'/','Integrator2'])
61 | set_param([sys,'/','Integrator2'],...
62 | 'Initial','-1',...
63 | 'position',[280,95,300,115])
64 | add_line(sys,[90,105;165,105])
65 | add_line(sys,[200,105;270,105])
66 |
67 | % Return any arguments.
68 | if (nargin | nargout)
69 | % Must use feval here to access system in memory
70 | if (nargin > 3)
71 | if (flag == 0)
72 | eval(['[ret,x0,str]=',sys,'(t,x,u,flag);'])
73 | else
74 | eval(['ret =', sys,'(t,x,u,flag);'])
75 | end
76 | else
77 | [ret,x0,str] = feval(sys);
78 | end
79 | end
80 |
--------------------------------------------------------------------------------
/Test_fns/simdopi2.m:
--------------------------------------------------------------------------------
1 | % SIMDOPI2.M (Modell of DOPpelINTegrator, s-function)
2 | %
3 | % This function implements the modell of the DOPPELINTEGRATOR.
4 | %
5 | % Syntax: [sys, x0] = simdopi2(t, x, u, flag)
6 | %
7 | % Input parameters:
8 | % t - given time point
9 | % x - current state vector
10 | % u - input vector
11 | % flag - flags
12 | %
13 | % Output parameters:
14 | % sys - Vector containing the new state derivatives
15 | % x0 - initial value
16 |
17 | % Author: Hartmut Pohlheim
18 | % History: 17.12.93 file created
19 |
20 | function [sys, x0] = simdopi2(t, x, u, flag);
21 |
22 | % Linear Systems Description
23 |
24 | if abs(flag) == 1
25 | sys(1) = u(1); % Derivatives
26 | sys(2) = x(1); % Derivatives
27 | elseif abs(flag) == 0
28 | sys=[2,0,0,1,0,0]; x0 = [0; -1];
29 | else
30 | sys = []; % Real time update (ignored).
31 | end
32 |
33 |
34 | % End of function
35 |
--------------------------------------------------------------------------------
/Test_fns/simlinq1.m:
--------------------------------------------------------------------------------
1 | function [ret,x0,str]=simlinq1(t,x,u,flag);
2 | %SIMLINQ1 is the M-file description of the SIMULINK system named SIMLINQ1.
3 | % The block-diagram can be displayed by typing: SIMLINQ1.
4 | %
5 | % SYS=SIMLINQ1(T,X,U,FLAG) returns depending on FLAG certain
6 | % system values given time point, T, current state vector, X,
7 | % and input vector, U.
8 | % FLAG is used to indicate the type of output to be returned in SYS.
9 | %
10 | % Setting FLAG=1 causes SIMLINQ1 to return state derivatives, FLAG=2
11 | % discrete states, FLAG=3 system outputs and FLAG=4 next sample
12 | % time. For more information and other options see SFUNC.
13 | %
14 | % Calling SIMLINQ1 with a FLAG of zero:
15 | % [SIZES]=SIMLINQ1([],[],[],0), returns a vector, SIZES, which
16 | % contains the sizes of the state vector and other parameters.
17 | % SIZES(1) number of states
18 | % SIZES(2) number of discrete states
19 | % SIZES(3) number of outputs
20 | % SIZES(4) number of inputs.
21 | % For the definition of other parameters in SIZES, see SFUNC.
22 | % See also, TRIM, LINMOD, LINSIM, EULER, RK23, RK45, ADAMS, GEAR.
23 |
24 | % Note: This M-file is only used for saving graphical information;
25 | % after the model is loaded into memory an internal model
26 | % representation is used.
27 |
28 | % the system will take on the name of this mfile:
29 | sys = mfilename;
30 | new_system(sys)
31 | simver(1.2)
32 | if(0 == (nargin + nargout))
33 | set_param(sys,'Location',[208,245,596,426])
34 | open_system(sys)
35 | end;
36 | set_param(sys,'algorithm', 'RK-45')
37 | set_param(sys,'Start time', '0.0')
38 | set_param(sys,'Stop time', '1')
39 | set_param(sys,'Min step size', '0.05')
40 | set_param(sys,'Max step size', '0.05')
41 | set_param(sys,'Relative error','1e-3')
42 | set_param(sys,'Return vars', '')
43 |
44 | add_block('built-in/Sum',[sys,'/','Sum'])
45 | set_param([sys,'/','Sum'],...
46 | 'position',[110,65,130,85])
47 |
48 | add_block('built-in/Integrator',[sys,'/','Integrator'])
49 | set_param([sys,'/','Integrator'],...
50 | 'Initial','100',...
51 | 'position',[180,65,200,85])
52 |
53 | add_block('built-in/Inport',[sys,'/','Inport'])
54 | set_param([sys,'/','Inport'],...
55 | 'position',[25,70,45,90])
56 | add_line(sys,[135,75;170,75])
57 | add_line(sys,[205,75;205,40;90,40;90,70;100,70])
58 | add_line(sys,[50,80;100,80])
59 |
60 | % Return any arguments.
61 | if (nargin | nargout)
62 | % Must use feval here to access system in memory
63 | if (nargin > 3)
64 | if (flag == 0)
65 | eval(['[ret,x0,str]=',sys,'(t,x,u,flag);'])
66 | else
67 | eval(['ret =', sys,'(t,x,u,flag);'])
68 | end
69 | else
70 | [ret,x0,str] = feval(sys);
71 | end
72 | end
--------------------------------------------------------------------------------
/Test_fns/simlinq2.m:
--------------------------------------------------------------------------------
1 | % SIMLINQ2.M (Model of Linear Quadratic Problem, s-function)
2 | %
3 | % This function implements the model of the Linear Quadratic Problem.
4 | %
5 | % Syntax: [sys, x0] = simlinq2(t, x, u, flag)
6 | %
7 | % Input parameters:
8 | % t - given time point
9 | % x - current state vector
10 | % u - input vector
11 | % flag - flags
12 | %
13 | % Output parameters:
14 | % sys - Vector containing the new state derivatives
15 | % x0 - initial value
16 | %
17 | % Author: Hartmut Pohlheim
18 | % History: 23.03.94 file created
19 |
20 | function [sys, x0] = simlinq2(t, x, u, flag);
21 |
22 | % Linear Systems Description
23 |
24 | if abs(flag) == 1
25 | sys(1) = u(1) + x(1); % Derivatives
26 | elseif abs(flag) == 0
27 | sys=[1,0,0,1,0,0]; x0 = [100];
28 | else
29 | sys = []; % Real time update (ignored).
30 | end
31 |
32 |
33 | % End of function
--------------------------------------------------------------------------------
/Test_fns/simobjp.m:
--------------------------------------------------------------------------------
1 | % SIMOBJP.M (Plot SIMulation results of OBJective function)
2 | %
3 | % This function takes the name of a simulation objective
4 | % function and the matrix of the best individuals and
5 | % plots the states of the system over time for the
6 | % last best individual.
7 | %
8 | % Author: Hartmut Pohlheim
9 | % History: 25.03.94 file created
10 |
11 | function [val, t, x] = simobjp(OBJ_F, IndAll);
12 |
13 | BestInd1=IndAll(size(IndAll,1),:);
14 |
15 | [val t x] = feval(OBJ_F, BestInd1);
16 |
17 | set(gcf,'Name',feval(OBJ_F,[],2));
18 | plot(t, x);
19 |
20 | % End of function
--------------------------------------------------------------------------------
/bs2rv.m:
--------------------------------------------------------------------------------
1 | % BS2RV.m - Binary string to real vector
2 | %
3 | % This function decodes binary chromosomes into vectors of reals. The
4 | % chromosomes are seen as the concatenation of binary strings of given
5 | % length, and decoded into real numbers in a specified interval using
6 | % either standard binary or Gray decoding.
7 | %
8 | % Syntax: Phen = bs2rv(Chrom,FieldD)
9 | %
10 | % Input parameters:
11 | %
12 | % Chrom - Matrix containing the chromosomes of the current
13 | % population. Each line corresponds to one
14 | % individual's concatenated binary string
15 | % representation. Leftmost bits are MSb and
16 | % rightmost are LSb.
17 | %
18 | % FieldD - Matrix describing the length and how to decode
19 | % each substring in the chromosome. It has the
20 | % following structure:
21 | %
22 | % [len; (num)
23 | % lb; (num)
24 | % ub; (num)
25 | % code; (0=binary | 1=gray)
26 | % scale; (0=arithmetic | 1=logarithmic)
27 | % lbin; (0=excluded | 1=included)
28 | % ubin]; (0=excluded | 1=included)
29 | %
30 | % where
31 | % len - row vector containing the length of
32 | % each substring in Chrom. sum(len)
33 | % should equal the individual length.
34 | % lb,
35 | % ub - Lower and upper bounds for each
36 | % variable.
37 | % code - binary row vector indicating how each
38 | % substring is to be decoded.
39 | % scale - binary row vector indicating where to
40 | % use arithmetic and/or logarithmic
41 | % scaling.
42 | % lbin,
43 | % ubin - binary row vectors indicating whether
44 | % or not to include each bound in the
45 | % representation range
46 | %
47 | % Output parameter:
48 | %
49 | % Phen - Real matrix containing the population phenotypes.
50 | %
51 | % Author: Carlos Fonseca, Updated: Andrew Chipperfield,
52 | % Date: 08/06/93, Date: 26-Jan-94,
53 | %
54 | % Tested under MATLAB v6 by Alex Shenfield (17-Jan-03)
55 |
56 | function Phen = bs2rv(Chrom,FieldD)
57 |
58 | % Identify the population size (Nind)
59 | % and the chromosome length (Lind)
60 | [Nind,Lind] = size(Chrom);
61 |
62 | % Identify the number of decision variables (Nvar)
63 | [seven,Nvar] = size(FieldD);
64 |
65 | if seven ~= 7
66 | error('FieldD must have 7 rows.');
67 | end
68 |
69 | % Get substring properties
70 | len = FieldD(1,:);
71 | lb = FieldD(2,:);
72 | ub = FieldD(3,:);
73 | code = ~(~FieldD(4,:));
74 | scale = ~(~FieldD(5,:));
75 | lin = ~(~FieldD(6,:));
76 | uin = ~(~FieldD(7,:));
77 |
78 | % Check substring properties for consistency
79 | if sum(len) ~= Lind,
80 | error('Data in FieldD must agree with chromosome length');
81 | end
82 |
83 | if ~all(lb(scale).*ub(scale)>0)
84 | error('Log-scaled variables must not include 0 in their range');
85 | end
86 |
87 | % Decode chromosomes
88 | Phen = zeros(Nind,Nvar);
89 |
90 | lf = cumsum(len);
91 | li = cumsum([1 len]);
92 | Prec = .5 .^ len;
93 |
94 | logsgn = sign(lb(scale));
95 | lb(scale) = log( abs(lb(scale)) );
96 | ub(scale) = log( abs(ub(scale)) );
97 | delta = ub - lb;
98 |
99 | Prec = .5 .^ len;
100 | num = (~lin) .* Prec;
101 | den = (lin + uin - 1) .* Prec;
102 |
103 | for i = 1:Nvar,
104 | idx = li(i):lf(i);
105 | if code(i) % Gray decoding
106 | Chrom(:,idx)=rem(cumsum(Chrom(:,idx)')',2);
107 | end
108 | Phen(:,i) = Chrom(:,idx) * [ (.5).^(1:len(i))' ];
109 | Phen(:,i) = lb(i) + delta(i) * (Phen(:,i) + num(i)) ./ (1 - den(i));
110 | end
111 |
112 | expand = ones(Nind,1);
113 | if any(scale)
114 | Phen(:,scale) = logsgn(expand,:) .* exp(Phen(:,scale));
115 | end
--------------------------------------------------------------------------------
/contents.m:
--------------------------------------------------------------------------------
1 | % Genetic Algorithm Toolbox.
2 | % Version 1.3 17-Jan-2003
3 | % Department of Automatic Control and Systems Engineering
4 | % University of Sheffield, England
5 | %
6 | % Creating populations
7 | % crtbase - create a base vector
8 | % crtbp - create a binary population
9 | % crtrp - create a real-valued population
10 | %
11 | % Fitness assignment
12 | % ranking - rank-based fitness assignment
13 | % scaling - proportional fitness-scaling
14 | %
15 | % Selection and reinsertion
16 | % reins - uniform random and fitness-based reinsertion
17 | % rws - roulette wheel selection
18 | % select - high-level selection routine
19 | % sus - stochastic universal sampling
20 | %
21 | % Mutation operators
22 | % mut - discrete mutation
23 | % mutate - high-level mutation function
24 | % mutbga - real-value mutation
25 | %
26 | % Crossover operators
27 | % recdis - discrete recombination
28 | % recint - intermediate recombination
29 | % reclin - line recombination
30 | % recmut - line recombination with mutation features
31 | % recombin - high-level recombination function
32 | % xovdp - double-point crossover
33 | % xovdprs - double-point reduced surrogate crossover
34 | % xovmp - general multi-point crossover
35 | % xovsh - shuffle crossover
36 | % xovshrs - shuffle reduced surrogate crossover
37 | % xovsp - single-point crossover
38 | % xovsprs - single-point reduced surrogate crossover
39 | %
40 | % Subpopulation support
41 | % migrate - exchange individuals between subpopulations
42 | %
43 | %
44 | % Utility functions
45 | % bs2rv - binary string to real-value conversion
46 | % rep - matrix replication
47 | %
48 | % Demonstration and other functions
49 | % mpga - multi-population genetic algorithm demonstration
50 | % objfun1 - De Jongs first test function (used by sga)
51 | % objharv - harvest function (used in mpga)
52 | % resplot - result plotting (used in mpga)
53 | % sga - simple genetic algorithm demonstration
--------------------------------------------------------------------------------
/crtbase.m:
--------------------------------------------------------------------------------
1 | % CRTBASE.m - Create base vector
2 | %
3 | % This function creates a vector containing the base of the loci
4 | % in a chromosome.
5 | %
6 | % Syntax: BaseVec = crtbase(Lind, Base)
7 | %
8 | % Input Parameters:
9 | %
10 | % Lind - A scalar or vector containing the lengths
11 | % of the alleles. Sum(Lind) is the length of
12 | % the corresponding chromosome.
13 | %
14 | % Base - A scalar or vector containing the base of
15 | % the loci contained in the Alleles.
16 | %
17 | % Output Parameters:
18 | %
19 | % BaseVec - A vector whose elements correspond to the base
20 | % of the loci of the associated chromosome structure.
21 | %
22 | % Author: Andrew Chipperfield
23 | % Date: 19-Jan-94
24 | %
25 | % Tested under MATLAB v6 by Alex Shenfield (17-Jan-03)
26 |
27 | function BaseVec = crtbase(Lind, Base)
28 |
29 | [ml LenL] = size(Lind) ;
30 | if nargin < 2
31 | Base = 2 * ones(LenL,1) ; % default to base 2
32 | end
33 | [mb LenB] = size(Base) ;
34 |
35 | % check parameter consistency
36 | if ml > 1 | mb > 1
37 | error( 'Lind or Base is not a vector') ;
38 | elseif (LenL > 1 & LenB > 1 & LenL ~= LenB) | (LenL == 1 & LenB > 1 )
39 | error( 'Vector dimensions must agree' ) ;
40 | elseif LenB == 1 & LenL > 1
41 | Base = Base * ones(LenL,1) ;
42 | end
43 |
44 | BaseVec = [] ;
45 | for i = 1:LenL
46 | BaseVec = [BaseVec, Base(i)*ones(Lind(i),1)'];
47 | end
--------------------------------------------------------------------------------
/crtbp.m:
--------------------------------------------------------------------------------
1 | % CRTBP.m - Create an initial population
2 | %
3 | % This function creates a binary population of given size and structure.
4 | %
5 | % Syntax: [Chrom Lind BaseV] = crtbp(Nind, Lind, Base)
6 | %
7 | % Input Parameters:
8 | %
9 | % Nind - Either a scalar containing the number of individuals
10 | % in the new population or a row vector of length two
11 | % containing the number of individuals and their length.
12 | %
13 | % Lind - A scalar containing the length of the individual
14 | % chromosomes.
15 | %
16 | % Base - A scalar containing the base of the chromosome
17 | % elements or a row vector containing the base(s)
18 | % of the loci of the chromosomes.
19 | %
20 | % Output Parameters:
21 | %
22 | % Chrom - A matrix containing the random valued chromosomes
23 | % row wise.
24 | %
25 | % Lind - A scalar containing the length of the chromosome.
26 | %
27 | % BaseV - A row vector containing the base of the
28 | % chromosome loci.
29 | %
30 | % Author: Andrew Chipperfield
31 | % Date: 19-Jan-94
32 | %
33 | % Tested under MATLAB v6 by Alex Shenfield (20-Jan-03)
34 |
35 | function [Chrom, Lind, BaseV] = crtbp(Nind, Lind, Base)
36 | nargs = nargin ;
37 |
38 | % Check parameter consistency
39 |
40 | if nargs >= 1, [mN, nN] = size(Nind) ; end
41 | if nargs >= 2, [mL, nL] = size(Lind) ; end
42 | if nargs == 3, [mB, nB] = size(Base) ; end
43 |
44 | if nN == 2
45 | if (nargs == 1)
46 | Lind = Nind(2) ; Nind = Nind(1) ; BaseV = crtbase(Lind) ;
47 | elseif (nargs == 2 & nL == 1)
48 | BaseV = crtbase(Nind(2),Lind) ; Lind = Nind(2) ; Nind = Nind(1) ;
49 | elseif (nargs == 2 & nL > 1)
50 | if Lind ~= length(Lind), error('Lind and Base disagree'); end
51 | BaseV = Lind ; Lind = Nind(2) ; Nind = Nind(1) ;
52 | end
53 | elseif nN == 1
54 | if nargs == 2
55 | if nL == 1, BaseV = crtbase(Lind) ;
56 | else, BaseV = Lind ; Lind = nL ; end
57 | elseif nargs == 3
58 | if nB == 1, BaseV = crtbase(Lind,Base) ;
59 | elseif nB ~= Lind, error('Lind and Base disagree') ;
60 | else BaseV = Base ; end
61 | end
62 | else
63 | error('Input parameters inconsistent') ;
64 | end
65 |
66 | % Create a structure of random chromosomes in row wise order, dimensions
67 | % Nind by Lind. The base of each chromosomes loci is given by the value
68 | % of the corresponding element of the row vector base.
69 |
70 | Chrom = floor(rand(Nind,Lind).*BaseV(ones(Nind,1),:)) ;
71 |
72 |
73 | % End of file
--------------------------------------------------------------------------------
/crtrp.m:
--------------------------------------------------------------------------------
1 | % CRTRP.M (CReaTe an initial (Real-value) Population)
2 | %
3 | % This function creates a population of given size of random real-values.
4 | %
5 | % Syntax: Chrom = crtrp(Nind,FieldDR);
6 | %
7 | % Input parameters:
8 | % Nind - A scalar containing the number of individuals in the new
9 | % population.
10 | %
11 | % FieldDR - A matrix of size 2 by number of variables describing the
12 | % boundaries of each variable. It has the following structure:
13 | % [lower_bound; (vector with lower bound for each veriable)
14 | % upper_bound] (vector with upper bound for each veriable)
15 | % [lower_bound_var_1 lower_bound_var_2 ... lower_bound_var_Nvar;
16 | % upper_bound_var_1 upper_bound_var_2 ... upper_bound_var_Nvar]
17 | % example - each individuals consists of 4 variables:
18 | % FieldDR = [-100 -50 -30 -20; % lower bound
19 | % 100 50 30 20] % upper bound
20 | %
21 | % Output parameter:
22 | % Chrom - A matrix containing the random valued individuals of the
23 | % new population of size Nind by number of variables.
24 | %
25 | % Author: Hartmut Pohlheim
26 | % History: 23.11.93 file created
27 | % 25.02.94 clean up, check parameter consistency
28 | % 20.01.03 tested under MATLAB v6 by Alex Shenfield
29 |
30 | function Chrom = crtrp(Nind,FieldDR);
31 |
32 | % Check parameter consistency
33 | if nargin < 2, error('parameter FieldDR missing'); end
34 | if nargin > 2, nargin = 2; end
35 |
36 | [mN, nN] = size(Nind);
37 | [mF, Nvar] = size(FieldDR);
38 |
39 | if (mN ~= 1 & nN ~= 1), error('Nind has to be a scalar'); end
40 | if mF ~= 2, error('FieldDR must be a matrix with 2 rows'); end
41 |
42 | % Compute Matrix with Range of variables and Matrix with Lower value
43 | Range = rep((FieldDR(2,:)-FieldDR(1,:)),[Nind 1]);
44 | Lower = rep(FieldDR(1,:), [Nind 1]);
45 |
46 | % Create initial population
47 | % Each row contains one individual, the values of each variable uniformly
48 | % distributed between lower and upper bound (given by FieldDR)
49 | Chrom = rand(Nind,Nvar) .* Range + Lower;
50 |
51 |
52 | % End of function
--------------------------------------------------------------------------------
/migrate.m:
--------------------------------------------------------------------------------
1 | % MIGRATE.M (MIGRATion of individuals between subpopulations)
2 | %
3 | % This function performs migration of individuals.
4 | %
5 | % Syntax: [Chrom, ObjV] = migrate(Chrom, SUBPOP, MigOpt, ObjV)
6 | %
7 | % Input parameters:
8 | % Chrom - Matrix containing the individuals of the current
9 | % population. Each row corresponds to one individual.
10 | % SUBPOP - Number of subpopulations
11 | % MigOpt - (optional) Vector containing migration parameters
12 | % MigOpt(1): MIGR - Rate of individuals to be migrated per
13 | % subpopulation (% of subpopulation)
14 | % if omitted or NaN, 0.2 (20%) is assumed
15 | % MigOpt(2): Select - number indicating the selection method
16 | % of replacing individuals
17 | % 0 - uniform selection
18 | % 1 - fitness-based selection (replace worst
19 | % individuals)
20 | % if omitted or NaN, 0 is assumed
21 | % MigOpt(3): Structure - number indicating the structure
22 | % of the subpopulations for migration
23 | % 0 - net structure (unconstrained migration)
24 | % 1 - neighbourhood structure
25 | % 2 - ring structure
26 | % if omitted or NaN, 0 is assumed
27 | % ObjV - (optional) Column vector containing the objective values
28 | % of the individuals in the current population, needed for
29 | % fitness-based migration, this saves the
30 | % recalculation of objective values for population.
31 | %
32 | % Output parameters:
33 | % Chrom - Matrix containing the individuals of the current
34 | % population after migration.
35 | % ObjV - if ObjV is input parameter, than column vector containing
36 | % the objective values of the individuals of the current
37 | % generation after migration.
38 | %
39 | % Author: Hartmut Pohlheim
40 | % History: 16.02.94 file created
41 | % 18.02.94 comments at the beginning added
42 | % exchange of ObjV too
43 | % 25.02.94 clean up
44 | % 26.02.94 ObjV optional input parameter
45 | % Select and Structure added, parameter reordered
46 | % 17.03.94 renamed to migrate.m, more parameter checks
47 | % 20.01.03 tested under MATLAB v6 by Alex Shenfield
48 |
49 | function [Chrom, ObjV] = migrate(Chrom, SUBPOP, MigOpt, ObjV);
50 |
51 |
52 | % Check parameter consistency
53 | if nargin < 2, error('Input parameter SUBPOP missing'); end
54 | if (nargout == 2 & nargin < 4), error('Input parameter ObjV missing'); end
55 |
56 | [Nind, Nvar] = size(Chrom);
57 | if length(SUBPOP) ~= 1, error('SUBPOP must be a scalar'); end
58 | if SUBPOP == 1, return; end
59 | if (Nind/SUBPOP) ~= fix(Nind/SUBPOP), error('Chrom and SUBPOP disagree'); end
60 | NIND = Nind/SUBPOP; % Compute number of individuals per subpopulation
61 |
62 | if nargin > 3,
63 | [mO, nO] = size(ObjV);
64 | if nO ~= 1, error('ObjV must be a column vector'); end
65 | if Nind ~= mO, error('Chrom and ObjV disagree'); end
66 | IsObjV = 1;
67 | else IsObjV = 0; ObjV = [];
68 | end
69 |
70 | if nargin < 3, MIGR = 0.2; Select = 0; Structure = 0; end
71 | if nargin > 2,
72 | if isempty(MigOpt), MIGR = 0.2; Select = 0; Structure = 0;
73 | elseif isnan(MigOpt), MIGR = 0.2; Select = 0; Structure = 0;
74 | else
75 | MIGR = NaN; Select = NaN; Structure = NaN;
76 | if length(MigOpt) > 3, error('Parameter MigOpt is too long'); end
77 | if length(MigOpt) >= 1, MIGR = MigOpt(1); end
78 | if length(MigOpt) >= 2, Select = MigOpt(2); end
79 | if length(MigOpt) >= 3, Structure = MigOpt(3); end
80 | if isnan(MIGR), MIGR =0.2; end
81 | if isnan(Select), Select = 0; end
82 | if isnan(Structure), Structure = 0; end
83 | end
84 | end
85 |
86 | if (MIGR < 0 | MIGR > 1), error('Parameter for migration rate must be a scalar in [0 1]'); end
87 | if (Select ~= 0 & Select ~= 1), error('Parameter for selection method must be 0 or 1'); end
88 | if (Structure < 0 | Structure > 2), error ('Parameter for structure must be 0, 1 or 2'); end
89 | if (Select == 1 & IsObjV == 0), error('ObjV for fitness-based migration needed');end
90 |
91 | if MIGR == 0, return; end
92 | MigTeil = max(floor(NIND * MIGR), 1); % Number of individuals to migrate
93 |
94 | % Perform migration between subpopulations --> create a matrix for migration
95 | % in every subpopulation from best individuals of the other subpopulations
96 |
97 | % Clear storing matrices
98 | ChromMigAll = [];
99 | if IsObjV == 1, ObjVAll = []; end
100 |
101 | % Create matrix with best/uniform individuals of all subpopulations
102 | for irun = 1:SUBPOP
103 | % sort ObjV of actual subpopulation
104 | if Select == 1, % fitness-based selection
105 | [Dummy, IndMigSo]=sort(ObjV((irun-1)*NIND+1:irun*NIND));
106 | else % if Select == 0 % uniform selection
107 | [Dummy, IndMigSo]=sort(rand(NIND, 1));
108 | end
109 | % take MigTeil (best) individuals, copy individuals and objective values
110 | IndMigTeil=IndMigSo(1:MigTeil)+(irun-1)*NIND;
111 | ChromMigAll = [ChromMigAll; Chrom(IndMigTeil,:)];
112 | if IsObjV == 1, ObjVAll = [ObjVAll; ObjV(IndMigTeil,:)]; end
113 | end
114 |
115 | % perform migration
116 | for irun = 1:SUBPOP
117 | ChromMig = ChromMigAll;
118 | if IsObjV == 1, ObjVMig = ObjVAll; end
119 | if Structure == 1, % neighbourhood
120 | % select individuals of neighbourhood subpopulations for ChromMig and ObjVMig
121 | popnum = [SUBPOP 1:SUBPOP 1];
122 | ins1 = popnum(irun); ins2 = popnum(irun + 2);
123 | InsRows = [(ins1-1)*MigTeil+1:ins1*MigTeil (ins2-1)*MigTeil+1:ins2*MigTeil];
124 | ChromMig = ChromMig(InsRows,:);
125 | if IsObjV == 1, ObjVMig = ObjVMig(InsRows,:); end
126 | elseif Structure == 2, % ring
127 | % select individuals of actual-1 subpopulation for ChromMig and ObjVMig
128 | popnum = [SUBPOP 1:SUBPOP 1];
129 | ins1 = popnum(irun);
130 | InsRows = (ins1-1)*MigTeil+1:ins1*MigTeil;
131 | ChromMig = ChromMig(InsRows,:);
132 | if IsObjV == 1, ObjVMig = ObjVMig(InsRows,:); end
133 | else % if Structure == 0, % complete net
134 | % delete individuals of actual subpopulation from ChromMig and ObjVMig
135 | DelRows = (irun-1)*MigTeil+1:irun*MigTeil;
136 | ChromMig(DelRows,:) = [];
137 | if IsObjV == 1, ObjVMig(DelRows,:) = []; end
138 | end
139 | % Create an index from a sorted vector with random numbers
140 | [Dummy,IndMigRa]=sort(rand(size(ChromMig,1),1));
141 | % Take MigTeil numbers from the random vector
142 | IndMigN=IndMigRa((1:MigTeil)');
143 | % copy MigTeil individuals into Chrom and ObjV
144 | Chrom((1:MigTeil)+(irun-1)*NIND,:) = ChromMig(IndMigN,:);
145 | if IsObjV == 1, ObjV((1:MigTeil)+(irun-1)*NIND,:) = ObjVMig(IndMigN,:); end
146 | end
147 |
148 |
149 | % End of function
--------------------------------------------------------------------------------
/mpga.m:
--------------------------------------------------------------------------------
1 | % MPGA.M Multi Population Genetic Algorithm
2 | %
3 | % This script implements the Multi Population Genetic Algorithm.
4 | % A real-valued representation of the individuals is used.
5 | %
6 | % Author: Andrew Chipperfield
7 | % History: 30-Mar-94 file created
8 | % 21-Jan-03 tested under MATLAB v6 by Alex Shenfield
9 |
10 | NVAR = 20; % No. of decision variables (control steps)
11 | RANGE = [0;200]; % Bounds on decision variables
12 |
13 | % Set field descriptor
14 | FieldD = rep(RANGE,[1,NVAR]);
15 |
16 | % Define GA Parameters
17 | GGAP = .8; % Generation gap, how many new individuals are created
18 | XOVR = 1; % Crossover rate
19 | MUTR = 1/NVAR; % Mutation rate depending on NVAR
20 | MAXGEN = 1200; % Maximum number of generations
21 | TERMEXACT = 1e-4; % Value for termination if minimum reached
22 | INSR = .9; % Insertion rate, how many of the offspring are inserted
23 | SUBPOP = 8; % Number of subpopulations
24 | MIGR = 0.2; % Migration rate between subpopulations
25 | MIGGEN = 20; % Number of generations between migration
26 | NIND = 20; % Number of individuals per subpopulation
27 |
28 | % Specify other routines as strings
29 | SEL_F = 'sus'; % Name of selection function
30 | XOV_F = 'recdis'; % Name of recombination function for individuals
31 | MUT_F = 'mutbga'; % Name of mutation function
32 | OBJ_F = 'objharv'; % Name of function for objective values
33 |
34 | % Get value of minimum, defined in objective function
35 | GlobalMin = feval(OBJ_F,[],3);
36 |
37 | % Get title of objective function, defined in objective function
38 | FigTitle = [feval(OBJ_F,[],2) ' (' int2str(SUBPOP) ':' int2str(MAXGEN) ') '];
39 |
40 | % Clear Best and storing matrix
41 | % Initialise Matrix for storing best results
42 | Best = NaN * ones(MAXGEN,3);
43 | Best(:,3) = zeros(size(Best,1),1);
44 | % Matrix for storing best individuals
45 | IndAll = [];
46 |
47 | % Create real population
48 | Chrom = crtrp(SUBPOP*NIND,FieldD);
49 |
50 | % reset count variables
51 | gen = 0;
52 |
53 | % Calculate objective function for population
54 | ObjV = feval(OBJ_F,Chrom);
55 | % count number of objective function evaluations
56 | Best(gen+1,3) = Best(gen+1,3) + NIND;
57 |
58 | % Generational loop
59 | while gen < MAXGEN,
60 |
61 | % Save the best and average objective values and the best individual
62 | [Best(gen+1,1),ix] = min(ObjV);
63 | Best(gen+1,2) = mean(ObjV);
64 | IndAll = [IndAll; Chrom(ix,:)];
65 |
66 | % Fitness assignment to whole population
67 | FitnV = ranking(ObjV,2,SUBPOP);
68 |
69 | % Select individuals from population
70 | SelCh = select(SEL_F, Chrom, FitnV, GGAP, SUBPOP);
71 |
72 | % Recombine selected individuals
73 | SelCh=recombin(XOV_F, SelCh, XOVR, SUBPOP);
74 |
75 | % Mutate offspring
76 | SelCh=mutate(MUT_F, SelCh, FieldD, [MUTR], SUBPOP);
77 |
78 | % Calculate objective function for offsprings
79 | ObjVOff = feval(OBJ_F,SelCh);
80 | Best(gen+1,3) = Best(gen+1,3) + size(SelCh,1);
81 |
82 | % Insert best offspring in population replacing worst parents
83 | [Chrom, ObjV] = reins(Chrom, SelCh, SUBPOP, [1 INSR], ObjV, ObjVOff);
84 |
85 | gen=gen+1;
86 |
87 | % Plot some results, rename title of figure for graphic output
88 | if ((rem(gen,20) == 1) | (rem(gen,MAXGEN) == 0)),
89 | set(gcf,'Name',[FigTitle ' in ' int2str(gen)]);
90 | resplot(Chrom(1:2:size(Chrom,1),:),...
91 | IndAll(max(1,gen-39):size(IndAll,1),:),...
92 | [ObjV; GlobalMin], Best(max(1,gen-19):gen,[1 2]), gen);
93 | end
94 |
95 | % migrate individuals between subpopulations
96 | if (rem(gen,MIGGEN) == 0)
97 | [Chrom, ObjV] = migrate(Chrom, SUBPOP, [MIGR, 1, 0], ObjV);
98 | end
99 |
100 | end
101 |
102 | % Results
103 | % add number of objective function evaluations
104 | Results = cumsum(Best(1:gen,3));
105 | % number of function evaluation, mean and best results
106 | Results = [Results Best(1:gen,2) Best(1:gen,1)];
107 |
108 | % Plot Results and show best individuals => optimum
109 | figure('Name',['Results of ' FigTitle]);
110 | subplot(2,1,1), plot(Results(:,1),Results(:,2),'-',Results(:,1),Results(:,3),':');
111 | subplot(2,1,2), plot(IndAll(gen-4:gen,:)');
112 |
113 | % End of script
--------------------------------------------------------------------------------
/mut.m:
--------------------------------------------------------------------------------
1 | % MUT.m
2 | %
3 | % This function takes the representation of the current population,
4 | % mutates each element with given probability and returns the resulting
5 | % population.
6 | %
7 | % Syntax: NewChrom = mut(OldChrom,Pm,BaseV)
8 | %
9 | % Input parameters:
10 | %
11 | % OldChrom - A matrix containing the chromosomes of the
12 | % current population. Each row corresponds to
13 | % an individuals string representation.
14 | %
15 | % Pm - Mutation probability (scalar). Default value
16 | % of Pm = 0.7/Lind, where Lind is the chromosome
17 | % length is assumed if omitted.
18 | %
19 | % BaseV - Optional row vector of the same length as the
20 | % chromosome structure defining the base of the
21 | % individual elements of the chromosome. Binary
22 | % representation is assumed if omitted.
23 | %
24 | % Output parameter:
25 | %
26 | % NewChrom - A Matrix containing a mutated version of
27 | % OldChrom.
28 | %
29 | % Author: Andrew Chipperfield
30 | % Date: 25-Jan-94
31 | %
32 | % Tested under MATLAB v6 by Alex Shenfield (21-Jan-03)
33 |
34 | function NewChrom = mut(OldChrom,Pm,BaseV)
35 |
36 | % get population size (Nind) and chromosome length (Lind)
37 | [Nind, Lind] = size(OldChrom) ;
38 |
39 | % check input parameters
40 | if nargin < 2, Pm = 0.7/Lind ; end
41 | if isnan(Pm), Pm = 0.7/Lind; end
42 |
43 | if (nargin < 3), BaseV = crtbase(Lind); end
44 | if (isnan(BaseV)), BaseV = crtbase(Lind); end
45 | if (isempty(BaseV)), BaseV = crtbase(Lind); end
46 |
47 | if (nargin == 3) & (Lind ~= length(BaseV))
48 | error('OldChrom and BaseV are incompatible'), end
49 |
50 | % create mutation mask matrix
51 | BaseM = BaseV(ones(Nind,1),:) ;
52 |
53 | % perform mutation on chromosome structure
54 | NewChrom = rem(OldChrom+(rand(Nind,Lind) 4,
63 | if isempty(SUBPOP), SUBPOP = 1;
64 | elseif isnan(SUBPOP), SUBPOP = 1;
65 | elseif length(SUBPOP) ~= 1, error('SUBPOP must be a scalar'); end
66 | end
67 |
68 | if (Nind/SUBPOP) ~= fix(Nind/SUBPOP), error('OldChrom and SUBPOP disagree'); end
69 | Nind = Nind/SUBPOP; % Compute number of individuals per subpopulation
70 |
71 | % Select individuals of one subpopulation and call low level function
72 | NewChrom = [];
73 | for irun = 1:SUBPOP,
74 | ChromSub = OldChrom((irun-1)*Nind+1:irun*Nind,:);
75 | if IsDiscret == 1, NewChromSub = feval(MUT_F, ChromSub, MutOpt, FieldDR);
76 | elseif IsDiscret == 0, NewChromSub = feval(MUT_F, ChromSub, FieldDR, MutOpt); end
77 | NewChrom=[NewChrom; NewChromSub];
78 | end
79 |
80 |
81 | % End of function
--------------------------------------------------------------------------------
/mutbga.m:
--------------------------------------------------------------------------------
1 | % MUTBGA.M (real-value MUTation like Breeder Genetic Algorithm)
2 | %
3 | % This function takes a matrix OldChrom containing the real
4 | % representation of the individuals in the current population,
5 | % mutates the individuals with probability MutR and returns
6 | % the resulting population.
7 | %
8 | % This function implements the mutation operator of the Breeder Genetic
9 | % Algorithm. (Muehlenbein et. al.)
10 | %
11 | % Syntax: NewChrom = mutbga(OldChrom, FieldDR, MutOpt)
12 | %
13 | % Input parameter:
14 | % OldChrom - Matrix containing the chromosomes of the old
15 | % population. Each line corresponds to one individual.
16 | % FieldDR - Matrix describing the boundaries of each variable.
17 | % MutOpt - (optional) Vector containing mutation rate and shrink value
18 | % MutOpt(1): MutR - number containing the mutation rate -
19 | % probability for mutation of a variable
20 | % if omitted or NaN, MutR = 1/variables per individual
21 | % is assumed
22 | % MutOpt(2): MutShrink - (optional) number for shrinking the
23 | % mutation range in the range [0 1], possibility to
24 | % shrink the range of the mutation depending on,
25 | % for instance actual generation.
26 | % if omitted or NaN, MutShrink = 1 is assumed
27 | %
28 | % Output parameter:
29 | % NewChrom - Matrix containing the chromosomes of the population
30 | % after mutation in the same format as OldChrom.
31 | %
32 | % Author: Hartmut Pohlheim
33 | % History: 23.11.93 file created
34 | % 24.11.93 function optimised (for,for-loop to for-loop)
35 | % mutation rate included
36 | % style improved
37 | % 06.12.93 change of function name
38 | % check of boundaries after mutation out of loop
39 | % 16.12.93 NewMutMat and OldMutMat included for compability
40 | % 16.02.94 preparation for multi-subpopulations at once
41 | % 25.02.94 NewMutMat and OldMutMat removed (now in mutran10.m)
42 | % clean up
43 | % change of function name in mutbga.m
44 | % 03.03.94 Lower and Upper directly used (less memory)
45 | % 19.03.94 multipopulation support removed
46 | % more parameter checks
47 | % 27.03.94 Delta exact calculated, for loop saved
48 | % 21.01.03 tested under MATLAB v6 by Alex Shenfield
49 |
50 | function NewChrom = mutbga(OldChrom, FieldDR, MutOpt);
51 |
52 | % Check parameter consistency
53 | if nargin < 2, error('Not enough input parameters'); end
54 |
55 | % Identify the population size (Nind) and the number of variables (Nvar)
56 | [Nind,Nvar] = size(OldChrom);
57 |
58 | [mF, nF] = size(FieldDR);
59 | if mF ~= 2, error('FieldDR must be a matrix with 2 rows'); end
60 | if Nvar ~= nF, error('FieldDR and OldChrom disagree'); end
61 |
62 | if nargin < 3, MutR = 1/Nvar; MutShrink = 1;
63 | elseif isempty(MutOpt), MutR = 1/Nvar; MutShrink = 1;
64 | elseif isnan(MutOpt), MutR = 1/Nvar; MutShrink = 1;
65 | else
66 | if length(MutOpt) == 1, MutR = MutOpt; MutShrink = 1;
67 | elseif length(MutOpt) == 2, MutR = MutOpt(1); MutShrink = MutOpt(2);
68 | else, error(' Too many parameters in MutOpt'); end
69 | end
70 |
71 | if isempty(MutR), MutR = 1/Nvar;
72 | elseif isnan(MutR), MutR = 1/Nvar;
73 | elseif length(MutR) ~= 1, error('Parameter for mutation rate must be a scalar');
74 | elseif (MutR < 0 | MutR > 1), error('Parameter for mutation rate must be a scalar in [0, 1]'); end
75 |
76 | if isempty(MutShrink), MutShrink = 1;
77 | elseif isnan(MutShrink), MutShrink = 1;
78 | elseif length(MutShrink) ~= 1, error('Parameter for shrinking mutation range must be a scalar');
79 | elseif (MutShrink < 0 | MutShrink > 1),
80 | error('Parameter for shrinking mutation range must be a scalar in [0, 1]');
81 | end
82 |
83 | % the variables are mutated with probability MutR
84 | % NewChrom = OldChrom (+ or -) * Range * MutShrink * Delta
85 | % Range = 0.5 * (upperbound - lowerbound)
86 | % Delta = Sum(Alpha_i * 2^-i) from 0 to ACCUR; Alpha_i = rand(ACCUR,1) < 1/ACCUR
87 |
88 | % Matrix with range values for every variable
89 | Range = rep(0.5 * MutShrink *(FieldDR(2,:)-FieldDR(1,:)),[Nind 1]);
90 |
91 | % zeros and ones for mutate or not this variable, together with Range
92 | Range = Range .* (rand(Nind,Nvar) < MutR);
93 |
94 | % compute, if + or - sign
95 | Range = Range .* (1 - 2 * (rand(Nind,Nvar) < 0.5));
96 |
97 | % used for later computing, here only ones computed
98 | ACCUR = 20;
99 | Vect = 2 .^ (-(0:(ACCUR-1))');
100 | Delta = (rand(Nind,ACCUR) < 1/ACCUR) * Vect;
101 | Delta = rep(Delta, [1 Nvar]);
102 |
103 | % perform mutation
104 | NewChrom = OldChrom + Range .* Delta;
105 |
106 | % Ensure variables boundaries, compare with lower and upper boundaries
107 | NewChrom = max(rep(FieldDR(1,:),[Nind 1]), NewChrom);
108 | NewChrom = min(rep(FieldDR(2,:),[Nind 1]), NewChrom);
109 |
110 |
111 | % End of function
--------------------------------------------------------------------------------
/objfun1.m:
--------------------------------------------------------------------------------
1 | % OBJFUN1.M (OBJective function for De Jong's FUNction 1)
2 | %
3 | % This function implements the De Jong function 1.
4 | %
5 | % Syntax: ObjVal = objfun1(Chrom,rtn_type)
6 | %
7 | % Input parameters:
8 | % Chrom - Matrix containing the chromosomes of the current
9 | % population. Each row corresponds to one individual's
10 | % string representation.
11 | % if Chrom == [], then special values will be returned
12 | % rtn_type - if Chrom == [] and
13 | % rtn_type == 1 (or []) return boundaries
14 | % rtn_type == 2 return title
15 | % rtn_type == 3 return value of global minimum
16 | %
17 | % Output parameters:
18 | % ObjVal - Column vector containing the objective values of the
19 | % individuals in the current population.
20 | % if called with Chrom == [], then ObjVal contains
21 | % rtn_type == 1, matrix with the boundaries of the function
22 | % rtn_type == 2, text for the title of the graphic output
23 | % rtn_type == 3, value of global minimum
24 | %
25 | %
26 | % Author: Hartmut Pohlheim
27 | % History: 26.11.93 file created
28 | % 27.11.93 text of title and rtn_type added
29 | % 30.11.93 show Dim in figure title
30 | % 16.12.93 rtn_type == 3, return value of global minimum
31 | % 01.03.94 name changed in obj*
32 | % 21.01.03 updated for MATLAB v6 by Alex Shenfield
33 |
34 | function ObjVal = objfun1(Chrom,rtn_type);
35 |
36 | % Dimension of objective function
37 | Dim = 20;
38 |
39 | % Compute population parameters
40 | [Nind,Nvar] = size(Chrom);
41 |
42 | % Check size of Chrom and do the appropriate thing
43 | % if Chrom is [], then define size of boundary-matrix and values
44 | if Nind == 0
45 | % return text of title for graphic output
46 | if rtn_type == 2
47 | ObjVal = ['DE JONG function 1-' int2str(Dim)];
48 | % return value of global minimum
49 | elseif rtn_type == 3
50 | ObjVal = 0;
51 | % define size of boundary-matrix and values
52 | else
53 | % lower and upper bound, identical for all n variables
54 | ObjVal = 100*[-5.12; 5.12];
55 | ObjVal = ObjVal(1:2,ones(Dim,1));
56 | end
57 | % if Dim variables, compute values of function
58 | elseif Nvar == Dim
59 | % function 1, sum of xi^2 for i = 1:Dim (Dim=30)
60 | % n = Dim, -5.12 <= xi <= 5.12
61 | % global minimum at (xi)=(0) ; fmin=0
62 | ObjVal = sum((Chrom .* Chrom)')';
63 | % ObjVal = diag(Chrom * Chrom'); % both lines produce the same
64 | % otherwise error, wrong format of Chrom
65 | else
66 | error('size of matrix Chrom is not correct for function evaluation');
67 | end
68 |
69 |
70 | % End of function
--------------------------------------------------------------------------------
/objharv.m:
--------------------------------------------------------------------------------
1 | % OBJHARV.M (OBJective function for HARVest problem)
2 | %
3 | % This function implements the HARVEST PROBLEM.
4 | %
5 | % Syntax: ObjVal = objharv(Chrom,rtn_type)
6 | %
7 | % Input parameters:
8 | % Chrom - Matrix containing the chromosomes of the current
9 | % population. Each row corresponds to one individual's
10 | % string representation.
11 | % if Chrom == [], then special values will be returned
12 | % rtn_type - if Chrom == [] and
13 | % rtn_type == 1 (or []) return boundaries
14 | % rtn_type == 2 return title
15 | % rtn_type == 3 return value of global minimum
16 | %
17 | % Output parameters:
18 | % ObjVal - Column vector containing the objective values of the
19 | % individuals in the current population.
20 | % if called with Chrom == [], then ObjVal contains
21 | % rtn_type == 1, matrix with the boundaries of the function
22 | % rtn_type == 2, text for the title of the graphic output
23 | % rtn_type == 3, value of global minimum
24 | %
25 | %
26 | % Author: Hartmut Pohlheim
27 | % History: 18.02.94 file created (copy of vallinq.m)
28 | % 01.03.94 name changed in obj*
29 | % 21.01.03 updated for MATLAB v6 by Alex Shenfield
30 |
31 | function ObjVal = objharv(Chrom,rtn_type);
32 |
33 | % global gen;
34 |
35 | % Dimension of objective function
36 | Dim = 20;
37 |
38 | % values from MICHALEWICZ
39 | a = 1.1;
40 | x0 = 100;
41 | xend = x0;
42 | XENDWEIGHT = 0.4/(Dim^0.6);
43 |
44 | % Compute population parameters
45 | [Nind,Nvar] = size(Chrom);
46 |
47 | % Check size of Chrom and do the appropriate thing
48 | % if Chrom is [], then define size of boundary-matrix and values
49 | if Nind == 0
50 | % return text of title for graphic output
51 | if rtn_type == 2
52 | ObjVal = ['HARVEST PROBLEM-' int2str(Dim)];
53 | % return value of global minimum
54 | elseif rtn_type == 3
55 | ObjVal = -sqrt(x0*(a^Dim-1)^2/(a^(Dim-1)*(a-1)));
56 | % define size of boundary-matrix and values
57 | else
58 | % lower and upper bound, identical for all n variables
59 | ObjVal1 = [0; 10*Dim];
60 | ObjVal = rep(ObjVal1,[1 Dim]);
61 | end
62 | % if Dim variables, compute values of function
63 | elseif Nvar == Dim
64 | ObjVal = zeros(Nind,1);
65 | X = rep(x0,[Nind 1]);
66 | for irun = 1:Nvar,
67 | X = a*X - Chrom(:,irun);
68 | end
69 | X;
70 | ObjVal = -(sum(sqrt(Chrom)')' - XENDWEIGHT * abs(X-x0));
71 | % otherwise error, wrong format of Chrom
72 | else
73 | error('size of matrix Chrom is not correct for function evaluation');
74 | end
75 |
76 |
77 | % End of function
--------------------------------------------------------------------------------
/ranking.m:
--------------------------------------------------------------------------------
1 | % RANKING.M (RANK-based fitness assignment)
2 | %
3 | % This function performs ranking of individuals.
4 | %
5 | % Syntax: FitnV = ranking(ObjV, RFun, SUBPOP)
6 | %
7 | % This function ranks individuals represented by their associated
8 | % cost, to be *minimized*, and returns a column vector FitnV
9 | % containing the corresponding individual fitnesses. For multiple
10 | % subpopulations the ranking is performed separately for each
11 | % subpopulation.
12 | %
13 | % Input parameters:
14 | % ObjV - Column vector containing the objective values of the
15 | % individuals in the current population (cost values).
16 | % RFun - (optional) If RFun is a scalar in [1, 2] linear ranking is
17 | % assumed and the scalar indicates the selective pressure.
18 | % If RFun is a 2 element vector:
19 | % RFun(1): SP - scalar indicating the selective pressure
20 | % RFun(2): RM - ranking method
21 | % RM = 0: linear ranking
22 | % RM = 1: non-linear ranking
23 | % If RFun is a vector with length(Rfun) > 2 it contains
24 | % the fitness to be assigned to each rank. It should have
25 | % the same length as ObjV. Usually RFun is monotonously
26 | % increasing.
27 | % If RFun is omitted or NaN, linear ranking
28 | % and a selective pressure of 2 are assumed.
29 | % SUBPOP - (optional) Number of subpopulations
30 | % if omitted or NaN, 1 subpopulation is assumed
31 | %
32 | % Output parameters:
33 | % FitnV - Column vector containing the fitness values of the
34 | % individuals in the current population.
35 | %
36 | %
37 | % Author: Hartmut Pohlheim (Carlos Fonseca)
38 | % History: 01.03.94 non-linear ranking
39 | % 10.03.94 multiple populations
40 | % 21.01.03 updated for MATLAB v6 by Alex Shenfield
41 |
42 | function FitnV = ranking(ObjV, RFun, SUBPOP)
43 |
44 | % Identify the vector size (Nind)
45 | [Nind,~] = size(ObjV);
46 |
47 | if nargin < 2, RFun = []; end
48 | if nargin > 1, if isnan(RFun), RFun = []; end, end
49 | if prod(size(RFun)) == 2,
50 | if RFun(2) == 1, NonLin = 1;
51 | elseif RFun(2) == 0, NonLin = 0;
52 | else error('Parameter for ranking method must be 0 or 1'); end
53 | RFun = RFun(1);
54 | if isnan(RFun), RFun = 2; end
55 | elseif prod(size(RFun)) > 2,
56 | if prod(size(RFun)) ~= Nind, error('ObjV and RFun disagree'); end
57 | elseif prod(size(RFun)) < 2, NonLin = 0;
58 | end
59 |
60 | if nargin < 3, SUBPOP = 1; end
61 | if nargin > 2,
62 | if isempty(SUBPOP), SUBPOP = 1;
63 | elseif isnan(SUBPOP), SUBPOP = 1;
64 | elseif length(SUBPOP) ~= 1, error('SUBPOP must be a scalar'); end
65 | end
66 |
67 | if (Nind/SUBPOP) ~= fix(Nind/SUBPOP), error('ObjV and SUBPOP disagree'); end
68 | Nind = Nind/SUBPOP; % Compute number of individuals per subpopulation
69 |
70 | % Check ranking function and use default values if necessary
71 | if isempty(RFun),
72 | % linear ranking with selective pressure 2
73 | RFun = 2*[0:Nind-1]'/(Nind-1);
74 | elseif prod(size(RFun)) == 1
75 | if NonLin == 1,
76 | % non-linear ranking
77 | if RFun(1) < 1, error('Selective pressure must be greater than 1');
78 | elseif RFun(1) > Nind-2, error('Selective pressure too big'); end
79 | Root1 = roots([RFun(1)-Nind [RFun(1)*ones(1,Nind-1)]]);
80 | RFun = (abs(Root1(1)) * ones(Nind,1)) .^ [(0:Nind-1)'];
81 | RFun = RFun / sum(RFun) * Nind;
82 | else
83 | % linear ranking with SP between 1 and 2
84 | if (RFun(1) < 1 | RFun(1) > 2),
85 | error('Selective pressure for linear ranking must be between 1 and 2');
86 | end
87 | RFun = 2-RFun + 2*(RFun-1)*[0:Nind-1]'/(Nind-1);
88 | end
89 | end;
90 |
91 | FitnV = [];
92 |
93 | % loop over all subpopulations
94 | for irun = 1:SUBPOP,
95 | % Copy objective values of actual subpopulation
96 | ObjVSub = ObjV((irun-1)*Nind+1:irun*Nind);
97 | % Sort does not handle NaN values as required. So, find those...
98 | NaNix = isnan(ObjVSub);
99 | Validix = find(~NaNix);
100 | % ... and sort only numeric values (smaller is better).
101 | [~,ix] = sort(-ObjVSub(Validix));
102 |
103 | % Now build indexing vector assuming NaN are worse than numbers,
104 | % (including Inf!)...
105 | ix = [find(NaNix) ; Validix(ix)];
106 | % ... and obtain a sorted version of ObjV
107 | Sorted = ObjVSub(ix);
108 |
109 | % Assign fitness according to RFun.
110 | i = 1;
111 | FitnVSub = zeros(Nind,1);
112 | for j = [find(Sorted(1:Nind-1) ~= Sorted(2:Nind)); Nind]',
113 | FitnVSub(i:j) = sum(RFun(i:j)) * ones(j-i+1,1) / (j-i+1);
114 | i =j+1;
115 | end
116 |
117 | % Finally, return unsorted vector.
118 | [~,uix] = sort(ix);
119 | FitnVSub = FitnVSub(uix);
120 |
121 | % Add FitnVSub to FitnV
122 | FitnV = [FitnV; FitnVSub];
123 | end
124 |
125 | % End of function
--------------------------------------------------------------------------------
/recdis.m:
--------------------------------------------------------------------------------
1 | % RECDIS.M (RECombination DIScrete)
2 | %
3 | % This function performs discret recombination between pairs of individuals
4 | % and returns the new individuals after mating.
5 | %
6 | % Syntax: NewChrom = recdis(OldChrom, XOVR)
7 | %
8 | % Input parameters:
9 | % OldChrom - Matrix containing the chromosomes of the old
10 | % population. Each line corresponds to one individual
11 | % (in any form, not necessarily real-values).
12 | % XOVR - Probability of recombination occurring between pairs
13 | % of individuals. (not used, only for compatibility)
14 | %
15 | % Output parameter:
16 | % NewChrom - Matrix containing the chromosomes of the population
17 | % after mating, ready to be mutated and/or evaluated,
18 | % in the same format as OldChrom.
19 | %
20 | % Author: Hartmut Pohlheim
21 | % History: 23.11.93 file created
22 | % 24.11.93 style improved
23 | % 06.12.93 change of name of function
24 | % 25.02.94 clean up
25 | % 19.03.94 multipopulation support removed
26 | % 21.01.03 tested under MATLAB v6 by Alex Shenfield
27 |
28 | function NewChrom = recdis(OldChrom, XOVR);
29 |
30 | % Identify the population size (Nind) and the number of variables (Nvar)
31 | [Nind,Nvar] = size(OldChrom);
32 |
33 | % Identify the number of matings
34 | Xops = floor(Nind/2);
35 |
36 | % which parent gives the value
37 | Mask1 = (rand(Xops,Nvar)<0.5);
38 | Mask2 = (rand(Xops,Nvar)<0.5);
39 |
40 | % Performs crossover
41 | odd = 1:2:Nind-1;
42 | even= 2:2:Nind;
43 | NewChrom(odd,:) = (OldChrom(odd,:).* Mask1) + (OldChrom(even,:).*(~Mask1));
44 | NewChrom(even,:) = (OldChrom(odd,:).* Mask2) + (OldChrom(even,:).*(~Mask2));
45 |
46 | % If the number of individuals is odd, the last individual cannot be mated
47 | % but must be included in the new population
48 | if rem(Nind,2), NewChrom(Nind,:)=OldChrom(Nind,:); end
49 |
50 |
51 | % End of function
--------------------------------------------------------------------------------
/recint.m:
--------------------------------------------------------------------------------
1 | % RECINT.M (RECombination extended INTermediate)
2 | %
3 | % This function performs extended intermediate recombination between
4 | % pairs of individuals and returns the new individuals after mating.
5 | %
6 | % Syntax: NewChrom = recint(OldChrom, XOVR)
7 | %
8 | % Input parameters:
9 | % OldChrom - Matrix containing the chromosomes of the old
10 | % population. Each line corresponds to one
11 | % individual
12 | % XOVR - Probability of crossover occurring between pairs
13 | % of individuals. (not used, only for compatibility)
14 | %
15 | % Output parameter:
16 | % NewChrom - Matrix containing the chromosomes of the population
17 | % after mating, ready to be mutated and/or evaluated,
18 | % in the same format as OldChrom.
19 | %
20 | % Author: Hartmut Pohlheim
21 | % History: 25.11.93 file created
22 | % 06.12.93 change of name of function
23 | % 25.02.94 clean up
24 | % 19.03.94 multipopulation support removed
25 | % 21.01.03 tested under MATLAB v6 by Alex Shenfield
26 |
27 | function NewChrom = recint(OldChrom, XOVR);
28 |
29 | % Identify the population size (Nind) and the number of variables (Nvar)
30 | [Nind,Nvar] = size(OldChrom);
31 |
32 | % Identify the number of matings
33 | Xops = floor(Nind/2);
34 |
35 | % Performs recombination
36 | odd = 1:2:Nind-1;
37 | even= 2:2:Nind;
38 |
39 | % position of value of offspring compared to parents
40 | Alpha = -0.25 + 1.5 * rand(Xops,Nvar);
41 |
42 | % recombination
43 | NewChrom(odd,:) = OldChrom(odd,:) + Alpha .* (OldChrom(even,:) - OldChrom(odd,:));
44 |
45 | % the same ones more for second half of offspring
46 | Alpha = -0.25 + 1.5 * rand(Xops,Nvar);
47 | NewChrom(even,:) = OldChrom(odd,:) + Alpha .* (OldChrom(even,:) - OldChrom(odd,:));
48 |
49 | % If the number of individuals is odd, the last individual cannot be mated
50 | % but must be included in the new population
51 | if rem(Nind,2), NewChrom(Nind,:)=OldChrom(Nind,:); end
52 |
53 | % End of function
--------------------------------------------------------------------------------
/reclin.m:
--------------------------------------------------------------------------------
1 | % RECLIN.M (RECombination extended LINe)
2 | %
3 | % This function performs extended line recombination between
4 | % pairs of individuals and returns the new individuals after mating.
5 | %
6 | % Syntax: NewChrom = reclin(OldChrom, XOVR)
7 | %
8 | % Input parameters:
9 | % OldChrom - Matrix containing the chromosomes of the old
10 | % population. Each line corresponds to one
11 | % individual
12 | % XOVR - Probability of crossover occurring between pairs
13 | % of individuals. (not used, only for compatibility)
14 | %
15 | % Output parameter:
16 | % NewChrom - Matrix containing the chromosomes of the population
17 | % after mating, ready to be mutated and/or evaluated,
18 | % in the same format as OldChrom.
19 | %
20 | % Author: Hartmut Pohlheim
21 | % History: 26.11.93 file created
22 | % 06.12.93 change of name of function
23 | % 25.02.94 clean up
24 | % 19.03.94 multipopulation support removed
25 | % 21.01.03 tested under MATLAB v6 by Alex Shenfield
26 |
27 | function NewChrom = reclin(OldChrom, XOVR);
28 |
29 | % Identify the population size (Nind) and the number of variables (Nvar)
30 | [Nind,Nvar] = size(OldChrom);
31 |
32 | % Identify the number of matings
33 | Xops = floor(Nind/2);
34 |
35 | % Performs recombination
36 | odd = 1:2:Nind-1;
37 | even= 2:2:Nind;
38 |
39 | % position of value of offspring compared to parents
40 | Alpha = -0.25 + 1.5 * rand(Xops,1);
41 | Alpha = Alpha(1:Xops,ones(Nvar,1));
42 |
43 | % recombination
44 | NewChrom(odd,:) = OldChrom(odd,:) + Alpha .* (OldChrom(even,:) - OldChrom(odd,:));
45 |
46 | % the same ones more for second half of offspring
47 | Alpha = -0.25 + 1.5 * rand(Xops,1);
48 | Alpha = Alpha(1:Xops,ones(Nvar,1));
49 | NewChrom(even,:) = OldChrom(odd,:) + Alpha .* (OldChrom(even,:) - OldChrom(odd,:));
50 |
51 | % If the number of individuals is odd, the last individual cannot be mated
52 | % but must be included in the new population
53 | if rem(Nind,2), NewChrom(Nind,:)=OldChrom(Nind,:); end
54 |
55 |
56 | % End of function
--------------------------------------------------------------------------------
/recmut.m:
--------------------------------------------------------------------------------
1 | % RECLIN.M (line RECombination with MUTation features)
2 | %
3 | % This function performs line recombination with mutation features between
4 | % pairs of individuals and returns the new individuals after mating.
5 | %
6 | % Syntax: NewChrom = recmut(OldChrom, FieldDR, MutOpt)
7 | %
8 | % Input parameters:
9 | % OldChrom - Matrix containing the chromosomes of the old
10 | % population. Each line corresponds to one individual
11 | % FieldDR - Matrix describing the boundaries of each variable.
12 | % MutOpt - (optional) Vector containing recombination rate and shrink value
13 | % MutOpt(1): MutR - number containing the recombination rate -
14 | % probability for recombine a pair of parents
15 | % if omitted or NaN, MutOpt(1) = 1 is assumed
16 | % MutOpt(2): MutShrink - (optional) number for shrinking the
17 | % recombination range in the range [0 1], possibility to
18 | % shrink the range of the recombination depending on,
19 | % for instance actual generation.
20 | % if omitted or NaN, MutOpt(2) = 1 is assumed
21 | %
22 | % Output parameter:
23 | % NewChrom - Matrix containing the chromosomes of the population
24 | % after mating, ready to be mutated and/or evaluated,
25 | % in the same format as OldChrom.
26 | %
27 | % Author: Hartmut Pohlheim
28 | % History: 27.03.94 file created
29 | % 22.01.03 tested under MATLAB v6 by Alex Shenfield
30 | % (NOTE : doesn't work with higher level recombin.m)
31 |
32 | function NewChrom = recmut(OldChrom, FieldDR, MutOpt);
33 |
34 | % Check parameter consistency
35 | if nargin < 2, error('Not enough input parameter'); end
36 |
37 | % Identify the population size (Nind) and the number of variables (Nvar)
38 | [Nind,Nvar] = size(OldChrom);
39 |
40 | [mF, nF] = size(FieldDR);
41 | if mF ~= 2, error('FieldDR must be a matrix with 2 rows'); end
42 | if Nvar ~= nF, error('FieldDR and OldChrom disagree'); end
43 |
44 | if nargin < 3, MutR = 1; MutShrink = 1;
45 | elseif isempty(MutOpt), MutR = 1; MutShrink = 1;
46 | elseif isnan(MutOpt), MutR = 1; MutShrink = 1;
47 | else
48 | if length(MutOpt) == 1, MutR = MutOpt; MutShrink = 1;
49 | elseif length(MutOpt) == 2, MutR = MutOpt(1); MutShrink = MutOpt(2);
50 | else, error(' Too many parameter in MutOpt'); end
51 | end
52 |
53 | if isempty(MutR), MutR = 1;
54 | elseif isnan(MutR), MutR = 1;
55 | elseif length(MutR) ~= 1, error('Parameter for recombination rate must be a scalar');
56 | elseif (MutR < 0 | MutR > 1), error('Parameter for recombination rate must be a scalar in [0, 1]'); end
57 |
58 | if isempty(MutShrink), MutShrink = 1;
59 | elseif isnan(MutShrink), MutShrink = 1;
60 | elseif length(MutShrink) ~= 1, error('Parameter for shrinking recombination range must be a scalar');
61 | elseif (MutShrink < 0 | MutShrink > 1),
62 | error('Parameter for shrinking recombination range must be a scalar in [0, 1]');
63 | end
64 |
65 | % Identify the number of matings
66 | Xops = floor(Nind/2);
67 |
68 | % NewChrom = OldChrom (+ or -) * Range * MutShrink * Delta * ChromDiff
69 | % - with probability 0.9, + with probability 0.1
70 | % Range = 0.5 * (upperbound - lowerbound), given by FieldDR
71 | % Delta = Sum(Alpha_i * 2^-i) from 0 to ACCUR; Alpha_i = rand(ACCUR,1) < 1/ACCUR
72 | % ChromDiff = (individual1 - individual2) / Distance between individuals
73 |
74 | % Matrix with range values for every variable
75 | Range = rep(0.5 * MutShrink *(FieldDR(2,:)-FieldDR(1,:)),[Xops 1]);
76 |
77 | % zeros and ones for recombine or not this variable, together with Range
78 | if MutR < 1, Range = Range .* rep((rand(Xops,1) < MutR), [1 Nvar]); end
79 |
80 | % compute, if + or - sign
81 | Range = Range .* (1 - 2 * (rand(Xops,Nvar) < 0.9));
82 |
83 | % compute distance between mating pairs
84 | NormO = zeros(Xops,1);
85 | for irun = 1:Xops,
86 | NormO(irun) = max(realmin,abs(norm(OldChrom(2*irun,:)) - norm(OldChrom(2*irun-1,:))));
87 | end
88 |
89 | % compute difference between variables divided by distance
90 | ChromDiff = zeros(Xops,Nvar);
91 | for irun = 1:Xops
92 | ChromDiff(irun,:) = diff([OldChrom(2*irun-1,:); OldChrom(2*irun,:)]) / NormO(irun);
93 | end
94 |
95 | % compute delta value for all individuals
96 | ACCUR = 20;
97 | Vect = 2 .^ (-(0:(ACCUR-1))');
98 | Delta = (rand(Xops,ACCUR) < 1/ACCUR) * Vect;
99 | Delta = rep(Delta, [1 Nvar]);
100 |
101 | % Performs recombination
102 | odd = 1:2:Nind-1;
103 | even= 2:2:Nind;
104 |
105 | % recombination
106 | NewChrom(odd,:) = OldChrom(odd,:) + Range .* Delta .* (ChromDiff);
107 | NewChrom(even,:) = OldChrom(even,:) + Range .* Delta .* (-ChromDiff);
108 |
109 | % If the number of individuals is odd, the last individual cannot be mated
110 | % but must be included in the new population
111 | if rem(Nind,2), NewChrom(Nind,:)=OldChrom(Nind,:); end
112 |
113 | % Ensure variables boundaries, compare with lower and upper boundaries
114 | NewChrom = max(rep(FieldDR(1,:),[Nind 1]), NewChrom);
115 | NewChrom = min(rep(FieldDR(2,:),[Nind 1]), NewChrom);
116 |
117 |
118 | % End of function
--------------------------------------------------------------------------------
/recombin.m:
--------------------------------------------------------------------------------
1 | % RECOMBIN.M (RECOMBINation high-level function)
2 | %
3 | % This function performs recombination between pairs of individuals
4 | % and returns the new individuals after mating. The function handles
5 | % multiple populations and calls the low-level recombination function
6 | % for the actual recombination process.
7 | %
8 | % Syntax: NewChrom = recombin(REC_F, OldChrom, RecOpt, SUBPOP)
9 | %
10 | % Input parameters:
11 | % REC_F - String containing the name of the recombination or
12 | % crossover function
13 | % Chrom - Matrix containing the chromosomes of the old
14 | % population. Each line corresponds to one individual
15 | % RecOpt - (optional) Scalar containing the probability of
16 | % recombination/crossover occurring between pairs
17 | % of individuals.
18 | % if omitted or NaN, 1 is assumed
19 | % SUBPOP - (optional) Number of subpopulations
20 | % if omitted or NaN, 1 subpopulation is assumed
21 | %
22 | % Output parameter:
23 | % NewChrom - Matrix containing the chromosomes of the population
24 | % after recombination in the same format as OldChrom.
25 | %
26 | % Author: Hartmut Pohlheim
27 | % History: 18.03.94 file created
28 | % 22.01.03 tested under MATLAB v6 by Alex Shenfield
29 | % (NOTE : doesn't work with low level recmut.m)
30 |
31 | function NewChrom = recombin(REC_F, Chrom, RecOpt, SUBPOP);
32 |
33 | % Check parameter consistency
34 | if nargin < 2, error('Not enough input parameter'); end
35 |
36 | % Identify the population size (Nind)
37 | [Nind,Nvar] = size(Chrom);
38 |
39 | if nargin < 4, SUBPOP = 1; end
40 | if nargin > 3,
41 | if isempty(SUBPOP), SUBPOP = 1;
42 | elseif isnan(SUBPOP), SUBPOP = 1;
43 | elseif length(SUBPOP) ~= 1, error('SUBPOP must be a scalar'); end
44 | end
45 |
46 | if (Nind/SUBPOP) ~= fix(Nind/SUBPOP), error('Chrom and SUBPOP disagree'); end
47 | Nind = Nind/SUBPOP; % Compute number of individuals per subpopulation
48 |
49 | if nargin < 3, RecOpt = 0.7; end
50 | if nargin > 2,
51 | if isempty(RecOpt), RecOpt = 0.7;
52 | elseif isnan(RecOpt), RecOpt = 0.7;
53 | elseif length(RecOpt) ~= 1, error('RecOpt must be a scalar');
54 | elseif (RecOpt < 0 | RecOpt > 1), error('RecOpt must be a scalar in [0, 1]'); end
55 | end
56 |
57 | % Select individuals of one subpopulation and call low level function
58 | NewChrom = [];
59 | for irun = 1:SUBPOP,
60 | ChromSub = Chrom((irun-1)*Nind+1:irun*Nind,:);
61 | NewChromSub = feval(REC_F, ChromSub, RecOpt);
62 | NewChrom=[NewChrom; NewChromSub];
63 | end
64 |
65 | % End of function
--------------------------------------------------------------------------------
/reins.m:
--------------------------------------------------------------------------------
1 | % REINS.M (RE-INSertion of offspring in population replacing parents)
2 | %
3 | % This function reinserts offspring in the population.
4 | %
5 | % Syntax: [Chrom, ObjVCh] = reins(Chrom, SelCh, SUBPOP, InsOpt, ObjVCh, ObjVSel)
6 | %
7 | % Input parameters:
8 | % Chrom - Matrix containing the individuals (parents) of the current
9 | % population. Each row corresponds to one individual.
10 | % SelCh - Matrix containing the offspring of the current
11 | % population. Each row corresponds to one individual.
12 | % SUBPOP - (optional) Number of subpopulations
13 | % if omitted or NaN, 1 subpopulation is assumed
14 | % InsOpt - (optional) Vector containing the insertion method parameters
15 | % ExOpt(1): Select - number indicating kind of insertion
16 | % 0 - uniform insertion
17 | % 1 - fitness-based insertion
18 | % if omitted or NaN, 0 is assumed
19 | % ExOpt(2): INSR - Rate of offspring to be inserted per
20 | % subpopulation (% of subpopulation)
21 | % if omitted or NaN, 1.0 (100%) is assumed
22 | % ObjVCh - (optional) Column vector containing the objective values
23 | % of the individuals (parents - Chrom) in the current
24 | % population, needed for fitness-based insertion
25 | % saves recalculation of objective values for population
26 | % ObjVSel - (optional) Column vector containing the objective values
27 | % of the offspring (SelCh) in the current population, needed for
28 | % partial insertion of offspring,
29 | % saves recalculation of objective values for population
30 | %
31 | % Output parameters:
32 | % Chrom - Matrix containing the individuals of the current
33 | % population after reinsertion.
34 | % ObjVCh - if ObjVCh and ObjVSel are input parameters, then column
35 | % vector containing the objective values of the individuals
36 | % of the current generation after reinsertion.
37 | %
38 | % Author: Hartmut Pohlheim
39 | % History: 10.03.94 file created
40 | % 19.03.94 parameter checking improved
41 | % 26.01.03 tested under MATLAB v6 by Alex Shenfield
42 |
43 | function [Chrom, ObjVCh] = reins(Chrom, SelCh, SUBPOP, InsOpt, ObjVCh, ObjVSel);
44 |
45 | % Check parameter consistency
46 | if nargin < 2, error('Not enough input parameter'); end
47 | if (nargout == 2 & nargin < 6), error('Input parameter missing: ObjVCh and/or ObjVSel'); end
48 |
49 | [NindP, NvarP] = size(Chrom);
50 | [NindO, NvarO] = size(SelCh);
51 |
52 | if nargin == 2, SUBPOP = 1; end
53 | if nargin > 2,
54 | if isempty(SUBPOP), SUBPOP = 1;
55 | elseif isnan(SUBPOP), SUBPOP = 1;
56 | elseif length(SUBPOP) ~= 1, error('SUBPOP must be a scalar'); end
57 | end
58 |
59 | if (NindP/SUBPOP) ~= fix(NindP/SUBPOP), error('Chrom and SUBPOP disagree'); end
60 | if (NindO/SUBPOP) ~= fix(NindO/SUBPOP), error('SelCh and SUBPOP disagree'); end
61 | NIND = NindP/SUBPOP; % Compute number of individuals per subpopulation
62 | NSEL = NindO/SUBPOP; % Compute number of offspring per subpopulation
63 |
64 | IsObjVCh = 0; IsObjVSel = 0;
65 | if nargin > 4,
66 | [mO, nO] = size(ObjVCh);
67 | if nO ~= 1, error('ObjVCh must be a column vector'); end
68 | if NindP ~= mO, error('Chrom and ObjVCh disagree'); end
69 | IsObjVCh = 1;
70 | end
71 | if nargin > 5,
72 | [mO, nO] = size(ObjVSel);
73 | if nO ~= 1, error('ObjVSel must be a column vector'); end
74 | if NindO ~= mO, error('SelCh and ObjVSel disagree'); end
75 | IsObjVSel = 1;
76 | end
77 |
78 | if nargin < 4, INSR = 1.0; Select = 0; end
79 | if nargin >= 4,
80 | if isempty(InsOpt), INSR = 1.0; Select = 0;
81 | elseif isnan(InsOpt), INSR = 1.0; Select = 0;
82 | else
83 | INSR = NaN; Select = NaN;
84 | if (length(InsOpt) > 2), error('Parameter InsOpt too long'); end
85 | if (length(InsOpt) >= 1), Select = InsOpt(1); end
86 | if (length(InsOpt) >= 2), INSR = InsOpt(2); end
87 | if isnan(Select), Select = 0; end
88 | if isnan(INSR), INSR =1.0; end
89 | end
90 | end
91 |
92 | if (INSR < 0 | INSR > 1), error('Parameter for insertion rate must be a scalar in [0, 1]'); end
93 | if (INSR < 1 & IsObjVSel ~= 1), error('For selection of offspring ObjVSel is needed'); end
94 | if (Select ~= 0 & Select ~= 1), error('Parameter for selection method must be 0 or 1'); end
95 | if (Select == 1 & IsObjVCh == 0), error('ObjVCh for fitness-based exchange needed'); end
96 |
97 | if INSR == 0, return; end
98 | NIns = min(max(floor(INSR*NSEL+.5),1),NIND); % Number of offspring to insert
99 |
100 | % perform insertion for each subpopulation
101 | for irun = 1:SUBPOP,
102 | % Calculate positions in old subpopulation, where offspring are inserted
103 | if Select == 1, % fitness-based reinsertion
104 | [Dummy, ChIx] = sort(-ObjVCh((irun-1)*NIND+1:irun*NIND));
105 | else % uniform reinsertion
106 | [Dummy, ChIx] = sort(rand(NIND,1));
107 | end
108 | PopIx = ChIx((1:NIns)')+ (irun-1)*NIND;
109 | % Calculate position of Nins-% best offspring
110 | if (NIns < NSEL), % select best offspring
111 | [Dummy,OffIx] = sort(ObjVSel((irun-1)*NSEL+1:irun*NSEL));
112 | else
113 | OffIx = (1:NIns)';
114 | end
115 | SelIx = OffIx((1:NIns)')+(irun-1)*NSEL;
116 | % Insert offspring in subpopulation -> new subpopulation
117 | Chrom(PopIx,:) = SelCh(SelIx,:);
118 | if (IsObjVCh == 1 & IsObjVSel == 1), ObjVCh(PopIx) = ObjVSel(SelIx); end
119 | end
120 |
121 | % End of function
--------------------------------------------------------------------------------
/rep.m:
--------------------------------------------------------------------------------
1 | % REP.m Replicate a matrix
2 | %
3 | % This function replicates a matrix in both dimensions.
4 | %
5 | % Syntax: MatOut = rep(MatIn,REPN);
6 | %
7 | % Input parameters:
8 | % MatIn - Input Matrix (before replicating)
9 | %
10 | % REPN - Vector of 2 numbers, how many replications in each dimension
11 | % REPN(1): replicate vertically
12 | % REPN(2): replicate horizontally
13 | %
14 | % Example:
15 | %
16 | % MatIn = [1 2 3]
17 | % REPN = [1 2]: MatOut = [1 2 3 1 2 3]
18 | % REPN = [2 1]: MatOut = [1 2 3;
19 | % 1 2 3]
20 | % REPN = [3 2]: MatOut = [1 2 3 1 2 3;
21 | % 1 2 3 1 2 3;
22 | % 1 2 3 1 2 3]
23 | %
24 | % Output parameter:
25 | % MatOut - Output Matrix (after replicating)
26 | %
27 | %
28 | % Author: Carlos Fonseca & Hartmut Pohlheim
29 | % History: 14.02.94 file created
30 | % 22.01.03 tested under MATLAB v6 by Alex Shenfield
31 |
32 | function MatOut = rep(MatIn,REPN)
33 |
34 | % Get size of input matrix
35 | [N_D,N_L] = size(MatIn);
36 |
37 | % Calculate
38 | Ind_D = rem(0:REPN(1)*N_D-1,N_D) + 1;
39 | Ind_L = rem(0:REPN(2)*N_L-1,N_L) + 1;
40 |
41 | % Create output matrix
42 | MatOut = MatIn(Ind_D,Ind_L);
43 |
44 | % End of function
--------------------------------------------------------------------------------
/resplot.m:
--------------------------------------------------------------------------------
1 | % RESPLOT.M (RESult PLOTing)
2 | %
3 | % This function plots some results during computation.
4 | %
5 | % Syntax: resplot(Chrom,IndAll,ObjV,Best,gen)
6 | %
7 | % Input parameters:
8 | % Chrom - Matrix containing the chromosomes of the current
9 | % population. Each line corresponds to one individual.
10 | % IndAll - Matrix containing the best individual (variables) of each
11 | % generation. Each line corresponds to one individual.
12 | % ObjV - Vector containing objective values of the current
13 | % generation
14 | % Best - Matrix containing the best and average Objective values of
15 | % each generation, [best value per generation,average value
16 | % per generation]
17 | % gen - Scalar containing the number of the current generation
18 | %
19 | % Output parameter:
20 | % no output parameter
21 | %
22 | % Author: Hartmut Pohlheim
23 | % History: 27.11.93 file created
24 | % 29.11.93 decision, if plot or not deleted
25 | % yscale not log
26 | % 15.12.93 MutMatrix as parameter and plot added
27 | % 16.03.94 function cleaned, MutMatrix removed, IndAll added
28 | % 22.01.03 tested under MATLAB v6 by Alex Shenfield
29 |
30 | function resplot(Chrom,IndAll,ObjV,Best,gen);
31 |
32 | % plot of best and mean value per generation
33 | subplot(2,2,1), plot(Best);
34 | title('Best and mean objective value');
35 | xlabel('generation'), ylabel('objective value');
36 |
37 | % plot of best individuals in all generations
38 | subplot(2,2,2), plot(IndAll);
39 | title(['Best individuals']);
40 | xlabel('generation'), ylabel('value of variable');
41 |
42 | % plot of variables of all individuals in current generation
43 | subplot(2,2,3), plot(Chrom');
44 | title(['All individuals in gen ',num2str(gen)]);
45 | xlabel('number of variable'), ylabel('value of variable');
46 |
47 | % plot of all objective values in current generation
48 | subplot(2,2,4), plot(ObjV,'y.');
49 | title(['All objective values']);
50 | xlabel('number of individual'), ylabel('objective value');
51 |
52 | drawnow;
53 |
54 |
55 | % End of function
--------------------------------------------------------------------------------
/rws.m:
--------------------------------------------------------------------------------
1 | % RWS.m - Roulette Wheel Selection
2 | %
3 | % Syntax:
4 | % NewChrIx = rws(FitnV, Nsel)
5 | %
6 | % This function selects a given number of individuals Nsel from a
7 | % population. FitnV is a column vector containing the fitness
8 | % values of the individuals in the population.
9 | %
10 | % The function returns another column vector containing the
11 | % indexes of the new generation of chromosomes relative to the
12 | % original population matrix, shuffled. The new population, ready
13 | % for mating, can be obtained by calculating
14 | % OldChrom(NewChrIx, :).
15 | %
16 | % Author: Carlos Fonseca, Updated: Andrew Chipperfield
17 | % Date: 04/10/93, Date: 27-Jan-94
18 | %
19 | % Tested under MATLAB v6 by Alex Shenfield (22-Jan-03)
20 |
21 | function NewChrIx = rws(FitnV,Nsel);
22 |
23 | % Identify the population size (Nind)
24 | [Nind,ans] = size(FitnV);
25 |
26 | % Perform Stochastic Sampling with Replacement
27 | cumfit = cumsum(FitnV);
28 | trials = cumfit(Nind) .* rand(Nsel, 1);
29 | Mf = cumfit(:, ones(1, Nsel));
30 | Mt = trials(:, ones(1, Nind))';
31 | [NewChrIx, ans] = find(Mt < Mf & ...
32 | [ zeros(1, Nsel); Mf(1:Nind-1, :) ] <= Mt);
33 | % end of function
--------------------------------------------------------------------------------
/scaling.m:
--------------------------------------------------------------------------------
1 | % SCALING.m - linear fitness scaling
2 | %
3 | % This function implements a linear fitness scaling algorithm as described
4 | % by Goldberg in "Genetic Algorithms in Search, Optimization and Machine
5 | % Learning", Addison Wesley, 1989. It use is not recommended when fitness
6 | % functions produce negative results as the scaling will become unreliable.
7 | % It is included in this version of the GA Toolbox only for the sake of
8 | % completeness.
9 | %
10 | % Syntax: FitnV = scaling(ObjV, Smul)
11 | %
12 | % Input parameters:
13 | %
14 | % Objv - A vector containing the values of individuals
15 | % fitness.
16 | %
17 | % Smul - Optional scaling parameter (default 2).
18 | %
19 | % Output parameters:
20 | %
21 | % FitnV - A vector containing the individual fitnesses
22 | % for the current population.
23 | %
24 | % Author: Andrew Chipperfield
25 | % Date: 24-Feb-94
26 |
27 | function FitnV = scaling( ObjV, Smul )
28 |
29 | if nargin == 1
30 | Smul = 2 ;
31 | end
32 |
33 | [Nind, Nobj] = size( ObjV ) ;
34 | Oave = sum( ObjV ) / Nind ;
35 | Omin = min( ObjV ) ;
36 | Omax = max( ObjV ) ;
37 |
38 | if (Omin > ( Smul * Oave - Omax ) / ( Smul - 1.0 ))
39 | delta = Omax - Oave
40 | a = ( Smul - 1.0 ) * Oave / delta
41 | b = Oave * ( Omax - Smul * Oave ) / delta
42 | else
43 | delta = Oave - Omin ;
44 | a = Oave / delta ;
45 | b = -Omin * Oave / delta ;
46 | end
47 |
48 | FitnV = ObjV.*a + b ;
--------------------------------------------------------------------------------
/select.m:
--------------------------------------------------------------------------------
1 | % SELECT.M (universal SELECTion)
2 | %
3 | % This function performs universal selection. The function handles
4 | % multiple populations and calls the low level selection function
5 | % for the actual selection process.
6 | %
7 | % Syntax: SelCh = select(SEL_F, Chrom, FitnV, GGAP, SUBPOP)
8 | %
9 | % Input parameters:
10 | % SEL_F - Name of the selection function
11 | % Chrom - Matrix containing the individuals (parents) of the current
12 | % population. Each row corresponds to one individual.
13 | % FitnV - Column vector containing the fitness values of the
14 | % individuals in the population.
15 | % GGAP - (optional) Rate of individuals to be selected
16 | % if omitted 1.0 is assumed
17 | % SUBPOP - (optional) Number of subpopulations
18 | % if omitted 1 subpopulation is assumed
19 | %
20 | % Output parameters:
21 | % SelCh - Matrix containing the selected individuals.
22 | %
23 | % Author: Hartmut Pohlheim
24 | % History: 10.03.94 file created
25 | % 22.01.03 tested under MATLAB v6 by Alex Shenfield
26 |
27 | function SelCh = select(SEL_F, Chrom, FitnV, GGAP, SUBPOP);
28 |
29 | % Check parameter consistency
30 | if nargin < 3, error('Not enough input parameter'); end
31 |
32 | % Identify the population size (Nind)
33 | [NindCh,Nvar] = size(Chrom);
34 | [NindF,VarF] = size(FitnV);
35 | if NindCh ~= NindF, error('Chrom and FitnV disagree'); end
36 | if VarF ~= 1, error('FitnV must be a column vector'); end
37 |
38 | if nargin < 5, SUBPOP = 1; end
39 | if nargin > 4,
40 | if isempty(SUBPOP), SUBPOP = 1;
41 | elseif isnan(SUBPOP), SUBPOP = 1;
42 | elseif length(SUBPOP) ~= 1, error('SUBPOP must be a scalar'); end
43 | end
44 |
45 | if (NindCh/SUBPOP) ~= fix(NindCh/SUBPOP), error('Chrom and SUBPOP disagree'); end
46 | Nind = NindCh/SUBPOP; % Compute number of individuals per subpopulation
47 |
48 | if nargin < 4, GGAP = 1; end
49 | if nargin > 3,
50 | if isempty(GGAP), GGAP = 1;
51 | elseif isnan(GGAP), GGAP = 1;
52 | elseif length(GGAP) ~= 1, error('GGAP must be a scalar');
53 | elseif (GGAP < 0), error('GGAP must be a scalar bigger than 0'); end
54 | end
55 |
56 | % Compute number of new individuals (to select)
57 | NSel=max(floor(Nind*GGAP+.5),2);
58 |
59 | % Select individuals from population
60 | SelCh = [];
61 | for irun = 1:SUBPOP,
62 | FitnVSub = FitnV((irun-1)*Nind+1:irun*Nind);
63 | ChrIx=feval(SEL_F, FitnVSub, NSel)+(irun-1)*Nind;
64 | SelCh=[SelCh; Chrom(ChrIx,:)];
65 | end
66 |
67 |
68 | % End of function
--------------------------------------------------------------------------------
/sga.m:
--------------------------------------------------------------------------------
1 | % sga.m
2 | %
3 | % This script implements the Simple Genetic Algorithm described
4 | % in the examples section of the GA Toolbox manual.
5 | %
6 | % Author: Andrew Chipperfield
7 | % History: 23-Mar-94 file created
8 | %
9 | % tested under MATLAB v6 by Alex Shenfield (22-Jan-03)
10 |
11 | NIND = 40; % Number of individuals per subpopulations
12 | MAXGEN = 300; % maximum Number of generations
13 | GGAP = .9; % Generation gap, how many new individuals are created
14 | NVAR = 20; % Number of variables
15 | PRECI = 20; % Precision of binary representation
16 |
17 | % Build field descriptor
18 | FieldD = [rep(PRECI,[1, NVAR]); rep([-512;512],[1, NVAR]);...
19 | rep([1; 0; 1 ;1], [1, NVAR])];
20 |
21 | % Initialise population
22 | Chrom = crtbp(NIND, NVAR*PRECI);
23 |
24 | % Reset counters
25 | Best = NaN*ones(MAXGEN,1); % best in current population
26 | gen = 0; % generational counter
27 |
28 | % Evaluate initial population
29 | ObjV = objfun1(bs2rv(Chrom,FieldD));
30 |
31 | % Track best individual and display convergence
32 | Best(gen+1) = min(ObjV);
33 | plot(log10(Best),'ro');xlabel('generation'); ylabel('log10(f(x))');
34 | text(0.5,0.95,['Best = ', num2str(Best(gen+1))],'Units','normalized');
35 | drawnow;
36 |
37 | % Generational loop
38 | while gen < MAXGEN,
39 |
40 | % Assign fitness-value to entire population
41 | FitnV = ranking(ObjV);
42 |
43 | % Select individuals for breeding
44 | SelCh = select('sus', Chrom, FitnV, GGAP);
45 |
46 | % Recombine selected individuals (crossover)
47 | SelCh = recombin('xovsp',SelCh,0.7);
48 |
49 | % Perform mutation on offspring
50 | SelCh = mut(SelCh);
51 |
52 | % Evaluate offspring, call objective function
53 | ObjVSel = objfun1(bs2rv(SelCh,FieldD));
54 |
55 | % Reinsert offspring into current population
56 | [Chrom, ObjV]=reins(Chrom,SelCh,1,1,ObjV,ObjVSel);
57 |
58 | % Increment generational counter
59 | gen = gen+1;
60 |
61 | % Update display and record current best individual
62 | Best(gen+1) = min(ObjV);
63 | plot(log10(Best),'ro'); xlabel('generation'); ylabel('log10(f(x))');
64 | text(0.5,0.95,['Best = ', num2str(Best(gen+1))],'Units','normalized');
65 | drawnow;
66 | end
67 | % End of GA
--------------------------------------------------------------------------------
/sus.m:
--------------------------------------------------------------------------------
1 | % SUS.M (Stochastic Universal Sampling)
2 | %
3 | % This function performs selection with STOCHASTIC UNIVERSAL SAMPLING.
4 | %
5 | % Syntax: NewChrIx = sus(FitnV, Nsel)
6 | %
7 | % Input parameters:
8 | % FitnV - Column vector containing the fitness values of the
9 | % individuals in the population.
10 | % Nsel - number of individuals to be selected
11 | %
12 | % Output parameters:
13 | % NewChrIx - column vector containing the indexes of the selected
14 | % individuals relative to the original population, shuffled.
15 | % The new population, ready for mating, can be obtained
16 | % by calculating OldChrom(NewChrIx,:).
17 | %
18 | % Author: Hartmut Pohlheim (Carlos Fonseca)
19 | % History: 12.12.93 file created
20 | % 22.02.94 clean up, comments
21 | % 22.01.03 tested under MATLAB v6 by Alex Shenfield
22 |
23 | function NewChrIx = sus(FitnV,Nsel);
24 |
25 | % Identify the population size (Nind)
26 | [Nind,ans] = size(FitnV);
27 |
28 | % Perform stochastic universal sampling
29 | cumfit = cumsum(FitnV);
30 | trials = cumfit(Nind) / Nsel * (rand + (0:Nsel-1)');
31 | Mf = cumfit(:, ones(1, Nsel));
32 | Mt = trials(:, ones(1, Nind))';
33 | [NewChrIx, ans] = find(Mt < Mf & [ zeros(1, Nsel); Mf(1:Nind-1, :) ] <= Mt);
34 |
35 | % Shuffle new population
36 | [ans, shuf] = sort(rand(Nsel, 1));
37 | NewChrIx = NewChrIx(shuf);
38 |
39 |
40 | % End of function
--------------------------------------------------------------------------------
/xovdp.m:
--------------------------------------------------------------------------------
1 | % XOVDP.M (CROSSOVer Double Point)
2 | %
3 | % This function performs double point crossover between pairs of
4 | % individuals and returns the current generation after mating.
5 | %
6 | % Syntax: NewChrom = xovdp(OldChrom, XOVR)
7 | %
8 | % Input parameters:
9 | % OldChrom - Matrix containing the chromosomes of the old
10 | % population. Each line corresponds to one individual
11 | % (in any form, not necessarily real values).
12 | % XOVR - Probability of recombination occurring between pairs
13 | % of individuals.
14 | %
15 | % Output parameter:
16 | % NewChrom - Matrix containing the chromosomes of the population
17 | % after mating, ready to be mutated and/or evaluated,
18 | % in the same format as OldChrom.
19 |
20 | % Author: Hartmut Pohlheim
21 | % History: 28.03.94 file created
22 | % 22.01.03 tested under MATLAB v6 by Alex Shenfield
23 |
24 | function NewChrom = xovdp(OldChrom, XOVR);
25 |
26 | if nargin < 2, XOVR = NaN; end
27 |
28 | % call low level function with appropriate parameters
29 | NewChrom = xovmp(OldChrom, XOVR, 2, 0);
30 |
31 | % End of function
--------------------------------------------------------------------------------
/xovdprs.m:
--------------------------------------------------------------------------------
1 | % XOVDPRS.M (CROSSOVer Double-Point with Reduced Surrogate)
2 | %
3 | % This function performs double-point 'reduced surrogate' crossover between
4 | % pairs of individuals and returns the current generation after mating.
5 | %
6 | % Syntax: NewChrom = xovdprs(OldChrom, XOVR)
7 | %
8 | % Input parameters:
9 | % OldChrom - Matrix containing the chromosomes of the old
10 | % population. Each line corresponds to one individual
11 | % (in any form, not necessarily real values).
12 | % XOVR - Probability of recombination occurring between pairs
13 | % of individuals.
14 | %
15 | % Output parameter:
16 | % NewChrom - Matrix containing the chromosomes of the population
17 | % after mating, ready to be mutated and/or evaluated,
18 | % in the same format as OldChrom.
19 | %
20 | % Author: Hartmut Pohlheim
21 | % History: 28.03.94 file created
22 | % 22.01.03 tested under MATLAB v6 by Alex Shenfield
23 |
24 | function NewChrom = xovdprs(OldChrom, XOVR);
25 |
26 | if nargin < 2, XOVR = NaN; end
27 |
28 | % call low-level function with appropriate parameters
29 | NewChrom = xovmp(OldChrom, XOVR, 2, 1);
30 |
31 | % End of function
--------------------------------------------------------------------------------
/xovmp.m:
--------------------------------------------------------------------------------
1 | % XOVMP.m Multi-point crossover
2 | %
3 | % Syntax: NewChrom = xovmp(OldChrom, Px, Npt, Rs)
4 | %
5 | % This function takes a matrix OldChrom containing the binary
6 | % representation of the individuals in the current population,
7 | % applies crossover to consecutive pairs of individuals with
8 | % probability Px and returns the resulting population.
9 | %
10 | % Npt indicates how many crossover points to use (1 or 2, zero
11 | % indicates shuffle crossover).
12 | % Rs indicates whether or not to force the production of
13 | % offspring different from their parents.
14 | %
15 | %
16 | % Author: Carlos Fonseca, Updated: Andrew Chipperfield
17 | % Date: 28/09/93, Date: 27-Jan-94
18 | %
19 | % tested under MATLAB v6 by Alex Shenfield (22-Jan-03)
20 |
21 | function NewChrom = xovmp(OldChrom, Px, Npt, Rs);
22 |
23 | % Identify the population size (Nind) and the chromosome length (Lind)
24 | [Nind,Lind] = size(OldChrom);
25 |
26 | if Lind < 2, NewChrom = OldChrom; return; end
27 |
28 | if nargin < 4, Rs = 0; end
29 | if nargin < 3, Npt = 0; Rs = 0; end
30 | if nargin < 2, Px = 0.7; Npt = 0; Rs = 0; end
31 | if isnan(Px), Px = 0.7; end
32 | if isnan(Npt), Npt = 0; end
33 | if isnan(Rs), Rs = 0; end
34 | if isempty(Px), Px = 0.7; end
35 | if isempty(Npt), Npt = 0; end
36 | if isempty(Rs), Rs = 0; end
37 |
38 | Xops = floor(Nind/2);
39 | DoCross = rand(Xops,1) < Px;
40 | odd = 1:2:Nind-1;
41 | even = 2:2:Nind;
42 |
43 | % Compute the effective length of each chromosome pair
44 | Mask = ~Rs | (OldChrom(odd, :) ~= OldChrom(even, :));
45 | Mask = cumsum(Mask')';
46 |
47 | % Compute cross sites for each pair of individuals, according to their
48 | % effective length and Px (two equal cross sites mean no crossover)
49 | xsites(:, 1) = Mask(:, Lind);
50 | if Npt >= 2,
51 | xsites(:, 1) = ceil(xsites(:, 1) .* rand(Xops, 1));
52 | end
53 | xsites(:,2) = rem(xsites + ceil((Mask(:, Lind)-1) .* rand(Xops, 1)) ...
54 | .* DoCross - 1 , Mask(:, Lind) )+1;
55 |
56 | % Express cross sites in terms of a 0-1 mask
57 | Mask = (xsites(:,ones(1,Lind)) < Mask) == ...
58 | (xsites(:,2*ones(1,Lind)) < Mask);
59 |
60 | if ~Npt,
61 | shuff = rand(Lind,Xops);
62 | [ans,shuff] = sort(shuff);
63 | for i=1:Xops
64 | OldChrom(odd(i),:)=OldChrom(odd(i),shuff(:,i));
65 | OldChrom(even(i),:)=OldChrom(even(i),shuff(:,i));
66 | end
67 | end
68 |
69 | % Perform crossover
70 | NewChrom(odd,:) = (OldChrom(odd,:).* Mask) + (OldChrom(even,:).*(~Mask));
71 | NewChrom(even,:) = (OldChrom(odd,:).*(~Mask)) + (OldChrom(even,:).*Mask);
72 |
73 | % If the number of individuals is odd, the last individual cannot be mated
74 | % but must be included in the new population
75 | if rem(Nind,2),
76 | NewChrom(Nind,:)=OldChrom(Nind,:);
77 | end
78 |
79 | if ~Npt,
80 | [ans,unshuff] = sort(shuff);
81 | for i=1:Xops
82 | NewChrom(odd(i),:)=NewChrom(odd(i),unshuff(:,i));
83 | NewChrom(even(i),:)=NewChrom(even(i),unshuff(:,i));
84 | end
85 | end
86 |
87 | % end of function
--------------------------------------------------------------------------------
/xovsh.m:
--------------------------------------------------------------------------------
1 | % XOVSH.M (CROSSOVer SHuffle)
2 | %
3 | % This function performs shuffle crossover between pairs of
4 | % individuals and returns the current generation after mating.
5 | %
6 | % Syntax: NewChrom = xovsh(OldChrom, XOVR)
7 | %
8 | % Input parameters:
9 | % OldChrom - Matrix containing the chromosomes of the old
10 | % population. Each line corresponds to one individual
11 | % (in any form, not necessarily real values).
12 | % XOVR - Probability of recombination occurring between pairs
13 | % of individuals.
14 | %
15 | % Output parameter:
16 | % NewChrom - Matrix containing the chromosomes of the population
17 | % after mating, ready to be mutated and/or evaluated,
18 | % in the same format as OldChrom.
19 | %
20 | % Author: Hartmut Pohlheim
21 | % History: 28.03.94 file created
22 | % 22.01.03 tested under MATLAB v6 by Alex Shenfield
23 |
24 | function NewChrom = xovsh(OldChrom, XOVR);
25 |
26 | if nargin < 2, XOVR = NaN; end
27 |
28 | % call low level function with appropriate parameters
29 | NewChrom = xovmp(OldChrom, XOVR, 0, 0);
30 |
31 | % End of function
--------------------------------------------------------------------------------
/xovshrs.m:
--------------------------------------------------------------------------------
1 | % XOVSHRS.M (CROSSOVer SHuffle with Reduced Surrogate)
2 | %
3 | % This function performs shuffle 'reduced surrogate' crossover between
4 | % pairs of individuals and returns the current generation after mating.
5 | %
6 | % Syntax: NewChrom = xovshrs(OldChrom, XOVR)
7 | %
8 | % Input parameters:
9 | % OldChrom - Matrix containing the chromosomes of the old
10 | % population. Each line corresponds to one individual
11 | % (in any form, not necessarily real values).
12 | % XOVR - Probability of recombination occurring between pairs
13 | % of individuals.
14 | %
15 | % Output parameter:
16 | % NewChrom - Matrix containing the chromosomes of the population
17 | % after mating, ready to be mutated and/or evaluated,
18 | % in the same format as OldChrom.
19 | %
20 | % Author: Hartmut Pohlheim
21 | % History: 28.03.94 file created
22 | % 22.01.03 tested under MATLAB v6 by Alex Shenfield
23 |
24 | function NewChrom = xovshrs(OldChrom, XOVR);
25 |
26 | if nargin < 2, XOVR = NaN; end
27 |
28 | % call low level function with appropriate parameters
29 | NewChrom = xovmp(OldChrom, XOVR, 0, 1);
30 |
31 | % End of function
--------------------------------------------------------------------------------
/xovsp.m:
--------------------------------------------------------------------------------
1 | % XOVSP.M (CROSSOVer Single-Point)
2 | %
3 | % This function performs single-point crossover between pairs of
4 | % individuals and returns the current generation after mating.
5 | %
6 | % Syntax: NewChrom = xovsp(OldChrom, XOVR)
7 | %
8 | % Input parameters:
9 | % OldChrom - Matrix containing the chromosomes of the old
10 | % population. Each line corresponds to one individual
11 | % (in any form, not necessarily real values).
12 | % XOVR - Probability of recombination occurring between pairs
13 | % of individuals.
14 | %
15 | % Output parameter:
16 | % NewChrom - Matrix containing the chromosomes of the population
17 | % after mating, ready to be mutated and/or evaluated,
18 | % in the same format as OldChrom.
19 | %
20 | % Author: Hartmut Pohlheim
21 | % History: 28.03.94 file created
22 | % 22.01.03 tested under MATLAB v6 by Alex Shenfield
23 |
24 | function NewChrom = xovsp(OldChrom, XOVR);
25 |
26 | if nargin < 2, XOVR = NaN; end
27 |
28 | % call low level function with appropriate parameters
29 | NewChrom = xovmp(OldChrom, XOVR, 1, 0);
30 |
31 |
32 | % End of function
--------------------------------------------------------------------------------
/xovsprs.m:
--------------------------------------------------------------------------------
1 | % XOVSPRS.M (CROSSOVer Single-Point with Reduced Surrogate)
2 | %
3 | % This function performs single-point 'reduced surrogate' crossover between
4 | % pairs of individuals and returns the current generation after mating.
5 | %
6 | % Syntax: NewChrom = xovsprs(OldChrom, XOVR)
7 | %
8 | % Input parameters:
9 | % OldChrom - Matrix containing the chromosomes of the old
10 | % population. Each line corresponds to one individual
11 | % (in any form, not necessarily real-values).
12 | % XOVR - Probability of recombination occurring between pairs
13 | % of individuals.
14 | %
15 | % Output parameter:
16 | % NewChrom - Matrix containing the chromosomes of the population
17 | % after mating, ready to be mutated and/or evaluated,
18 | % in the same format as OldChrom.
19 | %
20 | % Author: Hartmut Pohlheim
21 | % History: 28.03.94 file created
22 | % 22.01.03 tested under MATLAB v6 by Alex Shenfield
23 |
24 | function NewChrom = xovsprs(OldChrom, XOVR);
25 |
26 | if nargin < 2, XOVR = NaN; end
27 |
28 | % call low-level function with appropriate parameters
29 | NewChrom = xovmp(OldChrom, XOVR, 1, 1);
30 |
31 | % End of function
--------------------------------------------------------------------------------