这是一个基本问题,但很难从文档和示例中收集它的工作方式。我想为电池控制器创建一个简单的 MPC - 给定已知负载 ( P_load
)、已知太阳能发电 ( P_solar
) 和已知能源成本 (energy_charge
和demand_charge
),如何正确设置tvp_fun()
以使用这些值数组?
以下是我设置 TVP 的方法template_model.py
:
P_load = model.set_variable('_tvp', 'P_load')
P_solar = model.set_variable('_tvp', 'P_solar')
energy_charge = model.set_variable('_tvp', 'energy_charge')
demand_charge = model.set_variable('_tvp', 'demand_charge')
以下是我如何定义它们template_mpc.py
:
tvp_template = mpc.get_tvp_template()
tvp_template['_tvp', :, 'P_load'] = pd.read_csv("../../data/data_P_load.csv", squeeze=True).values
tvp_template['_tvp', :, 'P_solar'] = pd.read_csv("../../data/data_P_solar.csv", squeeze=True).values
tvp_template['_tvp', :, 'energy_charge'] = pd.read_csv("../../data/data_energy_charge.csv", squeeze=True).values
tvp_template['_tvp', :, 'demand_charge'] = pd.read_csv("../../data/data_demand_charge.csv", squeeze=True).values
def tvp_fun(t_now):
return tvp_template
mpc.set_tvp_fun(tvp_fun)
在template_simulator.py
我只是执行以下操作:
tvp_template = simulator.get_tvp_template()
def tvp_fun(t_now):
return tvp_template
simulator.set_tvp_fun(tvp_fun)
但是,我在运行时收到此错误消息python main.py
:
Traceback (most recent call last):
File "/Users/darren.hau/anaconda3/envs/der-live-optimizer/lib/python3.7/site-packages/casadi/tools/structure3.py", line 594, in __call__
self.master[i] = payload_
File "/Users/darren.hau/anaconda3/envs/der-live-optimizer/lib/python3.7/site-packages/casadi/casadi.py", line 7481, in __setitem__
return self.set(val, False, s)
File "/Users/darren.hau/anaconda3/envs/der-live-optimizer/lib/python3.7/site-packages/casadi/casadi.py", line 6372, in set
return _casadi.DM_set(self, *args)
RuntimeError: .../casadi/core/matrix_impl.hpp:1178: Assertion "is_scalar()" failed:
Can only convert 1-by-1 matrices to scalars
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Users/darren.hau/anaconda3/envs/der-live-optimizer/lib/python3.7/site-packages/casadi/tools/structure3.py", line 213, in traverseByPowerIndex
return dispatcher(payload,canonicalIndex,entry=self)
File "/Users/darren.hau/anaconda3/envs/der-live-optimizer/lib/python3.7/site-packages/casadi/tools/structure3.py", line 611, in __call__
raise Exception("Error in powerIndex slicing for canonicalIndex %s" % (str(canonicalIndex))) from e
Exception: Error in powerIndex slicing for canonicalIndex ('_tvp', 0, 'P_load')
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Users/darren.hau/anaconda3/envs/der-live-optimizer/lib/python3.7/site-packages/casadi/tools/structure3.py", line 348, in traverseByPowerIndex
payload=payload
File "/Users/darren.hau/anaconda3/envs/der-live-optimizer/lib/python3.7/site-packages/casadi/tools/structure3.py", line 274, in traverseByPowerIndex
raise Exception("Error occured in entry context with powerIndex %s, at canonicalIndex %s" % (str(powerIndex),str(canonicalIndex))) from e
Exception: Error occured in entry context with powerIndex (), at canonicalIndex ('_tvp', 0, 'P_load')
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Users/darren.hau/anaconda3/envs/der-live-optimizer/lib/python3.7/site-packages/casadi/tools/structure3.py", line 223, in traverseByPowerIndex
payload=payload
File "/Users/darren.hau/anaconda3/envs/der-live-optimizer/lib/python3.7/site-packages/casadi/tools/structure3.py", line 430, in traverseByPowerIndex
raise Exception("Error occured in struct context with powerIndex %s, at canonicalIndex %s" % (str(powerIndex),str(canonicalIndex))) from e
Exception: Error occured in struct context with powerIndex ('P_load',), at canonicalIndex ('_tvp', 0)
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Users/darren.hau/anaconda3/envs/der-live-optimizer/lib/python3.7/site-packages/casadi/tools/structure3.py", line 247, in traverseByPowerIndex
for i in p]
File "/Users/darren.hau/anaconda3/envs/der-live-optimizer/lib/python3.7/site-packages/casadi/tools/structure3.py", line 247, in <listcomp>
for i in p]
File "/Users/darren.hau/anaconda3/envs/der-live-optimizer/lib/python3.7/site-packages/casadi/tools/structure3.py", line 274, in traverseByPowerIndex
raise Exception("Error occured in entry context with powerIndex %s, at canonicalIndex %s" % (str(powerIndex),str(canonicalIndex))) from e
Exception: Error occured in entry context with powerIndex ('P_load',), at canonicalIndex ('_tvp', 0)
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Users/darren.hau/anaconda3/envs/der-live-optimizer/lib/python3.7/site-packages/casadi/tools/structure3.py", line 348, in traverseByPowerIndex
payload=payload
File "/Users/darren.hau/anaconda3/envs/der-live-optimizer/lib/python3.7/site-packages/casadi/tools/structure3.py", line 274, in traverseByPowerIndex
raise Exception("Error occured in entry context with powerIndex %s, at canonicalIndex %s" % (str(powerIndex),str(canonicalIndex))) from e
Exception: Error occured in entry context with powerIndex (slice(None, None, None), 'P_load'), at canonicalIndex ('_tvp',)
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "main.py", line 49, in <module>
mpc = template_mpc(model)
File "/Users/darren.hau/Desktop/der-live-optimizer/examples/bess/template_mpc.py", line 55, in template_mpc
tvp_template['_tvp', :, 'P_load'] = pd.read_csv("../../data/data_P_load.csv", squeeze=True).values
File "/Users/darren.hau/anaconda3/envs/der-live-optimizer/lib/python3.7/site-packages/casadi/tools/structure3.py", line 118, in proper
return f(self,mbt,*args)
File "/Users/darren.hau/anaconda3/envs/der-live-optimizer/lib/python3.7/site-packages/casadi/tools/structure3.py", line 635, in __setitem__
SetterDispatcher(struct=self.struct,master=self.master,mtype=self.mtype),payload=value)
File "/Users/darren.hau/anaconda3/envs/der-live-optimizer/lib/python3.7/site-packages/casadi/tools/structure3.py", line 430, in traverseByPowerIndex
raise Exception("Error occured in struct context with powerIndex %s, at canonicalIndex %s" % (str(powerIndex),str(canonicalIndex))) from e
Exception: Error occured in struct context with powerIndex ('_tvp', slice(None, None, None), 'P_load'), at canonicalIndex ()