Exemplo n.º 1
0
        /// <summary>
        /// Creates an activated/matched rules history
        /// </summary>
        /// <param name="configuration"></param>
        /// <param name="agent"></param>
        /// <returns></returns>
        private static RuleHistory CreateRuleHistory(InitialStateConfiguration configuration, IAgent agent)
        {
            RuleHistory history = new RuleHistory();

            if (configuration.RandomlySelectRule)
            {
                agent.AssignedRules.Where(r => r.IsAction && r.IsCollectiveAction == false).GroupBy(r => new { r.RuleSet, r.RuleLayer })
                .ForEach(g =>
                {
                    Rule selectedRule = g.RandomizeOne();

                    history.Matched.Add(selectedRule);
                    history.Activated.Add(selectedRule);
                });
            }
            else
            {
                Rule[] firstIterationsRule = agent.InitialStateConfiguration.ActivatedRulesOnFirstIteration.Select(rId => agent.AssignedRules.First(ar => ar.Id == rId)).ToArray();

                history.Matched.AddRange(firstIterationsRule);
                history.Activated.AddRange(firstIterationsRule);
            }

            return(history);
        }
Exemplo n.º 2
0
        /// <inheritdoc />
        protected override void InitializeAgents()
        {
            var agents = new List <IAgent>();

            Dictionary <string, AgentPrototype> agentPrototypes = _configuration.AgentConfiguration;

            if (agentPrototypes.Count == 0)
            {
                throw new SosielAlgorithmException("Agent prototypes were not defined. See configuration file");
            }

            InitialStateConfiguration initialState = _configuration.InitialState;

            var networks = new Dictionary <string, List <SOSIEL.Entities.Agent> >();


            //create agents, groupby is used for saving agents numeration, e.g. CEA1, FE1, HM1. HM2 etc
            initialState.AgentsState.GroupBy(state => state.PrototypeOfAgent).ForEach((agentStateGroup) =>
            {
                prototype = agentPrototypes[agentStateGroup.Key];

                var mentalProto = prototype.MentalProto;

                agentStateGroup.ForEach((agentState) =>
                {
                    for (int i = 0; i < agentState.NumberOfAgents; i++)
                    {
                        Agent agent = Agent.CreateAgent(agentState, prototype);
                        agent.SetId(generalAgentIndex);
                        agent.IsActive = true;

                        agentState.PrivateVariables.ForEach((privateVariable) => {
                            if (privateVariable.Value == "Sharer")
                            {
                                agent.Contrib = true;
                            }
                            else
                            {
                                agent.Contrib = false;
                            }
                        });
                        agents.Add(agent);

                        generalAgentIndex++;
                    }
                });
            });

            agentList = new AgentList(agents, agentPrototypes.Select(kvp => kvp.Value).ToList());
            Console.WriteLine("---Agents are initialized");
        }
Exemplo n.º 3
0
        /// <inheritdoc />
        protected override void InitializeAgents()
        {
            var agents = new List <IAgent>();

            Dictionary <string, AgentArchetype> agentArchetypes = _configuration.AgentConfiguration;

            if (agentArchetypes.Count == 0)
            {
                throw new SosielAlgorithmException("Agent archetypes were not defined. See configuration file");
            }

            InitialStateConfiguration initialState = _configuration.InitialState;

            var networks = new Dictionary <string, List <SOSIEL.Entities.Agent> >();

            //create agents, groupby is used for saving agents numeration, e.g. FE1, HM1. HM2 etc
            initialState.AgentsState.GroupBy(state => state.ArchetypeOfAgent).ForEach((agentStateGroup) =>
            {
                AgentArchetype archetype = agentArchetypes[agentStateGroup.Key];
                var mentalProto          = archetype.MentalProto;
                int index = 1;

                agentStateGroup.ForEach((agentState) =>
                {
                    for (int i = 0; i < agentState.NumberOfAgents; i++)
                    {
                        SOSIEL.Entities.Agent agent = Agent.CreateAgent(agentState, archetype);
                        agent.SetId(index);

                        agents.Add(agent);

                        networks.AddToDictionary((string)agent[AlgorithmVariables.Household], agent);
                        networks.AddToDictionary((string)agent[AlgorithmVariables.NuclearFamily], agent);

                        if (agent.ContainsVariable(AlgorithmVariables.ExternalRelations))
                        {
                            var externals = (string)agent[AlgorithmVariables.ExternalRelations];

                            foreach (var en in externals.Split(';'))
                            {
                                networks.AddToDictionary(en, agent);
                            }
                        }

                        //household and extended family are the same at the beginning
                        agent[AlgorithmVariables.ExtendedFamily] = new List <string>()
                        {
                            (string)agent[AlgorithmVariables.Household]
                        };

                        index++;
                    }
                });
            });

            //convert temp networks to list of connetcted agents
            networks.ForEach(kvp =>
            {
                var connectedAgents = kvp.Value;

                connectedAgents.ForEach(agent =>
                {
                    agent.ConnectedAgents.AddRange(connectedAgents.Where(a => a != agent).Except(agent.ConnectedAgents));
                });
            });


            agentList = new AgentList(agents, agentArchetypes.Select(kvp => kvp.Value).ToList());
        }
Exemplo n.º 4
0
        /// <summary>
        /// Factory method for initializing agents
        /// </summary>
        /// <param name="configuration"></param>
        /// <returns></returns>
        public virtual void Initialize(ConfigurationModel configuration)
        {
            Agents = new List <IAgent>();

            Dictionary <string, AgentPrototype> agentPrototypes = configuration.AgentConfiguration;

            if (agentPrototypes.Count == 0)
            {
                throw new SosielAlgorithmException("Agent prototypes were not defined. See configuration file");
            }


            InitialStateConfiguration initialState = configuration.InitialState;

            //add donothing rule if necessary
            agentPrototypes.ForEach(kvp =>
            {
                AgentPrototype prototype = kvp.Value;

                string prototypeName = kvp.Key;

                if (prototype.MentalProto.Any(set => set.Layers.Any(layer => layer.LayerConfiguration.UseDoNothing)))
                {
                    var added = prototype.AddDoNothingRules();

                    initialState.AgentsState.Where(aState => aState.PrototypeOfAgent == prototypeName).ForEach(aState =>
                    {
                        aState.AssignedRules = aState.AssignedRules.Concat(added).ToArray();
                    });
                }
            });

            //save prototypes to list
            Prototypes = new List <AgentPrototype>(agentPrototypes.Values);


            //create agents, groupby is used for saving agents numeration, e.g. FE1, HM1. HM2 etc
            initialState.AgentsState.GroupBy(state => state.PrototypeOfAgent).ForEach((agentStateGroup) =>
            {
                AgentPrototype prototype = agentPrototypes[agentStateGroup.Key];

                int index = 1;

                Dictionary <string, List <Agent> > networks = agentStateGroup.SelectMany(state => state.SocialNetwork ?? new string[0]).Distinct()
                                                              .ToDictionary(network => network, network => new List <Agent>());

                agentStateGroup.ForEach((agentState) =>
                {
                    Agent agent = Agent.CreateAgent(agentState, prototype);

                    for (int i = 0; i < agentState.NumberOfAgents; i++)
                    {
                        Agent newAgent = agent.Clone();

                        agent.SetId(index);

                        Agents.Add(agent);

                        //check social network and add to temp dictionary
                        if (agentState.SocialNetwork != null)
                        {
                            //set first network to agent variables as household
                            agent[VariablesUsedInCode.Household] = agentState.SocialNetwork.First();

                            agentState.SocialNetwork.ForEach((network) => networks[network].Add(agent));
                        }

                        index++;
                    }
                });


                //convert temp networks to list of connetcted agents
                networks.ForEach(kvp =>
                {
                    List <Agent> agents = kvp.Value;

                    agents.ForEach(agent =>
                    {
                        agent.ConnectedAgents.AddRange(agents.Where(a => a != agent).Cast <IAgent>());
                    });
                });
            });
        }