【发布时间】:2021-12-17 21:44:55
【问题描述】:
我们有一位用户想要使用基于梯度的方法解决具有中间离散变量的优化问题。他们遇到了this error。我知道我们可以重组问题以不使用离散变量,但鉴于离散变量不会改变,我是否也可以将此错误视为警告?或者是否存在衍生品无法正确传播的根本原因。为了清楚起见,我们在模型级别使用了 approx_totals。
这是一个展示这一点的小测试用例:
import openmdao.api as om
import numpy as np
class DiscreteComp(om.ExplicitComponent):
def setup(self):
self.add_input('a', 2.)
self.add_discrete_output('b', val=0)
def compute(self, inputs, outputs, discrete_inputs, discrete_outputs):
discrete_outputs['b'] = 2 * inputs['a']
class DummyComp(om.ExplicitComponent):
def setup(self):
self.add_input('a', 2.)
self.add_discrete_input('b', 1)
self.add_output('c')
def compute(self, inputs, outputs, discrete_inputs, discrete_outputs):
b = discrete_inputs['b']
outputs['c'] = inputs['a']**2 * b
prob = om.Problem()
prob.model.add_subsystem('discrete_comp', DiscreteComp(), promotes=['*'])
prob.model.add_subsystem('dummy_comp', DummyComp(), promotes=['*'])
prob.driver = om.pyOptSparseDriver()
prob.driver.options['optimizer'] = 'SLSQP'
prob.model.add_design_var('a', lower=-10, upper=10)
prob.model.add_objective('c')
prob.model.approx_totals()
prob.setup()
# run the optimization
prob.run_driver()
给出这个输出:
Traceback (most recent call last):
File "tmp.py", line 39, in <module>
prob.run_driver()
File "/home/john/anaconda3/envs/weis/lib/python3.8/site-packages/openmdao/core/problem.py", line 685, in run_driver
return self.driver.run()
File "/home/john/anaconda3/envs/weis/lib/python3.8/site-packages/openmdao/drivers/pyoptsparse_driver.py", line 480, in run
raise self._exc_info
File "/home/john/anaconda3/envs/weis/lib/python3.8/site-packages/openmdao/drivers/pyoptsparse_driver.py", line 643, in _gradfunc
sens_dict = self._compute_totals(of=self._quantities,
File "/home/john/anaconda3/envs/weis/lib/python3.8/site-packages/openmdao/core/driver.py", line 892, in _compute_totals
total_jac = _TotalJacInfo(problem, of, wrt, use_abs_names,
File "/home/john/anaconda3/envs/weis/lib/python3.8/site-packages/openmdao/core/total_jac.py", line 209, in __init__
raise RuntimeError("Total derivative %s '%s' depends upon "
RuntimeError: Total derivative with respect to '_auto_ivc.v0' depends upon discrete output variables ['discrete_comp.b'].
【问题讨论】:
标签: openmdao