SymbolicModel.hh 8.62 KB
Newer Older
Matthias Rungger's avatar
Matthias Rungger 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
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
/*
 * SymbolicModel.hh
 *
 *  created on: 09.10.2015
 *      author: rungger
 */

#ifndef SYMBOLICMODEL_HH_
#define SYMBOLICMODEL_HH_

#include <iostream>

#include "cuddObj.hh"
#include "dddmp.h"

#include "SymbolicSet.hh"


namespace scots {
/*
 * class: SymbolicModel
 *
 * base class for <SymbolicModelGrowthBound>
 * 
 * it stores the bdd of the transition relation and its bdd variable information
 * it provides an iterator to loop over all elements in the stateSpace and inputSpace
 *
 * the actual computation of the transition relation is computs in the derived
 * classes
 */
class SymbolicModel {
protected:
  /* var: ddmgr_ */
  Cudd *ddmgr_;
  /* var: stateSpace_ */
  SymbolicSet *stateSpace_;
  /* var: inputSpace_ */
  SymbolicSet *inputSpace_;
  /* var: nssVars_ 
   * number of state space bdd variables (= number of pre/post variables)  */
  size_t nssVars_;
  /* var: preVars_ 
   * array of indices of the state space pre bdd variables  */
  size_t* preVars_;
  /* var: postVars_ 
   * array of indices of the state space post bdd variables  */
  size_t* postVars_;
  /* var: nisVars_ 
   * number of input space bdd variables */
  size_t nisVars_;
  /* var: inpVars_ 
   * array of indices of the input space bdd variables  */
  size_t* inpVars_;
  /* var: iterator_ 
   * CuddMintermIterator to iterate over all elements in stateSpace_ and
   * inputSpace_ */
  CuddMintermIterator *iterator_;
  BDD it;
  /* var: transitionRelation_ 
   * the bdd representation of the transition relation  X x U x X
   * the bdd variables of the transition relation are 
   * given by preVars_ x inpVars_ x postVars_ */
  BDD transitionRelation_;
  friend class FixedPoint;
public:
  SymbolicModel(SymbolicSet *stateSpace, SymbolicSet *inputSpace): stateSpace_(stateSpace), inputSpace_(inputSpace) {
    if(stateSpace_->ddmgr_!=inputSpace_->ddmgr_) {
      std::ostringstream os;
      os << "Error: scots::SymbolicModel: stateSpace and inputSpace need to have the same dd manager.";
      throw std::invalid_argument(os.str().c_str());
    }
    ddmgr_=stateSpace_->ddmgr_;
    nssVars_=0;
    for(size_t i=0; i<stateSpace_->dim_; i++) 
      for(size_t j=0; j<stateSpace_->nofBddVars_[i]; j++) 
       nssVars_++; 
    nisVars_=0;
    for(size_t i=0; i<inputSpace_->dim_; i++) 
      for(size_t j=0; j<inputSpace_->nofBddVars_[i]; j++) 
       nisVars_++; 

    /* set the preVars_ to the variable indices of stateSpace_ 
     * and get new indices for the postVars_ */
    preVars_ = new size_t[nssVars_];
    postVars_ = new size_t[nssVars_];
    for(size_t k=0, i=0; i<stateSpace_->dim_; i++) {
      for(size_t j=0; j<stateSpace_->nofBddVars_[i]; k++, j++) {
       preVars_[k]=stateSpace_->indBddVars_[i][j];
       BDD t=stateSpace_->ddmgr_->bddVar();
       postVars_[k]=t.NodeReadIndex();
      }
    }
    inpVars_ = new size_t[nisVars_];
    for(size_t k=0, i=0; i<inputSpace_->dim_; i++) {
      for(size_t j=0; j<inputSpace_->nofBddVars_[i]; k++, j++) {
       inpVars_[k]=inputSpace_->indBddVars_[i][j];
      }
    }
    /* initialize the transition relation */
    transitionRelation_=stateSpace_->symbolicSet_*inputSpace_->symbolicSet_;
    it=transitionRelation_;
  };

  ~SymbolicModel(void) {
    delete[] preVars_;
    delete[] postVars_;
    delete[] inpVars_;
  };
  /* function:  getSize 
   * get the number of elements in the transition relation */
  inline double getSize(void) {
    return transitionRelation_.CountMinterm(2*nssVars_+nisVars_);
  };
Matthias Rungger's avatar
Matthias Rungger committed
114
115
116
117
118
  /* function:  getTransitionRelation 
   * get the BDD which represents the transition relation */
  inline BDD getTransitionRelation(void) {
    return transitionRelation_;
  }; 
Matthias Rungger's avatar
Matthias Rungger committed
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
  /* iterator methods */
  /* function: begin
   * initilize the iterator and compute the first element */
  inline void begin(void) {
    size_t nvars_=nssVars_+nisVars_;
    std::vector<size_t> ivars_;
    ivars_.reserve(nvars_);
    for(size_t i=0; i<nssVars_; i++) 
      ivars_.push_back(preVars_[i]);
    for(size_t i=0; i<nisVars_; i++) 
      ivars_.push_back(inpVars_[i]);
    /* set up iterator */
    iterator_ = new CuddMintermIterator(it,ivars_,nvars_);
  }
  /* function: next
   * compute the next minterm */
  inline void next(void) {
    ++(*iterator_);
  }
  /* function: progress
   * print progess of iteratin in percent */
  inline void progress(void) const {
    iterator_->printProgress();
  }  /* function: done
   * changes to one if iterator reached the last element */
  inline int done(void) {
    if(iterator_->done()) {
      delete iterator_;
      iterator_=NULL;
      return 1;
    } else 
    return 0;
  }
  /* function: currentMinterm
   * returns the pointer to the current minterm in the iteration */
  inline const int* currentMinterm(void) const {
    if (iterator_)
      return iterator_->currentMinterm();
    else
      return NULL;
  }
Matthias Rungger's avatar
Matthias Rungger committed
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
  /* function:  writeToFile
   * stores the bdd of transition function as a <SymbolicSet> with all information to file*/
  void writeToFile(const char *filename) {
    /* produce string with parameters to be written into file */
    std::stringstream dim;
    std::stringstream z;
    std::stringstream eta;
    std::stringstream first;
    std::stringstream last;
    std::stringstream nofGridPoints;
    std::stringstream indBddVars;

    size_t dim_ = 2*stateSpace_->dim_+inputSpace_->dim_;

    dim <<"#scots dimension: " << dim_ << std::endl;
    z << "#scots measurement error bound: ";
    eta << "#scots grid parameter eta: ";
    first << "#scots coordinate of first grid point: ";
    last << "#scots coordinate of last grid point: ";
    nofGridPoints << "#scots number of grid points (per dim): ";
    indBddVars << "#scots index of bdd variables: ";

    /* write information of the pre state space */
    for(size_t i=0; i<stateSpace_->dim_; i++) {
      z << stateSpace_->z_[i] << " ";
      eta << stateSpace_->eta_[i] << " ";
      first << stateSpace_->firstGridPoint_[i] << " ";
      last << stateSpace_->lastGridPoint_[i] << " ";
      nofGridPoints << stateSpace_->nofGridPoints_[i] << " ";
    }
    for(size_t j=0; j<nssVars_; j++) 
      indBddVars << preVars_[j] << " ";

    /* write information of the input space */
    for(size_t i=0; i<inputSpace_->dim_; i++) {
      z << inputSpace_->z_[i] << " ";
      eta << inputSpace_->eta_[i] << " ";
      first << inputSpace_->firstGridPoint_[i] << " ";
      last << inputSpace_->lastGridPoint_[i] << " ";
      nofGridPoints << inputSpace_->nofGridPoints_[i] << " ";
    }
    for(size_t j=0; j<nisVars_; j++) 
      indBddVars << inpVars_[j] << " ";

    /* write information of the post state space */
    for(size_t i=0; i<stateSpace_->dim_; i++) {
      z << stateSpace_->z_[i] << " ";
      eta << stateSpace_->eta_[i] << " ";
      first << stateSpace_->firstGridPoint_[i] << " ";
      last << stateSpace_->lastGridPoint_[i] << " ";
      nofGridPoints << stateSpace_->nofGridPoints_[i] << " ";
    }
    for(size_t j=0; j<nssVars_; j++) 
      indBddVars << postVars_[j] << " ";

    z << std::endl;
    eta << std::endl;
    first << std::endl;
    last << std::endl;
    nofGridPoints << std::endl;
    indBddVars << std::endl;

    std::stringstream paramss;
    paramss << "################################################################################" << std::endl
            << "########### symbolic controller synthesis toolbox information added ############" << std::endl
            << "################################################################################" << std::endl
            << dim.str() \
            << eta.str() \
            << z.str() \
            << first.str() \
            << last.str() \
            << nofGridPoints.str() \
            << indBddVars.str();
    std::string param=paramss.str();


    FILE *file = fopen (filename,"w");
    if (file == NULL){
        std::ostringstream os;
        os << "Error: Unable to open file for writing." << filename << "'.";
        throw std::runtime_error(os.str().c_str());
    }
    if (param.size() != fwrite(param.c_str(),1,param.size(),file)) {
        throw "Error: Unable to write set information to file.";
    }

    int storeReturnValue = Dddmp_cuddBddStore(
      ddmgr_->getManager(),
      NULL,
      transitionRelation_.getNode(),
      //(char**)varnameschar, // char ** varnames, IN: array of variable names (or NULL)
      NULL, // char ** varnames, IN: array of variable names (or NULL)
      NULL,
      DDDMP_MODE_BINARY,
      // DDDMP_VARNAMES,
      DDDMP_VARIDS,
      NULL,
      file
    );

    fclose(file);
    if (storeReturnValue!=DDDMP_SUCCESS) 
      throw "Error: Unable to write BDD to file.";
    else
      std::cout << "Symbolic set saved to file: "<< filename << std::endl;
  } 
Matthias Rungger's avatar
Matthias Rungger committed
266
267
268
269
}; /* close class def */
} /* close namespace */

#endif /* SYMBOLICMODEL_HH_ */