dlinear  0.0.1
Delta-complete SMT solver for linear programming
Loading...
Searching...
No Matches
Driver.h
1
10#pragma once
11
12#include <istream>
13#include <map>
14#include <string>
15#include <unordered_map>
16#include <utility>
17
18#include "dlinear/libs/libgmp.h"
19#include "dlinear/parser/Driver.h"
20#include "dlinear/parser/mps/BoundType.h"
21#include "dlinear/parser/mps/Sense.h"
22#include "dlinear/parser/mps/scanner.h"
23#include "dlinear/solver/Context.h"
24#include "dlinear/symbolic/symbolic.h"
25
26namespace dlinear::mps {
27
37class MpsDriver : public Driver {
38 public:
39 explicit MpsDriver(Context &context);
40
41 bool ParseStreamCore(std::istream &in) override;
42
47 static void error(const location &l, const std::string &m);
48
59 void ObjectiveSense(bool is_min);
60
71 void ObjectiveName(const std::string &row);
72
85 void AddRow(Sense sense, const std::string &row);
86
102 void AddColumn(const std::string &column, const std::string &row, mpq_class value);
103
121 void AddRhs(const std::string &rhs, const std::string &row, mpq_class value);
122
147 void AddRange(const std::string &rhs, const std::string &row, mpq_class value);
148
164 void AddBound(BoundType type, const std::string &bound, const std::string &column, mpq_class value);
165
181 void AddBound(BoundType type, const std::string &bound, const std::string &column);
182
188 void End();
189
191 [[nodiscard]] const std::string &problem_name() const { return problem_name_; }
193 std::string &m_problem_name() { return problem_name_; }
195 [[nodiscard]] bool strict_mps() const { return strict_mps_; }
200 void set_strict_mps(bool b) { strict_mps_ = b; }
202 [[nodiscard]] std::size_t n_assertions() const { return rhs_.size() + bounds_.size(); }
204 [[nodiscard]] bool is_min() const { return is_min_; }
206 [[nodiscard]] const std::string &obj_row() const { return obj_row_; }
208 [[nodiscard]] MpsScanner *scanner() { return scanner_; }
209
214 void ToSmt2(std::ostream &os) const;
215
216 private:
223 inline bool VerifyStrictRhs(const std::string &rhs);
224
231 inline bool VerifyStrictBound(const std::string &bound);
232
233 std::string problem_name_;
234 bool is_min_{true};
235 std::string obj_row_;
237 bool strict_mps_{false};
238
245 std::unordered_map<std::string, std::map<Expression, mpq_class>> rows_;
246 std::unordered_map<std::string, Sense> row_senses_;
247 std::unordered_map<std::string, Variable> columns_;
248 std::unordered_map<std::string, bool> skip_lower_bound_;
249 std::unordered_map<std::string, mpq_class> rhs_values_;
250
251 // TODO(TendTo): Could be optimized by using unordered_map.
252 std::map<std::string, Formula> rhs_;
253 std::map<std::string, Formula> bounds_;
254
255 std::string rhs_name_;
256 std::string bound_name_;
257};
258
259} // namespace dlinear::mps
Context class that holds the set of constraints and provide Assert/Push/Pop/CheckSat functionalities.
Definition Context.h:31
The Driver is the base class for all the parsers.
Definition Driver.h:26
const Context & context() const
Get read-only access to the enabled of the Driver.
Definition Driver.h:143
The MpsDriver class brings together all components.
Definition Driver.h:37
bool ParseStreamCore(std::istream &in) override
Parse the stream.
Definition Driver.cpp:18
void AddRow(Sense sense, const std::string &row)
Add a row to the problem.
Definition Driver.cpp:66
std::size_t n_assertions() const
Get read-only access to the number of assertions of the MpsDriver.
Definition Driver.h:202
void ObjectiveSense(bool is_min)
Set the objective sense of the problem after having encountered the OBJSENSE section.
Definition Driver.cpp:56
std::unordered_map< std::string, mpq_class > rhs_values_
The values of the hand side of the problem.
Definition Driver.h:249
static void error(const location &l, const std::string &m)
Error handling with associated line number.
Definition Driver.cpp:54
bool is_min_
True if the problem is a minimization problem.
Definition Driver.h:234
std::string bound_name_
The name of the first bound found. Used if strict_mps_ is true.
Definition Driver.h:256
bool is_min() const
Check whether the minimization is enabled.
Definition Driver.h:204
std::map< std::string, Formula > bounds_
Assertions built by combining the columns and the bounds.
Definition Driver.h:253
void ObjectiveName(const std::string &row)
Set the name of the objective row after having encountered the OBJNAME section.
Definition Driver.cpp:61
void ToSmt2(std::ostream &os) const
Print the problem in the smt2 format.
Definition Driver.cpp:234
std::string rhs_name_
The name of the first rhs found. Used if strict_mps_ is true.
Definition Driver.h:255
std::string obj_row_
The name of the objective row.
Definition Driver.h:235
bool strict_mps_
If true, the parser will check that all rhs, ranges and bounds have the same name.
Definition Driver.h:237
MpsScanner * scanner_
The scanner producing the tokens for the parser.
Definition Driver.h:236
bool strict_mps() const
Check whether the strict mps is enabled.
Definition Driver.h:195
std::map< std::string, Formula > rhs_
Assertions built by combining the rows and the rhs.
Definition Driver.h:252
void AddRange(const std::string &rhs, const std::string &row, mpq_class value)
Add a new row contraint based on the range.
Definition Driver.cpp:117
std::unordered_map< std::string, Sense > row_senses_
The sense of each row.
Definition Driver.h:246
void AddBound(BoundType type, const std::string &bound, const std::string &column, mpq_class value)
Add a bound to a variable (column).
Definition Driver.cpp:147
void set_strict_mps(bool b)
Set the strict mps mode.
Definition Driver.h:200
std::unordered_map< std::string, Variable > columns_
The columns of the problem. Contains the variables.
Definition Driver.h:247
bool VerifyStrictBound(const std::string &bound)
If strict_mps_ is true, keeps track of the name of the first bound found.
Definition Driver.cpp:30
void AddColumn(const std::string &column, const std::string &row, mpq_class value)
Add a column to the problem.
Definition Driver.cpp:75
std::string problem_name_
The name of the problem. Used to name the context.
Definition Driver.h:233
const std::string & problem_name() const
Get read-only access to the problem_name of the MpsDriver.
Definition Driver.h:191
std::unordered_map< std::string, std::map< Expression, mpq_class > > rows_
The rows of the problem.
Definition Driver.h:245
std::unordered_map< std::string, bool > skip_lower_bound_
True if there is no need to manually add the lb 0 <= V.
Definition Driver.h:248
void End()
Called when the parser has reached the ENDATA section.
Definition Driver.cpp:202
MpsScanner * scanner()
Get read-only access to the scanner of the MpsDriver.
Definition Driver.h:208
bool VerifyStrictRhs(const std::string &rhs)
If strict_mps_ is true, keeps track of the name of the first rhs found.
Definition Driver.cpp:42
void AddRhs(const std::string &rhs, const std::string &row, mpq_class value)
Add the right hand side of the row.
Definition Driver.cpp:89
std::string & m_problem_name()
Get read-write access to the problem_name of the MpsDriver.
Definition Driver.h:193
const std::string & obj_row() const
Get read-only access to the objective row name of the MpsDriver.
Definition Driver.h:206
MpsScanner is a derived class to add some extra function to the scanner class.
Definition scanner.h:40
Namespace for the MPS parser of the dlinear library.
Definition BoundType.cpp:13
Sense
Sense of a constraint row.
Definition Sense.h:23
BoundType
Bound type.
Definition BoundType.h:27