Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
355 views
in Technique[技术] by (71.8m points)

python - Scipy.optimize Inequality Constraint - Which side of the inequality is considered?

I am using the scipy.optimize module to find optimal input weights that would minimize my output. From the examples I've seen, we define the constraint with a one-sided equation; then we create a variable that's of the type 'inequality'. My question is how does the optimization package know whether the sum of the variables in my constraint need to be smaller than 1 or larger than 1?

...

def constraint1(x):
    return x[0]+x[1]+x[2]+x[3]-1

....

con1 = {'type': 'ineq', 'fun': constraint1}

link to full solution I'm using in my example: http://apmonitor.com/che263/index.php/Main/PythonOptimization

Thank you.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you refer to https://docs.scipy.org/doc/scipy-0.18.1/reference/tutorial/optimize.html and scrool down to Constrained minimization of multivariate scalar functions (minimize), you can find that

This algorithm allows to deal with constrained minimization problems of the form:

enter image description here

where the inequalities are of the form C_j(x) >= 0.

So when you define the constraint as

def constraint1(x):
    return x[0]+x[1]+x[2]+x[3]-1

and specify the type of the constraint as

con1 = {'type': 'ineq', 'fun': constraint1}

it automatically assumes that the constraint is in the standard form x[0]+x[1]+x[2]+x[3]-1>=0 i.e., x[0]+x[1]+x[2]+x[3]>=1


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...