-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgreedy_postal.rb
42 lines (35 loc) · 1.12 KB
/
greedy_postal.rb
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
class GreedyPostal
def initialize
end
end
def pack_van_greedy(weight, value, total_weight)
if (weight.min > total_weight)
puts 'No solution'
else
while (weight.min < total_weight)
i = @value.index(@value.max)
if (weight[i] < total_weight)
puts 'Max weight is ' << @value.max.to_s
tmp = [i, weight[i], value[i]]
@finalweight = @finalweight + weight[i]
@finalvalue = @finalvalue + value[i]
@packedvan.push(tmp)
total_weight = total_weight - weight[i]
end
@value.delete_at(i)
@weight.delete_at(i)
end
end
end
@weight = [110, 210, 310, 330, 150]
@value = [20, 30, 25, 15, 35]
total_weight = 600
@packedvan = []
@finalweight = 0
@finalvalue = 0
pack_van_greedy(@weight, @value, total_weight)
puts 'Optimal Solution: '
@packedvan.each do |package|
puts 'Added package ' << package[0].to_s << ' - Weight: ' << package[1].to_s << ' Value: ' << package[2].to_s
end
puts 'Total Weight: ' << @finalweight.to_s << ' Total Value: ' << @finalvalue.to_s