17 #ifdef HAVE_STOKHOS_GLPK
23 #ifdef HAVE_STOKHOS_CLP
24 #include "coin/ClpSimplex.hpp"
25 #include "coin/CoinBuild.hpp"
26 #include "coin/ClpInterior.hpp"
29 #ifdef HAVE_STOKHOS_QPOASES
30 #include "qpOASES.hpp"
33 #ifdef HAVE_STOKHOS_DAKOTA
34 #include "LinearSolver.hpp"
37 template <
typename ordinal_type,
typename value_type>
42 reduction_method(params.get(
"Reduced Quadrature Method",
"Q Squared")),
43 solver_method(params.get(
"Solver Method",
"TRSM")),
44 eliminate_dependent_rows(params.get(
"Eliminate Dependent Rows",
true)),
45 verbose(params.get(
"Verbose", false)),
46 reduction_tol(params.get(
"Reduction Tolerance", 1.0e-12)),
47 objective_value(params.get(
"Objective Value", 0.0))
51 template <
typename ordinal_type,
typename value_type>
65 if (reduction_method ==
"Q Squared") {
66 if (eliminate_dependent_rows)
67 reducedQuadrature_Q_Squared_CPQR(Q, F, weights,
68 red_weights, red_points, red_values);
70 reducedQuadrature_Q_Squared(Q, F, weights,
71 red_weights, red_points, red_values);
73 else if (reduction_method ==
"Q Squared2") {
74 reducedQuadrature_Q_Squared_CPQR2(Q, F, weights,
75 red_weights, red_points, red_values);
77 else if (reduction_method ==
"Q2") {
78 if (eliminate_dependent_rows)
79 reducedQuadrature_Q2_CPQR(Q, Q2, F, weights,
80 red_weights, red_points, red_values);
82 reducedQuadrature_Q2(Q, Q2, F, weights,
83 red_weights, red_points, red_values);
85 else if (reduction_method ==
"None") {
96 (*red_points)[i].resize(d);
98 (*red_points)[i][
j] = F(i,
j);
99 (*red_values)[i].resize(sz);
101 (*red_values)[i][
j] = Q(i,
j);
106 true, std::logic_error,
107 "Invalid dimension reduction method " << reduction_method);
125 template <
typename ordinal_type,
typename value_type>
148 Q2(i,jdx) = Q(i,
j)*Q(i,k);
167 std::cout <<
"sz = " << sz << std::endl;
168 std::cout <<
"nqp = " << nqp << std::endl;
169 std::cout <<
"sz2 = " << sz2 << std::endl;
177 std::cout <<
"||e1-Q2^T*u||_infty = " << err1.
normInf() << std::endl;
187 WQ(i,
j) = u[i]*Q(i,
j);
190 std::cout <<
"||vec(I-Q^T*diag(u)*Q)||_infty = " <<
vec_norm_inf(err2)
197 if (
std::abs(u[i]) > reduction_tol) ++rank;
208 if (
std::abs(u[i]) > reduction_tol) {
209 (*reduced_weights)[idx] = u[i];
210 (*reduced_points)[idx].
resize(d);
212 (*reduced_points)[idx][
j] = F(i,
j);
213 (*reduced_values)[idx].resize(sz);
215 (*reduced_values)[idx][
j] = Q(i,
j);
225 reduced_weights->resize(rank);
226 reduced_points->resize(rank);
227 reduced_values->resize(rank);
231 template <
typename ordinal_type,
typename value_type>
254 Q2(i,jdx) = Q(i,
j)*Q(i,k);
278 std::cout <<
"Q2 rank = " << r << std::endl;
296 std::cout <<
"sz = " << sz << std::endl;
297 std::cout <<
"nqp = " << nqp << std::endl;
298 std::cout <<
"sz2 = " << sz2 << std::endl;
306 std::cout <<
"||e1-Q2^T*u||_infty = " << err1.
normInf() << std::endl;
316 WQ(i,
j) = u[i]*Q(i,
j);
319 std::cout <<
"||vec(I-Q^T*diag(u)*Q)||_infty = " <<
vec_norm_inf(err2)
326 if (
std::abs(u[i]) > reduction_tol) ++rank;
337 if (
std::abs(u[i]) > reduction_tol) {
338 (*reduced_weights)[idx] = u[i];
339 (*reduced_points)[idx].
resize(d);
341 (*reduced_points)[idx][
j] = F(i,
j);
342 (*reduced_values)[idx].resize(sz);
344 (*reduced_values)[idx][
j] = Q(i,
j);
354 reduced_weights->resize(rank);
355 reduced_points->resize(rank);
356 reduced_values->resize(rank);
360 template <
typename ordinal_type,
typename value_type>
383 Q2(i,jdx) = Q(i,
j)*Q(i,k);
392 std::string orthogonalization_method =
393 params.get(
"Orthogonalization Method",
"Householder");
395 if (orthogonalization_method ==
"Householder")
399 orthogonalization_method, reduction_tol, verbose, Q2, ww,
401 bool restrict_r = params.get(
"Restrict Rank",
false);
408 std::cout <<
"Restricting rank from " << r <<
" to " << n << std::endl;
414 bool use_Z = params.get(
"Use Q in LP",
true);
424 QQ2(i,
j) =
Q2(i,piv[
j]);
428 std::cout <<
"Q2 rank = " << r << std::endl;
447 std::cout <<
"sz = " << sz << std::endl;
448 std::cout <<
"nqp = " << nqp << std::endl;
449 std::cout <<
"sz2 = " << sz2 << std::endl;
459 std::cout <<
"||e1-Q2^T*u||_infty = " << err1.
normInf() << std::endl;
469 WQ(i,
j) = u[i]*Q(i,
j);
472 std::cout <<
"||vec(I-Q^T*diag(u)*Q)||_infty = " <<
vec_norm_inf(err2)
479 if (
std::abs(u[i]) > reduction_tol) ++rank;
490 if (
std::abs(u[i]) > reduction_tol) {
491 (*reduced_weights)[idx] = u[i];
492 (*reduced_points)[idx].
resize(d);
494 (*reduced_points)[idx][
j] = F(i,
j);
495 (*reduced_values)[idx].resize(sz);
497 (*reduced_values)[idx][
j] = Q(i,
j);
507 reduced_weights->resize(rank);
508 reduced_points->resize(rank);
509 reduced_values->resize(rank);
513 template <
typename ordinal_type,
typename value_type>
545 std::cout <<
"sz = " << sz << std::endl;
546 std::cout <<
"nqp = " << nqp << std::endl;
547 std::cout <<
"sz2 = " << sz2 << std::endl;
555 std::cout <<
"||e1-Q2^T*u||_infty = " << err1.
normInf() << std::endl;
565 WQ(i,
j) = u[i]*Q(i,
j);
568 std::cout <<
"||vec(I-Q^T*diag(u)*Q)||_infty = " <<
vec_norm_inf(err2)
574 if (
std::abs(u[i]) > reduction_tol) ++rank;
585 if (
std::abs(u[i]) > reduction_tol) {
586 (*reduced_weights)[idx] = u[i];
587 (*reduced_points)[idx].
resize(d);
589 (*reduced_points)[idx][
j] = F(i,
j);
590 (*reduced_values)[idx].resize(sz);
592 (*reduced_values)[idx][
j] = Q(i,
j);
602 reduced_weights->resize(rank);
603 reduced_points->resize(rank);
604 reduced_values->resize(rank);
608 template <
typename ordinal_type,
typename value_type>
645 std::cout <<
"Q2 rank = " << r << std::endl;
663 std::cout <<
"sz = " << sz << std::endl;
664 std::cout <<
"nqp = " << nqp << std::endl;
665 std::cout <<
"sz2 = " << sz2 << std::endl;
673 std::cout <<
"||e1-Q2^T*u||_infty = " << err1.
normInf() << std::endl;
683 WQ(i,
j) = u[i]*Q(i,
j);
686 std::cout <<
"||vec(I-Q^T*diag(u)*Q)||_infty = " <<
vec_norm_inf(err2)
693 if (
std::abs(u[i]) > reduction_tol) ++rank;
704 if (
std::abs(u[i]) > reduction_tol) {
705 (*reduced_weights)[idx] = u[i];
706 (*reduced_points)[idx].
resize(d);
708 (*reduced_points)[idx][
j] = F(i,
j);
709 (*reduced_values)[idx].resize(sz);
711 (*reduced_values)[idx][
j] = Q(i,
j);
721 reduced_weights->resize(rank);
722 reduced_points->resize(rank);
723 reduced_values->resize(rank);
727 template <
typename ordinal_type,
typename value_type>
736 if (solver_method ==
"TRSM")
737 solver_TRSM(A, b, x, transa, uplo);
738 else if (solver_method ==
"Clp")
739 solver_CLP(A, b, x, transa, uplo);
740 else if (solver_method ==
"Clp-IP")
741 solver_CLP_IP(A, b, x, transa, uplo);
742 else if (solver_method ==
"GLPK")
743 solver_GLPK(A, b, x, transa, uplo);
744 else if (solver_method ==
"qpOASES")
745 solver_qpOASES(A, b, x, transa, uplo);
746 else if (solver_method ==
"Compressed Sensing")
747 solver_CompressedSensing(A, b, x, transa, uplo);
750 true, std::logic_error,
751 "Invalid solver method " << solver_method);
754 template <
typename ordinal_type,
typename value_type>
764 "TRSM solver requires triangular matrix!");
788 template <
typename ordinal_type,
typename value_type>
797 #ifdef HAVE_STOKHOS_GLPK
813 LPX *lp = lpx_create_prob();
814 lpx_set_prob_name(lp,
"Reduced Quadrature");
815 lpx_set_obj_dir(lp, LPX_MIN);
816 lpx_add_rows(lp, n_rows);
817 lpx_add_cols(lp, n_cols);
821 lpx_set_row_bnds(lp, i+1, LPX_FX, b[i], b[i]);
825 lpx_set_col_bnds(lp,
j+1, LPX_LO, 0.0, 0.0);
826 lpx_set_obj_coef(lp,
j+1, objective_value);
844 AA(i+1,
j+1) = A(
j,i);
853 row_indices[
j].
resize(n_rows+1);
855 row_indices[
j][i+1] = i+1;
856 lpx_set_mat_col(lp,
j+1, n_rows, &row_indices[
j][0], AA[j+1]);
868 row_indices[
j][i+1] = i+1;
869 lpx_set_mat_col(lp,
j+1, nr, &row_indices[
j][0], AA[j+1]);
880 row_indices[j][i+1] = j+i+1;
881 lpx_set_mat_col(lp, j+1, nr, &row_indices[j][0], AA[j+1]+j);
887 if (params.get(
"Write MPS File",
false) ==
true)
888 lpx_write_mps(lp, params.get(
"MPS Filename",
"lp.mps").c_str());
892 int status = lpx_get_status(lp);
894 std::cout <<
"glpk status = " << status << std::endl;
895 double z = lpx_get_obj_val(lp);
896 std::cout <<
"glpk objective = " << z << std::endl;
901 x[i] = lpx_get_col_prim(lp, i+1);
904 "GLPK solver called but not enabled!");
908 template <
typename ordinal_type,
typename value_type>
917 #ifdef HAVE_STOKHOS_CLP
935 model.resize(n_rows, 0);
939 model.setRowLower(i, b[i]);
940 model.setRowUpper(i, b[i]);
961 CoinBuild buildObject;
966 row_indices[
j][i] = i;
967 buildObject.addColumn(
968 n_rows, &row_indices[
j][0], AA[j], 0.0, DBL_MAX, objective_value);
980 row_indices[
j][i] = i;
981 buildObject.addColumn(nr, &row_indices[
j][0], AA[j], 0.0, DBL_MAX, objective_value);
992 row_indices[j][i] = j+i;
993 buildObject.addColumn(
994 nr, &row_indices[j][0], AA[j]+j, 0.0, DBL_MAX, objective_value);
997 buildObject.addColumn(0, NULL, NULL, 0.0, DBL_MAX, objective_value);
1000 model.addColumns(buildObject);
1006 double *solution = model.primalColumnSolution();
1011 "CLP solver called but not enabled!");
1015 template <
typename ordinal_type,
typename value_type>
1024 #ifdef HAVE_STOKHOS_CLP
1042 model.resize(n_rows, 0);
1046 model.setRowLower(i, b[i]);
1047 model.setRowUpper(i, b[i]);
1068 CoinBuild buildObject;
1071 row_indices[
j].
resize(n_rows);
1073 row_indices[
j][i] = i;
1074 buildObject.addColumn(
1075 n_rows, &row_indices[
j][0], AA[j], 0.0, DBL_MAX, objective_value);
1087 row_indices[
j][i] = i;
1088 buildObject.addColumn(nr, &row_indices[
j][0], AA[j], 0.0, DBL_MAX, objective_value);
1099 row_indices[j][i] = j+i;
1100 buildObject.addColumn(
1101 nr, &row_indices[j][0], AA[j]+j, 0.0, DBL_MAX, objective_value);
1104 buildObject.addColumn(0, NULL, NULL, 0.0, DBL_MAX, objective_value);
1107 model.addColumns(buildObject);
1113 double *solution = model.primalColumnSolution();
1118 "CLP solver called but not enabled!");
1122 template <
typename ordinal_type,
typename value_type>
1131 #ifdef HAVE_STOKHOS_QPOASES
1156 qpOASES::QProblem lp(n_cols, n_rows, qpOASES::HST_ZERO);
1161 int nWSR = params.get(
"Max Working Set Recalculations", 10000);
1189 lp.getPrimalSolution(x.
values());
1192 "qpOASES solver called but not enabled!");
1196 template <
typename ordinal_type,
typename value_type>
1205 #ifdef HAVE_STOKHOS_DAKOTA
1220 Pecos::CompressedSensingTool CS;
1223 Pecos::RealMatrixArray xx;
1224 Pecos::CompressedSensingOptions opts;
1225 Pecos::CompressedSensingOptionsList opts_list;
1226 CS.solve(AA, bb, xx, opts, opts_list);
1231 true, std::logic_error,
1232 "CompressedSensing solver called but not enabled!");
1236 template <
typename ordinal_type,
typename value_type>
1250 for (rank=1; rank<m; rank++) {
1251 if (
std::abs(R(rank,rank)) > r_max)
1253 if (
std::abs(R(rank,rank)) < r_min)
1255 if (r_min / r_max < tol)
1262 std::cout <<
"r_max = " << r_max << std::endl;
1263 std::cout <<
"r_min = " << r_min << std::endl;
1264 std::cout <<
"Condition number of R = " << cond_r << std::endl;
1270 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.