我想将数据从JSON文件加载到我的Pyomo模型中。 我从
开始m = AbstractModel()
m.T = RangeSet(1,48,1)
m.TS = Param(m.T)
dp = DataPortal()
稍后我想加载json文件以填充参数TS
dp.load(param = m.TS, filename = data.json)
数据是一个由整数1,2,...,48索引的时间序列 从而 看起来像这样:
{
"1" : 0.0001,
"2" : 0.1 ,
....,
"48" : 0
}
然后我创建
create_instance(dp)
我得到以下错误:
RuntimeError: Failed to set value for param=TS, index=1, value=35.676187.
source error message="Index '1' is not valid for indexed component 'TS'"
答案 0 :(得分:0)
因此,我提出了解决此问题的方法。 JSON文件应以以下方式编写:
foreach ($orders as $key => $order) {
// find number of products in common with other orders
foreach ($orders as $k2 => $o2) {
if ($k2 <= $key) continue;
$common["{$order['order_id']}-{$o2['order_id']}"] = count(array_intersect($order['products'], $o2['products']));
}
}
arsort($common);
$orders_used = array();
foreach (array_keys($common) as $order_pair) {
list($order1, $order2) = explode('-', $order_pair);
// already output these orders?
if (in_array($order1, $orders_used) || in_array($order2, $orders_used)) continue;
$orders_used[] = $order1;
$orders_used[] = $order2;
// if we've used all the orders, quit
if (count($orders_used) == count($orders)) break;
}
for ($i = 0; $i < count($orders_used); $i += 4) {
echo "batch " . ($i / 4 + 1) . ": orders " . implode(',', array_slice($orders_used, $i, 4)) . "\n";
}