-
Notifications
You must be signed in to change notification settings - Fork 0
/
field.rb
62 lines (57 loc) · 2.11 KB
/
field.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
require './truck.rb'
require './cargo.rb'
require './warehouse.rb'
require 'csv'
class Field
attr_reader :time
def initialize(bound: , initial_orders: , interval: , time_loss: )
@bound = bound
@time = 0
@interval = interval
@time_loss = time_loss
bounds = @bound.quadrisection
@trucks = bounds.map{|bound| Truck.new(bound: bound, time: 0, time_loss: 300)}
@orders = []
initial_orders.times do
order = Cargo.new(position: bound.random_inner_position, destination: bound.random_inner_position , time: 0)
@orders.push(order)
end
@warehouse = Warehouse.new(position: @bound.center, trucks: @trucks)
@finished_cargos = []
end
def action
truck = @trucks.min_by{ |truck| truck.time }
finished = truck.move(@orders)
@finished_cargos.push(finished) if finished
new_time = @trucks.min_by{ |truck| truck.time }.time
check_interval(new_time)
@warehouse.check_trucks
end
def check_interval(new_time)
cargo_made = (new_time / @interval) - (time / @interval)
cargo_made.times do
order = Cargo.new(position: @bound.random_inner_position, destination: @bound.random_inner_position , time: @time)
@orders.push(order)
end
@time = new_time
end
def output_all
order_count = @orders.length
carried_cargo_count = @trucks.inject(0){|sum, truck| sum + truck.cargos.length}
finished_count = @finished_cargos.length
in_time_count = @finished_cargos.count{ |cargo| cargo.in_time?}
historical_cargos = []
CSV.open("report.csv", "w") do |csv|
csv << ["一回の荷物のやり取りでのタイムロス","注文の発生間隔","未回収荷物の数", "配達中荷物の数", "配達済み荷物の数", "時間内に届けられた荷物の数"]
csv << ["#{@time_loss}秒","#{@interval}秒",order_count, carried_cargo_count, finished_count, in_time_count]
end
@trucks.each.with_index do |truck, i|
truck.output_history(i)
historical_cargos += truck.cargos
end
historical_cargos += @finished_cargos
historical_cargos.each.with_index do |cargo, i|
cargo.output_history(i)
end
end
end