diff --git a/src/algorithms/compass_search.cpp b/src/algorithms/compass_search.cpp index 01d2b3a6e..bbf729837 100644 --- a/src/algorithms/compass_search.cpp +++ b/src/algorithms/compass_search.cpp @@ -115,13 +115,12 @@ population compass_search::evolve(population pop) const vector_double cur_best_x(std::move(sel_xf.first)), cur_best_f(std::move(sel_xf.second)); // We need some auxiliary variables - bool flag = false; unsigned fevals = 0u; double newrange = m_start_range; while (newrange > m_stop_range && fevals <= m_max_fevals) { - flag = false; + bool flag = false; for (decltype(dim) i = 0u; i < dim; i++) { auto x_trial = cur_best_x; // move up diff --git a/src/algorithms/ihs.cpp b/src/algorithms/ihs.cpp index 5036bf245..b1b424778 100644 --- a/src/algorithms/ihs.cpp +++ b/src/algorithms/ihs.cpp @@ -120,7 +120,6 @@ population ihs::evolve(population pop) const // Declarations vector_double new_x(dim, 0.); - std::vector best_idxs(pop.size()); // Main loop for (decltype(m_gen) gen = 1u; gen <= m_gen; ++gen) { @@ -185,7 +184,7 @@ population ihs::evolve(population pop) const // we augment the list with the new fitness fitnesses.push_back(new_f); // select the best pop.size() individuals - best_idxs = select_best_N_mo(fitnesses, pop.size()); + const std::vector best_idxs = select_best_N_mo(fitnesses, pop.size()); // define the new population for (population::size_type i = 0u; i < pop.size(); ++i) { if (best_idxs[i] == pop.size()) { // this is the new guy diff --git a/src/algorithms/nsga2.cpp b/src/algorithms/nsga2.cpp index 57d5d913e..38e024c2c 100644 --- a/src/algorithms/nsga2.cpp +++ b/src/algorithms/nsga2.cpp @@ -132,7 +132,7 @@ population nsga2::evolve(population pop) const m_log.clear(); // Declarations - std::vector best_idx(NP), shuffle1(NP), shuffle2(NP); + std::vector shuffle1(NP), shuffle2(NP); vector_double::size_type parent1_idx, parent2_idx; std::pair children; @@ -296,7 +296,7 @@ population nsga2::evolve(population pop) const } // This method returns the sorted N best individuals in the population according to the crowded comparison // operator - best_idx = select_best_N_mo(popnew.get_f(), NP); + const std::vector best_idx = select_best_N_mo(popnew.get_f(), NP); // We insert into the population for (population::size_type i = 0; i < NP; ++i) { pop.set_xf(i, popnew.get_x()[best_idx[i]], popnew.get_f()[best_idx[i]]); diff --git a/src/algorithms/pso.cpp b/src/algorithms/pso.cpp index 53d146ab5..eeb766c51 100644 --- a/src/algorithms/pso.cpp +++ b/src/algorithms/pso.cpp @@ -218,9 +218,6 @@ population pso::evolve(population pop) const double acceleration_coefficient = m_eta1 + m_eta2; double sum_forces; - double r1 = 0.; - double r2 = 0.; - /* --- Main PSO loop --- */ // For each generation @@ -240,8 +237,8 @@ population pso::evolve(population pop) const /*-------Original algorithm used in the first PaGMO paper (~2007) ------------------------*/ if (m_variant == 1u) { for (decltype(dim) d = 0u; d < dim; ++d) { - r1 = drng(m_e); - r2 = drng(m_e); + const double r1 = drng(m_e); + const double r2 = drng(m_e); m_V[p][d] = m_omega * m_V[p][d] + m_eta1 * r1 * (lbX[p][d] - X[p][d]) + m_eta2 * r2 * (best_neighb[d] - X[p][d]); } @@ -252,7 +249,7 @@ population pso::evolve(population pop) const /*-------Check with Rastrigin-------------------------------------------------------------*/ else if (m_variant == 2u) { for (decltype(dim) d = 0u; d < dim; ++d) { - r1 = drng(m_e); + const double r1 = drng(m_e); m_V[p][d] = m_omega * m_V[p][d] + m_eta1 * r1 * (lbX[p][d] - X[p][d]) + m_eta2 * r1 * (best_neighb[d] - X[p][d]); } @@ -261,8 +258,8 @@ population pso::evolve(population pop) const /*-------PSO variant (commonly mistaken in literature for the canonical)----------------*/ /*-------Same random number for all components------------------------------------------*/ else if (m_variant == 3u) { - r1 = drng(m_e); - r2 = drng(m_e); + const double r1 = drng(m_e); + const double r2 = drng(m_e); for (decltype(dim) d = 0u; d < dim; ++d) { m_V[p][d] = m_omega * m_V[p][d] + m_eta1 * r1 * (lbX[p][d] - X[p][d]) + m_eta2 * r2 * (best_neighb[d] - X[p][d]); @@ -273,7 +270,7 @@ population pso::evolve(population pop) const /*-------Same random number for all components------------------------------------------*/ /*-------and with equal random weights of social and cognitive components---------------*/ else if (m_variant == 4u) { - r1 = drng(m_e); + const double r1 = drng(m_e); for (decltype(dim) d = 0u; d < dim; ++d) { m_V[p][d] = m_omega * m_V[p][d] + m_eta1 * r1 * (lbX[p][d] - X[p][d]) + m_eta2 * r1 * (best_neighb[d] - X[p][d]); @@ -295,8 +292,8 @@ population pso::evolve(population pop) const *-------------------------------------------------------------------------------------*/ else if (m_variant == 5u) { for (decltype(dim) d = 0u; d < dim; ++d) { - r1 = drng(m_e); - r2 = drng(m_e); + const double r1 = drng(m_e); + const double r2 = drng(m_e); m_V[p][d] = m_omega * (m_V[p][d] + m_eta1 * r1 * (lbX[p][d] - X[p][d]) + m_eta2 * r2 * (best_neighb[d] - X[p][d])); diff --git a/src/algorithms/pso_gen.cpp b/src/algorithms/pso_gen.cpp index f7ef359ad..3d5790a2c 100644 --- a/src/algorithms/pso_gen.cpp +++ b/src/algorithms/pso_gen.cpp @@ -220,9 +220,6 @@ population pso_gen::evolve(population pop) const double acceleration_coefficient = m_eta1 + m_eta2; double sum_forces; - double r1 = 0.; - double r2 = 0.; - /* --- Main PSO loop --- */ // For each generation @@ -242,8 +239,8 @@ population pso_gen::evolve(population pop) const /*-------Original algorithm used in the first PaGMO paper (~2007) ------------------------*/ if (m_variant == 1u) { for (decltype(dim) d = 0u; d < dim; ++d) { - r1 = drng(m_e); - r2 = drng(m_e); + const double r1 = drng(m_e); + const double r2 = drng(m_e); m_V[p][d] = m_omega * m_V[p][d] + m_eta1 * r1 * (lbX[p][d] - X[p][d]) + m_eta2 * r2 * (best_neighb[d] - X[p][d]); } @@ -254,7 +251,7 @@ population pso_gen::evolve(population pop) const /*-------Check with Rastrigin-------------------------------------------------------------*/ else if (m_variant == 2u) { for (decltype(dim) d = 0u; d < dim; ++d) { - r1 = drng(m_e); + const double r1 = drng(m_e); m_V[p][d] = m_omega * m_V[p][d] + m_eta1 * r1 * (lbX[p][d] - X[p][d]) + m_eta2 * r1 * (best_neighb[d] - X[p][d]); } @@ -263,8 +260,8 @@ population pso_gen::evolve(population pop) const /*-------PSO variant (commonly mistaken in literature for the canonical)----------------*/ /*-------Same random number for all components------------------------------------------*/ else if (m_variant == 3u) { - r1 = drng(m_e); - r2 = drng(m_e); + const double r1 = drng(m_e); + const double r2 = drng(m_e); for (decltype(dim) d = 0u; d < dim; ++d) { m_V[p][d] = m_omega * m_V[p][d] + m_eta1 * r1 * (lbX[p][d] - X[p][d]) + m_eta2 * r2 * (best_neighb[d] - X[p][d]); @@ -275,7 +272,7 @@ population pso_gen::evolve(population pop) const /*-------Same random number for all components------------------------------------------*/ /*-------and with equal random weights of social and cognitive components---------------*/ else if (m_variant == 4u) { - r1 = drng(m_e); + const double r1 = drng(m_e); for (decltype(dim) d = 0u; d < dim; ++d) { m_V[p][d] = m_omega * m_V[p][d] + m_eta1 * r1 * (lbX[p][d] - X[p][d]) + m_eta2 * r1 * (best_neighb[d] - X[p][d]); @@ -297,8 +294,8 @@ population pso_gen::evolve(population pop) const *-------------------------------------------------------------------------------------*/ else if (m_variant == 5u) { for (decltype(dim) d = 0u; d < dim; ++d) { - r1 = drng(m_e); - r2 = drng(m_e); + const double r1 = drng(m_e); + const double r2 = drng(m_e); m_V[p][d] = m_omega * (m_V[p][d] + m_eta1 * r1 * (lbX[p][d] - X[p][d]) + m_eta2 * r2 * (best_neighb[d] - X[p][d])); diff --git a/src/algorithms/simulated_annealing.cpp b/src/algorithms/simulated_annealing.cpp index 071fe09b1..c41cdcbbf 100644 --- a/src/algorithms/simulated_annealing.cpp +++ b/src/algorithms/simulated_annealing.cpp @@ -145,7 +145,7 @@ population simulated_annealing::evolve(population pop) const // Stores the number of accepted points for each component std::vector acp(dim, 0u); - double ratio = 0., currentT = m_Ts, probab = 0.; + double currentT = m_Ts; // Main SA loops for (decltype(m_n_T_adj) jter = 0u; jter < m_n_T_adj; ++jter) { @@ -174,7 +174,7 @@ population simulated_annealing::evolve(population pop) const } } else { // test it with Boltzmann to decide the acceptance - probab = std::exp(-std::abs(fOLD[0] - fNEW[0]) / currentT); + const double probab = std::exp(-std::abs(fOLD[0] - fNEW[0]) / currentT); // we compare prob with a random probability. if (probab > drng(m_e)) { xOLD[nter] = xNEW[nter]; @@ -209,7 +209,7 @@ population simulated_annealing::evolve(population pop) const } // end for(kter = 0; ... // adjust the step (adaptively) for (decltype(dim) iter = 0u; iter < dim; ++iter) { - ratio = static_cast(acp[iter]) / static_cast(m_bin_size); + const double ratio = static_cast(acp[iter]) / static_cast(m_bin_size); acp[iter] = 0u; // reset the counter if (ratio > .6) { // too many acceptances, increase the step by a factor 3 maximum diff --git a/src/problems/cec2006.cpp b/src/problems/cec2006.cpp index ba5761362..945778a6b 100644 --- a/src/problems/cec2006.cpp +++ b/src/problems/cec2006.cpp @@ -918,9 +918,6 @@ void cec2006::g19_objfun_impl(vector_double &f, const vector_double &x) const /// Implementation of the constraint function. void cec2006::g19_compute_constraints_impl(vector_double &c, const vector_double &x) const { - double sum1 = 0.; - double sum2 = 0.; - double A[10][5] = {{-16.0, 2.0, 0.0, 1.0, 0.0}, {0.0, -2.0, 0.0, 0.4, 2.0}, {-3.5, 0.0, 2.0, 0.0, 0.0}, {0.0, -2.0, 0.0, -4.0, -1.0}, {0.0, -9.0, -2.0, 1.0, -2.8}, {2.0, 0.0, -4.0, 0.0, 0.0}, {-1.0, -1.0, -1.0, -1.0, -1.0}, {-1.0, -2.0, -3.0, -2.0, -1.0}, {1.0, 2.0, 3.0, 4.0, 5.0}, @@ -937,10 +934,10 @@ void cec2006::g19_compute_constraints_impl(vector_double &c, const vector_double /* constraints g <= 0 */ for (unsigned j = 0u; j < 5u; ++j) { - sum1 = 0.0; + double sum1 = 0.0; for (unsigned i = 0u; i < 5u; ++i) sum1 += C[i][j] * x[10 + i]; - sum2 = 0.0; + double sum2 = 0.0; for (unsigned i = 0u; i < 10u; ++i) sum2 += A[i][j] * x[i]; c[j] = -((2.0 * sum1) + (3.0 * D[j] * std::pow(x[10 + j], 2.0)) + E[j] - sum2); diff --git a/src/problems/cec2013.cpp b/src/problems/cec2013.cpp index 14f173a4b..c2c210fdf 100644 --- a/src/problems/cec2013.cpp +++ b/src/problems/cec2013.cpp @@ -502,7 +502,7 @@ void cec2013::weierstrass_func(const double *x, double *f, const unsigned nx, co int r_flag) const /* Weierstrass's */ { unsigned i, j, k_max; - double sum = 0, sum2 = 0, a, b; + double sum2 = 0, a, b; shiftfunc(x, &m_y[0], nx, Os); for (i = 0u; i < nx; ++i) // shrink to the original search range @@ -529,7 +529,7 @@ void cec2013::weierstrass_func(const double *x, double *f, const unsigned nx, co k_max = 20; f[0] = 0.0; for (i = 0u; i < nx; ++i) { - sum = 0.0; + double sum = 0.0; sum2 = 0.0; for (j = 0u; j <= k_max; ++j) { sum += std::pow(a, j) * std::cos(2.0 * detail::pi() * std::pow(b, j) * (m_y[i] + 0.5));