Skip to content

SCIP 11: Split Model into BaseModel and Model - #1292

Open
Joao-Dionisio wants to merge 19 commits into
masterfrom
base-model
Open

Joao-Dionisio wants to merge 19 commits into
masterfrom
base-model

Conversation

@Joao-Dionisio

@Joao-Dionisio Joao-Dionisio commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

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 BaseModel class that holds the methods that are common to both types of solving. A Model class that has the non-exact methods. An ExactModel class 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 just from pyscipopt import Model, and users of exact-SCIP can instead do from 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 Model and BaseModel. I'm creating model.pxi, basemodel.pxi, exactmodel.pxi. scip.pxi would just have SCIP's base classes, but this can still be changed, of course.

@Joao-Dionisio Joao-Dionisio mentioned this pull request Nov 12, 2026
5 tasks
@Joao-Dionisio

Copy link
Copy Markdown
Member Author

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.

@Joao-Dionisio Joao-Dionisio changed the title Split Model into BaseModel and Model SCIP 11: Split Model into BaseModel and Model Nov 12, 2026
Comment thread tests/test_base_model.py Outdated
SCIP_PXI = Path(__file__).resolve().parents[1] / "src" / "pyscipopt" / "scip.pxi"
MODEL_PXI = SCIP_PXI.with_name("model.pxi")

BASE_MODEL_METHODS = frozenset({

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is to make it harder to accidentally add methods that would break ExactModel.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I doubt that it should be made even harder to add methods by another duplication.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there not already declarations for base methods? The agent will just remove the annoying test. So yes.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this should be done.

Comment thread src/pyscipopt/model.pxi Resolved
Comment thread src/pyscipopt/model.pxi Resolved
Comment thread src/pyscipopt/model.pxi Resolved
Comment thread src/pyscipopt/model.pxi Resolved
Comment thread src/pyscipopt/exact.pxi Resolved
Comment thread src/pyscipopt/exact.pxi
@@ -0,0 +1,82 @@
##@file exact.pxi

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creating this exact.pxi skeleton to make the diff nicer to review.

Comment thread src/pyscipopt/exact.pxi
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):

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
@Joao-Dionisio

Copy link
Copy Markdown
Member Author

I'm moving the BaseModel into its own file as well. Both to make the diff cleaner, but also to organize the codebase a bit. I'm not a huge fan of the C-style 20k LOC files.

@Joao-Dionisio

Copy link
Copy Markdown
Member Author

@DominikKamp @mmghannam I'm in the middle of reviewing this. Would you mind just letting me know if you have any high-level observations?

@Joao-Dionisio Joao-Dionisio added this to the v6.4.0 milestone Nov 15, 2026
Comment thread tests/test_base_model.py
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")

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because we don't have fractions yet.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which fractions?

@Joao-Dionisio Joao-Dionisio Nov 15, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@Joao-Dionisio Joao-Dionisio Nov 15, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@Joao-Dionisio Joao-Dionisio Nov 15, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/pyscipopt/__init__.py

# export user-relevant objects:
from pyscipopt.Multidict import multidict as multidict
from pyscipopt.scip import BaseModel as BaseModel

@Joao-Dionisio Joao-Dionisio Nov 15, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

@DominikKamp DominikKamp Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, do we also get a BaseVariable or is this distinction not required?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't implemented this distinction, since the number of methods would not justify it. BaseVariable would have 25 methods, ExactVariable 10, Variable 1.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it not still be simpler than duplicating 25 methods?

@Joao-Dionisio Joao-Dionisio Nov 15, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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()).

@DominikKamp DominikKamp Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is the same issue as with subclassing Model. We do not need an ExactVariable class for this then.

@Joao-Dionisio

Joao-Dionisio commented Nov 15, 2026 •

Copy link
Copy Markdown
Member Author

@Opt-Mucca if you don't mind, can you also give your high-level opinion on this, please? 🙏

@Opt-Mucca

Copy link
Copy Markdown
Collaborator

@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?

@Joao-Dionisio

Copy link
Copy Markdown
Member Author

Thank you @Opt-Mucca !

Is there a plan to package exact solving capabilities into the standard PyPI distribution?

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
@Joao-Dionisio

Copy link
Copy Markdown
Member Author

@DominikKamp is there something else you're concerned with or do you think that this can be merged?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants