-
Notifications
You must be signed in to change notification settings - Fork 7
/
solver_race.ex
84 lines (68 loc) · 2.15 KB
/
solver_race.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
defmodule SolverRace do
@moduledoc false
require Logger
import MinizincUtils
@model resource_file("mzn/golomb_mybab.mzn")
@data []
def run(solvers, opts \\ []) do
parent = self()
Enum.each(
solvers,
fn s ->
MinizincSolver.solve(
@model,
@data,
[
solver: s,
solution_handler:
fn (event, data) ->
callback_fun(s, event, data, parent)
end,
] ++ opts,
[name: solver_process_name(s)]
)
Logger.info "#{s} started as #{inspect :erlang.whereis(solver_process_name(s))}..."
end
)
receive_results(solvers, [])
end
defp callback_fun(solver, event, data, to) do
send(to, {solver, event, data})
end
defp receive_results([], standing) do
Logger.info "Race results: #{inspect Enum.reverse(standing)}"
end
defp receive_results(solvers, standing) do
receive do
{solver, :solution, solution} ->
Logger.info "#{solver}: #{MinizincResults.get_solution_objective(solution)}"
receive_results(solvers, standing)
{solver, :summary, summary} ->
solution = summary[:last_solution]
objective = MinizincResults.get_solution_objective(solution)
status = summary[:status]
Logger.info "Solver #{solver} finished with objective #{objective}, status: #{status}"
still_running = List.delete(solvers, solver)
Enum.each(
still_running,
fn s ->
Logger.info "Shutting down #{s}..."
MinizincSolver.stop_solver(solver_process_name(s))
end
)
receive_results(still_running, [{solver, objective} | standing])
{solver, :compiled, _info} ->
Logger.info "#{solver}: Compiled!"
receive_results(solvers, standing)
{solver, error, error_msg} ->
Logger.info "Solver #{solver} failed with #{error}. Message: #{error_msg}"
receive_results(List.delete(solvers, solver), standing)
end
end
defp solver_process_name(solver) do
String.to_atom(solver)
end
# defp solver_process_name("chuffed") do
# Chuffed
# end
end