Exemplo n.º 1
0
def test_build_graph_from_variables_constraints():
    d = Domain('d', 'test', [1, 2, 3])
    v1 = Variable('v1', d)
    v2 = Variable('v2', d)
    v3 = Variable('v3', d)
    c1 = constraint_from_str('c1', 'v1 * 0.5 + v2 - v3', [v1, v2, v3])

    graph = build_computation_graph(variables=[v1, v2, v3], constraints=[c1])
Exemplo n.º 2
0
def test_graph_density():
    d = Domain('d', 'test', [1, 2, 3])
    v1 = Variable('v1', d)
    v2 = Variable('v2', d)
    v3 = Variable('v3', d)
    c1 = constraint_from_str('c1', 'v1 * 0.5 + v2 - v3', [v1, v2, v3])

    dcop = DCOP('dcop_test', 'min')
    dcop.add_constraint(c1)

    graph = build_computation_graph(dcop)

    density = graph.density()
    assert density == 1 / 3
Exemplo n.º 3
0
def test_build_graph_from_dcop():
    d = Domain('d', 'test', [1, 2, 3])
    v1 = Variable('v1', d)
    v2 = Variable('v2', d)
    v3 = Variable('v3', d)
    dcop = DCOP('dcop_test', 'min')
    dcop += 'c1', 'v1 * 0.5 + v2 - v3', [v1, v2, v3]
    c1 = dcop.constraints['c1']

    graph = build_computation_graph(dcop)

    links = list(graph.links)
    assert 1 == len(links)
    assert ConstraintLink(c1.name, {'v1', 'v2', 'v3'}) in links

    nodes = list(graph.nodes)
    assert 3 == len(nodes)
    assert VariableComputationNode(v1, [c1]) in nodes
    assert VariableComputationNode(v2, [c1]) in nodes
    assert VariableComputationNode(v3, [c1]) in nodes
Exemplo n.º 4
0
def generate_routes_costs(mode: str, mapping,
                          dcop) -> Dict[str, Dict[str, float]]:
    routes = {}
    if mode == "graph":
        variables = list(dcop.variables)
        graph = constraints_hypergraph.build_computation_graph(dcop)

        # route = (1 + abs(degree_n - degree_v)) / (degree_n + degree_v)
        # logger.debug(f"agants {agents}")
        # logger.debug(f"variables {variables}")
        # mappings = find_corresponding_variables(agents, variables)
        # logger.debug(mappings)
        inverse_mapping = {
            variable: agent
            for agent, variables in mapping.items() for variable in variables
        }
        logger.debug(inverse_mapping)
        for agt_name in mapping:
            agt_routes = {}
            for hosted_variable in mapping[agt_name]:
                # hosted_variable = mappings[agt_name]
                neighbor_variables = list(graph.neighbors(hosted_variable))
                logger.debug(
                    f"routes for {agt_name} hosting {hosted_variable} with {neighbor_variables}"
                )

                degree = len(neighbor_variables)
                for neighbor in neighbor_variables:
                    if neighbor in inverse_mapping:
                        neighbor_agent = inverse_mapping[neighbor]
                        degree_neighbor = len(list(graph.neighbors(neighbor)))
                        route = (0.2 + abs(degree - degree_neighbor)) / (
                            degree + degree_neighbor)
                        logger.debug(
                            f"Route {agt_name} - {neighbor_agent} : {route}")
                        agt_routes[neighbor_agent] = route
            routes[agt_name] = agt_routes
    return routes