zoukankan      html  css  js  c++  java
  • scipy.optimize.minimize 解决实际问题

    接上博客问题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)
  • 相关阅读:
    圣杯布局,不太明白为什么后面的元素会飘上来
    CSS实现宽高成比例缩放
    javascript原生ajax;
    http-关于application/x-www-form-urlencoded等字符编码的解释说明
    jQuery判断滚动条滚到页面底部脚本
    下拉顶部刷新简单实现
    swig模板下拉框应用
    swig模板中文资料
    张宵 20200924-2 功能测试
    张宵 20200924-5 四则运算试题生成
  • 原文地址:https://www.cnblogs.com/shizhenqiang/p/8276004.html
Copyright © 2011-2022 走看看