Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/elevator/hall_orders.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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()

_ ->
Expand All @@ -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)
Expand Down
26 changes: 15 additions & 11 deletions lib/elevator/hall_orders/cost.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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()}) ::
Expand Down
4 changes: 2 additions & 2 deletions lib/elevator/hall_orders/order.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions scripts/hosts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading