spot 2.16
Loading...
Searching...
No Matches
aiger.hh
1// -*- coding: utf-8 -*-
2// Copyright (C) by the Spot authors, see the AUTHORS file for details.
3//
4// This file is part of Spot, a model checking library.
5//
6// Spot is free software; you can redistribute it and/or modify it
7// under the terms of the GNU General Public License as published by
8// the Free Software Foundation; either version 3 of the License, or
9// (at your option) any later version.
10//
11// Spot is distributed in the hope that it will be useful, but WITHOUT
12// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14// License for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19#pragma once
20
21#include <iosfwd>
22#include <spot/misc/common.hh>
23#include <spot/misc/bddlt.hh>
24#include <spot/twa/fwd.hh>
25#include <spot/twa/bdddict.hh>
26#include <spot/tl/formula.hh>
27#include <spot/tl/apcollect.hh>
28
29#include <unordered_map>
30#include <vector>
31#include <set>
32#include <memory>
33#include <algorithm> // std::none_of
34#include <sstream>
35
36
37namespace spot
38{
39 // Forward for synthesis
40 struct mealy_like;
41
42 class aig;
43
46 typedef std::shared_ptr<aig> aig_ptr;
49 typedef std::shared_ptr<const aig> const_aig_ptr;
50
63 class SPOT_API aig
64 {
65 protected:
66 const unsigned num_inputs_;
67 const unsigned num_outputs_;
68 const unsigned num_latches_;
69 const std::vector<std::string> input_names_;
70 const std::vector<std::string> output_names_;
71 unsigned max_var_;
72
73 std::vector<unsigned> next_latches_;
74 std::vector<unsigned> outputs_;
75 std::vector<std::pair<unsigned, unsigned>> and_gates_;
77 // Cache the function computed by each variable as a bdd.
78 // Bidirectional map
79 std::unordered_map<unsigned, bdd> var2bdd_;
80 std::unordered_map<int, unsigned> bdd2var_;
81 // First anonymous var marking the beginning of variables used
82 // as latches
83 int l0_;
84
87
88 // For simulation
89 std::vector<bool> state_;
90
91 public:
92
98 using safe_point = std::pair<unsigned, unsigned>;
101 std::tuple<std::vector<std::pair<unsigned, unsigned>>,
102 std::vector<std::pair<unsigned, bdd>>,
103 std::vector<bdd>>;
104
109 aig(const std::vector<std::string>& inputs,
110 const std::vector<std::string>& outputs,
111 unsigned num_latches,
112 bdd_dict_ptr dict = make_bdd_dict());
113
115 aig(unsigned num_inputs, unsigned num_outputs,
116 unsigned num_latches, bdd_dict_ptr dict = make_bdd_dict());
117
118 ~aig()
119 {
120 dict_->unregister_all_my_variables(this);
121 }
122
123 protected:
125 void register_new_lit_(unsigned v, const bdd &b);
127 void register_latch_(unsigned i, const bdd& b);
129 void register_input_(unsigned i, const bdd& b);
131 void unregister_lit_(unsigned v);
132
135 void split_cond_(const bdd& b, char so_mode,
136 std::vector<bdd>& cond_parts);
137
139 bdd accum_common_(const bdd& b) const;
140
142 unsigned cube2var_(const bdd& b, const int use_split_off);
143
144 public:
145
154
163 bool do_stash = false);
168 void reapply_(safe_point sp, const safe_stash& ss);
169
171 unsigned num_outputs() const
172 {
173 return num_outputs_;
174 }
177 const std::vector<unsigned>& outputs() const
178 {
179 SPOT_ASSERT(std::none_of(outputs_.begin(), outputs_.end(),
180 [](unsigned o){return o == -1u; }));
181 return outputs_;
182 }
183
187 unsigned output(unsigned num) const
188 {
189 return outputs_[num];
190 }
191
193 const std::vector<std::string>& output_names() const
194 {
195 return output_names_;
196 }
197
199 unsigned num_inputs() const
200 {
201 return num_inputs_;
202 }
204 const std::vector<std::string>& input_names() const
205 {
206 return input_names_;
207 }
208
210 unsigned num_latches() const
211 {
212 return num_latches_;
213 }
217 const std::vector<unsigned>& next_latches() const
218 {
219 SPOT_ASSERT(std::none_of(next_latches_.begin(), next_latches_.end(),
220 [](unsigned o){return o == -1u; }));
221 return next_latches_;
222 };
223
225 unsigned num_gates() const
226 {
227 return and_gates_.size();
228 };
230 const std::vector<std::pair<unsigned, unsigned>>& gates() const
231 {
232 return and_gates_;
233 };
234
236 unsigned max_var() const
237 {
238 return max_var_;
239 };
240
242 unsigned input_var(unsigned i, bool neg = false) const
243 {
244 SPOT_ASSERT(i < num_inputs_);
245 return (1 + i) * 2 + neg;
246 }
248 bdd input_bdd(unsigned i, bool neg = false) const
249 {
250 return aigvar2bdd(input_var(i, neg));
251 }
252
254 unsigned latch_var(unsigned i, bool neg = false) const
255 {
256 SPOT_ASSERT(i < num_latches_);
257 return (1 + num_inputs_ + i) * 2 + neg;
258 }
260 bdd latch_bdd(unsigned i, bool neg = false) const
261 {
262 return aigvar2bdd(latch_var(i, neg));
263 }
264
266 unsigned gate_var(unsigned i, bool neg = false) const
267 {
268 SPOT_ASSERT(i < num_gates());
269 return (1 + num_inputs_ + num_latches_ + i) * 2 + neg;
270 }
272 bdd gate_bdd(unsigned i, bool neg = false) const
273 {
274 return aigvar2bdd(gate_var(i, neg));
275 }
276
279 bdd aigvar2bdd(unsigned v, bool neg = false) const
280 {
281 return neg ? bdd_not(var2bdd_.at(v)) : var2bdd_.at(v);
282 }
283
286 unsigned bdd2aigvar(const bdd& b) const
287 {
288 return bdd2var_.at(b.id());
289 }
290
292 unsigned bdd2INFvar(const bdd& b);
293
295 unsigned bdd2ISOPvar(const bdd& b, const int use_split_off = 0);
296
315 unsigned encode_bdd(const std::vector<bdd>& c_alt,
316 char method = 1, bool use_dual = false,
317 int use_split_off = 0);
318
320 unsigned encode_bdd(const bdd& b,
321 char method = 1, bool use_dual = false,
322 int use_split_off = 0);
323
325 void set_output(unsigned i, unsigned v);
326
328 void set_next_latch(unsigned i, unsigned v);
329
331 static constexpr unsigned aig_true() noexcept
332 {
333 return 1;
334 };
335
337 static constexpr unsigned aig_false() noexcept
338 {
339 return 0;
340 };
341
343 unsigned aig_not(unsigned v);
344
346 unsigned aig_and(unsigned v1, unsigned v2);
347
351 unsigned aig_and(std::vector<unsigned>& vs);
352
354 unsigned aig_or(unsigned v1, unsigned v2);
355
359 unsigned aig_or(std::vector<unsigned>& vs);
360
362 unsigned aig_pos(unsigned v);
363
370 void encode_all_bdds(const std::vector<bdd>& all_bdd);
371
379 static aig_ptr
380 parse_aag(const std::string& aig_file,
381 bdd_dict_ptr dict = make_bdd_dict());
382
384 static aig_ptr
385 parse_aag(const char* data,
386 const std::string& filename,
387 bdd_dict_ptr dict = make_bdd_dict());
388
390 static aig_ptr
391 parse_aag(std::istream& iss,
392 const std::string& filename,
393 bdd_dict_ptr dict = make_bdd_dict());
394
399 twa_graph_ptr as_automaton(bool keepsplit = false) const;
400
409 const std::vector<bool>& circ_state() const
410 {
411 SPOT_ASSERT(state_.size() == max_var_ + 2
412 && "State vector does not have the correct size.\n"
413 "Forgot to initialize?");
414 return state_;
415 }
416
418 bool circ_state_of(unsigned var) const
419 {
420 SPOT_ASSERT(var <= max_var_ + 1
421 && "Variable out of range");
422 return circ_state()[var];
423 }
424
427 void circ_init();
428
435 void circ_step(const std::vector<bool>& inputs);
436
437 };
438
462 SPOT_API aig_ptr
463 mealy_machine_to_aig(const const_twa_graph_ptr& m, const char* mode,
464 const std::string* terminating_signal = nullptr);
465 SPOT_API aig_ptr
466 mealy_machine_to_aig(const mealy_like& m, const char* mode,
467 const std::string* terminating_signal = nullptr);
469
485 SPOT_API aig_ptr
486 mealy_machine_to_aig(const twa_graph_ptr& m, const char *mode,
487 const std::vector<std::string>& ins,
488 const std::vector<std::string>& outs,
489 const realizability_simplifier* rs = nullptr,
490 const std::string* terminating_signal = nullptr);
491 SPOT_API aig_ptr
492 mealy_machine_to_aig(mealy_like& m, const char *mode,
493 const std::vector<std::string>& ins,
494 const std::vector<std::string>& outs,
495 const realizability_simplifier* rs = nullptr,
496 const std::string* terminating_signal = nullptr);
498
520 SPOT_API aig_ptr
521 mealy_machines_to_aig(const std::vector<const_twa_graph_ptr>& m_vec,
522 const char* mode,
523 const std::string* terminating_signal = nullptr);
524 SPOT_API aig_ptr
525 mealy_machines_to_aig(const std::vector<mealy_like>& m_vec,
526 const char* mode,
527 const std::string* terminating_signal = nullptr);
528 SPOT_API aig_ptr
529 mealy_machines_to_aig(const std::vector<const_twa_graph_ptr>& m_vec,
530 const char* mode,
531 const std::vector<std::string>& ins,
532 const std::vector<std::vector<std::string>>& outs,
533 const realizability_simplifier* rs = nullptr,
534 const std::string* terminating_signal = nullptr);
535 SPOT_API aig_ptr
536 mealy_machines_to_aig(const std::vector<twa_graph_ptr>& m_vec,
537 const char* mode,
538 const std::vector<std::string>& ins,
539 const std::vector<std::vector<std::string>>& outs,
540 const realizability_simplifier* rs = nullptr,
541 const std::string* terminating_signal = nullptr);
542 SPOT_API aig_ptr
543 mealy_machines_to_aig(const std::vector<mealy_like>& m_vec,
544 const char* mode,
545 const std::vector<std::string>& ins,
546 const std::vector<std::vector<std::string>>& outs,
547 const realizability_simplifier* rs = nullptr,
548 const std::string* terminating_signal = nullptr);
550
553 SPOT_API std::ostream&
554 print_aiger(std::ostream& os, const_aig_ptr circuit);
555
594 SPOT_API std::ostream&
595 print_aiger(std::ostream& os, const const_twa_graph_ptr& aut,
596 const char* mode,
597 const std::string* terminating_signal = nullptr);
598}
A class representing AIG circuits.
Definition aiger.hh:64
std::vector< std::pair< unsigned, unsigned > > and_gates_
AND gates.
Definition aiger.hh:75
bdd gate_bdd(unsigned i, bool neg=false) const
Get the bdd associated to the ith gate.
Definition aiger.hh:272
unsigned encode_bdd(const std::vector< bdd > &c_alt, char method=1, bool use_dual=false, int use_split_off=0)
Add a bdd to the circuit Assumes that all bdd's given in c_alt fulfill the same purpose,...
const unsigned num_latches_
Number of latches.
Definition aiger.hh:68
bdd aigvar2bdd(unsigned v, bool neg=false) const
Get the bdd associated to a variable.
Definition aiger.hh:279
bdd_dict_ptr dict_
BDD dictionary.
Definition aiger.hh:76
static aig_ptr parse_aag(const std::string &aig_file, bdd_dict_ptr dict=make_bdd_dict())
Create a circuit from an aag file with restricted syntax.
const std::vector< std::pair< unsigned, unsigned > > & gates() const
Access the underlying container.
Definition aiger.hh:230
twa_graph_ptr as_automaton(bool keepsplit=false) const
Transform the circuit onto an equivalent monitor.
void reapply_(safe_point sp, const safe_stash &ss)
Reapply to stored changes on top of a safe_point.
const unsigned num_inputs_
Number of inputs.
Definition aiger.hh:66
std::pair< unsigned, unsigned > safe_point
Mark the beginning of a test translation.
Definition aiger.hh:98
std::vector< unsigned > next_latches_
Next-state for each latch.
Definition aiger.hh:73
const std::vector< unsigned > & outputs() const
Get the variables associated to the outputs.
Definition aiger.hh:177
unsigned gate_var(unsigned i, bool neg=false) const
Get the variable associated to the ith gate.
Definition aiger.hh:266
static constexpr unsigned aig_false() noexcept
Return the literal for constant false (0).
Definition aiger.hh:337
const std::vector< unsigned > & next_latches() const
Get the variables associated to the state of the latches in the next iteration.
Definition aiger.hh:217
int l0_
First latch variable.
Definition aiger.hh:83
void unregister_lit_(unsigned v)
Remove a literal from both maps.
unsigned aig_and(unsigned v1, unsigned v2)
Compute AND of v1 and v2.
unsigned num_gates() const
Get the total number of and gates.
Definition aiger.hh:225
std::unordered_map< int, unsigned > bdd2var_
BDD to AIG variable.
Definition aiger.hh:80
static aig_ptr parse_aag(const char *data, const std::string &filename, bdd_dict_ptr dict=make_bdd_dict())
Parse an AAG circuit from a data buffer.
bdd all_ins_
Conjunction of all inputs.
Definition aiger.hh:85
unsigned output(unsigned num) const
Return the variable associated to output num.
Definition aiger.hh:187
safe_point get_safe_point_() const
Save the current state of the circuit.
std::tuple< std::vector< std::pair< unsigned, unsigned > >, std::vector< std::pair< unsigned, bdd > >, std::vector< bdd > > safe_stash
RAII helper for saving/restoring AIG state.
Definition aiger.hh:103
const std::vector< std::string > & output_names() const
Get the set of output names.
Definition aiger.hh:193
unsigned aig_pos(unsigned v)
Returns the positive form of the given variable.
unsigned bdd2aigvar(const bdd &b) const
Get the variable associated to a bdd.
Definition aiger.hh:286
unsigned num_inputs() const
Get the number of inputs.
Definition aiger.hh:199
void circ_init()
(Re)initialize the stepwise evaluation of the circuit. This sets all latches to 0 and clears the outp...
const std::vector< std::string > output_names_
Output signal names.
Definition aiger.hh:70
void register_latch_(unsigned i, const bdd &b)
Register a latch with a given BDD.
static constexpr unsigned aig_true() noexcept
Return the literal for constant true (1).
Definition aiger.hh:331
std::unordered_map< unsigned, bdd > var2bdd_
AIG variable to BDD.
Definition aiger.hh:79
bdd input_bdd(unsigned i, bool neg=false) const
Get the bdd associated to the ith input.
Definition aiger.hh:248
unsigned input_var(unsigned i, bool neg=false) const
Get the variable associated to the ith input.
Definition aiger.hh:242
unsigned encode_bdd(const bdd &b, char method=1, bool use_dual=false, int use_split_off=0)
Just like the vector version but with no alternatives given.
aig(unsigned num_inputs, unsigned num_outputs, unsigned num_latches, bdd_dict_ptr dict=make_bdd_dict())
Constructing the circuit with generic names.
unsigned aig_and(std::vector< unsigned > &vs)
Computes the AND of all vars.
const std::vector< std::string > & input_names() const
Get the set of input names.
Definition aiger.hh:204
const unsigned num_outputs_
Number of outputs.
Definition aiger.hh:67
unsigned num_outputs() const
Get the number of outputs.
Definition aiger.hh:171
aig(const std::vector< std::string > &inputs, const std::vector< std::string > &outputs, unsigned num_latches, bdd_dict_ptr dict=make_bdd_dict())
Constructing an "empty" aig, knowing only about the necessary inputs, outputs and latches....
unsigned cube2var_(const bdd &b, const int use_split_off)
Translate a cube into gates, using split-off optionally.
unsigned num_latches() const
Get the number of latches in the circuit.
Definition aiger.hh:210
const std::vector< bool > & circ_state() const
Gives access to the current state of the circuit.
Definition aiger.hh:409
bdd accum_common_(const bdd &b) const
Split-off common sub-expressions as cube.
void set_output(unsigned i, unsigned v)
Associate the ith output to the variable v.
unsigned bdd2INFvar(const bdd &b)
Add a bdd to the circuit using if-then-else normal form.
void set_next_latch(unsigned i, unsigned v)
Associate the ith latch state after update to the variable v.
unsigned aig_not(unsigned v)
Negate a variable.
safe_stash roll_back_(safe_point sp, bool do_stash=false)
roll_back to the saved point.
const std::vector< std::string > input_names_
Input signal names.
Definition aiger.hh:69
bdd all_latches_
Conjunction of all latches.
Definition aiger.hh:86
unsigned latch_var(unsigned i, bool neg=false) const
Get the variable associated to the ith latch.
Definition aiger.hh:254
void circ_step(const std::vector< bool > &inputs)
Performs the next discrete step of the circuit, based on the inputs.
std::vector< unsigned > outputs_
Output functions.
Definition aiger.hh:74
unsigned max_var_
Maximum variable index.
Definition aiger.hh:71
unsigned aig_or(unsigned v1, unsigned v2)
Computes the OR of v1 and v2.
std::vector< bool > state_
Current simulation state.
Definition aiger.hh:89
bdd latch_bdd(unsigned i, bool neg=false) const
Get the bdd associated to the ith latch.
Definition aiger.hh:260
unsigned aig_or(std::vector< unsigned > &vs)
Computes the or of all vars.
void encode_all_bdds(const std::vector< bdd > &all_bdd)
Instead of successively adding bdds to the circuit, one can also pass a vector of all bdds needed to ...
unsigned bdd2ISOPvar(const bdd &b, const int use_split_off=0)
Add a bdd to the circuit using isop normal form.
void register_input_(unsigned i, const bdd &b)
Register an input with a given BDD.
static aig_ptr parse_aag(std::istream &iss, const std::string &filename, bdd_dict_ptr dict=make_bdd_dict())
Parse an AAG circuit from a stream.
void register_new_lit_(unsigned v, const bdd &b)
Register a new literal in both maps.
bool circ_state_of(unsigned var) const
Access to the state of a specific variable.
Definition aiger.hh:418
void split_cond_(const bdd &b, char so_mode, std::vector< bdd > &cond_parts)
Internal function that split a bdd into a conjunction hoping to increase reusage of gates.
unsigned max_var() const
Maximal variable index currently appearing in the circuit.
Definition aiger.hh:236
Simplify a reactive specification, preserving realizability.
Definition apcollect.hh:161
LTL/PSL formula interface.
std::shared_ptr< aig > aig_ptr
Shared pointer to an aig.
Definition aiger.hh:46
aig_ptr mealy_machine_to_aig(const const_twa_graph_ptr &m, const char *mode, const std::string *terminating_signal=nullptr)
Convert a mealy (like) machine into an aig relying on the transformation described by mode.
std::shared_ptr< const aig > const_aig_ptr
Shared pointer to a const aig.
Definition aiger.hh:49
aig_ptr mealy_machines_to_aig(const std::vector< const_twa_graph_ptr > &m_vec, const char *mode, const std::string *terminating_signal=nullptr)
Convert multiple mealy machines into an aig relying on the transformation described by mode.
bdd_dict_ptr make_bdd_dict()
Create a new, empty bdd_dict wrapped in a shared pointer.
Definition bdddict.hh:308
std::shared_ptr< bdd_dict > bdd_dict_ptr
Shared pointer to a bdd_dict.
Definition bdddict.hh:304
std::ostream & print_aiger(std::ostream &os, const_aig_ptr circuit)
Print the aig to stream in AIGER format.
std::shared_ptr< twa_graph > twa_graph_ptr
Shared pointer to a mutable twa_graph.
Definition fwd.hh:44
std::shared_ptr< const twa_graph > const_twa_graph_ptr
Shared pointer to a const twa_graph.
Definition fwd.hh:41
Definition automata.hh:26
A struct that represents different types of mealy like objects.
Definition synthesis.hh:279

Please direct any question, comment, or bug report to the Spot mailing list at spot@lrde.epita.fr.
Generated on Fri Feb 27 2015 10:00:07 for spot by doxygen 1.9.8