使用元模型进行openmdao v1.4优化

时间:2016-01-06 17:26:52

标签: openmdao

我在元模型上使用openmdao 1.4执行优化。使用教程我已经建立了一些我无法解决的问题:我认为问题来自于滥用setup()和run():我无法训练我的元模型并在同一时间对其进行优化时间(perhpas我应该使用两个不同的“组”来做这个..) 这是我的代码:

    from __future__ import print_function


from openmdao.api import Component, Group, MetaModel ,IndepVarComp, ExecComp, NLGaussSeidel, KrigingSurrogate, FloatKrigingSurrogate

import numpy as np


class KrigMM(Group):
    ''' FloatKriging gives responses as floats '''

    def __init__(self):
        super(KrigMM, self).__init__()

        # Create meta_model for f_x as the response

        pmm = self.add("pmm", MetaModel())
        pmm.add_param('x', val=0.)

        pmm.add_output('f_x:float', val=0., surrogate=FloatKrigingSurrogate())
        self.add('p1', IndepVarComp('x', 0.0))

        self.connect('p1.x','pmm.x')

       # mm.add_output('f_xy:norm_dist', val=(0.,0.), surrogate=KrigingSurrogate())


if __name__ == '__main__':
    # Setup and run the model.

    from openmdao.core.problem import Problem
    from openmdao.drivers.scipy_optimizer import ScipyOptimizer
    from openmdao.core.driver import Driver

    import numpy as np
    import doe_lhs

    #prob = Problem(root=ParaboloidProblem())
###########################################################    

    prob = Problem(root=Group())
    prob.root.add('meta',KrigMM(), promotes=['*'])

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

    prob.driver.add_desvar('p1.x', lower=0, upper=10)

    prob.driver.add_objective('pmm.f_x:float')
    prob.setup()
    prob['pmm.train:x'] = np.linspace(0,10,20)
    prob['pmm.train:f_x:float'] = np.sin(prob['pmm.train:x'])      
    prob.run()

    print('\n')
    print('Minimum of %f found for meta at %f' % (prob['pmm.f_x:float'],prob['pmm.x'])) #predicted value 

1 个答案:

答案 0 :(得分:1)

我相信你的问题确实很好。它只是你选择的窦房有0.0的局部最佳值,这恰好是你的初始条件。

如果我按如下方式更改初始条件:

if(!matcher.group(5).isEmpty()) {
  int age = matcher.group(6);
}

我明白了:

prob.setup()
prob['p1.x'] = 5
prob['pmm.train:x'] = np.linspace(0,10,20)
prob['pmm.train:f_x:float'] = np.sin(prob['pmm.train:x'])      
prob.run()