% driver script  for an  augmented Lagrangian algorithm
%
    global PAR
%
    PAR.cost      = 'cost_ex1';     % name of the external file for cost + gradient evaluation
    PAR.constr  = 'constr_ex1';  % name of the external file for constraint + jacobian evaluation
    PAR.penalty = [10];
%
    tol_opt = .0001;
    tol_con = .0001;
    itmax     = 8;
    xc = [-1 ; -1];   % initial estimate
%
% set flags for the box constrained trust region algorithm
    options.fcn      = 'lagr_a';
    options.goal     = 'minimization';
    options.free_g = 0;
    options.x_lower = -1.5*ones(size(xc));
    options.x_upper =  1.5*ones(size(xc));
    options.globalization = 'trust-region';
    options.max_step = .5;
    options.verbose = 0;
%
    iter = 0;
%
   PAR.flag = 'a';
   [cost,         nabla] = feval(PAR.cost,xc);	 %  evaluate cost & constraint jacobians 
   [constr, jacobian] = feval(PAR.constr,xc); % estimate the initial Lagrange multiplier
   PAR.lambda = jacobian'\nabla;                 % this is the least-squares solution
%
   optimal = norm(jacobian'*PAR.lambda - nabla) <= tol_opt; 
   feasible = norm(constr) <= tol_con; 
%
   fprintf('Begin Augmented Lagrangian \n \n')
   fprintf('initial point = %12.7e \n',xc)
   fprintf('multipliers = %12.7e \n',PAR.lambda)
   fprintf('(optimality, feasibility) = ( %2d , %2d ) \n',[optimal feasible])
%  loop for Augmented Lagrangian
    while (~optimal |~feasible) & iter <= itmax
        iter = iter + 1;
%
% Minimize the augmented lagrangian 
%
        xn = entrust(xc, options); 
%
%  estimate the new Lagrange multiplier
%
       xc = xn; 
       PAR.flag = 'a';
       [cost,         nabla] = feval(PAR.cost,xc);	 
       [constr, jacobian] = feval(PAR.constr,xc);
       PAR.lambda = jacobian'\nabla;   % this is the least-squares solution
%
% Check optimality and feasibility
%
       optimal = norm(jacobian'*PAR.lambda - nabla) <= tol_opt; 
       feasible = norm(constr) <= tol_con; 
%
       fprintf('Iteration %3d \n',iter)
       fprintf('current point = %12.7e \n',xc)
       fprintf('multipliers = %12.7e \n',PAR.lambda)
       fprintf('(optimality, feasibility) = ( %2d , %2d ) \n',[optimal feasible])
%
   end % while loop
%
