44 #ifndef ROL_CONSTRAINT_SIMOPT_H
45 #define ROL_CONSTRAINT_SIMOPT_H
102 template <
class Real>
214 Real cnorm = c.
norm();
215 const Real ctol = std::min(
atol_,
rtol_*cnorm);
222 Real alpha(1), tmp(0);
225 std::cout <<
"\n Default Constraint_SimOpt::solve\n";
227 std::cout << std::setw(6) << std::left <<
"iter";
228 std::cout << std::setw(15) << std::left <<
"rnorm";
229 std::cout << std::setw(15) << std::left <<
"alpha";
232 for (cnt = 0; cnt <
maxit_; ++cnt) {
242 while ( tmp > (1.0-
decr_*alpha)*cnorm &&
254 std::cout << std::setw(6) << std::left << cnt;
255 std::cout << std::scientific << std::setprecision(6);
256 std::cout << std::setw(15) << std::left << tmp;
257 std::cout << std::scientific << std::setprecision(6);
258 std::cout << std::setw(15) << std::left << alpha;
272 Ptr<Constraint_SimOpt<Real>> con = makePtrFromRef(*
this);
273 Ptr<Objective<Real>> obj = makePtr<NonlinearLeastSquaresObjective_SimOpt<Real>>(con,u,z,c,
true);
274 ParameterList parlist;
275 parlist.sublist(
"Status Test").set(
"Gradient Tolerance",ctol);
276 parlist.sublist(
"Status Test").set(
"Step Tolerance",
stol_);
277 parlist.sublist(
"Status Test").set(
"Iteration Limit",
maxit_);
278 parlist.sublist(
"Step").sublist(
"Trust Region").set(
"Subproblem Solver",
"Truncated CG");
279 parlist.sublist(
"General").sublist(
"Krylov").set(
"Iteration Limit",100);
280 Ptr<Step<Real>> step = makePtr<TrustRegionStep<Real>>(parlist);
281 Ptr<StatusTest<Real>> status = makePtr<StatusTest<Real>>(parlist);
282 Ptr<Algorithm<Real>> algo = makePtr<Algorithm<Real>>(step,status,
false);
287 Ptr<Constraint_SimOpt<Real>> con = makePtrFromRef(*
this);
288 Ptr<const Vector<Real>> zVec = makePtrFromRef(z);
289 Ptr<Constraint<Real>> conU
290 = makePtr<Constraint_State<Real>>(con,zVec);
291 Ptr<Objective<Real>> objU
292 = makePtr<Objective_FSsolver<Real>>();
293 ParameterList parlist;
294 parlist.sublist(
"Status Test").set(
"Constraint Tolerance",ctol);
295 parlist.sublist(
"Status Test").set(
"Step Tolerance",
stol_);
296 parlist.sublist(
"Status Test").set(
"Iteration Limit",
maxit_);
297 Ptr<Step<Real>> step = makePtr<CompositeStep<Real>>(parlist);
298 Ptr<StatusTest<Real>> status = makePtr<ConstraintStatusTest<Real>>(parlist);
299 Ptr<Algorithm<Real>> algo = makePtr<Algorithm<Real>>(step,status,
false);
300 Ptr<Vector<Real>> l = c.
dual().clone();
301 algo->run(u,*l,*objU,*conU,
print_);
305 ROL_TEST_FOR_EXCEPTION(
true, std::invalid_argument,
306 ">>> ERROR (ROL:Constraint_SimOpt:solve): Invalid solver type!");
331 ParameterList & list = parlist.sublist(
"SimOpt").sublist(
"Solve");
363 Real ctol = std::sqrt(ROL_EPSILON<Real>());
366 if (v.
norm() > std::sqrt(ROL_EPSILON<Real>())) {
367 h = std::max(1.0,u.
norm()/v.
norm())*tol;
370 Ptr<Vector<Real>> unew = u.
clone();
375 value(jv,*unew,z,ctol);
377 Ptr<Vector<Real>> cold = jv.
clone();
379 value(*cold,u,z,ctol);
406 Real ctol = std::sqrt(ROL_EPSILON<Real>());
409 if (v.
norm() > std::sqrt(ROL_EPSILON<Real>())) {
410 h = std::max(1.0,u.
norm()/v.
norm())*tol;
413 Ptr<Vector<Real>> znew = z.
clone();
418 value(jv,u,*znew,ctol);
420 Ptr<Vector<Real>> cold = jv.
clone();
422 value(*cold,u,z,ctol);
448 ROL_TEST_FOR_EXCEPTION(
true, std::logic_error,
449 "The method applyInverseJacobian_1 is used but not implemented!\n");
499 Real ctol = std::sqrt(ROL_EPSILON<Real>());
501 if (v.
norm() > std::sqrt(ROL_EPSILON<Real>())) {
502 h = std::max(1.0,u.
norm()/v.
norm())*tol;
504 Ptr<Vector<Real>> cold = dualv.
clone();
505 Ptr<Vector<Real>> cnew = dualv.
clone();
507 value(*cold,u,z,ctol);
508 Ptr<Vector<Real>> unew = u.
clone();
510 for (
int i = 0; i < u.
dimension(); i++) {
512 unew->axpy(h,*(u.
basis(i)));
514 value(*cnew,*unew,z,ctol);
515 cnew->axpy(-1.0,*cold);
517 ajv.
axpy(cnew->dot(v),*((u.
dual()).basis(i)));
570 Real ctol = std::sqrt(ROL_EPSILON<Real>());
572 if (v.
norm() > std::sqrt(ROL_EPSILON<Real>())) {
573 h = std::max(1.0,u.
norm()/v.
norm())*tol;
575 Ptr<Vector<Real>> cold = dualv.
clone();
576 Ptr<Vector<Real>> cnew = dualv.
clone();
578 value(*cold,u,z,ctol);
579 Ptr<Vector<Real>> znew = z.
clone();
581 for (
int i = 0; i < z.
dimension(); i++) {
583 znew->axpy(h,*(z.
basis(i)));
585 value(*cnew,u,*znew,ctol);
586 cnew->axpy(-1.0,*cold);
588 ajv.
axpy(cnew->dot(v),*((z.
dual()).basis(i)));
613 ROL_TEST_FOR_EXCEPTION(
true, std::logic_error,
614 "The method applyInverseAdjointJacobian_1 is used but not implemented!\n");
640 Real jtol = std::sqrt(ROL_EPSILON<Real>());
643 if (v.
norm() > std::sqrt(ROL_EPSILON<Real>())) {
644 h = std::max(1.0,u.
norm()/v.
norm())*tol;
647 Ptr<Vector<Real>> unew = u.
clone();
653 Ptr<Vector<Real>> jv = ahwv.
clone();
685 Real jtol = std::sqrt(ROL_EPSILON<Real>());
688 if (v.
norm() > std::sqrt(ROL_EPSILON<Real>())) {
689 h = std::max(1.0,u.
norm()/v.
norm())*tol;
692 Ptr<Vector<Real>> unew = u.
clone();
698 Ptr<Vector<Real>> jv = ahwv.
clone();
730 Real jtol = std::sqrt(ROL_EPSILON<Real>());
733 if (v.
norm() > std::sqrt(ROL_EPSILON<Real>())) {
734 h = std::max(1.0,u.
norm()/v.
norm())*tol;
737 Ptr<Vector<Real>> znew = z.
clone();
743 Ptr<Vector<Real>> jv = ahwv.
clone();
774 Real jtol = std::sqrt(ROL_EPSILON<Real>());
777 if (v.
norm() > std::sqrt(ROL_EPSILON<Real>())) {
778 h = std::max(1.0,u.
norm()/v.
norm())*tol;
781 Ptr<Vector<Real>> znew = z.
clone();
787 Ptr<Vector<Real>> jv = ahwv.
clone();
867 Ptr<Vector<Real>> ijv = (xs.
get_1())->clone();
872 catch (
const std::logic_error &e) {
878 Ptr<Vector<Real>> ijv_dual = (gs.
get_1())->clone();
884 catch (
const std::logic_error &e) {
920 Ptr<Vector<Real>> jv2 = jv.
clone();
934 Ptr<Vector<Real>> ajv1 = (ajvs.
get_1())->clone();
937 Ptr<Vector<Real>> ajv2 = (ajvs.
get_2())->clone();
955 Ptr<Vector<Real>> C11 = (ahwvs.
get_1())->clone();
956 Ptr<Vector<Real>> C21 = (ahwvs.
get_1())->clone();
962 Ptr<Vector<Real>> C12 = (ahwvs.
get_2())->clone();
963 Ptr<Vector<Real>> C22 = (ahwvs.
get_2())->clone();
975 const bool printToStream =
true,
976 std::ostream & outStream = std::cout) {
978 Real tol = ROL_EPSILON<Real>();
979 Ptr<Vector<Real>> r = c.
clone();
980 Ptr<Vector<Real>> s = u.
clone();
983 Ptr<Vector<Real>> cs = c.
clone();
987 Real rnorm = r->norm();
988 Real cnorm = cs->norm();
989 if ( printToStream ) {
990 std::stringstream hist;
991 hist << std::scientific << std::setprecision(8);
992 hist <<
"\nTest SimOpt solve at feasible (u,z):\n";
993 hist <<
" Solver Residual = " << rnorm <<
"\n";
994 hist <<
" ||c(u,z)|| = " << cnorm <<
"\n";
995 outStream << hist.str();
1017 const bool printToStream =
true,
1018 std::ostream & outStream = std::cout) {
1046 const bool printToStream =
true,
1047 std::ostream & outStream = std::cout) {
1048 Real tol = ROL_EPSILON<Real>();
1049 Ptr<Vector<Real>> Jv = dualw.
clone();
1052 Real wJv = w.
dot(Jv->dual());
1053 Ptr<Vector<Real>> Jw = dualv.
clone();
1056 Real vJw = v.
dot(Jw->dual());
1057 Real diff = std::abs(wJv-vJw);
1058 if ( printToStream ) {
1059 std::stringstream hist;
1060 hist << std::scientific << std::setprecision(8);
1061 hist <<
"\nTest SimOpt consistency of Jacobian_1 and its adjoint: \n |<w,Jv> - <adj(J)w,v>| = "
1063 hist <<
" |<w,Jv>| = " << std::abs(wJv) <<
"\n";
1064 hist <<
" Relative Error = " << diff / (std::abs(wJv)+ROL_UNDERFLOW<Real>()) <<
"\n";
1065 outStream << hist.str();
1087 const bool printToStream =
true,
1088 std::ostream & outStream = std::cout) {
1113 const bool printToStream =
true,
1114 std::ostream & outStream = std::cout) {
1115 Real tol = ROL_EPSILON<Real>();
1116 Ptr<Vector<Real>> Jv = dualw.
clone();
1119 Real wJv = w.
dot(Jv->dual());
1120 Ptr<Vector<Real>> Jw = dualv.
clone();
1123 Real vJw = v.
dot(Jw->dual());
1124 Real diff = std::abs(wJv-vJw);
1125 if ( printToStream ) {
1126 std::stringstream hist;
1127 hist << std::scientific << std::setprecision(8);
1128 hist <<
"\nTest SimOpt consistency of Jacobian_2 and its adjoint: \n |<w,Jv> - <adj(J)w,v>| = "
1130 hist <<
" |<w,Jv>| = " << std::abs(wJv) <<
"\n";
1131 hist <<
" Relative Error = " << diff / (std::abs(wJv)+ROL_UNDERFLOW<Real>()) <<
"\n";
1132 outStream << hist.str();
1141 const bool printToStream =
true,
1142 std::ostream & outStream = std::cout) {
1143 Real tol = ROL_EPSILON<Real>();
1144 Ptr<Vector<Real>> Jv = jv.
clone();
1147 Ptr<Vector<Real>> iJJv = u.
clone();
1150 Ptr<Vector<Real>> diff = v.
clone();
1152 diff->axpy(-1.0,*iJJv);
1153 Real dnorm = diff->norm();
1154 Real vnorm = v.
norm();
1155 if ( printToStream ) {
1156 std::stringstream hist;
1157 hist << std::scientific << std::setprecision(8);
1158 hist <<
"\nTest SimOpt consistency of inverse Jacobian_1: \n ||v-inv(J)Jv|| = "
1160 hist <<
" ||v|| = " << vnorm <<
"\n";
1161 hist <<
" Relative Error = " << dnorm / (vnorm+ROL_UNDERFLOW<Real>()) <<
"\n";
1162 outStream << hist.str();
1171 const bool printToStream =
true,
1172 std::ostream & outStream = std::cout) {
1173 Real tol = ROL_EPSILON<Real>();
1174 Ptr<Vector<Real>> Jv = jv.
clone();
1177 Ptr<Vector<Real>> iJJv = v.
clone();
1180 Ptr<Vector<Real>> diff = v.
clone();
1182 diff->axpy(-1.0,*iJJv);
1183 Real dnorm = diff->norm();
1184 Real vnorm = v.
norm();
1185 if ( printToStream ) {
1186 std::stringstream hist;
1187 hist << std::scientific << std::setprecision(8);
1188 hist <<
"\nTest SimOpt consistency of inverse adjoint Jacobian_1: \n ||v-inv(adj(J))adj(J)v|| = "
1190 hist <<
" ||v|| = " << vnorm <<
"\n";
1191 hist <<
" Relative Error = " << dnorm / (vnorm+ROL_UNDERFLOW<Real>()) <<
"\n";
1192 outStream << hist.str();
1203 const bool printToStream =
true,
1204 std::ostream & outStream = std::cout,
1206 const int order = 1) {
1207 std::vector<Real> steps(numSteps);
1208 for(
int i=0;i<numSteps;++i) {
1209 steps[i] = pow(10,-i);
1222 const std::vector<Real> &steps,
1223 const bool printToStream =
true,
1224 std::ostream & outStream = std::cout,
1225 const int order = 1) {
1227 ROL_TEST_FOR_EXCEPTION( order<1 || order>4, std::invalid_argument,
1228 "Error: finite difference order must be 1,2,3, or 4" );
1235 Real tol = std::sqrt(ROL_EPSILON<Real>());
1237 int numSteps = steps.size();
1239 std::vector<Real> tmp(numVals);
1240 std::vector<std::vector<Real>> jvCheck(numSteps, tmp);
1244 oldFormatState.copyfmt(outStream);
1247 Ptr<Vector<Real>> c = jv.
clone();
1249 this->
value(*c, u, z, tol);
1252 Ptr<Vector<Real>> Jv = jv.
clone();
1254 Real normJv = Jv->norm();
1257 Ptr<Vector<Real>> cdif = jv.
clone();
1258 Ptr<Vector<Real>> cnew = jv.
clone();
1259 Ptr<Vector<Real>> unew = u.
clone();
1261 for (
int i=0; i<numSteps; i++) {
1263 Real eta = steps[i];
1268 cdif->scale(
weights[order-1][0]);
1270 for(
int j=0; j<order; ++j) {
1272 unew->axpy(eta*
shifts[order-1][j], v);
1274 if(
weights[order-1][j+1] != 0 ) {
1276 this->
value(*cnew,*unew,z,tol);
1277 cdif->axpy(
weights[order-1][j+1],*cnew);
1282 cdif->scale(one/eta);
1285 jvCheck[i][0] = eta;
1286 jvCheck[i][1] = normJv;
1287 jvCheck[i][2] = cdif->norm();
1288 cdif->axpy(-one, *Jv);
1289 jvCheck[i][3] = cdif->norm();
1291 if (printToStream) {
1292 std::stringstream hist;
1295 << std::setw(20) <<
"Step size"
1296 << std::setw(20) <<
"norm(Jac*vec)"
1297 << std::setw(20) <<
"norm(FD approx)"
1298 << std::setw(20) <<
"norm(abs error)"
1300 << std::setw(20) <<
"---------"
1301 << std::setw(20) <<
"-------------"
1302 << std::setw(20) <<
"---------------"
1303 << std::setw(20) <<
"---------------"
1306 hist << std::scientific << std::setprecision(11) << std::right
1307 << std::setw(20) << jvCheck[i][0]
1308 << std::setw(20) << jvCheck[i][1]
1309 << std::setw(20) << jvCheck[i][2]
1310 << std::setw(20) << jvCheck[i][3]
1312 outStream << hist.str();
1318 outStream.copyfmt(oldFormatState);
1328 const bool printToStream =
true,
1329 std::ostream & outStream = std::cout,
1331 const int order = 1) {
1332 std::vector<Real> steps(numSteps);
1333 for(
int i=0;i<numSteps;++i) {
1334 steps[i] = pow(10,-i);
1347 const std::vector<Real> &steps,
1348 const bool printToStream =
true,
1349 std::ostream & outStream = std::cout,
1350 const int order = 1) {
1352 ROL_TEST_FOR_EXCEPTION( order<1 || order>4, std::invalid_argument,
1353 "Error: finite difference order must be 1,2,3, or 4" );
1360 Real tol = std::sqrt(ROL_EPSILON<Real>());
1362 int numSteps = steps.size();
1364 std::vector<Real> tmp(numVals);
1365 std::vector<std::vector<Real>> jvCheck(numSteps, tmp);
1369 oldFormatState.copyfmt(outStream);
1372 Ptr<Vector<Real>> c = jv.
clone();
1374 this->
value(*c, u, z, tol);
1377 Ptr<Vector<Real>> Jv = jv.
clone();
1379 Real normJv = Jv->norm();
1382 Ptr<Vector<Real>> cdif = jv.
clone();
1383 Ptr<Vector<Real>> cnew = jv.
clone();
1384 Ptr<Vector<Real>> znew = z.
clone();
1386 for (
int i=0; i<numSteps; i++) {
1388 Real eta = steps[i];
1393 cdif->scale(
weights[order-1][0]);
1395 for(
int j=0; j<order; ++j) {
1397 znew->axpy(eta*
shifts[order-1][j], v);
1399 if(
weights[order-1][j+1] != 0 ) {
1401 this->
value(*cnew,u,*znew,tol);
1402 cdif->axpy(
weights[order-1][j+1],*cnew);
1407 cdif->scale(one/eta);
1410 jvCheck[i][0] = eta;
1411 jvCheck[i][1] = normJv;
1412 jvCheck[i][2] = cdif->norm();
1413 cdif->axpy(-one, *Jv);
1414 jvCheck[i][3] = cdif->norm();
1416 if (printToStream) {
1417 std::stringstream hist;
1420 << std::setw(20) <<
"Step size"
1421 << std::setw(20) <<
"norm(Jac*vec)"
1422 << std::setw(20) <<
"norm(FD approx)"
1423 << std::setw(20) <<
"norm(abs error)"
1425 << std::setw(20) <<
"---------"
1426 << std::setw(20) <<
"-------------"
1427 << std::setw(20) <<
"---------------"
1428 << std::setw(20) <<
"---------------"
1431 hist << std::scientific << std::setprecision(11) << std::right
1432 << std::setw(20) << jvCheck[i][0]
1433 << std::setw(20) << jvCheck[i][1]
1434 << std::setw(20) << jvCheck[i][2]
1435 << std::setw(20) << jvCheck[i][3]
1437 outStream << hist.str();
1443 outStream.copyfmt(oldFormatState);
1455 const bool printToStream =
true,
1456 std::ostream & outStream = std::cout,
1458 const int order = 1 ) {
1459 std::vector<Real> steps(numSteps);
1460 for(
int i=0;i<numSteps;++i) {
1461 steps[i] = pow(10,-i);
1473 const std::vector<Real> &steps,
1474 const bool printToStream =
true,
1475 std::ostream & outStream = std::cout,
1476 const int order = 1 ) {
1482 Real tol = std::sqrt(ROL_EPSILON<Real>());
1484 int numSteps = steps.size();
1486 std::vector<Real> tmp(numVals);
1487 std::vector<std::vector<Real>> ahpvCheck(numSteps, tmp);
1490 Ptr<Vector<Real>> AJdif = hv.
clone();
1491 Ptr<Vector<Real>> AJp = hv.
clone();
1492 Ptr<Vector<Real>> AHpv = hv.
clone();
1493 Ptr<Vector<Real>> AJnew = hv.
clone();
1494 Ptr<Vector<Real>> unew = u.
clone();
1498 oldFormatState.copyfmt(outStream);
1506 Real normAHpv = AHpv->norm();
1508 for (
int i=0; i<numSteps; i++) {
1510 Real eta = steps[i];
1516 AJdif->scale(
weights[order-1][0]);
1518 for(
int j=0; j<order; ++j) {
1520 unew->axpy(eta*
shifts[order-1][j],v);
1522 if(
weights[order-1][j+1] != 0 ) {
1525 AJdif->axpy(
weights[order-1][j+1],*AJnew);
1529 AJdif->scale(one/eta);
1532 ahpvCheck[i][0] = eta;
1533 ahpvCheck[i][1] = normAHpv;
1534 ahpvCheck[i][2] = AJdif->norm();
1535 AJdif->axpy(-one, *AHpv);
1536 ahpvCheck[i][3] = AJdif->norm();
1538 if (printToStream) {
1539 std::stringstream hist;
1542 << std::setw(20) <<
"Step size"
1543 << std::setw(20) <<
"norm(adj(H)(u,v))"
1544 << std::setw(20) <<
"norm(FD approx)"
1545 << std::setw(20) <<
"norm(abs error)"
1547 << std::setw(20) <<
"---------"
1548 << std::setw(20) <<
"-----------------"
1549 << std::setw(20) <<
"---------------"
1550 << std::setw(20) <<
"---------------"
1553 hist << std::scientific << std::setprecision(11) << std::right
1554 << std::setw(20) << ahpvCheck[i][0]
1555 << std::setw(20) << ahpvCheck[i][1]
1556 << std::setw(20) << ahpvCheck[i][2]
1557 << std::setw(20) << ahpvCheck[i][3]
1559 outStream << hist.str();
1565 outStream.copyfmt(oldFormatState);
1578 const bool printToStream =
true,
1579 std::ostream & outStream = std::cout,
1581 const int order = 1 ) {
1582 std::vector<Real> steps(numSteps);
1583 for(
int i=0;i<numSteps;++i) {
1584 steps[i] = pow(10,-i);
1599 const std::vector<Real> &steps,
1600 const bool printToStream =
true,
1601 std::ostream & outStream = std::cout,
1602 const int order = 1 ) {
1608 Real tol = std::sqrt(ROL_EPSILON<Real>());
1610 int numSteps = steps.size();
1612 std::vector<Real> tmp(numVals);
1613 std::vector<std::vector<Real>> ahpvCheck(numSteps, tmp);
1616 Ptr<Vector<Real>> AJdif = hv.
clone();
1617 Ptr<Vector<Real>> AJp = hv.
clone();
1618 Ptr<Vector<Real>> AHpv = hv.
clone();
1619 Ptr<Vector<Real>> AJnew = hv.
clone();
1620 Ptr<Vector<Real>> znew = z.
clone();
1624 oldFormatState.copyfmt(outStream);
1632 Real normAHpv = AHpv->norm();
1634 for (
int i=0; i<numSteps; i++) {
1636 Real eta = steps[i];
1642 AJdif->scale(
weights[order-1][0]);
1644 for(
int j=0; j<order; ++j) {
1646 znew->axpy(eta*
shifts[order-1][j],v);
1648 if(
weights[order-1][j+1] != 0 ) {
1651 AJdif->axpy(
weights[order-1][j+1],*AJnew);
1655 AJdif->scale(one/eta);
1658 ahpvCheck[i][0] = eta;
1659 ahpvCheck[i][1] = normAHpv;
1660 ahpvCheck[i][2] = AJdif->norm();
1661 AJdif->axpy(-one, *AHpv);
1662 ahpvCheck[i][3] = AJdif->norm();
1664 if (printToStream) {
1665 std::stringstream hist;
1668 << std::setw(20) <<
"Step size"
1669 << std::setw(20) <<
"norm(adj(H)(u,v))"
1670 << std::setw(20) <<
"norm(FD approx)"
1671 << std::setw(20) <<
"norm(abs error)"
1673 << std::setw(20) <<
"---------"
1674 << std::setw(20) <<
"-----------------"
1675 << std::setw(20) <<
"---------------"
1676 << std::setw(20) <<
"---------------"
1679 hist << std::scientific << std::setprecision(11) << std::right
1680 << std::setw(20) << ahpvCheck[i][0]
1681 << std::setw(20) << ahpvCheck[i][1]
1682 << std::setw(20) << ahpvCheck[i][2]
1683 << std::setw(20) << ahpvCheck[i][3]
1685 outStream << hist.str();
1691 outStream.copyfmt(oldFormatState);
1704 const bool printToStream =
true,
1705 std::ostream & outStream = std::cout,
1707 const int order = 1 ) {
1708 std::vector<Real> steps(numSteps);
1709 for(
int i=0;i<numSteps;++i) {
1710 steps[i] = pow(10,-i);
1723 const std::vector<Real> &steps,
1724 const bool printToStream =
true,
1725 std::ostream & outStream = std::cout,
1726 const int order = 1 ) {
1732 Real tol = std::sqrt(ROL_EPSILON<Real>());
1734 int numSteps = steps.size();
1736 std::vector<Real> tmp(numVals);
1737 std::vector<std::vector<Real>> ahpvCheck(numSteps, tmp);
1740 Ptr<Vector<Real>> AJdif = hv.
clone();
1741 Ptr<Vector<Real>> AJp = hv.
clone();
1742 Ptr<Vector<Real>> AHpv = hv.
clone();
1743 Ptr<Vector<Real>> AJnew = hv.
clone();
1744 Ptr<Vector<Real>> unew = u.
clone();
1748 oldFormatState.copyfmt(outStream);
1756 Real normAHpv = AHpv->norm();
1758 for (
int i=0; i<numSteps; i++) {
1760 Real eta = steps[i];
1766 AJdif->scale(
weights[order-1][0]);
1768 for(
int j=0; j<order; ++j) {
1770 unew->axpy(eta*
shifts[order-1][j],v);
1772 if(
weights[order-1][j+1] != 0 ) {
1775 AJdif->axpy(
weights[order-1][j+1],*AJnew);
1779 AJdif->scale(one/eta);
1782 ahpvCheck[i][0] = eta;
1783 ahpvCheck[i][1] = normAHpv;
1784 ahpvCheck[i][2] = AJdif->norm();
1785 AJdif->axpy(-one, *AHpv);
1786 ahpvCheck[i][3] = AJdif->norm();
1788 if (printToStream) {
1789 std::stringstream hist;
1792 << std::setw(20) <<
"Step size"
1793 << std::setw(20) <<
"norm(adj(H)(u,v))"
1794 << std::setw(20) <<
"norm(FD approx)"
1795 << std::setw(20) <<
"norm(abs error)"
1797 << std::setw(20) <<
"---------"
1798 << std::setw(20) <<
"-----------------"
1799 << std::setw(20) <<
"---------------"
1800 << std::setw(20) <<
"---------------"
1803 hist << std::scientific << std::setprecision(11) << std::right
1804 << std::setw(20) << ahpvCheck[i][0]
1805 << std::setw(20) << ahpvCheck[i][1]
1806 << std::setw(20) << ahpvCheck[i][2]
1807 << std::setw(20) << ahpvCheck[i][3]
1809 outStream << hist.str();
1815 outStream.copyfmt(oldFormatState);
1825 const bool printToStream =
true,
1826 std::ostream & outStream = std::cout,
1828 const int order = 1 ) {
1829 std::vector<Real> steps(numSteps);
1830 for(
int i=0;i<numSteps;++i) {
1831 steps[i] = pow(10,-i);
1843 const std::vector<Real> &steps,
1844 const bool printToStream =
true,
1845 std::ostream & outStream = std::cout,
1846 const int order = 1 ) {
1852 Real tol = std::sqrt(ROL_EPSILON<Real>());
1854 int numSteps = steps.size();
1856 std::vector<Real> tmp(numVals);
1857 std::vector<std::vector<Real>> ahpvCheck(numSteps, tmp);
1860 Ptr<Vector<Real>> AJdif = hv.
clone();
1861 Ptr<Vector<Real>> AJp = hv.
clone();
1862 Ptr<Vector<Real>> AHpv = hv.
clone();
1863 Ptr<Vector<Real>> AJnew = hv.
clone();
1864 Ptr<Vector<Real>> znew = z.
clone();
1868 oldFormatState.copyfmt(outStream);
1876 Real normAHpv = AHpv->norm();
1878 for (
int i=0; i<numSteps; i++) {
1880 Real eta = steps[i];
1886 AJdif->scale(
weights[order-1][0]);
1888 for(
int j=0; j<order; ++j) {
1890 znew->axpy(eta*
shifts[order-1][j],v);
1892 if(
weights[order-1][j+1] != 0 ) {
1895 AJdif->axpy(
weights[order-1][j+1],*AJnew);
1899 AJdif->scale(one/eta);
1902 ahpvCheck[i][0] = eta;
1903 ahpvCheck[i][1] = normAHpv;
1904 ahpvCheck[i][2] = AJdif->norm();
1905 AJdif->axpy(-one, *AHpv);
1906 ahpvCheck[i][3] = AJdif->norm();
1908 if (printToStream) {
1909 std::stringstream hist;
1912 << std::setw(20) <<
"Step size"
1913 << std::setw(20) <<
"norm(adj(H)(u,v))"
1914 << std::setw(20) <<
"norm(FD approx)"
1915 << std::setw(20) <<
"norm(abs error)"
1917 << std::setw(20) <<
"---------"
1918 << std::setw(20) <<
"-----------------"
1919 << std::setw(20) <<
"---------------"
1920 << std::setw(20) <<
"---------------"
1923 hist << std::scientific << std::setprecision(11) << std::right
1924 << std::setw(20) << ahpvCheck[i][0]
1925 << std::setw(20) << ahpvCheck[i][1]
1926 << std::setw(20) << ahpvCheck[i][2]
1927 << std::setw(20) << ahpvCheck[i][3]
1929 outStream << hist.str();
1935 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