-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmodel.cpp
More file actions
407 lines (355 loc) · 11.3 KB
/
Copy pathmodel.cpp
File metadata and controls
407 lines (355 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*
Copyright (C) 2018, Jianwen Li (lijwen2748@gmail.com), Iowa State University
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/*
Author: Jianwen Li
Update Date: October 17, 2017
Data Structure for models
*/
#include "model.h"
#include "utility.h"
#include <stdlib.h>
#include <iostream>
#include <assert.h>
#include <vector>
using namespace std;
namespace car{
Model::Model (aiger* aig, const bool verbose)
{
verbose_ = verbose;
//According to aiger format, inputs should be [1 ... num_inputs_]
//and latches should be [num_inputs+1 ... num_latches+num_inputs]]
num_inputs_ = aig->num_inputs;
num_latches_ = aig->num_latches;
num_ands_ = aig->num_ands;
num_constraints_ = aig->num_constraints;
num_outputs_ = aig->num_outputs;
//preserve two more ids for TRUE (max_id_ - 1) and FALSE (max_id_)
max_id_ = aig->maxvar+2;
true_ = max_id_ - 1;
false_ = max_id_;
collect_trues (aig);
set_constraints (aig);
set_outputs (aig);
set_init (aig);
create_next_map (aig);
create_clauses (aig);
}
void Model::collect_trues (const aiger* aig)
{
for (int i = 0; i < aig->num_ands; i ++)
{
aiger_and& aa = aig->ands[i];
//and gate is always an even number in aiger
assert (aa.lhs % 2 == 0);
if (is_true (aa.rhs0) && is_true (aa.rhs1))
trues_.insert (aa.lhs);
else if (is_false (aa.rhs0) || is_false (aa.rhs1))
trues_.insert (aa.lhs + 1);
}
}
void Model::create_next_map (const aiger* aig)
{
for (int i = 0; i < aig->num_latches; i ++)
{
int val = (int)aig->latches[i].lit;
//a latch should not be a negative number
assert (val % 2 == 0);
val = val / 2;
//make sure our assumption about latches is correct
assert (val == (num_inputs_ + 1 + i));
//pay attention to the special case when next_val = 0 or 1
if (is_false (aig->latches[i].next)) //FALSE
{
next_map_.insert (std::pair<int, int> (val, false_));
insert_to_reverse_next_map (false_, val);
}
else if (is_true (aig->latches[i].next)) //TRUE
{
next_map_.insert (std::pair<int, int> (val, true_));
insert_to_reverse_next_map (true_, val);
}
else
{
int next_val = (int) aig->latches[i].next;
next_val = (next_val % 2 == 0) ? (next_val/2) : -(next_val/2);
next_map_.insert (std::pair<int, int> (val, next_val));
insert_to_reverse_next_map (abs (next_val), (next_val > 0) ? val : -val);
}
}
}
void Model::insert_to_reverse_next_map (const int index, const int val)
{
reverseNextMap::iterator it = reverse_next_map_.find (index);
if (it == reverse_next_map_.end ())
{
vector<int> v;
v.push_back (val);
reverse_next_map_.insert (std::pair<int, vector<int> > (index, v));
}
else
(it->second).push_back (val);
}
void Model::create_clauses (const aiger* aig)
{
//contraints, outputs and latches gates are stored in order,
//as the need for start solver construction
hash_set<unsigned> exist_gates;
vector<unsigned> gates;
gates.resize (max_id_+1, 0);
//create clauses for constraints
collect_necessary_gates (aig, aig->constraints, aig->num_constraints, exist_gates, gates);
for (vector<unsigned>::iterator it = gates.begin (); it != gates.end (); it ++)
{
if (*it == 0) continue;
aiger_and* aa = aiger_is_and (const_cast<aiger*>(aig), *it);
assert (aa != NULL);
add_clauses_from_gate (aa);
}
set_outputs_start ();
//create clauses for outputs
gates.resize (max_id_+1, 0);
collect_necessary_gates (aig, aig->outputs, aig->num_outputs, exist_gates, gates);
for (vector<unsigned>::iterator it = gates.begin (); it != gates.end (); it ++)
{
if (*it == 0) continue;
aiger_and* aa = aiger_is_and (const_cast<aiger*>(aig), *it);
assert (aa != NULL);
add_clauses_from_gate (aa);
}
set_latches_start ();
//create clauses for latches
gates.resize (max_id_+1, 0);
collect_necessary_gates (aig, aig->latches, aig->num_latches, exist_gates, gates, true);
for (vector<unsigned>::iterator it = gates.begin (); it != gates.end (); it ++)
{
if (*it == 0) continue;
aiger_and* aa = aiger_is_and (const_cast<aiger*>(aig), *it);
assert (aa != NULL);
add_clauses_from_gate (aa);
}
//create clauses for true and false
cls_.push_back (clause (true_));
cls_.push_back (clause (-false_));
//cout<<"constraint size: "<<constraints_.size()<<endl;
for(int i=0;i<constraints_.size();i++){
cls_.push_back(clause(constraints_[i]));
//cout<<"constraint: "<<constraints_[i]<<endl;
}
}
void Model::collect_necessary_gates (const aiger* aig, const aiger_symbol* as, const int as_size,
hash_set<unsigned>& exist_gates, vector<unsigned>& gates, bool next)
{
for (int i = 0; i < as_size; i ++)
{
aiger_and* aa;
if (next)
aa = necessary_gate (as[i].next, aig);
else
{
aa = necessary_gate (as[i].lit, aig);
if (aa == NULL)
{
if (is_true (as[i].lit))
outputs_[i] = true_;
else if (is_false (as[i].lit))
outputs_[i] = false_;
}
}
recursively_add (aa, aig, exist_gates, gates);
}
}
aiger_and* Model::necessary_gate (const unsigned id, const aiger* aig)
{
if (!is_true (id) && !is_false (id))
return aiger_is_and (const_cast<aiger*> (aig), (id % 2 == 0) ? id : (id-1));
return NULL;
}
void Model::recursively_add (const aiger_and* aa, const aiger* aig, hash_set<unsigned>& exist_gates, vector<unsigned>& gates)
{
if (aa == NULL)
return;
if (exist_gates.find (aa->lhs) != exist_gates.end ())
return;
gates[aa->lhs/2] = aa->lhs;
exist_gates.insert (aa->lhs);
aiger_and* aa0 = necessary_gate (aa->rhs0, aig);
recursively_add (aa0, aig, exist_gates, gates);
aiger_and* aa1 = necessary_gate (aa->rhs1, aig);
recursively_add (aa1, aig, exist_gates, gates);
}
void Model::add_clauses_from_gate (const aiger_and* aa)
{
assert (aa != NULL);
assert (!is_true (aa->lhs) && !is_false (aa->lhs));
if (is_true (aa->rhs0))
{
cls_.push_back (clause (car_var (aa->lhs), -car_var (aa->rhs1)));
cls_.push_back (clause (-car_var (aa->lhs), car_var (aa->rhs1)));
}
else if (is_true (aa->rhs1))
{
cls_.push_back (clause (car_var (aa->lhs), -car_var (aa->rhs0)));
cls_.push_back (clause (-car_var (aa->lhs), car_var (aa->rhs0)));
}
else
{
cls_.push_back (clause (car_var (aa->lhs), -car_var (aa->rhs0), -car_var (aa->rhs1)));
cls_.push_back (clause (-car_var (aa->lhs), car_var (aa->rhs0)));
cls_.push_back (clause (-car_var (aa->lhs), car_var (aa->rhs1)));
}
}
void Model::set_init (const aiger* aig)
{
for (int i = 0; i < aig->num_latches; i ++)
{
if (aig->latches[i].reset == 0)
init_.push_back (-(num_inputs_+1+i));
else if (aig->latches[i].reset == 1)
init_.push_back (num_inputs_+1+i);
else
{
cout << "Error setting initial state!" << endl;
exit (0);
}
}
}
void Model::set_constraints (const aiger* aig)
{
for (int i = 0; i < aig->num_constraints; i ++)
{
int id = (int) aig->constraints[i].lit;
constraints_.push_back ((id%2 == 0) ? (id/2) : -(id/2));
}
}
void Model::set_outputs (const aiger* aig)
{
for (int i = 0; i < aig->num_outputs; i ++)
{
int id = (int) aig->outputs[i].lit;
outputs_.push_back ((id%2 == 0) ? (id/2) : -(id/2));
}
}
int Model::prime (const int id)
{
nextMap::iterator it = next_map_.find (abs (id));
if (it == next_map_.end ()){
//return 0; //not found
cout << "cannot find prime for " << id << endl;
exit (0);
}
return (id > 0 ? it->second : -(it->second));
}
std::vector<int> Model::previous (const int id)
{
vector<int> res;
reverseNextMap::iterator it = reverse_next_map_.find (abs (id));
if (it == reverse_next_map_.end ())
return res; //not found
res = it->second;
if (id < 0)
{
for (int i = 0; i < res.size (); i ++)
res[i] = -res[i];
}
return res;
}
void Model::shrink_to_previous_vars (Cube& uc, bool& constraint)
{
Cube tmp;
constraint = true;
for (int i = 0; i < uc.size (); i ++)
{
vector<int> ids = previous (abs (uc[i]));
if (ids.empty ())
{
constraint = false;
continue;
}
else
{
for (int j = 0; j < ids.size (); j ++)
tmp.push_back ((uc[i] > 0) ? ids[j] : (-ids[j]));
}
}
uc = tmp;
}
void Model::shrink_to_latch_vars (Cube& uc, bool& constraint)
{
Cube tmp;
constraint = true;
for (int i = 0; i < uc.size (); i ++)
{
if (latch_var (abs (uc[i])))
tmp.push_back (uc[i]);
else
constraint = false;
}
uc = tmp;
}
//propagate the model based on \@ assump, and the results are stored in \@ res
bool Model::propagate (const std::vector<int>& assump, std::vector<int>& res) {
res.resize (max_id_ + 1, 0);
for (int i = 0; i < assump.size (); i ++) {
res[abs(assump[i])] = assump[i];
}
for (int i = 0; i < cls_.size (); i ++) {
Clause& cl = cls_[i];
vector<int> tmp;
int j = 0;
for (; j < cl.size (); j ++) {
if (is_true (cl[j]) || res[abs (cl[j])] == cl[j]) {
tmp.clear ();
break;
}
if (is_false (cl[j]) || res[abs (cl[j])] == -cl[j])
continue;
tmp.push_back (cl[j]);
}
if (j >= cl.size ()) {
if (tmp.size () == 1) {
res[abs(tmp[0])] = tmp[0];
}
//propagate to false
else if (tmp.size () == 0)
return false;
}
}
return true;
}
void Model::print ()
{
cout << "-------------------Model information--------------------" << endl;
cout << endl << "number of clauses: " << cls_.size () << endl;
for (int i = 0; i < cls_.size (); i ++)
car::print (cls_[i]);
cout << endl << "next map: " << endl;
car::print (next_map_);
cout << endl << "reverse next map:" << endl;
car::print (reverse_next_map_);
cout << endl << "Initial state:" << endl;
car::print (init_);
cout << endl << "number of Inputs: " << num_inputs_ << endl;
cout << endl << "number of Latches: " << num_latches_ << endl;
cout << endl << "number of Outputs: " << num_outputs_ << endl;
car::print (outputs_);
cout << endl << "number of constraints: " << num_constraints_ << endl;
car::print (constraints_);
cout << endl << "Max id used: " << max_id_ << endl;
cout << endl << "outputs start index: " << outputs_start_ << endl;
cout << endl << "latches start index: " << latches_start_ << endl;
cout << endl << "number of TRUE variables: " << trues_.size () << endl;
car::print (trues_);
cout << endl << "-------------------End of Model information--------------------" << endl;
}
}