/*!
 *  \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;
}
Esempio n. 2
0
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;
}
Esempio n. 3
-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;
}