Clingo
Loading...
Searching...
No Matches
iesolver.hh
1#pragma once
2
3#include <clingo/input/program.hh>
4
5namespace CppClingo::Input {
6
9
13struct IETerm {
15 friend auto operator<<(std::ostream &out, IETerm const &term) -> std::ostream &;
17 friend auto operator==(IETerm const &a, IETerm const &b) -> bool = default;
19 friend auto operator<=>(IETerm const &a, IETerm const &b) {
20 return std::tie(a.variable, a.coefficient) <=> std::tie(b.variable, b.coefficient);
21 }
22
27};
29using IETermVec = std::vector<IETerm>;
30
32void add_term(IETermVec &terms, IETerm term);
36auto simplify(IETermVec &terms) -> Number;
37
39struct IE {
41 friend auto operator<<(std::ostream &out, IE const &ie) -> std::ostream &;
42
47};
49using IEVec = std::vector<IE>;
50
53 public:
55 enum Type : uint8_t { Lower, Upper };
57 IEInterval() = default;
59 IEInterval(std::optional<Number> lower, std::optional<Number> upper)
60 : lower_{std::move(lower)}, upper_{std::move(upper)} {}
61
63 [[nodiscard]] auto has_value(Type type) const -> bool;
65 [[nodiscard]] auto value(Type type) const -> Number const &;
67 void set_value(Type type, Number bound);
72 auto refine(Type type, Number const &bound) -> bool;
73
78 auto refine(IEInterval const &bound) -> bool;
79
81 friend auto operator<<(std::ostream &out, IEInterval const &bound) -> std::ostream &;
83 friend auto operator==(IEInterval const &a, IEInterval const &b) -> bool = default;
85 friend auto operator!=(IEInterval const &a, IEInterval const &b) -> bool = default;
86
87 private:
89 std::optional<Number> lower_;
91 std::optional<Number> upper_;
92};
93
96
98class IESolver {
99 public:
101 IESolver(IESolver *parent = nullptr) : parent_{parent} {}
103 void add(IE ie);
107 [[nodiscard]] auto compute(Logger &log) -> bool;
109 [[nodiscard]] auto domain() const -> IEDomain const & { return domain_; }
111 [[nodiscard]] auto strengthens(String var) const -> bool;
112
113 private:
120 auto update_bound_(IETerm const &term, Number slack, size_t num_unbounded) -> bool;
124 auto update_slack_(IETerm const &term, Number &slack) -> bool;
125
127 IESolver *parent_;
129 IEDomain domain_;
131 IEVec ies_;
132};
133
135
136} // namespace CppClingo::Input
A interval of integers.
Definition iesolver.hh:52
friend auto operator<<(std::ostream &out, IEInterval const &bound) -> std::ostream &
Operator to print intervals.
friend auto operator==(IEInterval const &a, IEInterval const &b) -> bool=default
Equality compare two terms.
auto value(Type type) const -> Number const &
Get the value of the given bound.
auto refine(IEInterval const &bound) -> bool
Refine the interval with the given one.
auto has_value(Type type) const -> bool
Whether the given bound has a value.
void set_value(Type type, Number bound)
Set the value of the given bound.
IEInterval()=default
Create an unbounded interval.
auto refine(Type type, Number const &bound) -> bool
Refine the value of the given bound.
friend auto operator!=(IEInterval const &a, IEInterval const &b) -> bool=default
Inequality compare two terms.
IEInterval(std::optional< Number > lower, std::optional< Number > upper)
Create an interval with the given bounds.
Definition iesolver.hh:59
Type
Enum to indicate the lower and upper bound of the interval.
Definition iesolver.hh:55
A (partial) solver for inequalities.
Definition iesolver.hh:98
void add(IE ie)
Add the given inequality to the solver.
auto domain() const -> IEDomain const &
Get the domains of variables.
Definition iesolver.hh:109
IESolver(IESolver *parent=nullptr)
Construct an IESolver with an optional parent.
Definition iesolver.hh:101
auto compute(Logger &log) -> bool
Compute the bounds of variables in added inequalities.
auto strengthens(String var) const -> bool
Return true if the solver strengthens the domain of the given variable.
Simple logger to report message to stderr or via a callback.
Definition logger.hh:63
An arbitrary precision integer.
Definition number.hh:27
Reference to a string stored in a symbol store.
Definition symbol.hh:18
std::vector< IE > IEVec
A system of inequalities.
Definition iesolver.hh:49
std::vector< IETerm > IETermVec
A vector of terms representing a sum of terms.
Definition iesolver.hh:29
void add_term(IETermVec &terms, IETerm term)
Add a term to a sum of terms.
Util::ordered_map< String, IEInterval > IEDomain
A map from variables to intervals.
Definition iesolver.hh:95
auto simplify(IETermVec &terms) -> Number
Simplify the given sum of terms.
tsl::ordered_map< Key, T, Hash, KeyEqual, Allocator, ValueTypeContainer, IndexType > ordered_map
Alias for ordered maps.
Definition ordered_map.hh:16
A term of form coefficient times variable.
Definition iesolver.hh:13
friend auto operator<<(std::ostream &out, IETerm const &term) -> std::ostream &
Operator to print terms.
Number coefficient
The integer coefficient of the term.
Definition iesolver.hh:24
String variable
The variable of the term or the empty string if there is no variable.
Definition iesolver.hh:26
friend auto operator<=>(IETerm const &a, IETerm const &b)
Compare two terms.
Definition iesolver.hh:19
friend auto operator==(IETerm const &a, IETerm const &b) -> bool=default
Equality compare two terms.
An inequality of form terms >= bound.
Definition iesolver.hh:39
friend auto operator<<(std::ostream &out, IE const &ie) -> std::ostream &
Operator to print inequalities.
IETermVec terms
The terms.
Definition iesolver.hh:44
Number bound
The lower bound.
Definition iesolver.hh:46