Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python线性优化的Lambda目标函数_Python_Scipy_Linear Programming - Fatal编程技术网

python线性优化的Lambda目标函数

python线性优化的Lambda目标函数,python,scipy,linear-programming,Python,Scipy,Linear Programming,我试图解决一个需要优化线性函数的问题,但实际上这必须通过包装函数来访问,我可以用原始数据来解决这个问题,但由于实现限制,我需要一种方法来传递函数作为目标 比如说, import numpy as np from scipy.optimize import linprog def objective(x): c = [-1, 4] return np.dot(c,x) c = [-1, 4] A = [[-3, 1], [1, 2]] b = [6, 4] x0_bounds =

我试图解决一个需要优化线性函数的问题,但实际上这必须通过包装函数来访问,我可以用原始数据来解决这个问题,但由于实现限制,我需要一种方法来传递函数作为目标

比如说,

import numpy as np
from scipy.optimize import linprog

def objective(x):
    c = [-1, 4]
    return np.dot(c,x)
c = [-1, 4]
A = [[-3, 1], [1, 2]]
b = [6, 4]
x0_bounds = (None, None)
x1_bounds = (-3, None)
#This Works:
res = linprog(c, A_ub=A, b_ub=b, bounds=(x0_bounds, x1_bounds),
             options={"disp": True})
#This does not
res = linprog(objective, A_ub=A, b_ub=b, bounds=(x0_bounds, x1_bounds),
             options={"disp": True})

我在cvxopt、cvxpy或scipy中看不到任何合适的资源。非常感谢您的帮助。

您注意到linprog需要一个系数数组
c
,因此如果您有一个函数,您可以简单地推导出c数组:

import numpy as np

x = np.diag((1,1))
c = np.linalg.solve(x,objective(x))

res = linprog(c, A_ub=A, b_ub=b, bounds=(x0_bounds, x1_bounds),
             options={"disp": True})

线性函数可以完全由其系数定义,因此不清楚为什么需要传递整个函数而不是传递其系数。