cutscc.cc 12.3 KB
Newer Older
Flix Abecassis's avatar
Flix Abecassis committed
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
// Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009 Laboratoire
// d'Informatique de Paris 6 (LIP6), dpartement Systmes Rpartis
// Coopratifs (SRC), Universit Pierre et Marie Curie.
//
// This file is part of Spot, a model checking library.
//
// Spot 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 2 of the License, or
// (at your option) any later version.
//
// Spot 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 Spot; see the file COPYING.  If not, write to the Free
// Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.

#include <iostream>
#include <algorithm>
#include <set>
#include <fstream>
#include <sstream>
#include <string>
#include <queue>
#include <limits>
#include <math.h>
#include <sys/time.h>
#include <stdio.h>
#include "tgbaalgos/scc.hh"
#include "tgba/tgbaexplicit.hh"
#include "cutscc.hh"

namespace spot
{
  tgba* cut_scc(const tgba* a, const scc_map& m,
		const std::set<unsigned>* s)
  {
42
    tgba_explicit_string* sub_a = new tgba_explicit_string(a->get_dict());
Flix Abecassis's avatar
Flix Abecassis committed
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
408
409
410
411
412
413
414
415
416
417
418
419
420
    state* cur = a->get_init_state();
    std::queue<state*> tovisit;
    typedef Sgi::hash_set<const state*,
      state_ptr_hash, state_ptr_equal> hash_type;
    // Setup
    hash_type seen;
    unsigned scc_number;
    std::string cur_format = a->format_state(cur);
    std::set<unsigned>::iterator it;
    // Check if we have at least one accepting SCC.
    for (it = s->begin(); it != s->end() && !m.accepting(*it); it++)
      continue;
    assert(it != s->end());
    tovisit.push(cur);
    seen.insert(cur);
    sub_a->add_state(cur_format);
    sub_a->copy_acceptance_conditions_of(a);
    // If the initial is not part of one of the desired SCC, exit
    assert(s->find(m.scc_of_state(cur)) != s->end());

    // Perform BFS to visit each state.
    while (!tovisit.empty())
    {
      cur = tovisit.front();
      tovisit.pop();
      tgba_succ_iterator* sit = a->succ_iter(cur);
      for (sit->first(); !sit->done(); sit->next())
      {
	cur_format = a->format_state(cur);
	state* dst = sit->current_state();
	std::string dst_format = a->format_state(dst);
	scc_number= m.scc_of_state(dst);
	// Is the successor included in one of the desired SCC ?
	if (s->find(scc_number) != s->end())
	{
	  if (seen.find(dst) == seen.end())
	  {
	    tovisit.push(dst);
	    seen.insert(dst); // has_state?
	  }
	  else
	  {
	    delete dst;
	  }
	  tgba_explicit::transition* t =
	    sub_a->create_transition(cur_format, dst_format);
	  sub_a->add_conditions(t, sit->current_condition());
	  sub_a->
	    add_acceptance_conditions(t,
				      sit->current_acceptance_conditions());
	}
	else
	{
	  delete dst;
	}
      }
      delete sit;
    }

    hash_type::iterator it2;
    // Free visited states.
    for (it2 = seen.begin(); it2 != seen.end(); it2++)
    {
      delete *it2;
    }
    return sub_a;
  }

  void print_set(const sccs_set* s)
  {
    std::cout << "set : ";
    std::set<unsigned>::iterator vit;
    for (vit = s->sccs.begin(); vit != s->sccs.end(); ++vit)
      std::cout << *vit << " ";
    std::cout << std::endl;
  }

  unsigned set_distance(const sccs_set* s1,
			const sccs_set* s2,
			const std::vector<unsigned>& scc_sizes)
  {
    // Compute the distance between two sets.
    // Formula is : distance = size(s1) + size(s2) - size(s1 inter s2)
    std::set<unsigned>::iterator it;
    std::set<unsigned> result;
    unsigned inter_sum = 0;
    std::set_intersection(s1->sccs.begin(), s1->sccs.end(),
			  s2->sccs.begin(), s2->sccs.end(),
			  std::inserter(result, result.begin()));
    for (it = result.begin(); it != result.end(); ++it)
      inter_sum += scc_sizes[*it];
    return s1->size + s2->size - 2*inter_sum;
  }

  sccs_set* set_union(sccs_set* s1,
		      sccs_set* s2,
		      const std::vector<unsigned>& scc_sizes)
  {
    // Perform the union of two sets.
    sccs_set* result = new sccs_set;
    set_union(s1->sccs.begin(), s1->sccs.end(),
	      s2->sccs.begin(), s2->sccs.end(),
	      std::inserter(result->sccs, result->sccs.begin()));
    result->size = 0;
    std::set<unsigned>::iterator it;
    for (it = result->sccs.begin(); it != result->sccs.end(); ++it)
      result->size += scc_sizes[*it];
    delete s1;
    return result;
  }

  struct recurse_data
  {
    std::set<unsigned> seen;
    std::vector<std::vector<sccs_set* > >* rec_paths;
  };

  void find_paths_sub(unsigned init_scc,
		      const scc_map& m,
		      recurse_data& d,
		      const std::vector<unsigned>& scc_sizes)
  {
    // Find all the paths from the initial states to an accepting SCC
    // We need two stacks, one to track the current state, the other to track
    // the current iterator of this state.
    std::stack<scc_map::succ_type::const_iterator> it_stack;
    std::stack<unsigned> scc_stack;
    std::vector<const scc_map::succ_type*> scc_succ;
    unsigned scc_count = m.scc_count();
    scc_succ.reserve(scc_count);
    d.seen.insert(init_scc);
    unsigned i;
    for (i = 0; i < scc_count; ++i)
      scc_succ.push_back(&(m.succ(i)));
    // Setup the two stacks with the initial SCC.
    scc_stack.push(init_scc);
    it_stack.push(scc_succ[init_scc]->begin());
    while (!scc_stack.empty())
    {
      unsigned cur_scc = scc_stack.top();
      scc_stack.pop();
      d.seen.insert(cur_scc);
      scc_map::succ_type::const_iterator it;
      // Find the next unvisited successor.
      for (it = it_stack.top(); it != scc_succ[cur_scc]->end()
	     && d.seen.find(it->first) != d.seen.end(); ++it)
	continue;
      it_stack.pop();
      // If there are no successors and if the SCC is not accepting, this is
      // an useless path. Throw it away.
      if (scc_succ[cur_scc]->begin() == scc_succ[cur_scc]->end()
	  && !m.accepting(cur_scc))
	continue;
      std::vector<std::vector<sccs_set* > >* rec_paths = d.rec_paths;
      // Is there a successor to process ?
      if (it != scc_succ[cur_scc]->end())
      {
	// Yes, add it to the stack for later processing.
	unsigned dst = it->first;
	scc_stack.push(cur_scc);
	++it;
	it_stack.push(it);
	if (d.seen.find(dst) == d.seen.end())
	{
	  scc_stack.push(dst);
	  it_stack.push(scc_succ[dst]->begin());
	}
      }
      else
      {
	// No, all successors have been processed, update the current SCC.
	for (it = scc_succ[cur_scc]->begin();
	     it != scc_succ[cur_scc]->end(); ++it)
	{
	  unsigned dst = it->first;
	  std::vector<sccs_set*>::iterator lit;
	  // Extend all the reachable paths by adding the current SCC.
	  for (lit = (*rec_paths)[dst].begin();
	       lit != (*rec_paths)[dst].end(); ++lit)
	  {
	    sccs_set* path = new sccs_set;
	    path->sccs = (*lit)->sccs;
	    path->size = (*lit)->size + scc_sizes[cur_scc];
	    path->sccs.insert(path->sccs.begin(), cur_scc);
	    (*rec_paths)[cur_scc].push_back(path);
	  }
	}
	bool has_succ = false;
	for (it = scc_succ[cur_scc]->begin();
	     it != scc_succ[cur_scc]->end() && !has_succ; ++it)
	{
	  has_succ = !(*rec_paths)[it->first].empty();
	}
	// Create a new path iff the SCC is accepting and not included
	// in another path.
	if (m.accepting(cur_scc) && !has_succ)
	{
	  sccs_set* path = new sccs_set;
	  path->size = scc_sizes[cur_scc];
	  path->sccs.insert(path->sccs.begin(), cur_scc);
	  (*rec_paths)[cur_scc].push_back(path);
	}
      }

    }
    return;
  }

  std::vector<std::vector<sccs_set* > >* find_paths(tgba* a, const scc_map& m)
  {
    unsigned scc_count = m.scc_count();
    unsigned i;
    recurse_data d;
    d.rec_paths = new std::vector<std::vector<sccs_set* > >;
    for (i = 0; i < scc_count; i++)
    {
      std::vector<sccs_set*> list_set;
      d.rec_paths->push_back(list_set);
    }
    // We use a vector to recall the size of all SCCs.
    std::vector<unsigned> scc_sizes(scc_count, 0);
    for (i = 0; i < scc_count; ++i)
      scc_sizes[i] = m.states_of(i).size();
    state* initial_state = a->get_init_state();
    unsigned init = m.scc_of_state(initial_state);
    delete initial_state;
    // Find all interesting pathes in our automaton.
    find_paths_sub(init, m, d, scc_sizes);

    return d.rec_paths;
  }

  std::list<tgba*> split_tgba(tgba* a, const scc_map& m,
			      unsigned split_number)
  {
    // Main function to split an automaton tgba in split_number sub automata.
    unsigned i;
    unsigned scc_count = m.scc_count();
    unsigned j;
    std::vector<std::vector<sccs_set* > >* rec_paths = find_paths(a, m);
    state* initial_state = a->get_init_state();
    unsigned init = m.scc_of_state(initial_state);
    delete initial_state;
    std::vector<sccs_set*>* final_sets =&(*rec_paths)[init];
    if (rec_paths->empty())
    {
      std::list<tgba*> empty;
      return empty;
    }

    unsigned paths_count = final_sets->size();
    std::vector< std::vector<unsigned> > dist;
    for (i = 0; i < paths_count; ++i)
    {
      std::vector<unsigned> dist_sub(i, 0);
      dist.push_back(dist_sub);
    }

    // We use a vector to recall the size of all SCCs.
    std::vector<unsigned> scc_sizes(scc_count, 0);
    for (i = 0; i < scc_count; ++i)
      scc_sizes[i] = m.states_of(i).size();

    // Compute the distance between all pairs of pathes.
    for (i = 0; i < paths_count; ++i)
      for (j = 0; j < i; ++j)
      {
	dist[i][j] = set_distance((*final_sets)[i],
				  (*final_sets)[j], scc_sizes);
      }

    std::vector<bool> is_valid(paths_count, true);
    unsigned remaining_paths = paths_count;
    // While the number of subsets is strictly superior to split_number,
    // merge the two sets with the lowest distance.
    while (remaining_paths > split_number)
    {
      --remaining_paths;
      unsigned min_i = 1;
      unsigned min_j = 0;
      // Initialize with max value.
      unsigned min = (unsigned)(-1);
      // Find the two sets with the lowest distance.
      for (i = 0; i < paths_count; ++i)
	for (j = 0; j < i; ++j)
	  if (is_valid[i] && is_valid[j])
	  {
	    if (dist[i][j] < min)
	    {
	      min_i = i;
	      min_j = j;
	      min = dist[min_i][min_j];
	    }
	  }

      // Merge these sets.
      (*final_sets)[min_i] = set_union((*final_sets)[min_i],
				       (*final_sets)[min_j],
				       scc_sizes);
      // The second set is now unused.
      is_valid[min_j] = false;

      // Update the distances with other sets.
      for (j = 0; j < min_i; ++j)
	if (is_valid[min_i] && is_valid[j])
	  dist[min_i][j] = set_distance((*final_sets)[min_i],
					(*final_sets)[j], scc_sizes);
      for (i = min_i + 1; i < dist.size(); ++i)
	if (is_valid[i] && is_valid[min_i])
	  dist[i][min_i] = set_distance((*final_sets)[min_i],
					(*final_sets)[i], scc_sizes);
    }
    std::list<tgba*> result;
    // Final sets.
    for (i = 0; i < final_sets->size(); ++i)
      if (is_valid[i] == true)
      {
	//print_set((*final_sets)[i]);
	result.push_back(cut_scc(a, m, &(*final_sets)[i]->sccs));
      }

    // Free everything.
    for (i = 0; i < rec_paths->size(); ++i)
      for (j = 0; j < (*rec_paths)[i].size(); ++j)
      {
	delete (*rec_paths)[i][j];
      }
    delete rec_paths;
    return result;
  }

  unsigned max_spanning_paths(std::vector<sccs_set* >* paths, scc_map& m)
  {
    unsigned scc_count = m.scc_count();
    std::vector<bool> sccs_marked (scc_count, false);
    std::vector<bool> paths_marked (paths->size(), false);
    bool done = false;
    unsigned iter_count = 0;
    while (!done)
    {
      unsigned max = 0;
      unsigned max_index = 0;
      unsigned i;
      for (i = 0; i < paths->size(); ++i)
      {
	if (paths_marked[i])
	  continue;
	unsigned unmarked_sccs = 0;
	std::set<unsigned>* cur_path = &(*paths)[i]->sccs;
	std::set<unsigned>::iterator it;
	for (it = cur_path->begin(); it != cur_path->end(); ++it)
	{
	  if (!sccs_marked[*it])
	    ++unmarked_sccs;
	}
	if (unmarked_sccs > max)
	{
	  max = unmarked_sccs;
	  max_index = i;
	}
      }
      if (max == 0)
      {
	done = true;
	continue;
      }
      ++iter_count;
      paths_marked[max_index] = true;
      std::set<unsigned>* cur_path = &(*paths)[max_index]->sccs;
      std::set<unsigned>::iterator it;
      for (it = cur_path->begin(); it != cur_path->end(); ++it)
      {
	sccs_marked[*it] = true;
      }
    }
    return iter_count;
  }
}