Made GetRealRoots call Solve_X with y = 0
This commit is contained in:
@ -528,80 +528,7 @@ namespace JRAMPERSAD
|
|||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create initial random solutions
|
return solve_x(0, options);
|
||||||
std::random_device device;
|
|
||||||
std::uniform_real_distribution<double> unif(options.min_range, options.max_range);
|
|
||||||
std::vector<GA_Solution> solutions;
|
|
||||||
|
|
||||||
solutions.resize(options.data_size);
|
|
||||||
for (unsigned int i = 0; i < options.sample_size; i++)
|
|
||||||
solutions[i] = (GA_Solution{lrgst_expo, 0, unif(device)});
|
|
||||||
|
|
||||||
float timer{ 0 };
|
|
||||||
|
|
||||||
for (unsigned int count = 0; count < options.num_of_generations; count++)
|
|
||||||
{
|
|
||||||
std::generate(std::execution::par, solutions.begin() + options.sample_size, solutions.end(), [this, &unif, &device]() {
|
|
||||||
return GA_Solution{lrgst_expo, 0, unif(device)};
|
|
||||||
});
|
|
||||||
|
|
||||||
// Run our fitness function
|
|
||||||
for (auto& s : solutions) { s.fitness(constants); }
|
|
||||||
|
|
||||||
// Sort our solutions by rank
|
|
||||||
std::sort(std::execution::par, solutions.begin(), solutions.end(),
|
|
||||||
[](const auto& lhs, const auto& rhs) {
|
|
||||||
return lhs.rank > rhs.rank;
|
|
||||||
});
|
|
||||||
|
|
||||||
// Take top solutions
|
|
||||||
std::vector<GA_Solution> sample;
|
|
||||||
std::copy(
|
|
||||||
solutions.begin(),
|
|
||||||
solutions.begin() + options.sample_size,
|
|
||||||
std::back_inserter(sample)
|
|
||||||
);
|
|
||||||
solutions.clear();
|
|
||||||
|
|
||||||
if (count + 1 == options.num_of_generations)
|
|
||||||
{
|
|
||||||
std::copy(
|
|
||||||
sample.begin(),
|
|
||||||
sample.end(),
|
|
||||||
std::back_inserter(solutions)
|
|
||||||
);
|
|
||||||
sample.clear();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Mutate the top solutions by %
|
|
||||||
std::uniform_real_distribution<double> m((1 - options.mutation_percentage), (1 + options.mutation_percentage));
|
|
||||||
std::for_each(sample.begin(), sample.end(), [&m, &device](auto& s) {
|
|
||||||
s.x *= m(device);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Cross over not needed as it's only one value
|
|
||||||
|
|
||||||
std::copy(
|
|
||||||
sample.begin(),
|
|
||||||
sample.end(),
|
|
||||||
std::back_inserter(solutions)
|
|
||||||
);
|
|
||||||
sample.clear();
|
|
||||||
solutions.resize(options.data_size);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::sort(solutions.begin(), solutions.end(),
|
|
||||||
[](const auto& lhs, const auto& rhs) {
|
|
||||||
return lhs.x < rhs.x;
|
|
||||||
});
|
|
||||||
|
|
||||||
std::vector<double> ans;
|
|
||||||
for (auto& s : solutions)
|
|
||||||
{
|
|
||||||
ans.push_back(s.x);
|
|
||||||
}
|
|
||||||
return ans;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
double Function::solve_y(const double& x_val) const
|
double Function::solve_y(const double& x_val) const
|
||||||
|
Reference in New Issue
Block a user