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
|
cnvtst <- function (alpha, pnorm, xnorm,
dif, ftest, gnorm, gtp, f, flast, g, ipivot, accrcy) {
## Note: trace is integer (changed 180329)
## ---------------------------------------------------------
## test for convergence
## ---------------------------------------------------------
## set up
## ---------------------------------------------------------
conv <- 0;
eps <- .Machine$double.eps
toleps <- sqrt(accrcy) + sqrt(eps);
rtleps <- accrcy + eps;
imax <- 0;
ltest <- (flast - f <= -0.5*gtp);
## ---------------------------------------------------------
## if anti-zigzag test satisfied, test multipliers;
## if appropriate, modify active set
## ---------------------------------------------------------
if ( ! ltest) {
ind <- which( (ipivot != 0) & (ipivot != 2))
if ( length(ind) > 0 ) { ## how to ensure ind not empty
t <- -sum(ipivot[ind]*g[ind])
cmax <- min(t) # ?? why [cmax, imax] = min(t) ??
imax <- cmax
if (cmax >= 0) { imax <- 0 }
}
}
if (imax != 0) {
ipivot[ ind[imax] ] <- 0;
flast <- f;
} else {
conv = ( ( (alpha*pnorm < toleps*(1 + xnorm) )
&& (abs(dif) < rtleps*ftest)
&& (gnorm < accrcy^(1/3)*ftest) ) ||
( gnorm < .01*sqrt(accrcy)*ftest) )
}
result <- list(conv=conv, flast1=flast, ipivot1=ipivot)
}
crash <- function (x, low, up) {
##---------------------------------------------------------
## this initializes the constraint information, and
## ensures that the initial point satisfies
## low <= x <= up.
## the constraints are checked for consistency.
##---------------------------------------------------------
ierror <- 0
if (any(low > up)) { ierror = - max(which(low > up)) }
# above is check on error in bounds specification
xnew <- pmax (low, x) # force params into bounds
xnew <- pmin (up, xnew) # No diagnostic! ??
# we output revised parameters
n <- length(x)
ind <- which(low == x)
ipivot <- rep(0, n) ## zeros(size(x)) in Matlab
if (length(ind) > 0) { ipivot[ind] <- -1 }
ind <- which(x == up)
if (length(ind) > 0) { ipivot[ind] <- 1 }
ind <- which(low == up)
if (length(ind) > 0) { ipivot[ind] <- 2 }
list(ipivot=ipivot, ierror=ierror, xnew=xnew)
}
gtims <- function (v, x, g, accrcy, xnorm, sfun, ...) {
##---------------------------------------------------------
## compute the product of the Hessian times the vector v;
## store result in the vector gv
## (finite-difference version)
##---------------------------------------------------------
## cat("v, x, g:")
## print(v)
## print(x)
## print(g)
delta <- sqrt(accrcy)*(1 + xnorm)/sqrt(sum(v^2))
hg <- x + delta*v
tresult<-sfun(hg, ...)
## cat("tresult: ")
## print(tresult)
gv <- attr(tresult, "gradient")
gv <- (gv - g)/delta
gv
}
initpc <- function(d, upd1, ireset) {
##---------------------------------------------------------
## initialize the diagonal preconditioner (d -- a vector!)
## ---------------------------------------------------------
## global hyk sk yk sr yr yksk yrsr ?? do we have these
# global vectors hyk sk yk sr yr & scalars yksk yrsr
## ---------------------------------------------------------
#% cat("In initpc -- sk and hyk if upd1 false: ", upd1,"\n")
#% print(envjn$sk)
if (upd1) {
td <- d
} else {
if (ireset) {
envjn$hyk <- d * envjn$sk # vector * vector by element??
sds <- as.numeric(crossprod(envjn$sk, envjn$hyk)) # matrix multiply
if (all(envjn$hyk == 0) && trace > 1) { cat("INITPC: envjn$hyk = 0 \n") }
if (sds == 0 && trace > 1) { cat("INITPC: sds = 0 \n") }
td <- d - d*d*envjn$sk*envjn$sk/sds + envjn$yk*envjn$yk/envjn$yksk
# by element
} else {
envjn$hyk <- d * envjn$sr # vector * vector
sds <- as.numeric(crossprod(envjn$sr, envjn$hyk))
srds <- as.numeric(crossprod(envjn$sk, envjn$hyk))
yrsk <- as.numeric(crossprod(envjn$yr, envjn$sk))
envjn$hyk <- d*envjn$sk - envjn$hyk*srds/sds + envjn$yr*yrsk/envjn$yrsr
td <- d - d*d*envjn$sr*envjn$sr/sds+envjn$yr*envjn$yr/envjn$yrsr
sds <- as.numeric(crossprod(envjn$sk, envjn$hyk))
td <- td - envjn$hyk*envjn$hyk/sds + envjn$yk*envjn$yk/envjn$yksk
}
}
#% cat("td:")
#% print(td)
ans<-list(td=td)
}
lin1 <- function(p, x, f, alpha, g, sfun, ...){
## ---------------------------------------------------------
## line search (naive)
## ---------------------------------------------------------
## set up
## ---------------------------------------------------------
# cat("lin1: alpha=",alpha," p:\n")
# print(p)
if (is.null(alpha)) alpha <- 0
ierror <- 3
xnew <- x
fnew <- f
gnew <- g
maxit <- 15
if (alpha == 0) {
ierror <- 0
maxit <- 1
}
alpha1 <- alpha
## ---------------------------------------------------------
## line search
## ---------------------------------------------------------
for (itcnt in 1:maxit) {
xt <- x + alpha1*p
fg <- sfun(xt, ...) # Note: added dots 140902
ft<-fg
gt<- attr(fg,"gradient") # may simplify later
if (ft < f) {
ierror <- 0
xnew <- xt
fnew <- ft
gnew <- gt
## cat("about to break in lin1\n")
break
}
alpha1 <- alpha1 / 2
}
if (ierror == 3) { alpha1 <- 0 }
nf1 <- itcnt
## never used in SGN code
## if (nargout == 7) { ## ?? what is nargout?
## dfdp <- as.numeric(crossprod(gt, p) )
## varargout{1} <- dfdp ## ?? what is varargout
## end
result<-list(xnew=xnew, fnew=fnew, gnew=gnew, nf1=nf1,
ierror=ierror, alpha1=alpha1)
}
lmqnbc <- function (x, sfun, lower, upper, maxit, maxfun, stepmx, accrcy, trace, ...) {
## ---------------------------------------------------------
## This is a bounds-constrained truncated-newton method.
## The truncated-newton method is preconditioned by a
## limited-memory quasi-newton method (computed by
## this routine) with a diagonal scaling (routine ndia3).
## For further details, see routine tnbc.
## ---------------------------------------------------------
## global hyk sk yk sr yr yksk yrsr
## ---------------------------------------------------------
## check that initial x is feasible and that the bounds
## are consistent
## ---------------------------------------------------------
n <- length(x)
# JN: Define globals here
gtn<-list(yrsr=0, yksk=0, yr = rep(0, n), yk = rep(0, n),
sr = rep(0, n), sk = rep(0, n),
hg=rep(0,n), hyk=rep(0,n), hyr=rep(0,n) )
envjn<<-list2env(gtn) # Note this important tool for globals in Rtnmin
# end globals
## [ipivot, ierror, x] = crash(x, lower, upper)
if (trace > 1) cat("lmqnbc -- crout:")
crout<-crash(x, lower, upper)
if (trace) print(crout)
ierror <- crout$ierror
ipivot <- crout$ipivot
x<-crout$xnew # in case x changed by bounds
f <- 0
g <- rep(0,n)
if (ierror != 0) {
stop('LMQNBC: terminating (no feasible point)')
## return
}
## ---------------------------------------------------------
## initialize variables, parameters, and constants
## ---------------------------------------------------------
options(digits=5)
if (trace > 1) cat("stempmx, accrcy, maxfun:",stepmx, accrcy, maxfun, "\n")
if (trace > 0) cat(' it nf cg f |g|\n')
eps <- .Machine$double.eps
upd1 <- TRUE
ncg <- 0
conv <- FALSE
xnorm <- max(abs(x))
ierror <- 0
if ( (stepmx < sqrt(accrcy)) || (maxfun < 1) ) {
ierror <- -1
xstar <- x
almqn<-list(xstar=xstar, f=f, g=g, ierror=ierror,
nfngr=ncg)
if (trace > 1) {
cat("Exiting lmqnbc - almgn:")
print(almqn)
}
return(almqn) # return
}
## ---------------------------------------------------------
## compute initial function value and related information
## ---------------------------------------------------------
if (trace > 1) cat("Try initial fn\n")
fg <- sfun(x, ...)
nf <- 1
nit <- 0
g<- attr(fg,"gradient")
f<-fg
flast <- f
# if (is.null(g) ) { ## 160922 change
if (! is.numeric(g) ) { # try fix 180328
gnorm <- 1.0/eps
} else { gnorm <- max(abs(g)) } ## norm(g,'inf')
## ---------------------------------------------------------
## Test if Lagrange multipliers are non-negative.
## Because the constraints are only bounds, the Lagrange
## multipliers are components of the gradient.
## Then form the projected gradient.
## ---------------------------------------------------------
ind <- which((ipivot != 2) &
(as.numeric(crossprod(ipivot,g)) >0 ) )
if (length(ind) > 0) {
ipivot[ind] <- rep(0, length(ind))
}
g <- ztime (g, ipivot)
# if (is.null(g) ) { ## 160922 change
if (! is.numeric(g) ) { # try fix 180328
gnorm <- 1.0/eps
} else { gnorm <- max(abs(g)) } ## norm(g,'inf')
if (trace > 0) cat(nit,"\t", nf,"\t", ncg,"\t", f," ", gnorm,"\n")
if (trace > 1) {
print(x)
print(g)
}
## ---------------------------------------------------------
## check if the initial point is a local minimum.
## ---------------------------------------------------------
ftest <- 1 + abs(f)
if (gnorm < .01*sqrt(eps)*ftest) {
xstar <- x
almqn<-list(xstar=xstar, f=f, g=g, ierror=ierror,
nfngr=ncg)
return(almqn)
}
if (trace > 1) cat("before set initial, flast=",flast,"\n")
## ---------------------------------------------------------
## set initial values to other parameters
## ---------------------------------------------------------
icycle <- n-1
ireset <- 0
bounds <- TRUE
difnew <- 0
epsred <- .05
fkeep <- f
d <- rep(1,n)
## ---------------------------------------------------------
## ..........main iterative loop..........
## ---------------------------------------------------------
## compute the search direction
## ---------------------------------------------------------
argvec <- c(accrcy, gnorm, xnorm)
##[p, gtp, ncg1, d] <- ...
## modlnp (d, x, g, maxit, upd1, ireset, bounds, ipivot, ## argvec, sfun)
mres <- modlnp (d, x, g, maxit, upd1, ireset,
bounds, ipivot, argvec, sfun, ...)
ncg1 <- mres$ncg1
gtp <- mres$gtp
d <- mres$dnew
p <- mres$p
## cat("p:")
## print(p)
## tmp<-readline("cont.")
ncg <- ncg + ncg1
while (!conv) {
oldg <- g
pnorm <- max(abs(p)) # norm2(p, 'inf')
oldf <- f
## ---------------------------------------------------------
## line search
## ---------------------------------------------------------
pe <- pnorm + eps
spe <- stpmax (stepmx, pe, x, p, ipivot, lower, upper)
## cat("spe=",spe,"\n")
alpha <- step1 (f, gtp, spe)
alpha0 <- alpha
## cat("alpha0=",alpha0,"\n")
## [x_new, f_new, g_new, nf1, ierror, alpha] <- lin1 (p, x,
## f, alpha0, g, sfun)
reslin <- lin1 (p, x, f, alpha0, g, sfun, ...)
ierror <- reslin$ierror
alpha <- reslin$alpha1
## ---------------------------------------------------------
if ((alpha == 0) && (alpha0 != 0) || (ierror == 3)){
cat('Error in Line Search\n')
cat(' ierror = ', ierror, "\n")
cat(' alpha = ',alpha, "\n")
cat(' alpha0 = ', alpha0, "\n")
cat(' gtp = ', gtp, "\n")
## ############################
cat(' |g| = ', norm2(g), "\n")
cat(' |p| = ', norm2(p), "\n")
tmp <- readline('Hit any key to continue')
## ############################
}
## #######################
x <- reslin$xnew # need fixup
f <- reslin$fnew
g <- reslin$gnew
nf1 <- reslin$nf1
nf <- nf + nf1
nit <- nit + 1
## ---------------------------------------------------------
## update active set, if appropriate
## ---------------------------------------------------------
newcon <- FALSE
## cat("Check active set - alpha, spe, f:",alpha, spe,f,"\n")
if (abs(alpha-spe) <= 10*eps) {
newcon <- TRUE
ierror <- 0
## cat("flast:",flast,"\n")
# [ipivot, flast] <- modz (x, p, ipivot, lower, upper,
# flast, f, alpha)
## cat("ipivot before and after update by modz:\n")
## print(ipivot)
modzres<-modz(x, p, ipivot, lower, upper,
flast, f, alpha)
ipivot <- modzres$ipivot1
flast<-modzres$flast1
## print(ipivot)
}
if (ierror == 3) {
xstar <- x
almqn<-list(xstar=xstar, f=f, g=g, ierror=ierror,
nfngr=ncg)
if (trace > 0) {
cat("Error updating active set -- almqn:")
print(almqn)
}
return(almqn)
}
## ---------------------------------------------------------
## stop if more than maxfun evaluations have been made
## ---------------------------------------------------------
if (nf > maxfun) {
ierror <- 2
xstar <- x
almqn<-list(xstar=xstar, f=f, g=g, ierror=ierror,
nfngr=ncg)
if (trace > 0) {
cat("Too many function evaluations -- almqn:")
print(almqn)
}
return(almqn)
}
## ---------------------------------------------------------
## set up for convergence and resetting tests
## ---------------------------------------------------------
difold <- difnew
difnew <- oldf - f # scalar
if (icycle == 1) {
if (difnew > 2*difold) { epsred <- 2*epsred }
if (difnew < .5*difold) { epsred <- .5*epsred }
}
gv <- ztime (g, ipivot)
gnorm <- max(abs(gv))
ftest <- 1 + abs(f)
xnorm <- max(abs(x))
############### DISPLAY ##############
if (trace > 0) cat(nit,"\t", nf,"\t", ncg,"\t", f," ", gnorm,"\n")
## print(x)
## print(g)
## ---------------------------------------------------------
## test for convergence
## ---------------------------------------------------------
## [conv, flast, ipivot] <- cnvtst (alpha, pnorm, xnorm, ...
## difnew, ftest, gnorm, gtp, f, flast, g, ...
## ipivot, accrcy)
## cat("before cnvtst, flast=",flast,"\n")
ctres <- cnvtst (alpha, pnorm, xnorm,
difnew, ftest, gnorm, gtp, f, flast, g,
ipivot, accrcy)
conv <- ctres$conv
flast <- ctres$flast1
ipivot <- ctres$ipivot1
## cat("after cnvtst - conv, flast, ipivot:", conv, flast,"\n")
## print(ipivot)
if (conv) {
xstar <- x
almqn<-list(xstar=xstar, f=f, g=g, ierror=ierror,
nfngr=ncg)
return(almqn)
}
g <- ztime (g, ipivot)
## ---------------------------------------------------------
## modify data for LMQN preconditioner
## ---------------------------------------------------------
if (! newcon) { ## 0 is FALSE and 1 is TRUE supposedly
envjn$yk <- g - oldg
envjn$sk <- alpha*p
envjn$yksk <- as.numeric(crossprod(envjn$yk, envjn$sk))
## cat("yksk:",envjn$yksk,"\n")
ireset <- ( (icycle == n-1) |
(difnew < epsred*(fkeep-f)) )
if (! ireset) {
envjn$yrsr <-
as.numeric(crossprod(envjn$yr,envjn$sr))
ireset <- (envjn$yrsr <= 0)
}
upd1 <- (envjn$yksk <= 0)
}
## cat("newcon, upd1:", newcon, upd1,"\n")
## ---------------------------------------------------------
## compute the search direction
## ---------------------------------------------------------
argvec <- c(accrcy, gnorm, xnorm)
## [p, gtp, ncg1, d] <- ...
## modlnp (d, x, g, maxit, upd1, ireset, bounds,
## ipivot, argvec, sfun)
mres <- modlnp (d, x, g, maxit, upd1, ireset,
bounds, ipivot, argvec, sfun, ...)
ncg1 <- mres$ncg1
gtp <- mres$gtp
d <- mres$dnew
p <- mres$p
## cat("New p:")
## print(p)
## tmp<-readline("cont.")
ncg <- ncg + ncg1
## ---------------------------------------------------------
## update LMQN preconditioner
## ---------------------------------------------------------
if (! newcon) {
if (ireset) {
envjn$sr <- envjn$sk
envjn$yr <- envjn$yk
fkeep <- f
icycle <- 1
} else {
envjn$sr <- envjn$sr + envjn$sk
envjn$yr <- envjn$yr + envjn$yk
icycle <- icycle + 1
}
}
} # end while !conv
}
lmqn <- function (x, sfun, maxit, maxfun, stepmx, accrcy, trace, ...) {
## ---------------------------------------------------------
## truncated-newton method for unconstrained minimization
## (customized version)
## ---------------------------------------------------------
# global vectors hyk sk yk sr yr & scalars yksk yrsr
## ---------------------------------------------------------
## set up
## ---------------------------------------------------------
## format compact
## format short e
n<-length(x)
# JN: Define globals here. Is it necessary to set up.
gtn<-list(yrsr=0, yksk=0, yr = rep(0, n), yk = rep(0, n), sr = rep(0, n), sk = rep(0, n))
envjn<<-list2env(gtn)
# end globals
eps <- .Machine$double.eps
upd1 <- 1
ncg <- 0
xnorm <- max(abs(x)) # norm(x,'inf')
ierror <- 0
if (stepmx < sqrt(accrcy) || maxfun < 1) {
ierror <- -1
xstar <- x
almqn<-list(xstar=xstar, f=f, g=g, ierror=ierror,
nfngr=ncg)
return(almqn)
}
## ---------------------------------------------------------
## compute initial function value and related information
## ---------------------------------------------------------
fg <- sfun(x, ...)
#% print(fg)
g<- attr(fg, "gradient")
#% print(g)
if (is.null(g)) stop("Must have gradient defined for Rtnmin")
f<-fg
# if (is.null(g) ) { ## 160922 change
if (! is.numeric(g) ) { # try fix 180328
gnorm <- 1.0/eps
} else { gnorm <- max(abs(g)) } ## norm(g,'inf')
nf <- 1
nit <- 0
if (trace) cat("Itn ",nit," ",nf," ",ncg, " ",f, " ", gnorm,"\n")
## ---------------------------------------------------------
## check for small gradient at the starting point.
## ---------------------------------------------------------
ftest <- 1 + abs(f)
if (gnorm < .01*sqrt(eps)*ftest) {
ierror <- 0
xstar <- x
almqn<-list(xstar=xstar, f=f, g=g, ierror=ierror, nfngr=ncg)
return(almqn)
}
## ---------------------------------------------------------
## set initial values to other parameters
## ---------------------------------------------------------
n <- length(x)
icycle <- n-1
toleps <- sqrt(accrcy) + sqrt(eps)
rtleps <- accrcy + eps
difnew <- 0
epsred <- .05
fkeep <- f
conv <- FALSE
ireset <- 0
ipivot <- 0
if (trace > 1) cat("end initial values\n")
## ---------------------------------------------------------
## initialize diagonal preconditioner to the identity
## ---------------------------------------------------------
d <- rep(1,n) # as a vector
## ---------------------------------------------------------
## ..........main iterative loop..........
## ---------------------------------------------------------
## compute search direction
## ---------------------------------------------------------
argvec <- c(accrcy, gnorm, xnorm)
# cat("call modlnp\n")
mres <- modlnp (d, x, g, maxit, upd1, ireset, bounds=FALSE, ipivot, argvec, sfun, ...)
p <- mres$p
# cat("p from first call to modlnp\n")
# print(p)
# tmp<-readline("cont.")
gtp <- mres$gtp
ncg1<-mres$ncg1
d <- mres$dnew
#% cat("d from modlnp:")
#% print(d)
ncg <- ncg + ncg1
while ( ! conv) {
#% cat("top while ")
#% tmp <- readline("Top of iteration")
oldg <- g
pnorm <- max(abs(p)) # norm(p,'inf')
oldf <- f
## ---------------------------------------------------------
## line search
## ---------------------------------------------------------
if (trace > 1) cat("start linesearch\n")
pe <- pnorm + eps
spe <- stepmx/pe
if (trace > 1) cat("gtp, spe:", gtp, spe,"\n")
alpha0 <- step1 (f, gtp, spe)
reslin <- lin1 (p, x, f, alpha0, g, sfun, ...)
## [x, f, g, nf1, ierror, alpha] <-
x <- reslin$xnew # need fixup
f <- reslin$fnew
g <- reslin$gnew
nf1 <- reslin$nf1
ierror <- reslin$ierror
alpha <- reslin$alpha1
# cat("after lin1, alpha=",alpha,"\n")
# tmp<-readline("cont.")
nf <- nf + nf1
## ---------------------------------------------------------
nit <- nit + 1
# if (is.null(g) ) { ## 160922 change
if (! is.numeric(g) ) { # try fix 180328
gnorm <- 1.0/eps
} else { gnorm <- max(abs(g)) } ## norm(g,'inf')
### Display info
if (trace > 0) cat("Itn ",nit," ",nf," ",ncg, " ",f, " ", gnorm,"\n")
if (ierror == 3) {
if (length(ncg) == 0) { ncg <- 0 } # ?? is.null(ncg)??
xstar <- x
almqn<-list(xstar=xstar, f=f, g=g, ierror=ierror, nfngr=ncg)
return(almqn)
}
## ---------------------------------------------------------
## stop if more than maxfun evalutations have been made
## ---------------------------------------------------------
if (nf >= maxfun) {
ierror <- 2
xstar <- x
almqn<-list(xstar=xstar, f=f, g=g, ierror=ierror, nfngr=ncg)
return(almqn)
}
## ---------------------------------------------------------
## set up for convergence and resetting tests
## ---------------------------------------------------------
ftest <- 1 + abs(f)
xnorm <- max(abs(x)) # norm(x,'inf')
difold <- difnew
difnew <- oldf - f
envjn$yk <- g - oldg
envjn$sk <- alpha*p
if (icycle == 1) {
if (difnew > 2*difold) { epsred <- 2*epsred }
if (difnew < 0.5*difold) { epsred <- 0.5*epsred }
}
## ---------------------------------------------------------
## convergence test
## ---------------------------------------------------------
conv <- ( ( (alpha*pnorm < toleps*(1 + xnorm)) &&
(abs(difnew) < rtleps*ftest) &&
(gnorm < accrcy^(1/3)*ftest) )||
( gnorm < .01*sqrt(accrcy)*ftest ) )
if (conv) {
ierror <- 0
xstar <- x
almqn<-list(xstar=xstar, f=f, g=g, ierror=ierror, nfngr=ncg)
return(almqn)
}
## ---------------------------------------------------------
## update lmqn preconditioner
## ---------------------------------------------------------
envjn$yksk <- as.numeric(crossprod(envjn$yk, envjn$sk) )
ireset <- ((icycle == n-1) || (difnew < epsred*(fkeep-f)) )
if ( ! ireset) {
envjn$yrsr <- as.numeric(crossprod(envjn$yr, envjn$sr))
ireset <- (envjn$yrsr <= 0)
}
upd1 <- (envjn$yksk <= 0)
## ---------------------------------------------------------
## compute search direction
## ---------------------------------------------------------
argvec <- c(accrcy, gnorm, xnorm)
## cat("ireset =", ireset," upd1=",upd1,"\n")
#% cat("New d to modlnp:")
#% print(d)
## [p, gtp, ncg1, d] <- ...
mres <- modlnp (d, x, g, maxit, upd1, ireset, 0, ipivot, argvec, sfun, ...)
p <- mres$p
gtp <- mres$gtp
ncg1<-mres$ncg1
d <- mres$dnew
#% cat("returned dnew, envjn$john: ", envjn$john)
#% print(d)
ncg <- ncg + ncg1
## ---------------------------------------------------------
## store information for lmqn preconditioner
## ---------------------------------------------------------
if (ireset) {
envjn$sr <- envjn$sk
envjn$yr <- envjn$yk
fkeep <- f
icycle <- 1
} else {
envjn$sr <- envjn$sr + envjn$sk
envjn$yr <- envjn$yr + envjn$yk
icycle <- icycle + 1
}
} # end while
## [xstar, f, g, ierror] = ..
almqn<-list(xstar=xstar, f=f, g=g, ierror=ierror, nfngr=ncg)
} # end lmqn
modlnp <- function(d, x, g, maxit, upd1, ireset, bounds,
ipivot, argvec, sfun, ...) {
##---------------------------------------------------------
## this routine performs a preconditioned conjugate-gradient
## iteration to solve the Newton equations for a search
## direction for a truncated-newton algorithm.
## When the value of the quadratic model is sufficiently
## reduced, the iteration is terminated.
##---------------------------------------------------------
## parameters
##
## p - computed search direction
## g - current gradient
## gv,gz1,v - scratch vectors
## r - residual
## d - diagonal preconditoning matrix
## feval - value of quadratic function
##------------------------------------------------------------
## initialization
##------------------------------------------------------------
if (is.null(d)) stop("Null d")
## print(x)
## print(g)
## cat(bounds,"\n")
accrcy <- argvec[1]
gnorm <- argvec[2]
xnorm <- argvec[3]
if (maxit == 0) {
p <- -g
gtp <- as.numeric(crossprod(p, g))
ncg1 <- 1
dnew <- d
if (sqrt(sum(p^2))==0) {
## cat("MODLNP 01: |p| = 0\n")
## pause(1)
}
#% cat("modlnp - dout01:")
#% print(dnew)
result<-list(p=p, gtp=gtp, ncg1=ncg1, dnew=dnew)
return(result)
}
first <- 1
tol <- 1e-6 ######### was 1.d-12 #########
qold <- 0
ainit <- initpc (d, upd1, ireset)
dnew<-ainit$td
#% cat("after initpc dnew:")
#% print(dnew)
r <- -g
v <- rep(0, length(r))
p <- v
gtp <- as.numeric(crossprod(p, g))
rho <- rep(0, maxit+1)
beta <- rep(0, maxit)
v.gv <- rep(0, maxit)
rho[[1]] <- as.numeric(crossprod(r))
##------------------------------------------------------------
## main iteration (conjugate-gradient iterations for Ax = b)
##------------------------------------------------------------
ind <- 0
ncg1 <- 0
for (k in 1:maxit) {
ncg1 <- ncg1 + 1
if (bounds) { r <- ztime(r, ipivot) }
amsolve <- msolve (r, upd1, ireset, first, d)
zk<-amsolve$y
if (bounds) { zk <- ztime (zk, ipivot) }
#% cat("r:")
#% print(r)
#% cat("zk:")
#% print(zk)
rz <- as.numeric(crossprod(r,zk))
if (rz/gnorm < tol) {
ind <- 80
if (sqrt(sum(p^2))==0) {
p <- -g
gtp <- as.numeric(crossprod(p, g))
}
#% cat("modlnp - dout - ind80:")
#% print(dnew)
result<-list(p=p, gtp=gtp, ncg1=ncg1, dnew=dnew)
return(result)
}
if (k > 1) {
beta[k] <- rz/rzold
} else {
beta[k] <- 0
}
#% cat("beta[",k,"] =",beta[k],"\n")
v <- zk + beta[k]*v
if (bounds) { v <- ztime( v, ipivot) }
## cat("about to call gtims\n")
gv <- gtims(v, x, g, accrcy, xnorm, sfun, ...)
#% cat("After gtims: gv=")
#% print(gv)
## cat(bounds,"\n")
if (bounds) { gv <- ztime (gv, ipivot) }
#% cat("gv, v:")
## cat("gv=", gv, "\n")
#% print(gv)
#% print(v)
v.gv[[k]] <- as.numeric(crossprod(v, gv)) ## ?? v.gv has underscore!! ??
#% cat("v.gv[[",k,"]]=",v.gv[[k]],"\n")
if (v.gv[[k]]/gnorm < tol) {
ind <- 50
if (sqrt(sum(p^2))==0) {
p <- -g ## Mod SGN 140912
if (bounds) {
p <- ztime(p, ipivot)
}
gtp <- crossprod(p, g)
## cat("MODLNP 03: |p| = 0 \n")
## pause(1)
}
#% cat("modlnp - dout03:")
#% print(dnew)
result<-list(p=p, gtp=gtp, ncg1=ncg1, dnew=dnew)
return(result)
}
dnew <- ndia3(dnew, v, gv, r, v.gv[[k]]) # NB need to return something but it doesn't get used
#% cat("after ndia3 below dout03, dnew:")
#% print(dnew)
##------------------------------------------------------------
## compute current solution and related vectors
##------------------------------------------------------------
alpha <- rz / v.gv[[k]]
p <- p + alpha* v
r <- r - alpha*gv
rho[[k+1]] <- as.numeric(crossprod(r))
##------------------------------------------------------------
## test for convergence
##------------------------------------------------------------
gtp <- as.numeric(crossprod(p,g))
pr <- as.numeric(crossprod(r,p))
q <- (gtp + pr) / 2
qtest <- k * (1 - qold/q)
if (qtest <= 0.5) {
if (sqrt(sum(p^2))==0) {
## cat("MODLNP 04: |p| = 0\n")
## pause(1)
}
#% cat("modlnp - dout04:")
#% print(dnew)
result<-list(p=p, gtp=gtp, ncg1=ncg1, dnew=dnew)
return(result)
}
qold <- q
##------------------------------------------------------------
## perform cautionary test
##------------------------------------------------------------
if (gtp > 0) {
ind <- 40
if (sqrt(sum(p^2))==0) {
## cat("MODLNP 05: |p| = 0 \n")
## pause(1)
}
#% cat("modlnp - dout05:")
#% print(dnew)
result<-list(p=p, gtp=gtp, ncg1=ncg1, dnew=dnew)
return(result)
}
rzold <- rz
} ## end loop over k
k <- k-1
##------------------------------------------------------------
## terminate algorithm
##------------------------------------------------------------
if (ind == 40) {
p <- p - alpha*v
}
##------------------------------------------------------------
if (ind == 50 && k <= 1) {
amsolve <- msolve (g, upd1, ireset, first, d)
p<-amsolve$y
p <- -p
if (bounds) { p <- ztime (p, ipivot) }
}
##------------------------------------------------------------
if (ind == 80 && k <= 1) {
p <- -g
if (bounds) { p <- ztime (p, ipivot) }
}
##------------------------------------------------------------
## store new diagonal preconditioner
##------------------------------------------------------------
gtp <- as.numeric(crossprod(p, g))
ncg1 <- k + 1
if (sqrt(sum(p^2))==0) {
## cat("MODLNP 06: |p| = 0 \n")
## pause(1)
}
## cat("modlnp - dout06:")
#% print(dnew)
result<-list(p=p, gtp=gtp, ncg1=ncg1, dnew=dnew)
return(result)
}
modz <- function (x, p, ipivot, low, up, flast, f, alpha){
##---------------------------------------------------------------------
## update the constraint matrix if a new constraint is encountered
##---------------------------------------------------------------------
eps <- .Machine$double.eps
indl <- which(ipivot == 0 & p < 0)
if (length(indl) > 0) {
toll <- 10 * eps * (abs(low[indl]) + 1)
hitl <- which(x[indl]-low[indl] <= toll)
if (length(hitl) > 0) {
flast <- f
ipivot[indl[hitl]] <- -1
}
}
##---------------------------------------------------------------------
indu <- which((ipivot == 0) & (p > 0));
if (length(indu) > 0) {
tolu <- 10 * eps * (abs( up[indu]) + 1)
hitu <- which(up[indu]-x[indu] <= tolu)
if (length(hitu) > 0) {
flast <- f
ipivot[indu[hitu]] <- 1
}
}
##---------------------------------------------------------------------
flast1 <- flast
ipivot1 <- ipivot
list(flast1 = flast, ipivot1=ipivot)
}
msolve <- function(g, upd1, ireset, first, d) {
##---------------------------------------------------------
## This routine acts as a preconditioning step for the
## linear conjugate-gradient routine. It is also the
## method of computing the search direction from the
## gradient for the non-linear conjugate-gradient code.
## It represents a two-step self-scaled bfgs formula.
##---------------------------------------------------------
#% cat("msolve, envjn$john=",envjn$john,"\n")
envjn$john<-"msolve"
if (upd1) {
#% cat("upd1 is TRUE\n")
y <- g/d
} else {
gsk <- as.numeric(crossprod(g, envjn$sk))
if (ireset) {
envjn$hg <- g/d
if (first) {
envjn$hyk <- envjn$yk/d
ykhyk <- as.numeric(crossprod(envjn$yk, envjn$hyk))
}
ghyk <- as.numeric(crossprod(g, envjn$hyk))
y <- ssbfgs(envjn$sk,envjn$hg,envjn$hyk,envjn$yksk,ykhyk,gsk,ghyk)
} else {
envjn$hg <- g/d
if (first) {
envjn$hyk <- envjn$yk/d
envjn$hyr <- envjn$yr/d
envjn$yksr <- as.numeric(crossprod(envjn$yk, envjn$sr))
ykhyr <- as.numeric(crossprod(envjn$yk, envjn$hyr))
}
gsr <- as.numeric(crossprod(g, envjn$sr))
ghyr <- as.numeric(crossprod(g, envjn$hyr))
if (first) {
yrhyr <- as.numeric(crossprod(envjn$yr, envjn$hyr))
}
envjn$hg <- ssbfgs(envjn$sr,envjn$hg,envjn$hyr,envjn$yrsr,yrhyr,gsr,ghyr)
if (first) {
envjn$hyk <- ssbfgs(envjn$sr,envjn$hyk,envjn$hyr,envjn$yrsr,yrhyr,envjn$yksr,ykhyr)
}
ykhyk <- as.numeric(crossprod(envjn$hyk, envjn$yk))
ghyk <- as.numeric(crossprod(envjn$hyk, g))
y <- ssbfgs(envjn$sk,envjn$hg,envjn$hyk,envjn$yksk,ykhyk,gsk,ghyk)
}
}
amsolve<-list(y=y) ## returns y
}
ndia3<-function(e, v, gv, r, vgv){
##---------------------------------------------------------
## update the preconditioning matrix based on a diagonal
## version of the bfgs quasi-newton update.
##---------------------------------------------------------
tol <- 1e-6
vr <- as.numeric(crossprod(v,r))
#% cat("ndia3: vgv, vr, then e:",vgv, vr,"\n")
#% print(e)
if (abs(vr)>tol && abs(vgv)>tol) {
e <- e - (r*r)/vr + (gv*gv)/vgv # CAUTION! May be crossprod
ind <- which(e < tol)
if (length(ind)>0){
e[ind] <- 1
}
}
#% print(e)
e # Need to return the object?? Possibly not!
}
norm2 <- function (x) {
##---------------------------------------------------------
## compute the 2 norm of vector x
##---------------------------------------------------------
res<-sqrt(as.numeric(crossprod(x)))
}
ssbfgs <- function (s, hv, hy, ys, yhy, vs, vhy){
##---------------------------------------------------------
## self-scaled bfgs quasi-Newton update
## (used by preconditioner)
##---------------------------------------------------------
delta <- (1 + yhy/ys)*vs/ys - vhy/ys
beta <- -vs/ys
z <- hv + delta*s + beta*hy
}
step1 <- function(f, gtp, smax) {
##---------------------------------------------------------
## step1 returns the length of the initial step to be
## taken along the vector p in the next linear search.
##---------------------------------------------------------
## [fm is supposed to be an estimate of the optimal function value]
eps<-.Machine$double.eps
fm <- 0
d <- abs(f-fm)
alpha <- min(1, smax)
if ((2*d <= (-gtp)) && (d >= eps)) {
alpha = min(-2*d/gtp, smax)
}
alpha # need to ensure returned value
}
stpmax <- function(stepmx, pe, x, p, ipivot, low, up) {
##------------------------------------------------
## compute the maximum allowable step length
## (spe is the standard (unconstrained) max step)
##------------------------------------------------
## cat("stpmax: stepmx, pe:",stepmx, pe,"\n")
spe <- stepmx / pe
al <- spe
au <- spe
##------------------------------------------------
indl <- which(ipivot==0 & p < 0)
if ( length(indl)>0) {
tl <- low[indl] - x[indl]
al <- min(tl/p[indl])
}
##------------------------------------------------
indu <- which(ipivot==0 & p > 0)
if (length(indu)>0) {
tu <- up[indu] - x[indu]
au <- min(tu/p[indu])
}
##------------------------------------------------
spe <- min(c(spe, al, au))
}
ztime<-function(x,ipivot) {
## ---------------------------------------------------------
## this routine multiplies the vector x by the
## constraint matrix z
## ---------------------------------------------------------
ind <- which(ipivot!=0);
x[ind] <- 0
x1 <- x;
}
|