3

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.

omu_negru
  • 4,642
  • 4
  • 27
  • 38
  • What SymPy function are you using to parse? Can you provide a minimal example? – Wrzlprmft Oct 10 '17 at 15:35
  • @Wrzlprmft It's sort of there in the post, e.g. `S("x[k]")` fails. –  Oct 10 '17 at 15:37
  • @Desire: How can you be sure that `S`/`sympify` is used? Sympy has [an entire submodule dedicated to parsing](http://docs.sympy.org/latest/modules/parsing.html). Either way, I don’t like to go through a bunch of candidate functions to see which one produces a matching error. – Wrzlprmft Oct 10 '17 at 15:46
  • it's sympify... i'll update the post with the full stack trace – omu_negru Oct 10 '17 at 15:51
  • You can sanitize it if you'd like, but the local file references might be helpful to see which function in which module is being called. I'm having a hard time following the stacktrace as it's written. – Qi Chen Oct 10 '17 at 16:36
  • i ended up sanitizing it..... i think the problem i'm facing now is actually extracting concrete numbers from the parsed expression – omu_negru Oct 10 '17 at 17:10

0 Answers0