PSO, according to the principle of the algorithm, each run should be optimized result is the same, but with the increasing, the number of running deviation,
Trigger function
void MainWindow: : calButtonClicked ()
{
prodData.listVectorData=https://bbs.csdn.net/topics/ui-> prodData -> getListVectorData ();
injData.listVectorData=https://bbs.csdn.net/topics/ui-> injData -> getListVectorData ();
QVectorConvertEigen q1;
ArrayXd tnP0=q1. QVectorToVectorXd (prodData. ListVectorData. Ats (0)), array ();
ArrayXd prodDataArray=q1. QVectorToVectorXd (prodData. ListVectorData. At (1)), array ();
ArrayXXd injDataArray=q1. ListVectorToMatrix (injData. ListVectorData) array ();
CmpModel CMP.
CMP. SetData (tnP0 injDataArray, prodDataArray);
CMP. SetFitParams (600100);
CMP. The Run (100);
CMP. PrintResult ();
CmpModel cmp1;
Cmp1. SetData (tnP0 injDataArray, prodDataArray);
Cmp1. SetFitParams (50, 600);
Cmp1. Run (50);
Cmp1. PrintResult ();
}
Inherit the fitness function of the PSO class modification
double CmpModel: : GetFit (PARTICLE & amp; Particle)
{
Int injNums=injDataMatrix. Cols ();
MatrixXd prodInjMatrix=getProdInjMatrix (timeList, particle. X + 1] [injNums, injDataMatrix, prodDataVector);
Int cols.=prodInjMatrix cols ();
Int rows=prodInjMatrix. Rows ();
ArrayXd prodData=https://bbs.csdn.net/topics/prodDataVector.tail (rows);
MatrixXd injMatrix=prodInjMatrix. LeftCols (cols - 1);
VectorXd resVector (injNums + 1);
for (int i=0; i{
ResVector (I)=particle. X [I];
}
ArrayXd prodObs=(injMatrix * resVector) array ();//calculate the output of
Double qoj=prodData. Scheme () - prodObs. Scheme ();//injection-production balance coefficient
ProdObs +=qoj;
Double err=- (prodData - prodObs). Pow (2) the sum ()/rows;//relative error
//double err=particle. X [0] * sin (particle) X [0] * 3.1415926 * 10) + 2;
Return err;
}
Add the setting parameter function
//setting calculation fitness parametersVoid CmpModel: : setFitParams (int pNum, int maxIter)
{
Int injNums=injDataMatrix. Cols ();
SetDimN (injNums + 2, pNum);
Double xDown [injNums + 2], xUp [injNums + 2];
for (int i=0; i{
XDown [I]=0;
XUp [I]=1;
}
XDown [injNums]=0;
XUp [injNums]=2;
XDown [injNums + 1)=0.01;
XUp [injNums + 1)=100;
SetXdown (xDown);
SetXup (xUp);
SetVmax (0.125);
SetIteorMax (maxIter);
}
Void CmpModel: : printResult ()
{
Int injNums=injDataMatrix. Cols ();
Double the result [injNums + 2];
Double gBestFit=GetBest (result);
QDebug () & lt; <" GBestFit: "& lt;for (int i=0; i {
QDebug () & lt;}
}
Results:
The first trigger
Initialize all the particles are individual
GBestFit: 0.0106852
0.124679
0.129975
0.118851
0.0695183
0.0621348
1.11824
7.1922
Initialize all the particles are individual
GBestFit: 0.0163799
0.125802
0.123954
0.110999
0.0605069
0.0627186
0.939009
10.7074
Second trigger
Initialize all the particles are individual
GBestFit: 0.00572
0.123246
0.127932
0.117957
0.0668491
0.0630871
1.10139
9.26076
Initialize all the particles are individual
GBestFit: 1.56042
0.0482683
0.211839
0.187464
0.047509
0.177201
1.57394
57.4806
The third trigger
Initialize all the particles are individual
GBestFit: 0.133401
0.102157
0.159354
0.14124
0.0888743
0.0896266
1.65061
12.747
Initialize all the particles are individual
GBestFit: 3.4611
0.155183
0.243381
0.172704
0.135338
0.111841
1.81003
72.7145
Fourth trigger
Initialize all the particles are individual
GBestFit: 1.27257
0.073802
0.169055
0.160619
0.018564
0.161117
1.26562
72.6809
Initialize all the particles are individual
GBestFit: 6.41726
0.20154
0.226871
0.0451014
0.0630959
0.153779
1.34195
59.6622
The fifth triggering
Initialize all the particles are individual
GBestFit: 1.66646
0.063878
0.214967
0.173773
0.00151516
0.164087
1.34985
76.219
Initialize all the particles are individual
GBestFit: 6.02889
0.0983612
0.0824
0.0578501
0.206366
0.14068
1.25472
89.2219
But to optimize simple question
double CmpModel: : GetFit (PARTICLE & amp; Particle)
{
Double err=particle. X [0] * sin (particle) X [0] * 3.1415926 * 10) + 2;
Return err;
}
Is normal, do not know what is wrong, ask the great spirit show
CodePudding user response:
PSO algorithm is a random search algorithm, every time is not the same as the operation result is normal ah,Less simple problems because of dimension, easy solution distribution (as there is only one peak) on the surface, all is easy to converge to the optimal solution, if is a complicated problem, dimension, the height distribution is complex, PSO can often be convergence to the local optimal solution,
CodePudding user response:
Used for parameter identification of particle swarm optimization (pso), caused by the random number is different for every run every time the results are different, how to set up random Numbers in the MATLABCodePudding user response:
@ qq_40429293Set the same random number seed,