Allow the LossFunction contained in a LossFunctionWrapper to be NULL.

This is consistent with how NULL LossFunctions are treated everywhere
else.

Change-Id: Ic91e39ccb13137fcad7f85e78613a29ecde30d67
This commit is contained in:
Simon Rutishauser
2015-05-19 12:44:23 +02:00
parent 9064b4ed27
commit 1e241b7cc3
2 changed files with 29 additions and 2 deletions
+11 -2
View File
@@ -358,6 +358,9 @@ class CERES_EXPORT ScaledLoss : public LossFunction {
// whose scale can be mutated after an optimization problem has been
// constructed.
//
// Since we treat the a NULL Loss function as the Identity loss
// function, rho = NULL is a valid input.
//
// Example usage
//
// Problem problem;
@@ -394,8 +397,14 @@ class CERES_EXPORT LossFunctionWrapper : public LossFunction {
}
virtual void Evaluate(double sq_norm, double out[3]) const {
CHECK_NOTNULL(rho_.get());
rho_->Evaluate(sq_norm, out);
if (rho_.get() == NULL) {
out[0] = sq_norm;
out[1] = 1.0;
out[2] = 0.0;
}
else {
rho_->Evaluate(sq_norm, out);
}
}
void Reset(LossFunction* rho, Ownership ownership) {
+18
View File
@@ -228,6 +228,24 @@ TEST(LossFunction, LossFunctionWrapper) {
for (int i = 0; i < 3; ++i) {
EXPECT_NEAR(rho[i], rho_gold[i], 1e-12);
}
// Set to NULL
TrivialLoss loss_function4;
loss_function_wrapper.Reset(NULL, TAKE_OWNERSHIP);
loss_function_wrapper.Evaluate(s, rho);
loss_function4.Evaluate(s, rho_gold);
for (int i = 0; i < 3; ++i) {
EXPECT_NEAR(rho[i], rho_gold[i], 1e-12);
}
// Set to NULL, not taking ownership
loss_function_wrapper.Reset(NULL, DO_NOT_TAKE_OWNERSHIP);
loss_function_wrapper.Evaluate(s, rho);
loss_function4.Evaluate(s, rho_gold);
for (int i = 0; i < 3; ++i) {
EXPECT_NEAR(rho[i], rho_gold[i], 1e-12);
}
}
} // namespace internal