public override IExpression Rewrite(IMethodCall methodCall)
            {
                var newSeq = new List<List<Reposition>>();
                var thisMethod = methodCall.MethodToCall.ResolvedMethod;
                var types = thisMethod.Parameters.Select(p => p.Type.ResolvedType).ToList();
                var groups = types.Select( (t, i) => new{t,i}).GroupBy(a => a.t, a=>a.i);

                newSeq.AddRange(from @group in groups 
                                select @group.RotateRight(1).Select((i, index) => 
                                    new Reposition(i, @group.ElementAt(index))).ToList());

                var newArgs = new IExpression[types.Count];
                foreach (var repos in newSeq.Flatten())
                {
                    newArgs[repos.NewPos] = methodCall.Arguments.ElementAt(repos.OldPos);

                }
                return new MethodCall(methodCall)
                    {
                        Arguments = newArgs.ToList()
                    };
            }
Example #2
0
        private double ExpectedValue(double[] s1, double[] s2, Node cur, int player, double prob, List<double> allProbs)
        {
            if (prob < double.Epsilon)
                return 0;

            if (cur.terminal)
                return cur.payoff * prob;

            double[] s = player == 1 ? s1 : s2;

            //Debugging traces through the game tree
            for (int i = 0; i < cur.children.Length; i++)
            {
                if (!cur.children[i].terminal)
                    continue;

                allProbs.Add(s[cur.children[i].index]);
                Console.WriteLine("{0} -> {1} Opp: {2} Prob={3} Payoff={4} [{5}]", cur.playerView(), cur.children[i].action, cardToChar(cur.OpponentCard()), allProbs.Product(), cur.children[i].payoff, allProbs.Flatten(", "));
                using (TextWriter writer = new StreamWriter(LOG_FILENAME, true))
                    writer.WriteLine("P1: {0} P2: {1} Board: {2} {3}{4} Prob: {5:N6} Payoff: {6}",
                                        cardToChar(cur.hole1),
                                        cardToChar(cur.hole2),
                                        cardToChar(cur.board),
                                        cur.sequence,
                                        actionToChar(cur.children[i].action),
                                        allProbs.Product(),
                                        cur.children[i].payoff,
                                        allProbs.Select(d => d.ToString("N9")).Flatten(", "));
                allProbs.RemoveAt(allProbs.Count - 1);
            }

            Debug.Assert(cur.children.Sum(n => s[n.index]) >= 0.9999, string.Format("Failed {0}: {1}", cur.index, cur.children.Sum(n => s[n.index])));
            Debug.Assert(cur.children.Sum(n => s[n.index]) <= 1.0001, string.Format("Failed {0}: {1}", cur.index, cur.children.Sum(n => s[n.index])));

            double ev = 0;
            int nextPlayer = player == 1 ? 2 : 1;
            foreach (var child in cur.children)
            {
                int lookup = child.index;
                if (s[lookup] > double.Epsilon)
                {
                    allProbs.Add(s[lookup]);
                    if (child.round == Rounds.Flop && cur.round == Rounds.Preflop)
                        ev += ExpectedValue(s1, s2, child, 1, prob * s[lookup], allProbs);
                    else
                        ev += ExpectedValue(s1, s2, child, nextPlayer, prob * s[lookup], allProbs);
                    allProbs.RemoveAt(allProbs.Count - 1);
                }
            }
            return ev;
        }