Skip to content

Commit

Permalink
(drivers) convert-c-array-to-cpp-containers and use them
Browse files Browse the repository at this point in the history
  • Loading branch information
cvvergara committed Aug 29, 2024
1 parent 8aeb351 commit 09767cd
Show file tree
Hide file tree
Showing 6 changed files with 264 additions and 208 deletions.
39 changes: 24 additions & 15 deletions src/compatibleVehicles/compatibleVehicles_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,15 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#include <sstream>
#include <string>
#include <utility>
#include <vector>

#include "c_types/compatibleVehicles_rt.h"

#include "cpp_common/alloc.hpp"
#include "cpp_common/assert.hpp"

#include "cpp_common/matrix_cell_t.hpp"
#include "cpp_common/time_multipliers_t.hpp"
#include "cpp_common/orders_t.hpp"
#include "cpp_common/vehicle_t.hpp"
#include "problem/pickDeliver.hpp"
Expand Down Expand Up @@ -67,6 +70,8 @@ vrp_do_compatibleVehicles(
std::ostringstream notice;
std::ostringstream err;
try {
using Matrix = vrprouting::problem::Matrix;

/*
* verify preconditions
*/
Expand All @@ -75,32 +80,40 @@ vrp_do_compatibleVehicles(
pgassert(!(*err_msg));
pgassert(total_orders);
pgassert(total_vehicles);
pgassert(total_vehicles);
pgassert(total_cells);
pgassert(*return_count == 0);
pgassert(!(*return_tuples));

/* Data input starts */

/*
* transform to C++ containers
*/
std::vector<Vehicle_t> vehicles(vehicles_arr, vehicles_arr + total_vehicles);
std::vector<Orders_t> orders(orders_arr, orders_arr + total_orders);
std::vector<Matrix_cell_t> costs(matrix_cells_arr, matrix_cells_arr + total_cells);
std::vector<Time_multipliers_t> multipliers(multipliers_arr, multipliers_arr + total_multipliers);

/* Data input ends */

/* Processing starts */

Identifiers<Id> node_ids;

for (size_t i = 0; i < total_orders; ++i) {
auto o = orders_arr[i];
for (const auto &o : orders) {
node_ids += o.pick_node_id;
node_ids += o.deliver_node_id;
}

for (size_t i = 0; i < total_vehicles; ++i) {
auto v = vehicles_arr[i];
for (const auto &v : vehicles) {
node_ids += v.start_node_id;
node_ids += v.end_node_id;
}

/*
* Prepare matrix
*/
vrprouting::problem::Matrix matrix(
matrix_cells_arr, total_cells,
multipliers_arr, total_multipliers,
node_ids, static_cast<Multiplier>(factor));
Matrix matrix(costs, multipliers, node_ids, static_cast<Multiplier>(factor));

/*
* Verify matrix triangle inequality
Expand All @@ -126,11 +139,7 @@ vrp_do_compatibleVehicles(
/*
* Construct problem
*/
log << "Initialize problem\n";
vrprouting::problem::PickDeliver pd_problem(
orders_arr, total_orders,
vehicles_arr, total_vehicles,
matrix);
vrprouting::problem::PickDeliver pd_problem(orders, vehicles, matrix);

if (pd_problem.msg.has_error()) {
*log_msg = to_pg_msg(pd_problem.msg.get_log());
Expand All @@ -153,7 +162,7 @@ vrp_do_compatibleVehicles(
*/
if (!solution.empty()) {
(*return_tuples) = alloc(solution.size(), (*return_tuples));
int seq = 0;
size_t seq = 0;
for (const auto &row : solution) {
(*return_tuples)[seq] = row;
++seq;
Expand Down
Loading

0 comments on commit 09767cd

Please sign in to comment.