// // to be used only from javaScript... // bool KstCPlugin::setModule(KstPluginPtr plugin) { // Assumes that this is called with a write lock in place on this object Q_ASSERT(myLockStatus() == KstRWLock::WRITELOCKED); if (plugin != _plugin) { freeParameters(); if (_localData) { if (!_plugin || !_plugin->freeLocalData(&_localData)) { free(_localData); } _localData = 0L; } _inputVectors.clear(); _inputScalars.clear(); _inputStrings.clear(); _outputVectors.clear(); _outputScalars.clear(); _outputStrings.clear(); _plugin = plugin; } return true; }
KstCPlugin::~KstCPlugin() { freeParameters(); if (_localData) { if (!_plugin || !_plugin->freeLocalData(&_localData)) { free(_localData); } _localData = 0L; } }
KstPlugin::~KstPlugin() { freeParameters(); if (_localData) { if (!_plugin || !_plugin->freeLocalData(&_localData)) { free(_localData); } _localData = 0L; } //kstdDebug() << "Destroying KSTPlugin: " << long(this) << endl; }
void endProgram(int ret) { params = freeParameters(params); GOC_DEBUG("Closing libao Device\n"); closeOutputDevice(); GOC_DEBUG("Closing libao\n"); ao_shutdown(); // goc_termRestore(); exit(ret); }
int main(int argc, char * argv[]){ Population * pop, * selected; Individual * best_solution; int generation_num; initParameters(argc, argv); pop = genSeededPopulation(POP_SIZE); #if (defined DIVERSITY) printGeneComposition(pop); #endif determineFitness(pop); sortByFitness(pop); generation_num = 0; while(generation_num < MAX_NUM_GENERATIONS){ #if (defined VERBOSE || defined DIVERSITY) fprintf(stdout, "\n----------------- GENERATION %d -----------------\n", generation_num + 1); printPopulation(pop); #endif // FIX - use function pointers instead of flags + if statement if(selection_type == 1) selected = tournamentSelection(pop); else selected = randomSelection(pop); // FIX - use function pointers instead of flags + if statement evolvePopulation(selected, crossover_type, mutation_type); determineFitness(selected); // FIX - use function pointers instead of flags + if statement if(replacement_type == 1) pop = replaceAll(pop, selected); else pop = retainBest(pop, selected); generation_num++; } fprintf(stdout, "\nFINAL RESULT:\n"); printPopulation(pop); fprintf(stdout, "\nBEST SOLUTION:\n"); best_solution = findBest(pop); printIndividual(best_solution); freePopulation(pop); freeParameters(); return EXIT_SUCCESS; }
int main(void){ struct parameters *params = NULL; struct chromosome *chromoA = NULL; struct chromosome *chromoB = NULL; struct chromosome *chromoC = NULL; struct dataSet *trainingData = NULL; double testInputs[NUMINPUTS]; params = initialiseParameters(NUMINPUTS, NUMNODES, NUMOUTPUTS, ARITY); addNodeFunction(params, "add,sub,mul,sq,cube,sin"); trainingData = initialiseDataSetFromFile("./dataSets/symbolic.data"); chromoA = initialiseChromosome(params); chromoB = initialiseChromosome(params); setChromosomeFitness(params, chromoA, trainingData); mutateChromosome(params,chromoA); copyChromosome(chromoB, chromoA); removeInactiveNodes(chromoB); printf("chromoA with inactive nodes.\n"); printChromosome(chromoA, 0); printf("chromoB without inactive nodes.\n"); printChromosome(chromoB, 1); saveChromosome(chromoB, "chromoB.chromo"); chromoC = initialiseChromosomeFromFile("chromoB.chromo"); testInputs[0] = 3; executeChromosome(chromoC, testInputs); printf("Applied input: %f\n", testInputs[0]); printf("Generated output: %f\n", getChromosomeOutput(chromoC, 0)); freeChromosome(chromoA); freeChromosome(chromoB); freeChromosome(chromoC); freeDataSet(trainingData); freeParameters(params); return 0; }
int main(void){ struct parameters *params = NULL; struct chromosome *chromo = NULL; int numInputs = 1; int numNodes = 20; int numOutputs = 1; int nodeArity = 5; int numGens = 25000; double targetFitness = 0.5; int updateFrequency = 500; double weightRange = 5; params = initialiseParameters(numInputs, numNodes, numOutputs, nodeArity); setTargetFitness(params, targetFitness); setUpdateFrequency(params, updateFrequency); setConnectionWeightRange(params, weightRange); setCustomFitnessFunction(params, sinWave, "sinWave"); addNodeFunction(params, "tanh,softsign"); chromo = runCGP(params, NULL, numGens); printChromosome(chromo, 1); freeChromosome(chromo); freeParameters(params); return 0; }
bool KstCPlugin::setPlugin(KstPluginPtr plugin) { // Assumes that this is called with a write lock in place on this object Q_ASSERT(myLockStatus() == KstRWLock::WRITELOCKED); if (plugin == _plugin) { return true; } freeParameters(); if (_localData) { if (!_plugin || !_plugin->freeLocalData(&_localData)) { free(_localData); } _localData = 0L; } if (!plugin) { _inputVectors.clear(); _inputScalars.clear(); _inputStrings.clear(); _outputVectors.clear(); _outputScalars.clear(); _outputStrings.clear(); _plugin = 0L; return true; } Plugin::countScalarsVectorsAndStrings(plugin->data()._inputs, _inScalarCnt, _inArrayCnt, _inStringCnt, _inPid); if (_inputVectors.count() != _inArrayCnt || _inputScalars.count() != _inScalarCnt - _inPid || _inputStrings.count() != _inStringCnt) { _plugin = 0L; return false; } _outScalarCnt = 0; _outArrayCnt = 0; _outStringCnt = 0; _outputVectors.clear(); _outputScalars.clear(); _outputStrings.clear(); const QValueList<Plugin::Data::IOValue>& otable = plugin->data()._outputs; for (QValueList<Plugin::Data::IOValue>::ConstIterator it = otable.begin(); it != otable.end(); ++it) { if ((*it)._type == Plugin::Data::IOValue::TableType) { KstWriteLocker blockVectorUpdates(&KST::vectorList.lock()); KstVectorPtr v; if ((*it)._subType == Plugin::Data::IOValue::FloatNonVectorSubType) { v = new KstVector(KstObjectTag((*it)._name, tag()), 0, this, true); } else { v = new KstVector(KstObjectTag((*it)._name, tag()), 0, this, false); } _outputVectors.insert((*it)._name, v); ++_outArrayCnt; } else if ((*it)._type == Plugin::Data::IOValue::FloatType) { KstWriteLocker blockScalarUpdates(&KST::scalarList.lock()); KstScalarPtr s = new KstScalar(KstObjectTag((*it)._name, tag()), this); _outputScalars.insert((*it)._name, s); ++_outScalarCnt; } else if ((*it)._type == Plugin::Data::IOValue::StringType) { KstWriteLocker blockStringUpdates(&KST::stringList.lock()); KstStringPtr s = new KstString(KstObjectTag((*it)._name, tag()), this); _outputStrings.insert((*it)._name, s); ++_outStringCnt; } } allocateParameters(); _plugin = plugin; return true; }
// // to be used only from javaScript... // bool KstCPlugin::validate( ) { bool rc = false; if (_plugin) { if (_plugin.data()) { Plugin::countScalarsVectorsAndStrings(_plugin->data()._inputs, _inScalarCnt, _inArrayCnt, _inStringCnt, _inPid); if (_inArrayCnt > 0 || _inScalarCnt > 0 || _inStringCnt > 0) { if (_inputVectors.count() == _inArrayCnt && _inputScalars.count() == _inScalarCnt - _inPid && _inputStrings.count() == _inStringCnt) { _outScalarCnt = 0; _outArrayCnt = 0; _outStringCnt = 0; _outputVectors.clear(); _outputScalars.clear(); _outputStrings.clear(); freeParameters(); const QValueList<Plugin::Data::IOValue>& otable = _plugin->data()._outputs; for (QValueList<Plugin::Data::IOValue>::ConstIterator it = otable.begin(); it != otable.end(); ++it) { if ((*it)._type == Plugin::Data::IOValue::TableType) { KstWriteLocker blockVectorUpdates(&KST::vectorList.lock()); KstVectorPtr v; if ((*it)._subType == Plugin::Data::IOValue::FloatNonVectorSubType) { v = new KstVector(KstObjectTag((*it)._name, tag()), 0, this, true); } else { v = new KstVector(KstObjectTag((*it)._name, tag()), 0, this, false); } _outputVectors.insert((*it)._name, v); ++_outArrayCnt; } else if ((*it)._type == Plugin::Data::IOValue::FloatType) { KstWriteLocker blockScalarUpdates(&KST::scalarList.lock()); KstScalarPtr s = new KstScalar(KstObjectTag((*it)._name, tag()), this); _outputScalars.insert((*it)._name, s); ++_outScalarCnt; } else if ((*it)._type == Plugin::Data::IOValue::StringType) { KstWriteLocker blockStringUpdates(&KST::stringList.lock()); KstStringPtr s = new KstString(KstObjectTag((*it)._name, tag()), this); _outputStrings.insert((*it)._name, s); ++_outStringCnt; } } allocateParameters(); KstDataObjectList::Iterator oi = KST::dataObjectList.findTag(tagName()); if (oi == KST::dataObjectList.end()) { KST::dataObjectList.lock().writeLock(); KST::dataObjectList.append(this); KST::dataObjectList.lock().unlock(); } setDirty(true); rc = true; } } } } return rc; }
bool KstPlugin::setPlugin(KstSharedPtr<Plugin> plugin) { // Assumes that this is called with a write lock in place on this object if (plugin == _plugin) { return true; } freeParameters(); if (_localData) { if (!_plugin || !_plugin->freeLocalData(&_localData)) { free(_localData); } _localData = 0L; } if (!plugin) { _inputVectors.clear(); _inputScalars.clear(); _inputStrings.clear(); _outputVectors.clear(); _outputScalars.clear(); _outputStrings.clear(); _plugin = 0L; return true; } Plugin::countScalarsVectorsAndStrings(plugin->data()._inputs, _inScalarCnt, _inArrayCnt, _inStringCnt, _inPid); if (_inputVectors.count() != _inArrayCnt || _inputScalars.count() != _inScalarCnt - _inPid || _inputStrings.count() != _inStringCnt) { _plugin = 0L; return false; } _outScalarCnt = 0; _outArrayCnt = 0; _outStringCnt = 0; _outputVectors.clear(); _outputScalars.clear(); _outputStrings.clear(); const QValueList<Plugin::Data::IOValue>& otable = plugin->data()._outputs; for (QValueList<Plugin::Data::IOValue>::ConstIterator it = otable.begin(); it != otable.end(); ++it) { if ((*it)._type == Plugin::Data::IOValue::TableType) { KstVectorPtr v; if ((*it)._subType == Plugin::Data::IOValue::FloatNonVectorSubType) { v = new KstVector(QString::null, 0, this, true); } else { v = new KstVector(QString::null, 0, this, false); } v->KstObject::writeLock(); _outputVectors.insert((*it)._name, v); ++_outArrayCnt; KST::addVectorToList(v); } else if ((*it)._type == Plugin::Data::IOValue::FloatType) { KstScalarPtr s = new KstScalar(QString::null, this); s->KstObject::writeLock(); _outputScalars.insert((*it)._name, s); ++_outScalarCnt; } else if ((*it)._type == Plugin::Data::IOValue::StringType) { KstStringPtr s = new KstString(QString::null, this); s->KstObject::writeLock(); _outputStrings.insert((*it)._name, s); ++_outStringCnt; } } allocateParameters(); _plugin = plugin; return true; }
int main(void){ int i, gen; struct parameters *params = NULL; struct chromosome *population[POPULATIONSIZE]; struct chromosome *fittestChromosome = NULL; struct dataSet *trainingData = NULL; double targetFitness = 0; int maxGens = 10000; params = initialiseParameters(NUMINPUTS, NUMNODES, NUMOUTPUTS, ARITY); addNodeFunction(params, "or,nor,and,nand"); /*setTargetFitness(params, targetFitness);*/ setMutationType(params, "probabilistic"); setMutationRate(params, 0.08); trainingData = initialiseDataSetFromFile("./examples/parity3bit.data"); for(i=0; i<POPULATIONSIZE; i++){ population[i] = initialiseChromosome(params); } fittestChromosome = initialiseChromosome(params); /* for the number of allowed generations*/ for(gen=0; gen<maxGens; gen++){ /* set the fitnesses of the population of chromosomes*/ for(i=0; i<POPULATIONSIZE; i++){ setChromosomeFitness(params, population[i], trainingData); } /* copy over the last chromosome to fittestChromosome*/ copyChromosome(fittestChromosome, population[POPULATIONSIZE - 1]); /* for all chromosomes except the last*/ for(i=0; i<POPULATIONSIZE-1; i++){ /* copy ith chromosome to fittestChromosome if fitter*/ if(getChromosomeFitness(population[i]) < getChromosomeFitness(fittestChromosome)){ copyChromosome(fittestChromosome, population[i]); } } /* termination condition*/ if(getChromosomeFitness(fittestChromosome) <= targetFitness){ break; } /* set the first member of the population to be the fittest chromosome*/ copyChromosome(population[0], fittestChromosome); /* set remaining member of the population to be mutations of the fittest chromosome*/ for(i=1; i<POPULATIONSIZE; i++){ copyChromosome(population[i], fittestChromosome); mutateChromosome(params, population[i]); } } printf("gen\tfitness\n"); printf("%d\t%f\n", gen, getChromosomeFitness(fittestChromosome)); for(i=0; i<POPULATIONSIZE; i++){ freeChromosome(population[i]); } freeChromosome(fittestChromosome); freeDataSet(trainingData); freeParameters(params); return 0; }