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); }
public void playGame() { for (int iteration = 1; iteration <= _numberOfTurns; iteration++) { Console.WriteLine("Step number " + iteration); int index = 0; WSum optimal = null; bool error = false; bool loop = true; do { Console.WriteLine("\t Generation " + ++index); generateDataForStep(); countDataForSimplex(); logWSums(); optimal = doSimplex(); if (optimal != null) { error = moveGameState(optimal); } loop = optimal == null || !error; } while (loop); } }
private void wSumsRecursion(List <Agent> agentsList, List <KeyValuePair <Agent, KeyValuePair <AgentAction, AgentState> > > agentsWSList) { 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) { var rewardsBeforeAgents = new List <KeyValuePair <Agent, KeyValuePair <AgentAction, AgentState> > >(); foreach (var kp in agentsWSList) { rewardsBeforeAgents.Add(kp); } var lstKeyValuePair = new KeyValuePair <Agent, KeyValuePair <AgentAction, AgentState> > (agentsList.FirstOrDefault(), new KeyValuePair <AgentAction, AgentState>(lastAgnetAction, lastAgentState)); rewardsBeforeAgents.Add(lstKeyValuePair); int nextindex = _sums.Count + 1; int total = 0; foreach (var ws in rewardsBeforeAgents) { total += ws.Value.Key.reward; } WSum wsum = new WSum("WSUM" + nextindex, total, rewardsBeforeAgents); _sums.Add(wsum); } } } else { var currentAgent = agentsList.FirstOrDefault(); bool secondStateIter = false; foreach (var currentState in currentAgent.statesList) { bool secondIteration = false; foreach (var currentAction in currentAgent.actionsList) { if (secondIteration || secondStateIter) { var last = agentsWSList.LastOrDefault(); agentsWSList.Remove(last); } var currentKeyValuePair = new KeyValuePair <Agent, KeyValuePair <AgentAction, AgentState> > (currentAgent, new KeyValuePair <AgentAction, AgentState>(currentAction, currentState)); agentsWSList.Add(currentKeyValuePair); agentsList.Remove(currentAgent); wSumsRecursion(agentsList, agentsWSList); secondIteration = true; } secondStateIter = true; } } }