How do I use a Merit function in Gecode?

128 Views Asked by At

I am trying to use a merit function for my branching in Gecode. In the MPG, the Gecode Manual, an example merit function is stated, and how to pass it to the branching. But I cannot figure out where to put the function. Should it be inside the script or outside? Right now I have put it next to the copy function, etc. I cannot find any example code where someone uses a merit function.

I get the following error:

program.cpp(247): error C2059: syntax error: '}'                                                          
program.cpp(247): error C2853: 'm': a non-static data member cannot have a type that contains 'auto'      
program.cpp(259): fatal error C1004: unexpected end-of-file found   

This is the code I am trying out:

    // ...
    branch(*this, workers, BOOL_VAR_MERIT_MIN(m), BOOL_VAL_MAX());
}

auto m = [](const Space& home, BoolVar x, int i) {
    return i;
}
// ...

I know it is stupid to make a merit function that just returns the index, I am just trying to make the simplest merit function to work before I do what I want to do.

1

There are 1 best solutions below

0
On BEST ANSWER

According to the Gecode documentation the merit function should return a double. As suggested by the type definition of BoolBranchMerit:

typedef std::function<double(const Space& home, BoolVar x, int i)> Gecode::BoolBranchMerit

To be safe, you might also want to declare m as being an Gecode::BoolBranchMerit. So I think the following should fix your example:

    // ...
    branch(*this, workers, BOOL_VAR_MERIT_MIN(m), BOOL_VAL_MAX());
}

BoolBranchMerit m = [](const Space& home, BoolVar x, int i) -> double {
    return (double) i;
}
// ...