Exemple #1
0
        /// <summary>
        /// Try to get the length of arguments of a method
        /// </summary>
        /// <param name="pyObject">Object representing a method</param>
        /// <param name="length">Lenght of arguments</param>
        /// <returns>True if pyObject is a method</returns>
        private static bool TryGetArgLength(PyObject pyObject, out long length)
        {
            using (Py.GIL())
            {
                var inspect = lazyInspect.Value;
                if (inspect.isfunction(pyObject))
                {
                    var args   = inspect.getargspec(pyObject).args as PyObject;
                    var pyList = new PyList(args);
                    length = pyList.Length();
                    pyList.Dispose();
                    args.Dispose();
                    return(true);
                }

                if (inspect.ismethod(pyObject))
                {
                    var args   = inspect.getargspec(pyObject).args as PyObject;
                    var pyList = new PyList(args);
                    length = pyList.Length() - 1;
                    pyList.Dispose();
                    args.Dispose();
                    return(true);
                }
            }
            length = 0;
            return(false);
        }
Exemple #2
0
        public void TestEmptyCtor()
        {
            var s = new PyList();

            Assert.IsInstanceOf(typeof(PyList), s);
            Assert.AreEqual(0, s.Length());
        }
Exemple #3
0
 public static void LoadDefaultArgs()
 {
     using (new PythonEngine())
         using (var argv = new PyList(Runtime.Runtime.PySys_GetObject("argv")))
         {
             Assert.AreNotEqual(0, argv.Length());
         }
 }
Exemple #4
0
        public void TestPyObjectCtor()
        {
            var a = new PyList();
            var s = new PyList(a);

            Assert.IsInstanceOf(typeof(PyList), s);
            Assert.AreEqual(0, s.Length());
        }
Exemple #5
0
 public static void LoadDefaultArgs()
 {
     using (new PythonEngine())
     {
         // PySys_GetObject get a borrowed reference, it just no need for dispose
         var argv = new PyList(Runtime.Runtime.PySys_GetObject("argv"));
         Assert.AreNotEqual(0, argv.Length());
     }
 }
Exemple #6
0
        public void TestInsert()
        {
            var ai = new PyObject[] { new PyInt(3), new PyInt(2), new PyInt(1) };
            var s  = new PyList(ai);

            s.Insert(0, new PyInt(4));

            Assert.AreEqual(4, s.Length());
            Assert.AreEqual("4", s[0].ToString());
        }
Exemple #7
0
        public void ToPyList()
        {
            var list = new PyList();

            list.Append("hello".ToPython());
            list.Append("world".ToPython());
            var back = list.ToPython().As <PyList>();

            Assert.AreEqual(list.Length(), back.Length());
        }
Exemple #8
0
        public void TestPyObjectArrayCtor()
        {
            var ai = new PyObject[] { new PyInt(3), new PyInt(2), new PyInt(1) };
            var s  = new PyList(ai);

            Assert.IsInstanceOf(typeof(PyList), s);
            Assert.AreEqual(3, s.Length());
            Assert.AreEqual("3", s[0].ToString());
            Assert.AreEqual("2", s[1].ToString());
            Assert.AreEqual("1", s[2].ToString());
        }
Exemple #9
0
        public void TestSort()
        {
            var ai = new PyObject[] { new PyInt(3), new PyInt(1), new PyInt(2) };
            var s  = new PyList(ai);

            s.Sort();

            Assert.AreEqual(3, s.Length());
            Assert.AreEqual("1", s[0].ToString());
            Assert.AreEqual("2", s[1].ToString());
            Assert.AreEqual("3", s[2].ToString());
        }
        private static double[,] ConvertSimilarityPythonListToArray(PyList similarityList)
        {
            var length       = similarityList.Length();
            var similarities = new double[length, length];

            for (int i = 0; i < length; i++)
            {
                var columnRows = PyList.AsList(similarityList[i]);
                var row        = (double[])columnRows.AsManagedObject(typeof(double[]));
                for (int j = 0; j < length; j++)
                {
                    similarities[i, j] = row[j];
                }
            }

            return(similarities);
        }
Exemple #11
0
        private void DQN(object sender, RoutedEventArgs e)
        {
            bool side     = true; //own side
            int  numGames = 5000;

            using (Py.GIL())
            {
                dynamic dqn   = Py.Import("simple_dqn.dqn");
                dynamic Agent = dqn.Agent;

                Console.WriteLine("Done importing");
                PyInt   width      = new PyInt(458);
                dynamic state_size = new PyTuple(new PyObject[] { width });
                dynamic agent      = Agent(Py.kw("state_size", state_size), Py.kw("discount", 0.95));
                Console.WriteLine(agent.discount);

                for (int i = 0; i < numGames; i++)
                {
                    PyFloat epsilon = new PyFloat(Math.Max(0.1, 1 - (double)(i + 1) * 4 / numGames));
                    agent.epsilon = epsilon;
                    if (!isInit)
                    {
                        Init(true, i);
                    }
                    Playfield state          = GameManager.Instance.mPlayfield;
                    int       result         = state.getGameResult();
                    float     total_cost     = 0.0f;
                    float     total_reward   = 0.0f;
                    float     reward         = 0.0f;
                    float     cost           = 0.0f;
                    int       num_move       = 0;
                    int       end_turn_count = 0;

                    agent.new_episode();
                    GreedyPlayer greedyPlayer = (GreedyPlayer)GameManager.Instance.playerFirst;

                    while (result == -1)
                    {
                        Macro moveTodo = null;
                        reward = 0.0f;
                        cost   = 0.0f;

                        if (state.isOwnTurn)
                        {
                            List <Macro> macroMoveList = greedyPlayer.getMacros(state);

                            macroMoveList.Add(null);

                            dynamic          cur_state_vector  = DNNEval.Instance.parsePlayfield(state, side);
                            List <Playfield> stateList         = new List <Playfield>();
                            PyList           state_vector_list = new PyList();
                            foreach (Macro macro in macroMoveList)
                            {
                                Playfield nextState = new Playfield(state);
                                if (macro != null)
                                {
                                    nextState.doMacroAction(macro);
                                    stateList.Add(nextState);
                                    reward = nextState.observerMoveReward();
                                }
                                else
                                {
                                    nextState.endTurn(false, false);
                                    nextState.drawTurnStartCard();
                                    //simulateEnemyTurn(nextState, false, true);
                                }
                                dynamic state_vector = DNNEval.Instance.parsePlayfield(nextState, side);
                                state_vector_list.Append(state_vector);
                            }
                            int action_index = agent.act(cur_state_vector, state_vector_list);
                            Helpfunctions.Instance.logg("Length:" + state_vector_list.Length());

                            moveTodo = macroMoveList[action_index];

                            if (moveTodo != null)
                            {
                                Helpfunctions.Instance.logg("Player 1 ##########Move##########");
                                //moveTodo.print();
                                state.doMacroAction(moveTodo);
                                reward = state.observerMoveReward();

                                num_move += 1;
                                cost      = agent.observe(reward);
                                Helpfunctions.Instance.logg("action_index = " + action_index + "/" + (macroMoveList.Count - 1) + "reward = " + reward);
                            }
                            else
                            {
                                if (state.playerFirst.mana == state.playerFirst.ownMaxMana)
                                {
                                    end_turn_count++;
                                }
                                Helpfunctions.Instance.logg("Player 1 ##########Move##########");
                                Helpfunctions.Instance.logg("Player 1 end turn");
                                state.endTurn(false, false);
                                state.drawTurnStartCard();

                                dynamic end_state_vector = DNNEval.Instance.parsePlayfield(state, side);
                                agent.update_transition(end_state_vector);
                                reward = 0.0f;

                                num_move += 1;
                                cost      = agent.observe(reward);
                                Helpfunctions.Instance.logg("action_index = " + action_index + "/" + (macroMoveList.Count - 1) + "reward = " + reward);

                                total_reward += simulateEnemyTurn(state, false, false, agent);
                                Helpfunctions.Instance.logg("Player 2 end turn");
                            }

                            //state.printBoard();

                            total_cost   += cost;
                            total_reward += reward;
                            result        = state.getGameResult();
                        }
                        else
                        {
                            int debug = 1;
                        }
                    }

                    if (result == 0)
                    {
                        int debug = 1;
                    }

                    isInit = false;
                    Helpfunctions.Instance.logg("last reward: " + reward);
                    Helpfunctions.Instance.logg("total reward: " + total_reward);
                    Helpfunctions.Instance.logg("avg cost: " + total_cost / num_move);
                    Helpfunctions.Instance.WriteResultToFile(@"\learning_log.txt", "No." + i + " total/avg cost: " + total_reward + ", " + total_cost / num_move + ", end turn: " + (float)end_turn_count / num_move);
                }
            }


            //for e in xrange(num_episodes):
            //    observation = env.reset()
            //    done = False
            //    agent.new_episode()
            //    total_cost = 0.0
            //    total_reward = 0.0
            //    frame = 0
            //    while not done:
            //        frame += 1
            //        #env.render()
            //        action, values = agent.act(observation)
            //        #action = env.action_space.sample()
            //        observation, reward, done, info = env.step(action)
            //        print reward
            //        total_cost += agent.observe(reward)
            //        total_reward += reward
            //    print "total reward", total_reward
            //    print "mean cost", total_cost/frame
        }
Exemple #12
0
        private void DQNTurn(object sender, RoutedEventArgs e)
        {
            bool side     = true; //own side
            int  numGames = 500;

            using (Py.GIL())
            {
                dynamic dqn   = Py.Import("simple_dqn.dqn");
                dynamic Agent = dqn.Agent;

                Console.WriteLine("Done importing");
                PyInt   width      = new PyInt(458);
                dynamic state_size = new PyTuple(new PyObject[] { width });
                dynamic agent      = Agent(Py.kw("state_size", state_size), Py.kw("discount", 0.95));
                Console.WriteLine(agent.discount);

                for (int i = 0; i < numGames; i++)
                {
                    if (!isInit)
                    {
                        Init(true, i);
                    }
                    Playfield state          = GameManager.Instance.mPlayfield;
                    int       result         = state.getGameResult();
                    float     total_cost     = 0.0f;
                    float     total_reward   = 0.0f;
                    float     reward         = 0.0f;
                    float     cost           = 0.0f;
                    int       num_move       = 0;
                    int       end_turn_count = 0;

                    agent.new_episode();

                    while (result == -1)
                    {
                        Action moveTodo = null;
                        reward = 0.0f;
                        cost   = 0.0f;

                        if (state.isOwnTurn)
                        {
                            List <Action> moveList = Movegenerator.Instance.getMoveList(state, false, true, true, 0.0);
                            moveList.Add(null);

                            dynamic          cur_state_vector  = DNNEval.Instance.parsePlayfield(state, side);
                            List <Playfield> stateList         = new List <Playfield>();
                            PyList           state_vector_list = new PyList();
                            foreach (Action action in moveList)
                            {
                                Playfield nextState = new Playfield(state);
                                if (action != null)
                                {
                                    nextState.doAction(action);
                                    stateList.Add(nextState);
                                    reward = nextState.observerMoveReward();
                                }
                                else
                                {
                                    nextState.endTurn(false, false);
                                    nextState.drawTurnStartCard();
                                    //simulateEnemyTurn(nextState, false, true, agent);
                                }
                                dynamic state_vector = DNNEval.Instance.parsePlayfield(nextState, side);
                                state_vector_list.Append(state_vector);
                            }
                            int action_index = agent.act(cur_state_vector, state_vector_list);
                            Helpfunctions.Instance.logg("Length:" + state_vector_list.Length());

                            moveTodo = moveList[action_index];

                            if (moveTodo != null)
                            {
                                Helpfunctions.Instance.logg("Player 1 ##########Move##########");
                                moveTodo.print();
                                state.doAction(moveTodo);
                                reward = state.observerMoveReward();
                            }
                            else
                            {
                                if (state.playerFirst.mana == state.playerFirst.ownMaxMana)
                                {
                                    end_turn_count++;
                                }
                                Helpfunctions.Instance.logg("Player 1 ##########Move##########");
                                Helpfunctions.Instance.logg("Player 1 end turn");
                                state.endTurn(false, false);
                                state.drawTurnStartCard();
                                reward        = 0.0f;
                                total_reward += simulateEnemyTurn(state, false, false, agent);
                                Helpfunctions.Instance.logg("Player 2 end turn");
                            }

                            //state.printBoard();

                            num_move += 1;

                            cost = agent.observe(reward);
                            Helpfunctions.Instance.logg("reward = " + reward);
                            total_cost   += cost;
                            total_reward += reward;
                            result        = state.getGameResult();
                        }
                        else
                        {
                            int debug = 1;
                        }
                    }

                    if (result == 0)
                    {
                        int debug = 1;
                    }

                    isInit = false;
                    Helpfunctions.Instance.logg("last reward: " + reward);
                    Helpfunctions.Instance.logg("total reward: " + total_reward);
                    Helpfunctions.Instance.logg("avg cost: " + total_cost / num_move);
                    Helpfunctions.Instance.WriteResultToFile(@"\learning_log.txt", "No." + i + " total/avg cost: " + total_reward + ", " + total_cost / num_move + ", end turn: " + (float)end_turn_count / num_move);
                }
            }
        }