2017-05-15 5 views
0

Ich versuche, das Paraboloid-Beispiel aus der OpenMDAO-Dokumentation auszuführen, aber den ScipyOptimizer zu verwenden, und ich bekomme den Fehler im Titel. Ich bin mir nicht sicher, was ich falsch mache. Ich habe unten ein MWE angehängt.OpenMDAO/ScipyOptimizer "UnboundLocalError: lokale Variable 'f_new' referenziert vor Zuweisung"

OpenMDAO 1.7.3, Python 3.6.0.


# -*- coding: utf-8 -*- 
 
""" 
 
Paraboloid tutorial from OpenMDAO documentation: 
 
https://openmdao.readthedocs.io/en/latest/usr-guide/tutorials/paraboloid-tutorial.html 
 

 
""" 
 

 
# import print function from Python 3 if we are using Python 2 
 
from __future__ import print_function 
 

 
# import all the components we need from openmdao module 
 
from openmdao.api import IndepVarComp, Component, Problem, Group, ScipyOptimizer 
 

 
class Paraboloid(Component): 
 
    """ Evaluates the equation f(x,y) = (x-3)^2 + xy + (y+4)^2 - 3 """ 
 

 
    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 
 

 
# This if statement executes only if the script is run as a script, not if the 
 
# script is imported as a module. It's not necessary for this demo, but it 
 
# is good coding practice. 
 
if __name__ == "__main__": 
 

 
    # initiallize the overall problem 
 
    top = Problem() 
 

 
    # each problem has a root "group" that contains the component evaluating 
 
    # the objective function 
 
    root = top.root = Group() 
 

 
    # define components with the independent variables and their initial guesses 
 
    root.add('p1', IndepVarComp('x', 3.0)) 
 
    root.add('p2', IndepVarComp('y', -4.0)) 
 
    
 
    # add the component that defines our objective function 
 
    root.add('p', Paraboloid()) 
 

 
    # connect the components together 
 
    root.connect('p1.x', 'p.x') 
 
    root.connect('p2.y', 'p.y') 
 
    
 
    # specify our driver for the problem (optional) 
 
    top.driver = ScipyOptimizer() 
 
    top.driver.options['optimizer'] = 'SLSQP' 
 
    top.driver.options['disp'] = True # don't display optimizer output 
 

 
    # set up the problem 
 
    top.setup() 
 
    
 
    # run the optimization 
 
    top.run() 
 

 
    print(top['p.f_xy'])

Antwort

0

ich es herausgefunden. In einem klassischen Moment der Hirnfheit habe ich vergessen, dem Fahrer meine Designvariablen und Zielfunktionen hinzuzufügen. So etwas habe ich vermisst:

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') 
Verwandte Themen