如何将整数变量添加到 openmdao 驱动程序?
How can I add an integer variable to an openmdao driver?
我对这个 openmdao 错误感到困惑。为什么要提出这个问题?我能以某种方式告诉 openmdao 我没有梯度并使用有限差分吗?为什么这是为 childWeight 而不是 eta 提出的?
我可以通过将所有变量初始化为浮点数(例如 root.add('childWeight', IndepVarComp('x',100))
-> root.add('childWeight', IndepVarComp('x',100.0))
)来解决这个问题,但我想了解为什么会出现此错误。
from openmdao.api import Component, Group, Problem, ScipyOptimizer, IndepVarComp
class gym(Component):
def __init__(self):
super(gym, self).__init__()
self.add_param('eta', 0.01)
self.add_param('childWeight', 240)
self.add_output('acc', 1)
def solve_nonlinear(self, params, unknowns, resids):
<...... parameters are used to produce objective "acc" ...>
unknowns["acc"] = ....
top = Problem()
root = top.root = Group()
root.add('gym', gym())
top.driver = ScipyOptimizer()
top.driver.options['optimizer'] = 'BFGS'
root.add('eta',IndepVarComp('x', 0.01))
root.add('childWeight', IndepVarComp('x',100))
root.connect('eta.x', 'gym.eta')
root.connect('childWeight.x', 'gym.childWeight')
top.driver.add_desvar('eta.x', 0, 1.0)
top.driver.add_desvar('childWeight.x', 0, 1000)
top.driver.add_objective('gym.acc')
top.setup()
top.run()
引发错误
File "script.py", line 98, in <module>
top.setup()
File "/usr/local/lib/python2.7/site-packages/openmdao/core/problem.py", line 694, in setup
self.driver._setup()
File "/usr/local/lib/python2.7/site-packages/openmdao/drivers/scipy_optimizer.py", line 91, in _setup
super(ScipyOptimizer, self)._setup()
File "/usr/local/lib/python2.7/site-packages/openmdao/core/driver.py", line 115, in _setup
(item_name, name, oname))
RuntimeError: Parameter 'childWeight.x' is a 'pass_by_obj' variable and can't be used with a gradient based driver of type 'BFGS'.
问题出在这一行
root.add('childWeight', IndepVarComp('x',240))
您已经创建了一个整型变量。试试这个:
root.add('childWeight', IndepVarComp('x',240.))
如果你想使用有限差分,你还需要:
top.root.fd_options['force_fd'] = True
我认为出现此错误是因为 childWeight
的默认值是作为整数 240 而不是浮点数 240.0 提供的。如果您确保您的设计变量始终具有作为浮点数的默认值,则不应将它们归类为 pass_by_obj 变量。
我对这个 openmdao 错误感到困惑。为什么要提出这个问题?我能以某种方式告诉 openmdao 我没有梯度并使用有限差分吗?为什么这是为 childWeight 而不是 eta 提出的?
我可以通过将所有变量初始化为浮点数(例如 root.add('childWeight', IndepVarComp('x',100))
-> root.add('childWeight', IndepVarComp('x',100.0))
)来解决这个问题,但我想了解为什么会出现此错误。
from openmdao.api import Component, Group, Problem, ScipyOptimizer, IndepVarComp
class gym(Component):
def __init__(self):
super(gym, self).__init__()
self.add_param('eta', 0.01)
self.add_param('childWeight', 240)
self.add_output('acc', 1)
def solve_nonlinear(self, params, unknowns, resids):
<...... parameters are used to produce objective "acc" ...>
unknowns["acc"] = ....
top = Problem()
root = top.root = Group()
root.add('gym', gym())
top.driver = ScipyOptimizer()
top.driver.options['optimizer'] = 'BFGS'
root.add('eta',IndepVarComp('x', 0.01))
root.add('childWeight', IndepVarComp('x',100))
root.connect('eta.x', 'gym.eta')
root.connect('childWeight.x', 'gym.childWeight')
top.driver.add_desvar('eta.x', 0, 1.0)
top.driver.add_desvar('childWeight.x', 0, 1000)
top.driver.add_objective('gym.acc')
top.setup()
top.run()
引发错误
File "script.py", line 98, in <module>
top.setup()
File "/usr/local/lib/python2.7/site-packages/openmdao/core/problem.py", line 694, in setup
self.driver._setup()
File "/usr/local/lib/python2.7/site-packages/openmdao/drivers/scipy_optimizer.py", line 91, in _setup
super(ScipyOptimizer, self)._setup()
File "/usr/local/lib/python2.7/site-packages/openmdao/core/driver.py", line 115, in _setup
(item_name, name, oname))
RuntimeError: Parameter 'childWeight.x' is a 'pass_by_obj' variable and can't be used with a gradient based driver of type 'BFGS'.
问题出在这一行
root.add('childWeight', IndepVarComp('x',240))
您已经创建了一个整型变量。试试这个:
root.add('childWeight', IndepVarComp('x',240.))
如果你想使用有限差分,你还需要:
top.root.fd_options['force_fd'] = True
我认为出现此错误是因为 childWeight
的默认值是作为整数 240 而不是浮点数 240.0 提供的。如果您确保您的设计变量始终具有作为浮点数的默认值,则不应将它们归类为 pass_by_obj 变量。