bool AdaptSRNParametersOperator::applyOperator(Individual *individual, CommandExecutor*) { NeuralNetwork *net = dynamic_cast<NeuralNetwork*>(individual->getGenome()); if(net == 0) { Core::log("AdaptSRNParametersOperator: Could not apply operator because individual did not " "provide a NeuralNetwork as genome!"); return false; } double changeProbability = mChangeProbability->get(); QList<Neuron*> networkNeurons = net->getNeurons(); QList<SelfRegulatingNeuronActivationFunction*> consideredAF; for(QListIterator<Neuron*> i(networkNeurons); i.hasNext();) { //remove all neurons that can not be changed. Neuron *neuron = i.next(); SelfRegulatingNeuronActivationFunction *srnaf = dynamic_cast<SelfRegulatingNeuronActivationFunction*>(neuron->getActivationFunction()); if(srnaf == 0) { continue; } if(neuron->hasProperty(NeuralNetworkConstants::TAG_ELEMENT_PROTECTED)) { continue; } if(neuron->hasProperty(NeuralNetworkConstants::TAG_NEURON_PROTECT_ACTIVATION_FUNCTION)) { continue; } consideredAF.append(srnaf); } if(mGlobalSettings->get()) { if(consideredAF.empty()) { //there are no matching activation functions to change. return true; } //check if mutations take place if(Random::nextDouble() >= changeProbability) { return true; } SelfRegulatingNeuronActivationFunction *srnaf = consideredAF.at(0); double newAlpha = mutateParameter(srnaf->getAlpha(), 0); double newBeta = mutateParameter(srnaf->getBeta(), 1); double newGamma = mutateParameter(srnaf->getGamma(), 2); double newDelta = mutateParameter(srnaf->getDelta(), 3); double newAStar = mutateParameter(srnaf->getAStar(), 4); //check if the default AF should be mutated, too. SelfRegulatingNeuronActivationFunction *defaultAF = dynamic_cast<SelfRegulatingNeuronActivationFunction*>(net->getDefaultActivationFunction()); if(defaultAF != 0) { consideredAF.append(defaultAF); } for(QListIterator<SelfRegulatingNeuronActivationFunction*> i(consideredAF); i.hasNext();) { SelfRegulatingNeuronActivationFunction *af = i.next(); af->getAlpha()->set(newAlpha); af->getBeta()->set(newBeta); af->getGamma()->set(newGamma); af->getDelta()->set(newDelta); af->getAStar()->set(newAStar); } } else { for(QListIterator<SelfRegulatingNeuronActivationFunction*> i(consideredAF); i.hasNext();) { SelfRegulatingNeuronActivationFunction *af = i.next(); if(Random::nextDouble() >= changeProbability) { continue; } //change each parameter separately. double newAlpha = mutateParameter(af->getAlpha(), 0); double newBeta = mutateParameter(af->getBeta(), 1); double newGamma = mutateParameter(af->getGamma(), 2); double newDelta = mutateParameter(af->getDelta(), 3); double newAStar = mutateParameter(af->getAStar(), 4); af->getAlpha()->set(newAlpha); af->getBeta()->set(newBeta); af->getGamma()->set(newGamma); af->getDelta()->set(newDelta); af->getAStar()->set(newAStar); } } // // double probability = mInsertionProbability->get(); // // if(consideredNeurons.empty()) { // return true; // } // // //prepare the generation date as string. // QString generationDate = QString::number(Evolution::getEvolutionManager() // ->getCurrentGenerationValue()->get()); // // for(int i = 0; i < maxNumberOfNewBiases; ++i) { // // if(Random::nextDouble() >= probability) { // continue; // } // // //select neuron // Neuron *neuron = consideredNeurons.value(Random::nextInt(consideredNeurons.size())); // // if(neuron == 0) { // continue; // } // consideredNeurons.removeAll(neuron); // // // neuron->setProperty(NeuralNetworkConstants::TAG_NEURON_REINIT_BIAS); // neuron->setProperty(NeuralNetworkConstants::PROP_ELEMENT_MODIFIED); // // //mark the individual as significantly modified // individual->setProperty(EvolutionConstants::TAG_GENOME_SIGNIFICANT_CHANGE, // generationDate); // // } return true; }
void NetworkAgentControlParser::connectNetworksToInterfaces() { NeuralNetworkManager *nnm = Neuro::getNeuralNetworkManager(); bool addedNetwork = false; QStringList params = mNetLoaderArgument->getParameters(); int numberOfEntries = mNetLoaderArgument->getNumberOfEntries(); for(int i = 0; i < numberOfEntries; ++i) { QStringList paramSet; if(params.size() > i) { paramSet = params.at(i).split(" "); } if(paramSet.size() > 2) { Core::log("NetworkAgentControlParser: Could not interpret command line argument. "); continue; } QString agentName = ""; QString networkFile = ""; if(paramSet.size() > 0) { if(paramSet.at(0).trimmed() == "c") { networkFile = "c"; } else { agentName = paramSet.at(0); } } if(paramSet.size() > 1) { networkFile = paramSet.at(1); } if(networkFile.trimmed() == "c") { networkFile = ""; //use most recent network from the editor for this agent. QString filePrefix = Core::getInstance()->getConfigDirectoryPath() + "/properties"; QFile file(filePrefix + "/recentNetworks.txt"); if(file.open(QIODevice::ReadOnly | QIODevice::Text)) { QTextStream input(&file); if(!input.atEnd()) { networkFile = input.readLine(); } } file.close(); } PhysicsManager *pm = Physics::getPhysicsManager(); SimObjectGroup *agent = 0; if(agentName == "" && !pm->getSimObjectGroups().empty()) { agent = pm->getSimObjectGroups().at(0); } else { agent = pm->getSimObjectGroup(agentName); } if(agent == 0) { Core::log(QString("NetworkAgentControlParser: Could not find an agent with name [") .append(agentName).append("]! [Skipping]")); continue; } QString errorMessage; QList<QString> messages; NeuralNetwork *net = 0; //check if there is already a network that can be reused. QList<NeuralNetwork*> networks = nnm->getNeuralNetworks(); if(networks.size() > i) { net = networks.at(i); } if(net == 0) { if(networkFile == "") { //create standard network with matching number of input/output neurons. net = new ModularNeuralNetwork(); int numberOfInputs = agent->getOutputValues().size(); int numberOfOutputs = agent->getInputValues().size(); for(int i = 0; i < numberOfInputs; ++i) { Neuron *neuron = new Neuron("Neuron" + QString::number(i), *net->getDefaultTransferFunction(), *net->getDefaultActivationFunction()); neuron->setProperty(Neuron::NEURON_TYPE_INPUT); net->addNeuron(neuron); } for(int i = 0; i < numberOfOutputs; ++i) { Neuron *neuron = new Neuron("Neuron" + QString::number(i), *net->getDefaultTransferFunction(), *net->getDefaultActivationFunction()); neuron->setProperty(Neuron::NEURON_TYPE_OUTPUT); net->addNeuron(neuron); } } else { net = NeuralNetworkIO::createNetworkFromFile( networkFile, &errorMessage, &messages); } } if(!messages.empty()) { Core::log(QString("NetworkAgentControlParser: ").append(errorMessage)); for(int i = 0; i < messages.size(); ++i) { Core::log(QString("NetworkAgentControlParser: ").append(messages.at(i))); } } if(net == 0) { Core::log(QString("NetworkAgentControlParser: Could not load network from file [") .append(networkFile).append("]! [Skipping]")); continue; } agent->setController(net); nnm->addNeuralNetwork(net); addedNetwork = true; net->reset(); Core::log(QString("NetworkAgentControlParser: Loaded network from file [") .append(networkFile).append("] to control agent [") .append(agentName).append("]!")); } if(addedNetwork) { nnm->triggerCurrentNetworksReplacedEvent(); } }