From d2ab9523197c58d6481d99a403ef75f7fce4b70e Mon Sep 17 00:00:00 2001 From: "Mads M. Pedersen" <mmpe@dtu.dk> Date: Fri, 15 Jun 2018 10:20:53 +0200 Subject: [PATCH] fixed error with missing EasyPyOptSparseIPOPT --- tests/topfarm/test_drivers.py | 54 +++++--------------- topfarm/easy_drivers.py | 93 ++++++++++++++++++----------------- 2 files changed, 62 insertions(+), 85 deletions(-) diff --git a/tests/topfarm/test_drivers.py b/tests/topfarm/test_drivers.py index 8708d630..38fdc0f3 100644 --- a/tests/topfarm/test_drivers.py +++ b/tests/topfarm/test_drivers.py @@ -9,7 +9,7 @@ import numpy as np import pytest from topfarm.cost_models.dummy import DummyCost, DummyCostPlotComp from topfarm.plotting import NoPlot -from topfarm.easy_drivers import EasyScipyOptimizeDriver, EasySimpleGADriver,\ +from topfarm.easy_drivers import EasyScipyOptimizeDriver,\ EasyPyOptSparseSLSQP, EasyPyOptSparseIPOPT @@ -28,53 +28,25 @@ def topfarm_generator(): return _topfarm_obj - - -# # CONMIN-specific Settings -# self.driver.itmax = 30 -# self.driver.fdch = 0.00001 -# self.driver.fdchm = 0.000001 -# self.driver.ctlmin = 0.01 -# self.driver.delfun = 0.001 -# -# # NEWSUMT-specific Settings -# #self.driver.itmax = 10 -# -# # COBYLA-specific Settings -# #self.driver.rhobeg = 1.0 -# #self.driver.rhoend = 1.0e-4 -# #self.driver.maxfun = 1000 -# -# # SLSQP-specific Settings -# #self.driver.accuracy = 1.0e-6 -# #self.driver.maxiter = 50 -# -# # Genetic-specific Settings -# #self.driver.population_size = 90 -# #self.driver.crossover_rate = 0.9 -# #self.driver.mutation_rate = 0.02 -# #self.selection_method = 'rank' - - -@pytest.mark.parametrize('driver,tol',[(EasyScipyOptimizeDriver(), 1e-4), - #(EasyScipyOptimizeDriver(tol=1e-3), 1e-2), - #(EasyScipyOptimizeDriver(maxiter=13), 1e-1), - (EasyScipyOptimizeDriver(optimizer='COBYLA', tol=1e-3), 1e-2), - #(EasyPyOptSparseSLSQP(),1e-4), - (EasyPyOptSparseIPOPT(),1e-4), - #(EasySimpleGADriver(), 1e-4) - ][:]) +@pytest.mark.parametrize('driver,tol', [(EasyScipyOptimizeDriver(), 1e-4), + (EasyScipyOptimizeDriver(tol=1e-3), 1e-2), + (EasyScipyOptimizeDriver(maxiter=13), 1e-1), + (EasyScipyOptimizeDriver(optimizer='COBYLA', tol=1e-3), 1e-2), + (EasyPyOptSparseSLSQP(), 1e-4), + (EasyPyOptSparseIPOPT(), 1e-4), + #(EasySimpleGADriver(), 1e-4) + ][:]) def test_optimizers(driver, tol, topfarm_generator): - if isinstance(driver, str): + if driver.__class__.__name__=="PyOptSparseMissingDriver": pytest.xfail("reason") tf = topfarm_generator(driver) tf.evaluate() - print (driver.__class__.__name__) + print(driver.__class__.__name__) tf.optimize() tb_pos = tf.turbine_positions - #tf.plot_comp.show() + # tf.plot_comp.show() assert sum((tb_pos[2] - tb_pos[0])**2) > 2**2 - tol # check min spacing - assert tb_pos[1][0]< 6 + tol # check within border + assert tb_pos[1][0] < 6 + tol # check within border np.testing.assert_array_almost_equal(tb_pos, optimal, -int(np.log10(tol))) #print (tb_pos - optimal) diff --git a/topfarm/easy_drivers.py b/topfarm/easy_drivers.py index 60d5756a..4d61f43a 100644 --- a/topfarm/easy_drivers.py +++ b/topfarm/easy_drivers.py @@ -1,18 +1,14 @@ from openmdao.drivers.scipy_optimizer import ScipyOptimizeDriver -from openmdao.drivers.genetic_algorithm_driver import SimpleGADriver class EasyScipyOptimizeDriver(ScipyOptimizeDriver): def __init__(self, optimizer='SLSQP', maxiter=200, tol=1e-6, disp=True): - """ + """ Parameters ---------- - optimizer : {'Nelder-Mead', 'Powell', 'CG', 'BFGS', 'Newton-CG', 'L-BFGS-B', - 'TNC', 'COBYLA', 'SLSQP'} - Inequality constraints are supported by COBYLA and SLSQP, - but equality constraints are only supported by SLSQP. None of the other - optimizers support constraints. + optimizer : {'COBYLA', 'SLSQP'} + Gradients are only supported by SLSQP maxiter : int Maximum number of iterations. tol : float @@ -26,51 +22,60 @@ class EasyScipyOptimizeDriver(ScipyOptimizeDriver): try: from openmdao.drivers.pyoptsparse_driver import pyOptSparseDriver - class EasyPyOptSparseSLSQP(pyOptSparseDriver): - def __init__(self, maxit=200, acc=1e-6): - pyOptSparseDriver.__init__(self) - self.options.update({'optimizer': 'SLSQP'}) - #self.opt_settings.update({'maxit': maxit, 'acc': acc}) - + +# Not working: +# capi_return is NULL +# Call-back cb_slfunc_in_slsqp__user__routines failed. +# +# class EasyPyOptSparseSLSQP(pyOptSparseDriver): +# def __init__(self, maxit=200, acc=1e-6): +# pyOptSparseDriver.__init__(self) +# self.options.update({'optimizer': 'SLSQP'}) +# self.opt_settings.update({'MAXIT': maxit, 'ACC': acc}) + class EasyPyOptSparseIPOPT(pyOptSparseDriver): def __init__(self, max_iter=200): pyOptSparseDriver.__init__(self) self.options.update({'optimizer': 'IPOPT'}) self.opt_settings.update({'linear_solver': 'ma27', 'max_iter': max_iter}) - - -except: - EasyPyOptSparseSLSQP = "Importing PyOptSparse failed" -class EasySimpleGADriver(SimpleGADriver): - def __init__(self, elitism=True, max_gen=100): - """Simple Genetic Algorithm Driver with argument +except ModuleNotFoundError: + class PyOptSparseMissingDriver(object): + options = {} - Parameters - ---------- - bits : dict - Number of bits of resolution. Default is an empty dict, where every unspecified variable is assumed to be integer, and the number of bits is calculated automatically. If you have a continuous var, you should set a bits value as a key in this dictionary. - NotImplemented - debug_print : list - List of what type of Driver variables to print at each iteration. Valid items in list are 'desvars', 'ln_cons', 'nl_cons', 'objs', 'totals' - NotImplemented - elitism : bool - If True, replace worst performing point with best from previous generation each iteration. - max_gen : int - Number of generations before termination. - pop_size : - Number of points in the GA. Set to 0 and it will be computed as four times the number of bits. - NotImplemented - procs_per_model : int - Number of processors to give each model under MPI. - NotImplemented - run_parallel : bool - Set to True to execute the points in a generation in parallel. - NotImplemented - """ - SimpleGADriver.__init__(self) - self.options.update({'elitism': elitism, 'max_gen': max_gen}) + EasyPyOptSparseSLSQP = PyOptSparseMissingDriver + EasyPyOptSparseIPOPT = PyOptSparseMissingDriver + + +# class EasySimpleGADriver(SimpleGADriver): +# def __init__(self, elitism=True, max_gen=100): +# """Simple Genetic Algorithm Driver with argument +# +# Parameters +# ---------- +# bits : dict +# Number of bits of resolution. Default is an empty dict, where every unspecified variable is assumed to be integer, and the number of bits is calculated automatically. If you have a continuous var, you should set a bits value as a key in this dictionary. +# NotImplemented +# debug_print : list +# List of what type of Driver variables to print at each iteration. Valid items in list are 'desvars', 'ln_cons', 'nl_cons', 'objs', 'totals' +# NotImplemented +# elitism : bool +# If True, replace worst performing point with best from previous generation each iteration. +# max_gen : int +# Number of generations before termination. +# pop_size : +# Number of points in the GA. Set to 0 and it will be computed as four times the number of bits. +# NotImplemented +# procs_per_model : int +# Number of processors to give each model under MPI. +# NotImplemented +# run_parallel : bool +# Set to True to execute the points in a generation in parallel. +# NotImplemented +# """ +# SimpleGADriver.__init__(self) +# self.options.update({'elitism': elitism, 'max_gen': max_gen}) # # class COBYLADriverWrapper(CONMINdriver): -- GitLab