码迷,mamicode.com
首页 > 其他好文 > 详细

非线性规划带约束-scipy.optimize.minimize

时间:2018-01-03 17:30:05      阅读:4151      评论:0      收藏:0      [点我收藏+]

标签:port   ret   mini   color   线性   div   return   nump   str   

 1 # coding=utf-8
 2 
 3 from scipy import optimize
 4 import numpy as np
 5 
6 7 8 9 10 11 12 13 14 15 def get(args): 16 a, b, c, d, e, f, g, h = args 17 fun = lambda x:a*x[0]**g+b*x[0]*x[1]+c*x[1]**h+d*x[0]+e*x[1] + f 18 #fun = lambda x:(x[0] - 1) ** h + (x[1] - 2.5) ** h 19 return fun 20 21 22 def con(args): 23 # Equality constraint means that the constraint function result is to be zero whereas inequality means that it is to be non-negative 24 x1min, x1max, x2min, x2max = args 25 cons = ({type: ineq, fun: lambda x: x[0] - x1min},26 {type: ineq, fun: lambda x: -x[0] + x1max},27 {type: ineq, fun: lambda x: x[1] - x2min},28 {type: ineq, fun: lambda x: -x[1] + x2max}) 29 return cons 30 31 32 if __name__ == "__main__": 33 args = (2, 3, 7, 8, 9, 10, 2, 2) #a, b, c, d, e, f,g,h 34 args1 = (-1000, 1000, -1000, 1000) #x1min, x1max, x2min, x2max 35 x0 = np.asarray((0, 0)) 36 fun = get(args) 37 cons = con(args1) 38 res = optimize.minimize(fun, x0, method=SLSQP, constraints=cons) 39 print(res.fun) 40 print(res.success) 41 print(res.x)

 

非线性规划带约束-scipy.optimize.minimize

标签:port   ret   mini   color   线性   div   return   nump   str   

原文地址:https://www.cnblogs.com/shizhenqiang/p/8184395.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!