Skip to content

Commit

Permalink
Apply positions only for concerned activities
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Graber authored and alain-andre committed Jan 19, 2023
1 parent 7fe3b55 commit 43f8bd6
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 17 deletions.
1 change: 1 addition & 0 deletions ortools_vrp.proto
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ message Service {
repeated bool refill_quantities = 14;
uint32 problem_index = 15;
string point_id = 16;
uint32 alternative_index = 17;
}

message Rest {
Expand Down
1 change: 1 addition & 0 deletions ortools_vrp_pb.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 34 additions & 17 deletions tsp_simple.cc
Original file line number Diff line number Diff line change
Expand Up @@ -721,11 +721,16 @@ void RelationBuilder(const TSPTWDataDT& data, RoutingModel& routing,
break;
case NeverFirst:
for (int link_index = 0; link_index < relation.linked_ids.size(); ++link_index) {
current_index = data.IdIndex(relation.linked_ids[link_index]);
int32_t service_index =
data.ProblemIndex(RoutingIndexManager::NodeIndex(current_index));
alternative_size = data.AlternativeSize(service_index);

current_index = data.AlternativeActivityIndex(relation.linked_ids[link_index]);
if (current_index >= 0) {
alternative_size =
data.AlternativeActivitySize(relation.linked_ids[link_index]);
} else {
current_index = data.IdIndex(relation.linked_ids[link_index]);
alternative_size = data.AlternativeSize(current_index);
}
std::cout << "alternative_size :" << alternative_size
<< " current_index : " << current_index << std::endl;
for (int64_t alternative_index = current_index;
alternative_index < current_index + alternative_size; ++alternative_index) {
for (std::size_t v = 0; v < data.Vehicles().size(); ++v) {
Expand All @@ -743,10 +748,14 @@ void RelationBuilder(const TSPTWDataDT& data, RoutingModel& routing,
}

for (int link_index = 0; link_index < relation.linked_ids.size(); ++link_index) {
current_index = data.IdIndex(relation.linked_ids[link_index]);
int32_t service_index =
data.ProblemIndex(RoutingIndexManager::NodeIndex(current_index));
alternative_size = data.AlternativeSize(service_index);
current_index = data.AlternativeActivityIndex(relation.linked_ids[link_index]);
if (current_index >= 0) {
alternative_size =
data.AlternativeActivitySize(relation.linked_ids[link_index]);
} else {
current_index = data.IdIndex(relation.linked_ids[link_index]);
alternative_size = data.AlternativeSize(current_index);
}

for (int64_t alternative_index = current_index;
alternative_index < current_index + alternative_size; ++alternative_index) {
Expand All @@ -765,10 +774,14 @@ void RelationBuilder(const TSPTWDataDT& data, RoutingModel& routing,
break;
case NeverLast:
for (int link_index = 0; link_index < relation.linked_ids.size(); ++link_index) {
current_index = data.IdIndex(relation.linked_ids[link_index]);
int32_t service_index =
data.ProblemIndex(RoutingIndexManager::NodeIndex(current_index));
alternative_size = data.AlternativeSize(service_index);
current_index = data.AlternativeActivityIndex(relation.linked_ids[link_index]);
if (current_index >= 0) {
alternative_size =
data.AlternativeActivitySize(relation.linked_ids[link_index]);
} else {
current_index = data.IdIndex(relation.linked_ids[link_index]);
alternative_size = data.AlternativeSize(current_index);
}

for (int64_t alternative_index = current_index;
alternative_index < current_index + alternative_size; ++alternative_index) {
Expand All @@ -782,10 +795,14 @@ void RelationBuilder(const TSPTWDataDT& data, RoutingModel& routing,
break;
case ForceLast:
for (int link_index = 0; link_index < relation.linked_ids.size(); ++link_index) {
current_index = data.IdIndex(relation.linked_ids[link_index]);
int32_t service_index =
data.ProblemIndex(RoutingIndexManager::NodeIndex(current_index));
alternative_size = data.AlternativeSize(service_index);
current_index = data.AlternativeActivityIndex(relation.linked_ids[link_index]);
if (current_index >= 0) {
alternative_size =
data.AlternativeActivitySize(relation.linked_ids[link_index]);
} else {
current_index = data.IdIndex(relation.linked_ids[link_index]);
alternative_size = data.AlternativeSize(current_index);
}

for (int64_t alternative_index = current_index;
alternative_index < current_index + alternative_size; ++alternative_index) {
Expand Down
48 changes: 48 additions & 0 deletions tsptw_data_dt.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,20 @@ class TSPTWDataDT {
return tsptw_clients_[i.value()].alternative_index;
}

int32_t AlternativeActivityIndex(const std::string id) const {
if (alternative_activity_ids_map_.find(id) != alternative_activity_ids_map_.end()) {
return alternative_activity_ids_map_.at(id);
}
return -1;
}

int32_t AlternativeActivitySize(const std::string id) const {
if (alternative_activity_size_map_.find(id) != alternative_activity_size_map_.end()) {
return alternative_activity_size_map_.at(id);
}
return 0;
}

const std::vector<int64_t>& ReadyTime(const RoutingIndexManager::NodeIndex i) const {
return tsptw_clients_[i.value()].ready_time;
}
Expand Down Expand Up @@ -595,6 +609,7 @@ class TSPTWDataDT {
int32_t matrix_index;
int32_t problem_index;
int32_t alternative_index;
int32_t alternative_activity_index;
std::vector<int64_t> ready_time;
std::vector<int64_t> due_time;
std::vector<int64_t> maximum_lateness;
Expand Down Expand Up @@ -638,6 +653,8 @@ class TSPTWDataDT {
std::vector<Relation> tsptw_relations_;
std::vector<TSPTWClient> tsptw_clients_;
std::map<int32_t, int32_t> alternative_size_map_;
std::map<std::string, int32_t> alternative_activity_size_map_;
std::map<std::string, int32_t> alternative_activity_ids_map_;
std::vector<Route> tsptw_routes_;
std::vector<int> vehicles_day_;
std::vector<int64_t> service_times_;
Expand Down Expand Up @@ -750,6 +767,23 @@ void TSPTWDataDT::LoadInstance(const std::string& filename) {
alternative_size_map_[service.problem_index()] += 1;
if (ids_map_.find((std::string)service.id()) == ids_map_.end())
ids_map_[(std::string)service.id()] = node_index;

if (alternative_activity_ids_map_.find(
(std::string)service.id() + "#" +
std::to_string(service.alternative_index())) ==
alternative_activity_ids_map_.end()) {
alternative_activity_ids_map_[service.id() + "#" +
std::to_string(service.alternative_index())] =
node_index;
alternative_activity_size_map_[service.id() + "#" +
std::to_string(service.alternative_index())] =
1;
} else {
alternative_activity_size_map_[service.id() + "#" +
std::to_string(service.alternative_index())] +=
1;
}

node_index++;
}
++timewindow_index;
Expand Down Expand Up @@ -790,6 +824,20 @@ void TSPTWDataDT::LoadInstance(const std::string& filename) {
alternative_size_map_[service.problem_index()] += 1;
if (ids_map_.find((std::string)service.id()) == ids_map_.end())
ids_map_[(std::string)service.id()] = node_index;

if (alternative_activity_ids_map_.find(
(std::string)service.id() + "#" +
std::to_string(service.alternative_index())) ==
alternative_activity_ids_map_.end()) {
alternative_activity_ids_map_[service.id() + "#" +
std::to_string(service.alternative_index())] =
node_index;
alternative_activity_size_map_[service.id() + "#" +
std::to_string(service.alternative_index())] = 1;
} else {
alternative_activity_size_map_[service.id() + "#" +
std::to_string(service.alternative_index())] += 1;
}
node_index++;
}
if (previous_matrix_size == (int32)service_matrix_indices.size()) {
Expand Down

0 comments on commit 43f8bd6

Please sign in to comment.