diff --git a/lib/elevator/hall_orders.ex b/lib/elevator/hall_orders.ex index cd12fa6..fd1fce1 100644 --- a/lib/elevator/hall_orders.ex +++ b/lib/elevator/hall_orders.ex @@ -225,6 +225,12 @@ defmodule Elevator.HallOrders do Enum.filter(order_map, fn {_, order_state} -> case order_state do {:handling, cost_map} -> + if Cost.min_alive_cost(cost_map, alive) == Communicator.my_id() do + Logger.debug( + "\nCost map: #{inspect(cost_map)}\nAlive: #{inspect(alive)}\nI (#{inspect(Communicator.my_id())}) am the one to serve" + ) + end + Cost.min_alive_cost(cost_map, alive) == Communicator.my_id() _ -> @@ -236,7 +242,7 @@ defmodule Elevator.HallOrders do @type enum_orders :: Elevator.Types.hall_order_map() - | Enumerable.t({Elevator.Types.hall_order_key(), Elevator.Types.hall_order_value()}) + | Enumerable.t({Elevator.Types.hall_order_key(), Elevator.Types.hall_order_state()}) @spec orders_by_floor(enum_orders()) :: %{floor() => MapSet.t(hall_btn())} defp orders_by_floor(orders) do # Restructure order map to the format floor => MapSet(order) diff --git a/lib/elevator/hall_orders/cost.ex b/lib/elevator/hall_orders/cost.ex index a9b91f7..3ed1ac7 100644 --- a/lib/elevator/hall_orders/cost.ex +++ b/lib/elevator/hall_orders/cost.ex @@ -79,17 +79,21 @@ defmodule Elevator.HallOrders.Cost do def min_alive_cost(cost_map, alive_set) do alive_costs = Enum.filter(cost_map, fn {node, _} -> MapSet.member?(alive_set, node) end) - {min_node, _} = - Enum.min( - alive_costs, - fn {node1, cost1}, {node2, cost2} -> - cost1 < cost2 or (cost1 == cost2 and node1 < node2) - end, - # Fallback when no alive costs exist - fn -> {:nonode@nohost, :infinity} end - ) - - min_node + if Enum.count(alive_costs) != MapSet.size(alive_set) do + nil + else + {min_node, _} = + Enum.min( + alive_costs, + fn {node1, cost1}, {node2, cost2} -> + cost1 < cost2 or (cost1 == cost2 and node1 < node2) + end, + # Fallback when no alive costs exist + fn -> {:nonode@nohost, :infinity} end + ) + + min_node + end end @spec simulate_cost_until_served(combined_orders_t(), State.t(), {floor_t(), hall_btn_t()}) :: diff --git a/lib/elevator/hall_orders/order.ex b/lib/elevator/hall_orders/order.ex index 5d9b392..e2c1f01 100644 --- a/lib/elevator/hall_orders/order.ex +++ b/lib/elevator/hall_orders/order.ex @@ -32,10 +32,10 @@ defmodule Elevator.HallOrders.Order do new_order_state = case new_order_state do {:pending, barrier_set} -> - {:pending, MapSet.put(barrier_set, Node.self())} + {:pending, MapSet.put(barrier_set, Communicator.my_id())} {:arrived, barrier_set} -> - {:arrived, MapSet.put(barrier_set, Node.self())} + {:arrived, MapSet.put(barrier_set, Communicator.my_id())} _ -> new_order_state diff --git a/scripts/hosts b/scripts/hosts index 4036158..e729e0f 100644 --- a/scripts/hosts +++ b/scripts/hosts @@ -3,6 +3,10 @@ # # Lines starting with # and blank lines are ignored. +9 student@10.100.23.19 +10 student@10.100.23.20 +11 student@10.100.23.21 +12 student@10.100.23.22 16 student@10.100.23.26 19 student@10.100.23.29 21 student@10.100.23.172