Complex constraints in SciPy minimize

60 Views Asked by At

I am trying to set up a few constraints for my minimization problem which looks as below: result = minimize(objective_func, initial_guess, constraints=all_constraints) where the initial_guess = [0, 0, 5, 0, 8, 0, 8, 1, 8, 2, 8, 3, 8, 4, 8, 5, 10, 5, 15, 5, 17, 4, 20, 3] is a polyline which consists of the following points (0,0), (5,0), (8,0) ... (17, 4), (20, 3). How can I add into all_constraints a constraint that requires that all points are within a given shapely polygon inclusion_polygon ?

This is the current constraint that I have implemented with respect to the inclusion of the points from the given polyline:

def line_within_con(polyline):
    polyline = [(polyline[i], polyline[i + 1]) for i in range(0, len(polyline) - 1, 2)]
    line_string = LineString(polyline)
    if not line_string.within(inclusion_polygon):
        return (-1) * line_string.distance(inclusion_polygon)
    else:
        return line_string.distance(inclusion_polygon)

and all_constraints looks as below:

all_constraints = [..., {'type': 'ineq', 'fun':line_within_con}, ....]

This constraint aims to penalize the case where the polyline is not included within the polygon by the distance to the polygon. Am I doing something wrong with the implementation of this constraint, as the minimization problem seems to totally ignore this constraint. How can I improve it? Thank you in advance!

0

There are 0 best solutions below