Skip to content

Commit

Permalink
add requires_reinitialization parameter to options
Browse files Browse the repository at this point in the history
  • Loading branch information
lvanroye committed Nov 26, 2024
1 parent fb3d050 commit 30ae712
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 68 deletions.
12 changes: 6 additions & 6 deletions fatrop/solver/FatropOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,19 @@ void BoolOption::set(const std::string &value)
}
}
template <typename T>
NumericOption<T> NumericOption<T>::lower_bounded(const std::string &name, const std::string &description, const T &default_value, const T &lower_bound)
NumericOption<T> NumericOption<T>::lower_bounded(const std::string &name, const std::string &description, const T &default_value, const T &lower_bound, bool requires_reinitialization)
{
return NumericOption<T>(name, description, default_value, true, lower_bound, false, 0);
return NumericOption<T>(name, description, default_value, true, lower_bound, false, 0, requires_reinitialization);
}
template <typename T>
NumericOption<T> NumericOption<T>::upper_bounded(const std::string &name, const std::string &description, const T &default_value, const T &upper_bound)
NumericOption<T> NumericOption<T>::upper_bounded(const std::string &name, const std::string &description, const T &default_value, const T &upper_bound, bool requires_reinitialization)
{
return NumericOption<T>(name, description, default_value, false, 0, true, upper_bound);
return NumericOption<T>(name, description, default_value, false, 0, true, upper_bound, requires_reinitialization);
}
template <typename T>
NumericOption<T> NumericOption<T>::bounded(const std::string &name, const std::string &description, const T &default_value, const T &lower_bound, const T &upper_bound)
NumericOption<T> NumericOption<T>::bounded(const std::string &name, const std::string &description, const T &default_value, const T &lower_bound, const T &upper_bound, bool requires_reinitialization)
{
return NumericOption<T>(name, description, default_value, true, lower_bound, true, upper_bound);
return NumericOption<T>(name, description, default_value, true, lower_bound, true, upper_bound, requires_reinitialization);
}
template <typename T>
void NumericOption<T>::set(T value_in)
Expand Down
125 changes: 63 additions & 62 deletions fatrop/solver/FatropOptions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ namespace fatrop
class OptionBase
{
public:
OptionBase(const std::string &name, const std::string &description) : name(name), description(description) {}
OptionBase(const std::string &name, const std::string &description, bool requires_reinitialization) : name(name), description(description), requires_reinitialization(requires_reinitialization) {}
virtual void set(int value) { throw std::invalid_argument("Invalid type for option " + name); };
virtual void set(double value) { throw std::invalid_argument("Invalid type for option " + name); };
virtual void set(const std::string &value) { throw std::invalid_argument("Invalid type " + name); };
virtual void set(bool value) { throw std::invalid_argument("Invalid type " + name); };
// virtual void set(const OptionValueVariant &value);
std::string name;
std::string description;
std::string name; /// < The name of the option.
std::string description; // < The description of the option.
bool requires_reinitialization; /// < wether changing the option requires reinitialization of the solver's algorithmic objects. Reinitialization can require dynamic memory allocation.
};

/**
Expand All @@ -79,7 +79,7 @@ namespace fatrop
* @param description The description of the option.
* @param default_value The default value of the option.
*/
Option(const ::std::string &name, const std::string &description, const T &default_value) : OptionBase(name, description), value(default_value) {}
Option(const ::std::string &name, const std::string &description, const T &default_value, bool requires_reinitialization) : OptionBase(name, description, requires_reinitialization), value(default_value) {}
void set_value(T value_in) { this->value = value_in; }
const T &get() const { return value; }

Expand All @@ -95,7 +95,7 @@ namespace fatrop
class BoolOption : public Option<bool>
{
public:
BoolOption(const std::string &name, const std::string &description, bool default_value) : Option<bool>(name, description, default_value) {}
BoolOption(const std::string &name, const std::string &description, bool default_value, bool requires_reinitialization) : Option<bool>(name, description, default_value, requires_reinitialization) {}
void set(bool value) { Option<bool>::set_value(value); }
void set(const std::string &value);
};
Expand All @@ -120,7 +120,7 @@ namespace fatrop
* @param lower_bound The lower bound for the option value.
* @return A new NumericOption with the lower bound.
*/
static NumericOption<T> lower_bounded(const std::string &name, const std::string &description, const T &default_value, const T &lower_bound);
static NumericOption<T> lower_bounded(const std::string &name, const std::string &description, const T &default_value, const T &lower_bound, bool requires_reinitialization);
/**
* @brief Creates an upper-bounded numeric option.
* @param name The name of the option.
Expand All @@ -129,7 +129,7 @@ namespace fatrop
* @param upper_bound The upper bound for the option value.
* @return A new NumericOption with the upper bound.
*/
static NumericOption<T> upper_bounded(const std::string &name, const std::string &description, const T &default_value, const T &upper_bound);
static NumericOption<T> upper_bounded(const std::string &name, const std::string &description, const T &default_value, const T &upper_bound, bool requires_reinitialization);
/**
* @brief Creates a bounded numeric option with both lower and upper bounds.
* @param name The name of the option.
Expand All @@ -139,7 +139,7 @@ namespace fatrop
* @param upper_bound The upper bound for the option value.
* @return A new NumericOption with both bounds.
*/
static NumericOption<T> bounded(const std::string &name, const std::string &description, const T &default_value, const T &lower_bound, const T &upper_bound);
static NumericOption<T> bounded(const std::string &name, const std::string &description, const T &default_value, const T &lower_bound, const T &upper_bound, bool requires_reinitialization);
void set(T value_in);

private:
Expand All @@ -153,7 +153,7 @@ namespace fatrop
* @param upper_bounded Whether the option has an upper bound.
* @param upper_bound The upper bound.
*/
NumericOption(const std::string &name, const std::string &description, const T &default_value, bool lower_bounded, const T &lower_bound, bool upper_bounded, const T &upper_bound) : Option<T>(name, description, default_value), is_lower_bounded(lower_bounded), lower_bound(lower_bound), is_upper_bounded(upper_bounded), upper_bound(upper_bound) {}
NumericOption(const std::string &name, const std::string &description, const T &default_value, bool lower_bounded, const T &lower_bound, bool upper_bounded, const T &upper_bound, bool requires_reinitialization) : Option<T>(name, description, default_value, requires_reinitialization), is_lower_bounded(lower_bounded), lower_bound(lower_bound), is_upper_bounded(upper_bounded), upper_bound(upper_bound) {}
bool is_lower_bounded; ///< Whether the option has a lower bound.
T lower_bound; ///< The lower bound for the option value.
bool is_upper_bounded; ///< Whether the option has an upper bound.
Expand All @@ -175,63 +175,63 @@ namespace fatrop
struct FatropOptions
{
// print options
IntOption print_level = IntOption::lower_bounded("print_level", "fatrop print level", 10, 0);
IntOption print_level = IntOption::lower_bounded("print_level", "fatrop print level", 10, 0, false);
// fatrop algorithm options
IntOption max_iter = IntOption::lower_bounded("max_iter", "maximum number of iterations", 1000, 0);
DoubleOption tol = DoubleOption::lower_bounded("tol", "tolerance", 1e-8, 0.0);
DoubleOption acceptable_tol = DoubleOption::lower_bounded("acceptable_tol", "acceptable tolerance", 1e-6, 0.0);
IntOption max_watchdog_steps = IntOption::lower_bounded("max_watchdog_steps", "maximum number of watchdog steps", 4, 0);
IntOption acceptable_iter = IntOption::lower_bounded("acceptable_iter", "acceptable iter", 15, 0);
DoubleOption lammax = DoubleOption::lower_bounded("lammax", "lammax", 1e3, 0.0);
DoubleOption mu_init = DoubleOption::lower_bounded("mu_init", "mu_init", 1e2, 0.0);
DoubleOption kappa_eta = DoubleOption::lower_bounded("kappa_eta", "kappa_eta", 10.0, 0.0);
DoubleOption kappa_mu = DoubleOption::lower_bounded("kappa_mu", "kappa_mu", 0.2, 0.0);
DoubleOption theta_mu = DoubleOption::lower_bounded("theta_mu", "theta_mu", 1.5, 0.0);
DoubleOption delta_w0 = DoubleOption::lower_bounded("delta_w0", "delta_w0", 1e-4, 0.0);
DoubleOption delta_wmin = DoubleOption::lower_bounded("delta_wmin", "delta_wmin", 1e-20, 0.0);
DoubleOption kappa_wmin = DoubleOption::lower_bounded("kappa_wmin", "kappa_wmin", 1.0 / 3.0, 0.0);
DoubleOption kappa_wplus = DoubleOption::lower_bounded("kappa_wplus", "kappa_wplus", 8.0, 0.0);
DoubleOption kappa_wplusem = DoubleOption::lower_bounded("kappa_wplusem", "kappa_wplusem", 100.0, 0.0);
DoubleOption delta_c_stripe = DoubleOption::lower_bounded("delta_c_stripe", "delta_c_stripe", 1e-6, 0.0);
DoubleOption kappa_c = DoubleOption::lower_bounded("kappa_c", "kappa_c", 0.25, 0.0);
BoolOption warm_start_init_point = BoolOption("warm_start_init_point", "warm_start_init_point", false);
DoubleOption theta_min = DoubleOption::lower_bounded("theta_min", "theta_min", 1e-4, 0.0);
BoolOption recalc_y = BoolOption("recalc_y", "recalc_y", false);
DoubleOption recalc_y_feas_tol = DoubleOption::lower_bounded("recalc_y_feas_tol", "recalc_y_feas_tol", 1e-6, 0.0);
StringOption inequality_handling = StringOption("inequality_handling", "inequality_handling", "pd_ip");
DoubleOption kappa_d = DoubleOption::lower_bounded("kappa_d", "kappa_d", 1e-5, 0.0);
IntOption max_iter = IntOption::lower_bounded("max_iter", "maximum number of iterations", 1000, 0, true);
DoubleOption tol = DoubleOption::lower_bounded("tol", "tolerance", 1e-8, 0.0, false);
DoubleOption acceptable_tol = DoubleOption::lower_bounded("acceptable_tol", "acceptable tolerance", 1e-6, 0.0, false);
IntOption max_watchdog_steps = IntOption::lower_bounded("max_watchdog_steps", "maximum number of watchdog steps", 4, 0, false);
IntOption acceptable_iter = IntOption::lower_bounded("acceptable_iter", "acceptable iter", 15, 0, false);
DoubleOption lammax = DoubleOption::lower_bounded("lammax", "lammax", 1e3, 0.0, false);
DoubleOption mu_init = DoubleOption::lower_bounded("mu_init", "mu_init", 1e2, 0.0, false);
DoubleOption kappa_eta = DoubleOption::lower_bounded("kappa_eta", "kappa_eta", 10.0, 0.0, false);
DoubleOption kappa_mu = DoubleOption::lower_bounded("kappa_mu", "kappa_mu", 0.2, 0.0, false);
DoubleOption theta_mu = DoubleOption::lower_bounded("theta_mu", "theta_mu", 1.5, 0.0, false);
DoubleOption delta_w0 = DoubleOption::lower_bounded("delta_w0", "delta_w0", 1e-4, 0.0, false);
DoubleOption delta_wmin = DoubleOption::lower_bounded("delta_wmin", "delta_wmin", 1e-20, 0.0, false);
DoubleOption kappa_wmin = DoubleOption::lower_bounded("kappa_wmin", "kappa_wmin", 1.0 / 3.0, 0.0, false);
DoubleOption kappa_wplus = DoubleOption::lower_bounded("kappa_wplus", "kappa_wplus", 8.0, 0.0, false);
DoubleOption kappa_wplusem = DoubleOption::lower_bounded("kappa_wplusem", "kappa_wplusem", 100.0, 0.0, false);
DoubleOption delta_c_stripe = DoubleOption::lower_bounded("delta_c_stripe", "delta_c_stripe", 1e-6, 0.0, false);
DoubleOption kappa_c = DoubleOption::lower_bounded("kappa_c", "kappa_c", 0.25, 0.0, false);
BoolOption warm_start_init_point = BoolOption("warm_start_init_point", "warm_start_init_point", false, false);
DoubleOption theta_min = DoubleOption::lower_bounded("theta_min", "theta_min", 1e-4, 0.0, false);
BoolOption recalc_y = BoolOption("recalc_y", "recalc_y", false, false);
DoubleOption recalc_y_feas_tol = DoubleOption::lower_bounded("recalc_y_feas_tol", "recalc_y_feas_tol", 1e-6, 0.0, false);
StringOption inequality_handling = StringOption("inequality_handling", "inequality_handling", "pd_ip", true);
DoubleOption kappa_d = DoubleOption::lower_bounded("kappa_d", "kappa_d", 1e-5, 0.0, false);
// line search options
BoolOption accept_every_trial_step = BoolOption("accept_every_trial_step", "accept every trial step", false);
DoubleOption s_phi = DoubleOption::lower_bounded("s_phi", "s_phi", 2.3, 0.0);
DoubleOption delta = DoubleOption::lower_bounded("delta", "delta", 1.0, 0.0);
DoubleOption s_theta = DoubleOption::lower_bounded("s_theta", "s_theta", 1.1, 0.0);
DoubleOption gamma_theta = DoubleOption::lower_bounded("gamma_theta", "gamma_theta", 1e-5, 0.0);
DoubleOption gamma_phi = DoubleOption::lower_bounded("gamma_phi", "gamma_phi", 1e-8, 0.0);
DoubleOption eta_phi = DoubleOption::lower_bounded("eta_phi", "eta_phi", 1e-8, 0.0);
DoubleOption gamma_alpha = DoubleOption::lower_bounded("gamma_alpha", "gamma_alpha", 0.05, 0.0);
IntOption max_soc = IntOption::lower_bounded("max_soc", "max_soc", 2, 0);
BoolOption accept_every_trial_step = BoolOption("accept_every_trial_step", "accept every trial step", false, false);
DoubleOption s_phi = DoubleOption::lower_bounded("s_phi", "s_phi", 2.3, 0.0, false);
DoubleOption delta = DoubleOption::lower_bounded("delta", "delta", 1.0, 0.0, false);
DoubleOption s_theta = DoubleOption::lower_bounded("s_theta", "s_theta", 1.1, 0.0, false);
DoubleOption gamma_theta = DoubleOption::lower_bounded("gamma_theta", "gamma_theta", 1e-5, 0.0, false);
DoubleOption gamma_phi = DoubleOption::lower_bounded("gamma_phi", "gamma_phi", 1e-8, 0.0, false);
DoubleOption eta_phi = DoubleOption::lower_bounded("eta_phi", "eta_phi", 1e-8, 0.0, false);
DoubleOption gamma_alpha = DoubleOption::lower_bounded("gamma_alpha", "gamma_alpha", 0.05, 0.0, false);
IntOption max_soc = IntOption::lower_bounded("max_soc", "max_soc", 2, 0, false);
// linear solver options
BoolOption linsol_iterative_refinement = BoolOption("linsol_iterative_refinement", "iterative ref", true);
BoolOption linsol_perturbed_mode = BoolOption("linsol_perturbed_mode", "linear solver perturbed mode", false);
BoolOption linsol_diagnostic = BoolOption("linsol_diagnostic", "linear solver diagnostic mode", false);
DoubleOption linsol_perturbed_mode_param = DoubleOption::lower_bounded("linsol_perturbed_mode_param", "linear solver perturbed mode param", 1e-6, 0.);
IntOption linsol_min_it_ref = IntOption::lower_bounded("linsol_min_it_ref", "minimum number of iterative refinement steps", 0, 0);
IntOption linsol_max_it_ref = IntOption::lower_bounded("linsol_max_it_ref", "maximum number of iterative refinement steps", 5, 0);
DoubleOption linsol_min_it_acc = DoubleOption::lower_bounded("linsol_min_it_acc", "stopping criterion for iterative refinement procedure", 1e-8, 0.);
DoubleOption linsol_lu_fact_tol = DoubleOption::lower_bounded("linsol_lu_fact_tol", "pivoting tolerance parameter for lu fact", 1e-5, 0.);
BoolOption iterative_refinement_SOC = BoolOption("iterative_refinement_SOC", "Use iterative refinement for SOC", true);
BoolOption ls_scaling = BoolOption("ls_scaling", "Use automatic scaling for linear system", true);
BoolOption linsol_iterative_refinement = BoolOption("linsol_iterative_refinement", "iterative ref", true, false);
BoolOption linsol_perturbed_mode = BoolOption("linsol_perturbed_mode", "linear solver perturbed mode", false, false);
BoolOption linsol_diagnostic = BoolOption("linsol_diagnostic", "linear solver diagnostic mode", false, false);
DoubleOption linsol_perturbed_mode_param = DoubleOption::lower_bounded("linsol_perturbed_mode_param", "linear solver perturbed mode param", 1e-6, 0., false);
IntOption linsol_min_it_ref = IntOption::lower_bounded("linsol_min_it_ref", "minimum number of iterative refinement steps", 0, 0, false);
IntOption linsol_max_it_ref = IntOption::lower_bounded("linsol_max_it_ref", "maximum number of iterative refinement steps", 5, 0, false);
DoubleOption linsol_min_it_acc = DoubleOption::lower_bounded("linsol_min_it_acc", "stopping criterion for iterative refinement procedure", 1e-8, 0., false);
DoubleOption linsol_lu_fact_tol = DoubleOption::lower_bounded("linsol_lu_fact_tol", "pivoting tolerance parameter for lu fact", 1e-5, 0., false);
BoolOption iterative_refinement_SOC = BoolOption("iterative_refinement_SOC", "Use iterative refinement for SOC", true, false);
BoolOption ls_scaling = BoolOption("ls_scaling", "Use automatic scaling for linear system", true, false);
// fatrop data options
DoubleOption warm_start_mult_bound_push = DoubleOption::lower_bounded("warm_start_mult_bound_push", "warm_start_mult_bound_push", 1e-2, 0.0);
DoubleOption smax = DoubleOption::lower_bounded("smax", "smax", 100.0, 0.0);
DoubleOption bound_push = DoubleOption::lower_bounded("bound_push", "kappa1", 1e-2, 0.0);
DoubleOption bound_frac = DoubleOption::lower_bounded("bound_frac", "kappa2", 1e-2, 0.0);
DoubleOption kappa_sigma = DoubleOption::lower_bounded("kappa_sigma", "kappa_sigma", 1e10, 0.0);
DoubleOption bound_relax_factor = DoubleOption::lower_bounded("bound_relax_factor", "bound_relax_factor", 1e-8, 0.0);
DoubleOption constr_viol_tol = DoubleOption::lower_bounded("constr_viol_tol", "constr_viol_tol", 1e-4, 0.0);
DoubleOption warm_start_mult_bound_push = DoubleOption::lower_bounded("warm_start_mult_bound_push", "warm_start_mult_bound_push", 1e-2, 0.0, false);
DoubleOption smax = DoubleOption::lower_bounded("smax", "smax", 100.0, 0.0, false);
DoubleOption bound_push = DoubleOption::lower_bounded("bound_push", "kappa1", 1e-2, 0.0, false);
DoubleOption bound_frac = DoubleOption::lower_bounded("bound_frac", "kappa2", 1e-2, 0.0, false);
DoubleOption kappa_sigma = DoubleOption::lower_bounded("kappa_sigma", "kappa_sigma", 1e10, 0.0, false);
DoubleOption bound_relax_factor = DoubleOption::lower_bounded("bound_relax_factor", "bound_relax_factor", 1e-8, 0.0, false);
DoubleOption constr_viol_tol = DoubleOption::lower_bounded("constr_viol_tol", "constr_viol_tol", 1e-4, 0.0, false);
// restoration phase options
DoubleOption resto_rho = DoubleOption::lower_bounded("resto_rho", "Resto L1 penalty parameter", 1000., 0.0);
DoubleOption resto_xi = DoubleOption::lower_bounded("resto_xi", "Resto xi parameter", 1., 0.0);
DoubleOption resto_rho = DoubleOption::lower_bounded("resto_rho", "Resto L1 penalty parameter", 1000., 0.0, false);
DoubleOption resto_xi = DoubleOption::lower_bounded("resto_xi", "Resto xi parameter", 1., 0.0, false);
};

// /**
Expand Down Expand Up @@ -265,6 +265,7 @@ namespace fatrop
// T can be int, double, bool, string, or an OptionValueVariant
template <typename T>
void set(const std::string &name, T value);

public:
std::unordered_map<std::string, OptionBase *> options;
};
Expand Down

0 comments on commit 30ae712

Please sign in to comment.