人工鱼群算法详见文献-李晓磊. 一种新型的智能优化方法-人工鱼群算法[D]. 杭州: 浙江大学, 2003.
基于侧向光散射,根本不同角度下的光能分布计算颗粒系粒径分布函数的两个参数。

#pragma once
#include<vector>
#include<iostream>
#include<string>
using namespace std;
class fishswarm
{public:fishswarm(int fishnum,int Maxgen,int trynumer,double visual,double delta,double step, int T_number, int D_number);~fishswarm();void file_to_string(vector<string> &record, const string& line, char delimiter);double string_to_float(string str);void ReadFile(string T_file_name,string Emes_file_name);double Normal(double x, double miu, double sigma); //概率密度函数double FoodConsistence();//初始的所有浓度 real为测量值double Dist(double Xi[1][2], vector<vector<double>> X, vector<vector<double>> &D);double singecon(double x1, double x2);double Prey(double Xi[1][2], int i, double visual, double step, double try_number, double FieldDR[2][2], vector<vector<double>>LastY, double XI1[1][2]);double Swarm(vector<vector<double>>Xi, int i, double visual, double step, double delta, double try_number, double FieldDR[2][2], vector<vector<double>>LastY, double XI1[1][2], double YI1);double Follow(vector<vector<double>>Xi, int i, double visual, double step, double delta, double try_number, double FieldDR[2][2], vector<vector<double>>LastY, double XI2[1][2], double YI2);double start();
private:int fishnum;int Maxgen;int trynumber;double visual;double delta;double step;double FieldDR[2][2];int flag;int T_number;int D_number;vector<vector<double>>X;vector<vector<double>>E_mes;//光能分布测量值vector<vector<double>>T;vector<vector<double>>Y;
};
#include "fishswarm.h"
#include "math.h"
#include "time.h"
#include<iostream>
#include<stdlib.h>
#include<fstream>
#include<iomanip>
#include <streambuf>
#include<string>
#include<vector>
#include<algorithm>
#include<numeric>
#include <direct.h>
#define M_PI 3.14159265358979323846
#include "ctime"
const int N = 999;
using namespace std;
fishswarm::fishswarm(int fishnum, int Maxgen, int trynumer, double visual, double delta, double step,int T_number,int D_number)
{X = vector<vector<double>>(fishnum, vector<double>(2));Y = vector<vector<double>>(fishnum, vector<double>(1));T = vector<vector<double>>(T_number, vector<double>(D_number,1));E_mes = vector<vector<double>>(T_number, vector<double>(1));this->fishnum = fishnum;this->Maxgen= Maxgen;this->trynumber = trynumber;this->visual = visual;this->delta = delta;this->step = step;this->T_number = T_number;this->D_number = D_number;FieldDR[0][0] = 0.1;FieldDR[0][1] = 3;FieldDR[1][0] = 15;FieldDR[1][1] = 15;double randnumber;srand((unsigned)time(NULL));for (int i = 0; i < fishnum; i++) {randnumber = rand() % 100 / (double)101;X[i][0] = FieldDR[0][0] + (FieldDR[1][0] - FieldDR[0][0])*randnumber;}for (int i = 0; i < fishnum; i++) {randnumber = rand() % 100 / (double)101;X[i][1] = FieldDR[0][1] + (FieldDR[1][1] - FieldDR[0][1])*randnumber;}/*for (int i = 0; i < fishnum; i++) {for (int j = 0; j < 2; j++) {cout << X[i][j] << "  ";}cout << endl;}*/
}fishswarm::~fishswarm()
{}void fishswarm::file_to_string(vector<string> &record, const string& line, char delimiter)
{int linepos = 0;char c;int linemax = line.length();string curstring;record.clear();while (linepos<linemax){c = line[linepos];if (isdigit(c) || c == '.') {curstring += c;}else if (c == delimiter&&curstring.size()) {record.push_back(curstring);curstring = "";}++linepos;}if (curstring.size())record.push_back(curstring);return;
}double fishswarm::string_to_float(string str) {int i = 0, len = str.length();double sum = 0;while (i<len) {if (str[i] == '.') break;sum = sum * 10 + str[i] - '0';++i;}++i;double t = 1, d = 1;while (i<len) {d *= 0.1;t = str[i] - '0';sum += t*d;++i;}return sum;
}
void fishswarm::ReadFile(string T_file_name, string Emes_file_name) {vector<string> row;vector<double>b;string line;string filename;char path_dir[255];char p[] = { "\\" };_getcwd(path_dir, sizeof(path_dir));string dir;dir = path_dir;dir = dir + "\\" + T_file_name;ifstream in_T(dir);if (in_T.fail()) { cout << "File not found" << endl; return; }int i = 0;while (getline(in_T, line) && in_T.good()){file_to_string(row, line, ','); for (int i = 0, leng = row.size(); i<leng; i++) {b.push_back(string_to_float(row[i]));}T[i] = b;b.clear();i = i + 1;}in_T.close();i = 0;vector<string> row1;vector<double>bb;string line1;string filename1;dir = path_dir;dir = dir + "\\" + Emes_file_name;ifstream in_E(dir);if (in_E.fail()) { cout << "File not found" << endl; return; }while (getline(in_E, line1) && in_E.good()){file_to_string(row1, line1, ',');for (int i = 0, leng = row1.size(); i<leng; i++) {bb.push_back(string_to_float(row1[i]));}E_mes[i]=bb;i = i + 1;bb.clear();}in_E.close();
}double fishswarm::Normal(double x, double miu, double sigma)
{//return 0.1 / (sqrt(2 * M_PI)*sigma) * exp(-1 * (x - miu)*(x - miu) / (2 * sigma*sigma));return 0.1*sigma / miu*pow(x / miu, sigma - 1)*exp(-pow(x / miu, sigma));
}
double fishswarm::FoodConsistence()
{int fnum = X.size();vector<double>mu(fnum);vector<double>sig(fnum);vector<double>d(D_number);for (int i = 0; i < fnum; i++) {mu[i] = X[i][0];sig[i] = X[i][1];}double j = 0;for (int i = 0; i < D_number; i++) {d[i] = j + 0.1;j = j + 0.1;}vector<vector<double>>value(D_number, vector<double>(1));vector<vector<double>>E_value(T_number, vector<double>(1));double YYY = { 0 };for (int i = 0; i < fnum; i++) {for (int jj = 0; jj < D_number; jj++) {value[jj][0] = Normal(d[jj], mu[i], sig[i]);}for (int p = 0; p < T_number; p++) {for (int q = 0; q < 1; q++) {for (int t = 0; t < D_number; t++) {E_value[p][q] += T[p][t] * value[t][q];}}}double maxv = 0;for (int tt = 0; tt < T_number; tt++) {if (E_value[tt][0] > maxv) {maxv = E_value[tt][0];}}for (int tt = 0; tt < T_number; tt++) {E_value[tt][0] = E_value[tt][0] / maxv;}for (int n = 0; n < T_number; n++) {YYY = YYY + (E_value[n][0] - E_mes[n][0])*(E_value[n][0] - E_mes[n][0]);}Y[i][0] = 1 / YYY;YYY = 0;for (int tt = 0; tt < T_number; tt++) {E_value[tt][0] = 0;}}return 0;
}double fishswarm::Dist(double Xi[1][2], vector<vector<double>> X, vector<vector<double>> &D) {int fnum = X.size();for (int j = 0; j < fnum; j++) {D[j][0] = sqrt(pow((Xi[0][0] - X[j][0]), 2) + pow(Xi[0][1] - X[j][1], 2));}return 0;
}
double fishswarm::singecon(double x1, double x2) {vector<vector<double>>value(D_number, vector<double>(1));vector<vector<double>>E_value(T_number, vector<double>(1));vector<double>d(D_number);double jj = 0;for (int i = 0; i < D_number; i++) {d[i] = jj + 0.1;jj = jj + 0.1;}for (int i = 0; i < D_number; i++) {value[i][0] = Normal(d[i], x1, x2);}for (int p = 0; p < T_number; p++) {for (int q = 0; q < 1; q++) {for (int t = 0; t < D_number; t++) {E_value[p][q] += T[p][t] * value[t][q];}}}double maxv = 0;for (int tt = 0; tt < T_number; tt++) {if (E_value[tt][0] > maxv) {maxv = E_value[tt][0];}}for (int tt = 0; tt < T_number; tt++) {E_value[tt][0] = E_value[tt][0] / maxv;}double YYY = 0;for (int n = 0; n < T_number; n++) {YYY = YYY + (E_value[n][0] - E_mes[n][0])*(E_value[n][0] - E_mes[n][0]);}return 1 / YYY;
}double fishswarm::Prey(double xi[1][2], int ii, double visual, double step, double try_number, double FieldDR[2][2], vector<vector<double>>LastY, double XI1[1][2]) {double Yi, Yj, Xj1, Xj2; double YYYY = { 0 };Yi = LastY[ii][0]; srand((unsigned)time(NULL)); double randnumber;for (int i = 0; i < try_number; i++) {randnumber = rand() % 100 / (double)101;Xj1 = xi[0][0] + (2 * randnumber - 1)*visual;randnumber = rand() % 100 / (double)101;Xj2 = xi[0][1] + (2 * randnumber - 1)*visual;Yj = singecon(Xj1, Xj2);if (Yi < Yj) {randnumber = rand() % 100 / (double)101;XI1[0][0] = xi[0][0] + randnumber*step*(Xj1 - xi[0][0]) / sqrt(pow((Xj1 - xi[0][0]), 2) + pow((Xj2 - xi[0][1]), 2));XI1[0][1] = xi[0][1] + randnumber*step*(Xj2 - xi[0][1]) / sqrt(pow((Xj1 - xi[0][0]), 2) + pow((Xj2 - xi[0][1]), 2));for (int j = 0; j < 2; j++) {if (XI1[0][j] > FieldDR[1][j]) {XI1[0][j] = FieldDR[1][j];}if (XI1[0][j] < FieldDR[0][j]) {XI1[0][j] = FieldDR[0][j];}}break;}}if (XI1[0][0] == 0) {randnumber = rand() % 100 / (double)101;XI1[0][0] = xi[0][0] + (2 * randnumber - 1)*visual;randnumber = rand() % 100 / (double)101;XI1[0][1] = xi[0][1] + (2 * randnumber - 1)*visual;for (int j = 0; j < 2; j++) {if (XI1[0][j] > FieldDR[1][j]) {XI1[0][j] = FieldDR[1][j];}if (XI1[0][j] < FieldDR[0][j]) {XI1[0][j] = FieldDR[0][j];}}}YYYY = singecon(XI1[0][0], XI1[0][1]);return YYYY;
}
double fishswarm::Swarm(vector<vector<double>>X, int i, double visual, double step, double delta, double try_number, double FieldDR[2][2], vector<vector<double>>LastY, double XI1[1][2], double YI1) {int fnum = X.size();double xi[1][2]; vector<vector<double>>D(fnum, vector<double>(1));  int jj = 0; double Yc = 0; double Yi;for (int p = 0; p < 2; p++) {xi[0][p] = X[i][p];}Dist(xi, X, D);int number = 0;for (int j = 0; j < fnum; j++) {if (D[j][0]<visual) {number = number + 1;}}vector<int> index(number); vector<vector<double> > Xc(number);for (unsigned int j = 0; j < Xc.size(); j++) {Xc[j].resize(2);}for (int j = 0, kk = 0; j < fnum; j++) {if (D[j][0]<visual) {index[kk] = j;kk = kk + 1;}}if (number > 0) {for (int j = 0; j < number; j++) {for (int k = 0; k < 2; k++) {jj = index[j];Xc[j][k] = X[jj][k];}}double Xc1 = 0, Xc2 = 0;//两个优化参数for (int j = 0; j < number; j++) {Xc1 = Xc1 + Xc[j][0] / number;Xc2 = Xc2 + Xc[j][1] / number;}Yc = singecon(Xc1, Xc2);Yi = LastY[i][0]; srand((unsigned)time(NULL)); double randnumber;if (Yc / number > delta*Yi) {randnumber = rand() % 100 / (double)101;XI1[0][0] = xi[0][0] + randnumber*step*(Xc1 - xi[0][0]) / sqrt(pow((Xc1 - xi[0][0]), 2) + pow((Xc2 - xi[0][1]), 2));XI1[0][1] = xi[0][1] + randnumber*step*(Xc2 - xi[0][1]) / sqrt(pow((Xc1 - xi[0][0]), 2) + pow((Xc2 - xi[0][1]), 2));for (int j = 0; j < 2; j++) {if (XI1[0][j] > FieldDR[1][j]) {XI1[0][j] = FieldDR[1][j];}if (XI1[0][j] < FieldDR[0][j]) {XI1[0][j] = FieldDR[0][j];}}YI1 = singecon(XI1[0][0], XI1[0][1]);}else{YI1 = Prey(xi, i, visual, step, try_number, FieldDR, LastY, XI1);}}else {YI1 = Prey(xi, i, visual, step, try_number, FieldDR, LastY, XI1);}return YI1;
}
double fishswarm::Follow(vector<vector<double>>X, int i, double visual, double step, double delta, double try_number, double FieldDR[2][2], vector<vector<double>>LastY, double XI2[1][2], double YI2) {int fnum = X.size();vector<vector<double>>D(fnum, vector<double>(1));double xi[1][2];  int jj = 0; double Yc = 0; double Yi;for (int p = 0; p < 2; p++) {xi[0][p] = X[i][p];}Dist(xi, X, D);int number = 0;for (int j = 0; j < fnum; j++) {if (D[j][0]<visual) {number = number + 1;}}vector<int> index(number); vector<vector<double> > Xc(number); vector<vector<double> > YY(number);for (unsigned int j = 0; j <YY.size(); j++) {YY[j].resize(1);}for (unsigned int j = 0; j < Xc.size(); j++) {Xc[j].resize(2);}for (int j = 0, kk = 0; j < fnum; j++) {if (D[j][0]<visual) {index[kk] = j;kk = kk + 1;}}if (number > 0) {for (int j = 0; j < number; j++) {for (int k = 0; k < 2; k++) {jj = index[j];Xc[j][k] = X[jj][k];}}for (int j = 0; j < number; j++) {YY[j][0] = singecon(Xc[j][0], Xc[j][1]);}int max_index; double Ymax = 0; double Xmax1, Xmax2;for (int j = 0; j < number; j++) {if (YY[j][0] > Ymax) {max_index = j;Ymax = YY[j][0];}}Xmax1 = Xc[max_index][0]; Xmax2 = Xc[max_index][1];Yi = LastY[i][0]; srand((unsigned)time(NULL)); double randnumber;if (Ymax / number > delta*Yi) {randnumber = rand() % 100 / (double)101;XI2[0][0] = xi[0][0] + randnumber*step*(Xmax1 - xi[0][0]) / sqrt(pow((Xmax1 - xi[0][0]), 2) + pow((Xmax2 - xi[0][1]), 2));XI2[0][1] = xi[0][1] + randnumber*step*(Xmax1 - xi[0][1]) / sqrt(pow((Xmax1 - xi[0][0]), 2) + pow((Xmax2 - xi[0][1]), 2));for (int j = 0; j < 2; j++) {if (XI2[0][j] > FieldDR[1][j]) {XI2[0][j] = FieldDR[1][j];}if (XI2[0][j] < FieldDR[0][j]) {XI2[0][j] = FieldDR[0][j];}}YI2 = singecon(XI2[0][0], XI2[0][1]);}else{YI2 = Prey(xi, i, visual, step, try_number, FieldDR, LastY, XI2);}}else{YI2 = Prey(xi, i, visual, step, try_number, FieldDR, LastY, XI2);}return YI2;
}double fishswarm::start() {vector<vector<double>>BestX(Maxgen + 1, vector<double>(2));vector<vector<double>>BestY(Maxgen + 1, vector<double>(1));int gen = 0;double besty = 0;double bestx[1][2];double XI1[1][2] = { 0 }; double YI1 = { 0 }; double XI2[1][2] = { 0 }; double YI2 = { 0 };clock_t startTime, endTime;startTime = clock();while (gen <= Maxgen) {for (int i = 0; i < fishnum; i++) {YI1 = Swarm(X, i, visual, step, delta, trynumber, FieldDR, Y, XI1, YI1);YI2 = Follow(X, i, visual, step, delta, trynumber, FieldDR, Y, XI2, YI2);if (YI1 > YI2) {X[i][0] = XI1[0][0];X[i][1] = XI1[0][1];Y[i][0] = YI1;}else{X[i][0] = XI2[0][0];X[i][1] = XI2[0][1];Y[i][0] = YI2;}}int max_index; double Y_max = 0;for (int j = 0; j <fishnum; j++) {if (Y[j][0] > Y_max) {max_index = j;Y_max = Y[j][0];}}if (Y_max > besty) {besty = Y_max;bestx[0][0] = X[max_index][0];bestx[0][1] = X[max_index][1];BestY[gen][0] = Y_max;BestX[gen][0] = X[max_index][0];BestX[gen][1] = X[max_index][1];}else {BestY[gen][0] = BestY[gen - 1][0];BestX[gen][0] = BestX[gen - 1][0];BestX[gen][1] = BestX[gen - 1][1];}cout << BestX[gen][0] << "  ";cout << BestX[gen][1] << "  ";cout << BestY[gen][0] << endl;gen = gen + 1;cout << gen << endl;}endTime = clock();cout << "The run time is:" << (double)clock() / CLOCKS_PER_SEC << "s" << endl;ofstream outfile;outfile.open("dir\\filename.txt");outfile << "bestx1" << "   ";outfile << "bestx2" << "   ";outfile << "besty" << endl;for (int i = 0; i < gen; i++) {outfile << BestX[i][0] << "  ";outfile << BestX[i][1] << "  ";outfile << BestY[i][0] << endl;}outfile.close();cout << bestx[0][0] << " " << bestx[0][1] << endl;double j = 0; double D[200] = { 0 }; double dis[200]; double a = 0;for (int i = 0; i < 200; i++) {D[i] = j + 0.1;j = j + 0.1;}for (int i = 0; i < 200; i++) {dis[i] = Normal(D[i], bestx[0][0], bestx[0][1]);a = a + dis[i];cout << D[i] << "   " << dis[i] * 100 << endl;}cout << a;return 0;}

人工鱼群算法在颗粒粒径测量中应用相关推荐

  1. [机器学习]人工鱼群算法

    转载于http://blog.csdn.net/wp_csdn/article/details/54577567 1.起源   人工鱼群算法是李晓磊等人于2002年在动物群体智能行为研究的基础上提出的 ...

  2. Matlab基于人工鱼群算法求解TSP问题

    一. 人工鱼的结构模型 人工鱼是真实鱼抽象化.虚拟化的一个实体,其中封装了自身数据和一系列行为,可以接受环境的刺激信息,做出相应的活动.其所在的环境由问题的解空间和其他人工鱼的状态,它在下一时刻的行为 ...

  3. 【预测模型】基于人工鱼群算法优化BP神经网络实现数据预测matlab源码

    1 算法介绍 1.1 BP神经网络 1. 反向传播算法应用领域 反向传播算法应用较为广泛,从字面意思理解,与前向传播相互对应.在简单的神经网络中,反向传播算法,可以理解为最优化损失函数过程,求解每个参 ...

  4. 颗粒粒径分析方法汇总

    一.相关概念: 粒度与粒径:颗粒的大小称为粒度,一般颗粒的大小又以直径表示,故也称为粒径.粒度分布:用一定方法反映出一系列不同粒径区间颗粒分别占试样总量的百分比称为粒度分布.等效粒径:由于实际颗粒的形 ...

  5. 【机器学习】基于人工鱼群算法的多元非线性函数寻优

    基于人工鱼群算法的多元非线性函数寻优

  6. 【机器学习】基于人工鱼群算法的非线性函数寻优

    本微信图文介绍了人工鱼群算法的基本原理并对一元非线性函数进行极值寻优.

  7. matlab球落点的数学建模,MATLAB数学建模:智能优化算法-人工鱼群算法

    MATLAB 数学建模: 人工鱼群算法 1. 基本原理 人工鱼群算法是一种受鱼群聚集规律而启发的优化算法. 在人工鱼群算法中, 我们假定鱼群的活动行为分为: 觅食行为, 群聚行为, 追随行为和随机行为 ...

  8. MATLAB实战系列(十二)-如何用人工鱼群算法解决带时间窗车辆路径(CVRP)问题(附MATLAB代码)

    前言: 本文大体的思路是先对人工鱼进行编码,然后采用人工鱼群算法求解TSP问题中的觅食.聚群.追尾和随机行为对人工鱼群进行更新. 但是亟需需要解决的问题是:对于CVRP问题,如何对人工鱼进行编码.如果 ...

  9. matlab实战系列之人工鱼群算法求解TSP问题原理解析(下篇源码解析)

    从算法的名字中可以看出该算法是群体智能优化算法中的一种,人工鱼群算法通过模拟鱼群的觅食.聚群.追尾.随机等行为在搜索域中进行寻优. 人工鱼群算法有三个比较重要的概念:视野范围.k-距离邻域.多条鱼的中 ...

最新文章

  1. 多线程解决rospy.spin()语句之后,程序不再往下执行问题
  2. PowerShell2.0之维护网络(一)查看网络设置
  3. android显示3d模型_使用Unity AR Foundation在增强现实中查看模型
  4. std::string 收缩到合适大小_如何选择合适的自动伸缩门尺寸-深圳自动伸缩门供应商...
  5. csdn 用户 蚂蚁翘大象_用户界面设计师房间里的大象
  6. English Voice of Way Back Into Love
  7. java连接sqlserver非默认实例连接字符串设置
  8. C语言程序流程图switch,C语言流程控制之switch语句详解
  9. 利用LR做性能测试中出现的常见问题解决方案
  10. 透过表象看本质!?之三——Kalman滤波
  11. matlab锂电池充电电路,锂离子电池充放电电路模型及其仿真.doc
  12. MYSQL基本操作(增删改查)
  13. LOJ3124 CTS2019 氪金手游 概率、容斥、树形DP
  14. CSS3实现精美的纸张折角效果 -- 进阶版
  15. 我精心整理的一些大牌男装正品店
  16. 高校党员信息管理系统
  17. STC89C52RC - 2 - 开发环境搭建
  18. js实现web网页版台球游戏
  19. 解锁三星bl锁有几种方法_三星手机解锁详细图文教程 三星CROM Service下载解锁Bootloader...
  20. python写淘宝秒杀脚本_python实现淘宝秒杀脚本

热门文章

  1. Xshell下利用tftp和iperf3进行网络吞吐量测试
  2. 2022-3-22 Leetcode 72.编辑距离
  3. 微拉美多久可以恢复,微拉美提升能保持几年
  4. 新手坐高铁怎么找车厢_第一次坐高铁怎么找座位啊
  5. 【应用】西门子1200之自动装料和四节传送带
  6. Nexus 6p从8.0降级6.0+root
  7. php 二进制安全,php的二进制安全 - 范思哲思考者的个人空间 - OSCHINA - 中文开源技术交流社区...
  8. matlab2012添加工具箱,关于matlab添加工具箱的命令
  9. 螺杆真空泵安装流程图_胶囊充填机安装十二工位模具
  10. linux 内核 权限,linux – 内核模块执行中的不同权限