NewsItem *HiveMindInstance::randomNews(ControllerPtr excludeA, ControllerPtr excludeB) {
	int pickedNews = -1;
	NewsItem *pickedItem = NULL;

	if (getActiveCount() == 0) {
		cout << "WARNING: News requested while no agent had set news yet" << endl;
		return NULL;
	}

	if (getActiveCount() == 1 && (excludeA != NULL || excludeB != NULL)) {
		cout << "WARNING: Second news requested while only one agent has set news" << endl;
		return NULL;
	}

	if (getActiveCount() == 2 && excludeA != NULL && excludeB != NULL) {
		cout << "WARNING: Third news requested while only two agents have set news" << endl;
		return NULL;
	}

	/* Select a random news item that is not one of the exclude arguments */
	while (pickedItem == NULL || (pickedItem != NULL && (pickedItem->getContent() == excludeA || pickedItem->getContent() == excludeB))) {
		// select a news item
		int pickedPeer = Rand::randint(0, allItems.size());
		pickedNews = Rand::randint(0, allItems[pickedPeer]->size());
		pickedItem = allItems[pickedPeer]->at(pickedNews);
	}
	return pickedItem;
}
NewsItem *NewscastAgent::randomNews(ControllerPtr excludeA, ControllerPtr excludeB) {
	int pickedNews = -1;
	NewsItem *pickedItem = NULL;
	
	if (latestItems.size() == 0) {
		if (debug) { cout << "WARNING: News requested while no agent had set news yet" << endl; }
		return NULL;
	}
	
	if (latestItems.size() == 1 && (excludeA || excludeB)) {
		if (debug) { cout << "WARNING: Second news requested while only one agent has set news" << endl; }
		return NULL;
	}
	
	if (latestItems.size() == 2 && excludeA && excludeB) {
		if (debug) { cout << "WARNING: Third news requested while only two agents have set news" << endl; }
		return NULL;
	}
	
	/* Select a random news item that is not one of the exclude arguments */
	while (pickedItem == NULL ||
		   (pickedItem != NULL && pickedItem->getContent() != NULL && (pickedItem->getContent() == excludeA || pickedItem->getContent() == excludeB))) {
		// select a news item
		pickedNews = Rand::randint(0, latestItems.size());
		pickedItem = latestItems[pickedNews];
	}
	return pickedItem;
}
NewsItem *NewscastAgent::randomNews(vector<ControllerPtr > *exclude){
        int pickedNews = -1;
	NewsItem *pickedItem = NULL;
	
	if (latestItems.size() == 0) {
		if (debug) { cout << "WARNING: News requested while no agent had set news yet" << endl; }
		return NULL;
	}
	
        if(latestItems.size() <= exclude->size()) {
            if (debug) { cout << "WARNING: " << (exclude->size()+1) << " news items set while " << latestItems.size() << " available"; }
            return NULL;
        }
	
	/* Select a random news item that is not one of the exclude arguments */
	while (pickedItem == NULL || (pickedItem != NULL && pickedItem->getContent() != NULL && Util::checkExcludesForItem(pickedItem, exclude))) {
            pickedNews = Rand::randint(0, latestItems.size());
            pickedItem = latestItems[pickedNews];
	}
	return pickedItem;
}
Example #4
0
	void DoAdd(CommandSource &source, const std::vector<Anope::string> &params, NewsType ntype, const char **msgs)
	{
		const Anope::string text = params.size() > 1 ? params[1] : "";

		if (text.empty())
		{
			this->OnSyntaxError(source, "ADD");
			return;
		}

		if (Anope::ReadOnly)
			source.Reply(_("Services are in read-only mode. Any changes made may not persist."));

		NewsItem *ni = Serialize::New<NewsItem *>();
		ni->SetNewsType(ntype);
		ni->SetText(text);
		ni->SetTime(Anope::CurTime);
		ni->SetWho(source.GetNick());

		source.Reply(msgs[MSG_ADDED]);
		Log(LOG_ADMIN, source, this) << "to add a news item";
	}
ControllerPtr PopulationControlArchitecture::selectParentByBinaryTournament(ControllerPtr exclude) {
	NewsItem *selectedSolutionA = population->randomNews(exclude);
	ControllerPtr parentA;
	if (selectedSolutionA != NULL) {
		if (debug) {
			cout << population->getId() << " - ParentA: " << selectedSolutionA->getAgentId() << " (" << selectedSolutionA->getTimestamp() << ")" << endl;
		}
		parentA = selectedSolutionA->getContent();
	}
	if (parentA == NULL) { // the population gave us a NULL parent; generate a random one
		//cout << population->getId() << " - ParentA: NULL" << endl;
		if (debug) {
			cout << "Got NULL parent A, generating random parent A." << endl;
		}
		parentA = this->createRandomGenome();
	}

	// select a second parent, excluding the first from the selection
	NewsItem *selectedSolutionB = population->randomNews(parentA, exclude);
	ControllerPtr parentB;
	if (selectedSolutionB != NULL) {
		if (debug) {
			cout << population->getId() << " - ParentB: " << selectedSolutionB->getAgentId() << " (" << selectedSolutionB->getTimestamp() << ")" << endl;
		}
		parentB = selectedSolutionB->getContent();
	}
	if (parentB == NULL) { // the population gave us a NULL parent; generate a random one
		//cout << population->getId() << " - ParentB: NULL" << endl;
		if (debug) {
			cout << "Got NULL parent B, generating random parent B." << endl;
		}
		parentB = this->createRandomGenome();
	}

	if (debug) {
		cout << "ParentA: " << parentA->GetOriginalFitness() << " vs ParentB: " << parentB->GetOriginalFitness() << endl;
	}

	if (parentA->GetOriginalFitness() > parentB->GetOriginalFitness()) {
		return parentA;
	} else {
		return parentB;
	}
}