def test_SimpleGraph2(self):
        text_obj = Text("data/simple-graph.tml", inverse=False, closure=False)

        A = text_obj.events[0]
        B = text_obj.events[1]
        C = text_obj.events[2]

        # B-before-C will become B-after-C. Why? Becuase the relation has to exist due to the constraints and it doesn't make the graph invalid
        B_C_after = Relation("", text_obj, B, C, RelationType.AFTER)

        result = [text_obj.relations[0], B_C_after, text_obj.relations[2]]

        constraints = Constraints(text_obj, test=True)

        # A--before 0.9--B, B--before 0.6--C, A--after 0.7--C
        A_B = Directed_Pair(A, B, [0.9, 0, 0], [0, 1, 2])
        B_C = Directed_Pair(B, C, [0.6, 0, 0], [0, 1, 2])
        A_C = Directed_Pair(A, C, [0, 0, 0.7], [0, 1, 2])

        constraints.directed_pairs = [A_B, B_C, A_C]
        constraints._build_model()

        best_set = constraints.get_best_set()

        self.assertEqual(result, best_set)
    def test_AnotherSimpleGraph_2(self):
        text_obj = Text("data/simple-graph-2.tml", inverse=False, closure=False)

        A = text_obj.events[0]
        B = text_obj.events[1]
        C = text_obj.events[2]

        A_C_before = Relation("", text_obj, A, C, RelationType.BEFORE)

        result = [text_obj.relations[0], text_obj.relations[1], A_C_before]

        constraints = Constraints(text_obj, test=True)

        #                                           A--before 0.1--C
        # A--simultaneous 0.5--B, B--before 0.5--C, A--after 0.5--C
        A_B = Directed_Pair(A, B, [0, 0, 0, 0, 0, 0, 0, 0, 0.5], [0, 1, 2, 3, 4, 5, 6, 7, 8])
        B_C = Directed_Pair(B, C, [0.5, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 2, 3, 4, 5, 6, 7, 8])
        A_C = Directed_Pair(A, C, [0.1, 0, 0.5, 0, 0, 0, 0, 0, 0], [0, 1, 2, 3, 4, 5, 6, 7, 8])

        constraints.directed_pairs = [A_B, B_C, A_C]
        constraints._build_model()

        best_set = constraints.get_best_set()

        self.assertEqual(result, best_set)
    def create_constraints_from_conflict(self, conflict):
        constraint_dict = {}
        if conflict.type == Conflict.VERTEX:
            v_constraint = VertexConstraint(conflict.time, conflict.location_1)
            constraint = Constraints()
            constraint.vertex_constraints |= {v_constraint}
            constraint_dict[conflict.agent_1] = constraint
            constraint_dict[conflict.agent_2] = constraint

        elif conflict.type == Conflict.EDGE:
            constraint1 = Constraints()
            constraint2 = Constraints()

            e_constraint1 = EdgeConstraint(conflict.time, conflict.location_1,
                                           conflict.location_2)
            e_constraint2 = EdgeConstraint(conflict.time, conflict.location_2,
                                           conflict.location_1)

            constraint1.edge_constraints |= {e_constraint1}
            constraint2.edge_constraints |= {e_constraint2}

            constraint_dict[conflict.agent_1] = constraint1
            constraint_dict[conflict.agent_2] = constraint2

        return constraint_dict
    def create_constraints_from_conflict(self, conflict):
        constraint_dict = {}
        if conflict.type == Conflict.VERTEX:
            v_constraint = VertexConstraint(conflict.time, conflict.location_1)
            constraint = Constraints()
            constraint.vertex_constraints |= {v_constraint}
            # if one of the agents is at its goal position, there is no reason that it should dodge
            if self.agent_dict[
                    conflict.agent_1]['goal'].location != conflict.location_1:
                constraint_dict[conflict.agent_1] = constraint
            if self.agent_dict[
                    conflict.agent_2]['goal'].location != conflict.location_1:
                constraint_dict[conflict.agent_2] = constraint

        elif conflict.type == Conflict.EDGE:
            constraint1 = Constraints()
            constraint2 = Constraints()

            e_constraint1 = EdgeConstraint(conflict.time, conflict.location_1,
                                           conflict.location_2)
            e_constraint2 = EdgeConstraint(conflict.time, conflict.location_2,
                                           conflict.location_1)

            constraint1.edge_constraints |= {e_constraint1}
            constraint2.edge_constraints |= {e_constraint2}

            constraint_dict[conflict.agent_1] = constraint1
            constraint_dict[conflict.agent_2] = constraint2

        return constraint_dict
    def apply_global_model(self):
        changed = 0
        no_changes = 0
        ee_changed = 0
        et_changed = 0
        improvement = 0
        degradation = 0
        changes_with_no_effect = 0

        for text_obj in self.text_objects:
            # Create all optimal relations for text object
            ilp = Constraints(text_obj)
            text_obj.relations_plain_optimized = ilp.get_best_set()
            self.relations_optimized += text_obj.relations_plain_optimized

            tmp_changed, tmp_ee_changed, tmp_et_changed, tmp_improvement, tmp_degradation, tmp_changes_with_no_effect = ilp.get_number_of_relations_changed()
            changed += tmp_changed
            ee_changed += tmp_ee_changed
            et_changed += tmp_et_changed
            improvement += tmp_improvement
            degradation += tmp_degradation
            changes_with_no_effect += tmp_changes_with_no_effect

        print "Global model changed %s relations" % changed
        print "%s relations were event-event relations" % ee_changed
        print "%s relations were event-timex relations" % et_changed
        print "The global model lead to %s improvements" % improvement
        print "The global model introduced %s new misclassifications" % degradation
        print "The global model changed %s relations wrongly which had no effect since they were already wrong" % changes_with_no_effect
Beispiel #6
0
    def __init__(self,
                 graph,
                 agents,
                 model_name,
                 volume_conflict_table,
                 min_cost_table=0):
        self.graph = graph
        self.volume_conflict_table = volume_conflict_table
        if min_cost_table != 0:
            self.min_cost_path_table = min_cost_table
        else:
            try:
                with open(model_name + '.mct', 'r') as f:
                    data = f.read()
                    if data != '':
                        self.min_cost_path_table = ast.literal_eval(data)
            except FileNotFoundError:
                dj = Dijkstra(graph)
                dj.traverse()
                self.min_cost_path_table = dj.paths
                with open(model_name + '.mct', 'w') as f:
                    f.write(str(self.min_cost_path_table))

        self.agents = agents
        self.agent_dict = {}
        self.make_agent_dict()

        self.constraints = Constraints()
        self.constraint_dict = {}

        self.a_star = AStar(self)
Beispiel #7
0
 def compute_schedule(self, agent):
     solution = {}
     self.constraints = self.constraint_dict.setdefault(
         agent, Constraints())
     local_solution = self.a_star.search(agent)
     if not local_solution:
         return False
     solution.update({agent: local_solution})
     return solution
Beispiel #8
0
 def compute_solution(self):
     solution = {}
     for agent in self.agent_dict.keys():
         self.constraints = self.constraint_dict.setdefault(
             agent, Constraints())
         local_solution = self.a_star.search(agent)
         if not local_solution:
             return False
         solution.update({agent: local_solution})
     return solution
    def __init__(self, dimension, agents, obstacles):
        self.dimension = dimension
        self.obstacles = obstacles

        self.agents = agents
        self.agent_dict = {}

        self.make_agent_dict()

        self.constraints = Constraints()
        self.constraint_dict = {}

        self.a_star = AStar(self)
    def test_SimpleGraph(self):
        text_obj = Text("data/simple-graph.tml", inverse=False, closure=False)

        A = text_obj.events[0]
        B = text_obj.events[1]
        C = text_obj.events[2]

        A_C_before = Relation("closure", text_obj, A, C, RelationType.BEFORE)

        result = [text_obj.relations[0], text_obj.relations[1], A_C_before]

        constraints = Constraints(text_obj, test=True)

        # A--before 0.9--B, B--before 0.9--C, A--after 0.7--C
        A_B = Directed_Pair(A, B, [0.9, 0, 0], [0, 1, 2])
        B_C = Directed_Pair(B, C, [0.9, 0, 0], [0, 1, 2])
        A_C = Directed_Pair(A, C, [0, 0, 0.7], [0, 1, 2])

        constraints.directed_pairs = [A_B, B_C, A_C]
        constraints._build_model()

        best_set = constraints.get_best_set()

        self.assertEqual(result, best_set)
    def search(self):
        # print('%s cbs search start' % datetime.now())
        start = HighLevelNode()
        # TODO: Initialize it in a better way
        start.constraint_dict = {}
        for agent in self.env.agent_dict.keys():
            start.constraint_dict[agent] = Constraints()
        start.solution = self.env.compute_solution()
        if not start.solution:
            return {}
        start.cost = self.env.compute_solution_cost(start.solution)

        self.open_set |= {start}

        while self.open_set:
            # print('%s %sth search start' % (datetime.now(), self.search_count))
            self.search_count += 1
            P = min(self.open_set)
            self.open_set -= {P}
            self.closed_set |= {P}

            self.env.constraint_dict = P.constraint_dict
            conflict_dict = self.env.get_first_conflict(P.solution)
            # print('solution is %s' % P.solution)
            # print('conflict is %s' % conflict_dict)
            if not conflict_dict:
                #print("%s solution found" % datetime.now())
                #print("low level searched " + str(self.env.a_star.search_count) + " nodes")
                return self.generate_plan(P.solution)

            constraint_dict = self.env.create_constraints_from_conflict(conflict_dict)

            for agent in constraint_dict.keys():
                new_node = deepcopy(P)
                new_node.constraint_dict[agent].add_constraint(constraint_dict[agent])

                self.env.constraint_dict = new_node.constraint_dict
                new_node.solution.update(self.env.compute_schedule(agent))
                if not new_node.solution:
                    continue
                new_node.cost = self.env.compute_solution_cost(new_node.solution)
                if new_node not in self.closed_set:
                    self.open_set |= {new_node}

        return {}