I am testing my Solution class, which is as follows:
class Solution {
public:
/**
* @brief Constructor for Solution.
*
* @param cost The cost of the solution.
* @param y Vector indicating if there is a warehouse at site i (1 or 0).
* @param x Matrix indicating if customer j is served by warehouse i (1 or 0).
*/
Solution(double cost, vector<bool>& y, vector<vector<int>>& x);
/**
* @brief Gets the cost of the solution.
*
* @return double The cost of the solution.
*/
double getCost() ;
/**
* @brief Sets the cost of the solution.
*
* @param cost The new cost of the solution.
*/
void setCost(double cost);
/**
* @brief Gets the warehouse assignment vector.
*
* @return vector<uint8_t>& The warehouse assignment vector.
*/
vector<bool>& getY() ;
/**
* @brief Sets the warehouse assignment vector.
*
* @param y The new warehouse assignment vector.
*/
void setY(vector<bool>& y);
/**
* @brief Gets the customer assignment matrix.
*
* @return vector<vector<uint8_t>>& The customer assignment matrix.
*/
vector<vector<int>>& getX() ;
/**
* @brief Sets the customer assignment matrix.
*
* @param x The new customer assignment matrix.
*/
void setX(vector<vector<int>>& x);
/**
* @brief Compares two solutions.
*
* @param other Another instance of Solution to compare.
* @return true If the solutions are equal.
* @return false If the solutions are different.
*/
bool operator==(Solution& other) ;
/**
* @brief Calculate the cost of the solution given an instance.
*
* @param instance The instance for which to calculate the solution cost.
*/
void calculateCost(Instance& instance);
/**
* @brief Converts the solution to a string representation.
*
* @return string The string representation of the solution.
*/
string toString() ;
/**
* @brief Checks if the solution is feasible.
*
* @param instance The instance to check the solution against.
* @return true If the solution is feasible.
* @return false If the solution is not feasible.
*/
bool isFeasible(Instance& instance);
private:
/**
* @brief Calculates the total demand of customers.
*
* @param instance The instance to calculate the demand from.
* @return int The total demand.
*/
int calculateTotalDemand(Instance& instance);
double cost; ///< The cost of the solution.
vector<bool>& y; ///< Warehouse assignment vector.
vector<vector<int>>& x; ///< Customer assignment matrix.
int totalDemand; ///< Total demand of customers.
bool demandCalculated; ///< Flag to check if the total demand has been calculated.
};
#endif // SOLUTION_H
The implementation of the class is as follows (I will omit the methods that are not important)
Solution::Solution(double cost, vector<bool>& y, vector<vector<int>>& x)
: cost(cost), y(y), x(x), totalDemand(0), demandCalculated(false) {}
vector<bool>& Solution::getY() {
return this->y;
}
void Solution::setY(vector<bool>& y) {
this->y = y;
}
the test that fails is getY()
namespace {
int randomInt(int min, int max) {
static std::mt19937 rng(std::random_device{}());
std::uniform_int_distribution<int> dist(min, max);
return dist(rng);
}
double randomDouble(double min, double max) {
static std::mt19937 rng(std::random_device{}());
std::uniform_real_distribution<double> dist(min, max);
return dist(rng);
}
Instance createRandomInstance() {
int numFacilities = randomInt(5, 10);
int numCustomers = randomInt(5, 10);
vector<int> facilityCapacities(numFacilities);
vector<double> openingCosts(numFacilities);
vector<int> customerDemands(numCustomers);
vector<vector<double>> transportationCosts(numFacilities, vector<double>(numCustomers));
for (int i = 0; i < numFacilities; ++i) {
facilityCapacities[i] = randomInt(50, 200);
openingCosts[i] = randomDouble(1000.0, 5000.0);
}
for (int j = 0; j < numCustomers; ++j) {
customerDemands[j] = randomInt(10, 100);
for (int i = 0; i < numFacilities; ++i) {
transportationCosts[i][j] = randomDouble(10.0, 50.0);
}
}
vector<bool> y(numFacilities);
vector<vector<int>> x(numFacilities, vector<int>(numCustomers));
Solution bestSolution(0.0, y, x);
return Instance(numFacilities, numCustomers, facilityCapacities, customerDemands, openingCosts, transportationCosts, bestSolution);
}
Solution createRandomSolution(int numFacilities, int numCustomers) {
vector<bool> y(numFacilities);
vector<vector<int>> x(numFacilities, vector<int>(numCustomers));
for (int i = 0; i < numFacilities; ++i) {
y[i] = randomInt(0, 1);
for (int j = 0; j < numCustomers; ++j) {
x[i][j] = randomInt(0, 1);
}
}
double cost = randomDouble(100.0, 10000.0);
return Solution(cost, y, x);
}
Instance randomInstance = createRandomInstance();
Solution randomSolution = createRandomSolution(randomInstance.getNumFacilities(), randomInstance.getNumCustomers());
}
TEST(SolutionTest, GetYTest) {
const vector<bool>& y = randomSolution.getY();
EXPECT_EQ(y.size(), randomInstance.getNumFacilities());
}
TEST(SolutionTest, SetYTest) {
vector<bool> newY(randomInstance.getNumFacilities());
for (size_t i = 0; i < newY.size(); ++i) {
newY[i] = randomInt(0, 1);
}
cout << "newY: " << newY.size() << endl;
randomSolution.setY(newY);
cout << "randomSolution: " << randomSolution.getY().size() << endl;
cout << "newY: " << newY.size() << endl;
EXPECT_EQ(randomSolution.getY(), newY);
}
The createRandomSolution
function properly creates the solution objects. The problem is that when invoking the getY()
method, it does not return the array that was created in the function. It is even received correctly in the class constructor. The output of my test is the following:
[ RUN ] SolutionTest.GetYTest
y.size()
Which is: 1124207380080384
randomInstance.getNumFacilities()
Which is: 7
[ RUN ] SolutionTest.SetYTest
Expected equality of these values:
randomSolution.getY()
Which is: { false, false, false, false, false, false, false, false, true, false, false, false, true, true, true, false, false, false, false, true, true, false, true, false, false, true, true, true, false, true, false, true, ... }
newY
Which is: { false, true, true, true, false, true, false }
I have no idea where the problem comes from, I hope you can guide me to solve the problem.