This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Filling Sparse Matrix

Tags: None
(comma "," separated)
flaviotruzzi
Registered Member
Posts
7
Karma
0

Filling Sparse Matrix

Mon Aug 06, 2012 3:25 am
Hi There,

I am trying to make this code work, but I got an assertion failure in runtime. I made that working with the insert method, but the time is at least 50x slower, when using the Triplet vector the assertion error happens when a = 4.

When I run the program I get:

Creating Transation Matrix...
a:0 292820
a:1 492104
a:2 492368
a:3 492464
MDP-in-C--: /usr/include/eigen3/Eigen/src/Core/DenseCoeffsBase.h:394: Eigen::DenseCoeffsBase<Derived, 1>::Scalar& Eigen::DenseCoeffsBase<Derived, 1>::operator()(Eigen::DenseCoeffsBase<Derived, 1>::Index) [with Derived = Eigen::Matrix<int, -1, 1>; Eigen::DenseCoeffsBase<Derived, 1>::Scalar = int; Eigen::DenseCoeffsBase<Derived, 1>::Index = long int]: Assertion `index >= 0 && index < size()' failed.
Aborted

The code is bellow:
Code: Select all
void MDP::PopulateMtx() {

  int *sa =  new int[C+1];
  int *sc =  new int[C+1];

  cout << "Creating Transation Matrix... " << endl;

  for (int a = 0; a < A; a++) {
      T[a] = SparseMatrix<double>(S,S);
      R[a] = SparseVector<double>(S);

      //T[a].reserve(600000);
      R[a].reserve(600000);
      int nonzero = 0;
      std::vector<Tr> K;
      K.reserve(600000);
      for (int s = 0; s < S; s++) {
          getStateOfIndex(s,sa);

          for (int g = 0; g < G+1; g++) {

              memcpy(sc,sa,C*sizeof(int));

              sc[C] = g;

              float PI = 0;

              if (sc[C] == 0)
                PI =  1-Prequest;
              else
                PI = Prequest*Pg[sc[C]-1];

              if ((a > 0) && (sa[C] > 0) && (sa[a] > 0)) {

                 K.push_back(Tr(s,getIndexOfState(sc),PI*(1-CTR(sa[C]-1,a-1))));
                  //T[a].insert(s,getIndexOfState(sc)) = PI*(1-CTR(sa[C]-1,a-1));

                  sc[a] = sc[a]-1;

                  //T[a].insert(s,getIndexOfState(sc)) = PI*CTR(sa[C]-1,a-1);
                  K.push_back(Tr(s,getIndexOfState(sc),PI*CTR(sa[C]-1,a-1)));

                  R[a].insert(s) = eCPI(sa[C]-1,a-1);
                  nonzero++;
                  nonzero++;
              } else {
                  nonzero++;
                  //T[a].insert(s,getIndexOfState(sc)) = PI;

                  K.push_back(Tr(s,getIndexOfState(sc),PI));
              }

          }
      }

      T[a].setFromTriplets(K.begin(),K.end());
      T[a].finalize();

      cout << "a:" << a << " " << nonzero << endl;
  }
  cout << "Matrix T and R created" << endl;
  delete sa;
  delete sc;

}


Any ideas will be appreciated!

Thanks,
flaviotruzzi
Registered Member
Posts
7
Karma
0

Re: Filling Sparse Matrix

Mon Aug 06, 2012 8:30 am
Found the Error, It was the calculation of the state index, a code not displayed here, thanks and sorry.


Bookmarks



Who is online

Registered users: Baidu [Spider], Bing [Bot], Google [Bot], Yahoo [Bot]