<s> import unittest <EOL> from nose import SkipTest <EOL> try : <EOL> from pyopt_driver . pyopt_driver import pyOptDriver <EOL> except ImportError : <EOL> pass <EOL> from openmdao . util . testutil import assert_rel_error <EOL> from openmdao . main . api import Assembly , set_as_top , Component , Driver <EOL> from openmdao . main . datatypes . api import Array , Float , Int <EOL> from openmdao . main . interfaces import IHasParameters , implements <EOL> from openmdao . main . hasparameters import HasParameters <EOL> from openmdao . util . decorators import add_delegate <EOL> from openmdao . examples . simple . paraboloid import Paraboloid <EOL> from openmdao . examples . simple . paraboloid_derivative import ParaboloidDerivative <EOL> class OptimizationUnconstrained ( Assembly ) : <EOL> """<STR_LIT>""" <EOL> def configure ( self ) : <EOL> """<STR_LIT>""" <EOL> self . add ( '<STR_LIT>' , Paraboloid ( ) ) <EOL> self . add ( '<STR_LIT>' , pyOptDriver ( ) ) <EOL> self . driver . workflow . add ( '<STR_LIT>' ) <EOL> self . driver . add_objective ( '<STR_LIT>' ) <EOL> self . driver . add_parameter ( '<STR_LIT>' , low = - <NUM_LIT> , high = <NUM_LIT> ) <EOL> self . driver . add_parameter ( '<STR_LIT>' , low = - <NUM_LIT> , high = <NUM_LIT> ) <EOL> self . driver . print_results = False <EOL> class OptimizationConstrained ( Assembly ) : <EOL> """<STR_LIT>""" <EOL> def configure ( self ) : <EOL> """<STR_LIT>""" <EOL> self . add ( '<STR_LIT>' , Paraboloid ( ) ) <EOL> self . add ( '<STR_LIT>' , pyOptDriver ( ) ) <EOL> self . driver . workflow . add ( '<STR_LIT>' ) <EOL> self . driver . add_objective ( '<STR_LIT>' ) <EOL> self . driver . add_parameter ( '<STR_LIT>' , low = - <NUM_LIT> , high = <NUM_LIT> ) <EOL> self . driver . add_parameter ( '<STR_LIT>' , low = - <NUM_LIT> , high = <NUM_LIT> ) <EOL> self . driver . add_constraint ( '<STR_LIT>' ) <EOL> self . driver . print_results = False <EOL> class ArrayParaboloid ( Component ) : <EOL> """<STR_LIT>""" <EOL> x = Array ( [ <NUM_LIT:0.> , <NUM_LIT:0.> ] , iotype = '<STR_LIT>' , desc = '<STR_LIT>' ) <EOL> f_xy = Float ( iotype = '<STR_LIT>' , desc = '<STR_LIT>' ) <EOL> def execute ( self ) : <EOL> """<STR_LIT>""" <EOL> x = self . x <EOL> self . f_xy = ( x [ <NUM_LIT:0> ] - <NUM_LIT> ) ** <NUM_LIT:2> + x [ <NUM_LIT:0> ] * x [ <NUM_LIT:1> ] + ( x [ <NUM_LIT:1> ] + <NUM_LIT> ) ** <NUM_LIT:2> - <NUM_LIT> <EOL> class ArrayOpt ( Assembly ) : <EOL> """<STR_LIT>""" <EOL> def configure ( self ) : <EOL> """<STR_LIT>""" <EOL> self . add ( '<STR_LIT>' , ArrayParaboloid ( ) ) <EOL> self . add ( '<STR_LIT>' , pyOptDriver ( ) ) <EOL> self . driver . pyopt_diff = True <EOL> self . driver . workflow . add ( '<STR_LIT>' ) <EOL> self . driver . add_objective ( '<STR_LIT>' ) <EOL> self . driver . add_parameter ( '<STR_LIT>' , low = - <NUM_LIT> , high = <NUM_LIT> ) <EOL> self . driver . add_constraint ( '<STR_LIT>' ) <EOL> self . driver . print_results = False <EOL> class OptimizationConstrainedDerivatives ( Assembly ) : <EOL> """<STR_LIT>""" <EOL> def configure ( self ) : <EOL> """<STR_LIT>""" <EOL> self . add ( '<STR_LIT>' , ParaboloidDerivative ( ) ) <EOL> self . add ( '<STR_LIT>' , pyOptDriver ( ) ) <EOL> self . driver . workflow . add ( '<STR_LIT>' ) <EOL> self . driver . add_objective ( '<STR_LIT>' ) <EOL> self . driver . add_parameter ( '<STR_LIT>' , low = - <NUM_LIT> , high = <NUM_LIT> ) <EOL> self . driver . add_parameter ( '<STR_LIT>' , low = - <NUM_LIT> , high = <NUM_LIT> ) <EOL> self . driver . add_constraint ( '<STR_LIT>' ) <EOL> self . driver . print_results = False <EOL> class MultiFunction ( Component ) : <EOL> x1 = Float ( <NUM_LIT:1.0> , iotype = '<STR_LIT>' , desc = '<STR_LIT>' ) <EOL> x2 = Float ( <NUM_LIT:1.0> , iotype = '<STR_LIT>' , desc = '<STR_LIT>' ) <EOL> f1_x = Float ( iotype = '<STR_LIT>' , desc = '<STR_LIT>' ) <EOL> f2_x = Float ( iotype = '<STR_LIT>' , desc = '<STR_LIT>' ) <EOL> g1_x = Float ( iotype = '<STR_LIT>' , desc = '<STR_LIT>' ) <EOL> g2_x = Float ( iotype = '<STR_LIT>' , desc = '<STR_LIT>' ) <EOL> def execute ( self ) : <EOL> x1 = self . x1 <EOL> x2 = self . x2 <EOL> self . f1_x = x1 <EOL> self . f2_x = ( <NUM_LIT:1> + x2 ) / x1 <EOL> self . g1_x = x2 + <NUM_LIT> * x1 <EOL> self . g2_x = - x2 + <NUM_LIT> * x1 <EOL> class MultiObjectiveOptimization ( Assembly ) : <EOL> """<STR_LIT>""" <EOL> def configure ( self ) : <EOL> """<STR_LIT>""" <EOL> self . add ( '<STR_LIT>' , MultiFunction ( ) ) <EOL> self . add ( '<STR_LIT>' , pyOptDriver ( ) ) <EOL> self . driver . workflow . add ( '<STR_LIT>' ) <EOL> self . driver . print_results = False <EOL> self . driver . add_objective ( '<STR_LIT>' ) <EOL> self . driver . add_objective ( '<STR_LIT>' ) <EOL> self . driver . add_parameter ( '<STR_LIT>' , low = <NUM_LIT:0.1> , high = <NUM_LIT:1.0> ) <EOL> self . driver . add_parameter ( '<STR_LIT>' , low = <NUM_LIT:0.0> , high = <NUM_LIT> ) <EOL> self . driver . add_constraint ( '<STR_LIT>' ) <EOL> self . driver . add_constraint ( '<STR_LIT>' ) <EOL> class BenchMark ( Component ) : <EOL> x1 = Int ( <NUM_LIT:10> , iotype = '<STR_LIT>' , desc = '<STR_LIT>' ) <EOL> x2 = Int ( <NUM_LIT:10> , iotype = '<STR_LIT>' , desc = '<STR_LIT>' ) <EOL> x3 = Int ( <NUM_LIT:10> , iotype = '<STR_LIT>' , desc = '<STR_LIT>' ) <EOL> f_x = Float ( iotype = '<STR_LIT>' , desc = '<STR_LIT>' ) <EOL> g1_x = Float ( iotype = '<STR_LIT>' , desc = '<STR_LIT>' ) <EOL> h1_x = Float ( iotype = '<STR_LIT>' , desc = '<STR_LIT>' ) <EOL> def execute ( self ) : <EOL> x1 = self . x1 <EOL> x2 = self . x2 <EOL> x3 = self . x3 <EOL> self . f_x = - x1 * x2 * x3 <EOL> self . g1_x = x1 + <NUM_LIT> * x2 + <NUM_LIT> * x3 - <NUM_LIT> <EOL> self . h1_x = - x1 - <NUM_LIT> * x2 - <NUM_LIT> * x3 <EOL> class BenchMarkOptimization ( Assembly ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> """<STR_LIT>""" <EOL> super ( BenchMarkOptimization , self ) . __init__ ( ) <EOL> self . add ( '<STR_LIT>' , BenchMark ( ) ) <EOL> self . add ( '<STR_LIT>' , pyOptDriver ( ) ) <EOL> self . driver . workflow . add ( '<STR_LIT>' ) <EOL> self . driver . optimizer = '<STR_LIT>' <EOL> self . driver . title = '<STR_LIT>' <EOL> optdict = { } <EOL> optdict [ '<STR_LIT>' ] = <NUM_LIT> <EOL> optdict [ '<STR_LIT>' ] = <NUM_LIT:100> <EOL> optdict [ '<STR_LIT>' ] = <NUM_LIT:3> <EOL> optdict [ '<STR_LIT>' ] = <NUM_LIT:3> <EOL> optdict [ '<STR_LIT>' ] = <NUM_LIT> <EOL> optdict [ '<STR_LIT>' ] = <NUM_LIT> <EOL> optdict [ '<STR_LIT>' ] = <NUM_LIT> <EOL> optdict [ '<STR_LIT>' ] = <NUM_LIT> <EOL> optdict [ '<STR_LIT>' ] = <NUM_LIT> <EOL> optdict [ '<STR_LIT>' ] = <NUM_LIT:5> <EOL> optdict [ '<STR_LIT>' ] = <NUM_LIT:1.0> <EOL> optdict [ '<STR_LIT>' ] = <NUM_LIT> <EOL> optdict [ '<STR_LIT>' ] = <NUM_LIT:1> <EOL> optdict [ '<STR_LIT>' ] = <NUM_LIT:1.0> <EOL> self . driver . options = optdict <EOL> self . driver . add_objective ( '<STR_LIT>' ) <EOL> self . driver . add_parameter ( '<STR_LIT>' , low = <NUM_LIT:0> , high = <NUM_LIT> ) <EOL> self . driver . add_parameter ( '<STR_LIT>' , low = <NUM_LIT:0> , high = <NUM_LIT> ) <EOL> self . driver . add_parameter ( '<STR_LIT>' , low = <NUM_LIT:0> , high = <NUM_LIT> ) <EOL> self . driver . add_constraint ( '<STR_LIT>' ) <EOL> self . driver . add_constraint ( '<STR_LIT>' ) <EOL> class pyOptDriverTestCase ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> pass <EOL> def tearDown ( self ) : <EOL> self . top = None <EOL> def test_unconstrained ( self ) : <EOL> try : <EOL> from pyopt_driver . pyopt_driver import pyOptDriver <EOL> except ImportError : <EOL> raise SkipTest ( "<STR_LIT>" ) <EOL> self . top = OptimizationUnconstrained ( ) <EOL> set_as_top ( self . top ) <EOL> for optimizer in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> try : <EOL> self . top . driver . optimizer = optimizer <EOL> except ValueError : <EOL> raise SkipTest ( "<STR_LIT>" % optimizer ) <EOL> self . top . driver . title = '<STR_LIT>' <EOL> optdict = { } <EOL> self . top . driver . options = optdict <EOL> self . top . driver . pyopt_diff = True <EOL> self . top . run ( ) <EOL> assert_rel_error ( self , self . top . paraboloid . x , <NUM_LIT> , <NUM_LIT> ) <EOL> assert_rel_error ( self , self . top . paraboloid . y , - <NUM_LIT> , <NUM_LIT> ) <EOL> def test_basic_CONMIN ( self ) : <EOL> try : <EOL> from pyopt_driver . pyopt_driver import pyOptDriver <EOL> except ImportError : <EOL> raise SkipTest ( "<STR_LIT>" ) <EOL> self . top = OptimizationConstrained ( ) <EOL> set_as_top ( self . top ) <EOL> try : <EOL> self . top . driver . optimizer = '<STR_LIT>' <EOL> self . top . driver . optimizer = '<STR_LIT>' <EOL> except ValueError : <EOL> raise SkipTest ( "<STR_LIT>" ) <EOL> self . top . driver . title = '<STR_LIT>' <EOL> optdict = { } <EOL> self . top . driver . options = optdict <EOL> self . top . driver . pyopt_diff = True <EOL> self . top . run ( ) <EOL> assert_rel_error ( self , self . top . paraboloid . x , <NUM_LIT> , <NUM_LIT> ) <EOL> assert_rel_error ( self , self . top . paraboloid . y , - <NUM_LIT> , <NUM_LIT> ) <EOL> def test_array_CONMIN ( self ) : <EOL> try : <EOL> from pyopt_driver . pyopt_driver import pyOptDriver <EOL> except ImportError : <EOL> raise SkipTest ( "<STR_LIT>" ) <EOL> self . top = ArrayOpt ( ) <EOL> set_as_top ( self . top ) <EOL> try : <EOL> self . top . driver . optimizer = '<STR_LIT>' <EOL> except ValueError : <EOL> raise SkipTest ( "<STR_LIT>" ) <EOL> self . top . driver . title = '<STR_LIT>' <EOL> optdict = { } <EOL> self . top . driver . options = optdict <EOL> self . top . run ( ) <EOL> assert_rel_error ( self , self . top . paraboloid . x [ <NUM_LIT:0> ] , <NUM_LIT> , <NUM_LIT> ) <EOL> assert_rel_error ( self , self . top . paraboloid . x [ <NUM_LIT:1> ] , - <NUM_LIT> , <NUM_LIT> ) <EOL> def test_basic_CONMIN_derivatives ( self ) : <EOL> try : <EOL> from pyopt_driver . pyopt_driver import pyOptDriver <EOL> except ImportError : <EOL> raise SkipTest ( "<STR_LIT>" ) <EOL> self . top = OptimizationConstrainedDerivatives ( ) <EOL> set_as_top ( self . top ) <EOL> try : <EOL> self . top . driver . optimizer = '<STR_LIT>' <EOL> except ValueError : <EOL> raise SkipTest ( "<STR_LIT>" ) <EOL> self . top . driver . title = '<STR_LIT>' <EOL> optdict = { } <EOL> self . top . driver . options = optdict <EOL> self . top . run ( ) <EOL> assert_rel_error ( self , self . top . paraboloid . x , <NUM_LIT> , <NUM_LIT> ) <EOL> assert_rel_error ( self , self . top . paraboloid . y , - <NUM_LIT> , <NUM_LIT> ) <EOL> def test_GA_multi_obj_multi_con ( self ) : <EOL> try : <EOL> from pyopt_driver . pyopt_driver import pyOptDriver <EOL> except ImportError : <EOL> raise SkipTest ( "<STR_LIT>" ) <EOL> self . top = MultiObjectiveOptimization ( ) <EOL> set_as_top ( self . top ) <EOL> try : <EOL> self . top . driver . optimizer = '<STR_LIT>' <EOL> except ValueError : <EOL> raise SkipTest ( "<STR_LIT>" ) <EOL> self . top . driver . title = '<STR_LIT>' <EOL> optdict = { } <EOL> optdict [ '<STR_LIT>' ] = <NUM_LIT:100> <EOL> optdict [ '<STR_LIT>' ] = <NUM_LIT:5> <EOL> optdict [ '<STR_LIT>' ] = <NUM_LIT> <EOL> optdict [ '<STR_LIT>' ] = <NUM_LIT:0.5> <EOL> optdict [ '<STR_LIT>' ] = <NUM_LIT> <EOL> optdict [ '<STR_LIT>' ] = <NUM_LIT> <EOL> optdict [ '<STR_LIT>' ] = <NUM_LIT:1.0> <EOL> optdict [ '<STR_LIT>' ] = <NUM_LIT:1.0> <EOL> optdict [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> optdict [ '<STR_LIT>' ] = <NUM_LIT:0.0> <EOL> self . top . driver . options = optdict <EOL> self . top . run ( ) <EOL> def test_ALPSO_integer_design_var ( self ) : <EOL> try : <EOL> from pyopt_driver . pyopt_driver import pyOptDriver <EOL> except ImportError : <EOL> raise SkipTest ( "<STR_LIT>" ) <EOL> opt_problem = BenchMarkOptimization ( ) <EOL> set_as_top ( opt_problem ) <EOL> opt_problem . run ( ) <EOL> self . assertEqual ( opt_problem . benchmark . x1 , <NUM_LIT> ) <EOL> self . assertEqual ( opt_problem . benchmark . x2 , <NUM_LIT:12> ) <EOL> self . assertEqual ( opt_problem . benchmark . x3 , <NUM_LIT:12> ) <EOL> def test_initial_run ( self ) : <EOL> class MyComp ( Component ) : <EOL> x = Float ( <NUM_LIT:0.0> , iotype = '<STR_LIT>' , low = - <NUM_LIT:10> , high = <NUM_LIT:10> ) <EOL> xx = Float ( <NUM_LIT:0.0> , iotype = '<STR_LIT>' , low = - <NUM_LIT:10> , high = <NUM_LIT:10> ) <EOL> f_x = Float ( iotype = '<STR_LIT>' ) <EOL> y = Float ( iotype = '<STR_LIT>' ) <EOL> def execute ( self ) : <EOL> if self . xx != <NUM_LIT:1.0> : <EOL> self . raise_exception ( "<STR_LIT>" , RuntimeError ) <EOL> self . f_x = <NUM_LIT> * self . x <EOL> self . y = self . x <EOL> @ add_delegate ( HasParameters ) <EOL> class SpecialDriver ( Driver ) : <EOL> implements ( IHasParameters ) <EOL> def execute ( self ) : <EOL> self . set_parameters ( [ <NUM_LIT:1.0> ] ) <EOL> top = set_as_top ( Assembly ( ) ) <EOL> top . add ( '<STR_LIT>' , MyComp ( ) ) <EOL> try : <EOL> from pyopt_driver . pyopt_driver import pyOptDriver <EOL> except ImportError : <EOL> raise SkipTest ( "<STR_LIT>" ) <EOL> try : <EOL> top . driver . optimizer = '<STR_LIT>' <EOL> except ValueError : <EOL> raise SkipTest ( "<STR_LIT>" ) <EOL> top . driver . title = '<STR_LIT>' <EOL> optdict = { } <EOL> top . driver . options = optdict <EOL> top . driver . pyopt_diff = True <EOL> top . add ( '<STR_LIT>' , pyOptDriver ( ) ) <EOL> top . add ( '<STR_LIT>' , SpecialDriver ( ) ) <EOL> top . driver . workflow . add ( '<STR_LIT>' ) <EOL> top . subdriver . workflow . add ( '<STR_LIT>' ) <EOL> top . subdriver . add_parameter ( '<STR_LIT>' ) <EOL> top . driver . add_parameter ( '<STR_LIT>' ) <EOL> top . driver . add_constraint ( '<STR_LIT>' ) <EOL> top . driver . add_objective ( '<STR_LIT>' ) <EOL> top . run ( ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> unittest . main ( ) </s>
