3#include <clingo/base.hh>
4#include <clingo/core.hh>
6#include <clingo/propagate.h>
63 using pointer = Detail::ArrowProxy<value_type>;
65 using iterator = Detail::RandomAccessIterator<Trail>;
80 return Detail::call<clingo_assignment_trail_at>(assignment_, index);
89 [[nodiscard]]
auto size() const ->
size_type {
return Detail::call<clingo_assignment_trail_size>(assignment_); }
106 return iterator{*
this, Detail::call<clingo_assignment_trail_begin>(assignment_,
level)};
114 return iterator{*
this, Detail::call<clingo_assignment_trail_end>(assignment_,
level)};
151 using pointer = Detail::ArrowProxy<value_type>;
153 using iterator = Detail::RandomAccessIterator<Assignment>;
159 [[nodiscard]]
auto thread_id() const ->
ProgramId {
return Detail::call<clingo_assignment_thread_id>(assignment_); }
167 [[nodiscard]]
auto size() const ->
size_type {
return Detail::call<clingo_assignment_size>(assignment_); }
174 return Detail::call<clingo_assignment_at>(assignment_,
size);
185 return Detail::call<clingo_assignment_decision>(assignment_,
level);
192 return Detail::call<clingo_assignment_decision_level>(assignment_);
199 return Detail::call<clingo_assignment_has_conflict>(assignment_);
206 return Detail::call<clingo_assignment_has_literal>(assignment_, lit);
214 return Detail::call<clingo_assignment_is_false>(assignment_, lit);
222 return Detail::call<clingo_assignment_is_fixed>(assignment_, lit);
238 return Detail::call<clingo_assignment_is_true>(assignment_, lit);
246 [[nodiscard]]
auto is_total() const ->
bool {
return Detail::call<clingo_assignment_is_total>(assignment_); }
253 return Detail::call<clingo_assignment_level>(assignment_, lit);
285 return Detail::call<clingo_assignment_root_level>(assignment_);
330 return Detail::call<clingo_propagate_control_add_literal>(ctl_, freeze);
342 return Detail::call<clingo_propagate_control_add_clause>(ctl_, literals.data(), literals.size(),
367 return Detail::call<clingo_propagate_control_add_weight_constraint>(ctl_, literal, literals.data(),
368 literals.size(), bound, type);
379 return add_clause(Detail::transform(literals, [](
auto const &lit) {
return -lit; }), flags);
397 return Detail::call<clingo_propagate_control_has_watch>(ctl_, literal);
414 [[nodiscard]]
auto propagate() const ->
bool {
return Detail::call<clingo_propagate_control_propagate>(ctl_); }
445 return Library{Detail::call<clingo_propagate_init_library>(init_),
true};
451 [[nodiscard]]
auto base() const ->
Base {
return Base{Detail::call<clingo_propagate_init_base>(init_)}; }
457 return static_cast<PropagatorCheckMode>(Detail::call<clingo_propagate_init_get_check_mode>(init_));
464 Detail::handle_error(
472 return Detail::call<clingo_propagate_init_number_of_threads>(init_);
479 return static_cast<PropagatorUndoMode>(Detail::call<clingo_propagate_init_get_undo_mode>(init_));
486 Detail::handle_error(
495 return Detail::call<clingo_propagate_init_solver_literal>(init_, literal);
557 do_propagate(assignment, ctl, changes);
586 virtual void do_attach([[maybe_unused]] Assignment assignment, [[maybe_unused]] PropagateControl ctl) {}
587 virtual void do_propagate([[maybe_unused]] Assignment assignment, [[maybe_unused]] PropagateControl ctl,
589 virtual void do_undo([[maybe_unused]] Assignment assignment, [[maybe_unused]]
ProgramLiteralSpan changes) {}
590 virtual void do_check([[maybe_unused]] Assignment assignment, [[maybe_unused]] PropagateControl ctl) {}
608 return do_decide(assignment, literal);
623 auto &self = *
static_cast<Propagator *
>(data);
625 self.init(Assignment{assignment}, PropagateInit{
init});
630 auto &self = *
static_cast<Propagator *
>(data);
632 self.attach(Assignment{assignment}, PropagateControl{control});
637 size_t size,
void *data) ->
bool {
638 auto &self = *
static_cast<Propagator *
>(data);
640 self.propagate(Assignment{assignment}, PropagateControl{control},
SolverLiteralSpan{changes, size});
645 auto &self = *
static_cast<Propagator *
>(data);
648 }
catch (std::exception
const &e) {
649 printf(
"panic: %s\n", e.what());
654 auto &self = *
static_cast<Propagator *
>(data);
656 self.check(Assignment{assignment}, PropagateControl{control});
661 [](
void *data) { std::ignore = std::unique_ptr<Propagator>(
static_cast<Propagator *
>(data)); },
667 c_propagator.propagate,
672 auto &self = *
static_cast<Propagator *
>(data);
675 *decision =
static_cast<Heuristic &
>(self).decide(Assignment{assignment}, fallback);
Represents a (partial) assignment of a particular solver.
Definition propagate.hh:140
value_type reference
The reference type, which simply refers to the value type.
Definition propagate.hh:149
auto has_conflict() const -> bool
Check if the assignment is conflicting.
Definition propagate.hh:198
SolverLiteral value_type
The value type, which is a solver literal.
Definition propagate.hh:143
auto contains(SolverLiteral lit) const -> bool
Check whether the assignment contains the given literal.
Definition propagate.hh:205
auto thread_id() const -> ProgramId
Get the id of the thread to which the assignment belongs.
Definition propagate.hh:159
auto is_free(SolverLiteral lit) const -> bool
Check if the truth value of the given literal is free.
Definition propagate.hh:229
auto begin() const -> iterator
Get an iterator to the beginning of the assignment.
Definition propagate.hh:296
auto trail() const -> Trail
Get the trail of the assignment.
Definition propagate.hh:291
auto size() const -> size_type
Get the size of the assignment.
Definition propagate.hh:167
auto decision(ProgramId level) const -> SolverLiteral
Get the decision literal for the given decision level.
Definition propagate.hh:184
auto is_fixed(SolverLiteral lit) const -> bool
Check if the truth value of the given literal is fixed.
Definition propagate.hh:221
auto value(SolverLiteral lit) const -> std::optional< bool >
Get the truth value of the given literal.
Definition propagate.hh:263
auto is_total() const -> bool
Check whether the assignment is total.
Definition propagate.hh:246
Assignment(clingo_assignment_t const *assignment)
Construct an assignment from the underlying C representation.
Definition propagate.hh:156
auto at(size_type size) const -> value_type
Get the literal at the given index in the assignment.
Definition propagate.hh:173
Detail::RandomAccessIterator< Assignment > iterator
The iterator type, which is a random access iterator over the assignment.
Definition propagate.hh:153
auto level(SolverLiteral lit) const -> ProgramId
Get the level on which the given literal was assigned.
Definition propagate.hh:252
std::ptrdiff_t difference_type
The difference type.
Definition propagate.hh:147
auto end() const -> iterator
Get an iterator to the end of the assignment.
Definition propagate.hh:301
auto decision_level() const -> ProgramId
Get the current decision level.
Definition propagate.hh:191
auto root_level() const -> ProgramId
Get the root level of the assignment.
Definition propagate.hh:284
auto is_true(SolverLiteral lit) const -> bool
Check if the given literal is true.
Definition propagate.hh:237
auto is_false(SolverLiteral lit) const -> bool
Check if the given literal is false.
Definition propagate.hh:213
std::size_t size_type
The size type.
Definition propagate.hh:145
auto operator[](size_type size) const -> value_type
Get the literal at the given index in the assignment.
Definition propagate.hh:178
Detail::ArrowProxy< value_type > pointer
The pointer type, which is a proxy to the value type.
Definition propagate.hh:151
A base that maps signatures to atom bases, and captures term and theory bases.
Definition base.hh:607
Interface for user-defined propagators with a decision heuristic.
Definition propagate.hh:594
Heuristic()=default
The default constructor.
auto decide(Assignment assignment, SolverLiteral literal) -> SolverLiteral
Callback to decide on the next literal to assign.
Definition propagate.hh:607
The main library class for managing global information and logging.
Definition core.hh:471
Class to control a user-defined propagator.
Definition propagate.hh:311
PropagateControl(clingo_propagate_init_t *init)
Constructor from the underlying C representation.
Definition propagate.hh:320
auto add_clause(SolverLiteralSpan literals, ClauseFlags flags=ClauseFlags::none) const -> bool
Add a clause to the solver.
Definition propagate.hh:341
auto add_clause(SolverLiteralList literals, ClauseFlags flags=ClauseFlags::none) const -> bool
Add a clause to the solver.
Definition propagate.hh:354
auto propagate() const -> bool
Propagate consequences of previously added clauses.
Definition propagate.hh:414
PropagateControl(clingo_propagate_control_t *ctl)
Constructor from the underlying C representation.
Definition propagate.hh:316
void remove_watch(SolverLiteral lit) const
Remove a watch for the given solver literal.
Definition propagate.hh:403
auto add_nogood(SolverLiteralSpan literals, ClauseFlags flags=ClauseFlags::none) const -> bool
Add a nogood to the solver.
Definition propagate.hh:378
void add_watch(SolverLiteral literal) const
Add a watch for the given solver literal.
Definition propagate.hh:388
auto add_literal(bool freeze=true) const -> SolverLiteral
Adds a literal to the problem or the active solver thread.
Definition propagate.hh:329
auto add_weight_constraint(SolverLiteral literal, WeightedLiteralSpan literals, Weight bound, WeightConstraintType type) const -> bool
Add a weight constraint to the solver.
Definition propagate.hh:365
auto has_watch(SolverLiteral literal) const -> bool
Check whether the given solver literal is watched in the current context.
Definition propagate.hh:396
Object to initialize a user-defined propagator before each solving step.
Definition propagate.hh:434
PropagateInit(clingo_propagate_init_t *init)
Constructor from the underlying C representation.
Definition propagate.hh:439
auto library() const -> Library
Get the library object associated with the propagator.
Definition propagate.hh:444
void freeze_literal(SolverLiteral literal) const
Freeze the given literal in the solver.
Definition propagate.hh:512
void add_minimize(SolverLiteral literal, Weight weight, Weight priority) const
Add a weighted literal to minimize to the solver.
Definition propagate.hh:503
auto check_mode() const -> PropagatorCheckMode
Get the check mode of the propagator.
Definition propagate.hh:456
void undo_mode(PropagatorUndoMode mode) const
Set the undo mode of the propagator.
Definition propagate.hh:485
auto solver_literal(ProgramLiteral literal) const -> SolverLiteral
Map a program literal to a solver literal.
Definition propagate.hh:494
auto undo_mode() const -> PropagatorUndoMode
Get the undo mode of the propagator.
Definition propagate.hh:478
auto base() const -> Base
Get the base object associated with the propagator.
Definition propagate.hh:451
void check_mode(PropagatorCheckMode mode)
Set the check mode of the propagator.
Definition propagate.hh:463
auto number_of_threads() const -> ProgramId
Get the number of threads used in subsequent solving.
Definition propagate.hh:471
Interface for user-defined propagators.
Definition propagate.hh:521
Propagator()=default
The default constructor.
void check(Assignment assignment, PropagateControl ctl)
Callback that is called on propagation fixpoints or total assignments.
Definition propagate.hh:582
Propagator(Propagator &&other)=delete
Disable copy and move operations.
void attach(Assignment assignment, PropagateControl ctl)
Callback to initialize/setup thread-specific data for the propagator.
Definition propagate.hh:543
auto operator=(Propagator &&other) -> Propagator &=delete
Disable copy and move operations.
virtual ~Propagator()=default
The destructor.
void propagate(Assignment assignment, PropagateControl ctl, ProgramLiteralSpan changes)
Callback to propagate consequences of the given literals.
Definition propagate.hh:556
void undo(Assignment assignment, ProgramLiteralSpan changes)
Called when the solver backtracks to a previous decision level.
Definition propagate.hh:570
void init(Assignment assignment, PropagateInit init)
Callback to initialize the propagator before each solving step.
Definition propagate.hh:536
A trail is a sequence of solver literals.
Definition propagate.hh:52
value_type reference
The reference type, which simply refers to the value type.
Definition propagate.hh:61
auto end(ProgramId level) const -> iterator
Get an iterator to the end of the given decision level.
Definition propagate.hh:113
Trail(clingo_assignment_t const *assignment)
Construct a trail from the underlying C representation.
Definition propagate.hh:72
Detail::RandomAccessIterator< Trail > iterator
The iterator type, which is a random access iterator over the trail.
Definition propagate.hh:65
auto size() const -> size_type
Get the size of the trail.
Definition propagate.hh:89
auto operator[](size_type index) const -> value_type
Get the literal at the given index in the trail.
Definition propagate.hh:84
auto level(ProgramId level) const
Get the subrange of the literals of the given decision level.
Definition propagate.hh:121
std::ptrdiff_t difference_type
The difference type.
Definition propagate.hh:59
Detail::ArrowProxy< value_type > pointer
The pointer type, which is a proxy to the value type.
Definition propagate.hh:63
auto begin() const -> iterator
Get an iterator to the beginning of the trail.
Definition propagate.hh:94
SolverLiteral value_type
The value type is a solver literal.
Definition propagate.hh:55
auto at(size_type index) const -> value_type
Get the literal at the given index in the trail.
Definition propagate.hh:79
auto begin(ProgramId level) const -> iterator
Get an iterator to the beginning of the given decision level.
Definition propagate.hh:105
std::size_t size_type
The size type.
Definition propagate.hh:57
auto end() const -> iterator
Get an iterator to the end of the trail.
Definition propagate.hh:99
int32_t clingo_literal_t
Signed integer type used for aspif and solver literals.
Definition core.h:73
CLINGO_VISIBILITY_DEFAULT bool clingo_propagate_control_add_watch(clingo_propagate_control_t *control, clingo_literal_t literal)
Add a watch for the solver literal in the given phase.
CLINGO_VISIBILITY_DEFAULT bool clingo_propagate_init_set_undo_mode(clingo_propagate_init_t *init, clingo_propagator_undo_mode_t mode)
Configure when to call the undo method of the propagator.
struct clingo_propagate_control clingo_propagate_control_t
This object can be used to add clauses and propagate literals while solving.
Definition propagate.h:242
CLINGO_VISIBILITY_DEFAULT bool clingo_propagate_control_remove_watch(clingo_propagate_control_t *control, clingo_literal_t literal)
Removes the watch (if any) for the given solver literal.
int clingo_weight_constraint_type_t
Corresponding type to clingo_weight_constraint_type_e.
Definition propagate.h:239
CLINGO_VISIBILITY_DEFAULT bool clingo_propagate_init_control(clingo_propagate_init_t const *init, clingo_propagate_control_t **control)
Get the underlying propagate control object.
CLINGO_VISIBILITY_DEFAULT bool clingo_propagate_init_add_minimize(clingo_propagate_init_t *init, clingo_literal_t solver_literal, clingo_weight_t weight, clingo_weight_t priority)
Add the given literal to minimize to the solver.
int clingo_propagator_undo_mode_t
Corresponding type to clingo_propagator_undo_mode_e.
Definition propagate.h:230
struct clingo_assignment clingo_assignment_t
Represents a (partial) assignment of a particular solver.
Definition propagate.h:46
struct clingo_propagate_init clingo_propagate_init_t
Object to initialize a user-defined propagator before each solving step.
Definition propagate.h:255
int clingo_propagator_check_mode_t
Corresponding type to clingo_propagator_check_mode_e.
Definition propagate.h:222
int clingo_truth_value_t
Corresponding type to clingo_truth_value_e.
Definition propagate.h:55
CLINGO_VISIBILITY_DEFAULT bool clingo_propagate_init_set_check_mode(clingo_propagate_init_t *init, clingo_propagator_check_mode_t mode)
Configure when to call the check method of the propagator.
CLINGO_VISIBILITY_DEFAULT bool clingo_propagate_init_freeze_literal(clingo_propagate_init_t *init, clingo_literal_t solver_literal)
Freeze the given solver literal.
int clingo_clause_type_t
Corresponding type to clingo_clause_type_e.
Definition propagate.h:374
@ clingo_propagator_check_mode_both
call clingo_propagator::check() on propagation fixpoints and total assignments
Definition propagate.h:218
@ clingo_propagator_check_mode_none
do not call clingo_propagator::check() at all
Definition propagate.h:215
@ clingo_propagator_check_mode_fixpoint
call clingo_propagator::check() on propagation fixpoints
Definition propagate.h:217
@ clingo_propagator_check_mode_total
call clingo_propagator::check() on total assignments
Definition propagate.h:216
@ clingo_truth_value_false
false
Definition propagate.h:52
@ clingo_truth_value_true
true
Definition propagate.h:51
@ clingo_truth_value_free
no truth value
Definition propagate.h:50
@ clingo_clause_type_learnt
clause is subject to the solvers deletion policy
Definition propagate.h:366
@ clingo_clause_type_volatile
like clingo_clause_type_learnt but the clause is deleted after a solving step
Definition propagate.h:368
@ clingo_clause_type_static
clause is not subject to the solvers deletion policy
Definition propagate.h:367
@ clingo_propagator_undo_mode_default
call clingo_propagator::undo() for non-empty change lists
Definition propagate.h:226
@ clingo_propagator_undo_mode_always
also call clingo_propagator::check() when check has been called
Definition propagate.h:227
@ clingo_weight_constraint_type_implication_right
the literal implies the weight constraint
Definition propagate.h:235
@ clingo_weight_constraint_type_implication_left
the weight constraint implies the literal
Definition propagate.h:234
@ clingo_weight_constraint_type_equivalence
the weight constraint is equivalent to the literal
Definition propagate.h:236
clingo_literal_t SolverLiteral
A solver literal.
Definition core.hh:399
std::initializer_list< SolverLiteral const > SolverLiteralList
A list of solver literals.
Definition core.hh:403
clingo_weight_t Weight
A weight used in sum aggregates and minimize constraints.
Definition core.hh:408
clingo_literal_t ProgramLiteral
A program literal.
Definition core.hh:392
std::span< SolverLiteral const > SolverLiteralSpan
A span of solver literals.
Definition core.hh:401
std::span< WeightedLiteral const > WeightedLiteralSpan
A span of weighted literals.
Definition core.hh:415
clingo_id_t ProgramId
A program id used for various kinds of indices.
Definition core.hh:382
std::span< ProgramLiteral const > ProgramLiteralSpan
A span of program literals.
Definition core.hh:394
@ level
Set the level of an atom.
@ init
Modify the initial VSIDS score of an atom.
WeightConstraintType
Enumeration of weight constraint types.
Definition propagate.hh:30
ClauseFlags
Enumeration of clause flags.
Definition propagate.hh:37
PropagatorCheckMode
Enumeration of propagator check modes.
Definition propagate.hh:16
PropagatorUndoMode
Enumeration of propagator undo modes.
Definition propagate.hh:24
@ equivalence
The weight constraint is equivalent to the literal.
Definition propagate.hh:33
@ implication_left
The weight constraint implies the literal.
Definition propagate.hh:31
@ implication_right
The literal implies the weight constraint.
Definition propagate.hh:32
@ lock
Exempt the clause from deletion.
Definition propagate.hh:39
@ tag
Delete the clause at the end of the current solving step.
Definition propagate.hh:40
@ none
The empty set of flags.
Definition propagate.hh:38
@ fixpoint
Call check on propagation fixpoints.
@ both
Call check on propagation fixpoints and total assignments.
@ total
Call check on total assignments.
@ default_
Call undo for non-empty change lists.
@ always
Also call undo for empty change lists.
#define CLINGO_ENABLE_BITSET_ENUM(E,...)
Opt-in macro for enabling bit operations for a given enum type.
Definition enum.hh:18
An instance of this struct has to be registered with a solver to implement a custom propagator.
Definition propagate.h:493