Skip to content

Commit

Permalink
CostDetails calculates totals expressively
Browse files Browse the repository at this point in the history
  • Loading branch information
senhalil committed Aug 2, 2021
1 parent 1fae9cb commit efce5ca
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 25 deletions.
34 changes: 20 additions & 14 deletions models/solution/cost_details.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,26 @@

module Models
class CostDetails < Base
field :total, default: 0
field :fixed, default: 0
field :time, default: 0
field :distance, default: 0
field :value, default: 0
field :lateness, default: 0
field :overload, default: 0
field :fixed, default: 0
field :time, default: 0
field :distance, default: 0
field :value, default: 0
field :lateness, default: 0
field :overload, default: 0

def +(other)
merged_cost = CostDetails.create({})
self.attributes.each_key{ |key|
merged_cost[key] = (self[key] || 0) + (other[key] || 0)
}
merged_cost
end
def total
fixed + time + distance + value + lateness + overload
end

def +(other)
CostDetails.create(
fixed: fixed + other.fixed,
time: time + other.time,
distance: distance + other.distance,
value: value + other.value,
lateness: lateness + other.lateness,
overload: overload + other.overload,
)
end
end
end
4 changes: 1 addition & 3 deletions wrappers/ortools.rb
Original file line number Diff line number Diff line change
Expand Up @@ -385,16 +385,14 @@ def kill
private

def build_cost_details(cost_details)
cost = Models::CostDetails.create(
Models::CostDetails.create(
fixed: cost_details&.fixed || 0,
time: cost_details && (cost_details.time + cost_details.time_fake + cost_details.time_without_wait) || 0,
distance: cost_details && (cost_details.distance + cost_details.distance_fake) || 0,
value: cost_details&.value || 0,
lateness: cost_details&.lateness || 0,
overload: cost_details&.overload || 0
)
cost.total = cost.attributes.values.sum
cost
end

def check_services_compatible_days(vrp, vehicle, service)
Expand Down
11 changes: 3 additions & 8 deletions wrappers/wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1048,14 +1048,9 @@ def simplify_vehicle_pause(vrp, result = nil, options = { mode: :simplify })
cost_increase = vehicle.cost_time_multiplier.to_f * rest.duration +
vehicle.cost_waiting_time_multiplier.to_f * idle_time_created_by_inserted_pause

if route[:cost_details]
route[:cost_details].time += cost_increase
route[:cost_details].total += cost_increase
end
if result[:cost_details]
result[:cost_details].time += cost_increase
result[:cost_details].total += cost_increase
end

route[:cost_details]&.time += cost_increase
result[:cost_details]&.time += cost_increase
result[:cost] += cost_increase # totals are not calculated yet
}
}
Expand Down

0 comments on commit efce5ca

Please sign in to comment.