/usr/include/trilinos/ROL_EqualityConstraint_SimOpt.hpp is in libtrilinos-rol-dev 12.10.1-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 | // @HEADER
// ************************************************************************
//
// Rapid Optimization Library (ROL) Package
// Copyright (2014) Sandia Corporation
//
// Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
// license for use of this work by or on behalf of the U.S. Government.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the Corporation nor the names of the
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Questions? Contact lead developers:
// Drew Kouri (dpkouri@sandia.gov) and
// Denis Ridzal (dridzal@sandia.gov)
//
// ************************************************************************
// @HEADER
#ifndef ROL_EQUALITY_CONSTRAINT_SIMOPT_H
#define ROL_EQUALITY_CONSTRAINT_SIMOPT_H
#include "ROL_EqualityConstraint.hpp"
#include "ROL_Vector_SimOpt.hpp"
#include "ROL_Types.hpp"
#include <iostream>
/** @ingroup func_group
\class ROL::EqualityConstraint_SimOpt
\brief Defines the equality constraint operator interface for simulation-based optimization.
This equality constraint interface inherits from ROL_EqualityConstraint, for the
use case when \f$\mathcal{X}=\mathcal{U}\times\mathcal{Z}\f$ where \f$\mathcal{U}\f$ and
\f$\mathcal{Z}\f$ are Banach spaces. \f$\mathcal{U}\f$ denotes the "simulation space"
and \f$\mathcal{Z}\f$ denotes the "optimization space" (of designs, controls, parameters).
The simulation-based constraints are of the form
\f[
c(u,z) = 0 \,.
\f]
The basic operator interface, to be implemented by the user, requires:
\li #value -- constraint evaluation.
\li #applyJacobian_1 -- action of the partial constraint Jacobian --derivatives are
with respect to the first component \f$\mathcal{U}\f$;
\li #applyJacobian_2 -- action of the partial constraint Jacobian --derivatives are
with respect to the second component \f$\mathcal{Z}\f$;
\li #applyAdjointJacobian_1 -- action of the adjoint of the partial constraint Jacobian --derivatives are
with respect to the first component \f$\mathcal{U}\f$;
\li #applyAdjointJacobian_2 -- action of the adjoint of the partial constraint Jacobian --derivatives are
with respect to the second component \f$\mathcal{Z}\f$;
The user may also overload:
\li #applyAdjointHessian_11 -- action of the adjoint of the partial constraint Hessian --derivatives
are with respect to the first component only;
\li #applyAdjointHessian_12 -- action of the adjoint of the partial constraint Hessian --derivatives
are with respect to the first and second components;
\li #applyAdjointHessian_21 -- action of the adjoint of the partial constraint Hessian --derivatives
are with respect to the second and first components;
\li #applyAdjointHessian_22 -- action of the adjoint of the partial constraint Hessian --derivatives
are with respect to the second component only;
\li #solveAugmentedSystem -- solution of the augmented system --the default is an iterative
scheme based on the action of the Jacobian and its adjoint.
\li #applyPreconditioner -- action of a constraint preconditioner --the default is null-op.
---
*/
namespace ROL {
template <class Real>
class EqualityConstraint_SimOpt : public virtual EqualityConstraint<Real> {
private:
// Additional vector storage for solve
Teuchos::RCP<Vector<Real> > unew_;
Teuchos::RCP<Vector<Real> > jv_;
// Default parameters for solve (backtracking Newton)
const Real DEFAULT_atol_;
const Real DEFAULT_rtol_;
const Real DEFAULT_stol_;
const Real DEFAULT_factor_;
const Real DEFAULT_decr_;
const int DEFAULT_maxit_;
const bool DEFAULT_print_;
// User-set parameters for solve (backtracking Newton)
Real atol_;
Real rtol_;
Real stol_;
Real factor_;
Real decr_;
int maxit_;
bool print_;
// Flag to initialize vector storage in solve
bool firstSolve_;
public:
EqualityConstraint_SimOpt()
: EqualityConstraint<Real>(),
unew_(Teuchos::null), jv_(Teuchos::null),
DEFAULT_atol_(1.e-4*std::sqrt(ROL_EPSILON<Real>())),
DEFAULT_rtol_(1.e0),
DEFAULT_stol_(std::sqrt(ROL_EPSILON<Real>())),
DEFAULT_factor_(0.5),
DEFAULT_decr_(1.e-4),
DEFAULT_maxit_(500),
DEFAULT_print_(false),
atol_(DEFAULT_atol_), rtol_(DEFAULT_rtol_), stol_(DEFAULT_stol_), factor_(DEFAULT_factor_),
decr_(DEFAULT_decr_), maxit_(DEFAULT_maxit_), print_(DEFAULT_print_),
firstSolve_(true) {}
/** \brief Update constraint functions.
x is the optimization variable,
flag = true if optimization variable is changed,
iter is the outer algorithm iterations count.
*/
virtual void update( const Vector<Real> &u, const Vector<Real> &z, bool flag = true, int iter = -1 ) {
update_1(u,flag,iter);
update_2(z,flag,iter);
}
/** \brief Update constraint functions with respect to Sim variable.
x is the optimization variable,
flag = true if optimization variable is changed,
iter is the outer algorithm iterations count.
*/
virtual void update_1( const Vector<Real> &u, bool flag = true, int iter = -1 ) {}
/** \brief Update constraint functions with respect to Opt variable.
x is the optimization variable,
flag = true if optimization variable is changed,
iter is the outer algorithm iterations count.
*/
virtual void update_2( const Vector<Real> &z, bool flag = true, int iter = -1 ) {}
/** \brief Evaluate the constraint operator \f$c:\mathcal{U}\times\mathcal{Z} \rightarrow \mathcal{C}\f$
at \f$(u,z)\f$.
@param[out] c is the result of evaluating the constraint operator at @b \f$(u,z)\f$; a constraint-space vector
@param[in] u is the constraint argument; a simulation-space vector
@param[in] z is the constraint argument; an optimization-space vector
@param[in,out] tol is a tolerance for inexact evaluations; currently unused
On return, \f$\mathsf{c} = c(u,z)\f$,
where \f$\mathsf{c} \in \mathcal{C}\f$, \f$\mathsf{u} \in \mathcal{U}\f$, and $\f$\mathsf{z} \in\mathcal{Z}\f$.
---
*/
virtual void value(Vector<Real> &c,
const Vector<Real> &u,
const Vector<Real> &z,
Real &tol) = 0;
/** \brief Given \f$z\f$, solve \f$c(u,z)=0\f$ for \f$u\f$.
@param[out] c is the result of evaluating the constraint operator at @b \f$(u,z)\f$; a constraint-space vector
@param[in,out] u is the solution vector; a simulation-space vector
@param[in] z is the constraint argument; an optimization-space vector
@param[in,out] tol is a tolerance for inexact evaluations; currently unused
The defualt implementation is Newton's method globalized with a backtracking line search.
---
*/
virtual void solve(Vector<Real> &c,
Vector<Real> &u,
const Vector<Real> &z,
Real &tol) {
if ( firstSolve_ ) {
unew_ = u.clone();
jv_ = u.clone();
firstSolve_ = false;
}
update(u,z,true);
value(c,u,z,tol);
Real cnorm = c.norm(), alpha = 1.0, tmp = 0.0;
const Real ctol = std::min(atol_, rtol_*cnorm);
int cnt = 0;
if ( print_ ) {
std::cout << "\n Default EqualityConstraint_SimOpt::solve\n";
std::cout << " ";
std::cout << std::setw(6) << std::left << "iter";
std::cout << std::setw(15) << std::left << "rnorm";
std::cout << std::setw(15) << std::left << "alpha";
std::cout << "\n";
}
for (cnt = 0; cnt < maxit_; ++cnt) {
// Compute Newton step
applyInverseJacobian_1(*jv_,c,u,z,tol);
unew_->set(u);
unew_->axpy(-alpha, *jv_);
update(*unew_,z);
value(c,*unew_,z,tol);
tmp = c.norm();
// Perform backtracking line search
while ( tmp > (1.0-decr_*alpha)*cnorm &&
alpha > stol_ ) {
alpha *= factor_;
unew_->set(u);
unew_->axpy(-alpha,*jv_);
update(*unew_,z);
value(c,*unew_,z,tol);
tmp = c.norm();
}
if ( print_ ) {
std::cout << " ";
std::cout << std::setw(6) << std::left << cnt;
std::cout << std::scientific << std::setprecision(6);
std::cout << std::setw(15) << std::left << tmp;
std::cout << std::scientific << std::setprecision(6);
std::cout << std::setw(15) << std::left << alpha;
std::cout << "\n";
}
// Update iterate
cnorm = tmp;
u.set(*unew_);
if (cnorm < ctol) {
break;
}
update(u,z,true);
alpha = 1.0;
}
}
/** \brief Set solve parameters.
@param[in] parlist Teuchos::ParameterList containing solve parameters
For the default implementation, parlist has two sublist ("SimOpt"
and "Solve") and the "Solve" sublist has six input parameters.
- "Residual Tolerance": Absolute tolerance for the norm of the residual (Real)
- "Iteration Limit": Maximum number of Newton iterations (int)
- "Sufficient Decrease Tolerance": Tolerance signifying sufficient decrease in the residual norm, between 0 and 1 (Real)
- "Step Tolerance": Absolute tolerance for the step size parameter (Real)
- "Backtracking Factor": Rate for decreasing step size during backtracking, between 0 and 1 (Real)
- "Output Iteration History": Set to true in order to print solve iteration history (bool)
These parameters are accessed as parlist.sublist("SimOpt").sublist("Solve").get(...).
---
*/
virtual void setSolveParameters(Teuchos::ParameterList &parlist) {
Teuchos::ParameterList & list = parlist.sublist("SimOpt").sublist("Solve");
atol_ = list.get("Absolute Residual Tolerance", DEFAULT_atol_);
rtol_ = list.get("Relative Residual Tolerance", DEFAULT_rtol_);
maxit_ = list.get("Iteration Limit", DEFAULT_maxit_);
decr_ = list.get("Sufficient Decrease Tolerance", DEFAULT_decr_);
stol_ = list.get("Step Tolerance", DEFAULT_stol_);
factor_ = list.get("Backtracking Factor", DEFAULT_factor_);
print_ = list.get("Output Iteration History", DEFAULT_print_);
}
/** \brief Apply the partial constraint Jacobian at \f$(u,z)\f$,
\f$c_u(u,z) \in L(\mathcal{U}, \mathcal{C})\f$,
to the vector \f$v\f$.
@param[out] jv is the result of applying the constraint Jacobian to @b v at @b \f$(u,z)\f$; a constraint-space vector
@param[in] v is a simulation-space vector
@param[in] u is the constraint argument; an simulation-space vector
@param[in] z is the constraint argument; an optimization-space vector
@param[in,out] tol is a tolerance for inexact evaluations; currently unused
On return, \f$\mathsf{jv} = c_u(u,z)v\f$, where
\f$v \in \mathcal{U}\f$, \f$\mathsf{jv} \in \mathcal{C}\f$.
---
*/
virtual void applyJacobian_1(Vector<Real> &jv,
const Vector<Real> &v,
const Vector<Real> &u,
const Vector<Real> &z,
Real &tol) {
Real ctol = std::sqrt(ROL_EPSILON<Real>());
// Compute step length
Real h = tol;
if (v.norm() > std::sqrt(ROL_EPSILON<Real>())) {
h = std::max(1.0,u.norm()/v.norm())*tol;
}
// Update state vector to u + hv
Teuchos::RCP<Vector<Real> > unew = u.clone();
unew->set(u);
unew->axpy(h,v);
// Compute new constraint value
update(*unew,z);
value(jv,*unew,z,ctol);
// Compute current constraint value
Teuchos::RCP<Vector<Real> > cold = jv.clone();
update(u,z);
value(*cold,u,z,ctol);
// Compute Newton quotient
jv.axpy(-1.0,*cold);
jv.scale(1.0/h);
}
/** \brief Apply the partial constraint Jacobian at \f$(u,z)\f$,
\f$c_z(u,z) \in L(\mathcal{Z}, \mathcal{C})\f$,
to the vector \f$v\f$.
@param[out] jv is the result of applying the constraint Jacobian to @b v at @b \f$(u,z)\f$; a constraint-space vector
@param[in] v is an optimization-space vector
@param[in] u is the constraint argument; a simulation-space vector
@param[in] z is the constraint argument; an optimization-space vector
@param[in,out] tol is a tolerance for inexact evaluations; currently unused
On return, \f$\mathsf{jv} = c_z(u,z)v\f$, where
\f$v \in \mathcal{Z}\f$, \f$\mathsf{jv} \in \mathcal{C}\f$.
---
*/
virtual void applyJacobian_2(Vector<Real> &jv,
const Vector<Real> &v,
const Vector<Real> &u,
const Vector<Real> &z,
Real &tol) {
Real ctol = std::sqrt(ROL_EPSILON<Real>());
// Compute step length
Real h = tol;
if (v.norm() > std::sqrt(ROL_EPSILON<Real>())) {
h = std::max(1.0,u.norm()/v.norm())*tol;
}
// Update state vector to u + hv
Teuchos::RCP<Vector<Real> > znew = z.clone();
znew->set(z);
znew->axpy(h,v);
// Compute new constraint value
update(u,*znew);
value(jv,u,*znew,ctol);
// Compute current constraint value
Teuchos::RCP<Vector<Real> > cold = jv.clone();
update(u,z);
value(*cold,u,z,ctol);
// Compute Newton quotient
jv.axpy(-1.0,*cold);
jv.scale(1.0/h);
}
/** \brief Apply the inverse partial constraint Jacobian at \f$(u,z)\f$,
\f$c_u(u,z)^{-1} \in L(\mathcal{C}, \mathcal{U})\f$,
to the vector \f$v\f$.
@param[out] ijv is the result of applying the inverse constraint Jacobian to @b v at @b \f$(u,z)\f$; a simulation-space vector
@param[in] v is a constraint-space vector
@param[in] u is the constraint argument; a simulation-space vector
@param[in] z is the constraint argument; an optimization-space vector
@param[in,out] tol is a tolerance for inexact evaluations; currently unused
On return, \f$\mathsf{ijv} = c_u(u,z)^{-1}v\f$, where
\f$v \in \mathcal{C}\f$, \f$\mathsf{ijv} \in \mathcal{U}\f$.
---
*/
virtual void applyInverseJacobian_1(Vector<Real> &ijv,
const Vector<Real> &v,
const Vector<Real> &u,
const Vector<Real> &z,
Real &tol) {
TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error,
"The method applyInverseJacobian_1 is used but not implemented!\n");
}
/** \brief Apply the adjoint of the partial constraint Jacobian at \f$(u,z)\f$,
\f$c_u(u,z)^* \in L(\mathcal{C}^*, \mathcal{U}^*)\f$,
to the vector \f$v\f$. This is the primary interface.
@param[out] ajv is the result of applying the adjoint of the constraint Jacobian to @b v at @b (u,z); a dual simulation-space vector
@param[in] v is a dual constraint-space vector
@param[in] u is the constraint argument; a simulation-space vector
@param[in] z is the constraint argument; an optimization-space vector
@param[in,out] tol is a tolerance for inexact evaluations; currently unused
On return, \f$\mathsf{ajv} = c_u(u,z)^*v\f$, where
\f$v \in \mathcal{C}^*\f$, \f$\mathsf{ajv} \in \mathcal{U}^*\f$.
---
*/
virtual void applyAdjointJacobian_1(Vector<Real> &ajv,
const Vector<Real> &v,
const Vector<Real> &u,
const Vector<Real> &z,
Real &tol) {
applyAdjointJacobian_1(ajv, v, u, z, v.dual(), tol);
}
/** \brief Apply the adjoint of the partial constraint Jacobian at \f$(u,z)\f$,
\f$c_u(u,z)^* \in L(\mathcal{C}^*, \mathcal{U}^*)\f$,
to the vector \f$v\f$. This is the secondary interface, for use
with dual spaces where the user does not define the dual() operation.
@param[out] ajv is the result of applying the adjoint of the constraint Jacobian to @b v at @b (u,z); a dual simulation-space vector
@param[in] v is a dual constraint-space vector
@param[in] u is the constraint argument; a simulation-space vector
@param[in] z is the constraint argument; an optimization-space vector
@param[in] dualv is a vector used for temporary variables; a constraint-space vector
@param[in,out] tol is a tolerance for inexact evaluations; currently unused
On return, \f$\mathsf{ajv} = c_u(u,z)^*v\f$, where
\f$v \in \mathcal{C}^*\f$, \f$\mathsf{ajv} \in \mathcal{U}^*\f$.
---
*/
virtual void applyAdjointJacobian_1(Vector<Real> &ajv,
const Vector<Real> &v,
const Vector<Real> &u,
const Vector<Real> &z,
const Vector<Real> &dualv,
Real &tol) {
Real ctol = std::sqrt(ROL_EPSILON<Real>());
Real h = tol;
if (v.norm() > std::sqrt(ROL_EPSILON<Real>())) {
h = std::max(1.0,u.norm()/v.norm())*tol;
}
Teuchos::RCP<Vector<Real> > cold = dualv.clone();
Teuchos::RCP<Vector<Real> > cnew = dualv.clone();
update(u,z);
value(*cold,u,z,ctol);
Teuchos::RCP<Vector<Real> > unew = u.clone();
ajv.zero();
for (int i = 0; i < u.dimension(); i++) {
unew->set(u);
unew->axpy(h,*(u.basis(i)));
update(*unew,z);
value(*cnew,*unew,z,ctol);
cnew->axpy(-1.0,*cold);
cnew->scale(1.0/h);
ajv.axpy(cnew->dot(v),*((u.dual()).basis(i)));
}
update(u,z);
}
/** \brief Apply the adjoint of the partial constraint Jacobian at \f$(u,z)\f$,
\f$c_z(u,z)^* \in L(\mathcal{C}^*, \mathcal{Z}^*)\f$,
to vector \f$v\f$. This is the primary interface.
@param[out] ajv is the result of applying the adjoint of the constraint Jacobian to @b v at @b \f$(u,z)\f$; a dual optimization-space vector
@param[in] v is a dual constraint-space vector
@param[in] u is the constraint argument; a simulation-space vector
@param[in] z is the constraint argument; an optimization-space vector
@param[in,out] tol is a tolerance for inexact evaluations; currently unused
On return, \f$\mathsf{ajv} = c_z(u,z)^*v\f$, where
\f$v \in \mathcal{C}^*\f$, \f$\mathsf{ajv} \in \mathcal{Z}^*\f$.
---
*/
virtual void applyAdjointJacobian_2(Vector<Real> &ajv,
const Vector<Real> &v,
const Vector<Real> &u,
const Vector<Real> &z,
Real &tol) {
applyAdjointJacobian_2(ajv, v, u, z, v.dual(), tol);
}
/** \brief Apply the adjoint of the partial constraint Jacobian at \f$(u,z)\f$,
\f$c_z(u,z)^* \in L(\mathcal{C}^*, \mathcal{Z}^*)\f$,
to vector \f$v\f$. This is the secondary interface, for use
with dual spaces where the user does not define the dual() operation.
@param[out] ajv is the result of applying the adjoint of the constraint Jacobian to @b v at @b \f$(u,z)\f$; a dual optimization-space vector
@param[in] v is a dual constraint-space vector
@param[in] u is the constraint argument; a simulation-space vector
@param[in] z is the constraint argument; an optimization-space vector
@param[in] dualv is a vector used for temporary variables; a constraint-space vector
@param[in,out] tol is a tolerance for inexact evaluations; currently unused
On return, \f$\mathsf{ajv} = c_z(u,z)^*v\f$, where
\f$v \in \mathcal{C}^*\f$, \f$\mathsf{ajv} \in \mathcal{Z}^*\f$.
---
*/
virtual void applyAdjointJacobian_2(Vector<Real> &ajv,
const Vector<Real> &v,
const Vector<Real> &u,
const Vector<Real> &z,
const Vector<Real> &dualv,
Real &tol) {
Real ctol = std::sqrt(ROL_EPSILON<Real>());
Real h = tol;
if (v.norm() > std::sqrt(ROL_EPSILON<Real>())) {
h = std::max(1.0,u.norm()/v.norm())*tol;
}
Teuchos::RCP<Vector<Real> > cold = dualv.clone();
Teuchos::RCP<Vector<Real> > cnew = dualv.clone();
update(u,z);
value(*cold,u,z,ctol);
Teuchos::RCP<Vector<Real> > znew = z.clone();
ajv.zero();
for (int i = 0; i < z.dimension(); i++) {
znew->set(z);
znew->axpy(h,*(z.basis(i)));
update(u,*znew);
value(*cnew,u,*znew,ctol);
cnew->axpy(-1.0,*cold);
cnew->scale(1.0/h);
ajv.axpy(cnew->dot(v),*((z.dual()).basis(i)));
}
update(u,z);
}
/** \brief Apply the inverse of the adjoint of the partial constraint Jacobian at \f$(u,z)\f$,
\f$c_u(u,z)^{-*} \in L(\mathcal{U}^*, \mathcal{C}^*)\f$,
to the vector \f$v\f$.
@param[out] iajv is the result of applying the inverse adjoint of the constraint Jacobian to @b v at @b (u,z); a dual constraint-space vector
@param[in] v is a dual simulation-space vector
@param[in] u is the constraint argument; a simulation-space vector
@param[in] z is the constraint argument; an optimization-space vector
@param[in,out] tol is a tolerance for inexact evaluations; currently unused
On return, \f$\mathsf{iajv} = c_u(u,z)^{-*}v\f$, where
\f$v \in \mathcal{U}^*\f$, \f$\mathsf{iajv} \in \mathcal{C}^*\f$.
---
*/
virtual void applyInverseAdjointJacobian_1(Vector<Real> &iajv,
const Vector<Real> &v,
const Vector<Real> &u,
const Vector<Real> &z,
Real &tol) {
TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error,
"The method applyInverseAdjointJacobian_1 is used but not implemented!\n");
};
/** \brief Apply the simulation-space derivative of the adjoint of the constraint
simulation-space Jacobian at \f$(u,z)\f$ to the vector \f$w\f$ in the
direction \f$v\f$, according to \f$v\mapsto c_{uu}(u,z)(v,\cdot)^*w\f$.
@param[out] ahwv is the result of applying the simulation-space derivative of the adjoint of the constraint simulation-space Jacobian at @b \f$(u,z)\f$ to the vector @b \f$w\f$ in direction @b \f$w\f$; a dual simulation-space vector
@param[in] w is the direction vector; a dual constraint-space vector
@param[in] v is a simulation-space vector
@param[in] u is the constraint argument; a simulation-space vector
@param[in] z is the constraint argument; an optimization-space vector
@param[in,out] tol is a tolerance for inexact evaluations; currently unused
On return, \f$\mathsf{ahwv} = c_{uu}(u,z)(v,\cdot)^*w\f$, where
\f$w \in \mathcal{C}^*\f$, \f$v \in \mathcal{U}\f$, and
\f$\mathsf{ahwv} \in \mathcal{U}^*\f$.
---
*/
virtual void applyAdjointHessian_11(Vector<Real> &ahwv,
const Vector<Real> &w,
const Vector<Real> &v,
const Vector<Real> &u,
const Vector<Real> &z,
Real &tol) {
Real jtol = std::sqrt(ROL_EPSILON<Real>());
// Compute step size
Real h = tol;
if (v.norm() > std::sqrt(ROL_EPSILON<Real>())) {
h = std::max(1.0,u.norm()/v.norm())*tol;
}
// Evaluate Jacobian at new state
Teuchos::RCP<Vector<Real> > unew = u.clone();
unew->set(u);
unew->axpy(h,v);
update(*unew,z);
applyAdjointJacobian_1(ahwv,w,*unew,z,jtol);
// Evaluate Jacobian at old state
Teuchos::RCP<Vector<Real> > jv = ahwv.clone();
update(u,z);
applyAdjointJacobian_1(*jv,w,u,z,jtol);
// Compute Newton quotient
ahwv.axpy(-1.0,*jv);
ahwv.scale(1.0/h);
}
/** \brief Apply the optimization-space derivative of the adjoint of the constraint
simulation-space Jacobian at \f$(u,z)\f$ to the vector \f$w\f$ in the
direction \f$v\f$, according to \f$v\mapsto c_{uz}(u,z)(v,\cdot)^*w\f$.
@param[out] ahwv is the result of applying the optimization-space derivative of the adjoint of the constraint simulation-space Jacobian at @b \f$(u,z)\f$ to the vector @b \f$w\f$ in direction @b \f$w\f$; a dual optimization-space vector
@param[in] w is the direction vector; a dual constraint-space vector
@param[in] v is a simulation-space vector
@param[in] u is the constraint argument; a simulation-space vector
@param[in] z is the constraint argument; an optimization-space vector
@param[in,out] tol is a tolerance for inexact evaluations; currently unused
On return, \f$\mathsf{ahwv} = c_{uz}(u,z)(v,\cdot)^*w\f$, where
\f$w \in \mathcal{C}^*\f$, \f$v \in \mathcal{U}\f$, and
\f$\mathsf{ahwv} \in \mathcal{Z}^*\f$.
---
*/
virtual void applyAdjointHessian_12(Vector<Real> &ahwv,
const Vector<Real> &w,
const Vector<Real> &v,
const Vector<Real> &u,
const Vector<Real> &z,
Real &tol) {
Real jtol = std::sqrt(ROL_EPSILON<Real>());
// Compute step size
Real h = tol;
if (v.norm() > std::sqrt(ROL_EPSILON<Real>())) {
h = std::max(1.0,u.norm()/v.norm())*tol;
}
// Evaluate Jacobian at new state
Teuchos::RCP<Vector<Real> > unew = u.clone();
unew->set(u);
unew->axpy(h,v);
update(*unew,z);
applyAdjointJacobian_2(ahwv,w,*unew,z,jtol);
// Evaluate Jacobian at old state
Teuchos::RCP<Vector<Real> > jv = ahwv.clone();
update(u,z);
applyAdjointJacobian_2(*jv,w,u,z,jtol);
// Compute Newton quotient
ahwv.axpy(-1.0,*jv);
ahwv.scale(1.0/h);
}
/** \brief Apply the simulation-space derivative of the adjoint of the constraint
optimization-space Jacobian at \f$(u,z)\f$ to the vector \f$w\f$ in the
direction \f$v\f$, according to \f$v\mapsto c_{zu}(u,z)(v,\cdot)^*w\f$.
@param[out] ahwv is the result of applying the simulation-space derivative of the adjoint of the constraint optimization-space Jacobian at @b \f$(u,z)\f$ to the vector @b \f$w\f$ in direction @b \f$w\f$; a dual simulation-space vector
@param[in] w is the direction vector; a dual constraint-space vector
@param[in] v is a optimization-space vector
@param[in] u is the constraint argument; a simulation-space vector
@param[in] z is the constraint argument; an optimization-space vector
@param[in,out] tol is a tolerance for inexact evaluations; currently unused
On return, \f$\mathsf{ahwv} = c_{zu}(u,z)(v,\cdot)^*w\f$, where
\f$w \in \mathcal{C}^*\f$, \f$v \in \mathcal{Z}\f$, and
\f$\mathsf{ahwv} \in \mathcal{U}^*\f$.
---
*/
virtual void applyAdjointHessian_21(Vector<Real> &ahwv,
const Vector<Real> &w,
const Vector<Real> &v,
const Vector<Real> &u,
const Vector<Real> &z,
Real &tol) {
Real jtol = std::sqrt(ROL_EPSILON<Real>());
// Compute step size
Real h = tol;
if (v.norm() > std::sqrt(ROL_EPSILON<Real>())) {
h = std::max(1.0,u.norm()/v.norm())*tol;
}
// Evaluate Jacobian at new control
Teuchos::RCP<Vector<Real> > znew = z.clone();
znew->set(z);
znew->axpy(h,v);
update(u,*znew);
applyAdjointJacobian_1(ahwv,w,u,*znew,jtol);
// Evaluate Jacobian at old control
Teuchos::RCP<Vector<Real> > jv = ahwv.clone();
update(u,z);
applyAdjointJacobian_1(*jv,w,u,z,jtol);
// Compute Newton quotient
ahwv.axpy(-1.0,*jv);
ahwv.scale(1.0/h);
}
/** \brief Apply the optimization-space derivative of the adjoint of the constraint
optimization-space Jacobian at \f$(u,z)\f$ to the vector \f$w\f$ in the
direction \f$v\f$, according to \f$v\mapsto c_{zz}(u,z)(v,\cdot)^*w\f$.
@param[out] ahwv is the result of applying the optimization-space derivative of the adjoint of the constraint optimization-space Jacobian at @b \f$(u,z)\f$ to the vector @b \f$w\f$ in direction @b \f$w\f$; a dual optimization-space vector
@param[in] w is the direction vector; a dual constraint-space vector
@param[in] v is a optimization-space vector
@param[in] u is the constraint argument; a simulation-space vector
@param[in] z is the constraint argument; an optimization-space vector
@param[in,out] tol is a tolerance for inexact evaluations; currently unused
On return, \f$\mathsf{ahwv} = c_{zz}(u,z)(v,\cdot)^*w\f$, where
\f$w \in \mathcal{C}^*\f$, \f$v \in \mathcal{Z}\f$, and
\f$\mathsf{ahwv} \in \mathcal{Z}^*\f$.
---
*/
virtual void applyAdjointHessian_22(Vector<Real> &ahwv,
const Vector<Real> &w,
const Vector<Real> &v,
const Vector<Real> &u,
const Vector<Real> &z,
Real &tol) {
Real jtol = std::sqrt(ROL_EPSILON<Real>());
// Compute step size
Real h = tol;
if (v.norm() > std::sqrt(ROL_EPSILON<Real>())) {
h = std::max(1.0,u.norm()/v.norm())*tol;
}
// Evaluate Jacobian at new control
Teuchos::RCP<Vector<Real> > znew = z.clone();
znew->set(z);
znew->axpy(h,v);
update(u,*znew);
applyAdjointJacobian_2(ahwv,w,u,*znew,jtol);
// Evaluate Jacobian at old control
Teuchos::RCP<Vector<Real> > jv = ahwv.clone();
update(u,z);
applyAdjointJacobian_2(*jv,w,u,z,jtol);
// Compute Newton quotient
ahwv.axpy(-1.0,*jv);
ahwv.scale(1.0/h);
}
/** \brief Approximately solves the <em> augmented system </em>
\f[
\begin{pmatrix}
I & c'(x)^* \\
c'(x) & 0
\end{pmatrix}
\begin{pmatrix}
v_{1} \\
v_{2}
\end{pmatrix}
=
\begin{pmatrix}
b_{1} \\
b_{2}
\end{pmatrix}
\f]
where \f$v_{1} \in \mathcal{X}\f$, \f$v_{2} \in \mathcal{C}^*\f$,
\f$b_{1} \in \mathcal{X}^*\f$, \f$b_{2} \in \mathcal{C}\f$,
\f$I : \mathcal{X} \rightarrow \mathcal{X}^*\f$ is an identity
operator, and \f$0 : \mathcal{C}^* \rightarrow \mathcal{C}\f$
is a zero operator.
@param[out] v1 is the optimization-space component of the result
@param[out] v2 is the dual constraint-space component of the result
@param[in] b1 is the dual optimization-space component of the right-hand side
@param[in] b2 is the constraint-space component of the right-hand side
@param[in] x is the constraint argument; an optimization-space vector
@param[in,out] tol is the nominal relative residual tolerance
On return, \f$ [\mathsf{v1} \,\, \mathsf{v2}] \f$ approximately
solves the augmented system, where the size of the residual is
governed by special stopping conditions. \n\n
The default implementation is the preconditioned generalized
minimal residual (GMRES) method, which enables the use of
nonsymmetric preconditioners.
---
*/
virtual std::vector<Real> solveAugmentedSystem(Vector<Real> &v1,
Vector<Real> &v2,
const Vector<Real> &b1,
const Vector<Real> &b2,
const Vector<Real> &x,
Real &tol) {
return EqualityConstraint<Real>::solveAugmentedSystem(v1,v2,b1,b2,x,tol);
}
/** \brief Apply a constraint preconditioner at \f$x\f$, \f$P(x) \in L(\mathcal{C}, \mathcal{C})\f$,
to vector \f$v\f$. In general, this preconditioner satisfies the following relationship:
\f[
c'(x) c'(x)^* P(x) v \approx v \,.
\f]
It is used by the #solveAugmentedSystem method.
@param[out] pv is the result of applying the constraint preconditioner to @b v at @b x; a constraint-space vector
@param[in] v is a constraint-space vector
@param[in] x is the preconditioner argument; an optimization-space vector
@param[in,out] tol is a tolerance for inexact evaluations
On return, \f$\mathsf{pv} = P(x)v\f$, where
\f$v \in \mathcal{C}\f$, \f$\mathsf{pv} \in \mathcal{C}\f$. \n\n
The default implementation is a null-op.
---
*/
virtual void applyPreconditioner(Vector<Real> &pv,
const Vector<Real> &v,
const Vector<Real> &x,
const Vector<Real> &g,
Real &tol) {
const Vector_SimOpt<Real> &xs = Teuchos::dyn_cast<const Vector_SimOpt<Real> >(x);
Teuchos::RCP<ROL::Vector<Real> > ijv = (xs.get_1())->clone();
try {
applyInverseJacobian_1(*ijv, v, *(xs.get_1()), *(xs.get_2()), tol);
}
catch (const std::logic_error &e) {
EqualityConstraint<Real>::applyPreconditioner(pv, v, x, g, tol);
return;
}
const Vector_SimOpt<Real> &gs = Teuchos::dyn_cast<const Vector_SimOpt<Real> >(g);
Teuchos::RCP<ROL::Vector<Real> > ijv_dual = (gs.get_1())->clone();
ijv_dual->set(ijv->dual());
try {
applyInverseAdjointJacobian_1(pv, *ijv_dual, *(xs.get_1()), *(xs.get_2()), tol);
}
catch (const std::logic_error &e) {
EqualityConstraint<Real>::applyPreconditioner(pv, v, x, g, tol);
return;
}
}
//
// EqualityConstraint_SimOpt(void) : EqualityConstraint<Real>() {}
/** \brief Update constraint functions.
x is the optimization variable,
flag = true if optimization variable is changed,
iter is the outer algorithm iterations count.
*/
virtual void update( const Vector<Real> &x, bool flag = true, int iter = -1 ) {
const Vector_SimOpt<Real> &xs = Teuchos::dyn_cast<const Vector_SimOpt<Real> >(
Teuchos::dyn_cast<const Vector<Real> >(x));
update(*(xs.get_1()),*(xs.get_2()),flag,iter);
}
/** \brief Check if the vector, v, is feasible
*/
virtual bool isFeasible( const Vector<Real> &v ) { return true; }
virtual void value(Vector<Real> &c,
const Vector<Real> &x,
Real &tol) {
const Vector_SimOpt<Real> &xs = Teuchos::dyn_cast<const Vector_SimOpt<Real> >(
Teuchos::dyn_cast<const Vector<Real> >(x));
value(c,*(xs.get_1()),*(xs.get_2()),tol);
}
virtual void applyJacobian(Vector<Real> &jv,
const Vector<Real> &v,
const Vector<Real> &x,
Real &tol) {
const Vector_SimOpt<Real> &xs = Teuchos::dyn_cast<const Vector_SimOpt<Real> >(
Teuchos::dyn_cast<const Vector<Real> >(x));
const Vector_SimOpt<Real> &vs = Teuchos::dyn_cast<const Vector_SimOpt<Real> >(
Teuchos::dyn_cast<const Vector<Real> >(v));
applyJacobian_1(jv,*(vs.get_1()),*(xs.get_1()),*(xs.get_2()),tol);
Teuchos::RCP<Vector<Real> > jv2 = jv.clone();
applyJacobian_2(*jv2,*(vs.get_2()),*(xs.get_1()),*(xs.get_2()),tol);
jv.plus(*jv2);
}
virtual void applyAdjointJacobian(Vector<Real> &ajv,
const Vector<Real> &v,
const Vector<Real> &x,
Real &tol) {
Vector_SimOpt<Real> &ajvs = Teuchos::dyn_cast<Vector_SimOpt<Real> >(
Teuchos::dyn_cast<Vector<Real> >(ajv));
const Vector_SimOpt<Real> &xs = Teuchos::dyn_cast<const Vector_SimOpt<Real> >(
Teuchos::dyn_cast<const Vector<Real> >(x));
Teuchos::RCP<Vector<Real> > ajv1 = (ajvs.get_1())->clone();
applyAdjointJacobian_1(*ajv1,v,*(xs.get_1()),*(xs.get_2()),tol);
ajvs.set_1(*ajv1);
Teuchos::RCP<Vector<Real> > ajv2 = (ajvs.get_2())->clone();
applyAdjointJacobian_2(*ajv2,v,*(xs.get_1()),*(xs.get_2()),tol);
ajvs.set_2(*ajv2);
}
virtual void applyAdjointHessian(Vector<Real> &ahwv,
const Vector<Real> &w,
const Vector<Real> &v,
const Vector<Real> &x,
Real &tol) {
Vector_SimOpt<Real> &ahwvs = Teuchos::dyn_cast<Vector_SimOpt<Real> >(
Teuchos::dyn_cast<Vector<Real> >(ahwv));
const Vector_SimOpt<Real> &xs = Teuchos::dyn_cast<const Vector_SimOpt<Real> >(
Teuchos::dyn_cast<const Vector<Real> >(x));
const Vector_SimOpt<Real> &vs = Teuchos::dyn_cast<const Vector_SimOpt<Real> >(
Teuchos::dyn_cast<const Vector<Real> >(v));
// Block-row 1
Teuchos::RCP<Vector<Real> > C11 = (ahwvs.get_1())->clone();
Teuchos::RCP<Vector<Real> > C21 = (ahwvs.get_1())->clone();
applyAdjointHessian_11(*C11,w,*(vs.get_1()),*(xs.get_1()),*(xs.get_2()),tol);
applyAdjointHessian_21(*C21,w,*(vs.get_2()),*(xs.get_1()),*(xs.get_2()),tol);
C11->plus(*C21);
ahwvs.set_1(*C11);
// Block-row 2
Teuchos::RCP<Vector<Real> > C12 = (ahwvs.get_2())->clone();
Teuchos::RCP<Vector<Real> > C22 = (ahwvs.get_2())->clone();
applyAdjointHessian_12(*C12,w,*(vs.get_1()),*(xs.get_1()),*(xs.get_2()),tol);
applyAdjointHessian_22(*C22,w,*(vs.get_2()),*(xs.get_1()),*(xs.get_2()),tol);
C22->plus(*C12);
ahwvs.set_2(*C22);
}
virtual Real checkSolve(const ROL::Vector<Real> &u,
const ROL::Vector<Real> &z,
const ROL::Vector<Real> &c,
const bool printToStream = true,
std::ostream & outStream = std::cout) {
// Solve equality constraint for u.
Real tol = ROL_EPSILON<Real>();
Teuchos::RCP<ROL::Vector<Real> > r = c.clone();
Teuchos::RCP<ROL::Vector<Real> > s = u.clone();
solve(*r,*s,z,tol);
// Evaluate equality constraint residual at (u,z).
Teuchos::RCP<ROL::Vector<Real> > cs = c.clone();
update(*s,z);
value(*cs,*s,z,tol);
// Output norm of residual.
Real rnorm = r->norm();
Real cnorm = cs->norm();
if ( printToStream ) {
std::stringstream hist;
hist << std::scientific << std::setprecision(8);
hist << "\nTest SimOpt solve at feasible (u,z):\n";
hist << " Solver Residual = " << rnorm << "\n";
hist << " ||c(u,z)|| = " << cnorm << "\n";
outStream << hist.str();
}
return cnorm;
}
/** \brief Check the consistency of the Jacobian and its adjoint.
This is the primary interface.
@param[out] w is a dual constraint-space vector
@param[in] v is a simulation-space vector
@param[in] u is the constraint argument; a simulation-space vector
@param[in] z is the constraint argument; an optimization-space vector
@param[in] printToStream is is a flag that turns on/off output
@param[in] outStream is the output stream
---
*/
virtual Real checkAdjointConsistencyJacobian_1(const Vector<Real> &w,
const Vector<Real> &v,
const Vector<Real> &u,
const Vector<Real> &z,
const bool printToStream = true,
std::ostream & outStream = std::cout) {
return checkAdjointConsistencyJacobian_1(w, v, u, z, w.dual(), v.dual(), printToStream, outStream);
}
/** \brief Check the consistency of the Jacobian and its adjoint.
This is the secondary interface, for use with dual spaces where
the user does not define the dual() operation.
@param[out] w is a dual constraint-space vector
@param[in] v is a simulation-space vector
@param[in] u is the constraint argument; a simulation-space vector
@param[in] z is the constraint argument; an optimization-space vector
@param[in] dualw is a constraint-space vector
@param[in] dualv is a dual simulation-space vector
@param[in] printToStream is is a flag that turns on/off output
@param[in] outStream is the output stream
---
*/
virtual Real checkAdjointConsistencyJacobian_1(const Vector<Real> &w,
const Vector<Real> &v,
const Vector<Real> &u,
const Vector<Real> &z,
const Vector<Real> &dualw,
const Vector<Real> &dualv,
const bool printToStream = true,
std::ostream & outStream = std::cout) {
Real tol = ROL_EPSILON<Real>();
Teuchos::RCP<Vector<Real> > Jv = dualw.clone();
update(u,z);
applyJacobian_1(*Jv,v,u,z,tol);
Real wJv = w.dot(Jv->dual());
Teuchos::RCP<Vector<Real> > Jw = dualv.clone();
update(u,z);
applyAdjointJacobian_1(*Jw,w,u,z,tol);
Real vJw = v.dot(Jw->dual());
Real diff = std::abs(wJv-vJw);
if ( printToStream ) {
std::stringstream hist;
hist << std::scientific << std::setprecision(8);
hist << "\nTest SimOpt consistency of Jacobian_1 and its adjoint: \n |<w,Jv> - <adj(J)w,v>| = "
<< diff << "\n";
hist << " |<w,Jv>| = " << std::abs(wJv) << "\n";
hist << " Relative Error = " << diff / (std::abs(wJv)+ROL_UNDERFLOW<Real>()) << "\n";
outStream << hist.str();
}
return diff;
}
/** \brief Check the consistency of the Jacobian and its adjoint.
This is the primary interface.
@param[out] w is a dual constraint-space vector
@param[in] v is an optimization-space vector
@param[in] u is the constraint argument; a simulation-space vector
@param[in] z is the constraint argument; an optimization-space vector
@param[in] printToStream is is a flag that turns on/off output
@param[in] outStream is the output stream
---
*/
virtual Real checkAdjointConsistencyJacobian_2(const Vector<Real> &w,
const Vector<Real> &v,
const Vector<Real> &u,
const Vector<Real> &z,
const bool printToStream = true,
std::ostream & outStream = std::cout) {
return checkAdjointConsistencyJacobian_2(w, v, u, z, w.dual(), v.dual(), printToStream, outStream);
}
/** \brief Check the consistency of the Jacobian and its adjoint.
This is the secondary interface, for use with dual spaces where
the user does not define the dual() operation.
@param[out] w is a dual constraint-space vector
@param[in] v is an optimization-space vector
@param[in] u is the constraint argument; a simulation-space vector
@param[in] z is the constraint argument; an optimization-space vector
@param[in] dualw is a constraint-space vector
@param[in] dualv is a dual optimization-space vector
@param[in] printToStream is is a flag that turns on/off output
@param[in] outStream is the output stream
---
*/
virtual Real checkAdjointConsistencyJacobian_2(const Vector<Real> &w,
const Vector<Real> &v,
const Vector<Real> &u,
const Vector<Real> &z,
const Vector<Real> &dualw,
const Vector<Real> &dualv,
const bool printToStream = true,
std::ostream & outStream = std::cout) {
Real tol = ROL_EPSILON<Real>();
Teuchos::RCP<Vector<Real> > Jv = dualw.clone();
update(u,z);
applyJacobian_2(*Jv,v,u,z,tol);
Real wJv = w.dot(Jv->dual());
Teuchos::RCP<Vector<Real> > Jw = dualv.clone();
update(u,z);
applyAdjointJacobian_2(*Jw,w,u,z,tol);
Real vJw = v.dot(Jw->dual());
Real diff = std::abs(wJv-vJw);
if ( printToStream ) {
std::stringstream hist;
hist << std::scientific << std::setprecision(8);
hist << "\nTest SimOpt consistency of Jacobian_2 and its adjoint: \n |<w,Jv> - <adj(J)w,v>| = "
<< diff << "\n";
hist << " |<w,Jv>| = " << std::abs(wJv) << "\n";
hist << " Relative Error = " << diff / (std::abs(wJv)+ROL_UNDERFLOW<Real>()) << "\n";
outStream << hist.str();
}
return diff;
}
virtual Real checkInverseJacobian_1(const Vector<Real> &jv,
const Vector<Real> &v,
const Vector<Real> &u,
const Vector<Real> &z,
const bool printToStream = true,
std::ostream & outStream = std::cout) {
Real tol = ROL_EPSILON<Real>();
Teuchos::RCP<Vector<Real> > Jv = jv.clone();
update(u,z);
applyJacobian_1(*Jv,v,u,z,tol);
Teuchos::RCP<Vector<Real> > iJJv = u.clone();
update(u,z);
applyInverseJacobian_1(*iJJv,*Jv,u,z,tol);
Teuchos::RCP<Vector<Real> > diff = v.clone();
diff->set(v);
diff->axpy(-1.0,*iJJv);
Real dnorm = diff->norm();
Real vnorm = v.norm();
if ( printToStream ) {
std::stringstream hist;
hist << std::scientific << std::setprecision(8);
hist << "\nTest SimOpt consistency of inverse Jacobian_1: \n ||v-inv(J)Jv|| = "
<< dnorm << "\n";
hist << " ||v|| = " << vnorm << "\n";
hist << " Relative Error = " << dnorm / (vnorm+ROL_UNDERFLOW<Real>()) << "\n";
outStream << hist.str();
}
return dnorm;
}
virtual Real checkInverseAdjointJacobian_1(const Vector<Real> &jv,
const Vector<Real> &v,
const Vector<Real> &u,
const Vector<Real> &z,
const bool printToStream = true,
std::ostream & outStream = std::cout) {
Real tol = ROL_EPSILON<Real>();
Teuchos::RCP<Vector<Real> > Jv = jv.clone();
update(u,z);
applyAdjointJacobian_1(*Jv,v,u,z,tol);
Teuchos::RCP<Vector<Real> > iJJv = v.clone();
update(u,z);
applyInverseAdjointJacobian_1(*iJJv,*Jv,u,z,tol);
Teuchos::RCP<Vector<Real> > diff = v.clone();
diff->set(v);
diff->axpy(-1.0,*iJJv);
Real dnorm = diff->norm();
Real vnorm = v.norm();
if ( printToStream ) {
std::stringstream hist;
hist << std::scientific << std::setprecision(8);
hist << "\nTest SimOpt consistency of inverse adjoint Jacobian_1: \n ||v-inv(adj(J))adj(J)v|| = "
<< dnorm << "\n";
hist << " ||v|| = " << vnorm << "\n";
hist << " Relative Error = " << dnorm / (vnorm+ROL_UNDERFLOW<Real>()) << "\n";
outStream << hist.str();
}
return dnorm;
}
std::vector<std::vector<Real> > checkApplyJacobian_1(const Vector<Real> &u,
const Vector<Real> &z,
const Vector<Real> &v,
const Vector<Real> &jv,
const bool printToStream = true,
std::ostream & outStream = std::cout,
const int numSteps = ROL_NUM_CHECKDERIV_STEPS,
const int order = 1) {
std::vector<Real> steps(numSteps);
for(int i=0;i<numSteps;++i) {
steps[i] = pow(10,-i);
}
return checkApplyJacobian_1(u,z,v,jv,steps,printToStream,outStream,order);
}
std::vector<std::vector<Real> > checkApplyJacobian_1(const Vector<Real> &u,
const Vector<Real> &z,
const Vector<Real> &v,
const Vector<Real> &jv,
const std::vector<Real> &steps,
const bool printToStream = true,
std::ostream & outStream = std::cout,
const int order = 1) {
TEUCHOS_TEST_FOR_EXCEPTION( order<1 || order>4, std::invalid_argument,
"Error: finite difference order must be 1,2,3, or 4" );
Real one(1.0);
using Finite_Difference_Arrays::shifts;
using Finite_Difference_Arrays::weights;
Real tol = std::sqrt(ROL_EPSILON<Real>());
int numSteps = steps.size();
int numVals = 4;
std::vector<Real> tmp(numVals);
std::vector<std::vector<Real> > jvCheck(numSteps, tmp);
// Save the format state of the original outStream.
Teuchos::oblackholestream oldFormatState;
oldFormatState.copyfmt(outStream);
// Compute constraint value at x.
Teuchos::RCP<Vector<Real> > c = jv.clone();
this->update(u,z);
this->value(*c, u, z, tol);
// Compute (Jacobian at x) times (vector v).
Teuchos::RCP<Vector<Real> > Jv = jv.clone();
this->applyJacobian_1(*Jv, v, u, z, tol);
Real normJv = Jv->norm();
// Temporary vectors.
Teuchos::RCP<Vector<Real> > cdif = jv.clone();
Teuchos::RCP<Vector<Real> > cnew = jv.clone();
Teuchos::RCP<Vector<Real> > unew = u.clone();
for (int i=0; i<numSteps; i++) {
Real eta = steps[i];
unew->set(u);
cdif->set(*c);
cdif->scale(weights[order-1][0]);
for(int j=0; j<order; ++j) {
unew->axpy(eta*shifts[order-1][j], v);
if( weights[order-1][j+1] != 0 ) {
this->update(*unew,z);
this->value(*cnew,*unew,z,tol);
cdif->axpy(weights[order-1][j+1],*cnew);
}
}
cdif->scale(one/eta);
// Compute norms of Jacobian-vector products, finite-difference approximations, and error.
jvCheck[i][0] = eta;
jvCheck[i][1] = normJv;
jvCheck[i][2] = cdif->norm();
cdif->axpy(-one, *Jv);
jvCheck[i][3] = cdif->norm();
if (printToStream) {
std::stringstream hist;
if (i==0) {
hist << std::right
<< std::setw(20) << "Step size"
<< std::setw(20) << "norm(Jac*vec)"
<< std::setw(20) << "norm(FD approx)"
<< std::setw(20) << "norm(abs error)"
<< "\n"
<< std::setw(20) << "---------"
<< std::setw(20) << "-------------"
<< std::setw(20) << "---------------"
<< std::setw(20) << "---------------"
<< "\n";
}
hist << std::scientific << std::setprecision(11) << std::right
<< std::setw(20) << jvCheck[i][0]
<< std::setw(20) << jvCheck[i][1]
<< std::setw(20) << jvCheck[i][2]
<< std::setw(20) << jvCheck[i][3]
<< "\n";
outStream << hist.str();
}
}
// Reset format state of outStream.
outStream.copyfmt(oldFormatState);
return jvCheck;
} // checkApplyJacobian
std::vector<std::vector<Real> > checkApplyJacobian_2(const Vector<Real> &u,
const Vector<Real> &z,
const Vector<Real> &v,
const Vector<Real> &jv,
const bool printToStream = true,
std::ostream & outStream = std::cout,
const int numSteps = ROL_NUM_CHECKDERIV_STEPS,
const int order = 1) {
std::vector<Real> steps(numSteps);
for(int i=0;i<numSteps;++i) {
steps[i] = pow(10,-i);
}
return checkApplyJacobian_2(u,z,v,jv,steps,printToStream,outStream,order);
}
std::vector<std::vector<Real> > checkApplyJacobian_2(const Vector<Real> &u,
const Vector<Real> &z,
const Vector<Real> &v,
const Vector<Real> &jv,
const std::vector<Real> &steps,
const bool printToStream = true,
std::ostream & outStream = std::cout,
const int order = 1) {
TEUCHOS_TEST_FOR_EXCEPTION( order<1 || order>4, std::invalid_argument,
"Error: finite difference order must be 1,2,3, or 4" );
Real one(1.0);
using Finite_Difference_Arrays::shifts;
using Finite_Difference_Arrays::weights;
Real tol = std::sqrt(ROL_EPSILON<Real>());
int numSteps = steps.size();
int numVals = 4;
std::vector<Real> tmp(numVals);
std::vector<std::vector<Real> > jvCheck(numSteps, tmp);
// Save the format state of the original outStream.
Teuchos::oblackholestream oldFormatState;
oldFormatState.copyfmt(outStream);
// Compute constraint value at x.
Teuchos::RCP<Vector<Real> > c = jv.clone();
this->update(u,z);
this->value(*c, u, z, tol);
// Compute (Jacobian at x) times (vector v).
Teuchos::RCP<Vector<Real> > Jv = jv.clone();
this->applyJacobian_2(*Jv, v, u, z, tol);
Real normJv = Jv->norm();
// Temporary vectors.
Teuchos::RCP<Vector<Real> > cdif = jv.clone();
Teuchos::RCP<Vector<Real> > cnew = jv.clone();
Teuchos::RCP<Vector<Real> > znew = z.clone();
for (int i=0; i<numSteps; i++) {
Real eta = steps[i];
znew->set(z);
cdif->set(*c);
cdif->scale(weights[order-1][0]);
for(int j=0; j<order; ++j) {
znew->axpy(eta*shifts[order-1][j], v);
if( weights[order-1][j+1] != 0 ) {
this->update(u,*znew);
this->value(*cnew,u,*znew,tol);
cdif->axpy(weights[order-1][j+1],*cnew);
}
}
cdif->scale(one/eta);
// Compute norms of Jacobian-vector products, finite-difference approximations, and error.
jvCheck[i][0] = eta;
jvCheck[i][1] = normJv;
jvCheck[i][2] = cdif->norm();
cdif->axpy(-one, *Jv);
jvCheck[i][3] = cdif->norm();
if (printToStream) {
std::stringstream hist;
if (i==0) {
hist << std::right
<< std::setw(20) << "Step size"
<< std::setw(20) << "norm(Jac*vec)"
<< std::setw(20) << "norm(FD approx)"
<< std::setw(20) << "norm(abs error)"
<< "\n"
<< std::setw(20) << "---------"
<< std::setw(20) << "-------------"
<< std::setw(20) << "---------------"
<< std::setw(20) << "---------------"
<< "\n";
}
hist << std::scientific << std::setprecision(11) << std::right
<< std::setw(20) << jvCheck[i][0]
<< std::setw(20) << jvCheck[i][1]
<< std::setw(20) << jvCheck[i][2]
<< std::setw(20) << jvCheck[i][3]
<< "\n";
outStream << hist.str();
}
}
// Reset format state of outStream.
outStream.copyfmt(oldFormatState);
return jvCheck;
} // checkApplyJacobian
std::vector<std::vector<Real> > checkApplyAdjointHessian_11(const Vector<Real> &u,
const Vector<Real> &z,
const Vector<Real> &p,
const Vector<Real> &v,
const Vector<Real> &hv,
const bool printToStream = true,
std::ostream & outStream = std::cout,
const int numSteps = ROL_NUM_CHECKDERIV_STEPS,
const int order = 1 ) {
std::vector<Real> steps(numSteps);
for(int i=0;i<numSteps;++i) {
steps[i] = pow(10,-i);
}
return checkApplyAdjointHessian_11(u,z,p,v,hv,steps,printToStream,outStream,order);
}
std::vector<std::vector<Real> > checkApplyAdjointHessian_11(const Vector<Real> &u,
const Vector<Real> &z,
const Vector<Real> &p,
const Vector<Real> &v,
const Vector<Real> &hv,
const std::vector<Real> &steps,
const bool printToStream = true,
std::ostream & outStream = std::cout,
const int order = 1 ) {
using Finite_Difference_Arrays::shifts;
using Finite_Difference_Arrays::weights;
Real one(1.0);
Real tol = std::sqrt(ROL_EPSILON<Real>());
int numSteps = steps.size();
int numVals = 4;
std::vector<Real> tmp(numVals);
std::vector<std::vector<Real> > ahpvCheck(numSteps, tmp);
// Temporary vectors.
Teuchos::RCP<Vector<Real> > AJdif = hv.clone();
Teuchos::RCP<Vector<Real> > AJp = hv.clone();
Teuchos::RCP<Vector<Real> > AHpv = hv.clone();
Teuchos::RCP<Vector<Real> > AJnew = hv.clone();
Teuchos::RCP<Vector<Real> > unew = u.clone();
// Save the format state of the original outStream.
Teuchos::oblackholestream oldFormatState;
oldFormatState.copyfmt(outStream);
// Apply adjoint Jacobian to p.
this->update(u,z);
this->applyAdjointJacobian_1(*AJp, p, u, z, tol);
// Apply adjoint Hessian at (u,z), in direction v, to p.
this->applyAdjointHessian_11(*AHpv, p, v, u, z, tol);
Real normAHpv = AHpv->norm();
for (int i=0; i<numSteps; i++) {
Real eta = steps[i];
// Apply adjoint Jacobian to p at (u+eta*v,z).
unew->set(u);
AJdif->set(*AJp);
AJdif->scale(weights[order-1][0]);
for(int j=0; j<order; ++j) {
unew->axpy(eta*shifts[order-1][j],v);
if( weights[order-1][j+1] != 0 ) {
this->update(*unew,z);
this->applyAdjointJacobian_1(*AJnew, p, *unew, z, tol);
AJdif->axpy(weights[order-1][j+1],*AJnew);
}
}
AJdif->scale(one/eta);
// Compute norms of Jacobian-vector products, finite-difference approximations, and error.
ahpvCheck[i][0] = eta;
ahpvCheck[i][1] = normAHpv;
ahpvCheck[i][2] = AJdif->norm();
AJdif->axpy(-one, *AHpv);
ahpvCheck[i][3] = AJdif->norm();
if (printToStream) {
std::stringstream hist;
if (i==0) {
hist << std::right
<< std::setw(20) << "Step size"
<< std::setw(20) << "norm(adj(H)(u,v))"
<< std::setw(20) << "norm(FD approx)"
<< std::setw(20) << "norm(abs error)"
<< "\n"
<< std::setw(20) << "---------"
<< std::setw(20) << "-----------------"
<< std::setw(20) << "---------------"
<< std::setw(20) << "---------------"
<< "\n";
}
hist << std::scientific << std::setprecision(11) << std::right
<< std::setw(20) << ahpvCheck[i][0]
<< std::setw(20) << ahpvCheck[i][1]
<< std::setw(20) << ahpvCheck[i][2]
<< std::setw(20) << ahpvCheck[i][3]
<< "\n";
outStream << hist.str();
}
}
// Reset format state of outStream.
outStream.copyfmt(oldFormatState);
return ahpvCheck;
} // checkApplyAdjointHessian_11
std::vector<std::vector<Real> > checkApplyAdjointHessian_21(const Vector<Real> &u,
const Vector<Real> &z,
const Vector<Real> &p,
const Vector<Real> &v,
const Vector<Real> &hv,
const bool printToStream = true,
std::ostream & outStream = std::cout,
const int numSteps = ROL_NUM_CHECKDERIV_STEPS,
const int order = 1 ) {
std::vector<Real> steps(numSteps);
for(int i=0;i<numSteps;++i) {
steps[i] = pow(10,-i);
}
return checkApplyAdjointHessian_21(u,z,p,v,hv,steps,printToStream,outStream,order);
}
std::vector<std::vector<Real> > checkApplyAdjointHessian_21(const Vector<Real> &u,
const Vector<Real> &z,
const Vector<Real> &p,
const Vector<Real> &v,
const Vector<Real> &hv,
const std::vector<Real> &steps,
const bool printToStream = true,
std::ostream & outStream = std::cout,
const int order = 1 ) {
using Finite_Difference_Arrays::shifts;
using Finite_Difference_Arrays::weights;
Real one(1.0);
Real tol = std::sqrt(ROL_EPSILON<Real>());
int numSteps = steps.size();
int numVals = 4;
std::vector<Real> tmp(numVals);
std::vector<std::vector<Real> > ahpvCheck(numSteps, tmp);
// Temporary vectors.
Teuchos::RCP<Vector<Real> > AJdif = hv.clone();
Teuchos::RCP<Vector<Real> > AJp = hv.clone();
Teuchos::RCP<Vector<Real> > AHpv = hv.clone();
Teuchos::RCP<Vector<Real> > AJnew = hv.clone();
Teuchos::RCP<Vector<Real> > znew = z.clone();
// Save the format state of the original outStream.
Teuchos::oblackholestream oldFormatState;
oldFormatState.copyfmt(outStream);
// Apply adjoint Jacobian to p.
this->update(u,z);
this->applyAdjointJacobian_1(*AJp, p, u, z, tol);
// Apply adjoint Hessian at (u,z), in direction v, to p.
this->applyAdjointHessian_21(*AHpv, p, v, u, z, tol);
Real normAHpv = AHpv->norm();
for (int i=0; i<numSteps; i++) {
Real eta = steps[i];
// Apply adjoint Jacobian to p at (u,z+eta*v).
znew->set(z);
AJdif->set(*AJp);
AJdif->scale(weights[order-1][0]);
for(int j=0; j<order; ++j) {
znew->axpy(eta*shifts[order-1][j],v);
if( weights[order-1][j+1] != 0 ) {
this->update(u,*znew);
this->applyAdjointJacobian_1(*AJnew, p, u, *znew, tol);
AJdif->axpy(weights[order-1][j+1],*AJnew);
}
}
AJdif->scale(one/eta);
// Compute norms of Jacobian-vector products, finite-difference approximations, and error.
ahpvCheck[i][0] = eta;
ahpvCheck[i][1] = normAHpv;
ahpvCheck[i][2] = AJdif->norm();
AJdif->axpy(-one, *AHpv);
ahpvCheck[i][3] = AJdif->norm();
if (printToStream) {
std::stringstream hist;
if (i==0) {
hist << std::right
<< std::setw(20) << "Step size"
<< std::setw(20) << "norm(adj(H)(u,v))"
<< std::setw(20) << "norm(FD approx)"
<< std::setw(20) << "norm(abs error)"
<< "\n"
<< std::setw(20) << "---------"
<< std::setw(20) << "-----------------"
<< std::setw(20) << "---------------"
<< std::setw(20) << "---------------"
<< "\n";
}
hist << std::scientific << std::setprecision(11) << std::right
<< std::setw(20) << ahpvCheck[i][0]
<< std::setw(20) << ahpvCheck[i][1]
<< std::setw(20) << ahpvCheck[i][2]
<< std::setw(20) << ahpvCheck[i][3]
<< "\n";
outStream << hist.str();
}
}
// Reset format state of outStream.
outStream.copyfmt(oldFormatState);
return ahpvCheck;
} // checkApplyAdjointHessian_21
std::vector<std::vector<Real> > checkApplyAdjointHessian_12(const Vector<Real> &u,
const Vector<Real> &z,
const Vector<Real> &p,
const Vector<Real> &v,
const Vector<Real> &hv,
const bool printToStream = true,
std::ostream & outStream = std::cout,
const int numSteps = ROL_NUM_CHECKDERIV_STEPS,
const int order = 1 ) {
std::vector<Real> steps(numSteps);
for(int i=0;i<numSteps;++i) {
steps[i] = pow(10,-i);
}
return checkApplyAdjointHessian_12(u,z,p,v,hv,steps,printToStream,outStream,order);
}
std::vector<std::vector<Real> > checkApplyAdjointHessian_12(const Vector<Real> &u,
const Vector<Real> &z,
const Vector<Real> &p,
const Vector<Real> &v,
const Vector<Real> &hv,
const std::vector<Real> &steps,
const bool printToStream = true,
std::ostream & outStream = std::cout,
const int order = 1 ) {
using Finite_Difference_Arrays::shifts;
using Finite_Difference_Arrays::weights;
Real one(1.0);
Real tol = std::sqrt(ROL_EPSILON<Real>());
int numSteps = steps.size();
int numVals = 4;
std::vector<Real> tmp(numVals);
std::vector<std::vector<Real> > ahpvCheck(numSteps, tmp);
// Temporary vectors.
Teuchos::RCP<Vector<Real> > AJdif = hv.clone();
Teuchos::RCP<Vector<Real> > AJp = hv.clone();
Teuchos::RCP<Vector<Real> > AHpv = hv.clone();
Teuchos::RCP<Vector<Real> > AJnew = hv.clone();
Teuchos::RCP<Vector<Real> > unew = u.clone();
// Save the format state of the original outStream.
Teuchos::oblackholestream oldFormatState;
oldFormatState.copyfmt(outStream);
// Apply adjoint Jacobian to p.
this->update(u,z);
this->applyAdjointJacobian_2(*AJp, p, u, z, tol);
// Apply adjoint Hessian at (u,z), in direction v, to p.
this->applyAdjointHessian_12(*AHpv, p, v, u, z, tol);
Real normAHpv = AHpv->norm();
for (int i=0; i<numSteps; i++) {
Real eta = steps[i];
// Apply adjoint Jacobian to p at (u+eta*v,z).
unew->set(u);
AJdif->set(*AJp);
AJdif->scale(weights[order-1][0]);
for(int j=0; j<order; ++j) {
unew->axpy(eta*shifts[order-1][j],v);
if( weights[order-1][j+1] != 0 ) {
this->update(*unew,z);
this->applyAdjointJacobian_2(*AJnew, p, *unew, z, tol);
AJdif->axpy(weights[order-1][j+1],*AJnew);
}
}
AJdif->scale(one/eta);
// Compute norms of Jacobian-vector products, finite-difference approximations, and error.
ahpvCheck[i][0] = eta;
ahpvCheck[i][1] = normAHpv;
ahpvCheck[i][2] = AJdif->norm();
AJdif->axpy(-one, *AHpv);
ahpvCheck[i][3] = AJdif->norm();
if (printToStream) {
std::stringstream hist;
if (i==0) {
hist << std::right
<< std::setw(20) << "Step size"
<< std::setw(20) << "norm(adj(H)(u,v))"
<< std::setw(20) << "norm(FD approx)"
<< std::setw(20) << "norm(abs error)"
<< "\n"
<< std::setw(20) << "---------"
<< std::setw(20) << "-----------------"
<< std::setw(20) << "---------------"
<< std::setw(20) << "---------------"
<< "\n";
}
hist << std::scientific << std::setprecision(11) << std::right
<< std::setw(20) << ahpvCheck[i][0]
<< std::setw(20) << ahpvCheck[i][1]
<< std::setw(20) << ahpvCheck[i][2]
<< std::setw(20) << ahpvCheck[i][3]
<< "\n";
outStream << hist.str();
}
}
// Reset format state of outStream.
outStream.copyfmt(oldFormatState);
return ahpvCheck;
} // checkApplyAdjointHessian_12
std::vector<std::vector<Real> > checkApplyAdjointHessian_22(const Vector<Real> &u,
const Vector<Real> &z,
const Vector<Real> &p,
const Vector<Real> &v,
const Vector<Real> &hv,
const bool printToStream = true,
std::ostream & outStream = std::cout,
const int numSteps = ROL_NUM_CHECKDERIV_STEPS,
const int order = 1 ) {
std::vector<Real> steps(numSteps);
for(int i=0;i<numSteps;++i) {
steps[i] = pow(10,-i);
}
return checkApplyAdjointHessian_22(u,z,p,v,hv,steps,printToStream,outStream,order);
}
std::vector<std::vector<Real> > checkApplyAdjointHessian_22(const Vector<Real> &u,
const Vector<Real> &z,
const Vector<Real> &p,
const Vector<Real> &v,
const Vector<Real> &hv,
const std::vector<Real> &steps,
const bool printToStream = true,
std::ostream & outStream = std::cout,
const int order = 1 ) {
using Finite_Difference_Arrays::shifts;
using Finite_Difference_Arrays::weights;
Real one(1.0);
Real tol = std::sqrt(ROL_EPSILON<Real>());
int numSteps = steps.size();
int numVals = 4;
std::vector<Real> tmp(numVals);
std::vector<std::vector<Real> > ahpvCheck(numSteps, tmp);
// Temporary vectors.
Teuchos::RCP<Vector<Real> > AJdif = hv.clone();
Teuchos::RCP<Vector<Real> > AJp = hv.clone();
Teuchos::RCP<Vector<Real> > AHpv = hv.clone();
Teuchos::RCP<Vector<Real> > AJnew = hv.clone();
Teuchos::RCP<Vector<Real> > znew = z.clone();
// Save the format state of the original outStream.
Teuchos::oblackholestream oldFormatState;
oldFormatState.copyfmt(outStream);
// Apply adjoint Jacobian to p.
this->update(u,z);
this->applyAdjointJacobian_2(*AJp, p, u, z, tol);
// Apply adjoint Hessian at (u,z), in direction v, to p.
this->applyAdjointHessian_22(*AHpv, p, v, u, z, tol);
Real normAHpv = AHpv->norm();
for (int i=0; i<numSteps; i++) {
Real eta = steps[i];
// Apply adjoint Jacobian to p at (u,z+eta*v).
znew->set(z);
AJdif->set(*AJp);
AJdif->scale(weights[order-1][0]);
for(int j=0; j<order; ++j) {
znew->axpy(eta*shifts[order-1][j],v);
if( weights[order-1][j+1] != 0 ) {
this->update(u,*znew);
this->applyAdjointJacobian_2(*AJnew, p, u, *znew, tol);
AJdif->axpy(weights[order-1][j+1],*AJnew);
}
}
AJdif->scale(one/eta);
// Compute norms of Jacobian-vector products, finite-difference approximations, and error.
ahpvCheck[i][0] = eta;
ahpvCheck[i][1] = normAHpv;
ahpvCheck[i][2] = AJdif->norm();
AJdif->axpy(-one, *AHpv);
ahpvCheck[i][3] = AJdif->norm();
if (printToStream) {
std::stringstream hist;
if (i==0) {
hist << std::right
<< std::setw(20) << "Step size"
<< std::setw(20) << "norm(adj(H)(u,v))"
<< std::setw(20) << "norm(FD approx)"
<< std::setw(20) << "norm(abs error)"
<< "\n"
<< std::setw(20) << "---------"
<< std::setw(20) << "-----------------"
<< std::setw(20) << "---------------"
<< std::setw(20) << "---------------"
<< "\n";
}
hist << std::scientific << std::setprecision(11) << std::right
<< std::setw(20) << ahpvCheck[i][0]
<< std::setw(20) << ahpvCheck[i][1]
<< std::setw(20) << ahpvCheck[i][2]
<< std::setw(20) << ahpvCheck[i][3]
<< "\n";
outStream << hist.str();
}
}
// Reset format state of outStream.
outStream.copyfmt(oldFormatState);
return ahpvCheck;
} // checkApplyAdjointHessian_22
}; // class EqualityConstraint_SimOpt
} // namespace ROL
#endif
|