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 = maxsum.VariableAlgo(v, f_for_variable) # Algorithm for the factor f_a = maxsum.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
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(maxsum.VariableAlgo(v, f_for_variable)) node_agents.append(a) for f in factors: a = infrastructure.Agent('Fact_' + f.name, comm) a.add_computation(maxsum.FactorAlgo(f)) node_agents.append(a) return node_agents
def maxsum_smartlights_simple(): """ First SCEP implementation : * 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 No stop condition is implemented yet, the algorithm must be stooped manually. """ # building the Factor Graph # Lights : l1 = Variable('l1', list(range(10))) @relations.AsNAryFunctionRelation(l1) def cost_l1(l1): return 0.5 * l1 l2 = Variable('l2', list(range(10))) @relations.AsNAryFunctionRelation(l2) def cost_l2(l2): return l2 l3 = Variable('l3', list(range(10))) @relations.AsNAryFunctionRelation(l3) def cost_l3(l3): return l3 # 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 = maxsum.VariableAlgo(l1, [cost_l1.name, scene_rel.name]) algo_cost_l1 = maxsum.FactorAlgo(cost_l1) # Light 2 algo_l2 = maxsum.VariableAlgo(l2, [cost_l2.name, scene_rel.name]) algo_cost_l2 = maxsum.FactorAlgo(cost_l2) # Light 3 algo_l3 = maxsum.VariableAlgo( l3, [cost_l3.name, scene_rel.name, rule_rel.name]) algo_cost_l3 = maxsum.FactorAlgo(cost_l3) # Scene algo_y1 = maxsum.VariableAlgo(y1, [rule_rel.name, scene_rel.name]) algo_scene = maxsum.FactorAlgo(scene_rel) #Rule algo_rule = maxsum.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_cost_l1) a1.add_computation(algo_l1) a1.add_computation(algo_scene) a1.add_computation(algo_y1) a2 = infrastructure.Agent('Bulb2', comm) a2.add_computation(algo_cost_l2) a2.add_computation(algo_l2) a3 = infrastructure.Agent('Bulb3', comm) a3.add_computation(algo_cost_l3) a3.add_computation(algo_l3) a3.add_computation(algo_rule) dcop_agents = [a1, a2, a3] results, _, _ = infrastructure.synchronous_single_run(dcop_agents) 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