-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstarship_crewing.ex
More file actions
140 lines (123 loc) · 3.69 KB
/
Copy pathstarship_crewing.ex
File metadata and controls
140 lines (123 loc) · 3.69 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
defmodule StarshipCrewingExample do
alias BinPacker.OnePerBinConstraint
alias BinPacker.BallsProportionalToBinObjective
import BinPacker.Util, only: [sum_by: 2]
defmodule Ship do
defstruct [
:name,
:size
]
defimpl BinPacker.Bin do
def id(%@for{name: name}), do: name
def attribute(%@for{size: size}, _attribute), do: size
def attribute(%@for{}, _attribute), do: raise "not implemented"
end
end
defmodule CrewMember do
defstruct [
:name,
:profession,
attributes: Map.new()
]
defimpl BinPacker.Ball do
def id(%@for{name: name}), do: name
def attribute(%@for{profession: profession}, :profession), do: profession
def attribute(%@for{attributes: attributes}, attribute), do: Map.get(attributes, attribute)
end
end
def new do
ships = [
%Ship{name: "USS Enterprise", size: 49},
%Ship{name: "USS Bellerephon", size: 42},
%Ship{name: "USS Melbourne", size: 35},
%Ship{name: "USS Kyushu", size: 28},
%Ship{name: "USS Princeton", size: 21},
%Ship{name: "USS Bonestell", size: 14},
%Ship{name: "USS Tolstoy", size: 7}
]
first_names = [
"Jean Luc",
"William",
"Deanna",
"Geordi",
"Worf",
"Beverly",
"Data",
]
last_names = [
"Picard",
"Riker",
"Troi",
"La Forge",
"Worf",
"Crusher",
"Data",
]
professions = [
:captain,
:first_officer,
:counselor,
:engineer,
:security,
:doctor,
:helmsman
]
crew =
first_names
|> Enum.with_index(1)
|> Enum.reverse
|> Enum.flat_map(fn {first_name, i} ->
Enum.map(last_names, fn last_name ->
%CrewMember{name: first_name <> " " <> last_name,
attributes: %{
competency: i,
empathy: i,
aggressiveness: i,
}}
end)
end)
|> Enum.zip(Stream.cycle(professions))
|> Enum.map(fn {crew_member, profession} ->
%CrewMember{crew_member | profession: profession}
end)
BinPacker.new(
ships,
crew,
%{
{BallsProportionalToBinObjective, [ball_attribute: :competency, bin_attribute: :size]} => 10,
# {BallsProportionalToBinObjective, [ball_attribute: :empathy, bin_attribute: :size]} => 1,
# {BallsProportionalToBinObjective, [ball_attribute: :aggressiveness, bin_attribute: :size]} => 1
},
constraints: [
{OnePerBinConstraint, :profession}
]
)
end
def optimize(bin_packer) do
BinPacker.search(bin_packer)
end
def pretty_print(bin_packer) do
bin_packer
|> BinPacker.to_map()
|> Enum.sort_by(fn {%Ship{size: ship_size}, _crew} -> ship_size end)
|> Enum.each(fn {%Ship{name: ship_name, size: ship_size}, crew} ->
total_competency = sum_by(crew, fn %CrewMember{attributes: %{competency: competency}} -> competency end)
# total_empathy = sum_by(crew, fn %CrewMember{attributes: %{empathy: empathy}} -> empathy end)
# total_aggressiveness = sum_by(crew, fn %CrewMember{attributes: %{aggressiveness: aggressiveness}} -> aggressiveness end)
IO.puts "#{ship_name} (size: #{ship_size}, competency: #{total_competency})"
crew
|> Enum.sort_by(fn %CrewMember{profession: profession} -> profession end)
|> Enum.each(fn %CrewMember{name: name, profession: _profession} = c ->
IO.puts "-- #{name} (competency: #{c.attributes.competency})"
# IO.puts "-- #{name} (#{profession})"
end)
IO.puts ""
end)
end
def run do
s = new()
s = optimize(s)
pretty_print(s)
s
end
end