(Python)Scipy Minimize:多个变量列表

时间:2018-06-01 17:56:43

标签: python optimization scipy minimize numexpr

问题:优化1600行的总计利润。每行都有自己的一组三个参数(x1,x2,b)。我们的想法是选择公司1的出价金额x1和公司2的出价金额x2以及利润和成本公式中使用的参数b。

在先前的实现(一个业务)中,每行有一个参数(x),因此我可以发送1600个元素的列表并执行以下操作:

def profit_model(x):
    equation = ne.evaluate("<function of x>")
    return -1 * np.nansum(equation)

def est_cost(x):
    cost = ne.evaluate("<another function of x>")
    return np.nansum(cost)

cons = ({'type': 'ineq', 'fun': lambda x: np.array(cost_constraint - est_cost(x))},
        {'type': 'ineq', 'fun': lambda x: np.array(max_bid - (x))},
        {'type': 'ineq', 'fun': lambda x: np.array((x) - 1)})

x0 = np.zeros(len(master_bid_optimization_table))

opts = {'disp': True}

res = minimize(profit_model, x0, constraints=cons, options=opts)

ne.evaluate()允许我传递列表参数并对所有行求和。作为示例,每行具有影响profit_model的曲线参数。所以作为一个基本的例子:

mean = master_bid_optimization_table.ix[:, 'mean']

def example(x):
    equation = ne.evaluate("mean * x")
    return -1 * np.nansum(equation)

我需要推断这种相同的方法,但每行优化三个参数。

截至目前,我正在尝试如下:

def profit_model(x1, x2, b):
    bus_1_profit = ne.evaluate("<func of x1, x2, and b>")
    bus_2_profit = ne.evaluate("<func of x1, x2, and b>")
    equation = ne.evaluate("bus_1_profit + bus_2_profit")
    return -1 * np.nansum(equation)


def est_cost(x1, x2, b):
    bus_1_cost = ne.evaluate("<func of x1, x2, and b>")
    bus_2_cost = ne.evaluate("<func of x1, x2, and b>")
    cost = ne.evaluate("bus_1_cost + bus_2_cost")
    return np.nansum(cost)

cons = ({'type': 'ineq', 'fun': lambda (x1, x2, b): np.array(cost_constraint - est_cost(x1, x2, b))})

guess = [np.zeros(len(master_bid_optimization_table)), #<- x1
         np.zeros(len(master_bid_optimization_table)), #<- x2
         [.5]*int(len(master_bid_optimization_table))] #<- b

bound = [[(1, max_bid)]*int(len(master_bid_optimization_table)), #<- 1 <= x1 <= max_bid
          [(1, max_bid)]*int(len(master_bid_optimization_table)), #<- 1 <= x2 <= max_bid
          [(0, 1)]*int(len(master_bid_optimization_table))] #<- 0 <= b <= 1

opts = {'disp': True}

res = minimize(fun=profit_model, x0=guess, bounds=bound, options=opts)

问题显然来自于传递3个列表的列表。我需要一种方法来指定master_bid_optimization_table的每一行需要3个参数进行优化。

我也认为我对约束的lambda函数做错了。

如果有人需要更多代码或希望我创建一个虚拟数据帧和简单的反双曲线函数来最大化/最小化,请告诉我。

非常感谢任何帮助或指示。提前谢谢。

0 个答案:

没有答案