Openmdao - AttributeError:'float'对象在Paraboloid教程中没有属性'size'

时间:2017-07-07 19:07:07

标签: python attributeerror openmdao

我正在使用第一个Paraboloid Tutorial学习OpenMDAO。但是,在我使用约束情况(add_constraint(...))运行代码时,我得到错误:AttributeError:'float'对象没有属性'size'。 我只是复制粘贴了教程中的代码,但我无法解决错误。这是代码:

from __future__ import print_function
from openmdao.api import IndepVarComp, Component, Problem, Group
from openmdao.api import ScipyOptimizer
from openmdao.api import ExecComp

class Paraboloid(Component):
    def __init__(self):
        super(Paraboloid, self).__init__()

        self.add_param('x', val=0.0)
        self.add_param('y', val=0.0)

        self.add_output('f_xy', shape=1)

    def solve_nonlinear(self, params, unknowns, resids):
        """f(x,y) = (x-3)^2 + xy + (y+4)^2 - 3
        """

        x = params['x']
        y = params['y']

        unknowns['f_xy'] = (x-3.0)**2 + x*y + (y+4.0)**2 - 3.0

    def linearize(self, params, unknowns, resids):
        """ Jacobian for our paraboloid."""

        x = params['x']
        y = params['y']
        J = {}

        J['f_xy', 'x'] = 2.0*x - 6.0 + y
        J['f_xy', 'y'] = 2.0*y + 8.0 + x
        return J

if __name__ == "__main__":

    top = Problem()

    root = top.root = Group()

    root.add('p1', IndepVarComp('x', 3.0))
    root.add('p2', IndepVarComp('y', -4.0))
    root.add('p', Paraboloid())

    # Constraint Equation
    root.add('con', ExecComp('c = x-y'))

    root.connect('p1.x', 'p.x')
    root.connect('p2.y', 'p.y')
    root.connect('p.x', 'con.x')
    root.connect('p.y', 'con.y')

    top.driver = ScipyOptimizer()
    top.driver.options['optimizer'] = 'SLSQP'

    top.driver.add_desvar('p1.x', lower=-50, upper=50)
    top.driver.add_desvar('p2.y', lower=-50, upper=50)
    top.driver.add_objective('p.f_xy')
    top.driver.add_constraint('con.c', lower=15.0)

    top.setup()
    top.run()

    print('\n')
    print('Minimum of %f found at (%f, %f)' % (top['p.f_xy'], top['p.x'], top['p.y']))

我运行脚本,我得到以下内容:

##############################################
Setup: Checking root problem for potential issues...

No recorders have been specified, so no data will be saved.

Setup: Check of root problem complete.
##############################################

Traceback (most recent call last):
  File "paraboloid.py", line 65, in <module>
    top.run()
  File "/home/sim/.local/lib/python2.7/site-packages/openmdao/core/problem.py", line 1151, in run
    self.driver.run(self)
  File "/home/sim/.local/lib/python2.7/site-packages/openmdao/drivers/scipy_optimizer.py", line 211, in run
    options=self.opt_settings)
  File "/home/sim/.local/lib/python2.7/site-packages/scipy/optimize/_minimize.py", line 458, in minimize
    constraints, callback=callback, **options)
  File "/home/sim/.local/lib/python2.7/site-packages/scipy/optimize/slsqp.py", line 390, in _minimize_slsqp
    g = append(fprime(x),0.0)
  File "/home/sim/.local/lib/python2.7/site-packages/scipy/optimize/optimize.py", line 292, in function_wrapper
    return function(*(wrapper_args + args))
  File "/home/sim/.local/lib/python2.7/site-packages/openmdao/drivers/scipy_optimizer.py", line 334, in _gradfunc
    return_format='array')
  File "/home/sim/.local/lib/python2.7/site-packages/openmdao/core/driver.py", line 834, in calc_gradient
    sparsity=sparsity, inactives=inactives)
  File "/home/sim/.local/lib/python2.7/site-packages/openmdao/core/problem.py", line 1310, in calc_gradient
    inactives=inactives)
  File "/home/sim/.local/lib/python2.7/site-packages/openmdao/core/problem.py", line 1561, in _calc_gradient_ln_solver
    root._sys_linearize(root.params, unknowns, root.resids)
  File "/home/sim/.local/lib/python2.7/site-packages/openmdao/core/system.py", line 947, in _sys_linearize
    self._jacobian_cache = linearize(params, unknowns, resids)
  File "/home/sim/.local/lib/python2.7/site-packages/openmdao/core/group.py", line 836, in linearize
    sub._sys_linearize(sub.params, sub.unknowns, sub.resids)
  File "/home/sim/.local/lib/python2.7/site-packages/openmdao/core/system.py", line 947, in _sys_linearize
    self._jacobian_cache = linearize(params, unknowns, resids)
  File "/home/sim/.local/lib/python2.7/site-packages/openmdao/components/exec_comp.py", line 273, in linearize
    J[(u, param)] = numpy.zeros((jval.size, psize))
AttributeError: 'float' object has no attribute 'size'

只有在我设置约束方程时才会出现错误消息。 我相信这是一个愚蠢的错误但是,有人能指出正确的方向吗? 谢谢你的帮助!

2 个答案:

答案 0 :(得分:7)

这实际上是由1.12和1.13之间的小块变化引起的错误。我们现在已经修复了github存储库的Master分支。

答案 1 :(得分:-1)

我认为您可能正在使用较旧版本的numpy,imag方法不会返回数组。你可以尝试升级到更高版本的numpy吗?我使用的是1.12.0,代码适用于我。