Skip to content

Commit

Permalink
test 2
Browse files Browse the repository at this point in the history
  • Loading branch information
nychiang committed Nov 22, 2024
1 parent 2f012fa commit 28893d6
Show file tree
Hide file tree
Showing 4 changed files with 328 additions and 344 deletions.
124 changes: 62 additions & 62 deletions src/Drivers/Sparse/NlpSparseEx1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ SparseEx1::SparseEx1(int n, double scal_input)
scal{scal_input}
{
assert(n >= 3);
if (n > 3) {
if(n > 3) {
n_cons += n - 3;
}
}
Expand All @@ -39,20 +39,20 @@ bool SparseEx1::get_prob_sizes(size_type& n, size_type& m)
bool SparseEx1::get_vars_info(const size_type& n, double* xlow, double* xupp, NonlinearityType* type)
{
assert(n == n_vars);
for (index_type i = 0; i < n; i++) {
if (i == 0) {
for(index_type i = 0; i < n; i++) {
if(i == 0) {
xlow[i] = -1e20;
xupp[i] = 1e20;
type[i] = hiopNonlinear;
continue;
}
if (i == 1) {
if(i == 1) {
xlow[i] = 0.0;
xupp[i] = 1e20;
type[i] = hiopNonlinear;
continue;
}
if (i == 2) {
if(i == 2) {
xlow[i] = 1.5;
xupp[i] = 10.0;
type[i] = hiopNonlinear;
Expand All @@ -70,15 +70,15 @@ bool SparseEx1::get_cons_info(const size_type& m, double* clow, double* cupp, No
{
assert(m == n_cons);
index_type conidx{0};
clow[conidx] = scal * 10.0;
cupp[conidx] = scal * 10.0;
clow[conidx] = scal * 10.0;
cupp[conidx] = scal * 10.0;
type[conidx++] = hiopInterfaceBase::hiopLinear;
clow[conidx] = scal * 5.0;
cupp[conidx] = 1e20;
clow[conidx] = scal * 5.0;
cupp[conidx] = 1e20;
type[conidx++] = hiopInterfaceBase::hiopLinear;
for (index_type i = 3; i < n_vars; i++) {
clow[conidx] = scal * 1.0;
cupp[conidx] = scal * 2 * n_vars;
for(index_type i = 3; i < n_vars; i++) {
clow[conidx] = scal * 1.0;
cupp[conidx] = scal * 2 * n_vars;
type[conidx++] = hiopInterfaceBase::hiopLinear;
}
return true;
Expand All @@ -91,8 +91,8 @@ bool SparseEx1::get_sparse_blocks_info(size_type& nx,
{
nx = n_vars;
;
nnz_sparse_Jaceq = 2;
nnz_sparse_Jacineq = 2 + 2 * (n_vars - 3);
nnz_sparse_Jaceq = 2;
nnz_sparse_Jacineq = 2 + 2 * (n_vars - 3);
nnz_sparse_Hess_Lagr = n_vars;
return true;
}
Expand All @@ -101,7 +101,7 @@ bool SparseEx1::eval_f(const size_type& n, const double* x, bool new_x, double&
{
assert(n == n_vars);
obj_value = 0.;
for (auto i = 0; i < n; i++) {
for(auto i = 0; i < n; i++) {
obj_value += scal * 0.25 * pow(x[i] - 1., 4);
}

Expand All @@ -111,19 +111,19 @@ bool SparseEx1::eval_f(const size_type& n, const double* x, bool new_x, double&
bool SparseEx1::eval_grad_f(const size_type& n, const double* x, bool new_x, double* gradf)
{
assert(n == n_vars);
for (auto i = 0; i < n; i++) {
for(auto i = 0; i < n; i++) {
gradf[i] = scal * pow(x[i] - 1., 3);
}
return true;
}

bool SparseEx1::eval_cons(const size_type& n,
const size_type& m,
const size_type& num_cons,
bool SparseEx1::eval_cons(const size_type& n,
const size_type& m,
const size_type& num_cons,
const index_type* idx_cons,
const double* x,
bool new_x,
double* cons)
const double* x,
bool new_x,
double* cons)
{
return false;
}
Expand All @@ -136,7 +136,7 @@ bool SparseEx1::eval_cons(const size_type& n, const size_type& m, const double*
assert(n_cons == 2 + n - 3);

// local contributions to the constraints in cons are reset
for (auto j = 0; j < m; j++) {
for(auto j = 0; j < m; j++) {
cons[j] = 0.;
}

Expand All @@ -149,65 +149,65 @@ bool SparseEx1::eval_cons(const size_type& n, const size_type& m, const double*
cons[conidx++] += scal * (2 * x[0] + 1 * x[2]);

// --- constraint 3 body ---> 2*x_1 + 0.5*x_i, for i>=4
for (auto i = 3; i < n; i++) {
for(auto i = 3; i < n; i++) {
cons[conidx++] += scal * (2 * x[0] + 0.5 * x[i]);
}

return true;
}

bool SparseEx1::eval_Jac_cons(const size_type& n,
const size_type& m,
const size_type& num_cons,
bool SparseEx1::eval_Jac_cons(const size_type& n,
const size_type& m,
const size_type& num_cons,
const index_type* idx_cons,
const double* x,
bool new_x,
const size_type& nnzJacS,
index_type* iJacS,
index_type* jJacS,
double* MJacS)
const double* x,
bool new_x,
const size_type& nnzJacS,
index_type* iJacS,
index_type* jJacS,
double* MJacS)
{
return false;
}

bool SparseEx1::eval_Jac_cons(const size_type& n,
const size_type& m,
const double* x,
bool new_x,
const double* x,
bool new_x,
const size_type& nnzJacS,
index_type* iJacS,
index_type* jJacS,
double* MJacS)
index_type* iJacS,
index_type* jJacS,
double* MJacS)
{
assert(n == n_vars);
assert(m == n_cons);
assert(n >= 3);

assert(nnzJacS == 4 + 2 * (n - 3));

int nnzit{0};
int nnzit{0};
index_type conidx{0};

if (iJacS != NULL && jJacS != NULL) {
if(iJacS != NULL && jJacS != NULL) {
// --- constraint 1 body ---> 4*x_1 + 2*x_2 == 10
iJacS[nnzit] = conidx;
iJacS[nnzit] = conidx;
jJacS[nnzit++] = 0;
iJacS[nnzit] = conidx;
iJacS[nnzit] = conidx;
jJacS[nnzit++] = 1;
conidx++;

// --- constraint 2 body ---> 2*x_1 + x_3
iJacS[nnzit] = conidx;
iJacS[nnzit] = conidx;
jJacS[nnzit++] = 0;
iJacS[nnzit] = conidx;
iJacS[nnzit] = conidx;
jJacS[nnzit++] = 2;
conidx++;

// --- constraint 3 body ---> 2*x_1 + 0.5*x_i, for i>=4
for (auto i = 3; i < n; i++) {
iJacS[nnzit] = conidx;
for(auto i = 3; i < n; i++) {
iJacS[nnzit] = conidx;
jJacS[nnzit++] = 0;
iJacS[nnzit] = conidx;
iJacS[nnzit] = conidx;
jJacS[nnzit++] = i;
conidx++;
}
Expand All @@ -216,7 +216,7 @@ bool SparseEx1::eval_Jac_cons(const size_type& n,

// values for sparse Jacobian if requested by the solver
nnzit = 0;
if (MJacS != NULL) {
if(MJacS != NULL) {
// --- constraint 1 body ---> 4*x_1 + 2*x_2 == 10
MJacS[nnzit++] = scal * 4;
MJacS[nnzit++] = scal * 2;
Expand All @@ -226,7 +226,7 @@ bool SparseEx1::eval_Jac_cons(const size_type& n,
MJacS[nnzit++] = scal * 1;

// --- constraint 3 body ---> 2*x_1 + 0.5*x_4
for (auto i = 3; i < n; i++) {
for(auto i = 3; i < n; i++) {
MJacS[nnzit++] = scal * 2;
MJacS[nnzit++] = scal * 0.5;
}
Expand All @@ -237,28 +237,28 @@ bool SparseEx1::eval_Jac_cons(const size_type& n,

bool SparseEx1::eval_Hess_Lagr(const size_type& n,
const size_type& m,
const double* x,
bool new_x,
const double& obj_factor,
const double* lambda,
bool new_lambda,
const double* x,
bool new_x,
const double& obj_factor,
const double* lambda,
bool new_lambda,
const size_type& nnzHSS,
index_type* iHSS,
index_type* jHSS,
double* MHSS)
index_type* iHSS,
index_type* jHSS,
double* MHSS)
{
// Note: lambda is not used since all the constraints are linear and, therefore, do
// not contribute to the Hessian of the Lagrangian
assert(nnzHSS == n);

if (iHSS != NULL && jHSS != NULL) {
for (int i = 0; i < n; i++) {
if(iHSS != NULL && jHSS != NULL) {
for(int i = 0; i < n; i++) {
iHSS[i] = jHSS[i] = i;
}
}

if (MHSS != NULL) {
for (int i = 0; i < n; i++) {
if(MHSS != NULL) {
for(int i = 0; i < n; i++) {
MHSS[i] = scal * obj_factor * 3 * pow(x[i] - 1., 2);
}
}
Expand All @@ -268,7 +268,7 @@ bool SparseEx1::eval_Hess_Lagr(const size_type& n,
bool SparseEx1::get_starting_point(const size_type& n, double* x0)
{
assert(n == n_vars);
for (auto i = 0; i < n; i++) {
for(auto i = 0; i < n; i++) {
x0[i] = 0.0;
}
return true;
Expand Down
60 changes: 30 additions & 30 deletions src/Drivers/Sparse/NlpSparseEx1.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#include <cassert>

using size_type = hiop::size_type;
using size_type = hiop::size_type;
using index_type = hiop::index_type;

/* Test with bounds and constraints of all types. For some reason this
Expand Down Expand Up @@ -36,45 +36,45 @@ class SparseEx1 : public hiop::hiopInterfaceSparse
size_type& nnz_sparse_Hess_Lagr);

virtual bool eval_f(const size_type& n, const double* x, bool new_x, double& obj_value);
virtual bool eval_cons(const size_type& n,
const size_type& m,
const size_type& num_cons,
virtual bool eval_cons(const size_type& n,
const size_type& m,
const size_type& num_cons,
const index_type* idx_cons,
const double* x,
bool new_x,
double* cons);
const double* x,
bool new_x,
double* cons);
virtual bool eval_cons(const size_type& n, const size_type& m, const double* x, bool new_x, double* cons);
virtual bool eval_grad_f(const size_type& n, const double* x, bool new_x, double* gradf);
virtual bool eval_Jac_cons(const size_type& n,
const size_type& m,
const size_type& num_cons,
virtual bool eval_Jac_cons(const size_type& n,
const size_type& m,
const size_type& num_cons,
const index_type* idx_cons,
const double* x,
bool new_x,
const size_type& nnzJacS,
index_type* iJacS,
index_type* jJacS,
double* MJacS);
const double* x,
bool new_x,
const size_type& nnzJacS,
index_type* iJacS,
index_type* jJacS,
double* MJacS);
virtual bool eval_Jac_cons(const size_type& n,
const size_type& m,
const double* x,
bool new_x,
const double* x,
bool new_x,
const size_type& nnzJacS,
index_type* iJacS,
index_type* jJacS,
double* MJacS);
index_type* iJacS,
index_type* jJacS,
double* MJacS);
virtual bool get_starting_point(const size_type& n, double* x0);
virtual bool eval_Hess_Lagr(const size_type& n,
const size_type& m,
const double* x,
bool new_x,
const double& obj_factor,
const double* lambda,
bool new_lambda,
const double* x,
bool new_x,
const double& obj_factor,
const double* lambda,
bool new_lambda,
const size_type& nnzHSS,
index_type* iHSS,
index_type* jHSS,
double* MHSS);
index_type* iHSS,
index_type* jHSS,
double* MHSS);

// not implemented
virtual bool
Expand All @@ -91,6 +91,6 @@ class SparseEx1 : public hiop::hiopInterfaceSparse

private:
size_type n_vars, n_cons;
double scal;
double scal;
};
#endif
Loading

0 comments on commit 28893d6

Please sign in to comment.