social_learning.SocialExperiment.Initialize C# (CSharp) Method

Initialize() public method

Initialize the experiment with some optional XML configutation data.
public Initialize ( string name, XmlElement xmlConfig ) : void
name string
xmlConfig System.Xml.XmlElement
return void
        public void Initialize(string name, XmlElement xmlConfig)
        {
            _name = name;
            _populationSize = XmlUtils.GetValueAsInt(xmlConfig, "PopulationSize");
            _specieCount = XmlUtils.GetValueAsInt(xmlConfig, "SpecieCount");
            _activationScheme = ExperimentUtils.CreateActivationScheme(xmlConfig, "Activation");
            _complexityRegulationStr = XmlUtils.TryGetValueAsString(xmlConfig, "ComplexityRegulationStrategy");
            _complexityThreshold = XmlUtils.TryGetValueAsInt(xmlConfig, "ComplexityThreshold");
            _description = XmlUtils.TryGetValueAsString(xmlConfig, "Description");
            _timeStepsPerGeneration = (ulong)XmlUtils.GetValueAsInt(xmlConfig, "TimeStepsPerGeneration");
            _stepReward = XmlUtils.GetValueAsInt(xmlConfig, "StepReward");
            _agentType =(AgentTypes) Enum.Parse(typeof(AgentTypes), XmlUtils.TryGetValueAsString(xmlConfig, "AgentType"));
            _plantLayout = (PlantLayoutStrategies)Enum.Parse(typeof(PlantLayoutStrategies), XmlUtils.TryGetValueAsString(xmlConfig, "PlantLayout"));
            _paradigm = (EvolutionParadigm)Enum.Parse(typeof(EvolutionParadigm), XmlUtils.TryGetValueAsString(xmlConfig, "EvolutionParadigm"));
            bool? diverse = XmlUtils.TryGetValueAsBool(xmlConfig, "LogDiversity");
            if (diverse.HasValue && diverse.Value)
                _logDiversity = true;
            if (_agentType == AgentTypes.Social)
            {
                var memSection = xmlConfig.GetElementsByTagName("Memory")[0] as XmlElement;
                _memory = (MemoryParadigm)Enum.Parse(typeof(MemoryParadigm), XmlUtils.TryGetValueAsString(memSection, "Paradigm"));
                SocialAgent.DEFAULT_MEMORY_SIZE = XmlUtils.GetValueAsInt(memSection, "Size");
                if (_memory == MemoryParadigm.IncrementalGrowth)
                {
                    _memGens = XmlUtils.GetValueAsInt(memSection, "GrowthGenerations");
                    _maxMemorySize = XmlUtils.GetValueAsInt(memSection, "MaxSize");
                }
                _teaching = (TeachingParadigm)Enum.Parse(typeof(TeachingParadigm), XmlUtils.TryGetValueAsString(xmlConfig, "TeachingParadigm"));
            }
            var species = new List<PlantSpecies>();

            var plants = xmlConfig.GetElementsByTagName("Plant");
            for (int i = 0; i < plants.Count; i++)
            {
                var plant = plants[i] as XmlElement;
                species.Add(new PlantSpecies(i)
                {
                    Name = XmlUtils.GetValueAsString(plant, "Name"),
                    Radius = XmlUtils.GetValueAsInt(plant, "Radius"),
                    Reward = XmlUtils.GetValueAsInt(plant, "Reward"),
                    Count = XmlUtils.GetValueAsInt(plant, "Count")
                });
            }
           
            Random random = new Random();
            var agents = new List<ForagingAgent>();
            const int NUM_AGENTS = 10;
            for (int i = 0; i < NUM_AGENTS; i++)
            {
                agents.Add(new SpinningAgent(i) { X = random.Next(500), Y = random.Next(500), Orientation = random.Next(360) });
            }

            List<Predator> predators = new List<Predator>();
            _predCount = XmlUtils.GetValueAsInt(xmlConfig, "Predators");
            var predStr = XmlUtils.TryGetValueAsString(xmlConfig, "PredatorDistribution");
            if (predStr != null)
                PredatorDistribution = (PredatorDistributionTypes)Enum.Parse(typeof(PredatorDistributionTypes), predStr, true);
            _predTypes = XmlUtils.GetValueAsInt(xmlConfig, "PredatorTypes");
            if (PredatorDistribution == PredatorDistributionTypes.Alternating)
                _predGens = XmlUtils.GetValueAsDouble(xmlConfig, "PredatorGenerations");
            _distinguishPreds = XmlUtils.GetValueAsBool(xmlConfig, "DistinguishPredators");

            _world = new World(agents, XmlUtils.GetValueAsInt(xmlConfig, "WorldHeight"), XmlUtils.GetValueAsInt(xmlConfig, "WorldHeight"), species, predators)
            {
                AgentHorizon = XmlUtils.GetValueAsInt(xmlConfig, "AgentHorizon"),
                PlantLayoutStrategy = _plantLayout,
                StepReward = _stepReward,
                PredatorTypes = _predTypes
            };

            var outputs = XmlUtils.TryGetValueAsInt(xmlConfig, "Outputs");
            var navigation = XmlUtils.TryGetValueAsBool(xmlConfig, "AgentsNavigate");
            var hiding = XmlUtils.TryGetValueAsBool(xmlConfig, "AgentsHide");
            _navigationEnabled = navigation.HasValue ? navigation.Value : false;
            _hidingEnabled = hiding.HasValue ? hiding.Value : false;
            if (!outputs.HasValue)
            {
                if (_navigationEnabled || _hidingEnabled)
                    _outputs = (_navigationEnabled ? 2 : 0) + (_hidingEnabled ? _predTypes + 1 : 0);
                else
                    _outputs = outputs.HasValue ? outputs.Value : 2;
            }
            else
                _outputs = outputs.Value;
            var inputs = XmlUtils.TryGetValueAsInt(xmlConfig, "Inputs");
            _inputs = inputs.HasValue ? inputs.Value : _world.PlantTypes.Count() * World.SENSORS_PER_OBJECT_TYPE + (_distinguishPreds ? _predTypes : 1) * World.SENSORS_PER_OBJECT_TYPE + 1;

            _eaParams = new NeatEvolutionAlgorithmParameters();
            _eaParams.SpecieCount = _specieCount;
            _neatGenomeParams = new NeatGenomeParameters()
            {
                ActivationFn = PlainSigmoid.__DefaultInstance
            };
            if (_teaching != TeachingParadigm.EgalitarianEvolvedAcceptability)
                _neatGenomeParams.InitialInterconnectionsProportion = 0.1;
        }

Usage Example

Example #1
0
        void RunExperiment(string XMLFile, string filename)
        {
            _filename = filename;
            _experiment = new SocialExperiment();

            // Write the header for the results file in CSV format.
            using (TextWriter writer = new StreamWriter(_filename))
                writer.WriteLine("Generation,Average,Best,Updates");

            using (TextWriter writer = new StreamWriter(_filename.Replace(".csv", "_diversity_after.csv")))
                writer.WriteLine("Generation,Orientation Variance,Velocity Variance");

            using (TextWriter writer = new StreamWriter(_filename.Replace(".csv", "_diversity_before.csv")))
                writer.WriteLine("Generation,Orientation Variance,Velocity Variance");

            // Load the XML configuration file
            XmlDocument xmlConfig = new XmlDocument();
            xmlConfig.Load(XMLFile);
            _experiment.Initialize("EgalitarianSocialLearning", xmlConfig.DocumentElement);
            _experiment.TrialId = _trialNum;

            // Create the evolution algorithm and attach the update event.
            _ea = _experiment.CreateEvolutionAlgorithm();
            _ea.UpdateScheme = new SharpNeat.Core.UpdateScheme(1);
            _ea.UpdateEvent += new EventHandler(_ea_UpdateEvent);

            _experiment.Evaluator.TrialId = _trialNum;
            _experiment.Evaluator.DiversityFile = _filename.Replace(".csv", "_diversity.csv");

            // Start algorithm (it will run on a background thread).
            _ea.StartContinue();
        }
All Usage Examples Of social_learning.SocialExperiment::Initialize