File: solvopt.m

package info (click to toggle)
dynare 4.5.7-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 49,408 kB
  • sloc: cpp: 84,998; ansic: 29,058; pascal: 13,843; sh: 4,833; objc: 4,236; yacc: 3,622; makefile: 2,278; lex: 1,541; python: 236; lisp: 69; xml: 8
file content (1243 lines) | stat: -rw-r--r-- 38,279 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
function [x,f,exitflag,n_f_evals,n_grad_evals,n_constraint_evals,n_constraint_gradient_evals]=solvopt(x,fun,grad,func,gradc,optim,varargin)
% [x,f,options]=solvopt(x,fun,grad,func,gradc,options,varargin)
%
% The function SOLVOPT, developed by Alexei Kuntsevich and Franz Kappe,
% performs a modified version of Shor's r-algorithm in
% order to find a local minimum resp. maximum of a nonlinear function
% defined on the n-dimensional Euclidean space or % a solution of a nonlinear
% constrained problem:
% min { f(x): g(x) (<)= 0, g(x) in R(m), x in R(n) }
%
% Inputs:
%   x       n-vector (row or column) of the coordinates of the starting
%           point,
% fun       name of an M-file (M-function) which computes the value
%           of the objective function <fun> at a point x,
%           synopsis: f=fun(x)
% grad      name of an M-file (M-function) which computes the gradient
%           vector of the function <fun> at a point x,
%           synopsis: g=grad(x)
% func      name of an M-file (M-function) which computes the MAXIMAL
%           RESIDUAL(!) for a set of constraints at a point x,
%           synopsis: fc=func(x)
% gradc     name of an M-file (M-function) which computes the gradient
%           vector for the maximal residual constraint at a point x,
%           synopsis: gc=gradc(x)
% optim     Options structure with fields:
%    optim.minimizer_indicator= H, where sign(H)=-1 resp. sign(H)=+1 means minimize
%        resp. maximize <fun> (valid only for unconstrained problem)
%        and H itself is a factor for the initial trial step size
%        (optim.minimizer_indicator=-1 by default),
%    optim.TolX= relative error for the argument in terms of the
%        infinity-norm (1.e-4 by default),
%    optim.TolFun= relative error for the function value (1.e-6 by default),
%    optim.MaxIter= limit for the number of iterations (15000 by default),
%    optim.verbosity= control of the display of intermediate results and error
%        resp. warning messages (default value is 0, i.e., no intermediate
%        output but error and warning messages, see more in the manual),
%    optim.TolXConstraint= admissible maximal residual for a set of constraints
%        (optim.TolXConstraint=1e-8 by default, see more in the manual),
%   *optim.SpaceDilation= the coefficient of space dilation (2.5 by default),
%   *optim.LBGradientStep= lower bound for the stepsize used for the difference
%        approximation of gradients (1e-12 by default, see more in the manual).
%  (* ... changes should be done with care)
%
% Outputs:
% x                     optimal parameter vector (row resp. column),
% f                     optimum function value
% exitflag:             the number of iterations, if positive,
%                       or an abnormal stop code, if negative (see more in the manual),
% n_f_evals:            number of objective evaluations
% n_grad_evals:         number of gradient evaluations,
% n_constraint_evals:   number of constraint function evaluations,
% n_constraint_gradient_evals   number of constraint gradient evaluations.
%
%
% Algorithm: Kuntsevich, A.V., Kappel, F., SolvOpt - The solver for local nonlinear optimization problems
% (version 1.1, Matlab, C, FORTRAN). University of Graz, Graz, 1997.
%
%
% Copyright (C) 1997-2008, Alexei Kuntsevich and Franz Kappel
% Copyright (C) 2008-2015 Giovanni Lombardo
% Copyright (C) 2015-2017 Dynare Team
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.



% strings: ----{
errmes='SolvOpt error:';
wrnmes='SolvOpt warning:';
error1='No function name and/or starting point passed to the function.';
error2='Argument X has to be a row or column vector of dimension > 1.';
error30='<fun> returns an empty string.';
error31='Function value does not exist (NaN is returned).';
error32='Function equals infinity at the point.';
error40='<grad> returns an improper matrix. Check the dimension.';
error41='Gradient does not exist (NaN is returned by <grad>).';
error42='Gradient equals infinity at the starting point.';
error43='Gradient equals zero at the starting point.';
error50='<func> returns an empty string.';
error51='<func> returns NaN at the point.';
error52='<func> returns infinite value at the point.';
error60='<gradc> returns an improper vector. Check the dimension';
error61='<gradc> returns NaN at the point.';
error62='<gradc> returns infinite vector at the point.';
error63='<gradc> returns zero vector at an infeasible point.';
error5='Function is unbounded.';
error6='Choose another starting point.';
warn1= 'Gradient is zero at the point, but stopping criteria are not fulfilled.';
warn20='Normal re-setting of a transformation matrix.' ;
warn21='Re-setting due to the use of a new penalty coefficient.' ;
warn4= 'Iterations limit exceeded.';
warn31='The function is flat in certain directions.';
warn32='Trying to recover by shifting insensitive variables.';
warn09='Re-run from recorded point.';
warn08='Ravine with a flat bottom is detected.';
termwarn0='SolvOpt: Normal termination.';
termwarn1='SolvOpt: Termination warning:';
appwarn='The above warning may be reasoned by inaccurate gradient approximation';
endwarn=[...
    'Premature stop is possible. Try to re-run the routine from the obtained point.               ';...
    'Result may not provide the optimum. The function apparently has many extremum points.        ';...
    'Result may be inaccurate in the coordinates. The function is flat at the optimum.            ';...
    'Result may be inaccurate in a function value. The function is extremely steep at the optimum.'];
% ----}

% ARGUMENTS PASSED ----{
if nargin<2           % Function and/or starting point are not specified
    exitflag=-1;
    disp(errmes);
    disp(error1);
    return
end
if nargin<3
    app=1;             % No user-supplied gradients
elseif isempty(grad)
    app=1;
else
    app=0;                     % Exact gradients are supplied
end

% OPTIONS ----{
doptions.minimizer_indicator=1;
doptions.TolX=1e-6; %accuracy of argument
doptions.TolFun=1e-6; %accuracy of function (see Solvopt p.29)
doptions.MaxIter=15000;
doptions.verbosity=1;
doptions.TolXConstraint=1.e-8;
doptions.SpaceDilation=2.5;
doptions.LBGradientStep=1.e-11;

if nargin<4
    optim=doptions;
elseif isempty(optim)
    optim=doptions;
end
% Check the values:
optim.TolX=max(optim.TolX,1.e-12);
optim.TolFun=max(optim.TolFun,1.e-12);
optim.TolX=max(optim.LBGradientStep*1.e2,optim.TolX);
optim.TolX=min(optim.TolX,1);
optim.TolFun=min(optim.TolFun,1);
optim.TolXConstraint=max(optim.TolXConstraint,1e-12);
optim.SpaceDilation=max([optim.SpaceDilation,1.5]);
optim.LBGradientStep=max(optim.LBGradientStep,1e-11);
% ----}

if isempty(func)
    constr=0;
else
    constr=1;                  % Constrained problem
    if isempty(gradc)
        appconstr=1;
    else
        appconstr=0;            % Exact gradients of constraints are supplied
    end
end
% ----}

% STARTING POINT ----{
if max(size(x))<=1
    disp(errmes);
    disp(error2);
    exitflag=-2;
    return
elseif size(x,2)==1
    n=size(x,1);
    x=x';
    trx=1;
elseif size(x,1)==1
    n=size(x,2);
    trx=0;
else
    disp(errmes);
    disp(error2);
    exitflag=-2;
    return
end
% ----}

% WORKING CONSTANTS AND COUNTERS ----{

n_f_evals=0; n_grad_evals=0;      % function and gradient calculations
if constr
    n_constraint_evals=0;
    n_constraint_gradient_evals=0;      % same for constraints
end
epsnorm=1.e-15;
epsnorm2=1.e-30;    % epsilon & epsilon^2

if constr, h1=-1                  % NLP: restricted to minimization
    cnteps=optim.TolXConstraint;                % Max. admissible residual
else
    h1=sign(optim.minimizer_indicator);         % Minimize resp. maximize a function
end

k=0;                               % Iteration counter

wdef=1/optim.SpaceDilation-1;               % Default space transf. coeff.

%Gamma control ---{
ajb=1+.1/n^2;                    % Base I
ajp=20;
ajpp=ajp;                        % Start value for the power
ajs=1.15;                        % Base II
knorms=0; gnorms=zeros(1,10);    % Gradient norms stored
                                 %---}

%Display control ---{
if optim.verbosity<=0, dispdata=0
    if optim.verbosity==-1
        dispwarn=0;
    else
        dispwarn=1;
    end
else
    dispdata=round(optim.verbosity);
    dispwarn=1;
end
ld=dispdata;
%---}

%Stepsize control ---{
dq=5.1;                          % Step divider (at f_{i+1}>gamma*f_{i})
du20=2;du10=1.5;du03=1.05;       % Step multipliers (at certain steps made)
kstore=3;nsteps=zeros(1,kstore); % Steps made at the last 'kstore' iterations
if app
    des=6.3;                 % Desired number of steps per 1-D search
else
    des=3.3;
end
mxtc=3;                          % Number of trial cycles (steep wall detect)
                                 %---}
termx=0; limxterm=50;              % Counter and limit for x-criterion

ddx   =max(1e-11,optim.LBGradientStep);      % stepsize for gradient approximation

low_bound=-1+1e-4;                 % Lower bound cosine used to detect a ravine

ZeroGrad=n*1.e-16;                 % Lower bound for a gradient norm

nzero=0;                           % Zero-gradient events counter
                                   % Lower bound for values of variables taking into account
lowxbound=max([optim.TolX,1e-3]);
% Lower bound for function values to be considered as making difference
lowfbound=optim.TolFun^2;
krerun=0;                          % Re-run events counter
detfr=optim.TolFun*100;              % relative error for f/f_{record}
detxr=optim.TolX*10;               % relative error for norm(x)/norm(x_{record})

warnno=0;                          % the number of warn.mess. to end with

kflat=0;                           % counter for points of flatness
stepvanish=0;                      % counter for vanished steps
stopf=0;
% ----}  End of setting constants
% ----}  End of the preamble

% COMPUTE THE FUNCTION  ( FIRST TIME ) ----{
if trx
    f=feval(fun,x',varargin{:});
else
    f=feval(fun,x,varargin{:});
end
n_f_evals=n_f_evals+1;
if isempty(f)
    if dispwarn
        disp(errmes)
        disp(error30)
    end
    exitflag=-3;
    if trx
        x=x';
    end
    return
elseif isnan(f)
    if dispwarn
        disp(errmes)
        disp(error31)
        disp(error6)
    end
    exitflag=-3;
    if trx
        x=x';
    end
    return
elseif abs(f)==Inf
    if dispwarn
        disp(errmes)
        disp(error32)
        disp(error6)
    end
    exitflag=-3;
    if trx
        x=x';
    end
    return
end
xrec=x; frec=f;     % record point and function value
                    % Constrained problem
if constr,  fp=f; kless=0
    if trx
        fc=feval(func,x');
    else
        fc=feval(func,x);
    end
    if isempty(fc)
        if dispwarn
            disp(errmes)
            disp(error50)
        end
        exitflag=-5;
        if trx
            x=x';
        end
        return
    elseif isnan(fc)
        if dispwarn
            disp(errmes)
            disp(error51)
            disp(error6)
        end
        exitflag=-5;
        if trx
            x=x';
        end
        return
    elseif abs(fc)==Inf
        if dispwarn
            disp(errmes)
            disp(error52)
            disp(error6)
        end
        exitflag=-5;
        if trx
            x=x';
        end
        return
    end
    n_constraint_evals=n_constraint_evals+1;
    PenCoef=1;                              % first rough approximation
    if fc<=cnteps
        FP=1; fc=0;             % feasible point
    else
        FP=0;                   % infeasible point
    end
    f=f+PenCoef*fc;
end
% ----}
% COMPUTE THE GRADIENT ( FIRST TIME ) ----{
if app
    deltax=h1*ddx*ones(size(x));
    if constr
        if trx
            g=apprgrdn(x',fp,fun,deltax',1,varargin{:});
        else
            g=apprgrdn(x ,fp,fun,deltax,1,varargin{:});
        end
    else
        if trx
            g=apprgrdn(x',f,fun,deltax',1,varargin{:});
        else
            g=apprgrdn(x ,f,fun,deltax,1,varargin{:});
        end
    end
    n_f_evals=n_f_evals+n;
else
    if trx
        g=feval(grad,x',varargin{:});
    else
        g=feval(grad,x,varargin{:});
    end
    n_grad_evals=n_grad_evals+1;
end
if size(g,2)==1, g=g'; end
ng=norm(g);
if size(g,2)~=n
    if dispwarn
        disp(errmes)
        disp(error40)
    end
    exitflag=-4;
    if trx
        x=x';
    end
    return
elseif isnan(ng)
    if dispwarn
        disp(errmes)
        disp(error41)
        disp(error6)
    end
    exitflag=-4;
    if trx
        x=x';
    end
    return
elseif ng==Inf
    if dispwarn
        disp(errmes)
        disp(error42)
        disp(error6)
    end
    exitflag=-4;
    if trx
        x=x';
    end
    return
elseif ng<ZeroGrad
    if dispwarn
        disp(errmes)
        disp(error43)
        disp(error6)
    end
    exitflag=-4;
    if trx
        x=x';
    end
    return
end
if constr
    if ~FP
        if appconstr
            deltax=sign(x); idx=find(deltax==0);
            deltax(idx)=ones(size(idx));
            deltax=ddx*deltax;
            if trx
                gc=apprgrdn(x',fc,func,deltax',0);
            else
                gc=apprgrdn(x ,fc,func,deltax ,0);
            end
            n_constraint_evals=n_constraint_evals+n;
        else
            if trx
                gc=feval(gradc,x');
            else
                gc=feval(gradc,x);
            end
            n_constraint_gradient_evals=n_constraint_gradient_evals+1;
        end
        if size(gc,2)==1
            gc=gc';
        end
        ngc=norm(gc);
        if size(gc,2)~=n
            if dispwarn
                disp(errmes)
                disp(error60)
            end
            exitflag=-6;
            if trx
                x=x';
            end
            return
        elseif isnan(ngc)
            if dispwarn
                disp(errmes)
                disp(error61)
                disp(error6)
            end
            exitflag=-6;
            if trx
                x=x';
            end
            return
        elseif ngc==Inf
            if dispwarn
                disp(errmes)
                disp(error62)
                disp(error6)
            end
            exitflag=-6;
            if trx
                x=x';
            end
            return
        elseif ngc<ZeroGrad
            if dispwarn
                disp(errmes)
                disp(error63)
            end
            exitflag=-6;
            if trx
                x=x';
            end
            return
        end
        g=g+PenCoef*gc; ng=norm(g);
    end
end
grec=g; nng=ng;
% ----}
% INITIAL STEPSIZE
h=h1*sqrt(optim.TolX)*max(abs(x));     % smallest possible stepsize
if abs(optim.minimizer_indicator)~=1
    h=h1*max(abs([optim.minimizer_indicator,h]));     % user-supplied stepsize
else
    h=h1*max(1/log(ng+1.1),abs(h));    % calculated stepsize
end

% RESETTING LOOP ----{
while 1
    kcheck=0;                        % Set checkpoint counter.
    kg=0;                            % stepsizes stored
    kj=0;                            % ravine jump counter
    B=eye(n);                        % re-set transf. matrix to identity
    fst=f; g1=g;  dx=0;
    % ----}

    % MAIN ITERATIONS ----{

    while 1
        k=k+1;kcheck=kcheck+1;
        laststep=dx;

        % ADJUST GAMMA --{
        gamma=1+max([ajb^((ajp-kcheck)*n),2*optim.TolFun]);
        gamma=min([gamma,ajs^max([1,log10(nng+1)])]);
        % --}
        gt=g*B;   w=wdef;
        % JUMPING OVER A RAVINE ----{
        if (gt/norm(gt))*(g1'/norm(g1))<low_bound
            if kj==2
                xx=x;
            end
            if kj==0
                kd=4
            end
            kj=kj+1;  w=-.9; h=h*2;             % use large coef. of space dilation
            if kj>2*kd
                kd=kd+1;
                warnno=1;
                if any(abs(x-xx)<epsnorm*abs(x)) % flat bottom is detected
                    if dispwarn
                        disp(wrnmes)
                        disp(warn08)
                    end
                end
            end
        else
            kj=0;
        end
        % ----}
        % DILATION ----{
        z=gt-g1;
        nrmz=norm(z);
        if(nrmz>epsnorm*norm(gt))
            z=z/nrmz;
            g1=gt+w*(z*gt')*z;  B=B+w*(B*z')*z;
        else
            z=zeros(1,n);
            nrmz=0;
            g1=gt;
        end
        d1=norm(g1);  g0=(g1/d1)*B';
        % ----}
        % RESETTING ----{
        if kcheck>1
            idx=find(abs(g)>ZeroGrad); numelem=size(idx,2);
            if numelem>0, grbnd=epsnorm*numelem^2
                if all(abs(g1(idx))<=abs(g(idx))*grbnd) || nrmz==0
                    if dispwarn
                        disp(wrnmes)
                        disp(warn20)
                    end
                    if abs(fst-f)<abs(f)*.01
                        ajp=ajp-10*n;
                    else
                        ajp=ajpp;
                    end
                    h=h1*dx/3;
                    k=k-1;
                    break
                end
            end
        end
        % ----}
        % STORE THE CURRENT VALUES AND SET THE COUNTERS FOR 1-D SEARCH
        xopt=x;fopt=f;   k1=0;k2=0;ksm=0;kc=0;knan=0;  hp=h;
        if constr, Reset=0; end
        % 1-D SEARCH ----{
        while 1
            x1=x;f1=f;
            if constr
                FP1=FP;
                fp1=fp;
            end
            x=x+hp*g0;
            % FUNCTION VALUE
            if trx
                f=feval(fun,x',varargin{:});
            else
                f=feval(fun,x,varargin{:});
            end
            n_f_evals=n_f_evals+1;
            if h1*f==Inf
                if dispwarn
                    disp(errmes)
                    disp(error5)
                end
                exitflag=-7;
                if trx
                    x=x';
                end
                return
            end
            if constr, fp=f;
                if trx
                    fc=feval(func,x');
                else
                    fc=feval(func,x);
                end
                n_constraint_evals=n_constraint_evals+1;
                if  isnan(fc)
                    if dispwarn
                        disp(errmes)
                        disp(error51)
                        disp(error6)
                    end
                    exitflag=-5;
                    if trx
                        x=x';
                    end
                    return
                elseif abs(fc)==Inf
                    if dispwarn
                        disp(errmes)
                        disp(error52)
                        disp(error6)
                    end
                    exitflag=-5;
                    if trx
                        x=x';
                    end
                    return
                end
                if fc<=cnteps
                    FP=1;
                    fc=0;
                else
                    FP=0;
                    fp_rate=(fp-fp1);
                    if fp_rate<-epsnorm
                        if ~FP1
                            PenCoefNew=-15*fp_rate/norm(x-x1);
                            if PenCoefNew>1.2*PenCoef
                                PenCoef=PenCoefNew; Reset=1; kless=0; f=f+PenCoef*fc; break
                            end
                        end
                    end
                end
                f=f+PenCoef*fc;
            end
            if abs(f)==Inf || isnan(f)
                if dispwarn, disp(wrnmes)
                    if isnan(f)
                        disp(error31)
                    else
                        disp(error32)
                    end
                end
                if ksm || kc>=mxtc
                    exitflag=-3;
                    if trx
                        x=x';
                    end
                    return
                else
                    k2=k2+1;
                    k1=0;
                    hp=hp/dq;
                    x=x1;
                    f=f1;
                    knan=1;
                    if constr
                        FP=FP1;
                        fp=fp1;
                    end
                end
                % STEP SIZE IS ZERO TO THE EXTENT OF EPSNORM
            elseif all(abs(x-x1)<abs(x)*epsnorm)
                stepvanish=stepvanish+1;
                if stepvanish>=5
                    exitflag=-14;
                    if dispwarn
                        disp(termwarn1)
                        disp(endwarn(4,:))
                    end
                    if trx
                        x=x';
                    end
                    return
                else
                    x=x1;
                    f=f1;
                    hp=hp*10;
                    ksm=1;
                    if constr
                        FP=FP1;
                        fp=fp1;
                    end
                end
                % USE SMALLER STEP
            elseif h1*f<h1*gamma^sign(f1)*f1
                if ksm
                    break
                end
                k2=k2+1;k1=0; hp=hp/dq; x=x1;f=f1;
                if constr
                    FP=FP1;
                    fp=fp1;
                end
                if kc>=mxtc, break, end
                % 1-D OPTIMIZER IS LEFT BEHIND
            else
                if h1*f<=h1*f1
                    break
                end
                % USE LARGER STEP
                k1=k1+1;
                if k2>0
                    kc=kc+1;
                end
                k2=0;
                if k1>=20
                    hp=du20*hp;
                elseif k1>=10
                    hp=du10*hp;
                elseif k1>=3
                    hp=du03*hp;
                end
            end
        end
        % ----}  End of 1-D search
        % ADJUST THE TRIAL STEP SIZE ----{
        dx=norm(xopt-x);
        if kg<kstore
            kg=kg+1;
        end
        if kg>=2
            nsteps(2:kg)=nsteps(1:kg-1);
        end
        nsteps(1)=dx/(abs(h)*norm(g0));
        kk=sum(nsteps(1:kg).*[kg:-1:1])/sum([kg:-1:1]);
        if     kk>des
            if kg==1
                h=h*(kk-des+1);
            else
                h=h*sqrt(kk-des+1);
            end
        elseif kk<des
            h=h*sqrt(kk/des);
        end

        stepvanish=stepvanish+ksm;
        % ----}
        % COMPUTE THE GRADIENT ----{
        if app
            deltax=sign(g0); idx=find(deltax==0);
            deltax(idx)=ones(size(idx));  deltax=h1*ddx*deltax;
            if constr
                if trx
                    g=apprgrdn(x',fp,fun,deltax',1,varargin{:});
                else
                    g=apprgrdn(x ,fp,fun,deltax,1,varargin{:});
                end
            else
                if trx
                    g=apprgrdn(x',f,fun,deltax',1,varargin{:});
                else
                    g=apprgrdn(x ,f,fun,deltax ,1,varargin{:});
                end
            end
            n_f_evals=n_f_evals+n;
        else
            if trx
                g=feval(grad,x',varargin{:});
            else
                g=feval(grad,x,varargin{:});
            end
            n_grad_evals=n_grad_evals+1;
        end
        if size(g,2)==1
            g=g'
        end
        ng=norm(g);
        if isnan(ng)
            if dispwarn
                disp(errmes)
                disp(error41)
            end
            exitflag=-4;
            if trx
                x=x';
            end
            return
        elseif ng==Inf
            if dispwarn
                disp(errmes)
                disp(error42)
            end
            exitflag=-4;
            if trx
                x=x';
            end
            return
        elseif ng<ZeroGrad
            if dispwarn
                disp(wrnmes)
                disp(warn1)
            end
            ng=ZeroGrad;
        end
        % Constraints:
        if constr
            if ~FP
                if ng<.01*PenCoef
                    kless=kless+1;
                    if kless>=20
                        PenCoef=PenCoef/10;
                        Reset=1;
                        kless=0;
                    end
                else
                    kless=0;
                end
                if appconstr
                    deltax=sign(x); idx=find(deltax==0);
                    deltax(idx)=ones(size(idx));  deltax=ddx*deltax;
                    if trx
                        gc=apprgrdn(x',fc,func,deltax',0);
                    else
                        gc=apprgrdn(x ,fc,func,deltax ,0);
                    end
                    n_constraint_evals=n_constraint_evals+n;
                else
                    if trx
                        gc=feval(gradc,x');
                    else
                        gc=feval(gradc,x );
                    end
                    n_constraint_gradient_evals=n_constraint_gradient_evals+1;
                end
                if size(gc,2)==1
                    gc=gc';
                end
                ngc=norm(gc);
                if isnan(ngc)
                    if dispwarn
                        disp(errmes)
                        disp(error61)
                    end
                    exitflag=-6;
                    if trx
                        x=x';
                    end
                    return
                elseif ngc==Inf
                    if dispwarn
                        disp(errmes)
                        disp(error62)
                    end
                    exitflag=-6;
                    if trx
                        x=x';
                    end
                    return
                elseif ngc<ZeroGrad && ~appconstr
                    if dispwarn
                        disp(errmes)
                        disp(error63)
                    end
                    exitflag=-6;
                    if trx
                        x=x';
                    end
                    return
                end
                g=g+PenCoef*gc; ng=norm(g);
                if Reset
                    if dispwarn
                        disp(wrnmes)
                        disp(warn21)
                    end
                    h=h1*dx/3; k=k-1; nng=ng; break
                end
            end
        end
        if h1*f>h1*frec
            frec=f;
            xrec=x;
            grec=g;
        end
        % ----}
        if ng>ZeroGrad
            if knorms<10
                knorms=knorms+1;
            end
            if knorms>=2
                gnorms(2:knorms)=gnorms(1:knorms-1);
            end
            gnorms(1)=ng;
            nng=(prod(gnorms(1:knorms)))^(1/knorms);
        end

        % DISPLAY THE CURRENT VALUES ----{
        if k==ld
            disp('Iter.# ..... Function ... Step Value ... Gradient Norm ');
            disp(sprintf('%5i   %13.5e   %13.5e     %13.5e',k,f,dx,ng));
            ld=k+dispdata;
        end
        %----}
        % CHECK THE STOPPING CRITERIA ----{
        termflag=1;
        if constr
            if ~FP
                termflag=0;
            end
        end
        if kcheck<=5
            termflag=0;
        end
        if knan
            termflag=0
        end
        if kc>=mxtc
            termflag=0;
        end
        % ARGUMENT
        if termflag
            idx=find(abs(x)>=lowxbound);
            if isempty(idx) || all(abs(xopt(idx)-x(idx))<=optim.TolX*abs(x(idx)))
                termx=termx+1;
                % FUNCTION
                if abs(f-frec)> detfr * abs(f)    && ...
                        abs(f-fopt)<=optim.TolFun*abs(f) && ...
                        krerun<=3                      && ...
                        ~constr
                    if any(abs(xrec(idx)-x(idx))> detxr * abs(x(idx)))
                        if dispwarn
                            disp(wrnmes)
                            disp(warn09)
                        end
                        x=xrec;
                        f=frec;
                        g=grec;
                        ng=norm(g);
                        krerun=krerun+1;
                        h=h1*max([dx,detxr*norm(x)])/krerun;
                        warnno=2;
                        break
                    else
                        h=h*10;
                    end
                elseif  abs(f-frec)> optim.TolFun*abs(f)    && ...
                        norm(x-xrec)<optim.TolX*norm(x) && constr

                elseif  abs(f-fopt)<=optim.TolFun*abs(f)  || ...
                        abs(f)<=lowfbound               || ...
                        (abs(f-fopt)<=optim.TolFun && termx>=limxterm )
                    if stopf
                        if dx<=laststep
                            if warnno==1 && ng<sqrt(optim.TolFun)
                                warnno=0;
                            end
                            if ~app
                                if any(abs(g)<=epsnorm2)
                                    warnno=3;
                                end
                            end
                            if warnno~=0
                                exitflag=-warnno-10;
                                if dispwarn, disp(termwarn1)
                                    disp(endwarn(warnno,:))
                                    if app
                                        disp(appwarn);
                                    end
                                end
                            else
                                exitflag=k;
                                if dispwarn
                                    disp(termwarn0);
                                end
                            end
                            if trx
                                x=x';
                            end
                            return
                        end
                    else
                        stopf=1;
                    end
                elseif dx<1.e-12*max(norm(x),1) && termx>=limxterm
                    exitflag=-14;
                    if dispwarn
                        disp(termwarn1)
                        disp(endwarn(4,:))
                        if app
                            disp(appwarn)
                        end
                    end
                    x=xrec; f=frec;
                    if trx
                        x=x';
                    end
                    return
                else
                    stopf=0;
                end
            end
        end
        % ITERATIONS LIMIT
        if(k==optim.MaxIter)
            exitflag=-9;
            if trx
                x=x';
            end
            if dispwarn
                disp(wrnmes)
                disp(warn4)
            end
            return
        end
        % ----}
        % ZERO GRADIENT ----{
        if constr
            if ng<=ZeroGrad
                if dispwarn
                    disp(termwarn1)
                    disp(warn1)
                end
                exitflag=-8;
                if trx
                    x=x';
                end
                return
            end
        else
            if ng<=ZeroGrad
                nzero=nzero+1;
                if dispwarn
                    disp(wrnmes)
                    disp(warn1)
                end
                if nzero>=3
                    exitflag=-8;
                    if trx
                        x=x';
                    end
                    return
                end
                g0=-h*g0/2;
                for i=1:10
                    x=x+g0;
                    if trx
                        f=feval(fun,x',varargin{:});
                    else
                        f=feval(fun,x,varargin{:});
                    end
                    n_f_evals=n_f_evals+1;
                    if abs(f)==Inf
                        if dispwarn
                            disp(errmes)
                            disp(error32)
                        end
                        exitflag=-3;
                        if trx
                            x=x';
                        end
                        return
                    elseif isnan(f)
                        if dispwarn
                            disp(errmes)
                            disp(error32)
                        end
                        exitflag=-3;
                        if trx
                            x=x';
                        end
                        return
                    end
                    if app
                        deltax=sign(g0);
                        idx=find(deltax==0);
                        deltax(idx)=ones(size(idx));
                        deltax=h1*ddx*deltax;
                        if trx
                            g=apprgrdn(x',f,fun,deltax',1,varargin{:});
                        else
                            g=apprgrdn(x,f,fun,deltax,1,varargin{:});
                        end
                        n_f_evals=n_f_evals+n;
                    else
                        if trx
                            g=feval(grad,x',varargin{:});
                        else
                            g=feval(grad,x,varargin{:});
                        end
                        n_grad_evals=n_grad_evals+1;
                    end
                    if size(g,2)==1
                        g=g';
                    end
                    ng=norm(g);
                    if ng==Inf
                        if dispwarn
                            disp(errmes)
                            disp(error42)
                        end
                        exitflag=-4;
                        if trx
                            x=x';
                        end
                        return
                    elseif isnan(ng)
                        if dispwarn
                            disp(errmes)
                            disp(error41)
                        end
                        exitflag=-4;
                        if trx
                            x=x';
                        end
                        return
                    end
                    if ng>ZeroGrad
                        break
                    end
                end
                if ng<=ZeroGrad
                    if dispwarn
                        disp(termwarn1)
                        disp(warn1)
                    end
                    exitflag=-8;
                    if trx
                        x=x';
                    end
                    return
                end
                h=h1*dx;
                break
            end
        end
        % ----}
        % FUNCTION IS FLAT AT THE POINT ----{
        if ~constr && abs(f-fopt)<abs(fopt)*optim.TolFun && kcheck>5 && ng<1
            idx=find(abs(g)<=epsnorm2);
            ni=size(idx,2);
            if ni>=1 && ni<=n/2 && kflat<=3
                kflat=kflat+1;
                if dispwarn
                    disp(wrnmes)
                    disp(warn31)
                end
                warnno=1;
                x1=x; fm=f;
                for j=idx
                    y=x(j); f2=fm;
                    if y==0
                        x1(j)=1;
                    elseif abs(y)<1
                        x1(j)=sign(y);
                    else
                        x1(j)=y;
                    end
                    for i=1:20
                        x1(j)=x1(j)/1.15;
                        if trx
                            f1=feval(fun,x1',varargin{:});
                        else
                            f1=feval(fun,x1,varargin{:});
                        end
                        n_f_evals=n_f_evals+1;
                        if abs(f1)~=Inf && ~isnan(f1)
                            if h1*f1>h1*fm
                                y=x1(j);
                                fm=f1;
                            elseif h1*f2>h1*f1
                                break
                            elseif f2==f1
                                x1(j)=x1(j)/1.5;
                            end
                            f2=f1;
                        end
                    end
                    x1(j)=y;
                end
                if h1*fm>h1*f
                    if app
                        deltax=h1*ddx*ones(size(deltax));
                        if trx
                            gt=apprgrdn(x1',fm,fun,deltax',1,varargin{:});
                        else
                            gt=apprgrdn(x1 ,fm,fun,deltax ,1,varargin{:});
                        end
                        n_f_evals=n_f_evals+n;
                    else
                        if trx
                            gt=feval(grad,x1',varargin{:});
                        else
                            gt=feval(grad,x1,varargin{:});
                        end
                        n_grad_evals=n_grad_evals+1;
                    end
                    if size(gt,2)==1
                        gt=gt';
                    end
                    ngt=norm(gt);
                    if ~isnan(ngt) && ngt>epsnorm2
                        if dispwarn
                            disp(warn32)
                        end
                        optim.TolFun=optim.TolFun/5;
                        x=x1;
                        g=gt;
                        ng=ngt;
                        f=fm;
                        h=h1*dx/3;
                        break
                    end
                end
            end
        end
        % ----}
    end % iterations
end % restart
    % end of the function
    %
    end