I'm trying to parse an expression resulting from optimizing a pyomo model using SymPy. The model contains both simple and indexed variable references, which are represented in the resulting expression like this:
simple:
simple_var
indexed:
indexed_var[index_name]
where index_name is a value from a pyomo defined set.
I notice that when trying to parse the resulting expression, SymPy consistently fails with this error:
Traceback (most recent call last): Blockquote
jacobians = differentiate(model.objective.expr, wrt_list=var_list)
in differentiate tmp_expr, locals=dict((str(x), x) for x in sympy_vars) )
in sympify expr = parse_expr(a, local_dict=locals, transformations=transformations, evaluate=evaluate)
return eval_expr(code, local_dict, global_dict)
in parse_expr eval_expr(code, global_dict, local_dict)
TypeError: 'Symbol' object is not subscriptable
EDIT: This is basically the stacktrace without a lot of local file references
I tried to use pyomo to parse the expression in hopes it will pass on some extra context but the result is the same. The only obvious way to move forward for me would be to do some string replacement in the resulting expression, parse it with SymPy, then do some more string parsing after I'm done with SymPy and extract my data from there.
That seems really error prone, so here I am asking if there is a more elegant suggestion.