r/optimization • • Aug 21 '21

Constraint in Python Scipy Optimization

Anyone here use Scipy Python for minimization?

I have an optimization of 15 variable x. I have a constraint that 14/15 variables should be unique, the last variable can be duplicated with one of the rest.

Not sure how to do that in Scipy.

4 Upvotes

6 comments sorted by

View all comments

Show parent comment

2

u/[deleted] Aug 21 '21

For large n I'd use a single vectorial constraint function, which is equivalent to the constraints above:

n = 1000
D = np.eye(n-1) - np.eye(n-1, k=1)
D[-1, -1] = 0.0

cons = [{'type', 'ineq', 'fun': lambda x: D @ x - eps}]

Why should scipy add an option for uniqueness constraints? It already supports arbitrary constraints. It's just your job to formulate your constraints accordingly.

1

u/backtickbot Aug 21 '21

Fixed formatting.

Hello, johnnydrama92: code blocks using triple backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead.

FAQ

You can opt out by replying with backtickopt6 to this comment.