简体   繁体   English

Openmdao-AttributeError:“抛物线”对象中的“ float”对象没有属性“ size”

[英]Openmdao - AttributeError: 'float' object has no attribute 'size' in Paraboloid Tutorial

I am learning OpenMDAO with the first Paraboloid Tutorial . 我正在通过第一个抛物面教程学习OpenMDAO。 However, where I run the code with the constrained case (add_constraint(...)) I get the error: AttributeError: 'float' object has no attribute 'size'. 但是,在受限情况下运行代码(add_constraint(...))时,出现错误:AttributeError:'float'对象没有属性'size'。 I just copied-pasted the code from the tutorial but I can not fine the error. 我只是复制并粘贴了本教程中的代码,但无法解决该错误。 Here is the code: 这是代码:

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']))

I run the script and I get the following: 我运行脚本,然后得到以下信息:

##############################################
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'

The error message appear only when I put the constraint equation. 仅当我放入约束方程式时,才会出现错误消息。 I believe it is a silly error but, can anyone point me the right direction? 我认为这是一个愚蠢的错误,但是,谁能为我指出正确的方向? Thank you in advance of your help ! 预先感谢您的帮助!

This was actually a bug caused by a small change in numpy between 1.12 and 1.13. 实际上,这是一个错误,它是由于numpy在1.12和1.13之间的微小变化引起的。 We have a fix up on the Master branch of the github repository now. 现在,我们已经修复了github存储库的Master分支。

I think you may be using an older version of numpy, under which the imag method doesn't return an array. 我认为您可能使用的是numpy的旧版本,在此版本下, imag方法不会返回数组。 Can you try upgrading to a later version of numpy? 您可以尝试升级到更高版本的numpy吗? I am using 1.12.0 and the code works for me. 我正在使用1.12.0,并且代码适合我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM