Skip to content

Commit

Permalink
Moved default options to own class + improved the message errors in s…
Browse files Browse the repository at this point in the history
…olver factories
  • Loading branch information
cvanaret committed Oct 23, 2024
1 parent e97d2a6 commit 0b68f17
Show file tree
Hide file tree
Showing 50 changed files with 612 additions and 557 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ file(GLOB UNO_SOURCE_FILES
uno/ingredients/subproblems/interior_point_methods/*.cpp
uno/model/*.cpp
uno/optimization/*.cpp
uno/options/*.cpp
uno/preprocessing/*.cpp
uno/tools/*.cpp
)
Expand Down
4 changes: 2 additions & 2 deletions bindings/AMPL/AMPLModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "optimization/Iterate.hpp"
#include "tools/Logger.hpp"
#include "tools/Infinity.hpp"
#include "tools/Options.hpp"
#include "options/Options.hpp"
#include "symbolic/Concatenation.hpp"

namespace uno {
Expand All @@ -22,7 +22,7 @@ namespace uno {

int n_discrete = asl->i.nbv_ + asl->i.niv_ + asl->i.nlvbi_ + asl->i.nlvci_ + asl->i.nlvoi_;
if (0 < n_discrete) {
throw std::runtime_error("Error: " + std::to_string(n_discrete) + " variables are discrete, which Uno cannot handle.");
throw std::runtime_error("Error: " + std::to_string(n_discrete) + " variables are discrete, which Uno cannot handle");
// asl->i.need_nl_ = 0;
}

Expand Down
9 changes: 5 additions & 4 deletions bindings/AMPL/uno_ampl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
#include "AMPLModel.hpp"
#include "Uno.hpp"
#include "model/ModelFactory.hpp"
#include "options/Options.hpp"
#include "options/DefaultOptions.hpp"
#include "tools/Logger.hpp"
#include "tools/Options.hpp"

/*
size_t memory_allocation_amount = 0;
Expand Down Expand Up @@ -83,19 +84,19 @@ int main(int argc, char* argv[]) {
Uno::print_available_strategies();
}
else {
throw std::runtime_error("The second command line argument should be -AMPL.");
throw std::runtime_error("The second command line argument should be -AMPL");
}
}
else if (argc >= 3) {
Options options = Options::get_default();
Options options = DefaultOptions::load();

// AMPL expects: ./uno_ampl model.nl -AMPL [option_name=option_value, ...]
// model name
std::string model_name = std::string(argv[1]);

// -AMPL
if (std::string(argv[2]) != "-AMPL") {
throw std::runtime_error("The second command line argument should be -AMPL.");
throw std::runtime_error("The second command line argument should be -AMPL");
}

// overwrite the default options with the command line arguments
Expand Down
20 changes: 5 additions & 15 deletions uno/Uno.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
#include "ingredients/globalization_mechanisms/GlobalizationMechanismFactory.hpp"
#include "ingredients/globalization_strategies/GlobalizationStrategyFactory.hpp"
#include "ingredients/subproblems/SubproblemFactory.hpp"
#include "linear_algebra/Vector.hpp"
#include "model/Model.hpp"
#include "optimization/Iterate.hpp"
#include "solvers/QPSolverFactory.hpp"
#include "solvers/LPSolverFactory.hpp"
#include "solvers/SymmetricIndefiniteLinearSolverFactory.hpp"
#include "tools/Logger.hpp"
#include "tools/Options.hpp"
#include "options/Options.hpp"
#include "tools/Statistics.hpp"
#include "tools/Timer.hpp"

Expand All @@ -24,7 +25,7 @@ namespace uno {
max_iterations(options.get_unsigned_int("max_iterations")),
time_limit(options.get_double("time_limit")),
print_solution(options.get_bool("print_solution")),
strategy_combination(this->get_strategy_combination(options)) { }
strategy_combination(Uno::get_strategy_combination(options)) { }

Level Logger::level = INFO;

Expand Down Expand Up @@ -61,7 +62,7 @@ namespace uno {
statistics.start_new_line();
statistics.set("status", exception.what());
if (Logger::level == INFO) statistics.print_current_line();
DEBUG << exception.what();
DEBUG << exception.what() << '\n';
}
if (Logger::level == INFO) statistics.print_footer();

Expand All @@ -79,7 +80,7 @@ namespace uno {
if (Logger::level == INFO) statistics.print_current_line();
}
catch (const std::exception& e) {
DISCRETE << RED << "An error occurred at the initial iterate: " << e.what() << RESET;
DISCRETE << RED << "An error occurred at the initial iterate: " << e.what() << RESET << '\n';
throw;
}
}
Expand Down Expand Up @@ -117,17 +118,6 @@ namespace uno {
Iterate::number_eval_jacobian, number_hessian_evaluations, number_subproblems_solved};
}

std::string join(const std::vector<std::string>& vector, const std::string& separator) {
std::string result{};
if (not vector.empty()) {
result = vector[0];
for (size_t variable_index: Range(1, vector.size())) {
result += separator + vector[variable_index];
}
}
return result;
}

void Uno::print_available_strategies() {
std::cout << "Available strategies:\n";
std::cout << "- Constraint relaxation strategies: " << join(ConstraintRelaxationStrategyFactory::available_strategies(), ", ") << '\n';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include "reformulation/OptimizationProblem.hpp"
#include "symbolic/VectorView.hpp"
#include "symbolic/Expression.hpp"
#include "tools/Options.hpp"
#include "options/Options.hpp"
#include "tools/Statistics.hpp"

namespace uno {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "ConstraintRelaxationStrategyFactory.hpp"
#include "FeasibilityRestoration.hpp"
#include "l1Relaxation.hpp"
#include "tools/Options.hpp"
#include "options/Options.hpp"

namespace uno {
std::unique_ptr<ConstraintRelaxationStrategy> ConstraintRelaxationStrategyFactory::create(const Model& model, const Options& options) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "optimization/Iterate.hpp"
#include "optimization/WarmstartInformation.hpp"
#include "symbolic/VectorView.hpp"
#include "tools/Options.hpp"
#include "options/Options.hpp"

namespace uno {
FeasibilityRestoration::FeasibilityRestoration(const Model& model, const Options& options) :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "optimization/Iterate.hpp"
#include "optimization/WarmstartInformation.hpp"
#include "symbolic/VectorView.hpp"
#include "tools/Options.hpp"
#include "options/Options.hpp"
#include "tools/Statistics.hpp"

/*
Expand Down Expand Up @@ -80,7 +80,7 @@ namespace uno {
}

void l1Relaxation::switch_to_feasibility_problem(Statistics& /*statistics*/, Iterate& /*current_iterate*/) {
throw std::runtime_error("l1Relaxation::switch_to_feasibility_problem is not implemented\n");
throw std::runtime_error("l1Relaxation::switch_to_feasibility_problem is not implemented");
}

// use Byrd's steering rules to update the penalty parameter and compute a descent direction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "optimization/Iterate.hpp"
#include "optimization/WarmstartInformation.hpp"
#include "tools/Logger.hpp"
#include "tools/Options.hpp"
#include "options/Options.hpp"
#include "tools/Statistics.hpp"

namespace uno {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "model/Model.hpp"
#include "optimization/Iterate.hpp"
#include "symbolic/Expression.hpp"
#include "tools/Options.hpp"
#include "options/Options.hpp"

namespace uno {
GlobalizationMechanism::GlobalizationMechanism(ConstraintRelaxationStrategy& constraint_relaxation_strategy) :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "ingredients/constraint_relaxation_strategies/ConstraintRelaxationStrategy.hpp"
#include "ingredients/globalization_mechanisms/TrustRegionStrategy.hpp"
#include "ingredients/globalization_mechanisms/BacktrackingLineSearch.hpp"
#include "tools/Options.hpp"
#include "options/Options.hpp"

namespace uno {
std::unique_ptr<GlobalizationMechanism> GlobalizationMechanismFactory::create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "optimization/Iterate.hpp"
#include "optimization/WarmstartInformation.hpp"
#include "tools/Logger.hpp"
#include "tools/Options.hpp"
#include "options/Options.hpp"
#include "tools/Statistics.hpp"

namespace uno {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <cmath>
#include "GlobalizationStrategy.hpp"
#include "tools/Options.hpp"
#include "options/Options.hpp"

namespace uno {
GlobalizationStrategy::GlobalizationStrategy(const Options& options):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "l1MeritFunction.hpp"
#include "ProgressMeasures.hpp"
#include "tools/Logger.hpp"
#include "tools/Options.hpp"
#include "options/Options.hpp"
#include "tools/Statistics.hpp"

namespace uno {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "../ProgressMeasures.hpp"
#include "optimization/Iterate.hpp"
#include "tools/Logger.hpp"
#include "tools/Options.hpp"
#include "options/Options.hpp"
#include "tools/Statistics.hpp"

namespace uno {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "filters/Filter.hpp"
#include "filters/FilterFactory.hpp"
#include "optimization/Iterate.hpp"
#include "tools/Options.hpp"
#include "options/Options.hpp"
#include "tools/Statistics.hpp"

namespace uno {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "ingredients/globalization_strategies/ProgressMeasures.hpp"
#include "optimization/Iterate.hpp"
#include "tools/Logger.hpp"
#include "tools/Options.hpp"
#include "options/Options.hpp"
#include "tools/Statistics.hpp"

namespace uno {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "ingredients/globalization_strategies/ProgressMeasures.hpp"
#include "optimization/Iterate.hpp"
#include "tools/Logger.hpp"
#include "tools/Options.hpp"
#include "options/Options.hpp"
#include "tools/Statistics.hpp"

namespace uno {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "Filter.hpp"
#include "symbolic/Range.hpp"
#include "tools/Logger.hpp"
#include "tools/Options.hpp"
#include "options/Options.hpp"

namespace uno {
Filter::Filter(const Options& options) :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <stdexcept>
#include "FilterFactory.hpp"
#include "NonmonotoneFilter.hpp"
#include "tools/Options.hpp"
#include "options/Options.hpp"

namespace uno {
// FilterFactory class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "NonmonotoneFilter.hpp"
#include "symbolic/Range.hpp"
#include "tools/Logger.hpp"
#include "tools/Options.hpp"
#include "options/Options.hpp"

namespace uno {
NonmonotoneFilter::NonmonotoneFilter(const Options& options) :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include "Funnel.hpp"
#include "tools/Logger.hpp"
#include "tools/Options.hpp"
#include "options/Options.hpp"

namespace uno {
Funnel::Funnel(const Options& options):
Expand Down Expand Up @@ -48,7 +48,7 @@ namespace uno {
this->width = this->margin * this->width;
}
else {
throw std::runtime_error("Funnel update strategy " + std::to_string(this->update_strategy) + " is unknown.");
throw std::runtime_error("Funnel update strategy " + std::to_string(this->update_strategy) + " is unknown");
}
DEBUG << "\t\tNew funnel parameter is: " << this->width << '\n';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "FunnelMethod.hpp"
#include "../../ProgressMeasures.hpp"
#include "optimization/Iterate.hpp"
#include "tools/Options.hpp"
#include "options/Options.hpp"
#include "tools/Logger.hpp"
#include "tools/Statistics.hpp"

Expand Down
5 changes: 2 additions & 3 deletions uno/ingredients/hessian_models/ConvexifiedHessian.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
#include "solvers/DirectSymmetricIndefiniteLinearSolver.hpp"
#include "solvers/SymmetricIndefiniteLinearSolverFactory.hpp"
#include "tools/Logger.hpp"
#include "tools/Options.hpp"
#include "options/Options.hpp"
#include "tools/Statistics.hpp"

namespace uno {
ConvexifiedHessian::ConvexifiedHessian(size_t dimension, size_t maximum_number_nonzeros, const Options& options):
HessianModel(dimension, maximum_number_nonzeros, options.get_string("sparse_format"), /* use_regularization = */true),
// inertia-based convexification needs a linear solver
linear_solver(SymmetricIndefiniteLinearSolverFactory::create(options.get_string("linear_solver"), dimension, maximum_number_nonzeros)),
linear_solver(SymmetricIndefiniteLinearSolverFactory::create(dimension, maximum_number_nonzeros, options)),
regularization_initial_value(options.get_double("regularization_initial_value")),
regularization_increase_factor(options.get_double("regularization_increase_factor")) {
}
Expand Down Expand Up @@ -50,7 +50,6 @@ namespace uno {
symbolic_factorization_performed = true;
}
this->linear_solver->do_numerical_factorization(hessian);

if (this->linear_solver->rank() == number_original_variables && this->linear_solver->number_negative_eigenvalues() == 0) {
good_inertia = true;
DEBUG << "Factorization was a success\n";
Expand Down
2 changes: 1 addition & 1 deletion uno/ingredients/hessian_models/ExactHessian.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include "ExactHessian.hpp"
#include "reformulation/OptimizationProblem.hpp"
#include "tools/Options.hpp"
#include "options/Options.hpp"

namespace uno {
// exact Hessian
Expand Down
2 changes: 1 addition & 1 deletion uno/ingredients/hessian_models/ZeroHessian.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include "ZeroHessian.hpp"
#include "reformulation/OptimizationProblem.hpp"
#include "tools/Options.hpp"
#include "options/Options.hpp"

namespace uno {
ZeroHessian::ZeroHessian(size_t dimension, const Options& options) :
Expand Down
2 changes: 1 addition & 1 deletion uno/ingredients/subproblems/SubproblemFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "ingredients/subproblems/interior_point_methods/PrimalDualInteriorPointSubproblem.hpp"
#include "solvers/QPSolverFactory.hpp"
#include "solvers/SymmetricIndefiniteLinearSolverFactory.hpp"
#include "tools/Options.hpp"
#include "options/Options.hpp"

namespace uno {
std::unique_ptr<Subproblem> SubproblemFactory::create(size_t number_variables, size_t number_constraints, size_t number_objective_gradient_nonzeros,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "ingredients/subproblems/Direction.hpp"
#include "linear_algebra/Vector.hpp"
#include "reformulation/l1RelaxedProblem.hpp"
#include "tools/Options.hpp"
#include "options/Options.hpp"
#include "symbolic/VectorView.hpp"

namespace uno {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@

#include "LPSubproblem.hpp"
#include "ingredients/subproblems/Direction.hpp"
#include "linear_algebra/COOSparseStorage.hpp"
#include "optimization/WarmstartInformation.hpp"
#include "reformulation/OptimizationProblem.hpp"
#include "solvers/LPSolver.hpp"
#include "solvers/LPSolverFactory.hpp"
#include "tools/Options.hpp"
#include "options/Options.hpp"

namespace uno {
LPSubproblem::LPSubproblem(size_t number_variables, size_t number_constraints, size_t number_objective_gradient_nonzeros,
size_t number_jacobian_nonzeros, const Options& options) :
InequalityConstrainedMethod("zero", number_variables, number_constraints, 0, false, options),
solver(LPSolverFactory::create(options.get_string("LP_solver"), number_variables, number_constraints,
solver(LPSolverFactory::create(number_variables, number_constraints,
number_objective_gradient_nonzeros, number_jacobian_nonzeros, options)),
zero_hessian(SymmetricMatrix<size_t, double>::zero(number_variables)) {
}
Expand Down
Loading

0 comments on commit 0b68f17

Please sign in to comment.