49 #ifdef HAVE_STOKHOS_GLPK
55 #ifdef HAVE_STOKHOS_CLP
56 #include "coin/ClpSimplex.hpp"
57 #include "coin/CoinBuild.hpp"
58 #include "coin/ClpInterior.hpp"
61 #ifdef HAVE_STOKHOS_QPOASES
62 #include "qpOASES.hpp"
65 #ifdef HAVE_STOKHOS_DAKOTA
66 #include "LinearSolver.hpp"
69 template <
typename ordinal_type,
typename value_type>
74 reduction_method(params.get(
"Reduced Quadrature Method",
"Q Squared")),
75 solver_method(params.get(
"Solver Method",
"TRSM")),
76 eliminate_dependent_rows(params.get(
"Eliminate Dependent Rows",
true)),
77 verbose(params.get(
"Verbose", false)),
78 reduction_tol(params.get(
"Reduction Tolerance", 1.0e-12)),
79 objective_value(params.get(
"Objective Value", 0.0))
83 template <
typename ordinal_type,
typename value_type>
97 if (reduction_method ==
"Q Squared") {
98 if (eliminate_dependent_rows)
99 reducedQuadrature_Q_Squared_CPQR(Q, F, weights,
100 red_weights, red_points, red_values);
102 reducedQuadrature_Q_Squared(Q, F, weights,
103 red_weights, red_points, red_values);
105 else if (reduction_method ==
"Q Squared2") {
106 reducedQuadrature_Q_Squared_CPQR2(Q, F, weights,
107 red_weights, red_points, red_values);
109 else if (reduction_method ==
"Q2") {
110 if (eliminate_dependent_rows)
111 reducedQuadrature_Q2_CPQR(Q, Q2, F, weights,
112 red_weights, red_points, red_values);
114 reducedQuadrature_Q2(Q, Q2, F, weights,
115 red_weights, red_points, red_values);
117 else if (reduction_method ==
"None") {
128 (*red_points)[i].resize(d);
130 (*red_points)[i][
j] = F(i,
j);
131 (*red_values)[i].resize(sz);
133 (*red_values)[i][
j] = Q(i,
j);
138 true, std::logic_error,
139 "Invalid dimension reduction method " << reduction_method);
157 template <
typename ordinal_type,
typename value_type>
180 Q2(i,jdx) = Q(i,
j)*Q(i,k);
199 std::cout <<
"sz = " << sz << std::endl;
200 std::cout <<
"nqp = " << nqp << std::endl;
201 std::cout <<
"sz2 = " << sz2 << std::endl;
209 std::cout <<
"||e1-Q2^T*u||_infty = " << err1.
normInf() << std::endl;
219 WQ(i,
j) = u[i]*Q(i,
j);
222 std::cout <<
"||vec(I-Q^T*diag(u)*Q)||_infty = " <<
vec_norm_inf(err2)
229 if (
std::abs(u[i]) > reduction_tol) ++rank;
240 if (
std::abs(u[i]) > reduction_tol) {
241 (*reduced_weights)[idx] = u[i];
242 (*reduced_points)[idx].
resize(d);
244 (*reduced_points)[idx][
j] = F(i,
j);
245 (*reduced_values)[idx].resize(sz);
247 (*reduced_values)[idx][
j] = Q(i,
j);
257 reduced_weights->resize(rank);
258 reduced_points->resize(rank);
259 reduced_values->resize(rank);
263 template <
typename ordinal_type,
typename value_type>
286 Q2(i,jdx) = Q(i,
j)*Q(i,k);
310 std::cout <<
"Q2 rank = " << r << std::endl;
328 std::cout <<
"sz = " << sz << std::endl;
329 std::cout <<
"nqp = " << nqp << std::endl;
330 std::cout <<
"sz2 = " << sz2 << std::endl;
338 std::cout <<
"||e1-Q2^T*u||_infty = " << err1.
normInf() << std::endl;
348 WQ(i,
j) = u[i]*Q(i,
j);
351 std::cout <<
"||vec(I-Q^T*diag(u)*Q)||_infty = " <<
vec_norm_inf(err2)
358 if (
std::abs(u[i]) > reduction_tol) ++rank;
369 if (
std::abs(u[i]) > reduction_tol) {
370 (*reduced_weights)[idx] = u[i];
371 (*reduced_points)[idx].
resize(d);
373 (*reduced_points)[idx][
j] = F(i,
j);
374 (*reduced_values)[idx].resize(sz);
376 (*reduced_values)[idx][
j] = Q(i,
j);
386 reduced_weights->resize(rank);
387 reduced_points->resize(rank);
388 reduced_values->resize(rank);
392 template <
typename ordinal_type,
typename value_type>
415 Q2(i,jdx) = Q(i,
j)*Q(i,k);
424 std::string orthogonalization_method =
425 params.get(
"Orthogonalization Method",
"Householder");
427 if (orthogonalization_method ==
"Householder")
431 orthogonalization_method, reduction_tol, verbose, Q2, ww,
433 bool restrict_r = params.get(
"Restrict Rank",
false);
440 std::cout <<
"Restricting rank from " << r <<
" to " << n << std::endl;
446 bool use_Z = params.get(
"Use Q in LP",
true);
456 QQ2(i,
j) =
Q2(i,piv[
j]);
460 std::cout <<
"Q2 rank = " << r << std::endl;
479 std::cout <<
"sz = " << sz << std::endl;
480 std::cout <<
"nqp = " << nqp << std::endl;
481 std::cout <<
"sz2 = " << sz2 << std::endl;
491 std::cout <<
"||e1-Q2^T*u||_infty = " << err1.
normInf() << std::endl;
501 WQ(i,
j) = u[i]*Q(i,
j);
504 std::cout <<
"||vec(I-Q^T*diag(u)*Q)||_infty = " <<
vec_norm_inf(err2)
511 if (
std::abs(u[i]) > reduction_tol) ++rank;
522 if (
std::abs(u[i]) > reduction_tol) {
523 (*reduced_weights)[idx] = u[i];
524 (*reduced_points)[idx].
resize(d);
526 (*reduced_points)[idx][
j] = F(i,
j);
527 (*reduced_values)[idx].resize(sz);
529 (*reduced_values)[idx][
j] = Q(i,
j);
539 reduced_weights->resize(rank);
540 reduced_points->resize(rank);
541 reduced_values->resize(rank);
545 template <
typename ordinal_type,
typename value_type>
577 std::cout <<
"sz = " << sz << std::endl;
578 std::cout <<
"nqp = " << nqp << std::endl;
579 std::cout <<
"sz2 = " << sz2 << std::endl;
587 std::cout <<
"||e1-Q2^T*u||_infty = " << err1.
normInf() << std::endl;
597 WQ(i,
j) = u[i]*Q(i,
j);
600 std::cout <<
"||vec(I-Q^T*diag(u)*Q)||_infty = " <<
vec_norm_inf(err2)
606 if (
std::abs(u[i]) > reduction_tol) ++rank;
617 if (
std::abs(u[i]) > reduction_tol) {
618 (*reduced_weights)[idx] = u[i];
619 (*reduced_points)[idx].
resize(d);
621 (*reduced_points)[idx][
j] = F(i,
j);
622 (*reduced_values)[idx].resize(sz);
624 (*reduced_values)[idx][
j] = Q(i,
j);
634 reduced_weights->resize(rank);
635 reduced_points->resize(rank);
636 reduced_values->resize(rank);
640 template <
typename ordinal_type,
typename value_type>
677 std::cout <<
"Q2 rank = " << r << std::endl;
695 std::cout <<
"sz = " << sz << std::endl;
696 std::cout <<
"nqp = " << nqp << std::endl;
697 std::cout <<
"sz2 = " << sz2 << std::endl;
705 std::cout <<
"||e1-Q2^T*u||_infty = " << err1.
normInf() << std::endl;
715 WQ(i,
j) = u[i]*Q(i,
j);
718 std::cout <<
"||vec(I-Q^T*diag(u)*Q)||_infty = " <<
vec_norm_inf(err2)
725 if (
std::abs(u[i]) > reduction_tol) ++rank;
736 if (
std::abs(u[i]) > reduction_tol) {
737 (*reduced_weights)[idx] = u[i];
738 (*reduced_points)[idx].
resize(d);
740 (*reduced_points)[idx][
j] = F(i,
j);
741 (*reduced_values)[idx].resize(sz);
743 (*reduced_values)[idx][
j] = Q(i,
j);
753 reduced_weights->resize(rank);
754 reduced_points->resize(rank);
755 reduced_values->resize(rank);
759 template <
typename ordinal_type,
typename value_type>
768 if (solver_method ==
"TRSM")
769 solver_TRSM(A, b, x, transa, uplo);
770 else if (solver_method ==
"Clp")
771 solver_CLP(A, b, x, transa, uplo);
772 else if (solver_method ==
"Clp-IP")
773 solver_CLP_IP(A, b, x, transa, uplo);
774 else if (solver_method ==
"GLPK")
775 solver_GLPK(A, b, x, transa, uplo);
776 else if (solver_method ==
"qpOASES")
777 solver_qpOASES(A, b, x, transa, uplo);
778 else if (solver_method ==
"Compressed Sensing")
779 solver_CompressedSensing(A, b, x, transa, uplo);
782 true, std::logic_error,
783 "Invalid solver method " << solver_method);
786 template <
typename ordinal_type,
typename value_type>
796 "TRSM solver requires triangular matrix!");
820 template <
typename ordinal_type,
typename value_type>
829 #ifdef HAVE_STOKHOS_GLPK
845 LPX *lp = lpx_create_prob();
846 lpx_set_prob_name(lp,
"Reduced Quadrature");
847 lpx_set_obj_dir(lp, LPX_MIN);
848 lpx_add_rows(lp, n_rows);
849 lpx_add_cols(lp, n_cols);
853 lpx_set_row_bnds(lp, i+1, LPX_FX, b[i], b[i]);
857 lpx_set_col_bnds(lp,
j+1, LPX_LO, 0.0, 0.0);
858 lpx_set_obj_coef(lp,
j+1, objective_value);
876 AA(i+1,
j+1) = A(
j,i);
885 row_indices[
j].
resize(n_rows+1);
887 row_indices[
j][i+1] = i+1;
888 lpx_set_mat_col(lp,
j+1, n_rows, &row_indices[
j][0], AA[j+1]);
900 row_indices[
j][i+1] = i+1;
901 lpx_set_mat_col(lp,
j+1, nr, &row_indices[
j][0], AA[j+1]);
912 row_indices[j][i+1] = j+i+1;
913 lpx_set_mat_col(lp, j+1, nr, &row_indices[j][0], AA[j+1]+j);
919 if (params.get(
"Write MPS File",
false) ==
true)
920 lpx_write_mps(lp, params.get(
"MPS Filename",
"lp.mps").c_str());
924 int status = lpx_get_status(lp);
926 std::cout <<
"glpk status = " << status << std::endl;
927 double z = lpx_get_obj_val(lp);
928 std::cout <<
"glpk objective = " << z << std::endl;
933 x[i] = lpx_get_col_prim(lp, i+1);
936 "GLPK solver called but not enabled!");
940 template <
typename ordinal_type,
typename value_type>
949 #ifdef HAVE_STOKHOS_CLP
967 model.resize(n_rows, 0);
971 model.setRowLower(i, b[i]);
972 model.setRowUpper(i, b[i]);
993 CoinBuild buildObject;
998 row_indices[
j][i] = i;
999 buildObject.addColumn(
1000 n_rows, &row_indices[
j][0], AA[j], 0.0, DBL_MAX, objective_value);
1012 row_indices[
j][i] = i;
1013 buildObject.addColumn(nr, &row_indices[
j][0], AA[j], 0.0, DBL_MAX, objective_value);
1024 row_indices[j][i] = j+i;
1025 buildObject.addColumn(
1026 nr, &row_indices[j][0], AA[j]+j, 0.0, DBL_MAX, objective_value);
1029 buildObject.addColumn(0, NULL, NULL, 0.0, DBL_MAX, objective_value);
1032 model.addColumns(buildObject);
1038 double *solution = model.primalColumnSolution();
1043 "CLP solver called but not enabled!");
1047 template <
typename ordinal_type,
typename value_type>
1056 #ifdef HAVE_STOKHOS_CLP
1074 model.resize(n_rows, 0);
1078 model.setRowLower(i, b[i]);
1079 model.setRowUpper(i, b[i]);
1100 CoinBuild buildObject;
1103 row_indices[
j].
resize(n_rows);
1105 row_indices[
j][i] = i;
1106 buildObject.addColumn(
1107 n_rows, &row_indices[
j][0], AA[j], 0.0, DBL_MAX, objective_value);
1119 row_indices[
j][i] = i;
1120 buildObject.addColumn(nr, &row_indices[
j][0], AA[j], 0.0, DBL_MAX, objective_value);
1131 row_indices[j][i] = j+i;
1132 buildObject.addColumn(
1133 nr, &row_indices[j][0], AA[j]+j, 0.0, DBL_MAX, objective_value);
1136 buildObject.addColumn(0, NULL, NULL, 0.0, DBL_MAX, objective_value);
1139 model.addColumns(buildObject);
1145 double *solution = model.primalColumnSolution();
1150 "CLP solver called but not enabled!");
1154 template <
typename ordinal_type,
typename value_type>
1163 #ifdef HAVE_STOKHOS_QPOASES
1188 qpOASES::QProblem lp(n_cols, n_rows, qpOASES::HST_ZERO);
1193 int nWSR = params.get(
"Max Working Set Recalculations", 10000);
1221 lp.getPrimalSolution(x.
values());
1224 "qpOASES solver called but not enabled!");
1228 template <
typename ordinal_type,
typename value_type>
1237 #ifdef HAVE_STOKHOS_DAKOTA
1252 Pecos::CompressedSensingTool CS;
1255 Pecos::RealMatrixArray xx;
1256 Pecos::CompressedSensingOptions opts;
1257 Pecos::CompressedSensingOptionsList opts_list;
1258 CS.solve(AA, bb, xx, opts, opts_list);
1263 true, std::logic_error,
1264 "CompressedSensing solver called but not enabled!");
1268 template <
typename ordinal_type,
typename value_type>
1282 for (rank=1; rank<m; rank++) {
1283 if (
std::abs(R(rank,rank)) > r_max)
1285 if (
std::abs(R(rank,rank)) < r_min)
1287 if (r_min / r_max < tol)
1294 std::cout <<
"r_max = " << r_max << std::endl;
1295 std::cout <<
"r_min = " << r_min << std::endl;
1296 std::cout <<
"Condition number of R = " << cond_r << std::endl;
1302 template <
typename ordinal_type,
typename value_type>
ScalarType * values() const
void underdetermined_solver(const Teuchos::SerialDenseMatrix< ordinal_type, value_type > &A, const Teuchos::SerialDenseVector< ordinal_type, value_type > &b, Teuchos::SerialDenseVector< ordinal_type, value_type > &x, Teuchos::ETransp transa, Teuchos::EUplo uplo) const
ordinal_type n_choose_k(const ordinal_type &n, const ordinal_type &k) const
Compute bionomial coefficient (n ; k) = n!/( k! (n-k)! )
virtual Teuchos::RCP< const Stokhos::UserDefinedQuadrature< ordinal_type, value_type > > createReducedQuadrature(const Teuchos::SerialDenseMatrix< ordinal_type, value_type > &Q, const Teuchos::SerialDenseMatrix< ordinal_type, value_type > &Q2, const Teuchos::SerialDenseMatrix< ordinal_type, value_type > &F, const Teuchos::Array< value_type > &weights) const
Get reduced quadrature object.
ordinal_type n_choose_k(const ordinal_type &n, const ordinal_type &k)
Compute bionomial coefficient (n ; k) = n!/( k! (n-k)! )
ordinal_type computeRank(const Teuchos::SerialDenseMatrix< ordinal_type, value_type > &R, const value_type tol) const
void solver_TRSM(const Teuchos::SerialDenseMatrix< ordinal_type, value_type > &A, const Teuchos::SerialDenseVector< ordinal_type, value_type > &b, Teuchos::SerialDenseVector< ordinal_type, value_type > &x, Teuchos::ETransp transa, Teuchos::EUplo uplo) const
int multiply(ETransp transa, ETransp transb, ScalarType alpha, const SerialDenseMatrix< OrdinalType, ScalarType > &A, const SerialDenseMatrix< OrdinalType, ScalarType > &B, ScalarType beta)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
int resize(OrdinalType length_in)
void CPQR_Householder3(const Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &A, Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &Q, Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &R, Teuchos::Array< ordinal_type > &piv)
Compute column-pivoted QR using Householder reflections.
KOKKOS_INLINE_FUNCTION PCE< Storage > min(const typename PCE< Storage >::value_type &a, const PCE< Storage > &b)
void solver_CLP_IP(const Teuchos::SerialDenseMatrix< ordinal_type, value_type > &A, const Teuchos::SerialDenseVector< ordinal_type, value_type > &b, Teuchos::SerialDenseVector< ordinal_type, value_type > &x, Teuchos::ETransp transa, Teuchos::EUplo uplo) const
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
int putScalar(const ScalarType value=Teuchos::ScalarTraits< ScalarType >::zero())
void reducedQuadrature_Q_Squared_CPQR(const Teuchos::SerialDenseMatrix< ordinal_type, value_type > &Q, const Teuchos::SerialDenseMatrix< ordinal_type, value_type > &F, const Teuchos::Array< value_type > &weights, Teuchos::RCP< Teuchos::Array< value_type > > &red_weights, Teuchos::RCP< Teuchos::Array< Teuchos::Array< value_type > > > &red_points, Teuchos::RCP< Teuchos::Array< Teuchos::Array< value_type > > > &red_values) const
ReducedQuadratureFactory(const Teuchos::ParameterList ¶ms)
Constructor.
void reducedQuadrature_Q2_CPQR(const Teuchos::SerialDenseMatrix< ordinal_type, value_type > &Q, const Teuchos::SerialDenseMatrix< ordinal_type, value_type > &Q2, const Teuchos::SerialDenseMatrix< ordinal_type, value_type > &F, const Teuchos::Array< value_type > &weights, Teuchos::RCP< Teuchos::Array< value_type > > &red_weights, Teuchos::RCP< Teuchos::Array< Teuchos::Array< value_type > > > &red_points, Teuchos::RCP< Teuchos::Array< Teuchos::Array< value_type > > > &red_values) const
void solver_qpOASES(const Teuchos::SerialDenseMatrix< ordinal_type, value_type > &A, const Teuchos::SerialDenseVector< ordinal_type, value_type > &b, Teuchos::SerialDenseVector< ordinal_type, value_type > &x, Teuchos::ETransp transa, Teuchos::EUplo uplo) const
OrdinalType length() const
void reducedQuadrature_Q2(const Teuchos::SerialDenseMatrix< ordinal_type, value_type > &Q, const Teuchos::SerialDenseMatrix< ordinal_type, value_type > &Q2, const Teuchos::SerialDenseMatrix< ordinal_type, value_type > &F, const Teuchos::Array< value_type > &weights, Teuchos::RCP< Teuchos::Array< value_type > > &red_weights, Teuchos::RCP< Teuchos::Array< Teuchos::Array< value_type > > > &red_points, Teuchos::RCP< Teuchos::Array< Teuchos::Array< value_type > > > &red_values) const
KOKKOS_INLINE_FUNCTION PCE< Storage > abs(const PCE< Storage > &a)
void solver_CompressedSensing(const Teuchos::SerialDenseMatrix< ordinal_type, value_type > &A, const Teuchos::SerialDenseVector< ordinal_type, value_type > &b, Teuchos::SerialDenseVector< ordinal_type, value_type > &x, Teuchos::ETransp transa, Teuchos::EUplo uplo) const
void resize(size_type new_size, const value_type &x=value_type())
scalar_type vec_norm_inf(const Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &A)
Vector-infinity norm of a matrix.
void solver_CLP(const Teuchos::SerialDenseMatrix< ordinal_type, value_type > &A, const Teuchos::SerialDenseVector< ordinal_type, value_type > &b, Teuchos::SerialDenseVector< ordinal_type, value_type > &x, Teuchos::ETransp transa, Teuchos::EUplo uplo) const
OrdinalType numCols() const
ScalarTraits< ScalarType >::magnitudeType normInf() const
void solver_GLPK(const Teuchos::SerialDenseMatrix< ordinal_type, value_type > &A, const Teuchos::SerialDenseVector< ordinal_type, value_type > &b, Teuchos::SerialDenseVector< ordinal_type, value_type > &x, Teuchos::ETransp transa, Teuchos::EUplo uplo) const
int reshape(OrdinalType numRows, OrdinalType numCols)
void reducedQuadrature_Q_Squared(const Teuchos::SerialDenseMatrix< ordinal_type, value_type > &Q, const Teuchos::SerialDenseMatrix< ordinal_type, value_type > &F, const Teuchos::Array< value_type > &weights, Teuchos::RCP< Teuchos::Array< value_type > > &red_weights, Teuchos::RCP< Teuchos::Array< Teuchos::Array< value_type > > > &red_points, Teuchos::RCP< Teuchos::Array< Teuchos::Array< value_type > > > &red_values) const
void reducedQuadrature_Q_Squared_CPQR2(const Teuchos::SerialDenseMatrix< ordinal_type, value_type > &Q, const Teuchos::SerialDenseMatrix< ordinal_type, value_type > &F, const Teuchos::Array< value_type > &weights, Teuchos::RCP< Teuchos::Array< value_type > > &red_weights, Teuchos::RCP< Teuchos::Array< Teuchos::Array< value_type > > > &red_points, Teuchos::RCP< Teuchos::Array< Teuchos::Array< value_type > > > &red_values) const
int shape(OrdinalType numRows, OrdinalType numCols)
#define TEUCHOS_ASSERT(assertion_test)
SerialDenseMatrix< OrdinalType, ScalarType > & assign(const SerialDenseMatrix< OrdinalType, ScalarType > &Source)
OrdinalType stride() const
OrdinalType numRows() const
Encapsulate various orthogonalization (ie QR) methods.