mercredi 7 octobre 2020

Random Device member class gives same sequence?

My problem is as follows: I've got a class that holds a vector of another class that have to use random number generators. One way of implementation gives me a compile error 'error: use of deleted function ‘Ant::Ant(const Ant&)’ and a second approach gives me identical results in the repeated runs of the algorithm in main() using a for loop. If i run separate program instances, one after another, randomness is there (or seems to be).

Let me show you if i can.

EDIT: I am on Linux using GCC 7.5.0 One class holds a vector to a custom class and initialize as follow.

AntSystemSimple.h

class AntSystemSimple {
public:

    ...
private:
    std::vector<Ant> mAnts;
public:
   void init() 
    ...
};

AntSystem.cpp

AntSystemSimple::AntSystemSimple()
{
   ...
    mAnts.resize(1);
    ...
}

void AntSystemSimple::init() {
    this->setParameters();
    mAnts.resize(mNumOfAnts);
}

Ants.h

class Ant {
public:
    Ant();
    virtual ~Ant();

    void init(int numOfDestinations);
   void computeTour(...)
   ...

private:
   ...
    std::random_device randomDevice;
    std::mt19937_64 gen;
};

Ants.cpp

Ant::Ant():
...
*gen(randomDevice())*
{
...
}

void Ant::computeTour(...){
...
  std::uniform_real_distribution<double> dis(0.0, 1.0);
  double exceed = dis(gen);
...
}

First approach, that gives compile error 'error: use of deleted function ‘Ant::Ant(const Ant&)’ is the above. Eclipse shows the error at class Ant and required from class AntSystemSimple::ctor mAnts.resize(1) if it is any help.

Second approach, is to remove completely the randomDevice as member and initialize mt193737 as gen(std::random_device{}()). This compiles and works for consecutive calls of the compiled program. But if i ran the `main.cpp' like this:

AntSystemSimple antSystem;
for (int i = 0; i < repeat; ++i) {
        antSystem.init();
        antSystem.setInputDataMatrix(distances);
        antSystem.setParameters();

        antSystem.run();

        std::cout << "Best Length is: " << antSystem.getBestLength() << std::endl;
        printVectorInt("Best Path is: ", antSystem.getBestTour());
}

produces the same paths(randomness is the same) everytime.

I don't know if i described the problem enough, feel free to correct and ask any information. I would really like to undestand the problem here and what its causing it.

Thank you a lot.




Aucun commentaire:

Enregistrer un commentaire