int main (int argc, char** argv) { try { // 1. Build the system. System::Handle lSystem = new System; // 2. Build evaluation operator. DCDCBoost2xGAEvalOp::Handle lEvalOp = new DCDCBoost2xGAEvalOp; // 3. Instanciate the evolver. unsigned int lNbBits = (unsigned int)(NBSTATE*NBINPUTSYBMOLE + 1)*NBSWITCH; #ifdef USE_MPI MPI::GA::EvolverBitString::Handle lEvolver = new MPI::GA::EvolverBitString(lEvalOp,lNbBits); #else GA::EvolverBitString::Handle lEvolver = new GA::EvolverBitString(lEvalOp,lNbBits); #endif // 4. Initialize the vivarium GA::BitString::Alloc::Handle lBSAlloc = new GA::BitString::Alloc; LogFitness::Alloc::Handle lFitAlloc = new LogFitness::Alloc; Vivarium::Handle lVivarium = new Vivarium(lBSAlloc, lFitAlloc); // 5. Initialize the evolver and evolve the vivarium. lEvolver->initialize(lSystem, argc, argv); ////////////////////// //Read individual from file ostringstream lFilename; lFilename << "individual-ga-test.xml"; Individual::Handle lIndividual = new Individual(lBSAlloc); lIndividual->readFromFile(lFilename.str(), *lSystem); Beagle::Context::Handle lContext = castHandleT<Beagle::Context>(lSystem->getContextAllocator().allocate()); lContext->setSystemHandle(lSystem); lContext->setIndividualHandle(lIndividual); lContext->setIndividualIndex(0); LogFitness::Handle lFitness = castHandleT<LogFitness>(lEvalOp->evaluate(*lIndividual,*lContext)); cout << "\nResulting fitness: " << lFitness->getValue() << endl; ofstream lFitnessStream("fitness.xml"); lFitnessStream << lFitness->serialize() << endl; return 0; ///////////////////// lEvolver->evolve(lVivarium); } catch(Exception& inException) { inException.terminate(cerr); } catch(std::exception& inException) { cerr << "Standard exception catched:" << endl << flush; cerr << inException.what() << endl << flush; return 1; } return 0; }
/*! * \brief Main routine for the even-6 parity problem. * \param argc Number of arguments on the command-line. * \param argv Arguments on the command-line. * \return Return value of the program. * \ingroup Parity */ int main(int argc, char *argv[]) { try { // Build primitives GP::PrimitiveSuperSet::Handle lSuperSet = new GP::PrimitiveSuperSet; lSuperSet->addPrimitive(new GP::BitwiseAnd("AND"), true); lSuperSet->addPrimitive(new GP::BitwiseOr("OR"), true); lSuperSet->addPrimitive(new GP::BitwiseNand("NAND"), true); lSuperSet->addPrimitive(new GP::BitwiseNor("NOR"), true); lSuperSet->addPrimitive(new GP::BitwiseNot("NOT"), true); lSuperSet->addPrimitive(new GP::BitwiseXor("XOR"), true); lSuperSet->addPrimitive(new GP::ADF(GP::Invoker::eGenerator, GP::Primitive::eAny, "ADFP", "ARGP")); lSuperSet->addPrimitive(new GP::ArgumentT<Int>(GP::Argument::eCaching, GP::Argument::eGenerator, "ARGP")); GP::PrimitiveSet::Handle lSet = new GP::PrimitiveSet; lSet->insert(new GP::BitwiseAnd("AND")); lSet->insert(new GP::BitwiseOr("OR")); lSet->insert(new GP::BitwiseNand("NAND")); lSet->insert(new GP::BitwiseNor("NOR")); for (unsigned int i=0; i<ParitySizeM; i++) { lSet->insert(new GP::TokenT<Int>(std::string("IN")+uint2str(i))); } lSuperSet->insert(lSet); // Build a system System::Handle lSystem = new System; // Set GP package lSystem->addPackage(new GP::PackageBase(lSuperSet)); lSystem->addPackage(new GP::PackageConstrained); // Set fitness evaluation operator allocator lSystem->setEvaluationOp("ParityEvalOp", new ParityFastEvalOp::Alloc); // Initialize the evolver Evolver::Handle lEvolver = new Evolver; lEvolver->initialize(lSystem, argc, argv); // Create population Vivarium::Handle lVivarium = new Vivarium; // Launch evolution lEvolver->evolve(lVivarium, lSystem); } catch(Exception& inException) { inException.terminate(); } catch(exception& inException) { cerr << "Standard exception caught:" << endl; cerr << inException.what() << endl << flush; return 1; } catch(...) { cerr << "Unknown exception caught!" << endl << flush; return 1; } return 0; }
/*! * \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; }
double GPExperiment::suboptimizeAndCompareToTarget(unsigned suboptimizeType, GPNetwork* candidate, float* buffer) { // suboptimize according to suboptimize type if (suboptimizeType == 0) { renderIndividualByBlockPerformance(candidate, params->aux_render_block_size, numConstantValues, constantValues, target_num_frames, target_sample_times, buffer); double fitness = compareToTarget(params->exp_suboptimize_ff_type, buffer); candidate->doneRendering(); return fitness; } // run Beagle CMAES else if (suboptimizeType == 1) { std::vector<GPMutatableParam*>* candidateParams = candidate->getAllMutatableParams(); if (candidateParams->size() != 0) { try { // test initial conditions suboptimize_network = candidate; renderIndividualByBlockPerformance(suboptimize_network, params->aux_render_block_size, numConstantValues, constantValues, target_num_frames, target_sample_times, buffer); suboptimize_min_fitness = compareToTarget(params->exp_suboptimize_ff_type, buffer); // backup initial params logger->debug << "BEFORE(" << suboptimize_min_fitness << "): " << logger->net_to_string_print(suboptimize_network) << std::flush; std::vector<GPMutatableParam*>* candidate_params = suboptimize_network->getAllMutatableParams(); unsigned num_params = candidate_params->size(); suboptimize_best_params.resize(num_params); for (unsigned i = 0; i < num_params; i++) { GPMutatableParam* param = candidate_params->at(i); suboptimize_best_params[i] = param->getCopy(); } using namespace Beagle; // Build system System::Handle lSystem = new System; // Install the GA float vector and CMA-ES packages const unsigned int lVectorSize = candidateParams->size(); lSystem->addPackage(new GA::PackageFloatVector(lVectorSize)); lSystem->addPackage(new GA::PackageCMAES(lVectorSize)); // Initialize state space DoubleArray::Handle initMinValueArray = new DoubleArray(lVectorSize); DoubleArray::Handle initMaxValueArray = new DoubleArray(lVectorSize); DoubleArray::Handle minValueArray = new DoubleArray(lVectorSize); DoubleArray::Handle maxValueArray = new DoubleArray(lVectorSize); DoubleArray::Handle incValueArray = new DoubleArray(lVectorSize); // populate min/max/inc vectors for (unsigned i = 0; i < candidateParams->size(); i++) { GPMutatableParam* param = candidateParams->at(i); float paramMin = param->getMin(); float paramMax = param->getMax(); if (param->isDiscrete()) { // values are to allow any number in discrete range including min/max initMinValueArray->at(i) = paramMin + 0.5f; initMaxValueArray->at(i) = paramMax + 0.5f; minValueArray->at(i) = paramMin + 0.4f; maxValueArray->at(i) = paramMax + 0.6f; incValueArray->at(i) = 1.0f; } else { initMinValueArray->at(i) = paramMin; initMaxValueArray->at(i) = paramMax; minValueArray->at(i) = paramMin; maxValueArray->at(i) = paramMax; incValueArray->at(i) = 0.01f; } } // register state space with system Register::Description lDescription("Parameter state space", "Vector", "", ""); unsigned seed_beagle = seed + 1; lSystem->getRegister().insertEntry("ec.rand.seed", new ULong(seed_beagle), lDescription); lSystem->getRegister().insertEntry("ga.init.minvalue", initMinValueArray, lDescription); lSystem->getRegister().insertEntry("ga.init.maxvalue", initMaxValueArray, lDescription); lSystem->getRegister().insertEntry("ga.float.minvalue", minValueArray, lDescription); lSystem->getRegister().insertEntry("ga.float.maxvalue", maxValueArray, lDescription); lSystem->getRegister().insertEntry("ga.float.inc", incValueArray, lDescription); // Add evaluation operator allocator Register::Description hackDescription("Hack", "", "", ""); AudioComparisonParams::Handle evalParams = new AudioComparisonParams(); evalParams->experiment = this; evalParams->type = suboptimizeType; evalParams->candidate = candidate; evalParams->candidateFramesBuffer = buffer; lSystem->getRegister().insertEntry("audio.params", evalParams, hackDescription); // Set evaluation op lSystem->setEvaluationOp("AudioComparisonEvalOp", new AudioComparisonEvalOp::Alloc); // Set fitness type lSystem->getFactory().setConcept("Fitness", "FitnessSimpleMin"); // Initialize the evolver Evolver::Handle lEvolver = new Evolver; lEvolver->initialize(lSystem, beagle_cfg_file_path); // Create population Vivarium::Handle lVivarium = new Vivarium; // Launch evolution lEvolver->evolve(lVivarium, lSystem); // Lamarckian! for (unsigned i = 0; i < num_params; i++) { GPMutatableParam* param = suboptimize_best_params[i]; GPMutatableParam* stored_param = candidate_params->at(i); if (param->isDiscrete()) { stored_param->setDValue(param->getDValue()); } else { stored_param->setCValue(param->getCValue()); } delete param; } logger->debug << "AFTER(" << suboptimize_min_fitness << "): " << logger->net_to_string_print(suboptimize_network) << std::flush; } catch(Beagle::Exception& inException) { logger->error << "Beagle exception caught:" << std::flush; logger->error << inException.what() << std::flush; inException.terminate(logger->error); return -1; } catch (std::exception& inException) { logger->error << "Standard exception caught:" << std::flush; logger->error << inException.what() << std::flush; return -1; } } candidate->doneRendering(); return suboptimize_min_fitness; } return -1; }
/*! * \brief Main routine for the Knapsack problem. * \param argc Number of arguments on the command-line. * \param argv Arguments on the command-line. * \return Return value of the program. * \ingroup Knapsack */ int main(int argc, char** argv) { try { // Build the system System::Handle lSystem = new System; // Install the GA bit string and multi-objective optimization packages const unsigned int lNumberOfBits = 24; lSystem->addPackage(new BitStr::Package(lNumberOfBits)); lSystem->addPackage(new EMO::PackageMultiObj); // Add evaluation operator allocator lSystem->setEvaluationOp("KnapsackEvalOp", new KnapsackEvalOp::Alloc); // Initialize the evolver Evolver::Handle lEvolver = new Evolver; lEvolver->initialize(lSystem, argc, argv); // Create population Vivarium::Handle lVivarium = new Vivarium; // Launch evolution lEvolver->evolve(lVivarium, lSystem); } catch(Exception& inException) { inException.terminate(cerr); } catch(std::exception& inException) { cerr << "Standard exception caught:" << endl << flush; cerr << inException.what() << endl << flush; return 1; } return 0; }