Giter VIP home page Giter VIP logo

Comments (5)

zoq avatar zoq commented on August 24, 2024

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?

from ensmallen.

rcurtin avatar rcurtin commented on August 24, 2024

@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 DBL_MAX (or similar); think, e.g., adding some constant multiplied by how far you are from the edge of the bounds. So for a one-dimensional problem, if you want something in [0, 1], maybe add, say, 1000 * (std::min(x[0], x[0] - 1)) or something like this if x[0] is not in [0, 1]. Just an idea.

from ensmallen.

Kaltxi avatar Kaltxi commented on August 24, 2024

@zoq Sorry for late reply! I was using DE. PSO for me has a strange bug of not terminating even due to maxIterations - it just goes on and on; I've looked at the ensmallen code, and it looks like it should work...

@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 DBL_MAX:

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
}

from ensmallen.

Kaltxi avatar Kaltxi commented on August 24, 2024

Ok, there is no problem with maxIterations, I was counting evaluations, not iterations... But bounds for PSO do not work, I still need to use the loss-bounding function. It kinda makes sense, even in documentation it is written that bounds are only for the initial population.

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);
}

from ensmallen.

mlpack-bot avatar mlpack-bot commented on August 24, 2024

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! 👍

from ensmallen.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.