python


OpenMDAO/ScipyOptimizer “UnboundLocalError: local variable 'f_new' referenced before assignment”


Trying to run the paraboloid example from OpenMDAO documentation but using the ScipyOptimizer, and I get the error in the title. Not sure what I'm doing wrong. I've attached a MWE below.
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'])
I figured it out. In a classic moment of brain-fartiness, I forgot to add my design variables and objective functions to the driver. Something like this is what I was missing:
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')

Related Links

Using SL4a to make intent that adds new contact
Text file not being imported with module
Web.py - prevent caching of code/output
Reference counting while adding element to list
Python: Trouble with dill installation
python open file error
Dealing with Sprites and Collisions Using Pygame
Pip-installed module does not import
Does a trailing comma after an n-tuple in Python change its value?
Program that either waits for user input or runs at defined intervals?
pexpect for passwordless access does not work
Kivy Widget does not accepts properties and commands
Efficient combinations of combinations
what metric should I monitor for a spider in scrapy
Why is Aptana/Pydev showing “undefined variable” errors for “print” and “__name__” in my helloworld python code?
Python Tkinter: Delete label not working

Categories

HOME
sass
answer-set-programming
shopify
reverse-engineering
translation
json-ld
google-sheets-api
survival-analysis
vlc
orientation
yocto
tizen-wearable-sdk
shader
dropbox
metronic
window
hashicorp-vault
abcpdf
postmessage
cloudflare
scaling
bar-chart
pyyaml
off-canvas-menu
ng-show
primes
linq-to-sql
plupload
shippo
thingsboard
restfb
npm-install
cgal
google-api-dotnet-client
roundcube
encase
clockwork
outsystems
geo
rhomobile
npm-shrinkwrap
gettext
alchemy.js
cross-entropy
microdata
resuming-training
mod-fcgid
gist
tiddlywiki
slot
instruction-set
castle-dynamicproxy
sqlexception
tuxedo
instruments
freefem++
mpeg-4
harp
ytplayerview
findcontrol
odftoolkit
x-ua-compatible
formvalidation-plugin
power-law
pic24
opensocial
anjuta
openkinect
mimosa
installshield-2011
nimrod
vmware-server
apache2.2
prng
android-4.0
web-safe-fonts
qsqltablemodel
cadisplaylink
shared-objects
datamember
yajl
savestate
web-statistics
configurable
web-analytics-tools

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App