Repository navigation
SCIP 11: Split Model into BaseModel and Model - #1292
Joao-Dionisio wants to merge 19 commits into
Conversation
…mn/Row/NLRow to model.pxi
… addMatrixVar to Model and lint for dtype=float and Model returns
|
This should be the first to be merged for two reasons. One, it would make the diff in exact-solving much nicer. And two, we already have enough content for a minor release, and if we manage to merge this in there in time, then we can make use of eventual bug reports that come up. |
| SCIP_PXI = Path(__file__).resolve().parents[1] / "src" / "pyscipopt" / "scip.pxi" | ||
| MODEL_PXI = SCIP_PXI.with_name("model.pxi") | ||
|
|
||
| BASE_MODEL_METHODS = frozenset({ |
There was a problem hiding this comment.
This is to make it harder to accidentally add methods that would break ExactModel.
There was a problem hiding this comment.
I doubt that it should be made even harder to add methods by another duplication.
There was a problem hiding this comment.
I was thinking of contributions to BaseModel that should actually go in model. Do you think it should rest entirely on the reviewer, even years from now?
There was a problem hiding this comment.
Are there not already declarations for base methods? The agent will just remove the annoying test. So yes.
There was a problem hiding this comment.
We should still have a way of ensuring that a method added to BaseModel will not break ExactModel. Maybe this can just be fixed with a point on a PR checklist.
There was a problem hiding this comment.
Yes, this should be done.
| @@ -0,0 +1,82 @@ | |||
| ##@file exact.pxi | |||
There was a problem hiding this comment.
Creating this exact.pxi skeleton to make the diff nicer to review.
| raise ValueError("SCIP was built without exact solving support, ExactModel needs a SCIP build with exact solving enabled") | ||
| super().__init__(problemName, defaultPlugins, createscip=createscip, threadsafe=threadsafe) | ||
|
|
||
| cdef _configureScip(self): |
There was a problem hiding this comment.
Has to be like this because SCIPenableExactSolving() must be called in the INIT stage, and the way we're creating the model above puts us directly in the problem stage.
Co-authored-by: João Dionísio <57299939+Joao-Dionisio@users.noreply.github.com>
|
I'm moving the |
|
@DominikKamp @mmghannam I'm in the middle of reviewing this. Would you mind just letting me know if you have any high-level observations? |
| assert m.getNVars() == 0 | ||
| assert type(Model().addVar()) is Variable | ||
| path = tmp_path / "exact.lp" | ||
| path.write_text("Minimize\n obj: x + 3 y\nSubject To\n c1: 2 x + 3 y >= 1\nBounds\n 0 <= x <= 10\n 0 <= y <= 10\nGeneral\n x\nEnd\n") |
There was a problem hiding this comment.
Because we don't have fractions yet.
There was a problem hiding this comment.
Python fractions. For us to implement the tests with them, we'd need to implement exact variables and exact constraints now, which is out of scope.
There was a problem hiding this comment.
I meant this line does not contain fractions, why need a general type for it? Maybe the issue is just that fractions were even mentioned here. But it should still be possible to create linear constraints in the usual way.
There was a problem hiding this comment.
We would have to change how expressions are handled. There is more than one path in PySCIPOpt where floats are introduced, like when variables are created, the corresponding expression is {Term(var): 1.0}. Even if we were to change this, it's preferable to do this elsewhere.
There was a problem hiding this comment.
Floats are not bad in general if this is what the user wants to handle exactly. So I do not see an issue with this expression. The ExactVariable is a subclass of the BaseVariable which Term supports, if there are coefficient computations, this is on the user, but not sure on how much the linear expression handling relies on coefficients being float.
There was a problem hiding this comment.
Floats are not bad in general, but it would be very unexpected if someone were to write model.setObjective(x + 0.1*y) in an exact model and see that the coefficients were 1 and 3602879701896397/36028797018963968. While #1264 is not merged, having SCIP read the problem directly is what I'm most comfortable with. I understand this is a test, but people (and agents) also read them to understand what should be done.
|
|
||
| # export user-relevant objects: | ||
| from pyscipopt.Multidict import multidict as multidict | ||
| from pyscipopt.scip import BaseModel as BaseModel |
There was a problem hiding this comment.
I gave it more than one pass already, but can you check whether I missed something @DominikKamp ? Either a method in BaseModel that relies on floating-point, or a method in Model that can also be used by exact SCIP. There are many methods that could, in theory, be used in exact SCIP, but haven't been implemented in SCIP yet, like methods related to AND constraints, for example. For these, I just put them in Model, rather than BaseModel and raising NotImplementedError in ExactModel.
Methods in BaseModel:
addConss, addMatrixCons, addNNodes, addSol, addVarLocks, addVarLocksType, allColsInLP, attachEventHandlerCallback, backtrackProbing, branchVar, catchEvent, catchVarEvent, checkSol, chgVarBranchPriority, chgVarType, constructLP, create, createChild, createProbBasic, cutoffNode, deactivatePricer, delCons, delConsLocal, delVar, disableDebugSol, disablePropagation, dropEvent, dropVarEvent, enableDebugSol, endProbing, free, freeProb, freeSol, freeTransform, from_ptr, getBestChild, getBestLeaf, getBestNode, getBestSibling, getBestSol, getBestboundNode, getBranchScoreMultiple, getChildren, getConsNVars, getConsVars, getConss, getCurrentNode, getDepth, getDeterministicTime, getFocusDepth, getFocusNode, getHeurTiming, getLeaves, getMajorVersion, getMaxDepth, getMaxTotalDepth, getMemExternEstim, getMemTotal, getMemUsed, getMinorVersion, getNBacktracks, getNBestSolsFound, getNBinVars, getNChildren, getNConss, getNContVars, getNCuts, getNCutsApplied, getNFeasibleLeaves, getNImplVars, getNInfeasibleLeaves, getNIntVars, getNLPBranchCands, getNLPCols, getNLPIterations, getNLPRows, getNLPs, getNLeaves, getNLimSolsFound, getNNodeLPIterations, getNNodes, getNNodesLeft, getNObjlimLeaves, getNReaders, getNRuns, getNSepaRounds, getNSiblings, getNSols, getNSolsFound, getNStrongbranchLPIterations, getNTotalNodes, getNVars, getObjVal, getObjectiveSense, getOpenNodes, getParam, getParams, getPlungeDepth, getPresolvingTime, getPrimalDualIntegral, getPrioChild, getPrioSibling, getProbName, getProbingDepth, getPseudoBranchCands, getReadingTime, getRootNode, getSiblings, getSolTime, getSols, getSolvingTime, getStage, getStageName, getStatus, getTechVersion, getTotalTime, getTransformedCons, getTransformedVar, getTreesizeEstimation, getVarPseudocost, getVarPseudocostScore, getVarStrongbranchNode, getVars, hideOutput, inProbing, inRepropagation, includeBranchrule, includeConshdlr, includeCutsel, includeDefaultPlugins, includeEventhdlr, includeHeur, includeNodesel, includePresol, includePricer, includeProp, includeReader, includeRelax, includeSepa, interruptSolve, isObjChangedProbing, isObjIntegral, markDoNotAggrVar, markDoNotMultaggrVar, newProbingNode, nodesGetCommonAncestor, nodesSharePath, optimize, optimizeNogil, presolve, printCons, printExternalCodeVersions, printProblem, printStatistics, printStatisticsJson, printVersion, readParams, readProblem, redirectOutput, relax, repropagateNode, resetParam, resetParams, restartSolve, separateSol, setBoolParam, setCharParam, setCheck, setEmphasis, setEnforced, setHeurTiming, setHeuristics, setInitial, setIntParam, setLogfile, setLongintParam, setMaximize, setMinimize, setModifiable, setObjIntegral, setParam, setParams, setPresolve, setProbName, setRealParam, setRemovable, setSeparating, setStringParam, startProbing, to_ptr, updateVarPseudocost, version, wasNodeLastBranchParent, writeName, writeParams, writeProblem, writeStatistics, writeStatisticsJson
Methods in Model:
activateBenders, addBendersSubproblem, addCoefKnapsack, addCoefLinear, addCoefLogicor, addCons, addConsAnd, addConsCardinality, addConsCoeff, addConsCumulative, addConsDisjunction, addConsElemDisjunction, addConsIndicator, addConsKnapsack, addConsLinear, addConsLocal, addConsLogicor, addConsNode, addConsOr, addConsSOS1, addConsSOS2, addConsXor, addCut, addExprNonlinear, addMatrixConsDisjunction, addMatrixConsIndicator, addMatrixVar, addObjoffset, addPoolCut, addPyCons, addRowDive, addRowExact, addVar, addVarSOS1, addVarSOS2, addVarToRow, adjustedVarLb, adjustedVarUb, aggregateVars, allowNegSlackExact, appendVarSOS1, appendVarSOS2, applyCutsProbing, branchLPExact, branchVarVal, cacheRowExtensions, calcChildEstimate, calcNodeselPriority, catchRowEvent, checkBendersSubproblemOptimality, checkQuadraticNonlinear, chgCapacityKnapsack, chgCoefLinear, chgLhs, chgReoptObjective, chgRhs, chgRowLhsDive, chgRowRhsDive, chgVarLb, chgVarLbDive, chgVarLbGlobal, chgVarLbNode, chgVarLbProbing, chgVarObjDive, chgVarObjProbing, chgVarUb, chgVarUbDive, chgVarUbGlobal, chgVarUbNode, chgVarUbProbing, computeBestSolSubproblems, copyLargeNeighborhoodSearch, count, createCons, createConsFromExpr, createEmptyRowSepa, createEmptyRowUnspec, createOrigSol, createPartialSol, createSol, delCoefLinear, dropRowEvent, enableExactSolving, enableReoptimization, endDive, endStrongbranch, epsilon, feasCeil, feasFloor, feasFrac, feasRound, feastol, fixVar, fixVarProbing, flushRowExtensions, frac, freeBendersSubproblems, freeReoptSolve, generateIIS, getActivity, getAvgDualbound, getAvgLowerbound, getBendersAuxiliaryVar, getBendersSubproblem, getBendersVar, getBipartiteGraphRepresentation, getCapacityKnapsack, getColRedCost, getCondition, getConsVals, getCountedSols, getCutEfficacy, getCutLPSolCutoffDistance, getCutoffbound, getDualMultiplier, getDualSolVal, getDualbound, getDualboundRoot, getDualfarkasKnapsack, getDualfarkasLinear, getDualfarkasLogicor, getDualsolKnapsack, getDualsolLinear, getDualsolLogicor, getFirstPrimalBound, getGap, getIIS, getLPBInvARow, getLPBInvRow, getLPBasisInd, getLPBranchCands, getLPColsData, getLPObjVal, getLPRowsData, getLPSolstat, getLhs, getLinearConsIndicator, getLocalEstimate, getLowerbound, getLowerboundRoot, getNCountedSols, getNNlRows, getNReoptRuns, getNVarsAnd, getNVarsLogicor, getNlRowActivityBounds, getNlRowSolActivity, getNlRowSolFeasibility, getNlRows, getObjective, getObjlimit, getObjoffset, getPrimalRay, getPrimalRayVal, getPrimalbound, getResultantAnd, getRhs, getRowActivity, getRowDualSol, getRowLPActivity, getRowLinear, getRowNumIntCols, getRowObjParallelism, getRowParallelism, getSlack, getSlackVarIndicator, getSolObjVal, getSolVal, getTermsQuadratic, getUpperbound, getVal, getValsLinear, getVarDict, getVarLbDive, getVarRedcost, getVarStrongbranch, getVarStrongbranchLast, getVarUbDive, getVarsAnd, getVarsLogicor, getWeightsKnapsack, hasPrimalRay, includeBenders, includeBendersDefaultCuts, includeBenderscut, includeIISfinder, infinity, initBendersDefault, isAndConsSorted, isCutEfficacious, isEQ, isExact, isFeasEQ, isFeasGE, isFeasGT, isFeasIntegral, isFeasLE, isFeasLT, isFeasNegative, isFeasPositive, isFeasZero, isGE, isGT, isHugeValue, isInfinity, isIntegral, isLE, isLPSolBasic, isLT, isNLPConstructed, isNegative, isPositive, isReoptEnabled, isZero, lpiGetIterations, printBestSol, printNlRow, printRow, printSol, propagateProbing, readSol, readSolFile, releaseRow, setBendersSubproblemIsConvex, setObjective, setObjlimit, setParamsCountsols, setRelaxSolVal, setSolVal, setupBendersSubproblem, solveBendersSubproblem, solveConcurrent, solveDiveLP, solveProbingLP, solveProbingLPWithPricing, sortAndCons, startDive, startStrongbranch, tightenVarLb, tightenVarLbGlobal, tightenVarUb, tightenVarUbGlobal, translateSubSol, trySol, updateBendersLowerbounds, updateNodeLowerbound, writeBestSol, writeBestTransSol, writeLP, writeMIP, writeSol, writeTransSol
There was a problem hiding this comment.
I'm also going to move to Model the methods that do not yet have a getter in the exact model (like isLPSolBasic, for which there is no public getter).
There was a problem hiding this comment.
Oh, do we also get a BaseVariable or is this distinction not required?
There was a problem hiding this comment.
I haven't implemented this distinction, since the number of methods would not justify it. BaseVariable would have 25 methods, ExactVariable 10, Variable 1.
There was a problem hiding this comment.
Would it not still be simpler than duplicating 25 methods?
There was a problem hiding this comment.
There is no duplication. ExactVariable is a subclass of Variable and overwrites the methods that are not exact-compatible (raising an error with the one method that would be going into the non-exact Variable class, which is getAvgSol()).
There was a problem hiding this comment.
It is the same issue as with subclassing Model. We do not need an ExactVariable class for this then.
|
@Opt-Mucca if you don't mind, can you also give your high-level opinion on this, please? 🙏 |
|
@Joao-Dionisio The changes look good! The naming scheme makes sense. I've got no high-level critiques (or even low-level ones). It also starts the move away from one mega file. Is there a plan to package exact solving capabilities into the standard PyPI distribution? |
|
Thank you @Opt-Mucca !
Yep, it's a WIP in scipoptsuite-deploy#46, along with SCIP in debug mode. Still not finished though! |
# Conflicts: # src/pyscipopt/scip.pxi # src/pyscipopt/scip.pyi
… members, add changelog entries
|
@DominikKamp is there something else you're concerned with or do you think that this can be merged? |
We want to introduce proper exact solving capabilities with the next major release. For this, we decided it would be best to have 3 distinct classes. A
BaseModelclass that holds the methods that are common to both types of solving. AModelclass that has the non-exact methods. AnExactModelclass that has the exact methods. This way, we have the best of both worlds, as most users will not experience any difference at all, they will justfrom pyscipopt import Model, and users of exact-SCIP can instead dofrom pyscipopt import ExactModel, and work with an exact model as they would a non-exact model. We decided to do things like this because we are able to not leak dangerous methods into the exact model.This PR does the most difficult part of the work, the splitting of
ModelandBaseModel. I'm creatingmodel.pxi,basemodel.pxi,exactmodel.pxi.scip.pxiwould just have SCIP's base classes, but this can still be changed, of course.