Skip to content
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

Force minimum length of segments to be greater than x #66

Open
lucascibona opened this issue Mar 17, 2020 · 2 comments
Open

Force minimum length of segments to be greater than x #66

lucascibona opened this issue Mar 17, 2020 · 2 comments

Comments

@lucascibona
Copy link

Hello,

How can I add a constraint in the outer optimization function: minimum length of each segment should be >= x?

@cjekel
Copy link
Owner

cjekel commented Mar 18, 2020

I think there are two ways to approach this.

  1. Add a penalty parameter to the objective function. See [1] for a reasonable penalty formulation.
  2. Use a constrained optimization algorithm.

It's fairly straight forward to just add a constraint function and use a constrained optimization algorithm (option 2). If this doesn't work for you, you may want to investigate a penalty formulation.

This jupyter notebook uses SLSQP to apply a constraint function to force a minimum line segment length. You'll need to supply a starting point (or guess) to start the optimization. If you don't know what is a good starting point, check out how I use Latin Hypercube random sampling to run multiple optimizations in the fitfast function.

A constraint function could look like:

def my_con(var):
    var = np.sort(var)
    distances = np.zeros(number_of_line_segments)
    distances[0] = var[0] - my_pwlf.break_0
    distances[-1] = my_pwlf.break_n - var[-1]
    for i in range(number_of_line_segments - 2):
        distances[i+1] = var[i+1] - var[i]
    # element must be greater or equal to 0.0
    # in a successfully optimized problem
    return np.array((distances.min() - min_length))

This is a single constraint for the minimum length of all segments. It's possible that the min() in this function will create issues with the gradient of the constraint. If you run into issues with this, you may want to investigate using a separate constraint for each line segment. That could be done by changing:

    return np.array((distances.min() - min_length))

to

    return distances - min_length

[1] Haftka, R. T. and Starnes, J. H., Jr., "Applications of a Quadratic Extended Interior Penalty Functions for Structural Optimization," AIAA Journal, Vol. 14, pp. 718-724, 1976 PDF

@lucascibona
Copy link
Author

thank you for the very detailed answer -- i'll go through your suggestion asap

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants