PySCIPOpt icon indicating copy to clipboard operation
PySCIPOpt copied to clipboard

get counted solutions

Open ramabile opened this issue 6 years ago • 11 comments

Dear SCIPers, this question has the purpose to check whether the function is already implemented somewhere. Otherwise I think I will implement it at some point.

I want to collect the counted solutions. I use this script:

from pyscipopt import Model
m = Model()
x = m.addVar("x", "B")
y = m.addVar("y", "B")
z = m.addVar("z", "B")

m.setBoolParam("constraints/countsols/collect", True)
m.hideOutput()

m.count()

nsols = m.getNCountedSols()
sols = m.getSols()
print(nsols)
print(sols)

nsols is 8 (right), but sols is empty (it should be an 8-long list). This is expected since counted solutions are not obtained with getSols. So: how to get them? I did not find anything appropriate and already implemented.

1)Looking at this: https://scip.zib.de/doc/html/COUNTER.php it seems there are several missing interfaces. Do you confirm? One step further is to implement SCIPgetCountedSparseSolutions which may not be enough.

2)Documentation suggests: https://scip.zib.de/doc/html/group__CONSHDLRS.php#ga1c3b0e8f05febeac36ad6f77ce45bf66 which writes the counted solutions. Arguably, a more Pythonic way is to store the counted solutions in a list instead of writing them (and then reading them again). Is there anything similar?

3)[SCIP side from a Python user] What is the design choice behind a dialog for a writing interface without a writing interface? If there were a writing interface, is there any design choice to avoid something like SCIPgetCountedOriginalSolutions which would be so much appreciated?

Thanks!

ramabile avatar Feb 24 '19 10:02 ramabile

Counting solutions has been implemented in SCIP as an afterthought and is more proof-of-concept than API functionality. We should change that...

mattmilten avatar Feb 27 '19 10:02 mattmilten

Thanks @mattmilten for the clarification! As a humble newcomer, I would have expected that SCIPdialogExecWriteAllsolutions calls SCIPwriteAllsolutions which on turn calls SCIPgetAllsolutions.

Please help me to understand your ellipsis. Do you suggest to change SCIP before someone changes PySCIPOpt? Is any effort to mimic the current behaviour worthy?

Thanks!

ramabile avatar Feb 28 '19 07:02 ramabile

Currently, there are no concrete plans to implement proper access to the counted solutions. So (re)implementing this in PySCIPOpt would be the way to go if you need this rather sooner than later.

mattmilten avatar Feb 28 '19 13:02 mattmilten

@ramabile Did you end up implementing this at your end?

soumyamittal avatar Apr 01 '19 20:04 soumyamittal

closed due to inactivity

mattmilten avatar Dec 28 '20 21:12 mattmilten

Has there been any update on how to get the solutions? I am able to get the sparse solutions, but converting them into the new space is difficult... It seems like access to the hashmap is difficult.

EtashGuha avatar Jan 02 '21 05:01 EtashGuha

I am not aware of any progress in this regard. If you want to share your prototype code via a pull request, we might be able to complete this.

I am reopening this issue again.

mattmilten avatar Jan 02 '21 14:01 mattmilten

Great thanks. I created a pull request under name Multiple sol #470: https://github.com/scipopt/PySCIPOpt/pull/470/files I created a new function called getSparseSols(). This is based off of https://www.scipopt.org/doc/html/cons__countsols_8c_source.php#l02693

I'm very new to this so I apologize if I havent done a whole lot.

EtashGuha avatar Jan 03 '21 03:01 EtashGuha

After some clean-up, the commits will be merged into a feature branch and we can work together on finalizing the functionality.

CGraczyk avatar Jan 08 '21 10:01 CGraczyk

Is there any news with this PR? Interested in this functionaliy (even before its merged), I tried installing it, though got a few errors regarding the extended functionality: >pip install git+https://github.com/scipopt/PySCIPOpt.git@multipleSol Collecting git+https://github.com/scipopt/PySCIPOpt.git@multipleSol Cloning https://github.com/scipopt/PySCIPOpt.git (to revision multipleSol) to ... Running command git clone -q https://github.com/scipopt/PySCIPOpt.git 'C:\Users\user\AppData\...' Running command git checkout -b multipleSol --track origin/multipleSol Branch 'multipleSol' set up to track remote branch 'multipleSol' from 'origin'. Switched to a new branch 'multipleSol' ERROR: Command errored out with exit status 1: command: '...\assignMultiSol\python.exe' -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = .... Complete output (55 lines): Using include path <C:\Program Files\SCIPOptSuite 7.0.2\include>. Using SCIP library <libscip> at <C:\Program Files\SCIPOptSuite 7.0.2\lib>. Compiling src\pyscipopt\scip.pyx because it changed. [1/1] Cythonizing src\pyscipopt\scip.pyx Error compiling Cython file: ------------------------------------------------------------ ... SCIP_Real SCIPfeastol(SCIP* scip) SCIP_RETCODE SCIPsetObjIntegral(SCIP* scip) SCIP_Real SCIPgetLocalOrigEstimate(SCIP* scip) SCIP_Real SCIPgetLocalTransEstimate(SCIP* scip) SCIP_RETCODE SCIPgetProbvarLinearSum(SCIP* scip, SCIP_VAR** vars, SCIP_Real* scalars, int* nvars, int varssize, SCIP_Real* constant, int* requiredsize, SCIP_Bool mergemultiples) SCIP_Bool SCIPsparseSolGetNextSol(SCIP_SPARSESOL* sparsesol, SCIP_Longint* sol, int nvars) ^ ------------------------------------------------------------ src\pyscipopt\scip.pxd:631:38: 'SCIP_SPARSESOL' is not a type identifier Error compiling Cython file: ------------------------------------------------------------ ... cdef extern from "scip/cons_countsols.h": SCIP_RETCODE SCIPcount(SCIP* scip) SCIP_RETCODE SCIPsetParamsCountsols(SCIP* scip) SCIP_Longint SCIPgetNCountedSols(SCIP* scip, SCIP_Bool* valid) void SCIPgetCountedSparseSols(SCIP* scip, SCIP_VAR*** vars, int* nvars, SCIP_SPARSESOL*** sols, int* nsols); ^ ------------------------------------------------------------ src\pyscipopt\scip.pxd:1613:76: 'SCIP_SPARSESOL' is not a type identifier Error compiling Cython file: ------------------------------------------------------------ ... PY_SCIP_CALL( SCIPgetProbvarLinearSum(self._scip, vars, scalars, &nvars, sparsenvars, &constant, &requiredsize, True) ) realvalue = constant; for i in range(_nallvars): # I have no idea how to get the hashmap? # https://www.scipopt.org/doc-5.0.0/html/group__SparseSol.php#gaed7a10e2bd49c6b18a16e98d37174498 if not SCIPsparseSolGetNextSol(sparsesol, sol, sparsenvars): ^ ------------------------------------------------------------ src\pyscipopt\scip.pyx:4159:0: Expected an increase in indentation level Traceback (most recent call last): File "<string>", line 1, in <module> File "...\pip-req-build-onaxqani\setup.py", line 74, extensions = cythonize(extensions, compiler_directives = {'language_level': 3}) File "...\Cython\Build\Dependencies.py", line 1102, in cythonize cythonize_one(*args) File "...\Cython\Build\Dependencies.py", line 1225, in cythonize_one raise CompileError(None, pyx_file): src\pyscipopt\scip.pyx WARNING: Discarding git+https://github.com/scipopt/PySCIPOpt.git@multipleSol. Command errored out with exit status 1

Am I doing something wrong or are there still compilation issues?

kligler avatar May 30 '21 11:05 kligler

Hi, that branch is still a WIP and you shouldn't install from there, feel free to checkout that branch though

CGraczyk avatar Jun 04 '21 09:06 CGraczyk

Hi all, Any news on this issue? I am a developer of the CPMpy modeling library and we would really like to add SCIP as a solver. Ideally, we can also add the solveAll method for SCIP through the appropriate Python interface.

IgnaceBleukx avatar Sep 18 '23 12:09 IgnaceBleukx

Hello, @ramabile! I think the issue is that the function getSols prints the solution of the transformed problem. SCIP enters a presolving stage, where it looks for ways to make the problem simpler. In very simple instances, this means that all the variables might actually get deleted, which is happening in your case.

I've now changed the getSols function so that it prints the solution with respect to the original variables. Please let me know if you find that this actually didn't fix your problem!

@IgnaceBleukx, I think the problems related to this issue are now fixed.

Joao-Dionisio avatar Oct 06 '23 16:10 Joao-Dionisio