Skip to content

Commit

Permalink
Merge pull request #21 from zeam-vm/develop
Browse files Browse the repository at this point in the history
Implement feature/19 and change to version 0.3.0
  • Loading branch information
zacky1972 authored Aug 12, 2022
2 parents 959ef05 + 2689511 commit 306507b
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 29 deletions.
33 changes: 19 additions & 14 deletions bench/shortest_path_bench.exs
Original file line number Diff line number Diff line change
@@ -1,41 +1,46 @@
:rand.seed(:exsss, {100, 101, 102})

inputs =
Stream.unfold(3, fn
0 -> nil
n -> {:math.pow(10, n) |> round() , n - 1}
n -> {:math.pow(10, n) |> round(), n - 1}
end)
|> Enum.reverse()
|> Enum.map(fn n ->
1..4
|> Enum.map(& div(n * (n - 1), 4 * 2) * &1)
|> Enum.map(&(div(n * (n - 1), 4 * 2) * &1))
|> Enum.map(fn m -> {n, m} end)
end)
|> List.flatten()
|> Enum.with_index()
|> Enum.map(fn {{n, m}, i} -> {n, m, "input#{i}.txt"} end)
|> Stream.map(fn {n, m, file} -> {n, m, ShortestPath.InputCaseGenerator.generate_priv(file, n, m)} end)
|> Stream.map(fn {n, m, file} ->
{n, m, ShortestPath.InputCaseGenerator.generate_priv(file, n, m)}
end)
|> Enum.map(fn {n, m, {:ok, file}} -> {"N,M = #{n},#{m}", {n, m, file}} end)
|> Map.new()

Benchee.run(
%{
"Dijkstra.MainA" => fn {_n, _m, file} ->
pid = spawn(fn ->
receive do
{:r_pid, r_pid} ->
{n, m, i} = ShortestPath.InputReader.read_directly(file)
ShortestPath.Dijkstra.MainA.main(n, m, i)
send(r_pid, :ok)
after 1000 -> :error
end
end)
pid =
spawn(fn ->
receive do
{:r_pid, r_pid} ->
ShortestPath.SolverFromWeightedEdgeList.main_p(file, ShortestPath.Dijkstra.MainA)
send(r_pid, :ok)
after
1000 -> :error
end
end)

send(pid, {:r_pid, self()})

receive do
:ok -> :ok
after 2000 ->
Process.exit(pid, :normal)
after
2000 ->
Process.exit(pid, :normal)
end
end
},
Expand Down
3 changes: 2 additions & 1 deletion lib/shortest_path/dijkstra/main_a.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
defmodule ShortestPath.Dijkstra.MainA do
@behaviour ShortestPath.SolverFromWeightedEdgeList
@inf 1_000_000_000_000

@spec main(pos_integer(), pos_integer(), list()) :: list()
@impl true
def main(n, _, vicinities) do
# 各頂点
node_map =
Expand Down
4 changes: 2 additions & 2 deletions lib/shortest_path/input_case_generator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule ShortestPath.InputCaseGenerator do
A generator of input case for an undirected graph.
"""

@path "test/support/in"
@path_in "test/support/in"
@weight_max 1_000
@size_grid 16

Expand All @@ -18,7 +18,7 @@ defmodule ShortestPath.InputCaseGenerator do
```
"""
def generate(file, n, m, x \\ @size_grid, y \\ @size_grid) do
generate_s(@path, file, n, m, x, y, true)
generate_s(@path_in, file, n, m, x, y, true)
end

@doc """
Expand Down
13 changes: 10 additions & 3 deletions lib/shortest_path/input_reader.ex
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
defmodule ShortestPath.InputReader do
@support_in "test/support/in"
@path_in "test/support/in"
@buffer_size 65536

@doc """
Reads a given file and returns a tuple of *N*, *M* and the list of the lists `[Vaj, Vbj, Wj]` in `test/support/in`.
"""
@spec read(Path.t()) ::
{pos_integer(), pos_integer(),
ShortestPath.SolverFromWeightedEdgeList.weighted_edge_list()}
def read(file) do
read_directly(Path.join(@support_in, file))
read_directly(Path.join(@path_in, file))
end

@doc """
Reads a given file and returns a tuple of *N*, *M* and the list of the lists `[Vaj, Vbj, Wj]` in anywhere in the file system.
"""
@spec read_directly(Path.t()) ::
{pos_integer(), pos_integer(),
ShortestPath.SolverFromWeightedEdgeList.weighted_edge_list()}
def read_directly(file) do
stream = File.stream!(file, [:read], :line)
stream = File.stream!(file, [read_ahead: @buffer_size], :line)

[n, m] =
stream
Expand Down
1 change: 1 addition & 0 deletions lib/shortest_path/output_writer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ defmodule ShortestPath.OutputWriter do
@doc """
Returns a string for IO.puts/2 from the given list of the list that contains all distances between all vertices.
"""
@spec puts(ShortestPath.SolverFromWeightedEdgeList.weight_between_nodes_list()) :: String.t()
def puts(outputs) do
"#{Enum.count(outputs)}\n" <>
(outputs
Expand Down
14 changes: 14 additions & 0 deletions lib/shortest_path/solver_from_weighted_edge_list.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
defmodule ShortestPath.SolverFromWeightedEdgeList do
@type weighted_edge_list :: list(list(pos_integer()))
@type weight_between_nodes_list :: list(list(pos_integer()))

@callback main(pos_integer(), pos_integer(), weighted_edge_list) :: weight_between_nodes_list

@spec main_p(Path.t(), module()) :: String.t()
def main_p(file, module) do
{n, m, inputs} = ShortestPath.InputReader.read_directly(file)

Function.capture(module, :main, 3).(n, m, inputs)
|> ShortestPath.OutputWriter.puts()
end
end
3 changes: 2 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule ShortestPath.MixProject do
def project do
[
app: :shortest_path,
version: "0.1.0",
version: "0.3.0",
elixir: "~> 1.14-rc",
start_permanent: Mix.env() == :prod,
deps: deps(),
Expand Down Expand Up @@ -33,6 +33,7 @@ defmodule ShortestPath.MixProject do
# {:dep_from_hexpm, "~> 0.3.0"},
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
{:ex_doc, "~> 0.28", only: :dev, runtime: false},
{:dialyxir, "~> 1.2", only: [:dev], runtime: false},
{:benchee, "~> 1.1", only: :dev}
]
end
Expand Down
2 changes: 2 additions & 0 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
%{
"benchee": {:hex, :benchee, "1.1.0", "f3a43817209a92a1fade36ef36b86e1052627fd8934a8b937ac9ab3a76c43062", [:mix], [{:deep_merge, "~> 1.0", [hex: :deep_merge, repo: "hexpm", optional: false]}, {:statistex, "~> 1.0", [hex: :statistex, repo: "hexpm", optional: false]}], "hexpm", "7da57d545003165a012b587077f6ba90b89210fd88074ce3c60ce239eb5e6d93"},
"deep_merge": {:hex, :deep_merge, "1.0.0", "b4aa1a0d1acac393bdf38b2291af38cb1d4a52806cf7a4906f718e1feb5ee961", [:mix], [], "hexpm", "ce708e5f094b9cd4e8f2be4f00d2f4250c4095be93f8cd6d018c753894885430"},
"dialyxir": {:hex, :dialyxir, "1.2.0", "58344b3e87c2e7095304c81a9ae65cb68b613e28340690dfe1a5597fd08dec37", [:mix], [{:erlex, ">= 0.2.6", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "61072136427a851674cab81762be4dbeae7679f85b1272b6d25c3a839aff8463"},
"earmark_parser": {:hex, :earmark_parser, "1.4.26", "f4291134583f373c7d8755566122908eb9662df4c4b63caa66a0eabe06569b0a", [:mix], [], "hexpm", "48d460899f8a0c52c5470676611c01f64f3337bad0b26ddab43648428d94aabc"},
"erlex": {:hex, :erlex, "0.2.6", "c7987d15e899c7a2f34f5420d2a2ea0d659682c06ac607572df55a43753aa12e", [:mix], [], "hexpm", "2ed2e25711feb44d52b17d2780eabf998452f6efda104877a3881c2f8c0c0c75"},
"ex_doc": {:hex, :ex_doc, "0.28.4", "001a0ea6beac2f810f1abc3dbf4b123e9593eaa5f00dd13ded024eae7c523298", [:mix], [{:earmark_parser, "~> 1.4.19", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "bf85d003dd34911d89c8ddb8bda1a958af3471a274a4c2150a9c01c78ac3f8ed"},
"makeup": {:hex, :makeup, "1.1.0", "6b67c8bc2882a6b6a445859952a602afc1a41c2e08379ca057c0f525366fc3ca", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "0a45ed501f4a8897f580eabf99a2e5234ea3e75a4373c8a52824f6e873be57a6"},
"makeup_elixir": {:hex, :makeup_elixir, "0.16.0", "f8c570a0d33f8039513fbccaf7108c5d750f47d8defd44088371191b76492b0b", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "28b2cbdc13960a46ae9a8858c4bebdec3c9a6d7b4b9e7f4ed1502f8159f338e7"},
Expand Down
13 changes: 5 additions & 8 deletions test/shortest_path/dijkstra/main_a_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ defmodule ShortestPath.Dijkstra.MainATest do
use ExUnit.Case
doctest ShortestPath.Dijkstra.MainA
alias ShortestPath.Dijkstra.MainA
@path_in "test/support/in"

test "solve sample_01 by Dijkstra" do
{n, m, inputs} = ShortestPath.InputReader.read("sample_01.txt")

actual =
MainA.main(n, m, inputs)
|> ShortestPath.OutputWriter.puts()
Path.join(@path_in, "sample_01.txt")
|> ShortestPath.SolverFromWeightedEdgeList.main_p(MainA)

expected =
File.read!("test/support/out/sample_01.txt")
Expand All @@ -18,11 +17,9 @@ defmodule ShortestPath.Dijkstra.MainATest do
end

test "solve sample_02 by Dijkstra" do
{n, m, inputs} = ShortestPath.InputReader.read("sample_02.txt")

actual =
MainA.main(n, m, inputs)
|> ShortestPath.OutputWriter.puts()
Path.join(@path_in, "sample_02.txt")
|> ShortestPath.SolverFromWeightedEdgeList.main_p(MainA)

expected =
File.read!("test/support/out/sample_02.txt")
Expand Down

0 comments on commit 306507b

Please sign in to comment.