ControllerPtr PopulationControlArchitecture::getNextCandidate() {
	currentTime++;
	ControllerPtr candidate;

	// if there is no full list of active solutions yet, generate a new candidate randomly
	if (activeSolutions.size() < mu) {
		candidate = this->createRandomGenome();
		if (debug) {
			cout << "Generated " << candidate->ToString() << endl;
		}
	}

	// get news from the population, spreading the active solution
	population->update(currentTime);

	// if there is a full list of active solutions, mutate an existing solution or re-evaluate an active to create a new candidate
	if (activeSolutions.size() == mu) {
		// decide whether to re-evaluate or mutate a controller
		if (Rand::randouble() < reEvaluationRate) { // re-evaluate
			// pick one of the active solutions for re-evaluation
			candidate = activeSolutions[Rand::randint(0, activeSolutions.size())];
			if (debug) {
				cout << "Selected re-evaluation of " << candidate->ToShortString() << endl;
			}
		} else { // mutate existing

			// parent selection: random from the population
			ControllerPtr parentA = selectParentByBinaryTournament(ControllerPtr());
			if (debug) {
				cout << "Selected first parent: " << parentA->ToShortString() << endl;
			}

			// crossover or clone; crossover only if there is a second active solution in the hivemind
			if (population->getActiveCount() > 1 && Rand::randouble() < crossoverRate) {
				if (debug) {
					cout << "Selecting second parent, from a choice of " << population->getActiveCount() << endl;
				}
				// crossover
				// select a second parent, excluding the first from the selection
				vector<ControllerPtr> *parents = new vector<ControllerPtr>();
				parents->push_back(parentA);
				while (parents->size()<(unsigned) numberOfParents) {
					if (debug) {
						cout << parents->size() << endl;
					}
					ControllerPtr tmpC = selectParentsByBinaryTournament(parents);
					parents->push_back(tmpC);
				}
				// create a candidate from crossover between the parents
				candidate = parentA->crossOverWithMP(parents);

				if (debug) {
					cout << " Created " << candidate->ToShortString() << " from crossover between " << parents->at(0)->ToShortString();
					for (uint i = 1; i < parents->size(); i++) cout << " and " << parents->at(i)->ToShortString();
					cout << endl;
				}
				parents->clear();
				delete parents;
			} else {
				// clone
				candidate = parentA->Clone();
				if (debug) {
					cout << "Spawned " << candidate->ToShortString() << " from " << parentA->ToShortString() << endl;
				}
			}

			// mutation
			if (Rand::randouble() < mutationRate) {
				//cout << "Before mutation: " << candidate->ToString() << endl;
				candidate->mutate();
				//cout << "After mutation: " << candidate->ToString() << endl;
				if (debug) {
					cout << "Mutated " << candidate->ToShortString() << endl;
				}
			}
		}
	}

	return candidate;

}