Skip to content

Commit

Permalink
Provide rest timewindows compatible with vehicle
Browse files Browse the repository at this point in the history
  • Loading branch information
fonsecadeline committed Sep 17, 2021
1 parent c8efa30 commit 3e30418
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
13 changes: 13 additions & 0 deletions models/timewindow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,18 @@ class Timewindow < Base
# validates_numericality_of :start, allow_nil: true
# validates_numericality_of :end, allow_nil: true, greater_than: :start, if: :start
# validates_numericality_of :day_index, allow_nil: true

def compatible_with(timewindow, lateness, check_days = false)
raise unless timewindow.is_a?(Models::Timewindow)

return false if check_days &&
self.day_index && timewindow.day_index &&
self.day_index != timewindow.day_index

return true if lateness

(self.end.nil? || timewindow.start.nil? || timewindow.start <= self.end) &&
(self.start.nil? || timewindow.end.nil? || timewindow.end >= self.start)
end
end
end
46 changes: 46 additions & 0 deletions test/models/timewindow_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Copyright © Mapotempo, 2021
#
# This file is part of Mapotempo.
#
# Mapotempo is free software. You can redistribute it and/or
# modify since you respect the terms of the GNU Affero General
# Public License as published by the Free Software Foundation,
# either version 3 of the License, or (at your option) any later version.
#
# Mapotempo is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the Licenses for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Mapotempo. If not, see:
# <http://www.gnu.org/licenses/agpl.html>
#
require './test/test_helper'

module Models
class TimewindowTest < Minitest::Test
include Rack::Test::Methods

def test_compatibility_between_timewindows
tw1 = Models::Timewindow.new(start: 10, end: 20, day_index: 0)
assert_raises RuntimeError do
tw1.compatible_with([0, 10], true)
end
assert tw1.compatible_with(tw1, false)

tw2 = Models::Timewindow.new(start: 10, end: 20, day_index: 1)
assert tw1.compatible_with(tw2, false)
refute tw1.compatible_with(tw2, false, true)
refute tw1.compatible_with(tw2, true, true) # lateness has no impact on days incompatibility

# ignore days :
tw2.start = 21
tw2.end = 25
refute tw1.compatible_with(tw2, false)
tw2.start = 5
assert tw1.compatible_with(tw2, false)
tw2.end = 8
refute tw1.compatible_with(tw2, false)
end
end
end
2 changes: 1 addition & 1 deletion wrappers/ortools.rb
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ def solve(vrp, job, thread_proc = nil, &block)
),
rests: vehicle.rests.collect{ |rest|
OrtoolsVrp::Rest.new(
time_windows: rest.timewindows.collect{ |tw|
time_windows: rest.timewindows.select{ |tw| tw.compatible_with(vehicle.timewindow, false) }.collect{ |tw|
OrtoolsVrp::TimeWindow.new(start: tw.start, end: tw.end || 2**56)
},
duration: rest.duration,
Expand Down

0 comments on commit 3e30418

Please sign in to comment.