Inheritance: NeuralAgent
Exemplo n.º 1
0
        private ForagingAgent getAgent(int i, IBlackBox phenome)
        {
            switch (AgentType)
            {
            case AgentTypes.Neural:
                return(new NeuralAgent(i, _genomeList[i].SpecieIdx, phenome, _agentsNavigate, _agentsHide));

            case AgentTypes.Social:
                var a = new SocialAgent(i, _genomeList[i].SpecieIdx, phenome, _agentsNavigate, _agentsHide,
                                        sar => sar.Last.Value.Reward > 0)
                {
                    MemorySize = CurrentMemorySize
                };
                var network = (FastCyclicNetwork)phenome;
                network.Momentum             = ((SocialAgent)a).Momentum;
                network.BackpropLearningRate = ((SocialAgent)a).LearningRate;
                return(a);

            case AgentTypes.QLearning:
                return(new QLearningAgent(i, _genomeList[i].SpecieIdx, phenome, _agentsNavigate, _agentsHide,
                                          8, 4, _world));

            case AgentTypes.Spinning:
                return(new SpinningAgent(i));

            default:
                return(null);
            }
        }
Exemplo n.º 2
0
        private void GenomesToAcceptability(IList <TGenome> genomeList)
        {
            string TEMP_NETWORK_FILE = string.Format("____temp{0}____network.xml", TrialId);

            var neatGenomeParams = new NeatGenomeParameters()
            {
                ActivationFn = PlainSigmoid.__DefaultInstance,
                InitialInterconnectionsProportion = 1
            };

            int inputs = _world.PlantTypes.Count() * World.SENSORS_PER_OBJECT_TYPE
                         + _world.Predators.Count() * World.SENSORS_PER_OBJECT_TYPE
                         + 1;
            int outputs = 2;

            var factory = new NeatGenomeFactory(inputs, outputs, neatGenomeParams);

            for (int i = 0; i < _agents.Length; i++)
            {
                // Decode the genome.
                IBlackBox phenome             = _genomeDecoder.Decode(genomeList[i]);
                IAcceptabilityFunction accept = new RecurrentNeuralAcceptability(phenome);

                // Check that the genome is valid.
                if (phenome == null)
                {
                    Console.WriteLine("Couldn't decode genome {0}!", i);
                    _agents[i] = new SpinningAgent(i);
                    continue;
                }

                // Create a feed forward network with 10 hidden nodes and random weights
                SocialExperiment.CreateNetwork(TEMP_NETWORK_FILE, inputs, outputs);
                using (var xr = XmlReader.Create(TEMP_NETWORK_FILE))
                {
                    var controllerGenome  = NeatGenomeXmlIO.ReadCompleteGenomeList(xr, false, factory)[0];
                    var controllerPhenome = _genomeDecoder.Decode((TGenome)controllerGenome);
                    _agents[i] = new SocialAgent(i, _genomeList[i].SpecieIdx, controllerPhenome, _agentsNavigate, _agentsHide, accept)
                    {
                        MemorySize = CurrentMemorySize
                    };
                    var network = (FastCyclicNetwork)controllerPhenome;
                    network.Momentum             = ((SocialAgent)_agents[i]).Momentum;
                    network.BackpropLearningRate = ((SocialAgent)_agents[i]).LearningRate;
                }
            }

            File.Delete(TEMP_NETWORK_FILE);
        }
Exemplo n.º 3
0
        // Handle the reward-based teaching paradigms whenever a plant is eaten.
        void _world_PlantEaten(object sender, IAgent eaterAgent, Plant eaten)
        {
            if (!_learningEnabled)
            {
                return;
            }
            // if we're not dealing with a social teacher, then skip this notification.
            if (!(eaterAgent is SocialAgent))
            {
                return;
            }

            // Only learn from rewards if we're using a reward-based social learning paradigm
            if (TeachParadigm != TeachingParadigm.EveryoneRewards &&
                TeachParadigm != TeachingParadigm.SubcultureRewards &&
                TeachParadigm != TeachingParadigm.EveryoneRewardsAndPolling &&
                TeachParadigm != TeachingParadigm.SubcultureRewardsAndPolling &&
                TeachParadigm != TeachingParadigm.SubcultureRewardFiltering)
            {
                return;
            }

            SocialAgent eater = (SocialAgent)eaterAgent;



            // Only learn from positive rewards.
            //if (eaten.Species.Reward > 0)
            //{
            // Train all the agents in parallel
            for (int i = 0; i < _agents.Length; i++)
            {
                SocialAgent agent = (SocialAgent)_agents[i];

                // Do not try to teach yourself
                if (agent == eater)
                {
                    continue;
                }

                // Only update individuals in your subculture
                if ((TeachParadigm == TeachingParadigm.SubcultureRewards ||
                     TeachParadigm == TeachingParadigm.SubcultureRewardFiltering ||
                     TeachParadigm == TeachingParadigm.SubcultureRewardsAndPolling) &&
                    _agentGroups[eater.Id] != _agentGroups[i])
                {
                    continue;
                }

                // Only learn from high-valued actions
                //if (TeachParadigm == TeachingParadigm.SubcultureRewardFiltering
                //    && eaten.Species.Reward < _rewardThreshold
                //    && _rewards.Count > 20)
                //    //return;
                //    continue;

                // Teach the teacher to act like the eater
                if (agent.AcceptabilityFn.Accept(eater.Memory))
                {
                    TeachAgent(eater, agent);
                }
            }
            //}
        }