mardi 14 novembre 2017

|C++ | Q: The program does not crash neither executes completely |

it has been a few hours since I am dealing with this issue. I was wondering if someone could point out what am I doing wrong, and if possible - how to fix it. Essentially, I am simply trying to generate n number of object pairs and store them into a vector<pair<Foo, Foo>>. The algorithm involves random number generator. I use STL <random> and its components like m19937, uniform_real_distribution and uniform_int_distribution. Below is the simplified version of what I am trying to do representing the case I got at hand. The second loop always cuts short. However, I fail to see the reason why. Essentially, I never get to see the program execute completely. The last two messages never show.

program

#include <iostream>
#include <vector>
#include <random>
#include <utility>

// utility
using std::pair;

// random
using std::mt19937;
using std::uniform_int_distribution;
using std::uniform_real_distribution;

// iostream
using std::cout;
using std::endl;

// vector
using std::vector;

class Event{
private:
  double x, y;
public:
  Event(const double X, const double Y);
};

Event::Event(const double X, const double Y): x(X), y(Y){}

int main(){
  cout << "Initializing storage..." << endl;
  vector<Event> population;
  vector<pair<Event,Event>> selection;

  cout << "Initializing necessary member variables..." << endl;
  const unsigned int SEED = 14112017;
  const unsigned int MAX_ITERATIONS = 10000;

  const double MIN = 1;
  const double MAX = 10000;

  mt19937 engine(SEED);

  cout << "Generating the initial population..." << endl;
  uniform_real_distribution<> real_distribution(MIN, MAX);
  for(unsigned int i = 0; i < MAX_ITERATIONS; ++i){
    double x = real_distribution(engine);
    double y = real_distribution(engine);

    Event event(x, y);

    population.push_back(event);
  }
  cout << "Success! The initial population has been generated successfully" << endl;

  population.shrink_to_fit();

  cout << "Starting the selection process..." << endl;
  unsigned int random = 0;
  uniform_int_distribution<> int_distribution(MIN, MAX);
  for(unsigned int i = 0; i < MAX_ITERATIONS; ++i){
    random = int_distribution(engine);
    Event event_x = population.at(random);

    random = int_distribution(engine);
    Event event_y = population.at(random);

    pair<Event, Event> bound(event_x, event_y);

    selection.push_back(bound);
  }
  cout << "Success! The selection process has been completed successfully" << endl;

  selection.shrink_to_fit();

  cout << "population size: " << population.size() << endl;
  cout << "selection size: " << selection.size() << endl;

  return 0;
}

I compile the above using cygwins C++ compiler, and I execute the code in command-prompt. The OS is Windows 10 x64. The box has 32 GB memory.




Aucun commentaire:

Enregistrer un commentaire