PySCIPOpt icon indicating copy to clipboard operation
PySCIPOpt copied to clipboard

Wrapping getVanillafullstrongData() in pyscipopt

Open kothariharshit opened this issue 4 years ago • 7 comments

I tried to wrap getVanillafullstrongData() in python, but I think I made some mistake. Can you please add this functionality or guide me on how to wrap this function. Thanks!

kothariharshit avatar Mar 15 '22 19:03 kothariharshit

This is not a public function in scip (missing the SCIP_EXPORT preamble) and is only available internally and cannot be accessed through the interface.

CGraczyk avatar Mar 16 '22 10:03 CGraczyk

the scip I have says:

/** recovers candidate variables and their scores from last vanilla full strong branching call */
SCIP_EXPORT
SCIP_RETCODE SCIPgetVanillafullstrongData(
   SCIP*                 scip,               /**< SCIP data structure */
   SCIP_VAR***           cands,              /**< pointer to store candidate variables; or NULL */
   SCIP_Real**           candscores,         /**< pointer to store candidate scores; or NULL */
   int*                  ncands,             /**< pointer to store number of candidates; or NULL */
   int*                  npriocands,         /**< pointer to store number of priority candidates; or NULL */
   int*                  bestcand            /**< pointer to store best branching candidate; or NULL */
   );

fserra avatar Mar 16 '22 13:03 fserra

You are right, i just followed the linked function of his comment and it links to the non public one!

@kothariharshit so the usual process for wrapping is that you need to add the functioncall into the scip.pxd file and then implement the python version calling this function in the scip.pyx file - can you show me what you have tried to add this function to your pyscipopt?

CGraczyk avatar Mar 16 '22 14:03 CGraczyk

Thank you for your reply. I added this to scip.pxd -

    SCIP_RETCODE SCIPincludeBranchruleVanillafullstrong(SCIP* scip)
    SCIP_RETCODE SCIPgetVanillafullstrongData(SCIP* scip,
                                              SCIP_VAR*** cands,
                                              SCIP_Real** candscores,
                                              int* ncands,
                                              int* npriocands,
                                              int* bestcand)

And added this to scip.pyx (I have used the code from https://github.com/scipopt/PySCIPOpt/issues/399#issuecomment-654932515) -

def getVanillafullstrongData(self):
        cdef SCIP_VAR** cands
        cdef SCIP_Real* candscores
        cdef int        ncands
        cdef int        npriocands
        cdef int        bestcand
        cdef int        scip_result

        PY_SCIP_CALL(SCIPgetVanillafullstrongData(self._scip,
            &cands, &candscores, &ncands, &npriocands, &bestcand, &scip_result))

        assert cands is not NULL
        assert ncands > 0 and npriocands >= 0
        assert bestcand > -1

        return (
            [Variable.create(cands[i]) for i in range(ncands)],
            None if candscores is NULL else [candscores[i] for i in range(ncands)],
            npriocands,
            bestcand,
            scip_result
        )

Does this look correct?

kothariharshit avatar Mar 16 '22 16:03 kothariharshit

Well what does your compiler say? can you post the error when recompiling pyscipopt? The issue you quote in your edit concludes with a working wrapper for this function. So if it does not you need to provide the compiler error you have encountered.

Further have a look at README section "extending the interface" and contribution guidelines maybe for some general points.

CGraczyk avatar Mar 16 '22 16:03 CGraczyk

just like with the issue you mentioned you should probably open a pull request and see what the CI says about this.

Just from a quick look, I would say the scip_result variable is not needed... there is no scip_result argument to the function.

also you should probably initialize candscores to NULL

fserra avatar Mar 16 '22 17:03 fserra

Thank you for your reply. I figured out that the issue was not in the wrapper code, but in my understanding of the function definition. I forgot to call strong branching and hence the candidates were None. Thank you for the help!

kothariharshit avatar Mar 16 '22 23:03 kothariharshit