Source code for naginterfaces.library.examples.glopt.handle_solve_mcs_ex

#!/usr/bin/env python3
"``naginterfaces.library.opt.handle_solve_mcs`` Python Example."

# NAG Copyright 2021.

from math import exp
from naginterfaces.base import utils
from naginterfaces.library import opt
from naginterfaces.library import glopt

[docs]def main(): """ Example for :func:`naginterfaces.library.glopt.handle_solve_mcs`. Global optimization by multi-level coordinate search. >>> main() naginterfaces.library.glopt.handle_solve_mcs 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.handle_solve_mcs Python Example Results.' ) print('Global optimization of the Peaks objective function.') # Define the 'Peaks' functions objfun = lambda x, inform: ( 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), inform, ) # Create the problem in the NAG Optimization Modelling Suite. # Define an empty problem with 2 variables nvar = 2 handle = opt.handle_init(nvar) # Define the objective for the problem as being nonlinear opt.handle_set_nlnobj( handle, idxfd=list(range(1, nvar+1)), ) # Declare the simple bounds for the variables (both are in [-3., 3.]) opt.handle_set_simplebounds( handle, bl=[-3., -3.], bu=[3., 3.], ) # Set some optional parameters for option in [ 'MCS Max Objective Calls = 100000', 'MCS Print Frequency = 10', 'Print Solution = yes', ]: opt.handle_opt_set(handle, option) # Use an explicit I/O manager for abbreviated iteration output iom = utils.FileObjManager(locus_in_output=False) # Solve the problem soln = glopt.handle_solve_mcs(handle, objfun=objfun, io_manager=iom) # Destroy the handle opt.handle_free(handle) # Extract the solution and print it objval = soln.rinfo[0] print('Final objective value is {:.5f}'.format(objval)) print( 'Global optimum is (' + ', '.join(['{:.5f}'.format(xi) for xi in soln.x]) + ')' )
if __name__ == '__main__': import doctest import sys sys.exit( doctest.testmod( None, verbose=True, report=False, optionflags=doctest.ELLIPSIS | doctest.REPORT_NDIFF, ).failed )