Home > source > @penlab > unconstr_min.m

# unconstr_min

## PURPOSE

unconstrained minimization

## SYNOPSIS

function [nFlag,rResults]=unconstr_min(obj)

## DESCRIPTION

     unconstrained minimization
     returns in rResults=[aug_lagr(xBest),norm(grad(xBest)), norm(grad(x_0))]

     obj() etc. should work with one parameter x and return first parameter value/grad/hess
     expects an open nl-file?, N, N_EQUAL, ... global variables

     in the structure called 'fnc' there should be obj, obj_grad, obj_hess - functions to evaluate
     the equality constrainted problem

## CROSS-REFERENCE INFORMATION

This function calls:

This function is called by:

- penlab PenLab (Pennon Laboratory for Matlab, previously PennonM)

## SOURCE CODE

    0001 function [nFlag,rResults]=unconstr_min(obj)
    0002 % unconstrained minimization
    0003 % returns in rResults=[aug_lagr(xBest),norm(grad(xBest)), norm(grad(x_0))]
    0004 %
    0005 % obj() etc. should work with one parameter x and return first parameter value/grad/hess
    0006 % expects an open nl-file?, N, N_EQUAL, ... global variables
    0007 %
    0008 % in the structure called 'fnc' there should be obj, obj_grad, obj_hess - functions to evaluate
    0009 % the equality constrainted problem
    0010
    0011 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    0012
    0013   starttime=cputime;
    0014   % reset default parameters if necessary
    0015   MAX_MITER = obj.allopts.max_inner_iter;
    0016   ALPHA = obj.allopts.inner_stop_limit;
    0017   TOL_DIR = obj.allopts.unc_dir_stop_limit;
    0018   solver = obj.allopts.unc_solver;
    0019   linesearch = obj.allopts.unc_linesearch;
    0020
    0021   miter=0;
    0022
    0023   % x0 = xall as a starting point
    0024   %fx=fnc.obj(x);
    0025   %gradx=fnc.obj_grad(x);
    0026   obj.eval_alx();
    0027   obj.eval_aldx();
    0028   fx_start = obj.ALx;
    0029   rNormG = norm(obj.ALdx);
    0030   rNormG_start = rNormG;
    0031
    0032   obj.print(3,Inf,'object(x_%3i) = %24.16E',miter,obj.ALx);
    0033   obj.print(3,Inf,'||grad(x)||_2 = %24.16E',rNormG);
    0034   obj.print(4,Inf,'        --- start of inner iter ---');
    0035   obj.print(4,Inf,' ');
    0036   obj.print(3,4,'     ----');
    0037
    0038   nFlag=1; % max it limit reached
    0039   rResults=[];
    0040   obj.initer_last=0;
    0041   obj.lsiter_last=0;
    0042   obj.stats_time_fact_last=0;
    0043
    0044     while (miter < MAX_MITER)
    0045
    0046       %hessx=fnc.obj_hess(x);
    0047       obj.eval_alddx();
    0048
    0049       %%%%%%%% Newton solver %%%%%%%%%
    0050       switch solver
    0051       case 0  % (Modified) Cholesky factorization
    0052     [dir,nFlagSol] = obj.solve_chol(obj.ALddx,-obj.ALdx);
    0053       %case 1  % CG+??
    0054       %case   %
    0055       %case   %
    0056       otherwise
    0057         obj.print(1,Inf,'unconstr_min() error: Sorry, no such solver, terminating...');
    0058     nFlag=100;
    0059     break;
    0060       end
    0061
    0062       if (nFlagSol>0)
    0063         nFlag=2; % solver failed, cannot continue
    0064     obj.print(3,Inf,'FAILURE: Newton solver (%i) cannot continue (flag %i)',solver,nFlagSol);
    0065         break;
    0066       end
    0067
    0068       %%%%%%%% linesearch %%%%%%%%%
    0069       switch linesearch
    0070       case 0  % Do nothing, leave original data
    0071      % such as x, grad_x (useful for TR, ...)
    0072         nFlagLS = 0;
    0073       case 1  % Do full steps, no linesearch at all
    0074         [rRelStep, nFlagLS] = obj.ls_fullstep(dir);
    0075       case 2  % Armijo linesearch
    0076         [rRelStep, nFlagLS] = obj.ls_armijo(dir);
    0077       case 3  % Pennlp/Pennon ("els) linesearch
    0078         [rRelStep, nFlagLS] = obj.ls_pennon(dir);
    0079       %case   %
    0080       otherwise
    0081         obj.print(1,Inf,'unconstr_min() error: Sorry, no such LS, terminating...');
    0082     nFlag=100;
    0083     break;
    0084       end
    0085
    0086       if (nFlagLS>0)
    0087         nFlag=3; % LS failed, cannot continue
    0088     obj.print(3,Inf,'FAILURE: Linesearch (%i) cannot continue (flag %i)',linesearch,nFlagLS);
    0089         break;
    0090       end
    0091
    0092       %%%%%%%% %%%%%%%%%
    0093       rNormG = norm(obj.ALdx);
    0094       miter=miter+1;
    0095
    0096       obj.print(4,Inf,' ');
    0097       obj.print(3,Inf,'object(x_%3i) = %24.16E',miter,obj.ALx);
    0098       obj.print(3,Inf,'||grad(x)||_2 = %24.16E',rNormG);
    0099       obj.print(4,Inf,'        --- end of %3i in. iter ---\n',miter);
    0100       obj.print(3,4,'     ----');
    0101
    0102       %%%%%%%% stopping criterion %%%%%%%%%
    0103       % just make it simple at the begining
    0104       if (rNormG < ALPHA)
    0105         nFlag=0;
    0106     break;
    0107       end
    0108
    0109     end % of while
    0110
    0111   if (nFlag==1)
    0112     obj.print(3,Inf,'FAILURE: Unconstr minimization max iter (%i) reached.',miter);
    0113   elseif (nFlag==0)
    0114     obj.print(4,Inf,'Unconstr min OK');
    0115   end
    0116
    0117   rResults=[obj.ALx,rNormG, rNormG_start];
    0118
    0119   % update stats
    0120   obj.initer = obj.initer+obj.initer_last;
    0121   obj.lsiter = obj.lsiter+obj.lsiter_last;
    0122   obj.miter=obj.miter+miter;
    0123   obj.miter_last=miter;
    0124   obj.stats_time_miter_last = cputime - starttime;
    0125   obj.stats_time_miters = obj.stats_time_miters + obj.stats_time_miter_last;
    0126   obj.stats_time_fact = obj.stats_time_fact + obj.stats_time_fact_last;
    0127
    0128   return;
    0129