Source code for naginterfaces.​library.​examples.​glopt.​bnd_mcs_solve_ex

#!/usr/bin/env python
"``naginterface.library.glopt.bnd_mcs_solve`` Python Example."

# NAG Copyright 2017-2019.

# pylint: disable=invalid-name,too-many-locals

from math import exp

from naginterfaces.library import glopt

[docs]def main(): """ Example for :func:`naginterfaces.library.glopt.bnd_mcs_solve`. Global optimization by multi-level coordinate search. >>> main() naginterfaces.library.glopt.bnd_mcs_solve Python Example Results. Global optimization of the Peaks objective function. Final objective value is -6.55113 Global optimum is (0.22828, -1.62553) """ print('naginterfaces.library.glopt.bnd_mcs_solve Python Example Results.') print('Global optimization of the Peaks objective function.') # Initialize the communication structure for the solver: comm = glopt.bnd_mcs_init() # The problem configuration: n = 2 objfun = lambda x, _nstate: ( 3.*(1. - x[0])**2*exp(-x[0]**2 - (x[1] + 1.)**2) - (10.*(x[0]/5. - x[0]**3 - x[1]**5)*exp(-x[0]**2 - x[1]**2)) - 1./3.0*exp(-(x[0] + 1.)**2 - x[1]**2) ) ibound = 0 bl = [-3.]*n bu = [3.]*n # Set some algorithmic parameters: glopt.bnd_mcs_optset_int('Function Evaluations Limit', 100000, comm) glopt.bnd_mcs_optset_real('Local Searches Tolerance', 1.e-10, comm) glopt.bnd_mcs_optset_int('Splits Limit', 20, comm) glopt.bnd_mcs_optset_int('Static Limit', 8, comm) glopt.bnd_mcs_optset_int('Local Searches Limit', 40, comm) glopt.bnd_mcs_optset_char('Local Searches', 'On', comm) mcs_soln = glopt.bnd_mcs_solve(objfun, ibound, bl, bu, comm) print('Final objective value is {:.5f}'.format(mcs_soln.obj)) print( 'Global optimum is (' + ', '.join(['{:.5f}'.format(xi) for xi in mcs_soln.x]) + ')' )
if __name__ == '__main__': import doctest import sys sys.exit( doctest.testmod( None, verbose=True, report=False, optionflags=doctest.REPORT_NDIFF, ).failed )