0

我正在运行教程中的“Sellar exmaple”代码。根据教程页面上给出的文档,这ExecComp只是声明一个 normal 的简写Component。所以我尝试将示例中的 s 重新定义ExecComp为普通Components 并在同一个示例中使用它们。

示例中的ExecComps 定义如下 -

self.add('obj_cmp', ExecComp('obj = x**2 + z[1] + y1 + exp(-y2)',
                             z=np.array([0.0, 0.0]), x=0.0, y1=0.0, y2=0.0),
                             promotes=['*'])
self.add('con_cmp1', ExecComp('con1 = 3.16 - y1'), promotes=['*'])
self.add('con_cmp2', ExecComp('con2 = y2 - 24.0'), promotes=['*'])

我定义的正常Components如下-

目标组件

class SellarObjective(Component):
    def __init__(self):
        super(SellarObjective, self).__init__()    
        self.add_param('x', val=0.0)
        self.add_param('y2', val=0.0)
        self.add_param('y1', val=0.0)
        self.add_param('z', val=np.zeros(2))    
        self.add_output('obj', val=0.0)

    def solve_nonlinear(self, params, unknowns, resids):
        unknowns['obj'] = params['x']**2 + params['z'][0] + params['y1'] + exp(-params['y2'])

    def linearize(self, params, unknowns, resids):
        J = {}
        J['obj', 'x'] = 2 * params['x']
        J['obj', 'y2'] = (-1) * exp(-params['y2'])
        J['obj', 'y1'] = 1.0
        J['obj', 'z[0]'] = 1.0
        return J

约束 1

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

        self.add_param('y1', val=0.0)
        self.add_output('con1', val=0.0)

    def solve_nonlinear(self, params, unknowns, resids):
        unknowns['con1'] = 3.16 - params['y1']

    def linearize(self, params, unknowns, resids):
        J = {}
        J['con1', 'y1'] = -1.0
        return J

约束 2

class SellarConstraint2(Component):
    def __init__(self):
        super(SellarConstraint2, self).__init__()
        self.add_param('y2', val=0.0)
        self.add_output('con2', val=0.0)

    def solve_nonlinear(self, params, unknowns, resids):
        unknowns['con2'] = params['y2'] - 24.0

    def linearize(self, params, unknowns, resids):
        J = {}
        J['con2', 'y2'] = 1.0
        return J

Component在重写的实现中将这些新声明的 s 实例化为 -

self.add('obj_cmp', SellarObjective(), promotes=['*'])
self.add('con_cmp1', SellarConstraint1(), promotes=['*'])
self.add('con_cmp2', SellarConstraint2(), promotes=['*'])

代码中的其他所有内容都与教程中的相同。但是在执行它们之后,当我比较结果时 - 结果不匹配。

我在这里遗漏了一些明显的东西吗?感谢您的时间。

4

1 回答 1

1

您的替换目标类有两个小问题:

  1. 目标是 的函数z[1],不z[0]
  2. 目标对 z 的导数是一个数组,不能z[1]用作键。您必须z改用。

将您的目标补偿更正为以下内容,它应该可以工作:

class SellarObjective(Component):
    def __init__(self):
        super(SellarObjective, self).__init__()    
        self.add_param('x', val=0.0)
        self.add_param('y2', val=0.0)
        self.add_param('y1', val=0.0)
        self.add_param('z', val=np.zeros(2))    
        self.add_output('obj', val=0.0)

    def solve_nonlinear(self, params, unknowns, resids):
        unknowns['obj'] = params['x']**2 + params['z'][1] + params['y1'] + np.exp(-params['y2'])

    def linearize(self, params, unknowns, resids):
        J = {}
        J['obj', 'x'] = 2 * params['x']
        J['obj', 'y2'] = (-1) * np.exp(-params['y2'])
        J['obj', 'y1'] = 1.0
        J['obj', 'z'] = np.array([[0,1],])
        return J
于 2016-02-26T03:57:29.740 回答