Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BDD/Generator API #550

Merged
merged 6 commits into from
Oct 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 5 additions & 11 deletions docs/example/queens.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,21 +218,15 @@ uint64_t n_queens_list(uint64_t N, uint64_t column,
partial_assignment.push_back(row_q);

// Construct the assignment for this entire column
adiar::assignment_file column_assignment;
std::vector<adiar::pair<adiar::bdd::label_type, bool>> column_assignment;

{ // The assignment_writer has to be detached, before we call any
// bdd functions. It is automatically detached upon destruction,
// hence we have it in this little scope.
adiar::file_writer<map_pair<bdd::label_t, bool>> aw(column_assignment);

for (uint64_t row = 0; row < N; row++) {
aw << assignment(label_of_position(N, row, column),
row == row_q);
}
for (uint64_t row = 0; row < N; row++) {
column_assignment.push_back({label_of_position(N, row, column), row == row_q});
}

adiar::bdd restricted_constraints = adiar::bdd_restrict(constraints,
column_assignment);
column_assignment.begin(),
column_assignment.end());

if (adiar::bdd_pathcount(restricted_constraints) == 1) {
solutions += 1;
Expand Down
16 changes: 6 additions & 10 deletions example/queens.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,19 +248,15 @@ uint64_t n_queens_list(uint64_t N, uint64_t column,
partial_assignment.push_back(row_q);

// Construct the assignment for this entire column
adiar::shared_file<adiar::map_pair<adiar::bdd::label_type, adiar::assignment>> column_assignment;
std::vector<adiar::pair<adiar::bdd::label_type, bool>> column_assignment;

{ // The assignment_writer has to be detached, before we call any bdd
// functions. It is automatically detached upon destruction, hence we have
// it in this little scope.
adiar::file_writer<adiar::map_pair<adiar::bdd::label_type, adiar::assignment>> aw(column_assignment);

for (uint64_t row = 0; row < N; row++) {
aw << adiar::map_pair<adiar::bdd::label_type, adiar::assignment>(label_of_position(N, row, column), row == row_q);
}
for (uint64_t row = 0; row < N; row++) {
column_assignment.push_back({label_of_position(N, row, column), row == row_q});
}

adiar::bdd restricted_constraints = adiar::bdd_restrict(constraints, column_assignment);
adiar::bdd restricted_constraints = adiar::bdd_restrict(constraints,
column_assignment.begin(),
column_assignment.end());

if (adiar::bdd_pathcount(restricted_constraints) == 1) {
solutions += 1;
Expand Down
3 changes: 0 additions & 3 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,6 @@ test/adiar/domain:
test/adiar/exec_policy:
make $(MAKE_FLAGS) test TEST_FOLDER=test/adiar TEST_NAME=exec_policy

test/adiar/map:
make $(MAKE_FLAGS) test TEST_FOLDER=test/adiar TEST_NAME=map

test/adiar/bdd/apply:
make $(MAKE_FLAGS) test TEST_FOLDER=test/adiar/bdd TEST_NAME=apply

Expand Down
4 changes: 1 addition & 3 deletions src/adiar/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@ set_property(GLOBAL PROPERTY USE_FOLDERS On)
set(HEADERS
# adiar
adiar.h
assignment.h
bool_op.h
builder.h
deprecated.h
domain.h
exception.h
exec_policy.h
file.h
functional.h
map.h
statistics.h
types.h

# adiar/bdd
bdd.h
Expand Down
1 change: 0 additions & 1 deletion src/adiar/adiar.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#include <adiar/exception.h>
#include <adiar/exec_policy.h>
#include <adiar/functional.h>
#include <adiar/file.h> // <-- TODO: Remove!

////////////////////////////////////////////////////////////////////////////////
/// Global Domain
Expand Down
20 changes: 0 additions & 20 deletions src/adiar/assignment.h

This file was deleted.

85 changes: 71 additions & 14 deletions src/adiar/bdd.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@
#include <string>
#include <iostream>

#include <adiar/assignment.h>
#include <adiar/bool_op.h>
#include <adiar/exec_policy.h>
#include <adiar/file.h> // <-- TODO: Remove
#include <adiar/functional.h>
#include <adiar/types.h>

#include <adiar/bdd/bdd.h>
#include <adiar/zdd/zdd.h>
Expand All @@ -33,6 +32,20 @@ namespace adiar
///
/// \{

/// \cond
//////////////////////////////////////////////////////////////////////////////
/// \brief End marker for the BDD assignment generators.
//////////////////////////////////////////////////////////////////////////////
template<>
struct generator_end<pair<bdd::label_type, bool>>
{
using value_type = pair<bdd::label_type, bool>;

static constexpr value_type value =
make_pair(static_cast<bdd::label_type>(-1), false);
};
/// \endcond

//////////////////////////////////////////////////////////////////////////////
/// \name Basic BDD Constructors
///
Expand Down Expand Up @@ -459,28 +472,55 @@ namespace adiar
//////////////////////////////////////////////////////////////////////////////
/// \brief Restrict a subset of variables to constant values.
///
/// \details For each tuple (i,v) in the assignment `xs` the variable
/// \details For each tuple (i,v) in the assignment `xs`, the variable
/// with label i is set to the constant value v. This binds the
/// scope of the variables in `xs`, i.e. any later mention of
/// a variable i is not the same as variable i in `xs`.
///
/// \param f BDD to restrict
/// \param f BDD to restrict.
///
/// \param xs Assignments (i,v) to variables in (in ascending order)
/// \param xs Assignments (i,v) to variables in (in ascending order).
///
/// \returns \f$ f|_{(i,v) \in xs : x_i = v} \f$
//////////////////////////////////////////////////////////////////////////////
// TODO v2.0 : Replace with `generator<pair<...>>`.
__bdd bdd_restrict(const bdd &f,
const shared_file<map_pair<bdd::label_type, assignment>> &xs);
const generator<pair<bdd::label_type, bool>> &xs);

//////////////////////////////////////////////////////////////////////////////
/// \brief Restrict a subset of variables to constant values.
//////////////////////////////////////////////////////////////////////////////
// TODO v2.0 : Replace with `generator<pair<...>>`.
__bdd bdd_restrict(const exec_policy &ep,
const bdd &f,
const shared_file<map_pair<bdd::label_type, assignment>> &xs);
const generator<pair<bdd::label_type, bool>> &xs);

//////////////////////////////////////////////////////////////////////////////
/// \brief Restrict a subset of variables to constant values.
///
/// \details For each tuple (i,v) provided by the iterator, the variable
/// with label i is set to the constant value v.
///
/// \param f BDD to restrict
///
/// \param begin Single-pass forward iterator that provides the to-be
/// restricted variables in \em ascending order.
///
/// \param end Marks the end for `begin`.
///
/// \returns \f$ f|_{(i,v) \in xs : x_i = v} \f$
//////////////////////////////////////////////////////////////////////////////
template<typename ForwardIt>
__bdd bdd_restrict(const bdd& f, ForwardIt begin, ForwardIt end)
{ return bdd_restrict(f, make_generator(begin, end)); }

//////////////////////////////////////////////////////////////////////////////
/// \brief Restrict a subset of variables to constant values.
//////////////////////////////////////////////////////////////////////////////
template<typename ForwardIt>
__bdd bdd_restrict(const exec_policy &ep,
const bdd& f,
ForwardIt begin,
ForwardIt end)
{ return bdd_restrict(ep, f, make_generator(begin, end)); }

//////////////////////////////////////////////////////////////////////////////
/// \brief Existential quantification of a single variable.
Expand Down Expand Up @@ -1016,9 +1056,9 @@ namespace adiar
//////////////////////////////////////////////////////////////////////////////
/// \brief Evaluate a BDD according to an assignment to its variables.
///
/// \param f The BDD to evaluate
/// \param f The BDD to evaluate.
///
/// \param xs A list of tuples `(i,v)` in ascending order of `i`.
/// \param xs Assignments (i,v) to variables in (in ascending order).
///
/// \pre Assignment tuples in `xs` is in ascending order
///
Expand All @@ -1027,9 +1067,26 @@ namespace adiar
///
/// \throws invalid_argument If a level in the BDD does not exist in `xs`.
//////////////////////////////////////////////////////////////////////////////
// TODO v2.0 : Replace with `generator<pair<...>>`
bool bdd_eval(const bdd &f,
const shared_file<map_pair<bdd::label_type, boolean>> &xs);
bool bdd_eval(const bdd &f, const generator<pair<bdd::label_type, bool>> &xs);

//////////////////////////////////////////////////////////////////////////////
/// \brief Evaluate a BDD according to an assignment to its variables.
///
/// \param f The BDD to evaluate.
///
/// \param begin Single-pass forward iterator that provides the assignment in
/// \em ascending order.
///
/// \param end Marks the end for `begin`.
///
/// \throws out_of_range If traversal of the BDD leads to going beyond the end
/// of the content of `xs`.
///
/// \throws invalid_argument If a level in the BDD does not exist in `xs`.
//////////////////////////////////////////////////////////////////////////////
template<typename ForwardIt>
bool bdd_eval(const bdd &f, ForwardIt begin, ForwardIt end)
{ return bdd_eval(f, make_generator(begin, end)); }

//////////////////////////////////////////////////////////////////////////////
/// \brief Get the labels of the levels of the BDD.
Expand Down
2 changes: 1 addition & 1 deletion src/adiar/bdd/bdd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ namespace adiar

__bdd bdd_from(const exec_policy &ep, const zdd &A)
{
const shared_file<bdd::label_type> dom = domain_get();
const internal::shared_file<bdd::label_type> dom = domain_get();
internal::file_stream<domain_var> ds(dom);

return bdd_from(ep, A, make_generator(ds));
Expand Down
4 changes: 1 addition & 3 deletions src/adiar/bdd/count.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,7 @@ namespace adiar

uint64_t bdd_satcount(const exec_policy &ep, const bdd &f)
{
const bdd::label_type domain_size = domain_isset() ? domain_get()->size() : 0;
const bdd::label_type varcount = bdd_varcount(f);
return bdd_satcount(ep, f, std::max(domain_size, varcount));
return bdd_satcount(ep, f, std::max<bdd::label_type>(domain_size(), bdd_varcount(f)));
};

uint64_t bdd_satcount(const bdd &f)
Expand Down
Loading
Loading