public int policy_per_state(State main_node)
        {
            int main_node_index = main_node.state_no;
            //get all nodes which are connected to main index_state node
            List <int> edgeList = Ad_Graph.get_linked_nodes(main_node_index);

            double[] pi = new double[no_action];

            foreach (int connected_node_index in edgeList)
            {
                //State connected_node= Global_nodes.Find(x => x.state_no == connected_node_index); //use a map instead fo linear search for find
                State  connected_node       = Global_nodes[state_map[connected_node_index]];
                double connected_node_value = connected_node.state_value;

                for (int j = 0; j < no_action; j++)
                {
                    var trans = new Tuple <string, int, string>(main_node_index.ToString(), j + 1, connected_node_index.ToString()); //here so many keys wont be found , i can eithermake others transision as zero

                    if (trans_prob.ContainsKey(trans))
                    {
                        pi[j] += trans_prob[trans] * connected_node_value;
                    }
                }
            }


            double max = pi[0];
            int    pos = 0;

            for (int i = 0; i < pi.Length; i++)
            {
                if (pi[i] > max)
                {
                    max = pi[i]; pos = i;
                }
            }
            //Console.WriteLine("pi: {0} {1} {2}", pi[0],pi[1],pi[2]);

            return(pos + 1);
        }
Exemplo n.º 2
0
        public void state_flow(int start_state)
        {
            if (start_state == 0)
            {
                writer.WriteLine(string.Format("{0}", "0"));
                return;
            }

            List <int> edgeList    = Ad_Graph.get_linked_nodes(start_state);
            int        best_action = mdp_model[start_state.ToString()].Item2;

            Double prob           = 0;
            Double max_prob       = 0;
            int    state_max_prob = 0;

            //Console.WriteLine(start_state + " " + best_action);
            var row = string.Format("{0},{1}", start_state, best_action);

            writer.WriteLine(row);

            foreach (int connected_node_index in edgeList)                                                                         //calculate utility for each node
            {
                var trans = new Tuple <string, int, string>(start_state.ToString(), best_action, connected_node_index.ToString()); //here so many kets wont be found , i can eithermake others transision as zero

                if (trans_prob.ContainsKey(trans))
                {
                    prob = trans_prob[trans];

                    if (prob > max_prob)
                    {
                        max_prob       = prob;
                        state_max_prob = connected_node_index;
                    }
                }
            }


            state_flow(state_max_prob);
        }