Commit 08931e0a authored by Tim Molderez's avatar Tim Molderez
Browse files

Migrating load balancing example to its own repo

parent 582157df
Loading
Loading
Loading
Loading
+0 −12
Original line number Diff line number Diff line
@@ -19,12 +19,7 @@
use Mix.Config

config :marlon,
  connect_handler: {:example, :connect_node},
  disconnect_handler: {:example, :disconnect_node},
  python_path: 'python',
  communication_time: 2000,
  processing_time: 5000,
  visualization: true

config :libcluster,
  topologies: [
@@ -38,12 +33,5 @@ config :libcluster,
    ]
  ]

config :smile_it_demo_ui, SmileItDemoUiWeb.Endpoint,
  url: [host: "localhost"],
  secret_key_base: "/bytVRunQxwRMpbJ15th2HpDDHJchOMwl+SvoIjLMgnjcxtL9wdT8S3aoIsAneFa",
  render_errors: [view: SmileItDemoUiWeb.ErrorView, accepts: ~w(html json)],
  pubsub: [name: SmileItDemoUi.PubSub,
           adapter: Phoenix.PubSub.PG2]

config :logger,
  level: :info
 No newline at end of file
+66 −0
Original line number Diff line number Diff line
# To run this example: iex -S mix run -r lib/examples/thermostat/thermostat-multi.exs

use Marlon

defactor House do
  def init([init_temp]) do
    {:ok, hp} = Heatpump.start_link([init_temp], [])
    agent = Heatpump.attach_agent(hp, DistributeLoad)
    {:ok, %{heatpump: hp, iterations: 1000}}
  end

  async def loop(this) do
    Heatpump.step(this.heatpump)
    Process.send_after self(), {:loop_reply}, 50
    {:noreply, this}
  end

  reply def loop_reply(this) do
    if (this.iterations > 0) do
      House.loop(self())
    end
    {:noreply, %{this | iterations: this.iterations - 1}}
  end
end

defactor Heatpump do
  def init([init_temp]) do
    {:ok, %{temperature: init_temp, load: 0}}
  end

  async def step(this) do
    IO.puts (Kernel.inspect(self()) <> " temperature: " <> Float.to_string(this.temperature))
    Heatpump.do_action(self()) # The attached agent should now do an action
    {:noreply, this}
  end

  async def on(this) do
    ...
    Heatpump.update_reward(self(), hp)
    {:noreply, hp}
  end

  async def off(this) do
    ...
    Heatpump.update_reward(self(), hp)
    {:noreply, hp}
  end
end

defgoal DistributeLoad do
  type Marlon.ESRL
  params [discount_factor: 0.0, learning_rate: 1.0, epsilon: 0.8]
  actions [on: [], off: []]

  shared [:load]
  shared_deviation [load: 0.5]

  reward fn(_agent, hp, shared) ->
    1 / Util.sum(shared.load))
  end
end

{:ok, house_1} = House.start_link([5.0], [])
{:ok, house_2} = House.start_link([8.0], [])
House.loop(house_1)
House.loop(house_2)
 No newline at end of file
+2 −4
Original line number Diff line number Diff line
@@ -74,8 +74,7 @@ defactor LoadBalancingExample do
    if (this[:master] != nil) do
      Master.work_cancelled(this[:master], node) 
      if (Application.fetch_env!(:marlon, :visualization)) do
        ip = Enum.at(String.split(Atom.to_string(node), "@"),1)
        Marlon.Utils.visualize(:broker_node, {:leave, ip})
        Marlon.Utils.visualize(:broker_node, {:leave, node})
      end  
    end
    {:noreply, this}
@@ -88,7 +87,7 @@ defactor LoadBalancingExample do
  end 

  reply def nodedown(this, node) do
    LoadBalancingExample.disconnect_node(self(), node)
    # LoadBalancingExample.disconnect_node(self(), node)
    {:noreply, this}
  end 
end
@@ -181,7 +180,6 @@ defactor Master do

  sync def request_work(this, from, chunk_size, worker_ip) do
    Marlon.Utils.visualize(this[:visualization], {:update, Node.self(), "Communicating"})
    IO.inspect worker_ip
    Marlon.Utils.visualize(this[:visualization], {:update, worker_ip, "Communicating"})
    new_pending = this[:pending_request_size] + chunk_size
    Worker.notify_wait_time(elem(from,0), new_pending * this[:comm_time])
+1 −1
Original line number Diff line number Diff line
@@ -33,7 +33,7 @@ defmodule Marlon.Application do
    # Check whether this is the node initiated the Marlon system

    node_name = Atom.to_string(Node.self())
    children = if (String.contains?(node_name, "worker")) do
    children = if (String.contains?(node_name, "aux")) do
      Logger.info "Starting Marlon on auxiliary node"
      [{__MODULE__, []}]
    else
+1 −1
Original line number Diff line number Diff line
@@ -22,7 +22,7 @@ defmodule Marlon.Mixfile do

  def project do
    [
      app: :marlon, # Application ID
      app: :marlon,
      version: "0.1.0",
      elixir: "~> 1.5",
      start_permanent: Mix.env == :prod,
Loading