I have a Pyomo model and I'd like to get the gradient and Hessian of the objective. A related SO question asks the same question. When I try the solution proposed there
from pyomo.core.base.symbolic import differentiate
from pyomo.core.base.expr import identify_variables
varList = list(identify_variables(zipfe.loglikelihood.expr))
firstDerivs = differentiate(zipfe.loglikelihood.expr, wrt_list=varList)
I get the following error:
Traceback (most recent call last):
File "/home/pauperei/.conda/envs/py36/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2862, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-9-6f2637b1fe13>", line 1, in <module>
firstDerivs = differentiate(zipfe.loglikelihood.expr, wrt_list=varList)
File "/home/pauperei/.conda/envs/py36/lib/python3.6/site-packages/pyomo/core/base/symbolic.py", line 122, in differentiate
tmp_expr, locals=dict((str(x), x) for x in sympy_vars) )
File "/home/pauperei/.conda/envs/py36/lib/python3.6/site-packages/sympy/core/sympify.py", line 354, in sympify
expr = parse_expr(a, local_dict=locals, transformations=transformations, evaluate=evaluate)
File "/home/pauperei/.conda/envs/py36/lib/python3.6/site-packages/sympy/parsing/sympy_parser.py", line 894, in parse_expr
return eval_expr(code, local_dict, global_dict)
File "/home/pauperei/.conda/envs/py36/lib/python3.6/site-packages/sympy/parsing/sympy_parser.py", line 807, in eval_expr
code, global_dict, local_dict) # take local objects in preference
File "<string>", line 1, in <module>
TypeError: 'Symbol' object does not support indexing
Here is how my objective looks like (first few lines):
zipfe.loglikelihood.pprint()
loglikelihood : Size=1, Index=None, Active=True
Key : Active : Sense : Expression
None : True : minimize : log( 1 + exp( alpha1[0] + 2.0*alpha1[1] + alpha1[4] + 2.8986705607108596*( delta[0] + 2.0*delta[1] ) ) ) - ( 2.0*beta1[0] + beta1[3] + 2.8986705607108596*( gamma[0] + 2.0*gamma[1] ) ) + log( exp( - log( 1 + exp( alpha1[0] + 2.0*alpha1[1] + alpha1[4] + 2.8986705607108596*( delta[0] + 2.0*delta[1] ) ) ) + 2.0*beta1[0] + beta1[3] + 2.8986705607108596*( gamma[0] + 2.0*gamma[1] ) ) + exp( - log( 1 + exp( alpha1[0] + 5.0*alpha1[1] + 2.8986705607108596*( delta[0] + 2.0*delta[1] ) ) ) + 5.0*beta1[0] + 2.8986705607108596*( gamma[0] + 2.0*gamma[1] ) ) + exp( - log( 1 + exp( alpha1[0] + 2.0*alpha1[1] + alpha1[7] + 2.8986705607108596*( delta[0] + 2.0*delta[1] ) ) ) + 2.0*beta1[0] + beta1[6] + 2.8986705607108596*( gamma[0] + 2.0*gamma[1] ) ) + exp( - log( 1 + exp( alpha1[0] + alpha1[1] + alpha1[6]
It seems that the problem is that Sympy doesn't like indexed variables like alpha1[0]
. Is there any workaround to this problem?
I'm using Pyomo 5.2 and Python 3.6. I'll try to add a minimal working example soon.
In the last couple of days this has been aded as To Do in the Pyomo GitHub repository so hopefully there will be a solution soon.
To use indexed variables, use Indexed .
>>> alpha1 = IndexedBase('alpha1')
>>> alpha1[0]
alpha1[0]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.