Skip to content

Commit

Permalink
Allow direct dump/load of vrp/solution as json
Browse files Browse the repository at this point in the history
  • Loading branch information
Braktar committed Jul 21, 2021
1 parent 3872981 commit 41e3605
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ GIT

GIT
remote: https://github.com/mapotempo/active_hash.git
revision: d04b246227675806f040be09e7bf0dfaa8ffc590
revision: 87339816c202ffe96517467578ad0ed989321460
branch: dev
specs:
active_hash (3.1.0)
Expand Down
15 changes: 15 additions & 0 deletions models/concerns/validate_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def check_consistency(hash)
@hash = hash

ensure_no_conflicting_skills
ensure_uniq_ids

configuration = @hash[:configuration]
schedule = configuration[:schedule] if configuration
Expand Down Expand Up @@ -58,6 +59,20 @@ def ensure_no_conflicting_skills
)
end

def ensure_uniq_ids
# Active Hash now can check this, but won't raise in case of identical objects
# Due to this PR https://github.com/Mapotempo/active_hash/pull/5
# This allows internally to dump/load the vrp/solution directly as json
[:matrices, :units, :points, :rests, :zones, :timewindows,
:vehicles, :services, :shipments, :subtours].each{ |key|
next if @hash[key].to_a.collect{ |v| v[:id] }.uniq!.nil?

raise OptimizerWrapper::DiscordantProblemError.new(
"#{key} ids should be unique"
)
}
end

def check_matrices
# matrix_index consistency
if @hash[:matrices].nil? || @hash[:matrices].empty?
Expand Down
2 changes: 2 additions & 0 deletions models/solution/step.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ def initialize(object, options = {})
Parsers::RestParser.parse(object, options)
when 'Models::Point'
Parsers::PointParser.parse(object, options)
when 'Hash'
object # Allow direct loading of json solution
else
raise 'Unknown step class'
end
Expand Down
Binary file not shown.

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions test/lib/interpreters/split_clustering_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -611,10 +611,11 @@ def test_max_split_size_with_empty_fill

def test_max_split_poorly_populated_route_limit_result
vrp = TestHelper.load_vrp(self, fixture_file: 'max_split_functionality')
result = Marshal.load(File.binread('test/fixtures/max_split_poorly_populated_route_limit_result.bindump')) # rubocop: disable Security/MarshalLoad
Interpreters::SplitClustering.remove_poor_routes(vrp, result)
result = JSON.parse(File.read('test/fixtures/max_split_poorly_populated_route_limit_result.json'))
solution = Models::Solution.create(result)
Interpreters::SplitClustering.remove_poor_routes(vrp, solution)

assert_equal 0, result.unassigned.size, 'remove_poor_routes should not remove any services from this result'
assert_equal 0, solution.unassigned.size, 'remove_poor_routes should not remove any services from this result'
end

def test_max_split_functionality
Expand Down
4 changes: 2 additions & 2 deletions test/models/vrp_consistency_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ def test_duplicated_ids_are_not_allowed
].each{ |symbol|
vrp = Oj.load(vrp_base)
vrp[symbol] << vrp[symbol].first
assert_raises ActiveHash::IdError do TestHelper.create(vrp) end
assert_raises OptimizerWrapper::DiscordantProblemError do TestHelper.create(vrp) end
}
end

Expand Down Expand Up @@ -371,7 +371,7 @@ def test_reject_when_duplicated_ids
vrp = VRP.toy
vrp[:services] << vrp[:services].first

assert_raises ActiveHash::IdError do
assert_raises OptimizerWrapper::DiscordantProblemError do
OptimizerWrapper.wrapper_vrp('demo', { services: { vrp: [:demo] }}, TestHelper.create(vrp), nil)
end
end
Expand Down

0 comments on commit 41e3605

Please sign in to comment.