Пример #1
0
/*!
 *  \brief Read an individual bag from a XML file.
 *  \param inFileName Filename to read individual bag from.
 *  \param ioSystem Evolutionary system used to read individual.
 *  \param inBagTag Name of the XML tag containing the list of individuals to read.
 *  \return True if an individual of the given name was found and read from file.
 *    false if nothing was found.
 */
bool IndividualBag::readFromFile(std::string inFileName,
                                 System& ioSystem,
                                 std::string inBagTag)
{
	Beagle_StackTraceBeginM();
	std::ifstream lIFS(inFileName.c_str());
	PACC::XML::Document lParser(lIFS, inFileName);
	lIFS.close();
	PACC::XML::ConstFinder lBagFinder(lParser.getFirstDataTag());
	PACC::XML::ConstIterator lIndivTag = lBagFinder.find(std::string("//IndividualBag")+inBagTag);
	if(!lIndivTag) return false;

	Context::Alloc::Handle lContextAlloc =
	    castHandleT<Context::Alloc>(ioSystem.getFactory().getConceptAllocator("Concept"));
	Context::Handle lContext =
	    castHandleT<Context>(lContextAlloc->allocate());
	lContext->setSystemHandle(&ioSystem);

	Beagle_LogInfoM(
	    ioSystem.getLogger(),
	    std::string("Reading an individual bag from file '")+inFileName+
	    std::string("' that is in-between the XML tags '")+inBagTag+std::string("'")
	);

	readIndividuals(lIndivTag, *lContext);

	return true;
	Beagle_StackTraceEndM();
}
 explicit JavaLog(jobject log) : log_(log) {
   const Context::Handle ctx = Context::getContext();
   LocalRef<jclass> logClass(ctx->env()->GetObjectClass(log));
   appendId_ = ctx->env()->GetMethodID(
       logClass.get(), "append",
       "(Ljava/time/LocalDateTime;Lcom/yandex/contest/invoker/ILog$Level;"
       "Ljava/lang/String;Ljava/lang/String;)V");
   ctx->throwIfOccured();
 }
LocalRef<jobject> newPrimitiveWrapper(const T obj) {
  using jinfo = traits::jinfo<T>;
  static_assert(jinfo::is_primitive, "Should be primitive.");

  const Context::Handle ctx = Context::getContext();
  LocalRef<jclass> clazz(ctx->env()->FindClass(jinfo::jwrapperclass().c_str()));
  const jmethodID valueOfId = ctx->env()->GetStaticMethodID(
      clazz.get(), "valueOf", jinfo::valueOfSig().c_str());
  LocalRef<jobject> wrapper(
      ctx->env()->CallStaticObjectMethod(clazz.get(), valueOfId, obj));
  ctx->throwIfOccured();
  return wrapper;
}
void getIterable(jobject jobj, const std::function<void(jobject)> &cb) {
  const Context::Handle ctx = Context::getContext();
  LocalRef<jclass> clazz(ctx->env()->GetObjectClass(jobj));
  const jmethodID iteratorId = ctx->env()->GetMethodID(
      clazz.get(), "iterator", "()Ljava/util/Iterator;");
  LocalRef<jobject> iter(ctx->env()->CallObjectMethod(jobj, iteratorId));
  LocalRef<jclass> iterClass(ctx->env()->GetObjectClass(iter.get()));
  const jmethodID hasNextId =
      ctx->env()->GetMethodID(iterClass.get(), "hasNext", "()Z");
  const jmethodID nextId =
      ctx->env()->GetMethodID(iterClass.get(), "next", "()Ljava/lang/Object;");
  while (ctx->env()->CallBooleanMethod(iter.get(), hasNextId)) {
    LocalRef<jobject> jobj(ctx->env()->CallObjectMethod(iter.get(), nextId));
    ctx->throwIfOccured();
    cb(jobj.get());
  }
}
void getMap(jobject jobj, const std::function<void(jobject, jobject)> &cb) {
  const Context::Handle ctx = Context::getContext();
  LocalRef<jclass> clazz(ctx->env()->GetObjectClass(jobj));
  const jmethodID entrySetId =
      ctx->env()->GetMethodID(clazz.get(), "entrySet", "()Ljava/util/Set;");
  LocalRef<jobject> entrySet(ctx->env()->CallObjectMethod(jobj, entrySetId));
  LocalRef<jclass> mapEntryClass(ctx->env()->FindClass("java/util/Map$Entry"));
  const jmethodID getKeyId = ctx->env()->GetMethodID(
      mapEntryClass.get(), "getKey", "()Ljava/lang/Object;");
  const jmethodID getValueId = ctx->env()->GetMethodID(
      mapEntryClass.get(), "getValue", "()Ljava/lang/Object;");
  ctx->throwIfOccured();
  getIterable(entrySet.get(), [&ctx, &cb, getKeyId, getValueId](jobject jobj) {
    LocalRef<jobject> jkey(ctx->env()->CallObjectMethod(jobj, getKeyId));
    LocalRef<jobject> jvalue(ctx->env()->CallObjectMethod(jobj, getValueId));
    cb(jkey.get(), jvalue.get());
  });
}
Пример #6
0
/*!
 *  \brief Read individual from a XML file. If several individuals are in file, read
 *    first tagged occurence of individual.
 *  \param inFileName Filename to read individual from.
 *  \param ioSystem Evolutionary system to read individual.
 *  \return True if an individual was found and read from file, false if nothing was found.
 */
bool Individual::readFromFile(std::string inFileName, System& ioSystem)
{
	Beagle_StackTraceBeginM();
	std::ifstream lIFS(inFileName.c_str());
	PACC::XML::Document lParser(lIFS, inFileName);
	lIFS.close();
	PACC::XML::ConstFinder lIndivFinder(lParser.getFirstDataTag());
	PACC::XML::ConstIterator lIndivTag = lIndivFinder.find("//Individual");
	if(!lIndivTag) return false;

	Context::Alloc::Handle lContextAlloc =
	    castHandleT<Context::Alloc>(ioSystem.getFactory().getConceptAllocator("Context"));
	Context::Handle lContext = castHandleT<Context>(lContextAlloc->allocate());
	lContext->setSystemHandle(&ioSystem);
	lContext->setIndividualHandle(this);
	lContext->setIndividualIndex(0);

	readWithContext(lIndivTag, *lContext);

	return true;
	Beagle_StackTraceEndM();
}
/*!
 *  \brief Test the fitness of a given individual.
 *  \param inIndividual Handle to the individual to test.
 *  \param ioSystem Handle to the system to use to test the individual.
 *  \par Note:
 *    This method is provided as a mean to test some individuals after an evolution.
 */
Fitness::Handle Beagle::MPI::EvaluationOp::test(Individual::Handle inIndividual, System::Handle ioSystem)
{
	Beagle_LogInfoM(
					ioSystem->getLogger(),
					"evaluation", "Beagle::MPIEvaluationOp",
					std::string("Testing the following individual: ")+inIndividual->serialize()
					);
    
	Context::Alloc::Handle lContextAlloc =
    castHandleT<Context::Alloc>(ioSystem->getContextAllocatorHandle());
	Context::Handle lContext = castHandleT<Context>(lContextAlloc->allocate());
	lContext->setSystemHandle(ioSystem);
	lContext->setIndividualHandle(inIndividual);
	Fitness::Handle lFitness = evaluate(*inIndividual, *lContext);
	
	Beagle_LogInfoM(
					ioSystem->getLogger(),
					"evaluation", "Beagle::MPIEvaluationOp",
					std::string("New fitness of the individual: ")+lFitness->serialize()
					);
    
	return lFitness;
}
Пример #8
0
/*!
 *  \brief Apply the evaluation process on the invalid individuals of the deme.
 *  \param ioDeme Deme to process.
 *  \param ioContext Context of the evolution.
 */
void EvaluationMultipleOp::operate(Deme& ioDeme, Context& ioContext)
{
	Beagle_StackTraceBeginM();
	Beagle_LogTraceM(
	    ioContext.getSystem().getLogger(),
	    "Evaluating the fitness of the individuals in the " << 
	    uint2ordinal(ioContext.getDemeIndex()+1) << " deme"
	);

	Beagle_AssertM( ioDeme.size()!=0 );

	// Prepare stats
	prepareStats(ioDeme,ioContext);

	// Generate a vector of indicies into the population
	std::vector<unsigned int> lEvalVector;
	for(unsigned int i=0; i<ioDeme.size(); i++) {
		if((ioDeme[i]->getFitness() == NULL) ||
		        (ioDeme[i]->getFitness()->isValid() == false)) {
			lEvalVector.push_back(i);
			Beagle_LogDebugM(
			    ioContext.getSystem().getLogger(),
			    "Added " << uint2ordinal(i+1) << " individual for evaluation."
			);
		}
	}
	std::random_shuffle(lEvalVector.begin(), lEvalVector.end(),
	                    ioContext.getSystem().getRandomizer());

	Beagle_LogDebugM(
	    ioContext.getSystem().getLogger(),
	    "There are " << lEvalVector.size() << " individuals to be evaluated."
	);

	History::Handle lHistory = castHandleT<History>(ioContext.getSystem().haveComponent("History"));

	while ( !lEvalVector.empty() ) {
		// Put individuals and context into bags.
		Individual::Bag lIndividuals;
		Context::Bag lContexts;
		lIndividuals.resize( mIndisPerGroup );
		lContexts.resize( mIndisPerGroup );
		unsigned int lIndiCounter =0;

		for (unsigned int i=0; i<mIndisPerGroup; i++) {
			// Set individual
			lIndividuals[i] = ioDeme[lEvalVector.back()];
			lIndiCounter++;
			// Set context
			Context::Alloc::Handle lContextAlloc =
			    castHandleT<Context::Alloc>(ioContext.getSystem().getFactory().getConceptAllocator("Context"));
			Context::Handle lContext = castHandleT<Context>(lContextAlloc->clone(ioContext));
			lContext->setIndividualIndex( lEvalVector.back() );
			lContext->setIndividualHandle( ioDeme[lEvalVector.back()] );
			lContexts[i] = lContext;
			// Remove this index from the evaluation vector
			lEvalVector.pop_back();
			if(lEvalVector.empty()) {
				lIndividuals.resize( lIndiCounter );
				lContexts.resize( lIndiCounter );
				break;
			}
		}

		// Evaluate individuals
		std::ostringstream lOSS;
		lOSS << "Evaluating the fitness of the ";
		for(unsigned int i=0; i<lIndiCounter; i++) {
			// Add to message
			if (i==lIndiCounter-1) lOSS << " and ";
			lOSS << uint2ordinal(lContexts[i]->getIndividualIndex()+1);
			if (i<lIndiCounter-2) lOSS << ", ";
		}
		lOSS << " individuals";
		Beagle_LogVerboseM(
		    ioContext.getSystem().getLogger(),
		    lOSS.str()
		);
		Fitness::Bag::Handle lFitnessBag = evaluateIndividuals(lIndividuals, lContexts);

		// Assign fitnesses
		for (unsigned int i=0; i<lIndiCounter; i++) {
			Beagle_LogDebugM(
			    ioContext.getSystem().getLogger(),
			    "Considering fitness of the " << uint2ordinal(lContexts[i]->getIndividualIndex()+1) << " individual"
			);
			Beagle_AssertM( i < lFitnessBag->size() );
			Fitness::Handle lFitness = lFitnessBag->at(i);
			Beagle_NonNullPointerAssertM( lFitness );
			lIndividuals[i]->setFitness( lFitness );
			lIndividuals[i]->getFitness()->setValid();
			if(lHistory != NULL) {
				lHistory->allocateID(*lIndividuals[i]);
				lHistory->trace(ioContext, std::vector<HistoryID>(), lIndividuals[i], getName(), "evaluation");
			}

			Beagle_LogVerboseM(
			    ioContext.getSystem().getLogger(),
			    *lIndividuals[i]->getFitness()
			);
		}

		// Update stats
		updateStats(lIndividuals.size(),ioContext);
	}

	updateHallOfFameWithDeme(ioDeme,ioContext);
	Beagle_StackTraceEndM();
}