Skip to content

Commit

Permalink
run linters
Browse files Browse the repository at this point in the history
  • Loading branch information
rkaminsk committed Sep 28, 2023
1 parent 2387ae1 commit 699f56c
Show file tree
Hide file tree
Showing 20 changed files with 839 additions and 1,076 deletions.
2 changes: 2 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ repos:
rev: v1.3.5
hooks:
- id: clang-format
exclude: _clingodl.c
args: ["-i"]
- id: clang-tidy
exclude: _clingodl.c
args: ["-fix"]
24 changes: 12 additions & 12 deletions app/lib/clingo-dl-app/app.hh
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
#ifndef CLINGODL_APP_HH
#define CLINGODL_APP_HH

#include <clingo.hh>
#include <clingo-dl.h>
#include <clingo.hh>
#include <optional>

namespace ClingoDL {
Expand All @@ -36,19 +36,19 @@ using int_value_t = int;

//! Helper class to rewrite logic programs to use with the clingo DL theory.
class Rewriter {
public:
public:
Rewriter(clingodl_theory_t *theory, clingo_program_builder_t *builder);
//! Rewrite the given files.
void rewrite(Clingo::Control &ctl, Clingo::StringSpan files);
//! Rewrite the given program.
void rewrite(Clingo::Control &ctl, char const *str);

private:
private:
//! C callback to add a statement using the builder.
static bool add_(clingo_ast_t *stm, void *data);
static auto add_(clingo_ast_t *stm, void *data) -> bool;

//! C callback to rewrite a statement and add it via the builder.
static bool rewrite_(clingo_ast_t *stm, void *data);
static auto rewrite_(clingo_ast_t *stm, void *data) -> bool;

clingodl_theory_t *theory_; //!< A theory handle to rewrite statements.
clingo_program_builder_t *builder_; //!< The builder to add rewritten statements to.
Expand All @@ -66,36 +66,36 @@ struct OptimizerConfig {

//! Class providing a configurable optimization algorithm.
class Optimizer : private Clingo::SolveEventHandler {
private:
private:
//! Type used to store bounds.
using Bound = std::optional<int_value_t>;
using EventHandler = Clingo::SolveEventHandler;

public:
public:
Optimizer(OptimizerConfig const &opt_cfg, EventHandler &handler, clingodl_theory_t *theory);
//! Run the optimization algorithm.
//!
//! \note
//! With an API extension to implement a custom enumerator, one could
//! implement this more nicely. Right now, this implementation is
//! restricted to the application.
void solve(Clingo::Control& ctl);
void solve(Clingo::Control &ctl);

private:
private:
//! Function to add DL specific statistics.
void on_statistics(Clingo::UserStatistics step, Clingo::UserStatistics accu) override;
//! Add information about bounds to the given root statistics object and
//! pass call the theory specific handler.
void add_stats(Clingo::UserStatistics root) const;
//! Function to extract the current bound and pass the model to the theory.
bool on_model(Clingo::Model &model) override;
auto on_model(Clingo::Model &model) -> bool override;
//! Extract the bound from the given model.
int_value_t get_bound(Clingo::Model &model);
auto get_bound(Clingo::Model &model) -> int_value_t;
//! Prepare the program for solving.
//!
//! This adds constraints to enforce the current upper or search bound as
//! well as removes no longer required bounds.
void prepare_(Clingo::Control& ctl);
void prepare_(Clingo::Control &ctl);

OptimizerConfig const &opt_cfg_; //!< Configuration of the optimization algorithm.
EventHandler &handler_; //!< Theory specific solve event handler.
Expand Down
48 changes: 21 additions & 27 deletions app/lib/src/app.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,52 +24,47 @@

#include <clingo-dl-app/app.hh>

#include <cmath>
#include <iostream>
#include <limits>
#include <cmath>

namespace ClingoDL {

Rewriter::Rewriter(clingodl_theory_t *theory, clingo_program_builder_t *builder)
: theory_{theory}
, builder_{builder} {
}
Rewriter::Rewriter(clingodl_theory_t *theory, clingo_program_builder_t *builder) : theory_{theory}, builder_{builder} {}

void Rewriter::rewrite(Clingo::Control &ctl, Clingo::StringSpan files) {
Clingo::Detail::handle_error(clingo_ast_parse_files(files.begin(), files.size(), rewrite_, this, ctl.to_c(), nullptr, nullptr, 0));
Clingo::Detail::handle_error(
clingo_ast_parse_files(files.begin(), files.size(), rewrite_, this, ctl.to_c(), nullptr, nullptr, 0));
}

void Rewriter::rewrite(Clingo::Control &ctl, char const *str) {
Clingo::Detail::handle_error(clingo_ast_parse_string(str, rewrite_, this, ctl.to_c(), nullptr, nullptr, 0));
}

bool Rewriter::add_(clingo_ast_t *stm, void *data) {
auto *self = static_cast<Rewriter*>(data);
auto Rewriter::add_(clingo_ast_t *stm, void *data) -> bool {
auto *self = static_cast<Rewriter *>(data);
return clingo_program_builder_add(self->builder_, stm);
}

bool Rewriter::rewrite_(clingo_ast_t *stm, void *data) {
auto *self = static_cast<Rewriter*>(data);
auto Rewriter::rewrite_(clingo_ast_t *stm, void *data) -> bool {
auto *self = static_cast<Rewriter *>(data);
return clingodl_rewrite_ast(self->theory_, stm, add_, self);
}

Optimizer::Optimizer(OptimizerConfig const &opt_cfg, Clingo::SolveEventHandler &handler, clingodl_theory_t *theory)
: opt_cfg_{opt_cfg}
, handler_{handler}
, theory_{theory} {
}
: opt_cfg_{opt_cfg}, handler_{handler}, theory_{theory} {}

void Optimizer::solve(Clingo::Control& ctl) {
void Optimizer::solve(Clingo::Control &ctl) {
Clingo::AST::with_builder(ctl, [&](Clingo::AST::ProgramBuilder &builder) {
Rewriter rewriter{theory_, builder.to_c()};
rewriter.rewrite(ctl,
// add a fixed bound
"#program __ub(s,b)."
"&diff { s-0 } <= b."
// add a retractable bound
"#program __sb(s,b)."
"#external __sb(b). [true]"
"&diff { s-0 } <= b :- __sb(b).");
// add a fixed bound
"#program __ub(s,b)."
"&diff { s-0 } <= b."
// add a retractable bound
"#program __sb(s,b)."
"#external __sb(b). [true]"
"&diff { s-0 } <= b :- __sb(b).");
});
if (opt_cfg_.has_initial) {
upper_bound_ = opt_cfg_.initial;
Expand Down Expand Up @@ -111,7 +106,7 @@ void Optimizer::add_stats(Clingo::UserStatistics root) const {
}
}

bool Optimizer::on_model(Clingo::Model &model) {
auto Optimizer::on_model(Clingo::Model &model) -> bool {
// update (upper) bound
optimization = get_bound(model);
upper_bound_ = *optimization - 1;
Expand All @@ -134,7 +129,7 @@ bool Optimizer::on_model(Clingo::Model &model) {
return false;
}

int_value_t Optimizer::get_bound(Clingo::Model &model) {
auto Optimizer::get_bound(Clingo::Model &model) -> int_value_t {
// get bound
if (opt_cfg_.index == 0) {
if (!clingodl_lookup_symbol(theory_, opt_cfg_.symbol.to_c(), &opt_cfg_.index)) {
Expand All @@ -153,7 +148,7 @@ int_value_t Optimizer::get_bound(Clingo::Model &model) {
return value.int_number; // NOLINT
}

void Optimizer::prepare_(Clingo::Control& ctl) {
void Optimizer::prepare_(Clingo::Control &ctl) {
if (upper_bound_ && upper_bound_ != upper_bound_last_) {
upper_bound_last_ = upper_bound_;
ctl.ground({{"__ub", {opt_cfg_.symbol, Clingo::Number(*upper_bound_)}}});
Expand All @@ -165,8 +160,7 @@ void Optimizer::prepare_(Clingo::Control& ctl) {
if (search_bound_ && search_bound_ != upper_bound_) {
search_bound_last_ = search_bound_;
ctl.ground({{"__sb", {opt_cfg_.symbol, Clingo::Number(*search_bound_)}}});
}
else {
} else {
search_bound_last_ = Bound{};
}
}
Expand Down
66 changes: 27 additions & 39 deletions app/src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@

// }}}

#include <clingo.hh>
#include <clingo-dl.h>
#include <clingo-dl-app/app.hh>
#include <sstream>
#include <clingo-dl.h>
#include <clingo.hh>
#include <fstream>
#include <limits>
#include <sstream>

#ifdef CLINGODL_PROFILE
#include <gperftools/profiler.h>
Expand All @@ -39,27 +39,19 @@ using Clingo::Detail::handle_error;

//! Application class to run clingo-dl.
class App : public Clingo::Application, private Clingo::SolveEventHandler {
public:
App() {
handle_error(clingodl_create(&theory_));
}
public:
App() { handle_error(clingodl_create(&theory_)); }
App(App const &) = default;
App(App &&) = default;
App &operator=(App const &) = default;
App &operator=(App &&) = default;
~App() override {
clingodl_destroy(theory_);
}
auto operator=(App const &) -> App & = default;
auto operator=(App &&) -> App & = default;
~App() override { clingodl_destroy(theory_); }
//! Set program name to clingo-dl.
char const *program_name() const noexcept override {
return "clingo-dl";
}
auto program_name() const noexcept -> char const * override { return "clingo-dl"; }
//! Set the version.
char const *version() const noexcept override {
return CLINGODL_VERSION;
}
auto version() const noexcept -> char const * override { return CLINGODL_VERSION; }
//! Pass models to the theory.
bool on_model(Clingo::Model &model) override {
auto on_model(Clingo::Model &model) -> bool override {
handle_error(clingodl_on_model(theory_, model.to_c()));
return true;
}
Expand All @@ -82,16 +74,15 @@ class App : public Clingo::Application, private Clingo::SolveEventHandler {
#endif
if (!opt_cfg_.active) {
ctl.solve(Clingo::SymbolicLiteralSpan{}, this, false, false).get();
}
else {
} else {
Optimizer{opt_cfg_, *this, theory_}.solve(ctl);
}
#ifdef CLINGODL_PROFILE
ProfilerStop();
#endif
}
//! Parse the variable to minimize and an optional initial bound.
bool parse_bound(char const *value) {
auto parse_bound(char const *value) -> bool {
std::ostringstream oss;
oss << "(" << value << ",)";
auto term = Clingo::parse_term(oss.str().c_str());
Expand All @@ -109,7 +100,7 @@ class App : public Clingo::Application, private Clingo::SolveEventHandler {
return true;
}
//! Parse factor to adjust optimization step length.
bool parse_factor(char const *value) {
auto parse_factor(char const *value) -> bool {
std::stringstream strValue;
strValue.imbue(std::locale::classic());
strValue << value;
Expand All @@ -124,32 +115,29 @@ class App : public Clingo::Application, private Clingo::SolveEventHandler {
//! Register options of the theory and optimization related options.
void register_options(Clingo::ClingoOptions &options) override {
handle_error(clingodl_register_options(theory_, options.to_c()));
char const * group = "Clingo.DL Options";
char const *group = "Clingo.DL Options";
options.add(group, "minimize-variable",
"Minimize the given variable\n"
" <arg> : <variable>[,<initial>]\n"
" <variable>: the variable to minimize\n"
" <initial> : upper bound for the variable",
[this](char const *value) { return parse_bound(value); });
options.add(group, "minimize-factor",
"Factor to adjust minimization step size [1]",
[this](char const *value) { return parse_factor(value); },
false, "<factor>");
"Minimize the given variable\n"
" <arg> : <variable>[,<initial>]\n"
" <variable>: the variable to minimize\n"
" <initial> : upper bound for the variable",
[this](char const *value) { return parse_bound(value); });
options.add(
group, "minimize-factor", "Factor to adjust minimization step size [1]",
[this](char const *value) { return parse_factor(value); }, false, "<factor>");
}
//! Validate options of the theory.
void validate_options() override {
handle_error(clingodl_validate_options(theory_));
}
void validate_options() override { handle_error(clingodl_validate_options(theory_)); }

private:
private:
clingodl_theory_t *theory_{nullptr}; //!< The underlying DL theory.
OptimizerConfig opt_cfg_; //!< The optimization configuration.
};

} // namespace CLingoDL
} // namespace ClingoDL

//! Run the clingo-dl application.
int main(int argc, char *argv[]) { // NOLINT(bugprone-exception-escape)
auto main(int argc, char *argv[]) -> int { // NOLINT(bugprone-exception-escape)
ClingoDL::App app;
return Clingo::clingo_main(app, {argv + 1, static_cast<size_t>(argc - 1)});
}
Loading

0 comments on commit 699f56c

Please sign in to comment.