Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions include/packingsolver/algorithms/truck.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <fstream>
#include <iomanip>
#include <stdexcept>

namespace packingsolver
{
Expand Down Expand Up @@ -87,6 +88,33 @@ struct SemiTrailerTruckData
return {middle_axle_weight, rear_axle_weight};
}

/**
* Check that the provided truck data is consistent.
*
* A bin type that is not a semi-trailer truck needs no truck data:
* compute_axle_weights returns {0, 0} without looking at any other
* field, so the other fields can hold any value. A semi-trailer truck
* needs 'harness_rear_axle_distance' and 'front_axle_middle_axle_distance'
* to compute the rear and middle axle weights.
*/
void check() const
{
if (!is)
return;
if (harness_rear_axle_distance <= 0) {
throw std::invalid_argument(
FUNC_SIGNATURE + ": "
"a semi-trailer truck bin type must have a strictly "
"positive 'harness_rear_axle_distance'.");
}
if (front_axle_middle_axle_distance <= 0) {
throw std::invalid_argument(
FUNC_SIGNATURE + ": "
"a semi-trailer truck bin type must have a strictly "
"positive 'front_axle_middle_axle_distance'.");
}
}

void read(std::string label, std::string value)
{
if (label == "IS_SEMI_TRAILER_TRUCK") {
Expand Down
3 changes: 2 additions & 1 deletion src/boxstacks/instance_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,6 @@ void InstanceBuilder::read_bin_types(
double maximum_stack_density = std::numeric_limits<double>::max();

SemiTrailerTruckData semi_trailer_truck_data;
semi_trailer_truck_data.is = true;

for (Counter i = 0; i < (Counter)line.size(); ++i) {
if (labels[i] == "X") {
Expand Down Expand Up @@ -1001,6 +1000,8 @@ Instance InstanceBuilder::build()
bin_type_id < instance_.number_of_bin_types();
++bin_type_id) {
const BinType& bin_type = instance_.bin_type(bin_type_id);
// Check truck data consistency.
bin_type.semi_trailer_truck_data.check();
// Update bin_type.copies.
if (bin_type.copies == -1)
instance_.bin_types_[bin_type_id].copies = instance_.number_of_items();
Expand Down
2 changes: 2 additions & 0 deletions src/rectangle/instance_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1131,6 +1131,8 @@ Instance InstanceBuilder::build()
bin_type_id < instance_.number_of_bin_types();
++bin_type_id) {
const BinType& bin_type = instance_.bin_type(bin_type_id);
// Check truck data consistency.
bin_type.semi_trailer_truck_data.check();
// Update bin_type.copies.
if (bin_type.copies == -1)
instance_.bin_types_[bin_type_id].copies = instance_.number_of_items();
Expand Down
3 changes: 2 additions & 1 deletion test/algorithms/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
add_executable(PackingSolver_algorithms_test)
target_sources(PackingSolver_algorithms_test PRIVATE
meet_in_the_middle_test.cpp)
meet_in_the_middle_test.cpp
truck_test.cpp)
target_include_directories(PackingSolver_algorithms_test PRIVATE
${PROJECT_SOURCE_DIR}/src)
target_link_libraries(PackingSolver_algorithms_test
Expand Down
64 changes: 64 additions & 0 deletions test/algorithms/truck_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include "packingsolver/algorithms/truck.hpp"

#include <gtest/gtest.h>

#include <stdexcept>

using namespace packingsolver;

namespace
{

/** Semi-trailer truck with a complete geometry. */
SemiTrailerTruckData complete_truck()
{
SemiTrailerTruckData semi_trailer_truck_data;
semi_trailer_truck_data.is = true;
semi_trailer_truck_data.tractor_weight = 8000;
semi_trailer_truck_data.front_axle_middle_axle_distance = 380;
semi_trailer_truck_data.front_axle_tractor_gravity_center_distance = 100;
semi_trailer_truck_data.front_axle_harness_distance = 320;
semi_trailer_truck_data.empty_trailer_weight = 6000;
semi_trailer_truck_data.harness_rear_axle_distance = 800;
semi_trailer_truck_data.trailer_gravity_center_rear_axle_distance = 400;
semi_trailer_truck_data.trailer_start_harness_distance = 100;
return semi_trailer_truck_data;
}

}

TEST(Truck, ComputeAxleWeights)
{
SemiTrailerTruckData semi_trailer_truck_data = complete_truck();
std::pair<Weight, Weight> axle_weights
= semi_trailer_truck_data.compute_axle_weights(200000, 2000);
EXPECT_NEAR(axle_weights.first, 6315.789473684211, 1e-6);
EXPECT_NEAR(axle_weights.second, 3000.0, 1e-6);
}

TEST(Truck, CheckNotATruck)
{
// A bin type that isn't a semi-trailer truck needs no truck data.
SemiTrailerTruckData semi_trailer_truck_data;
EXPECT_NO_THROW(semi_trailer_truck_data.check());
}

TEST(Truck, CheckWithoutHarnessRearAxleDistance)
{
// Every axle weight is computed from 'harness_weight', which divides by
// 'harness_rear_axle_distance', so a semi-trailer truck without it is an
// inconsistent input, not a case to silently skip.
SemiTrailerTruckData semi_trailer_truck_data = complete_truck();
semi_trailer_truck_data.harness_rear_axle_distance = 0;
EXPECT_THROW(semi_trailer_truck_data.check(), std::invalid_argument);
}

TEST(Truck, CheckWithoutFrontAxleMiddleAxleDistance)
{
// The middle axle weight is computed from 'harness_weight', which
// divides by 'front_axle_middle_axle_distance', so a semi-trailer truck
// without it is an inconsistent input, not a case to silently skip.
SemiTrailerTruckData semi_trailer_truck_data = complete_truck();
semi_trailer_truck_data.front_axle_middle_axle_distance = 0;
EXPECT_THROW(semi_trailer_truck_data.check(), std::invalid_argument);
}
Loading