So while writing the qpso code, i came across this error. Kindly someone tell me how to fix this.
In file included from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\char_traits.h:39:0, from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\ios:40, from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\ostream:38, from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\iostream:39, from QPSOAlgorithm.cpp:1:c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_algobase.h: In instantiation of 'static _Tp* std::__copy_move<_IsMove, true, std::random_access_iterator_tag>::__copy_m(const _Tp*, const _Tp*, _Tp*) [with _Tp = double [100]; bool _IsMove = false]':c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_algobase.h:386:44: required from '_OI std::__copy_move_a(_II, _II, _OI) [with bool _IsMove = false; _II = double (*)[100]; _OI = double (*)[100]]'c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_algobase.h:422:45: required from '_OI std::__copy_move_a2(_II, _II, _OI) [with bool _IsMove = false; _II = double (*)[100]; _OI = double (*)[100]]'c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_algobase.h:455:8: required from '_OI std::copy(_II, _II, _OI) [with _II = double (*)[100]; _OI = double (*)[100]]'QPSOAlgorithm.cpp:184:84: required from herec:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_algobase.h:364:4: error: static assertion failed: type is not assignable static_assert( __assignable::type::value, "type is not assignable" );
Line 184 as shown in the error is the std::copy(std::begin(R), std::end(R), std::begin(R1));
line in the code.
QPSOAlgorithm.cpp
#include<iostream>#include<bits/stdc++.h>#include <cstdlib>#include <ctime>#include <cmath>#include <random>#include <list>#include "LennardTask.cpp"#include "LennardConstants.cpp"#include "Task.cpp"#include "Solution.cpp"using namespace std;class QPSOAlgorithm { private: Solution best; private: int Np, Nd, Nt, Precision,tau, noOfAtoms = 3; private: double w, c1, c2, r1, r2, xMin, xMax, vMin, vMax, wMin, wMax, phi, chi, nInfinite, StepMax, StepMin, epsilon, Ftotal, Step_change, Step, K, gBestValue, gBestValue2, sqrtValue; private: double pBestValue[30], gBestPosition[100], bestFitnessHistory[50], M[30];// , def[]; private: double pBestPosition[30][100], R[30][100], R1[30][100], V[30][100], r[30][100], F[30][100], H[30][100]; //private: array R[30][100], R1[30][100]; public: QPSOAlgorithm() { Np = 30; //Swarm sizes Nd = 100; //Nd = task.getNumberOfDimensions() * 3; // 3 * nekaj Nt = 50; Precision = 100; w = 0.5;//0.9; //Omega used to calculate velocity of the particles. Inertia weight coefficient c1 = 1.5;//2.05; // random number used in the formula c2 = 2;//2.05; // random number used in the formula r1 = 0.0; // random number used in the formula r2 = 0.0; // random number used in the formula xMin = -5.12; //lower bound for the Particles xMax = 5.12; //Upper bound for the Particles vMin = 0; //Lower bound for the velocity vMax = 1; //Upper bound for the velocity wMin = 0.4; //Lower bound for inertia weight wMax = 0.9;// Upper bound for inertia weight phi = c1 + c2; sqrtValue = pow(phi, 2) - 4 * phi; chi = 2.0 / abs(2.0 - phi - (pow(sqrtValue,0.5))); nInfinite = - std::numeric_limits<double>::infinity(); //Double.NEGATIVE_INFINITY; tau = 100;//5; // tau used in the formula StepMin = std::numeric_limits<double>::infinity(); //Double.POSITIVE_INFINITY; StepMax = - std::numeric_limits<double>::infinity(); //Double.NEGATIVE_INFINITY; epsilon = -3.28; // Epsilon used to calculate Ezero, Eone as mentioned in algorithm 2 Ftotal = 0; Step_change = 0.5; Step = 0.5; K = 0.1; // Constant Threshold gBestValue = nInfinite; // Global best value gBestValue2 = 0; // pBestValue = new double[Np]; // Individual best value of the particles. // gBestPosition = new double[Nd]; // Global best coordinates of the particles. //bestFitnessHistory = new double[Nt]; //Best value //M = new double[Np]; //def = new double[Nd]; // pBestPosition = new double[Np][Nd]; // Individual best coordinates of the particles // R = new double[Np][Nd]; // Coordinates of the particles which will be randomly generated. // R1 = new double[Np][Nd]; //Temp Coordinates // V = new double[Np][Nd]; // Velocity of the particles which will be randomly generated. // r = new double[Np][Nd]; // F = new double[Np][Nd]; // H = new double[Np][Nd]; } public: Solution execute(Task task, double lennardConstants[]) { LennardTask lennardTask(task.getNoOfAtoms()); //LennardConstants lennardConstants; srand((unsigned)time(0)); // inicializacija // initPopulation(); //This loop initialized the variables. All the individual best values will be assigned with 'infinity' value for (int p = 0; p < Np; p++) { pBestValue[p] = nInfinite; } //This loop randomly generates the coordinates and the velocity for the particles. for (int p = 0; p < Np; p++) { for (int i = 0; i < Nd; i++) { double randomNumR = (double)rand(); double randomNumV = (double)rand(); R[p][i] = (randomNumR < 0.5 ? -(xMin + (xMax - xMin) * randomNumR) : (xMin + (xMax - xMin) * randomNumR)); V[p][i] = (randomNumV < 0.5 ? -(vMin + (vMax - vMin) * randomNumV) : (vMin + (vMax - vMin) * randomNumV)); } } bestFitnessHistory[0] = gBestValue; //Best value will be global best at the beginning. int j = 0; while (!task.isStopCriterion()) { //This is the stopping criteria. The below code executes as long as this condition is satisfied. //// j++; //In the below section, we randomly generate the coordinates and run the PSO iteratively. w = wMax - ((wMax - wMin) / Nt) * j; for (int p = 0; p < Np; p++) { for (int i = 0; i < Nd; i++) { r1 = (double)rand(); r2 = (double)rand(); V[p][i] = w * V[p][i] + (r1 * c1 * (pBestPosition[p][i] - R[p][i]))+ (r2 * c2 * (gBestPosition[i] - R[p][i])); // classic Velocity update formulate if (V[p][i] > vMax) { V[p][i] = vMax; } else if (V[p][i] < vMin) { V[p][i] = vMin; } //We update the coordinates with the velocity. R[p][i] = R[p][i] + V[p][i]; if (R[p][i] > xMax) { R[p][i] = xMax; } else if (R[p][i] < xMin) { R[p][i] = xMin; } } } //Here, we run the PSO iteratively to get individual best and global best. for (int p = 0; p < Np; p++) { if (task.isStopCriterion()) // we break the execution if the criteria is met. break; //M[p] = task.eval(R[p]).getEval(); //M[p] = lennardTask.eval(lennardConstants.x13); //the static coordinates. M[p] = lennardTask.eval(lennardConstants); //the static coordinates. task.incrementNumberOfEvaluations(); //We compare the energy obtained with the individual best and the global best. // If energy is greater than the individual and global best, we assign that energy to individual and global values. if (M[p] > pBestValue[p]) { pBestValue[p] = M[p]; for (int i = 0; i < Nd; i++) { pBestPosition[p][i] = R[p][i]; // we assign individual best position } } if (M[p] > gBestValue) { gBestValue = M[p]; for (int i = 0; i < Nd; i++) { gBestPosition[i] = R[p][i];// we assign global best position } } } bestFitnessHistory[j] = gBestValue; // we assign,global best to be the best value. //////////////////////////////////////////////////// if (j % tau == 0) { //iteration condition. if (bestFitnessHistory[j] - bestFitnessHistory[j - 1] < (bestFitnessHistory[j] - bestFitnessHistory[0]) / j) { // Trigger condition 1. Equation 8 to check if the quasi-physical policy is satisfied bool deltaR = true; double sum = 0; for (int p = 0; p < Np; p++) { sum = sum + (pBestValue[p] - gBestValue); } if (sum / Np >= K) // Trigger condition 2. Equation 9. Used to determine the degree of swarm aggregation. //This helps in ensuring that particle search results are similar. If it is not met then again the particles are generated randomly. deltaR = false; if (deltaR == true) {//Trigger condition 2 is met //From here algorithm 2 starts. //Attraction and Repulsion of the particles // algorithm 2 std::copy(std::begin(R), std::end(R), std::begin(R1)); //std::copy(R[0][0],R[0][0]+Np*Nd,R1[0][0]); //R1 = R; //Copy the coordinates into a temp variable. //We get stepMax and stepMin using this loop. for (int p = 0; p < Np; p++) { for (int p1 = 0; p1 < Np; p1++) { sum = 0; for (int i = 0; i < Nd; i++) { sum = sum + ((R1[p][i] - R1[p1][i]) * (R1[p][i] - R1[p1][i])); } sum = pow(sum,0.5); r[p][p1] = sum; if (sum > StepMax) StepMax = sum; if (sum < StepMin) StepMin = sum; } } double nowStep = StepMax; double Ezero = 0; //We calculate Ezero sing Equation 3 for (int p = 0; p < Np - 1; p++) { for (int p1 = p + 1; p1 < Np; p1++) { Ezero += pow(epsilon / r[p][p1], 12) - pow(epsilon / r[p][p1], 6); } } Ezero *= 4 * epsilon; //We calculate Fi using Equation 12 and 13. for (int p = 0; p < Np; p++) { for (int i = 0; i < Np; i++) { if (i == p) continue; for (int w1 = 0; w1 < Nd; w1++) { F[p][w1] += ((48 * pow(r[p][i], -14)) - (24 * pow(r[p][i], -8))) * (R1[p][w1] - R1[i][w1]); } } } //Now we calculate FTotal using equation 14. Ftotal = 0; for (int p = 0; p < Np; p++) { sum = 0; for (int w1 = 0; w1 < Nd; w1++) { sum += pow(F[p][w1], 2); } Ftotal += sum; } Ftotal /= Np; Ftotal = pow(Ftotal,0.5); //Condition check in algorithm 2 for (int j1 = 0; j1 < j; j1++) { if (Ftotal < epsilon) {//If FTotal is lesser than epsilon break the loop. break; } for (int p = 0; p < Np; p++) { for (int i = 0; i < Nd; i++) { R1[p][i] = R1[p][i] + nowStep * F[p][i]; } } //Calculate EOne; double Eone = 0; for (int p = 0; p < Np - 1; p++) { for (int p1 = p + 1; p1 < Np; p1++) { Eone += pow(epsilon / r[p][p1], 12) - pow(epsilon / r[p][p1], 6); } } Eone *= 4 * epsilon; if (Eone < Ezero) Ezero = Eone; else if (Eone >= Ezero && nowStep > StepMin) nowStep = nowStep * Step_change; ///// //Finding the direction for (int p = 0; p < Np; p++) { for (int i = 0; i < Np; i++) { if (i == p) continue; for (int w1 = 0; w1 < Nd; w1++) { H[p][w1] += ((48 * pow(r[p][i], -14)) - (24 * pow(r[p][i], -8))) * (R1[p][w1] - R1[i][w1]); } } } Ftotal = 0; for (int p = 0; p < Np; p++) { sum = 0; for (int w1 = 0; w1 < Nd; w1++) { sum += pow(H[p][w1], 2); } Ftotal += sum; } Ftotal /= Np; Ftotal = pow(Ftotal,0.5); // end algorithm 2 } for (int p = 0; p < Np; p++) { for (int i = 0; i < Nd; i++) { R[p][i] = R1[p][i] + Step * H[p][i]; } } } else { //If Trigger 2 is not met, then we randomly generate the coordinates again and repeat the process. for (int p = 0; p < Np; p++) { for (int i = 0; i < Nd; i++) { double randomNumR = (double)rand(); double randomNumV = (double)rand(); R[p][i] = (randomNumR < 0.5 ? -(xMin + (xMax - xMin) * randomNumR) : (xMin + (xMax - xMin) * randomNumR)); V[p][i] = (randomNumV < 0.5 ? -(vMin + (vMax - vMin) * randomNumV) : (vMin + (vMax - vMin) * randomNumV)); } } } } } task.incrementNumberOfIterations(); // } } //Return Gbest //double doubles[] = ArrayUtils.toObject(pBestValue); list<double> list; list.assign(pBestValue, pBestValue + 30);// = Arrays.asList(doubles); best.setPBestValuesList(list); best.setGBestValue(gBestValue); return best; };};