Skip to content

Commit eac7934

Browse files
committed
Move code around
1 parent 233314c commit eac7934

File tree

2 files changed

+47
-39
lines changed

2 files changed

+47
-39
lines changed

src/ring_calculator.erl

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,53 @@
11
-module(ring_calculator).
22

3-
-export([init/1]).
3+
-export([init/1, nvals/3, nvals/4]).
44

55
-spec init(rebar_state:t()) -> {ok, rebar_state:t()}.
66
init(State) ->
77
{ok, State1} = ring_calculator_prv:init(State),
88
{ok, State1}.
9+
10+
-spec nvals(RingSize :: pos_integer(), NrNodes :: pos_integer(), NrLocations :: pos_integer()) -> ok.
11+
nvals(RingSize, NrMachines, NrLocations) ->
12+
nvals(RingSize, NrMachines, NrLocations, false).
13+
14+
-spec nvals(RingSize :: pos_integer(), NrNodes :: pos_integer(),
15+
NrLocations :: pos_integer(), Verbose :: boolean()) -> ok.
16+
nvals(RingSize, NrMachines, NrLocations, Verbose) when NrMachines > 0, NrLocations > 0 ->
17+
LVals = lists:seq(1, min(NrMachines, NrLocations)),
18+
TargetNVals = lists:seq(1, NrMachines),
19+
Sols = lists:flatten([ solutions(RingSize, LVal, TargetNVals, NrMachines, Locs, Verbose)
20+
|| LVal <- LVals, %% NVal <- TargetNVals,
21+
Locs <- lists:seq(1, NrLocations) ]),
22+
case Sols of
23+
[] ->
24+
io:format("No solution~n");
25+
_ ->
26+
{BestLVal, BestNVal, Config, Ring} =
27+
lists:last(lists:sort([ Sol || {_, _, _, _} = Sol <- Sols ])),
28+
io:format("~nBest solution:~nTarget Nval ~p~nLocation val ~p~nConfig ~p~nRing: ~s~n",
29+
[BestNVal, BestLVal, Config, ring_calculator_ring:show(Ring, {BestNVal, BestLVal})])
30+
end.
31+
32+
33+
solutions(RingSize, LVal, [NVal | NVals], NrMachines, NrLocations, Verbose) when 0 < LVal, LVal =< NrLocations ->
34+
%% spread machines evenly
35+
Config = spread_over_locations(lists:duplicate(NrLocations, 0), NrMachines),
36+
[ io:format("Config ~p ", [Config]) || Verbose ],
37+
Solution = ring_calculator_ring:solve(RingSize, Config, {NVal, LVal}),
38+
ZeroViolations = ring_calculator_ring:zero_violations(Solution, {NVal, LVal}),
39+
[ io:format("Target Nval: ~p, Locaction val: ~p Success: ~p~n", [NVal, LVal, ZeroViolations ]) || Verbose ],
40+
if ZeroViolations ->
41+
[ io:format(".") || not Verbose ],
42+
[{LVal, NVal, Config, Solution} | solutions(RingSize, LVal, NVals, NrMachines, NrLocations, Verbose) ];
43+
true ->
44+
[ io:format("x") || not Verbose ],
45+
[]
46+
end;
47+
solutions(_RingSize, _LVal, _, _NrMachines, _NrLocations, _Verbose) ->
48+
[].
49+
50+
spread_over_locations(Config, 0) ->
51+
Config;
52+
spread_over_locations([Hd | Tl], NrMachines) ->
53+
spread_over_locations(Tl++[Hd + 1], NrMachines - 1).

src/ring_calculator_prv.erl

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -39,47 +39,10 @@ do(State) ->
3939
NrNodes = proplists:get_value(nodes, KVs, 8),
4040
Locs = proplists:get_value(locations, KVs, 4),
4141
Verbose = proplists:get_value(verbose, KVs, false),
42-
calc_nvals(RingSize, NrNodes, Locs, Verbose),
42+
ring_calculator:nvals(RingSize, NrNodes, Locs, Verbose),
4343
{ok, State}.
4444

4545
-spec format_error(any()) -> iolist().
4646
format_error(Reason) ->
4747
io_lib:format("~p", [Reason]).
4848

49-
calc_nvals(RingSize, NrMachines, NrLocations, Verbose) when NrMachines > 0, NrLocations > 0 ->
50-
LVals = lists:seq(1, min(NrMachines, NrLocations)),
51-
TargetNVals = lists:seq(1, NrMachines),
52-
Sols = lists:flatten([ solutions(RingSize, LVal, TargetNVals, NrMachines, Locs, Verbose)
53-
|| LVal <- LVals, %% NVal <- TargetNVals,
54-
Locs <- lists:seq(1, NrLocations) ]),
55-
case Sols of
56-
[] ->
57-
io:format("No solution~n");
58-
_ ->
59-
{BestLVal, BestNVal, Config, Ring} =
60-
lists:last(lists:sort([ Sol || {_, _, _, _} = Sol <- Sols ])),
61-
io:format("~nBest solution:~nTarget Nval ~p~nLocation val ~p~nConfig ~p~nRing: ~s~n",
62-
[BestNVal, BestLVal, Config, ring_calculator_ring:show(Ring, {BestNVal, BestLVal})])
63-
end.
64-
65-
solutions(RingSize, LVal, [NVal | NVals], NrMachines, NrLocations, Verbose) when 0 < LVal, LVal =< NrLocations ->
66-
%% spread machines evenly
67-
Config = spread_over_locations(lists:duplicate(NrLocations, 0), NrMachines),
68-
[ io:format("Config ~p ", [Config]) || Verbose ],
69-
Solution = ring_calculator_ring:solve(RingSize, Config, {NVal, LVal}),
70-
ZeroViolations = ring_calculator_ring:zero_violations(Solution, {NVal, LVal}),
71-
[ io:format("Target Nval: ~p, Locaction val: ~p Success: ~p~n", [NVal, LVal, ZeroViolations ]) || Verbose ],
72-
if ZeroViolations ->
73-
[ io:format(".") || not Verbose ],
74-
[{LVal, NVal, Config, Solution} | solutions(RingSize, LVal, NVals, NrMachines, NrLocations, Verbose) ];
75-
true ->
76-
[ io:format("x") || not Verbose ],
77-
[]
78-
end;
79-
solutions(_RingSize, _LVal, _, _NrMachines, _NrLocations, _Verbose) ->
80-
[].
81-
82-
spread_over_locations(Config, 0) ->
83-
Config;
84-
spread_over_locations([Hd | Tl], NrMachines) ->
85-
spread_over_locations(Tl++[Hd + 1], NrMachines - 1).

0 commit comments

Comments
 (0)