scipy.optimize.minimize 解决实际问题

mac2022-06-30  111

接上博客问题http://www.cnblogs.com/shizhenqiang/p/8274806.html

# coding=utf-8 from scipy import optimize import numpy as np def get(): ar = [160, 130, 220, 170, 140, 130, 190, 150, 190, 200, 230] fun = lambda x:(x[0]*ar[0]+x[1]*ar[1]+x[2]*ar[2]+x[3]*ar[3]+x[4]*ar[4]+ x[5]*ar[5]+x[6]*ar[6]+ x[7]*ar[7]+ x[8]*ar[8]+x[9]*ar[9]+x[10]*ar[10]) return fun def con(): # Equality constraint means that the constraint function result is to be zero whereas inequality means that it is to be non-negative x1min, x2min, x3min, x4min,x5min ,x6min,x7min,x8min,x9min,x10min,x11min = [50, 60, 50, 30, 70, 10, 10, 80, 140,30,50] cons = ({'type': 'eq', 'fun': lambda x: x[0] + x[1] + x[2] + x[3] - x1min},\ {'type': 'eq', 'fun': lambda x: x[4] + x[5] + x[6] + x[7] - x2min},\ {'type': 'eq', 'fun': lambda x: x[8] + x[9] + x[10] - x3min},\ {'type': 'ineq', 'fun': lambda x: x[0]+x[4]+x[8] - x4min},\ {'type': 'ineq', 'fun': lambda x: x[1] + x[5] + x[9] - x5min},\ {'type': 'ineq', 'fun': lambda x: x[2] + x[6] + x[10] - x6min}, \ {'type': 'ineq', 'fun': lambda x: x[3] + x[7] - x7min}, \ {'type': 'ineq', 'fun': lambda x: -(x[0] + x[4] + x[8] - x8min)}, \ {'type': 'ineq', 'fun': lambda x: -(x[1] + x[5] + x[9] - x9min)}, \ {'type': 'ineq', 'fun': lambda x: -(x[2] + x[6] + x[10] - x10min)}, \ {'type': 'ineq', 'fun': lambda x: -(x[3] + x[7] - x11min)}, \ ) return cons if __name__ == "__main__": #args = (2, 3, 7, 8, 9, 10, 2, 2) #a, b, c, d, e, f,g,h #args = (0, 0,0, 0,0, 0, 0, 0) #a, b, c, d, e, f,g,h #args1 = (-1000, 1000, -1000, 1000) #x1min, x1max, x2min, x2max x0 = np.asarray((0, 0,0,0,0,0,0,0,0,0,0)) fun = get() cons = con() bnds = ((0, None), (0, None),(0, None), (0, None),(0, None), (0, None),(0, None), (0, None),(0, None), (0, None),(0, None)) res = optimize.minimize(fun, x0, method='CG', bounds=bnds,constraints=cons) #print(res) print(res.fun) print(res.success) print(res.x)

 

转载于:https://www.cnblogs.com/shizhenqiang/p/8276004.html

相关资源:JAVA上百实例源码以及开源项目
最新回复(0)