/// <summary>
        /// Creates agent instance based on agent archetype and agent configuration.
        /// </summary>
        /// <param name="agentConfiguration"></param>
        /// <param name="archetype"></param>
        /// <returns></returns>
        public static SosielHarvestAgent Create(
            AgentStateConfiguration agentConfiguration, AgentArchetype archetype, string name)
        {
            var agent = new SosielHarvestAgent();

            agent.Id                      = name;
            agent.Archetype               = archetype;
            agent.privateVariables        = new Dictionary <string, dynamic>(agentConfiguration.PrivateVariables);
            agent.AssignedDecisionOptions = archetype.DecisionOptions.Where(
                r => agentConfiguration.AssignedDecisionOptions.Contains(r.Name)).ToList();
            agent.AssignedGoals = archetype.Goals.Where(
                g => agentConfiguration.AssignedGoals.Contains(g.Name)).ToList();
            agent.AssignedDecisionOptions.ForEach(
                decisionOption => agent.DecisionOptionActivationFreshness.Add(decisionOption, 1));

            // Generates goal importance.
            agentConfiguration.GoalStates.ForEach(kvp =>
            {
                var goalName = kvp.Key;
                var config   = kvp.Value;
                var goal     = agent.AssignedGoals.FirstOrDefault(g => g.Name == goalName);
                if (goal != null)
                {
                    var goalState = new GoalState(goal, agent, config.Value, config.FocalValue, config.Importance,
                                                  config.MinValue, config.MaxValue, config.MinValueReference, config.MaxValueReference);
                    agent.InitialGoalStates.Add(goal, goalState);
                }
            });

            // Initializes initial anticipated influence for each kh and goal assigned to the agent
            agent.AssignedDecisionOptions.ForEach(decisionOption =>
            {
                Dictionary <string, double> source;
                if (decisionOption.AutoGenerated && agent.Archetype.DoNothingAnticipatedInfluence != null)
                {
                    source = agent.Archetype.DoNothingAnticipatedInfluence;
                }
                else
                {
                    agentConfiguration.AnticipatedInfluenceState.TryGetValue(decisionOption.Name, out source);
                }

                var 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(decisionOption, inner);
            });

            InitializeDynamicVariables(agent);
            agent.AgentStateConfiguration = agentConfiguration;
            return(agent);
        }
Example #2
0
        /// <summary>
        /// Executes agent initializing. It's the first initializing step.
        /// </summary>
        protected override void InitializeAgents()
        {
            // Debugger.Launch();
            _log.WriteLine("  SosielHarvestAlgorithm: Initializing agents...");
            var agents          = new List <IAgent>();
            var agentArchetypes = _configuration.AgentArchetypeConfiguration;

            if (agentArchetypes.Count == 0)
            {
                throw new SosielAlgorithmException("Agent archetypes are not defined. Please check configuration files");
            }

            // Create agents, groupby is used for saving agents numeration, e.g. FE1, HM1, HM2, etc.
            _configuration.InitialState.AgentStates.Where(s => s.Mode == _sheMode)
            .GroupBy(state => state.Archetype)
            .ForEach((agentStateGroup) =>
            {
                int index       = 1;
                var archetype   = agentArchetypes[agentStateGroup.Key];
                var mentalProto = archetype.MentalProto; //do not remove
                agentStateGroup.ForEach((agentState) =>
                {
                    for (var i = 0; i < agentState.NumberOfAgents; i++)
                    {
                        var name = agentState.Name;
                        if (string.IsNullOrEmpty(name) || agentState.NumberOfAgents > 1)
                        {
                            name = $"{agentState.Archetype}{index}";
                        }
                        var agent = SosielHarvestAgent.Create(agentState, archetype, name);
                        agents.Add(agent);
                        index++;
                    }
                });

                agents.ForEach(agent =>
                {
                    if (agent.ContainsVariable(AlgorithmVariables.Group))
                    {
                        agent.ConnectedAgents.AddRange(agents.Where(
                                                           a => a != agent && a.ContainsVariable(AlgorithmVariables.Group) &&
                                                           a[AlgorithmVariables.Group] == agent[AlgorithmVariables.Group]));
                    }
                });
            });

            agentList = new AgentList(agents, agentArchetypes.Select(kvp => kvp.Value).ToList());
            numberOfAgentsAfterInitialize = agentList.Agents.Count;
        }
 private static void InitializeDynamicVariables(SosielHarvestAgent agent)
 {
     agent[SosielVariables.IsActive] = true;
 }