Skip to content

Commit

Permalink
Merge pull request #76 from cvanaret/refactoring
Browse files Browse the repository at this point in the history
Refactoring: split all the classes into hpp and cpp
  • Loading branch information
cvanaret authored Nov 6, 2024
2 parents a6aca0b + de602f0 commit a77a310
Show file tree
Hide file tree
Showing 51 changed files with 1,569 additions and 1,217 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ file(GLOB UNO_SOURCE_FILES
uno/optimization/*.cpp
uno/options/*.cpp
uno/preprocessing/*.cpp
uno/reformulation/*.cpp
uno/solvers/*.cpp
uno/tools/*.cpp
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "ConstraintRelaxationStrategy.hpp"
#include "ingredients/globalization_strategies/GlobalizationStrategy.hpp"
#include "ingredients/globalization_strategies/GlobalizationStrategyFactory.hpp"
#include "ingredients/subproblems/Direction.hpp"
#include "optimization/Direction.hpp"
#include "ingredients/subproblems/Subproblem.hpp"
#include "ingredients/subproblems/SubproblemFactory.hpp"
#include "linear_algebra/SymmetricMatrix.hpp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <functional>
#include "FeasibilityRestoration.hpp"
#include "ingredients/globalization_strategies/GlobalizationStrategy.hpp"
#include "ingredients/subproblems/Direction.hpp"
#include "optimization/Direction.hpp"
#include "ingredients/subproblems/Subproblem.hpp"
#include "linear_algebra/SymmetricIndefiniteLinearSystem.hpp"
#include "model/Model.hpp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <memory>
#include "ConstraintRelaxationStrategy.hpp"
#include "ingredients/globalization_strategies/ProgressMeasures.hpp"
#include "reformulation/OptimalityProblem.hpp"
#include "reformulation/l1RelaxedProblem.hpp"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <cassert>
#include "l1Relaxation.hpp"
#include "ingredients/globalization_strategies/GlobalizationStrategy.hpp"
#include "ingredients/subproblems/Direction.hpp"
#include "optimization/Direction.hpp"
#include "ingredients/subproblems/Subproblem.hpp"
#include "optimization/Iterate.hpp"
#include "optimization/WarmstartInformation.hpp"
Expand Down Expand Up @@ -166,7 +166,7 @@ namespace uno {
DEBUG << "Further aggressively decrease the penalty parameter to " << this->penalty_parameter << '\n';
}
else {
DEBUG << RED << "l1Relaxation: all multipliers are almost 0. The penalty parameter won't be decreased" << RESET << '\n';
DEBUG << "l1Relaxation: all multipliers are almost 0. The penalty parameter won't be decreased\n";
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

#include <memory>
#include "ConstraintRelaxationStrategy.hpp"
#include "ingredients/globalization_strategies/ProgressMeasures.hpp"
#include "optimization/Multipliers.hpp"
#include "reformulation/l1RelaxedProblem.hpp"

namespace uno {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#ifndef UNO_GLOBALIZATIONMECHANISM_H
#define UNO_GLOBALIZATIONMECHANISM_H

#include "ingredients/subproblems/Direction.hpp"
#include "optimization/Direction.hpp"

namespace uno {
// forward declarations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace uno {
double constrained_predicted_reduction = l1MeritFunction::constrained_merit_function(predicted_reduction, objective_multiplier);
DEBUG << "Constrained predicted reduction: " << constrained_predicted_reduction << '\n';
if (constrained_predicted_reduction <= 0.) {
WARNING << YELLOW << "The direction is not a descent direction for the merit function. You should decrease the penalty parameter.\n" << RESET;
WARNING << "The direction is not a descent direction for the merit function. You should decrease the penalty parameter.\n";
}
// compute current exact penalty
const double current_merit_value = l1MeritFunction::constrained_merit_function(current_progress, objective_multiplier);
Expand Down
27 changes: 27 additions & 0 deletions uno/ingredients/hessian_models/HessianModelFactory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) 2018-2024 Charlie Vanaret
// Licensed under the MIT license. See LICENSE file in the project directory for details.

#include "HessianModelFactory.hpp"
#include "HessianModel.hpp"
#include "ConvexifiedHessian.hpp"
#include "ExactHessian.hpp"
#include "ZeroHessian.hpp"
#include "solvers/DirectSymmetricIndefiniteLinearSolver.hpp"

namespace uno {
std::unique_ptr<HessianModel> HessianModelFactory::create(const std::string& hessian_model, size_t dimension, size_t maximum_number_nonzeros,
bool convexify, const Options& options) {
if (hessian_model == "exact") {
if (convexify) {
return std::make_unique<ConvexifiedHessian>(dimension, maximum_number_nonzeros + dimension, options);
}
else {
return std::make_unique<ExactHessian>(dimension, maximum_number_nonzeros, options);
}
}
else if (hessian_model == "zero") {
return std::make_unique<ZeroHessian>(dimension, options);
}
throw std::invalid_argument("Hessian model " + hessian_model + " does not exist");
}
} // namespace
25 changes: 4 additions & 21 deletions uno/ingredients/hessian_models/HessianModelFactory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,17 @@

#include <memory>
#include <string>
#include "HessianModel.hpp"
#include "ConvexifiedHessian.hpp"
#include "ExactHessian.hpp"
#include "ZeroHessian.hpp"
#include "solvers/DirectSymmetricIndefiniteLinearSolver.hpp"

namespace uno {
// forward declarations
class HessianModel;
class Options;

class HessianModelFactory {
public:
static std::unique_ptr<HessianModel> create(const std::string& hessian_model, size_t dimension, size_t maximum_number_nonzeros,
bool convexify, const Options& options);
};

inline std::unique_ptr<HessianModel> HessianModelFactory::create(const std::string& hessian_model, size_t dimension, size_t maximum_number_nonzeros,
bool convexify, const Options& options) {
if (hessian_model == "exact") {
if (convexify) {
return std::make_unique<ConvexifiedHessian>(dimension, maximum_number_nonzeros + dimension, options);
}
else {
return std::make_unique<ExactHessian>(dimension, maximum_number_nonzeros, options);
}
}
else if (hessian_model == "zero") {
return std::make_unique<ZeroHessian>(dimension, options);
}
throw std::invalid_argument("Hessian model " + hessian_model + " does not exist");
}
} // namespace

#endif // UNO_HESSIANMODELFACTORY_H,
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
// Licensed under the MIT license. See LICENSE file in the project directory for details.

#include "InequalityConstrainedMethod.hpp"
#include "ingredients/subproblems/Direction.hpp"
#include "optimization/Direction.hpp"
#include "optimization/Iterate.hpp"
#include "linear_algebra/Vector.hpp"
#include "reformulation/l1RelaxedProblem.hpp"
#include "options/Options.hpp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT license. See LICENSE file in the project directory for details.

#include "LPSubproblem.hpp"
#include "ingredients/subproblems/Direction.hpp"
#include "optimization/Direction.hpp"
#include "optimization/WarmstartInformation.hpp"
#include "reformulation/OptimizationProblem.hpp"
#include "solvers/LPSolver.hpp"
Expand All @@ -18,6 +18,8 @@ namespace uno {
zero_hessian(SymmetricMatrix<size_t, double>::zero(number_variables)) {
}

LPSubproblem::~LPSubproblem() { }

void LPSubproblem::generate_initial_iterate(const OptimizationProblem& /*problem*/, Iterate& /*initial_iterate*/) {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace uno {
public:
LPSubproblem(size_t number_variables, size_t number_constraints, size_t number_objective_gradient_nonzeros, size_t number_jacobian_nonzeros,
const Options& options);
~LPSubproblem();

void generate_initial_iterate(const OptimizationProblem& problem, Iterate& initial_iterate) override;
void solve(Statistics& statistics, const OptimizationProblem& problem, Iterate& current_iterate, const Multipliers& current_multipliers,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT license. See LICENSE file in the project directory for details.

#include "QPSubproblem.hpp"
#include "ingredients/subproblems/Direction.hpp"
#include "optimization/Direction.hpp"
#include "ingredients/hessian_models/HessianModelFactory.hpp"
#include "linear_algebra/SymmetricMatrix.hpp"
#include "optimization/Iterate.hpp"
Expand All @@ -29,6 +29,8 @@ namespace uno {
options)) {
}

QPSubproblem::~QPSubproblem() { }

void QPSubproblem::initialize_statistics(Statistics& statistics, const Options& options) {
if (this->use_regularization) {
statistics.add_column("regularization", Statistics::double_width, options.get_int("statistics_regularization_column_order"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace uno {
public:
QPSubproblem(size_t number_variables, size_t number_constraints, size_t number_objective_gradient_nonzeros, size_t number_jacobian_nonzeros,
size_t number_hessian_nonzeros, const Options& options);
~QPSubproblem();

void initialize_statistics(Statistics& statistics, const Options& options) override;
void generate_initial_iterate(const OptimizationProblem& problem, Iterate& initial_iterate) override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

#include <cmath>
#include "PrimalDualInteriorPointSubproblem.hpp"
#include "ingredients/subproblems/Direction.hpp"
#include "optimization/Direction.hpp"
#include "optimization/Iterate.hpp"
#include "ingredients/hessian_models/HessianModelFactory.hpp"
#include "linear_algebra/SparseStorageFactory.hpp"
#include "solvers/DirectSymmetricIndefiniteLinearSolver.hpp"
Expand Down Expand Up @@ -49,12 +50,12 @@ namespace uno {
l1_constraint_violation_coefficient(options.get_double("l1_constraint_violation_coefficient")) {
}

inline void PrimalDualInteriorPointSubproblem::initialize_statistics(Statistics& statistics, const Options& options) {
void PrimalDualInteriorPointSubproblem::initialize_statistics(Statistics& statistics, const Options& options) {
statistics.add_column("regularization", Statistics::double_width - 1, options.get_int("statistics_regularization_column_order"));
statistics.add_column("barrier param.", Statistics::double_width - 1, options.get_int("statistics_barrier_parameter_column_order"));
}

inline void PrimalDualInteriorPointSubproblem::generate_initial_iterate(const OptimizationProblem& problem, Iterate& initial_iterate) {
void PrimalDualInteriorPointSubproblem::generate_initial_iterate(const OptimizationProblem& problem, Iterate& initial_iterate) {
if (problem.has_inequality_constraints()) {
throw std::runtime_error("The problem has inequality constraints. Create an instance of HomogeneousEqualityConstrainedModel");
}
Expand Down Expand Up @@ -497,7 +498,7 @@ namespace uno {
}
}
else {
WARNING << YELLOW << "Barrier subproblem: the bounds are in the wrong order in the lower bound multiplier reset" << RESET << '\n';
WARNING << "Barrier subproblem: the bounds are in the wrong order in the lower bound multiplier reset\n";
}
}

Expand All @@ -515,7 +516,7 @@ namespace uno {
}
}
else {
WARNING << YELLOW << "Barrier subproblem: the bounds are in the wrong order in the upper bound multiplier reset" << RESET << '\n';
WARNING << "Barrier subproblem: the bounds are in the wrong order in the upper bound multiplier reset\n";
}
}
}
Expand Down
25 changes: 25 additions & 0 deletions uno/model/BoundRelaxedModel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) 2018-2024 Charlie Vanaret
// Licensed under the MIT license. See LICENSE file in the project directory for details.

#include "BoundRelaxedModel.hpp"
#include "optimization/Iterate.hpp"
#include "options/Options.hpp"

namespace uno {
BoundRelaxedModel::BoundRelaxedModel(std::unique_ptr<Model> original_model, const Options& options):
Model(original_model->name + " -> bounds relaxed", original_model->number_variables, original_model->number_constraints,
original_model->objective_sign),
model(std::move(original_model)),
relaxation_factor(options.get_double("tolerance")) {
}

double BoundRelaxedModel::variable_lower_bound(size_t variable_index) const {
const double lower_bound = this->model->variable_lower_bound(variable_index);
return lower_bound - this->relaxation_factor * std::max(1., std::abs(lower_bound));
}

double BoundRelaxedModel::variable_upper_bound(size_t variable_index) const {
const double upper_bound = this->model->variable_upper_bound(variable_index);
return upper_bound + this->relaxation_factor * std::max(1., std::abs(upper_bound));
}
} // namespace
23 changes: 4 additions & 19 deletions uno/model/BoundRelaxedModel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
#ifndef UNO_BOUNDRELAXEDMODEL_H
#define UNO_BOUNDRELAXEDMODEL_H

#include <memory>
#include "Model.hpp"
#include "optimization/Iterate.hpp"
#include "options/Options.hpp"

namespace uno {
// forward declaration
class Options;

class BoundRelaxedModel: public Model {
public:
BoundRelaxedModel(std::unique_ptr<Model> original_model, const Options& options);
Expand Down Expand Up @@ -64,23 +66,6 @@ namespace uno {
const std::unique_ptr<Model> model{};
const double relaxation_factor;
};

inline BoundRelaxedModel::BoundRelaxedModel(std::unique_ptr<Model> original_model, const Options& options):
Model(original_model->name + " -> bounds relaxed", original_model->number_variables, original_model->number_constraints,
original_model->objective_sign),
model(std::move(original_model)),
relaxation_factor(options.get_double("tolerance")) {
}

inline double BoundRelaxedModel::variable_lower_bound(size_t variable_index) const {
const double lower_bound = this->model->variable_lower_bound(variable_index);
return lower_bound - this->relaxation_factor * std::max(1., std::abs(lower_bound));
}

inline double BoundRelaxedModel::variable_upper_bound(size_t variable_index) const {
const double upper_bound = this->model->variable_upper_bound(variable_index);
return upper_bound + this->relaxation_factor * std::max(1., std::abs(upper_bound));
}
} // namespace

#endif // UNO_BOUNDRELAXEDMODEL_H
Loading

0 comments on commit a77a310

Please sign in to comment.