Exemple #1
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");
        }
Exemple #2
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.AssignedDecisionOptions = prototype.DecisionOptions.Where(r => agentConfiguration.AssignedDecisionOptions.Contains(r.Id)).ToList();
            agent.AssignedGoals           = prototype.Goals.Where(g => agentConfiguration.AssignedGoals.Contains(g.Name)).ToList();

            agent.AssignedDecisionOptions.ForEach(kh => agent.DecisionOptionActivationFreshness.Add(kh, 1));

            //generate goal importance
            agentConfiguration.GoalsState.ForEach(kvp =>
            {
                var goalName      = kvp.Key;
                var configuration = kvp.Value;

                var goal = agent.AssignedGoals.FirstOrDefault(g => g.Name == goalName);
                if (goal == null)
                {
                    return;
                }

                double importance = configuration.Importance;

                if (configuration.Randomness)
                {
                    if (string.IsNullOrEmpty(configuration.BasedOn))
                    {
                        var from = (int)(configuration.RandomFrom * 10);
                        var to   = (int)(configuration.RandomTo * 10);

                        importance = GenerateImportance(agent, configuration.RandomFrom, configuration.RandomTo);
                    }
                    else
                    {
                        var anotherGoalImportance = agent.InitialGoalStates[agent.AssignedGoals.FirstOrDefault(g => g.Name == configuration.BasedOn)]
                                                    .Importance;

                        importance = Math.Round(1 - anotherGoalImportance, 2);
                    }
                }

                GoalState goalState = new GoalState(configuration.Value, goal.FocalValue, importance);

                agent.InitialGoalStates.Add(goal, goalState);

                agent[string.Format("{0}_Importance", goal.Name)] = importance;
            });

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

                if (kh.AutoGenerated && agent.Prototype.DoNothingAnticipatedInfluence != null)
                {
                    source = agent.Prototype.DoNothingAnticipatedInfluence;
                }
                else
                {
                    agentConfiguration.AnticipatedInfluenceState.TryGetValue(kh.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(kh, inner);
            });


            InitializeDynamicvariables(agent);

            agent.AgentStateConfiguration = agentConfiguration;

            return(agent);
        }
Exemple #3
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 <Common.Entities.Agent> >();

            //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];
                var mentalProto          = prototype.MentalProto;
                int index = 1;

                agentStateGroup.ForEach((agentState) =>
                {
                    for (int i = 0; i < agentState.NumberOfAgents; i++)
                    {
                        Common.Entities.Agent agent = Agent.CreateAgent(agentState, prototype);
                        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, agentPrototypes.Select(kvp => kvp.Value).ToList());
        }