44 #ifndef ROL_CONSTRAINT_SIMOPT_H
45 #define ROL_CONSTRAINT_SIMOPT_H
102 template <
class Real>
216 Real cnorm = c.
norm();
217 const Real ctol = std::min(
atol_,
rtol_*cnorm);
224 Real alpha(1), tmp(0);
227 std::cout <<
"\n Default Constraint_SimOpt::solve\n";
229 std::cout << std::setw(6) << std::left <<
"iter";
230 std::cout << std::setw(15) << std::left <<
"rnorm";
231 std::cout << std::setw(15) << std::left <<
"alpha";
234 for (cnt = 0; cnt <
maxit_; ++cnt) {
244 while ( tmp > (1.0-
decr_*alpha)*cnorm &&
256 std::cout << std::setw(6) << std::left << cnt;
257 std::cout << std::scientific << std::setprecision(6);
258 std::cout << std::setw(15) << std::left << tmp;
259 std::cout << std::scientific << std::setprecision(6);
260 std::cout << std::setw(15) << std::left << alpha;
274 Ptr<Constraint_SimOpt<Real>> con = makePtrFromRef(*
this);
275 Ptr<Objective<Real>> obj = makePtr<NonlinearLeastSquaresObjective_SimOpt<Real>>(con,u,z,c,
true);
276 ParameterList parlist;
277 parlist.sublist(
"Status Test").set(
"Gradient Tolerance",ctol);
278 parlist.sublist(
"Status Test").set(
"Step Tolerance",
stol_);
279 parlist.sublist(
"Status Test").set(
"Iteration Limit",
maxit_);
280 parlist.sublist(
"Step").sublist(
"Trust Region").set(
"Subproblem Solver",
"Truncated CG");
281 parlist.sublist(
"General").sublist(
"Krylov").set(
"Iteration Limit",100);
282 Ptr<Step<Real>> step = makePtr<TrustRegionStep<Real>>(parlist);
283 Ptr<StatusTest<Real>> status = makePtr<StatusTest<Real>>(parlist);
284 Ptr<Algorithm<Real>> algo = makePtr<Algorithm<Real>>(step,status,
false);
289 Ptr<Constraint_SimOpt<Real>> con = makePtrFromRef(*
this);
290 Ptr<const Vector<Real>> zVec = makePtrFromRef(z);
291 Ptr<Constraint<Real>> conU
292 = makePtr<Constraint_State<Real>>(con,zVec);
293 Ptr<Objective<Real>> objU
294 = makePtr<Objective_FSsolver<Real>>();
295 ParameterList parlist;
296 parlist.sublist(
"Status Test").set(
"Constraint Tolerance",ctol);
297 parlist.sublist(
"Status Test").set(
"Step Tolerance",
stol_);
298 parlist.sublist(
"Status Test").set(
"Iteration Limit",
maxit_);
299 Ptr<Step<Real>> step = makePtr<CompositeStep<Real>>(parlist);
300 Ptr<StatusTest<Real>> status = makePtr<ConstraintStatusTest<Real>>(parlist);
301 Ptr<Algorithm<Real>> algo = makePtr<Algorithm<Real>>(step,status,
false);
302 Ptr<Vector<Real>> l = c.
dual().clone();
303 algo->run(u,*l,*objU,*conU,
print_);
307 ROL_TEST_FOR_EXCEPTION(
true, std::invalid_argument,
308 ">>> ERROR (ROL:Constraint_SimOpt:solve): Invalid solver type!");
333 ParameterList & list = parlist.sublist(
"SimOpt").sublist(
"Solve");
365 Real ctol = std::sqrt(ROL_EPSILON<Real>());
368 if (v.
norm() > std::sqrt(ROL_EPSILON<Real>())) {
369 h = std::max(1.0,u.
norm()/v.
norm())*tol;
372 Ptr<Vector<Real>> unew = u.
clone();
377 value(jv,*unew,z,ctol);
379 Ptr<Vector<Real>> cold = jv.
clone();
381 value(*cold,u,z,ctol);
408 Real ctol = std::sqrt(ROL_EPSILON<Real>());
411 if (v.
norm() > std::sqrt(ROL_EPSILON<Real>())) {
412 h = std::max(1.0,u.
norm()/v.
norm())*tol;
415 Ptr<Vector<Real>> znew = z.
clone();
420 value(jv,u,*znew,ctol);
422 Ptr<Vector<Real>> cold = jv.
clone();
424 value(*cold,u,z,ctol);
450 ROL_TEST_FOR_EXCEPTION(
true, std::logic_error,
451 "The method applyInverseJacobian_1 is used but not implemented!\n");
501 Real ctol = std::sqrt(ROL_EPSILON<Real>());
503 if (v.
norm() > std::sqrt(ROL_EPSILON<Real>())) {
504 h = std::max(1.0,u.
norm()/v.
norm())*tol;
506 Ptr<Vector<Real>> cold = dualv.
clone();
507 Ptr<Vector<Real>> cnew = dualv.
clone();
509 value(*cold,u,z,ctol);
510 Ptr<Vector<Real>> unew = u.
clone();
512 for (
int i = 0; i < u.
dimension(); i++) {
514 unew->axpy(h,*(u.
basis(i)));
516 value(*cnew,*unew,z,ctol);
517 cnew->axpy(-1.0,*cold);
519 ajv.
axpy(cnew->dot(v),*((u.
dual()).basis(i)));
572 Real ctol = std::sqrt(ROL_EPSILON<Real>());
574 if (v.
norm() > std::sqrt(ROL_EPSILON<Real>())) {
575 h = std::max(1.0,u.
norm()/v.
norm())*tol;
577 Ptr<Vector<Real>> cold = dualv.
clone();
578 Ptr<Vector<Real>> cnew = dualv.
clone();
580 value(*cold,u,z,ctol);
581 Ptr<Vector<Real>> znew = z.
clone();
583 for (
int i = 0; i < z.
dimension(); i++) {
585 znew->axpy(h,*(z.
basis(i)));
587 value(*cnew,u,*znew,ctol);
588 cnew->axpy(-1.0,*cold);
590 ajv.
axpy(cnew->dot(v),*((z.
dual()).basis(i)));
615 ROL_TEST_FOR_EXCEPTION(
true, std::logic_error,
616 "The method applyInverseAdjointJacobian_1 is used but not implemented!\n");
642 Real jtol = std::sqrt(ROL_EPSILON<Real>());
645 if (v.
norm() > std::sqrt(ROL_EPSILON<Real>())) {
646 h = std::max(1.0,u.
norm()/v.
norm())*tol;
649 Ptr<Vector<Real>> unew = u.
clone();
655 Ptr<Vector<Real>> jv = ahwv.
clone();
687 Real jtol = std::sqrt(ROL_EPSILON<Real>());
690 if (v.
norm() > std::sqrt(ROL_EPSILON<Real>())) {
691 h = std::max(1.0,u.
norm()/v.
norm())*tol;
694 Ptr<Vector<Real>> unew = u.
clone();
700 Ptr<Vector<Real>> jv = ahwv.
clone();
732 Real jtol = std::sqrt(ROL_EPSILON<Real>());
735 if (v.
norm() > std::sqrt(ROL_EPSILON<Real>())) {
736 h = std::max(1.0,u.
norm()/v.
norm())*tol;
739 Ptr<Vector<Real>> znew = z.
clone();
745 Ptr<Vector<Real>> jv = ahwv.
clone();
776 Real jtol = std::sqrt(ROL_EPSILON<Real>());
779 if (v.
norm() > std::sqrt(ROL_EPSILON<Real>())) {
780 h = std::max(1.0,u.
norm()/v.
norm())*tol;
783 Ptr<Vector<Real>> znew = z.
clone();
789 Ptr<Vector<Real>> jv = ahwv.
clone();
869 Ptr<Vector<Real>> ijv = (xs.
get_1())->clone();
874 catch (
const std::logic_error &e) {
880 Ptr<Vector<Real>> ijv_dual = (gs.
get_1())->clone();
886 catch (
const std::logic_error &e) {
922 Ptr<Vector<Real>> jv2 = jv.
clone();
937 Ptr<Vector<Real>> ajv1 = (ajvs.
get_1())->clone();
940 Ptr<Vector<Real>> ajv2 = (ajvs.
get_2())->clone();
958 Ptr<Vector<Real>> C11 = (ahwvs.
get_1())->clone();
959 Ptr<Vector<Real>> C21 = (ahwvs.
get_1())->clone();
965 Ptr<Vector<Real>> C12 = (ahwvs.
get_2())->clone();
966 Ptr<Vector<Real>> C22 = (ahwvs.
get_2())->clone();
978 const bool printToStream =
true,
979 std::ostream & outStream = std::cout) {
981 Real tol = ROL_EPSILON<Real>();
982 Ptr<Vector<Real>> r = c.
clone();
983 Ptr<Vector<Real>> s = u.
clone();
986 Ptr<Vector<Real>> cs = c.
clone();
990 Real rnorm = r->norm();
991 Real cnorm = cs->norm();
992 if ( printToStream ) {
993 std::stringstream hist;
994 hist << std::scientific << std::setprecision(8);
995 hist <<
"\nTest SimOpt solve at feasible (u,z):\n";
996 hist <<
" Solver Residual = " << rnorm <<
"\n";
997 hist <<
" ||c(u,z)|| = " << cnorm <<
"\n";
998 outStream << hist.str();
1020 const bool printToStream =
true,
1021 std::ostream & outStream = std::cout) {
1049 const bool printToStream =
true,
1050 std::ostream & outStream = std::cout) {
1051 Real tol = ROL_EPSILON<Real>();
1052 Ptr<Vector<Real>> Jv = dualw.
clone();
1055 Real wJv = w.
dot(Jv->dual());
1056 Ptr<Vector<Real>> Jw = dualv.
clone();
1059 Real vJw = v.
dot(Jw->dual());
1060 Real diff = std::abs(wJv-vJw);
1061 if ( printToStream ) {
1062 std::stringstream hist;
1063 hist << std::scientific << std::setprecision(8);
1064 hist <<
"\nTest SimOpt consistency of Jacobian_1 and its adjoint: \n |<w,Jv> - <adj(J)w,v>| = "
1066 hist <<
" |<w,Jv>| = " << std::abs(wJv) <<
"\n";
1067 hist <<
" Relative Error = " << diff / (std::abs(wJv)+ROL_UNDERFLOW<Real>()) <<
"\n";
1068 outStream << hist.str();
1090 const bool printToStream =
true,
1091 std::ostream & outStream = std::cout) {
1116 const bool printToStream =
true,
1117 std::ostream & outStream = std::cout) {
1118 Real tol = ROL_EPSILON<Real>();
1119 Ptr<Vector<Real>> Jv = dualw.
clone();
1122 Real wJv = w.
dot(Jv->dual());
1123 Ptr<Vector<Real>> Jw = dualv.
clone();
1126 Real vJw = v.
dot(Jw->dual());
1127 Real diff = std::abs(wJv-vJw);
1128 if ( printToStream ) {
1129 std::stringstream hist;
1130 hist << std::scientific << std::setprecision(8);
1131 hist <<
"\nTest SimOpt consistency of Jacobian_2 and its adjoint: \n |<w,Jv> - <adj(J)w,v>| = "
1133 hist <<
" |<w,Jv>| = " << std::abs(wJv) <<
"\n";
1134 hist <<
" Relative Error = " << diff / (std::abs(wJv)+ROL_UNDERFLOW<Real>()) <<
"\n";
1135 outStream << hist.str();
1144 const bool printToStream =
true,
1145 std::ostream & outStream = std::cout) {
1146 Real tol = ROL_EPSILON<Real>();
1147 Ptr<Vector<Real>> Jv = jv.
clone();
1150 Ptr<Vector<Real>> iJJv = u.
clone();
1153 Ptr<Vector<Real>> diff = v.
clone();
1155 diff->axpy(-1.0,*iJJv);
1156 Real dnorm = diff->norm();
1157 Real vnorm = v.
norm();
1158 if ( printToStream ) {
1159 std::stringstream hist;
1160 hist << std::scientific << std::setprecision(8);
1161 hist <<
"\nTest SimOpt consistency of inverse Jacobian_1: \n ||v-inv(J)Jv|| = "
1163 hist <<
" ||v|| = " << vnorm <<
"\n";
1164 hist <<
" Relative Error = " << dnorm / (vnorm+ROL_UNDERFLOW<Real>()) <<
"\n";
1165 outStream << hist.str();
1174 const bool printToStream =
true,
1175 std::ostream & outStream = std::cout) {
1176 Real tol = ROL_EPSILON<Real>();
1177 Ptr<Vector<Real>> Jv = jv.
clone();
1180 Ptr<Vector<Real>> iJJv = v.
clone();
1183 Ptr<Vector<Real>> diff = v.
clone();
1185 diff->axpy(-1.0,*iJJv);
1186 Real dnorm = diff->norm();
1187 Real vnorm = v.
norm();
1188 if ( printToStream ) {
1189 std::stringstream hist;
1190 hist << std::scientific << std::setprecision(8);
1191 hist <<
"\nTest SimOpt consistency of inverse adjoint Jacobian_1: \n ||v-inv(adj(J))adj(J)v|| = "
1193 hist <<
" ||v|| = " << vnorm <<
"\n";
1194 hist <<
" Relative Error = " << dnorm / (vnorm+ROL_UNDERFLOW<Real>()) <<
"\n";
1195 outStream << hist.str();
1206 const bool printToStream =
true,
1207 std::ostream & outStream = std::cout,
1209 const int order = 1) {
1210 std::vector<Real> steps(numSteps);
1211 for(
int i=0;i<numSteps;++i) {
1212 steps[i] = pow(10,-i);
1225 const std::vector<Real> &steps,
1226 const bool printToStream =
true,
1227 std::ostream & outStream = std::cout,
1228 const int order = 1) {
1230 ROL_TEST_FOR_EXCEPTION( order<1 || order>4, std::invalid_argument,
1231 "Error: finite difference order must be 1,2,3, or 4" );
1238 Real tol = std::sqrt(ROL_EPSILON<Real>());
1240 int numSteps = steps.size();
1242 std::vector<Real> tmp(numVals);
1243 std::vector<std::vector<Real>> jvCheck(numSteps, tmp);
1247 oldFormatState.copyfmt(outStream);
1250 Ptr<Vector<Real>> c = jv.
clone();
1252 this->
value(*c, u, z, tol);
1255 Ptr<Vector<Real>> Jv = jv.
clone();
1257 Real normJv = Jv->norm();
1260 Ptr<Vector<Real>> cdif = jv.
clone();
1261 Ptr<Vector<Real>> cnew = jv.
clone();
1262 Ptr<Vector<Real>> unew = u.
clone();
1264 for (
int i=0; i<numSteps; i++) {
1266 Real eta = steps[i];
1271 cdif->scale(
weights[order-1][0]);
1273 for(
int j=0; j<order; ++j) {
1275 unew->axpy(eta*
shifts[order-1][j], v);
1277 if(
weights[order-1][j+1] != 0 ) {
1279 this->
value(*cnew,*unew,z,tol);
1280 cdif->axpy(
weights[order-1][j+1],*cnew);
1285 cdif->scale(one/eta);
1288 jvCheck[i][0] = eta;
1289 jvCheck[i][1] = normJv;
1290 jvCheck[i][2] = cdif->norm();
1291 cdif->axpy(-one, *Jv);
1292 jvCheck[i][3] = cdif->norm();
1294 if (printToStream) {
1295 std::stringstream hist;
1298 << std::setw(20) <<
"Step size"
1299 << std::setw(20) <<
"norm(Jac*vec)"
1300 << std::setw(20) <<
"norm(FD approx)"
1301 << std::setw(20) <<
"norm(abs error)"
1303 << std::setw(20) <<
"---------"
1304 << std::setw(20) <<
"-------------"
1305 << std::setw(20) <<
"---------------"
1306 << std::setw(20) <<
"---------------"
1309 hist << std::scientific << std::setprecision(11) << std::right
1310 << std::setw(20) << jvCheck[i][0]
1311 << std::setw(20) << jvCheck[i][1]
1312 << std::setw(20) << jvCheck[i][2]
1313 << std::setw(20) << jvCheck[i][3]
1315 outStream << hist.str();
1321 outStream.copyfmt(oldFormatState);
1331 const bool printToStream =
true,
1332 std::ostream & outStream = std::cout,
1334 const int order = 1) {
1335 std::vector<Real> steps(numSteps);
1336 for(
int i=0;i<numSteps;++i) {
1337 steps[i] = pow(10,-i);
1350 const std::vector<Real> &steps,
1351 const bool printToStream =
true,
1352 std::ostream & outStream = std::cout,
1353 const int order = 1) {
1355 ROL_TEST_FOR_EXCEPTION( order<1 || order>4, std::invalid_argument,
1356 "Error: finite difference order must be 1,2,3, or 4" );
1363 Real tol = std::sqrt(ROL_EPSILON<Real>());
1365 int numSteps = steps.size();
1367 std::vector<Real> tmp(numVals);
1368 std::vector<std::vector<Real>> jvCheck(numSteps, tmp);
1372 oldFormatState.copyfmt(outStream);
1375 Ptr<Vector<Real>> c = jv.
clone();
1377 this->
value(*c, u, z, tol);
1380 Ptr<Vector<Real>> Jv = jv.
clone();
1382 Real normJv = Jv->norm();
1385 Ptr<Vector<Real>> cdif = jv.
clone();
1386 Ptr<Vector<Real>> cnew = jv.
clone();
1387 Ptr<Vector<Real>> znew = z.
clone();
1389 for (
int i=0; i<numSteps; i++) {
1391 Real eta = steps[i];
1396 cdif->scale(
weights[order-1][0]);
1398 for(
int j=0; j<order; ++j) {
1400 znew->axpy(eta*
shifts[order-1][j], v);
1402 if(
weights[order-1][j+1] != 0 ) {
1404 this->
value(*cnew,u,*znew,tol);
1405 cdif->axpy(
weights[order-1][j+1],*cnew);
1410 cdif->scale(one/eta);
1413 jvCheck[i][0] = eta;
1414 jvCheck[i][1] = normJv;
1415 jvCheck[i][2] = cdif->norm();
1416 cdif->axpy(-one, *Jv);
1417 jvCheck[i][3] = cdif->norm();
1419 if (printToStream) {
1420 std::stringstream hist;
1423 << std::setw(20) <<
"Step size"
1424 << std::setw(20) <<
"norm(Jac*vec)"
1425 << std::setw(20) <<
"norm(FD approx)"
1426 << std::setw(20) <<
"norm(abs error)"
1428 << std::setw(20) <<
"---------"
1429 << std::setw(20) <<
"-------------"
1430 << std::setw(20) <<
"---------------"
1431 << std::setw(20) <<
"---------------"
1434 hist << std::scientific << std::setprecision(11) << std::right
1435 << std::setw(20) << jvCheck[i][0]
1436 << std::setw(20) << jvCheck[i][1]
1437 << std::setw(20) << jvCheck[i][2]
1438 << std::setw(20) << jvCheck[i][3]
1440 outStream << hist.str();
1446 outStream.copyfmt(oldFormatState);
1458 const bool printToStream =
true,
1459 std::ostream & outStream = std::cout,
1461 const int order = 1 ) {
1462 std::vector<Real> steps(numSteps);
1463 for(
int i=0;i<numSteps;++i) {
1464 steps[i] = pow(10,-i);
1476 const std::vector<Real> &steps,
1477 const bool printToStream =
true,
1478 std::ostream & outStream = std::cout,
1479 const int order = 1 ) {
1485 Real tol = std::sqrt(ROL_EPSILON<Real>());
1487 int numSteps = steps.size();
1489 std::vector<Real> tmp(numVals);
1490 std::vector<std::vector<Real>> ahpvCheck(numSteps, tmp);
1493 Ptr<Vector<Real>> AJdif = hv.
clone();
1494 Ptr<Vector<Real>> AJp = hv.
clone();
1495 Ptr<Vector<Real>> AHpv = hv.
clone();
1496 Ptr<Vector<Real>> AJnew = hv.
clone();
1497 Ptr<Vector<Real>> unew = u.
clone();
1501 oldFormatState.copyfmt(outStream);
1509 Real normAHpv = AHpv->norm();
1511 for (
int i=0; i<numSteps; i++) {
1513 Real eta = steps[i];
1519 AJdif->scale(
weights[order-1][0]);
1521 for(
int j=0; j<order; ++j) {
1523 unew->axpy(eta*
shifts[order-1][j],v);
1525 if(
weights[order-1][j+1] != 0 ) {
1528 AJdif->axpy(
weights[order-1][j+1],*AJnew);
1532 AJdif->scale(one/eta);
1535 ahpvCheck[i][0] = eta;
1536 ahpvCheck[i][1] = normAHpv;
1537 ahpvCheck[i][2] = AJdif->norm();
1538 AJdif->axpy(-one, *AHpv);
1539 ahpvCheck[i][3] = AJdif->norm();
1541 if (printToStream) {
1542 std::stringstream hist;
1545 << std::setw(20) <<
"Step size"
1546 << std::setw(20) <<
"norm(adj(H)(u,v))"
1547 << std::setw(20) <<
"norm(FD approx)"
1548 << std::setw(20) <<
"norm(abs error)"
1550 << std::setw(20) <<
"---------"
1551 << std::setw(20) <<
"-----------------"
1552 << std::setw(20) <<
"---------------"
1553 << std::setw(20) <<
"---------------"
1556 hist << std::scientific << std::setprecision(11) << std::right
1557 << std::setw(20) << ahpvCheck[i][0]
1558 << std::setw(20) << ahpvCheck[i][1]
1559 << std::setw(20) << ahpvCheck[i][2]
1560 << std::setw(20) << ahpvCheck[i][3]
1562 outStream << hist.str();
1568 outStream.copyfmt(oldFormatState);
1581 const bool printToStream =
true,
1582 std::ostream & outStream = std::cout,
1584 const int order = 1 ) {
1585 std::vector<Real> steps(numSteps);
1586 for(
int i=0;i<numSteps;++i) {
1587 steps[i] = pow(10,-i);
1602 const std::vector<Real> &steps,
1603 const bool printToStream =
true,
1604 std::ostream & outStream = std::cout,
1605 const int order = 1 ) {
1611 Real tol = std::sqrt(ROL_EPSILON<Real>());
1613 int numSteps = steps.size();
1615 std::vector<Real> tmp(numVals);
1616 std::vector<std::vector<Real>> ahpvCheck(numSteps, tmp);
1619 Ptr<Vector<Real>> AJdif = hv.
clone();
1620 Ptr<Vector<Real>> AJp = hv.
clone();
1621 Ptr<Vector<Real>> AHpv = hv.
clone();
1622 Ptr<Vector<Real>> AJnew = hv.
clone();
1623 Ptr<Vector<Real>> znew = z.
clone();
1627 oldFormatState.copyfmt(outStream);
1635 Real normAHpv = AHpv->norm();
1637 for (
int i=0; i<numSteps; i++) {
1639 Real eta = steps[i];
1645 AJdif->scale(
weights[order-1][0]);
1647 for(
int j=0; j<order; ++j) {
1649 znew->axpy(eta*
shifts[order-1][j],v);
1651 if(
weights[order-1][j+1] != 0 ) {
1654 AJdif->axpy(
weights[order-1][j+1],*AJnew);
1658 AJdif->scale(one/eta);
1661 ahpvCheck[i][0] = eta;
1662 ahpvCheck[i][1] = normAHpv;
1663 ahpvCheck[i][2] = AJdif->norm();
1664 AJdif->axpy(-one, *AHpv);
1665 ahpvCheck[i][3] = AJdif->norm();
1667 if (printToStream) {
1668 std::stringstream hist;
1671 << std::setw(20) <<
"Step size"
1672 << std::setw(20) <<
"norm(adj(H)(u,v))"
1673 << std::setw(20) <<
"norm(FD approx)"
1674 << std::setw(20) <<
"norm(abs error)"
1676 << std::setw(20) <<
"---------"
1677 << std::setw(20) <<
"-----------------"
1678 << std::setw(20) <<
"---------------"
1679 << std::setw(20) <<
"---------------"
1682 hist << std::scientific << std::setprecision(11) << std::right
1683 << std::setw(20) << ahpvCheck[i][0]
1684 << std::setw(20) << ahpvCheck[i][1]
1685 << std::setw(20) << ahpvCheck[i][2]
1686 << std::setw(20) << ahpvCheck[i][3]
1688 outStream << hist.str();
1694 outStream.copyfmt(oldFormatState);
1707 const bool printToStream =
true,
1708 std::ostream & outStream = std::cout,
1710 const int order = 1 ) {
1711 std::vector<Real> steps(numSteps);
1712 for(
int i=0;i<numSteps;++i) {
1713 steps[i] = pow(10,-i);
1726 const std::vector<Real> &steps,
1727 const bool printToStream =
true,
1728 std::ostream & outStream = std::cout,
1729 const int order = 1 ) {
1735 Real tol = std::sqrt(ROL_EPSILON<Real>());
1737 int numSteps = steps.size();
1739 std::vector<Real> tmp(numVals);
1740 std::vector<std::vector<Real>> ahpvCheck(numSteps, tmp);
1743 Ptr<Vector<Real>> AJdif = hv.
clone();
1744 Ptr<Vector<Real>> AJp = hv.
clone();
1745 Ptr<Vector<Real>> AHpv = hv.
clone();
1746 Ptr<Vector<Real>> AJnew = hv.
clone();
1747 Ptr<Vector<Real>> unew = u.
clone();
1751 oldFormatState.copyfmt(outStream);
1759 Real normAHpv = AHpv->norm();
1761 for (
int i=0; i<numSteps; i++) {
1763 Real eta = steps[i];
1769 AJdif->scale(
weights[order-1][0]);
1771 for(
int j=0; j<order; ++j) {
1773 unew->axpy(eta*
shifts[order-1][j],v);
1775 if(
weights[order-1][j+1] != 0 ) {
1778 AJdif->axpy(
weights[order-1][j+1],*AJnew);
1782 AJdif->scale(one/eta);
1785 ahpvCheck[i][0] = eta;
1786 ahpvCheck[i][1] = normAHpv;
1787 ahpvCheck[i][2] = AJdif->norm();
1788 AJdif->axpy(-one, *AHpv);
1789 ahpvCheck[i][3] = AJdif->norm();
1791 if (printToStream) {
1792 std::stringstream hist;
1795 << std::setw(20) <<
"Step size"
1796 << std::setw(20) <<
"norm(adj(H)(u,v))"
1797 << std::setw(20) <<
"norm(FD approx)"
1798 << std::setw(20) <<
"norm(abs error)"
1800 << std::setw(20) <<
"---------"
1801 << std::setw(20) <<
"-----------------"
1802 << std::setw(20) <<
"---------------"
1803 << std::setw(20) <<
"---------------"
1806 hist << std::scientific << std::setprecision(11) << std::right
1807 << std::setw(20) << ahpvCheck[i][0]
1808 << std::setw(20) << ahpvCheck[i][1]
1809 << std::setw(20) << ahpvCheck[i][2]
1810 << std::setw(20) << ahpvCheck[i][3]
1812 outStream << hist.str();
1818 outStream.copyfmt(oldFormatState);
1828 const bool printToStream =
true,
1829 std::ostream & outStream = std::cout,
1831 const int order = 1 ) {
1832 std::vector<Real> steps(numSteps);
1833 for(
int i=0;i<numSteps;++i) {
1834 steps[i] = pow(10,-i);
1846 const std::vector<Real> &steps,
1847 const bool printToStream =
true,
1848 std::ostream & outStream = std::cout,
1849 const int order = 1 ) {
1855 Real tol = std::sqrt(ROL_EPSILON<Real>());
1857 int numSteps = steps.size();
1859 std::vector<Real> tmp(numVals);
1860 std::vector<std::vector<Real>> ahpvCheck(numSteps, tmp);
1863 Ptr<Vector<Real>> AJdif = hv.
clone();
1864 Ptr<Vector<Real>> AJp = hv.
clone();
1865 Ptr<Vector<Real>> AHpv = hv.
clone();
1866 Ptr<Vector<Real>> AJnew = hv.
clone();
1867 Ptr<Vector<Real>> znew = z.
clone();
1871 oldFormatState.copyfmt(outStream);
1879 Real normAHpv = AHpv->norm();
1881 for (
int i=0; i<numSteps; i++) {
1883 Real eta = steps[i];
1889 AJdif->scale(
weights[order-1][0]);
1891 for(
int j=0; j<order; ++j) {
1893 znew->axpy(eta*
shifts[order-1][j],v);
1895 if(
weights[order-1][j+1] != 0 ) {
1898 AJdif->axpy(
weights[order-1][j+1],*AJnew);
1902 AJdif->scale(one/eta);
1905 ahpvCheck[i][0] = eta;
1906 ahpvCheck[i][1] = normAHpv;
1907 ahpvCheck[i][2] = AJdif->norm();
1908 AJdif->axpy(-one, *AHpv);
1909 ahpvCheck[i][3] = AJdif->norm();
1911 if (printToStream) {
1912 std::stringstream hist;
1915 << std::setw(20) <<
"Step size"
1916 << std::setw(20) <<
"norm(adj(H)(u,v))"
1917 << std::setw(20) <<
"norm(FD approx)"
1918 << std::setw(20) <<
"norm(abs error)"
1920 << std::setw(20) <<
"---------"
1921 << std::setw(20) <<
"-----------------"
1922 << std::setw(20) <<
"---------------"
1923 << std::setw(20) <<
"---------------"
1926 hist << std::scientific << std::setprecision(11) << std::right
1927 << std::setw(20) << ahpvCheck[i][0]
1928 << std::setw(20) << ahpvCheck[i][1]
1929 << std::setw(20) << ahpvCheck[i][2]
1930 << std::setw(20) << ahpvCheck[i][3]
1932 outStream << hist.str();
1938 outStream.copyfmt(oldFormatState);
virtual void value(Vector< Real > &c, const Vector< Real > &x, Real &tol)
Evaluate the constraint operator at .
virtual const Vector & dual() const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis...
virtual void applyPreconditioner(Vector< Real > &pv, const Vector< Real > &v, const Vector< Real > &x, const Vector< Real > &g, Real &tol)
Apply a constraint preconditioner at , , to vector . Ideally, this preconditioner satisfies the follo...
virtual void scale(const Real alpha)=0
Compute where .
virtual void applyJacobian_2(Vector< Real > &jv, const Vector< Real > &v, const Vector< Real > &u, const Vector< Real > &z, Real &tol)
Apply the partial constraint Jacobian at , , to the vector .
virtual void applyAdjointHessian(Vector< Real > &ahwv, const Vector< Real > &w, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply the derivative of the adjoint of the constraint Jacobian at to vector in direction ...
virtual ROL::Ptr< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
ROL::Ptr< const Vector< Real > > get_2() const
virtual int dimension() const
Return dimension of the vector space.
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)
, , , ,
virtual ROL::Ptr< Vector > basis(const int i) const
Return i-th basis vector.
virtual void plus(const Vector &x)=0
Compute , where .
const double weights[4][5]
virtual void axpy(const Real alpha, const Vector &x)
Compute where .
virtual void applyJacobian_1(Vector< Real > &jv, const Vector< Real > &v, const Vector< Real > &u, const Vector< Real > &z, Real &tol)
Apply the partial constraint Jacobian at , , to the vector .
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)
virtual Real checkSolve(const Vector< Real > &u, const Vector< Real > &z, const Vector< Real > &c, const bool printToStream=true, std::ostream &outStream=std::cout)
Defines the linear algebra or vector space interface for simulation-based optimization.
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)
Apply the simulation-space derivative of the adjoint of the constraint simulation-space Jacobian at ...
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)
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.
virtual void update(const Vector< Real > &x, bool flag=true, int iter=-1)
Update constraint functions. x is the optimization variable, flag = true if optimization variable is ...
virtual void update_1(const Vector< Real > &u, bool flag=true, int iter=-1)
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 applyAdjointHessian_22(Vector< Real > &ahwv, const Vector< Real > &w, const Vector< Real > &v, const Vector< Real > &u, const Vector< Real > &z, Real &tol)
Apply the optimization-space derivative of the adjoint of the constraint optimization-space Jacobian ...
Contains definitions of custom data types in ROL.
void set_1(const Vector< Real > &vec)
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)
virtual void applyJacobian(Vector< Real > &jv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply the constraint Jacobian at , , to vector .
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)
Check the consistency of the Jacobian and its adjoint. This is the primary interface.
virtual void applyPreconditioner(Vector< Real > &pv, const Vector< Real > &v, const Vector< Real > &x, const Vector< Real > &g, Real &tol)
Apply a constraint preconditioner at , , to vector . In general, this preconditioner satisfies the fo...
virtual void zero()
Set to zero vector.
Defines the linear algebra or vector space interface.
virtual void update(const Vector< Real > &u, const Vector< Real > &z, bool flag=true, int iter=-1)
Update constraint functions. x is the optimization variable, flag = true if optimization variable is ...
virtual Real dot(const Vector &x) const =0
Compute where .
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)
Ptr< Vector< Real > > jv_
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)
virtual void applyInverseJacobian_1(Vector< Real > &ijv, const Vector< Real > &v, const Vector< Real > &u, const Vector< Real > &z, Real &tol)
Apply the inverse partial constraint Jacobian at , , to the vector .
const int DEFAULT_solverType_
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)
Apply the optimization-space derivative of the adjoint of the constraint simulation-space Jacobian at...
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)
Approximately solves the augmented system where , , , , is an identity operator, and is a zero operator.
virtual void applyAdjointJacobian_1(Vector< Real > &ajv, const Vector< Real > &v, const Vector< Real > &u, const Vector< Real > &z, Real &tol)
Apply the adjoint of the partial constraint Jacobian at , , to the vector . This is the primary inter...
virtual void value(Vector< Real > &c, const Vector< Real > &u, const Vector< Real > &z, Real &tol)=0
Evaluate the constraint operator at .
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)
Apply the adjoint of the partial constraint Jacobian at , , to vector . This is the secondary interfa...
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)
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)
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)
Apply the adjoint of the partial constraint Jacobian at , , to the vector . This is the secondary int...
virtual void applyAdjointJacobian_2(Vector< Real > &ajv, const Vector< Real > &v, const Vector< Real > &u, const Vector< Real > &z, Real &tol)
Apply the adjoint of the partial constraint Jacobian at , , to vector . This is the primary interface...
const Real DEFAULT_factor_
#define ROL_NUM_CHECKDERIV_STEPS
Number of steps for derivative checks.
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)
Apply the simulation-space derivative of the adjoint of the constraint optimization-space Jacobian at...
basic_nullstream< char, char_traits< char >> nullstream
virtual void solve(Vector< Real > &c, Vector< Real > &u, const Vector< Real > &z, Real &tol)
Given , solve for .
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)
Approximately solves the augmented system where , , , , is an identity or Riesz operator...
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)
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)
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.
virtual void setSolveParameters(ParameterList &parlist)
Set solve parameters.
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)
Check the consistency of the Jacobian and its adjoint. This is the primary interface.
virtual void set(const Vector &x)
Set where .
virtual Real norm() const =0
Returns where .
virtual void update_2(const Vector< Real > &z, bool flag=true, int iter=-1)
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.
Ptr< Vector< Real > > unew_
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)
Real ROL_EPSILON(void)
Platform-dependent machine epsilon.
Defines the constraint operator interface for simulation-based optimization.
virtual void applyAdjointJacobian(Vector< Real > &ajv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply the adjoint of the the constraint Jacobian at , , to vector .
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< 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)
, , , ,
virtual void applyInverseAdjointJacobian_1(Vector< Real > &iajv, const Vector< Real > &v, const Vector< Real > &u, const Vector< Real > &z, Real &tol)
Apply the inverse of the adjoint of the partial constraint Jacobian at , , to the vector ...
void set_2(const Vector< Real > &vec)
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)
, , , ,
Defines the general constraint operator interface.
const bool DEFAULT_print_
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)
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)
const Vector< Real > & dual(void) const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis...
ROL::Ptr< const Vector< Real > > get_1() const