<jupyter_start><jupyter_text>Migrating from `uncon_conjgrd_comp` to `handle_solve_bounds_foas`This notebook illustrates the steps required to upgrade from the solver `uncon_conjgrd_comp` (`E04DG`) to `handle_solve_bounds_foas` (`E04KF`) introduced at Mark 27 of the NAG Library.From the usage perspective, the main difference between the solvers is the user call-backs,`uncon_conjgrd_comp` has a single user call-back that can return *objective function* and *gradient* evaluations, while `handle_solve_bounds_foas` has two separate user call-backs, one for the *objective funtion* and one for the *objective gradient*.In this notebook the 2d Rosenbrock problem is solved with both solvers and illustrates the changes necessary for the migration to `handle_solve_bounds_foas`. The solution to the problem is (1, 1).<jupyter_code># NAG Copyright 2020.
from naginterfaces.base import utils
from naginterfaces.library import opt
import numpy as np
# Define E04DG user call-back
def objfun_e04dg(mode, x, _nstate, _data=None):
    objf = (1. - x[0])**2 + 100.*(x[1] - x[0]**2)**2
    if mode == 2:
        fdx = [
            2.*x[0] - 400.*x[0]*(x[1]-x[0]**2) - 2.,
            200.*(x[1]-x[0]**2),
        ]
        return objf, fdx
    return objf, np.zeros(len(x))
# Define user call-backs for E04KF
def objfun_e04kf(x, _inform, _data=None): 
    """Return the objective function value"""
    objf = (1. - x[0])**2 + 100.*(x[1] - x[0]**2)**2
    return objf

def objgrd_e04kf(x, fdx, inform, _data=None):
    """The objective's gradient. Note that fdx has to be updated IN-PLACE"""
    fdx[:] = [
        2.*x[0] - 400.*x[0]*(x[1]-x[0]**2) - 2.,
        200.*(x[1]-x[0]**2),
    ]
    return inform
# The initial guess
x = [-1.5, 1.9]

# Use an explicit I/O manager for abbreviated iteration output
iom = utils.FileObjManager(locus_in_output=False)<jupyter_output><empty_output><jupyter_text>Solve the problem with `uncon_conjgrd_comp`<jupyter_code>comm = opt.nlp1_init('uncon_conjgrd_comp')
slv = opt.uncon_conjgrd_comp(objfun_e04dg, x, comm, io_manager=iom)
print('Solution: \n', slv.x)<jupyter_output>Solution: 
 [1.00000676 1.00001354]<jupyter_text>Now solve with the new solver `handle_solve_bounds_foas`<jupyter_code># Create an empty handle for the problem
nvar = len(x)
handle = opt.handle_init(nvar)

# Define the nonlinear objective in the handle
# Setup a gradient vector of length nvar
opt.handle_set_nlnobj(handle, idxfd=list(range(1, nvar+1)))

# Set some algorithmic options
for option in [
        'Print Options = No',      # print Options?
        'Print Solution = Yes',    # print on the screen the solution point X
        'Print Level = 1',         # print details of each iteration (screen)
]:
    opt.handle_opt_set(handle, option)

# Solve the problem and print the solution
opt.handle_solve_bounds_foas(handle, x, objfun=objfun_e04kf, objgrd=objgrd_e04kf, 
        io_manager=iom)

# Destroy the handle and free allocated memory
opt.handle_free(handle)<jupyter_output>-------------------------------------------------------------------------------
  E04KF, First order method for bound-constrained problems
 -------------------------------------------------------------------------------

 Status: converged, an optimal solution was found
 Value of the objective             2.12807E-15
 Norm of gradient                   3.67342E-08

 Variables:
     idx    lower bound       value       upper bound       gradient
       1       -inf        1.00000E+00       inf        -1.87204E-08
       2       -inf        1.00000E+00       inf        -3.67342E-08
{'fraction_non_alphanumeric': 0.14776536312849162, 'fraction_numerical': 0.0446927374301676, 'mean_word_length': 2.9051254089422027, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '294638', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1287, 'n_tokens_neox': 1172, 'n_words': 349, 'path': 'local_optimization/FOAS/migration/migration_e04dg_e04kf.ipynb', 'repo_name': 'Brunochris13/NAGPythonExamples', 'size': '3580'}