• 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)
  • 相关阅读:
    HDU1698(线段树入门题)
    POJ2528(离散化+线段树区间更新)
    POJ3630(Trie树)
    HDU1251(字典树)
    HDU1247(经典字典树)
    POJ2513(字典树+图的连通性判断)
    POJ1363
    UVa11624(逃离火焰问题)
    HDOJ1495(倒水BFS)
    poj3414Pots(倒水BFS)
  • 原文地址:https://www.cnblogs.com/shizhenqiang/p/8276004.html
Copyright © 2020-2023  润新知