-module(search_algorithms). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]). -define(FILEPATH, "src/search_algorithms.gleam"). -export([breadth_first/3, depth_first/3, dijkstra_assoc/3, dijkstra/4, a_star_assoc/4, a_star/5]). -if(?OTP_RELEASE >= 27). -define(MODULEDOC(Str), -moduledoc(Str)). -define(DOC(Str), -doc(Str)). -else. -define(MODULEDOC(Str), -compile([])). -define(DOC(Str), -compile([])). -endif. -file("src/search_algorithms.gleam", 9). ?DOC(" Breadth First Search\n"). -spec breadth_first(fun((GJY) -> list(GJY)), fun((GJY) -> boolean()), GJY) -> {ok, list(GJY)} | {error, nil}. breadth_first(Next, Found, Initial) -> _pipe@1 = internal@generalized_search:generalized_search( internal@search_container:new_queue(), fun gleam@function:identity/1, fun(_, _) -> false end, fun(State) -> _pipe = Next(erlang:element(2, State)), gleam@list:map(_pipe, fun(State@1) -> {0, State@1} end) end, fun(State@2) -> Found(erlang:element(2, State@2)) end, {0, Initial} ), gleam@result:map( _pipe@1, fun(List) -> gleam@list:map(List, fun(T) -> erlang:element(2, T) end) end ). -file("src/search_algorithms.gleam", 28). ?DOC(" Depth First Search\n"). -spec depth_first(fun((GKD) -> list(GKD)), fun((GKD) -> boolean()), GKD) -> {ok, list(GKD)} | {error, nil}. depth_first(Next, Found, Initial) -> _pipe@1 = internal@generalized_search:generalized_search( internal@search_container:new_stack(), fun gleam@function:identity/1, fun(_, _) -> true end, fun(State) -> _pipe = Next(erlang:element(2, State)), gleam@list:map(_pipe, fun(State@1) -> {0, State@1} end) end, fun(State@2) -> Found(erlang:element(2, State@2)) end, {0, Initial} ), gleam@result:map( _pipe@1, fun(List) -> gleam@list:map(List, fun(T) -> erlang:element(2, T) end) end ). -file("src/search_algorithms.gleam", 46). -spec dijkstra_generalized( fun(({integer(), GKI}) -> list({integer(), GKI})), fun((GKI) -> boolean()), GKI ) -> {ok, {integer(), list(GKI)}} | {error, nil}. dijkstra_generalized(Get_next_states_packed, Has_found_end, Initial) -> Unpack = fun(Packed_states) -> case Packed_states of [] -> {0, []}; Packed_states@1 -> Last@1 = case gleam@list:last(Packed_states@1) of {ok, Last} -> Last; _assert_fail -> erlang:error(#{gleam_error => let_assert, message => <<"Pattern match failed, no pattern matched the value."/utf8>>, file => <>, module => <<"search_algorithms"/utf8>>, function => <<"dijkstra_generalized"/utf8>>, line => 55, value => _assert_fail, start => 1708, 'end' => 1754, pattern_start => 1719, pattern_end => 1727}) end, Fst = erlang:element(1, Last@1), Snd = gleam@list:map( Packed_states@1, fun(T) -> erlang:element(2, T) end ), {Fst, Snd} end end, Result = internal@generalized_search:generalized_search( internal@search_container:new_lifo_heap(), fun(T@1) -> erlang:element(2, T@1) end, fun internal@utils:least_costly/2, Get_next_states_packed, fun(T@2) -> Has_found_end(erlang:element(2, T@2)) end, {0, Initial} ), gleam@result:map(Result, Unpack). -file("src/search_algorithms.gleam", 77). ?DOC(" Dijkstra w/ associated transition costs\n"). -spec dijkstra_assoc( fun((GKN) -> list({GKN, integer()})), fun((GKN) -> boolean()), GKN ) -> {ok, {integer(), list(GKN)}} | {error, nil}. dijkstra_assoc(Get_next_states, Has_found_end, Initial) -> Get_next_states_packed = fun(Arg) -> {Current_cost, Current_state} = Arg, Next_states = Get_next_states(Current_state), _pipe = Next_states, gleam@list:map( _pipe, fun(State_cost_tuple) -> {Current_cost + erlang:element(2, State_cost_tuple), erlang:element(1, State_cost_tuple)} end ) end, dijkstra_generalized(Get_next_states_packed, Has_found_end, Initial). -file("src/search_algorithms.gleam", 95). ?DOC(" Dijkstra\n"). -spec dijkstra( fun((GKQ) -> list(GKQ)), fun((GKQ, GKQ) -> integer()), fun((GKQ) -> boolean()), GKQ ) -> {ok, {integer(), list(GKQ)}} | {error, nil}. dijkstra(Get_next_states, Get_next_cost, Has_found_end, Initial) -> Get_next_states_packed = fun(Arg) -> {Current_cost, Current_state} = Arg, Next_states = Get_next_states(Current_state), Next_costs = gleam@list:map( Next_states, fun(Next_state) -> Get_next_cost(Current_state, Next_state) + Current_cost end ), gleam@list:zip(Next_costs, Next_states) end, dijkstra_generalized(Get_next_states_packed, Has_found_end, Initial). -file("src/search_algorithms.gleam", 115). ?DOC(" A*\n"). -spec a_star_generalized( fun(({integer(), {GKT, integer()}}) -> list({integer(), {GKT, integer()}})), fun((GKT) -> integer()), fun((GKT) -> boolean()), GKT ) -> {ok, {integer(), list(GKT)}} | {error, nil}. a_star_generalized( Get_next_states_packed, Approx_remaining_cost, Has_found_end, Initial ) -> Unpack = fun(Packed_states) -> case Packed_states of [] -> {0, []}; Packed_states@1 -> Last@1 = case gleam@list:last(Packed_states@1) of {ok, Last} -> Last; _assert_fail -> erlang:error(#{gleam_error => let_assert, message => <<"Pattern match failed, no pattern matched the value."/utf8>>, file => <>, module => <<"search_algorithms"/utf8>>, function => <<"a_star_generalized"/utf8>>, line => 129, value => _assert_fail, start => 3896, 'end' => 3942, pattern_start => 3907, pattern_end => 3915}) end, Fst = erlang:element(2, erlang:element(2, Last@1)), Snd = gleam@list:map( Packed_states@1, fun(States) -> erlang:element(1, erlang:element(2, States)) end ), {Fst, Snd} end end, Result = internal@generalized_search:generalized_search( internal@search_container:new_lifo_heap(), fun(Packed_state) -> erlang:element(1, erlang:element(2, Packed_state)) end, fun internal@utils:least_costly/2, Get_next_states_packed, fun(Packed_state@1) -> Has_found_end(erlang:element(1, erlang:element(2, Packed_state@1))) end, {Approx_remaining_cost(Initial), {Initial, 0}} ), gleam@result:map(Result, Unpack). -file("src/search_algorithms.gleam", 153). ?DOC(" A* w/ associated transition costs\n"). -spec a_star_assoc( fun((GKY) -> list({GKY, integer()})), fun((GKY) -> integer()), fun((GKY) -> boolean()), GKY ) -> {ok, {integer(), list(GKY)}} | {error, nil}. a_star_assoc(Get_next_states, Approx_remaining_cost, Has_found_end, Initial) -> Get_next_states_packed = fun(Arg) -> {_, {Current_state, Current_cost}} = Arg, _pipe = Get_next_states(Current_state), gleam@list:map( _pipe, fun(State_cost_tuple) -> Remaining = Approx_remaining_cost( erlang:element(1, State_cost_tuple) ), Next_cost = Current_cost + erlang:element(2, State_cost_tuple), Next_estimate = Next_cost + Remaining, {Next_estimate, {erlang:element(1, State_cost_tuple), Next_cost}} end ) end, a_star_generalized( Get_next_states_packed, Approx_remaining_cost, Has_found_end, Initial ). -file("src/search_algorithms.gleam", 180). -spec a_star( fun((GLB) -> list(GLB)), fun((GLB, GLB) -> integer()), fun((GLB) -> integer()), fun((GLB) -> boolean()), GLB ) -> {ok, {integer(), list(GLB)}} | {error, nil}. a_star( Get_next_states, Get_next_cost, Approx_remaining_cost, Has_found_end, Initial ) -> Get_next_states_packed = fun(Arg) -> {_, {Current_state, Current_cost}} = Arg, Next_states = Get_next_states(Current_state), gleam@list:map( Next_states, fun(Next_state) -> Remaining = Approx_remaining_cost(Next_state), Next_cost = Current_cost + Get_next_cost( Current_state, Next_state ), Next_estimate = Next_cost + Remaining, {Next_estimate, {Next_state, Next_cost}} end ) end, a_star_generalized( Get_next_states_packed, Approx_remaining_cost, Has_found_end, Initial ).