Ejemplo n.º 1
0
 def query_blockages(self, evidence_list):
     print_info("Printing probability that each of the edges is blocked:")
     blockage_nodes = [n for n in self.networkObjects if n.n_type == 'E']
     for bl_node in blockage_nodes:
         blockage_chances = self.enumerate_ask(bl_node, evidence_list)
         print_info("\tP(" + str(bl_node) + " Blockage = True) = " +
                    str(blockage_chances[0]))
Ejemplo n.º 2
0
 def add_agent(self, s_vertex):
     index = 1
     initial_state = AgentState(s_vertex, self.graph.get_people_array(),
                                self.k_value)
     ag = ValueIterationAgent(index, initial_state)
     print_info("ADDED AGENT TO ENVIRONMENT.")
     self.agent = ag
Ejemplo n.º 3
0
 def query_floodings(self, evidence_list):
     print_info(
         "Printing probability that each of the vertices is flooded:")
     flood_nodes = [n for n in self.networkObjects if n.n_type == 'V']
     for fl_node in flood_nodes:
         flood_chances = self.enumerate_ask(fl_node, evidence_list)
         print_info("\tP(" + str(fl_node) + " Flooding = True) = " +
                    str(flood_chances[0]))
Ejemplo n.º 4
0
 def print_changes(self):
     print_info("TIME IS: " + str(self.env_state.time))
     print_info("OUR VERTICES PEOPLE COUNT: ")
     people_arr = self.graph.get_people_array_with_shelter()
     print_info(str(people_arr))
     ppl_saved = sum(
         [v.ppl_count for v in self.graph.vertices if v.is_shelter()])
     print_info("PEOPLE SAVED: " + str(ppl_saved) + "/" +
                str(self.total_ppl))
     print_info("AGENT STATE: " + str(str(self.agent.curr_state)))
Ejemplo n.º 5
0
    def __init__(self, config_file_path, k_value=K_DEFAULT_VALUE):
        self.graph = Graph(config_file_path)
        self.grapCopy = deepcopy(self.graph)
        # self.env_time = 0
        # Array of the actual agents running in the environment
        self.agent = None
        self.agent_score = 0
        self.k_value = k_value  # Penalty related constant

        self.dead_ppl = 0
        self.total_ppl = sum(self.graph.get_people_array_with_shelter())

        s_vertex = self.graph.vertices[0]
        people_at_vertices_count = self.graph.get_people_array()
        edges_blocked_status = self.graph.get_edges_blocked_status()

        self.env_state = EnvState(s_vertex, people_at_vertices_count,
                                  edges_blocked_status, 0, 0, 0, False)

        Environment.PERCEPT = self

        self.all_possible_states = []
        self.stateUtilityAndPolicyDict = {}

        print_info("CREATED ENVIRONMENT WITH " +
                   str(self.graph.num_of_vertices()) + " VERTICES, AND " +
                   str(self.graph.num_of_roads()) + " ROADS.")
        print_info("OUR GRAPH:")
        print(str(self.graph))
        print_info("ENVIRONMENT STARTING STATE: " + str(self.env_state))
Ejemplo n.º 6
0
    def simulation(self):
        print("------------------------------------------------")
        self.initializeStatesDict()
        self.runValueIteration(Environment.VALUE_ITERATION_DELTA)
        dictCopy = deepcopy(self.stateUtilityAndPolicyDict)
        play = 'Y'
        while play == 'Y':
            self.stateUtilityAndPolicyDict = deepcopy(dictCopy)
            print_info("STARTING SIMULATION:")
            self.setRealEdgesStatus()
            self.add_agent(self.env_state.ag_loc)
            print_info("CREATED RANDOM INSTANCE OF ENVIRONMENT:")
            self.print_env()
            if not self.agent:
                self.print_env()
            else:
                ag = self.agent
                while not ag.curr_state.is_terminated:
                    if ag.is_traversing():
                        ag.traverse_update()
                    for v in self.graph.vertices:
                        if not v.is_shelter(
                        ) and v.deadline < self.env_state.time:
                            self.dead_ppl += v.ppl_count
                            v.ppl_count = 0
                    print_debug("PRINTING ENVIRONMENT STATUS:")
                    self.print_changes()
                    print_debug("AGENTS OPERATING IN ENVIRONMENT:")
                    self.update()
                    print_debug("DONE WITH AGENTS OPERATING IN ENVIRONMENT.")
                    self.env_state.time += 1
                    print("------------------------------------------------")

            print_debug("GAME OVER")
            print_info("PRINTING ENVIRONMENT STATUS:")
            self.print_env()
            print_query("Play again? (Y/N)")
            play = input()
            self.graph = deepcopy(self.grapCopy)
            self.initEnvironmentVariables()
Ejemplo n.º 7
0
    def simulation(self):
        evidenceList = []
        inp = 1
        while 1 <= inp <= 3:
            print_query("Enter what you want to do:")
            print_query("\t1. Add piece of evidence to evidence list.")
            print_query("\t2. Do probabilistic reasoning.")
            print_query("\t3. Reset evidence list to empty")
            print_query("\t4. Quit the program.")
            inp = int(input())

            if inp == 1:
                print_info("You chose to add evidence.")
                print_info("Examples of evidence input:")
                print_info("\tFlooding at vertex 1 at time 1 -> V11")
                print_info("\tEdge 2 not blocked at time 0 -> not E20")
                print_query("Enter evidence:")
                ev = str(input()).split()
                if len(ev) is 1:
                    evidenceVar = self.bayesNet.getBayesNodeByName(ev[0])
                    evidenceList.append(
                        bayesNetwork.VarWithVal(evidenceVar, True))
                else:
                    evidenceVar = self.bayesNet.getBayesNodeByName(ev[1])
                    evidenceList.append(
                        bayesNetwork.VarWithVal(evidenceVar, False))

            elif inp == 2:
                print_info(
                    "You chose to do probabilistic reasoning. Choose your query:"
                )
                print_query(
                    "\t1. What is the probability that each of the vertices is flooded?"
                )
                print_query(
                    "\t2. What is the probability that each of the edges is blocked?"
                )
                print_query(
                    "\t3. What is the probability that a certain path (set of edges) is free from blockages?"
                )
                print_query(
                    "\t4. BONUS: What is the path between 2 given vertices that has"
                    +
                    " the highest probability of being free from blockages at time t=1?"
                )
                print_query("\t5. Go back.")
                query_choice = int(input())
                print_info("Given our evidence: " +
                           str([str(ev) for ev in evidenceList]))
                if query_choice == 1:
                    self.bayesNet.query_floodings(evidenceList)
                elif query_choice == 2:
                    self.bayesNet.query_blockages(evidenceList)
                elif query_choice == 3:
                    print_query("Enter Path:")
                    print_query("\tFor example: E11 E21")
                    pathElements = str(input()).split()
                    path = []
                    for edge in pathElements:
                        pathElement = self.bayesNet.getBayesNodeByName(edge)
                        path.append(pathElement)

                    res = self.bayesNet.query_pathNotBlocked(
                        path, evidenceList)
                    print_info("P(Path not blocked = True) = " + str(res))
                elif query_choice == 4:
                    print_query("Enter 2 vertices:")
                    print_query("\tFor example: V1 V4")
                    pathElements = str(input()).split()
                    v1 = self.graph.get_vertex_from_string(pathElements[0])
                    v2 = self.graph.get_vertex_from_string(pathElements[1])
                    path, prob = self.bayesNet.getBestRoute(
                        v1, v2, evidenceList)
                    print_query("Best path is: " +
                                self.bayesNet.get_path_str(path) +
                                " with probability: " + str(prob))
                    X = 1
                    pass

            elif inp == 3:
                evidenceList = []
Ejemplo n.º 8
0
 def printNodeInfo(self):
     if self.n_type == 'V':
         print_info("VERTEX " + str(self.index) + ", time " +
                    str(self.time) + ":")
         if not self.parents:
             print_info("\tP(Flooding = True) = " +
                        str(self.probabilityTable['1']))
             print_info("\tP(Flooding = False) = " +
                        str(1 - self.probabilityTable['1']))
         elif len(self.parents) == 1:
             print_info("\tP(Flooding = True | Flooding " +
                        str(self.parents[0]) + " = True) = " +
                        str(self.probabilityTable['1']))
             # print_info("\tP(Flooding = False | Flooding " + str(self.parents[0]) + " = True) = " +
             #            str(1 - self.probabilityTable['1']))
             print_info("\tP(Flooding = True | Flooding " +
                        str(self.parents[0]) + " = False) = " +
                        str(self.probabilityTable['0']))
             # print_info("\tP(Flooding = False | Flooding " + str(self.parents[0]) + " = False) = " +
             #            str(1 - self.probabilityTable['0']))
     else:  # 2 Parents
         print_info("EDGE " + str(self.index) + ", time " +
                    str(self.time) + ":")
         for pos_vals in [(True, True), (True, False), (False, True),
                          (False, False)]:
             print_info(
                 "\tP(Blockage = True | Flooding " +
                 str(self.parents[0]) + " = " + str(pos_vals[0]) +
                 ", Flooding " + str(self.parents[1]) + " = " +
                 str(pos_vals[1]) + ") = " +
                 str(self.probabilityTable[bin(pos_vals[0])[2:] +
                                           bin(pos_vals[1])[2:]]))
Ejemplo n.º 9
0
    def print_env(self):
        print_info("TIME IS: " + str(self.env_state.time))

        print_info("OUR VERTICES TYPES:")
        v_types = []
        for vertex in self.graph.vertices:
            v_types.append(vertex.v_type)
        print_info(str(v_types))

        print_info("OUR VERTICES DEADLINES:")
        deadlines = []
        for vertex in self.graph.vertices:
            deadlines.append(vertex.deadline)
        print_info(str(deadlines))

        print_info("OUR VERTICES PEOPLE COUNT: ")
        people_arr = self.graph.get_people_array_with_shelter()
        print_info(str(people_arr))

        print_info("OUR EDGES ACTUAL BLOCKAGE STATUS:")
        edges_actual_blockage_status = self.graph.get_edges_actual_blocked_status(
        )
        print_info(str(edges_actual_blockage_status))

        ppl_saved = sum(
            [v.ppl_count for v in self.graph.vertices if v.is_shelter()])
        print_info("PEOPLE SAVED: " + str(ppl_saved) + "/" +
                   str(self.total_ppl))

        print_info("OUR AGENTS:")
        ag_names = []
        ag_states = []
        agent = self.agent
        ag_names.append(str(agent))
        ag_states.append(str(agent.curr_state))
        print_info("AGENTS: " + str(ag_names))
        print_info("AGENTS SCORES: " + str(self.agent_score))
        print_info("AGENTS STATES: " + str(ag_states))
Ejemplo n.º 10
0
from environment import Environment as Env
from helper_funcs import print_debug, print_query, print_info

ENVIRONMENT_SETTINGS_FILE = "environment_example_forgrading.txt"

env = Env(ENVIRONMENT_SETTINGS_FILE)

print_info("OUR GRAPH:")
print_info(env.graph)

print("------------------------------------------------")
print_debug("STARTING SIMULATION:")
env.simulation()