Esempio n. 1
0
        public bool moveGameState(WSum optimal)
        {
            var agentsListPair = optimal.sumList;
            var prob           = _probs;

            for (int i = 0; i < agentsListPair.Count; i++)
            {
                var element = (from sublist in prob
                               from item in sublist.agentsVector
                               where item.Key.Id == agentsListPair[i].Key.Id &&
                               item.Value.action.id == agentsListPair[i].Value.Key.id &&
                               item.Value.stateBefore.id == agentsListPair[i].Value.Value.id
                               select sublist).ToList();
                prob = null;
                prob = element;
            }

            double sum         = prob.Sum(x => x.probTotal);
            double probability = getRandomNumber(0, sum);
            double cumulative  = 0.0;
            Probs  result      = null;

            foreach (var strat in prob)
            {
                cumulative += strat.probTotal;
                if (probability < cumulative)
                {
                    result = strat;
                    break;
                }
            }
            if (result == null)
            {
                return(false);
            }
            foreach (var coalition in _coalitions)
            {
                foreach (var agent in coalition.coalitionMembers)
                {
                    var agentActionStatePair = agentsListPair.Find(x => x.Key.Id == agent.Id).Value;
                    agent.totalReward  += agentActionStatePair.Key.reward;
                    agent._currentState = result.agentsVector.Find(x => x.Key.Id == agent.Id).Value.stateAfter;
                }
            }

            return(true);
        }
Esempio n. 2
0
        private void probsRecursion(List <Agent> agentsList, List <KeyValuePair <Agent, TransferPair> > agentsPRList)
        {
            if (agentsList.Count == 0)
            {
                return;
            }
            if (agentsList.Count == 1) //Last agent in list
            {
                foreach (AgentState lastAgentState in agentsList.FirstOrDefault().statesList)
                {
                    foreach (AgentAction lastAgnetAction in agentsList.FirstOrDefault().actionsList)
                    {
                        foreach (AgentState lastAgentNextState in agentsList.FirstOrDefault().statesList)
                        {
                            var rewardsBeforeAgents = new List <KeyValuePair <Agent, TransferPair> >();
                            foreach (var kp in agentsPRList)
                            {
                                rewardsBeforeAgents.Add(kp);
                            }
                            var currentRew = agentsList.FirstOrDefault().probList.FindAll(x => x.action.action.Equals(lastAgnetAction.action))
                                             .Find(y => y.nextState.state.Equals(lastAgentNextState.state));

                            var lstKeyValuePair = new KeyValuePair <Agent, TransferPair>(agentsList.FirstOrDefault(),
                                                                                         new TransferPair(lastAgentState, lastAgnetAction, lastAgentNextState, currentRew.probability));

                            rewardsBeforeAgents.Add(lstKeyValuePair);
                            int nextindex = _probs.Count + 1;

                            double total = 0;
                            foreach (var ws in rewardsBeforeAgents)
                            {
                                if (ws.Equals(rewardsBeforeAgents.FirstOrDefault()))
                                {
                                    total = ws.Value.prob;
                                }
                                else
                                {
                                    total *= ws.Value.prob;
                                }
                            }

                            Probs probs = new Probs("ProBS" + nextindex, total, rewardsBeforeAgents);
                            _probs.Add(probs);
                        }
                    }
                }
            }
            else
            {
                var  currentAgent    = agentsList.FirstOrDefault();
                bool secondIteration = false;
                foreach (var currentState in currentAgent.statesList)
                {
                    foreach (var currentAction in currentAgent.actionsList)
                    {
                        foreach (var nextState in currentAgent.statesList)
                        {
                            if (secondIteration)
                            {
                                var last = agentsPRList.LastOrDefault();
                                agentsPRList.Remove(last);
                            }

                            var currentRew = currentAgent.probList.FindAll(x => x.action.action.Equals(currentAction.action))
                                             .Find(y => y.nextState.state.Equals(nextState.state));

                            var lstKeyValuePair = new KeyValuePair <Agent, TransferPair>(agentsList.FirstOrDefault(),
                                                                                         new TransferPair(currentState, currentAction, nextState, currentRew.probability));


                            agentsPRList.Add(lstKeyValuePair);
                            agentsList.Remove(currentAgent);
                            probsRecursion(agentsList, agentsPRList);
                            secondIteration = true;
                        }
                    }
                }
            }
        }