Example #1
0
def distribute_agents(var_facts):
    comm = InProcessCommunicationLayer()

    dcop_agents = []
    factors = [f for _, f in var_facts]

    i = 0
    for v, f in var_facts:

        # Algorithm for variable
        # build the list of factors that depend on this variable
        f_for_variable = [
            f.name for f in factors
            if v.name in [i.name for i in f.dimensions]
        ]
        v_a = amaxsum.VariableAlgo(v, f_for_variable)

        # Algorithm for the factor
        f_a = amaxsum.FactorAlgo(f)

        # Agent hosting the factor and variable
        a = Agent('a_' + str(i), comm)
        a.add_computation(v_a)
        a.add_computation(f_a)
        i += 1

        dcop_agents.append(a)

    return dcop_agents, comm
Example #2
0
def distribue_agent_for_all(variables, factors):
    comm = infrastructure.communication.InProcessCommunicationLayer()

    node_agents = []
    for v in variables:
        f_for_variable = [
            f.name for f in factors
            if v.name in [i.name for i in f.dimensions]
        ]

        a = infrastructure.Agent('Var_' + v.name, comm)
        a.add_computation(amaxsum.VariableAlgo(v, f_for_variable))
        node_agents.append(a)

    for f in factors:
        a = infrastructure.Agent('Fact_' + f.name, comm)
        a.add_computation(amaxsum.FactorAlgo(f))
        node_agents.append(a)

    return node_agents
def maxsum_smartlights_multiplecomputationagent_costvariable():
    """

    This sample use variable with integrated cost.


     * 3 lights l1, l2 & l3
       each light can have a luminosity level in the  0-9 range
       The energy cost is a linear function of the luminosity level, with l1
       more efficient than l2 and l3

     * one scene action y1, the room luminosity level
       y1 = (l1 + l2 + l3 )/3
       y1 domain is also between 0-9

     * one rule :
       l3 must be off AND y1 Must be 5


    """
    # building the Factor Graph
    # Lights :

    l1 = VariableWithCostFunc('l1', list(range(10)), lambda x: x * 0.5)
    l2 = VariableWithCostFunc('l2', list(range(10)), lambda x: x)
    l3 = VariableWithCostFunc('l3', list(range(10)), lambda x: x)

    # Scene action
    y1 = Variable('y1', list(range(10)))

    @relations.AsNAryFunctionRelation(l1, l2, l3, y1)
    def scene_rel(l1, l2, l3, y1):
        if y1 == round(l1 / 3.0 + l2 / 3.0 + l3 / 3.0):
            return 0
        return INFNT

    # Rule
    @relations.AsNAryFunctionRelation(l3, y1)
    def rule_rel(l3, y1):
        """
        This rule means : target luminosity if 5, and x3 is off.

        :param x3:
        :param y1:
        :return:
        """
        return 10 * (abs(y1 - 5) + l3)

    # Create computation for factors and variables
    # Light 1
    algo_l1 = amaxsum.VariableAlgo(l1, [scene_rel.name])

    # Light 2
    algo_l2 = amaxsum.VariableAlgo(l2, [scene_rel.name])

    # Light 3
    algo_l3 = amaxsum.VariableAlgo(l3, [scene_rel.name, rule_rel.name])

    # Scene
    algo_y1 = amaxsum.VariableAlgo(y1, [rule_rel.name, scene_rel.name])
    algo_scene = amaxsum.FactorAlgo(scene_rel)

    # Rule
    algo_rule = amaxsum.FactorAlgo(rule_rel)

    # Distribution of the computation on the three physical light-bulb nodes.
    # We have 9 computations to distribute on 3 agents, mapping the 3 light
    # bulbs.
    comm = infrastructure.communication.InProcessCommunicationLayer()

    a1 = infrastructure.Agent('Bulb1', comm)
    a1.add_computation(algo_l1)
    a1.add_computation(algo_scene)
    a1.add_computation(algo_y1)

    a2 = infrastructure.Agent('Bulb2', comm)
    a2.add_computation(algo_l2)

    a3 = infrastructure.Agent('Bulb3', comm)
    a3.add_computation(algo_l3)
    a3.add_computation(algo_rule)

    dcop_agents = [a1, a2, a3]

    results, _, _ = infrastructure.synchronous_single_run(dcop_agents, 5)

    print(results)

    if results == {'l1': 9, 'y1': 5, 'l3': 0, 'l2': 5}:
        logging.info('SUCCESS !! ')
        return 0
    else:
        logging.info('invalid result found, needs some debugging ...' +
                     str(results))
        return 1