|
| 1 | +### Using the sharing ADMM with flex actors (e.g. for resource optimization) with Mango.jl |
| 2 | + |
| 3 | +```julia |
| 4 | +using Mango |
| 5 | +using DistributedResourceOptimization |
| 6 | + |
| 7 | +@role struct HandleOptimizationResultRole |
| 8 | + got_it::Bool = false |
| 9 | +end |
| 10 | + |
| 11 | +function Mango.handle_message(role::HandleOptimizationResultRole, message::OptimizationFinishedMessage, meta::Any) |
| 12 | + role.got_it = true |
| 13 | +end |
| 14 | + |
| 15 | +container = create_tcp_container("127.0.0.1", 5555) |
| 16 | + |
| 17 | +# create participant models |
| 18 | +flex_actor = create_admm_flex_actor_one_to_many(10, [0.1, 0.5, -1]) |
| 19 | +flex_actor2 = create_admm_flex_actor_one_to_many(15, [0.1, 0.5, -1]) |
| 20 | +flex_actor3 = create_admm_flex_actor_one_to_many(10, [0.1, 0.5, -1]) |
| 21 | + |
| 22 | +# create coordinator with objective |
| 23 | +coordinator = create_sharing_target_distance_admm_coordinator() |
| 24 | + |
| 25 | +# create roles to integrate admm in Mango.jl |
| 26 | +dor = DistributedOptimizationRole(flex_actor, tid=:custom) |
| 27 | +dor2 = DistributedOptimizationRole(flex_actor2, tid=:custom) |
| 28 | +dor3 = DistributedOptimizationRole(flex_actor3, tid=:custom) |
| 29 | +coord_role = CoordinatorRole(coordinator, tid=:custom, include_self=true) |
| 30 | + |
| 31 | +# role to handle a result |
| 32 | +handle = HandleOptimizationResultRole() |
| 33 | +handle2 = HandleOptimizationResultRole() |
| 34 | +handle3 = HandleOptimizationResultRole() |
| 35 | + |
| 36 | +# create agents |
| 37 | +add_agent_composed_of(container, dor, handle) |
| 38 | +c = add_agent_composed_of(container, dor2, handle2) |
| 39 | +ca = add_agent_composed_of(container, coord_role, dor3, handle3) |
| 40 | + |
| 41 | +# create a topology of the agents |
| 42 | +auto_assign!(complete_topology(3, tid=:custom), container) |
| 43 | + |
| 44 | +# run the simulation with start message and wait for result |
| 45 | +activate(container) do |
| 46 | + wait(send_message(c, StartCoordinatedDistributedOptimization(create_admm_start(create_admm_sharing_data([0.2, 1, -2]))), address(ca))) |
| 47 | + wait(coord_role.task) |
| 48 | +end |
| 49 | +``` |
| 50 | + |
| 51 | +### Using COHDA with Mango.jl |
| 52 | + |
| 53 | +```julia |
| 54 | +using Mango |
| 55 | +using DistributedResourceOptimization |
| 56 | + |
| 57 | +container = create_tcp_container("127.0.0.1", 5555) |
| 58 | + |
| 59 | +# create agents with local model wrapped in the general distributed optimization role |
| 60 | +agent_one = add_agent_composed_of(container, DistributedOptimizationRole( |
| 61 | + create_cohda_participant(1, [[0.0, 1, 2], [1, 2, 3]]))) |
| 62 | +agent_two = add_agent_composed_of(container, DistributedOptimizationRole( |
| 63 | + create_cohda_participant(2, [[0.0, 1, 2], [1, 2, 3]]))) |
| 64 | + |
| 65 | +# create start message |
| 66 | +initial_message = create_cohda_start_message([1.2, 2, 3]) |
| 67 | + |
| 68 | +# create topology |
| 69 | +auto_assign!(complete_topology(2), container) |
| 70 | + |
| 71 | +# run simulation |
| 72 | +activate(container) do |
| 73 | + send_message(agent_one, initial_message, address(agent_two)) |
| 74 | +end |
| 75 | +``` |
0 commit comments