├── .gitignore ├── FitnessFunction.h ├── LICENSE ├── PSO.cpp ├── PSO.h ├── README.md └── main_test.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | -------------------------------------------------------------------------------- /FitnessFunction.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "PSO.h" 3 | #include 4 | #define PI 3.1415926 5 | 6 | double FitnessFunction(Particle& particle); 7 | 8 | double FitnessFunction(Particle& particle) 9 | { 10 | double x = particle.position_[0]; 11 | double y = particle.position_[1]; 12 | double temp = sqrt(x * x + y * y); 13 | double fitness = sin(temp) / temp + exp(0.5* cos(2 * PI * x) + 0.5 * cos(2 * PI * y)) - 2.71289; 14 | return fitness; 15 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 hgpvision 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /PSO.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hgpvision/PSO/31164fbe56a0516e2eaff2619b71c22ea7d04b71/PSO.cpp -------------------------------------------------------------------------------- /PSO.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hgpvision/PSO/31164fbe56a0516e2eaff2619b71c22ea7d04b71/PSO.h -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PSO 2 | 较为通用的粒子群优化算法(变量维度可变) 3 | 4 | # 使用 5 | 6 | 编写适应度函数及主函数即可,无任何依赖项 7 | 8 | # 示例 9 | 10 | 求解f(x) = sin(sqrt(x^2+y^2))/sqrt(x^2+y^2) + exp((cos(2\*pi\*x)+cos(2\*pi\*y))/2) - 2.71289的极大值。该函数有很多局部极大值点,而极限位置为(0,0),在(0,0)附近取得极大值。 11 | 12 | 编写适应度函数如`FitnessFunction.h`,编写主函数如`main_test.cpp`,运行过程会打印出每次迭代的结果,最终得到的结果接近(0,0),适应度值为1.00539。 13 | 14 | 示例是一个非常简单的例子,其中变量维度为2(坐标为二维),本程序可以适用于不同变量维度,更改对应参数即可。 15 | -------------------------------------------------------------------------------- /main_test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hgpvision/PSO/31164fbe56a0516e2eaff2619b71c22ea7d04b71/main_test.cpp --------------------------------------------------------------------------------