Skip to content

Commit

Permalink
finish todos in box constraint solver
Browse files Browse the repository at this point in the history
  • Loading branch information
Huangzizhou committed Nov 23, 2023
1 parent f49f693 commit 95f949d
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 37 deletions.
47 changes: 46 additions & 1 deletion src/polysolve/Utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,49 @@ namespace polysolve

Eigen::SparseMatrix<double> sparse_identity(int rows, int cols);

} // namespace polysolve
} // namespace polysolve

namespace nlohmann
{
template <typename T, int nrows, int ncols, int maxdim1, int maxdim2>
struct adl_serializer<Eigen::Matrix<T, nrows, ncols, Eigen::ColMajor, maxdim1, maxdim2>>
{
static void to_json(json &j, const Eigen::Matrix<T, nrows, ncols, Eigen::ColMajor, maxdim1, maxdim2> &matrix)
{
for (int row = 0; row < matrix.rows(); ++row)
{
json column = json::array();
for (int col = 0; col < matrix.cols(); ++col)
{
column.push_back(matrix(row, col));
}
j.push_back(column);
}
}

static void from_json(const json &j, Eigen::Matrix<T, nrows, ncols, Eigen::ColMajor, maxdim1, maxdim2> &matrix)
{
using Scalar = typename Eigen::Matrix<T, nrows, ncols, Eigen::ColMajor, maxdim1, maxdim2>::Scalar;
assert(j.size() > 0);
assert(nrows >= j.size() || nrows == -1);
assert(j.at(0).is_number() || ncols == -1 || ncols >= j.at(0).size());

const int n_cols = j.at(0).is_number() ? 1 : j.at(0).size();
if (nrows == -1 || ncols == -1)
matrix.setZero(j.size(), n_cols);

for (std::size_t row = 0; row < j.size(); ++row)
{
const auto& jrow = j.at(row);
if (jrow.is_number())
matrix(row) = jrow;
else
for (std::size_t col = 0; col < jrow.size(); ++col)

Check warning on line 100 in src/polysolve/Utils.hpp

View check run for this annotation

Codecov / codecov/patch

src/polysolve/Utils.hpp#L100

Added line #L100 was not covered by tests
{
const auto& value = jrow.at(col);
matrix(row, col) = value.get<Scalar>();

Check warning on line 103 in src/polysolve/Utils.hpp

View check run for this annotation

Codecov / codecov/patch

src/polysolve/Utils.hpp#L102-L103

Added lines #L102 - L103 were not covered by tests
}
}
}
};
} // namespace nlohmann
44 changes: 8 additions & 36 deletions src/polysolve/nonlinear/BoxConstraintSolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,40 +78,12 @@ namespace polysolve::nonlinear
json box_constraint_params = solver_params["box_constraints"];
if (box_constraint_params["max_change"] > 0)
max_change_val_ = box_constraint_params["max_change"];
// todo
// else
// nlohmann::adl_serializer<Eigen::VectorXd>::from_json(box_constraint_params["max_change"], max_change_);
else
max_change_ = box_constraint_params["max_change"];

Check warning on line 82 in src/polysolve/nonlinear/BoxConstraintSolver.cpp

View check run for this annotation

Codecov / codecov/patch

src/polysolve/nonlinear/BoxConstraintSolver.cpp#L82

Added line #L82 was not covered by tests

if (box_constraint_params.contains("bounds"))
if (box_constraint_params.contains("bounds") && box_constraint_params["bounds"].is_array() && box_constraint_params["bounds"].size() == 2)
{
if (box_constraint_params["bounds"].is_string())
{
if (std::filesystem::is_regular_file(box_constraint_params["bounds"].get<std::string>()))
{
// todo
// polyfem::io::read_matrix(box_constraint_params["bounds"].get<std::string>(), bounds_);
assert(bounds_.cols() == 2);
}
}
else if (box_constraint_params["bounds"].is_array() && box_constraint_params["bounds"].size() == 2)
{
if (box_constraint_params["bounds"][0].is_number())
{
bounds_.setZero(1, 2);
bounds_ << box_constraint_params["bounds"][0], box_constraint_params["bounds"][1];
}
else if (box_constraint_params["bounds"][0].is_array())
{
bounds_.setZero(box_constraint_params["bounds"][0].size(), 2);
Eigen::VectorXd tmp;
// todo
// nlohmann::adl_serializer<Eigen::VectorXd>::from_json(box_constraint_params["bounds"][0], tmp);
bounds_.col(0) = tmp;
// todo
// nlohmann::adl_serializer<Eigen::VectorXd>::from_json(box_constraint_params["bounds"][1], tmp);
bounds_.col(1) = tmp;
}
}
bounds_ = box_constraint_params["bounds"];
}
}

Expand Down Expand Up @@ -149,8 +121,8 @@ namespace polysolve::nonlinear
bool consider_max_change) const
{
Eigen::VectorXd min;
if (bounds_.rows() == x.size())
min = bounds_.col(0);
if (bounds_.cols() == x.size())
min = bounds_.row(0);

Check warning on line 125 in src/polysolve/nonlinear/BoxConstraintSolver.cpp

View check run for this annotation

Codecov / codecov/patch

src/polysolve/nonlinear/BoxConstraintSolver.cpp#L125

Added line #L125 was not covered by tests
else if (bounds_.size() == 2)
min = Eigen::VectorXd::Constant(x.size(), 1, bounds_(0));
else
Expand All @@ -166,8 +138,8 @@ namespace polysolve::nonlinear
bool consider_max_change) const
{
Eigen::VectorXd max;
if (bounds_.rows() == x.size())
max = bounds_.col(1);
if (bounds_.cols() == x.size())
max = bounds_.row(1);

Check warning on line 142 in src/polysolve/nonlinear/BoxConstraintSolver.cpp

View check run for this annotation

Codecov / codecov/patch

src/polysolve/nonlinear/BoxConstraintSolver.cpp#L142

Added line #L142 was not covered by tests
else if (bounds_.size() == 2)
max = Eigen::VectorXd::Constant(x.size(), 1, bounds_(1));
else
Expand Down

0 comments on commit 95f949d

Please sign in to comment.