carl  24.04
Computer ARithmetic Library
OPBImporter.h
Go to the documentation of this file.
1 #pragma once
2 
6 
7 #include <iostream>
8 #include <fstream>
9 #include <map>
10 #include <optional>
11 #include <tuple>
12 #include <vector>
13 
14 namespace carl::io {
15 
16 using OPBPolynomial = std::vector<std::pair<int,carl::Variable>>;
17 using OPBConstraint = std::tuple<OPBPolynomial, Relation, int>;
18 
19 struct OPBFile {
21  std::vector<OPBConstraint> constraints;
22 
23  OPBFile() = default;
24  explicit OPBFile(OPBPolynomial obj): objective(std::move(obj)) {}
25  OPBFile(OPBPolynomial obj, std::vector<OPBConstraint> cons): objective(std::move(obj)), constraints(std::move(cons)) {}
26 };
27 
28 std::optional<OPBFile> parseOPBFile(std::ifstream& in);
29 
30 template<typename Pol>
31 class OPBImporter {
32 private:
34  std::ifstream mIn;
35 
36  std::map<carl::Variable, carl::Variable> variableCache; // maps old int variables to bool
37 
38  carl::MultivariatePolynomial<Number> convert(const std::vector<std::pair<int,carl::Variable>>& poly) {
39  Pol lhs;
40  for (const auto& term: poly) {
41  auto it = variableCache.find(term.second);
42  if (it == variableCache.end()) {
43  // We haven't seen this variable, yet. Create a new map entry for it.
45  variableCache[term.second] = boolVar;
46  }
47 
48  const carl::Variable& booleanVariable = variableCache[term.second];
49  lhs += Pol(Number(term.first)) * Pol(booleanVariable);
50  }
51 
52  return lhs;
53  }
54 
55 public:
56  explicit OPBImporter(const std::string& filename):
57  mIn(filename)
58  {}
59 
60  std::optional<std::pair<Formula<Pol>,Pol>> parse() {
61  if (!mIn.is_open()) return std::nullopt;
62 
63  auto file = parseOPBFile(mIn);
64  if (!file) return std::nullopt;
65  Formulas<Pol> constraints;
66  for (const auto& cons: file->constraints) {
67  auto lhs = convert(std::get<0>(cons));
68  Relation rel = std::get<1>(cons);
69  Number rhs = std::get<2>(cons);
70  Constraint<Pol> pbc(lhs - Pol(rhs), rel);
71  constraints.emplace_back(std::move(pbc));
72  }
73  Formula<Pol> resC(FormulaType::AND, std::move(constraints));
74  Pol objective;
75  for (const auto& term: file->objective) {
76  objective += Number(term.first) * term.second;
77  }
78  return std::make_pair(std::move(resC), std::move(objective));
79  }
80 };
81 
82 }
A small wrapper that configures logging for carl.
MultivariatePolynomial< Rational > Pol
Definition: HornerTest.cpp:17
std::vector< Formula< Poly > > Formulas
Variable fresh_boolean_variable() noexcept
Definition: VariablePool.h:192
Relation
Definition: Relation.h:20
std::optional< OPBFile > parseOPBFile(std::ifstream &in)
Definition: OPBImporter.cpp:95
std::vector< std::pair< int, carl::Variable > > OPBPolynomial
Definition: OPBImporter.h:16
std::tuple< OPBPolynomial, Relation, int > OPBConstraint
Definition: OPBImporter.h:17
A Variable represents an algebraic variable that can be used throughout carl.
Definition: Variable.h:85
T type
A type associated with the type.
Definition: typetraits.h:77
Represent a polynomial (in)equality against zero.
Definition: Constraint.h:62
Represent an SMT formula, which can be an atom for some background theory or a boolean combination of...
Definition: Formula.h:47
OPBPolynomial objective
Definition: OPBImporter.h:20
std::vector< OPBConstraint > constraints
Definition: OPBImporter.h:21
OPBFile(OPBPolynomial obj)
Definition: OPBImporter.h:24
OPBFile(OPBPolynomial obj, std::vector< OPBConstraint > cons)
Definition: OPBImporter.h:25
std::ifstream mIn
Definition: OPBImporter.h:34
carl::MultivariatePolynomial< Number > convert(const std::vector< std::pair< int, carl::Variable >> &poly)
Definition: OPBImporter.h:38
typename UnderlyingNumberType< Pol >::type Number
Definition: OPBImporter.h:33
std::map< carl::Variable, carl::Variable > variableCache
Definition: OPBImporter.h:36
OPBImporter(const std::string &filename)
Definition: OPBImporter.h:56
std::optional< std::pair< Formula< Pol >, Pol > > parse()
Definition: OPBImporter.h:60