-
Notifications
You must be signed in to change notification settings - Fork 119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bounds for search space #364
Comments
Hello, depending on the optimizer it's possible to set the bounds, see https://ensmallen.org/docs.html#pso for an example. What optimizer are you using? |
@zoq is right, there are some techniques that specifically support constrained optimization. If you still want to use a differentiable optimizer like SGD or L-BFGS or something that doesn't specifically have supports for constraints, I would suggest a "soft" constraint instead of simply evaluating to |
@zoq Sorry for late reply! I was using DE. PSO for me has a strange bug of not terminating even due to @rcurtin thanks for the soft constraint idea, not sure if it is useful for non-differentiable optimizers, my testing for DE seems to yield the same results as with auto outOfBoundsLoss(const arma::mat& coordinates,
const arma::mat& lower,
const arma::mat& upper) -> std::optional<double> {
constexpr double PENALTY = 1000.0;
std::optional<double> loss{};
for (int i = 0; i < coordinates.size(); ++i) {
if (coordinates[i] < lower[i]) {
loss = PENALTY * (lower[i] - coordinates[i]) + loss.value_or(0.0);
} else if (coordinates[i] > upper[i]) {
loss = PENALTY * (coordinates[i] - upper[i]) + loss.value_or(0.0);
}
}
return loss;
}
auto ObjectiveType::Evaluate(const arma::mat coordinates) -> double {
const auto outOfBounds =
outOfBoundsLoss(aberrations, {-100, -10'000}, {100, 10'000});
if (outOfBounds.has_value()) {
return outOfBounds.value();
}
// In-bounds calculations
} |
Ok, there is no problem with Also, a better approach with the above code would be not to return the out-of-bounds loss, but instead add it as penalty, otherwise algorithm can optimize out-of-bounds and converge to one of the bounds: auto ObjectiveType::Evaluate(const arma::mat coordinates) -> double {
const auto penalty =
outOfBoundsLoss(aberrations, {-100, -10'000}, {100, 10'000});
// In-bounds calculations
// result = ...
return result + penalty.value_or(0.0);
} |
This issue has been automatically marked as stale because it has not had any recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions! 👍 |
Hello! First of all, thanks for your work, really like the library!
I have optimization problems of arbitrary functions which could have many minima, but only the ones in a particular part of the variable space are of interest. Currently the best approach I could find was to evaluate the function to
std::numerical_limits::max()
when inputs are outside the bounds, which certainly made the results better. Is this the idiomatic way? If not, is there a better approach or maybe plans to add bounds as parameters to the optimizer?The text was updated successfully, but these errors were encountered: