Beispiel #1
0
        /// <summary>
        /// Creates agent instance based on agent prototype and agent configuration
        /// </summary>
        /// <param name="agentConfiguration"></param>
        /// <param name="prototype"></param>
        /// <returns></returns>
        public static Agent CreateAgent(AgentStateConfiguration agentConfiguration, AgentPrototype prototype)
        {
            Agent agent = new Agent();

            agent.Prototype        = prototype;
            agent.privateVariables = new Dictionary <string, dynamic>(agentConfiguration.PrivateVariables);

            agent.AssignedRules = prototype.Rules.Where(r => agentConfiguration.AssignedRules.Contains(r.Id)).ToList();
            agent.AssignedGoals = prototype.Goals.Where(g => agentConfiguration.AssignedGoals.Contains(g.Name)).ToList();

            agent.AssignedRules.ForEach(rule => agent.RuleActivationFreshness.Add(rule, 1));


            //initializes initial anticipated influence for each rule and goal assigned to the agent
            agent.AssignedRules.ForEach(r =>
            {
                Dictionary <string, double> source;

                if (r.AutoGenerated && agent.Prototype.DoNothingAnticipatedInfluence != null)
                {
                    source = agent.Prototype.DoNothingAnticipatedInfluence;
                }
                else
                {
                    agentConfiguration.AnticipatedInfluenceState.TryGetValue(r.Id, out source);
                }


                Dictionary <Goal, double> inner = new Dictionary <Goal, double>();

                agent.AssignedGoals.ForEach(g =>
                {
                    inner.Add(g, source != null && source.ContainsKey(g.Name) ? source[g.Name] : 0);
                });

                agent.AnticipationInfluence.Add(r, inner);
            });


            agent.InitialStateConfiguration = agentConfiguration;

            return(agent);
        }
Beispiel #2
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>());
                    });
                });
            });
        }