Scipy minimisation optimisation row-wise on DataFrame

不羁岁月 提交于 2021-01-29 18:41:12

问题


TYPO FIXEDD

I need to perform a minimization optimisation for each timestep in my timeseries. The optimisation sets the price based on values in different columns across the row and a series of inequality constraints.

My Dataframe has the following columns across time series of 48 years:

['CAPEX_TOT', 'CAPEX_R', 'CAPEX_WS', 'debt_BP', 'principal','interest',
   'debt_service', 'debt_EP', 'OPEX', 'OPEX_R', 'OPEX_WS',
   'DELIVERY_BAWSCA', 'DELIVERY_OTHER_DEMAND',
   'DELIVERY_SAN_FRANCISCO_CITY', 'DELIVERIES_WS', 'DELIVERIES_R',
   'PRICE_crR', 'PRICE_crWS', 'REVENUE', 'FUND_BALANCE_BP',
   'FUND_BALANCE_EP']

PRICE_crR and PRICE_crWS represent the prices for two different customer classes on the basis purely of cost recovery. The optimisation must seek to achieve cost recovery (first constraint in the code below) whilst observing a couple of key policy constraints which are represented by the second and third constraints in the code below.

This is what I have so far.

the objective function to minimise

finance_df['revenue_R'] = finance_df.apply(lambda row: row * row.DELIVERIES_R)

the constraints

cons = ({'type': 'ineq', 'fun': finance_df.apply(lambda row: row - row.price_crR, axis=1)},
    {'type': 'ineq', 'fun': finance_df.apply(lambda row: ((row * row.DELIVERIES_R) - row.OPEX_R + OTHER_REVENUE)\
                                            / (debt_CAPEX_ratio * row.debt_service), axis=1)},
    {'type': 'ineq', 'fun': finance_df.apply(lambda row: (1.05 * row.price_crR) - row, axis=1)})

non negativity constraints

bnds = ((0, None), (0, None))

series of initial best guesses

price_0 = [7, 7.5, 8, 8.5, 9, 9.5, 10]

the optimisation function

res = minimize(finance_df['revenue_R'], price_0, method='SLSQP', bounds=bnds, constraints=cons)

When running the above script I get the following error message:

    ("'Series' object has no attribute 'DELIVERIES_R'", 'occurred at index CAPEX_TOT')

回答1:


The problem is caused by an incorrect value, finance_df.apply(lambda row: row * row.DELIVERIES_R). Unlike the iterating across a row (DataSeries), iterating across a table (DataFrame) requires the axis = 1 to be specified if row-wise, namely df.apply(fun, axis=1) else you get columns (axis 0) and as the column and the error you saw.

(Response first given as comment, now converted to answer to close quickly this typo question).



来源:https://stackoverflow.com/questions/57508028/scipy-minimisation-optimisation-row-wise-on-dataframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!