コード例 #1
0
ファイル: FoeQ.cs プロジェクト: xh2310/MultiAgentQLearning
            public static double GetValue(State s, FoeQTable q)
            {
                var context = SolverContext.GetContext();
                var model   = context.CreateModel();

                var actionDecisions = new List <Decision>();

                foreach (var action in Enum.GetNames(typeof(Action)))
                {
                    var decision = new Decision(Domain.RealNonnegative, action);
                    model.AddDecision(decision);
                    actionDecisions.Add(decision);
                }

                var valueDecision = new Decision(Domain.Real, "value");

                model.AddDecision(valueDecision);

                model.AddConstraint("probSumConst", actionDecisions[0] + actionDecisions[1] + actionDecisions[2] + actionDecisions[3] + actionDecisions[4] == 1.0);

                int constCount = 0;

                foreach (Action oppPlayer in Enum.GetValues(typeof(Action)))
                {
                    var qConstraintValues = new List <double>();

                    foreach (Action currentPlayer in Enum.GetValues(typeof(Action)))
                    {
                        qConstraintValues.Add(q.GetQValue(s, currentPlayer, oppPlayer));
                    }

                    model.AddConstraint("Const" + constCount, qConstraintValues[0] * actionDecisions[0] + qConstraintValues[1] * actionDecisions[1] + qConstraintValues[2] * actionDecisions[2] + qConstraintValues[3] * actionDecisions[3] + qConstraintValues[4] * actionDecisions[4] >= valueDecision);

                    ++constCount;
                }

                model.AddGoal("MaximizeV", GoalKind.Maximize, valueDecision);

                context.Solve(new SimplexDirective());

                context.ClearModel();

                return(valueDecision.GetDouble());
            }
コード例 #2
0
ファイル: Program.cs プロジェクト: xh2310/MultiAgentQLearning
        private static void RunFoeQ()
        {
            var S   = new StateSet();
            var A   = new JointActionSet();
            var P   = new Transition(S, A);
            var Q_A = new FoeQTable();
            var R   = new Rewards();

            var ERR = new Dictionary <int, double>();

            var j = 0;

            while (j <= 1000000)
            {
                //Initialize state according to Figure 4
                var initialState = new State(2, 1, BallPossessor.B);
                var gameOver     = false;
                var currentState = new State(3, 1, BallPossessor.B);

                var actions = A.GetNextJointAction();

                var playerAAction = actions.CurrentPlayerAction;
                var playerBAction = actions.OpposingPlayerAction;

                while (!gameOver)
                {
                    if (j % 20000 == 0)
                    {
                        Console.WriteLine(j);
                    }

                    var q_fig_4_initial = Q_A.GetQValue(initialState, Action.South, Action.Stick);

                    var nextState = P.GetNextState(currentState, new JointAction(playerAAction, playerBAction));

                    var playerAReward = R.GetPlayerAReward(nextState);
                    var playerBReward = R.GetPlayerBReward(nextState);

                    if (playerAReward != 0.0 || playerBReward != 0.0)
                    {
                        gameOver = true;
                    }

                    Q_A.UpdateQValue(currentState, nextState, playerAAction, playerBAction, playerAReward);

                    if (currentState.Equals(initialState) && playerAAction == Action.South && playerBAction == Action.Stick)
                    {
                        var q_fig_4 = Q_A.GetQValue(initialState, Action.South, Action.Stick);
                        var diff    = Math.Abs(q_fig_4 - q_fig_4_initial);

                        ERR.Add(j, diff);
                    }

                    currentState = nextState;

                    actions = A.GetNextJointAction();

                    playerAAction = actions.CurrentPlayerAction;
                    playerBAction = actions.OpposingPlayerAction;

                    ++j;
                }
            }

            using (StreamWriter sw = File.CreateText("output.csv"))
            {
                foreach (var kvp in ERR)
                {
                    sw.WriteLine(kvp.Key + "," + kvp.Value);
                }
            }
        }