Detailed Description
Packing circles in a rectangle of minimal size.
This example shows how to setup quadratic constraints in SCIP when using SCIP as callable library. The example implements a model for the computation of a smallest rectangle that contains a number of given circles in the plane or the computation of the maximal number of circles that can be placed into a given rectangle.
Suppose that n circles with radii \(r_i\) are given. The task is to find coordinates \((x_i, y_i)\) for the circle midpoints and a rectangle of width \(W \geq 0\) and height \(H \geq 0\), such that
- every circle is placed within the rectangle ( \(r_i \leq x_i \leq W-r_i\), \(r_i \leq y_i \leq H-r_i\)),
- circles are not overlapping \(\left((x_i-x_j)^2 + (y_i-y_j)^2 \geq (r_i + r_j)^2\right)\), and
- the area of the rectangle is minimal.
Alternatively, one may fix the size of the rectangle and maximize the number of circles that can be fit into the rectangle without being overlapping.
Definition in file circlepacking.c.
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "scip/scip.h"
#include "scip/scipdefplugins.h"
Go to the source code of this file.
Functions | |
static void | visualizeSolutionMatplotlib (SCIP *scip, SCIP_SOL *sol) |
static void | visualizeSolutionGnuplot (SCIP *scip, SCIP_SOL *sol) |
static SCIP_RETCODE | visualizeSolutionAscii (SCIP *scip, SCIP_SOL *sol) |
static | SCIP_DECL_EVENTINIT (eventInitDispsol) |
static | SCIP_DECL_EVENTEXIT (eventExitDispsol) |
static | SCIP_DECL_EVENTEXEC (eventExecDispsol) |
static SCIP_RETCODE | includeEventHdlrDispsol (SCIP *scip) |
static SCIP_RETCODE | setupProblem (SCIP *scip, SCIP_Real fixwidth, SCIP_Real fixheight) |
static SCIP_RETCODE | runPacking (SCIP_Real fixwidth, SCIP_Real fixheight, SCIP_Bool dognuplot, SCIP_Bool domatplotlib) |
int | main (int argc, char **argv) |
Variables | |
int | ncircles = 0 |
SCIP_Real * | r = NULL |
int | rsize = 0 |
SCIP_VAR ** | x |
SCIP_VAR ** | y |
SCIP_VAR ** | b |
SCIP_VAR * | a |
SCIP_VAR * | w |
SCIP_VAR * | h |
SCIP_Bool | minarea |
Function Documentation
◆ visualizeSolutionMatplotlib()
plots solution by use of Python/Matplotlib
- Parameters
-
scip SCIP data structure sol solution to plot
Definition at line 65 of file circlepacking.c.
References minarea, ncircles, NULL, r, SCIPerrorMessage, SCIPgetSolOrigObj(), SCIPgetSolVal(), SCIPinfoMessage(), and SCIPround().
Referenced by runPacking().
◆ visualizeSolutionGnuplot()
plots solution by use of gnuplot
- Parameters
-
scip SCIP data structure sol solution to plot
Definition at line 127 of file circlepacking.c.
References MAX, minarea, ncircles, NULL, r, SCIP_Real, SCIPerrorMessage, SCIPgetSolOrigObj(), SCIPgetSolVal(), SCIPinfoMessage(), and SCIPround().
Referenced by runPacking().
◆ visualizeSolutionAscii()
|
static |
plots solution by use of ascii graphics
- Parameters
-
scip SCIP data structure sol solution to plot
Definition at line 185 of file circlepacking.c.
References cos(), minarea, ncircles, NULL, phi(), r, SCIP_CALL, SCIP_OKAY, SCIP_Real, SCIPallocBufferArray, SCIPceil(), SCIPfreeBufferArray, SCIPgetIntParam(), SCIPgetSolOrigObj(), SCIPgetSolVal(), SCIPinfoMessage(), SCIPround(), SCIPsnprintf(), and sin().
Referenced by SCIP_DECL_EVENTEXEC().
◆ SCIP_DECL_EVENTINIT()
|
static |
initialization method of event handler (called after problem was transformed)
Definition at line 277 of file circlepacking.c.
References NULL, SCIP_CALL, SCIP_EVENTTYPE_BESTSOLFOUND, SCIP_OKAY, and SCIPcatchEvent().
◆ SCIP_DECL_EVENTEXIT()
|
static |
deinitialization method of event handler (called before transformed problem is freed)
Definition at line 286 of file circlepacking.c.
References NULL, SCIP_CALL, SCIP_EVENTTYPE_BESTSOLFOUND, SCIP_OKAY, and SCIPdropEvent().
◆ SCIP_DECL_EVENTEXEC()
|
static |
execution method of event handler
Definition at line 295 of file circlepacking.c.
References NULL, SCIP_CALL, SCIP_EVENTTYPE_BESTSOLFOUND, SCIP_OKAY, SCIPeventGetSol(), SCIPeventGetType(), and visualizeSolutionAscii().
◆ includeEventHdlrDispsol()
|
static |
creates event handler for dispsol event
- Parameters
-
scip SCIP data structure
Definition at line 311 of file circlepacking.c.
References NULL, SCIP_CALL, SCIP_OKAY, SCIPincludeEventhdlrBasic(), SCIPsetEventhdlrExit(), and SCIPsetEventhdlrInit().
Referenced by runPacking().
◆ setupProblem()
|
static |
create problem in given SCIP and add all variables and constraints to model the circle packing problem
- Parameters
-
scip SCIP data structure fixwidth a given fixed width for the rectangle, or SCIP_INVALID if not fixed fixheight a given fixed height for the rectangle, or SCIP_INVALID if not fixed
Definition at line 330 of file circlepacking.c.
References MIN, minarea, ncircles, NULL, r, SCIP_Bool, SCIP_CALL, SCIP_INVALID, SCIP_MAXSTRLEN, SCIP_OBJSENSE_MAXIMIZE, SCIP_OKAY, SCIP_Real, SCIP_VARTYPE_BINARY, SCIP_VARTYPE_CONTINUOUS, SCIPaddBilinTermQuadratic(), SCIPaddCoefLinear(), SCIPaddCons(), SCIPaddLinearVarQuadratic(), SCIPaddSquareCoefQuadratic(), SCIPaddVar(), SCIPallocMemoryArray, SCIPcreateConsBasicLinear(), SCIPcreateConsBasicQuadratic(), SCIPcreateProbBasic(), SCIPcreateVarBasic(), SCIPfixVar(), SCIPinfinity(), SCIPisLT(), SCIPreleaseCons(), SCIPsetObjsense(), SCIPsnprintf(), and SQR.
Referenced by runPacking().
◆ runPacking()
|
static |
run circle packing example
Sets up SCIP and the SCIP problem, solves the problem, and shows the solution.
- Parameters
-
fixwidth a given fixed width for the rectangle, or SCIP_INVALID if not fixed fixheight a given fixed height for the rectangle, or SCIP_INVALID if not fixed dognuplot whether to draw best solution with gnuplot domatplotlib whether to draw best solution with python/matplotlib
Definition at line 540 of file circlepacking.c.
References FALSE, includeEventHdlrDispsol(), minarea, ncircles, NULL, r, SCIP_CALL, SCIP_OKAY, SCIPcreate(), SCIPfree(), SCIPfreeMemoryArray, SCIPgetBestSol(), SCIPgetNSols(), SCIPincludeDefaultPlugins(), SCIPinfoMessage(), SCIPprintOrigProblem(), SCIPprintSol(), SCIPreleaseVar(), SCIPsetRealParam(), SCIPsolve(), setupProblem(), visualizeSolutionGnuplot(), and visualizeSolutionMatplotlib().
Referenced by main().
◆ main()
int main | ( | int | argc, |
char ** | argv | ||
) |
main method starting SCIP
- Parameters
-
argc number of arguments from the shell argv array of shell arguments
Definition at line 615 of file circlepacking.c.
References BMSallocMemoryArray, BMSreallocMemoryArray, FALSE, ncircles, r, rsize, runPacking(), SCIP_ALLOC_ABORT, SCIP_Bool, SCIP_INVALID, SCIP_OKAY, SCIP_Real, SCIPprintError(), and TRUE.
Variable Documentation
◆ ncircles
int ncircles = 0 |
Number of possible circles
Definition at line 47 of file circlepacking.c.
Referenced by getNCircles(), main(), runPacking(), setupProblem(), solvePricingMINLP(), visualizeSolutionAscii(), visualizeSolutionGnuplot(), and visualizeSolutionMatplotlib().
◆ r
Radii
Definition at line 50 of file circlepacking.c.
Referenced by addFlowrowToCommodity(), addFracCounter(), addRelaxation(), allRowsInLP(), applyCompression(), atomic_posintpower< Type >::atomic_posintpower(), atomic_signpower< Type >::atomic_signpower(), atomic_userexpr< Type >::atomic_userexpr(), branchruledataEnsureArraySize(), checkCons(), cleanupNetwork(), computeAndConstraintInfos(), consdataCreate(), consdataFreeRows(), constructCompression(), createPrizeConstraints(), createRelaxation(), createSubscip(), cutsSubstituteMIR(), deleteCommodity(), detectParallelCols(), displayReaders(), dualBoundStrengthening(), extractCapacities(), extractCapacityRows(), extractFlow(), extractFlowRows(), extractNodes(), findUncapacitatedArcs(), forkAddLP(), generateAverageRay(), generateClusterCuts(), generateDisjCutSOS1(), getBaseRows(), getDualProof(), getFarkasProof(), getFlowrowFit(), getIncidentNodes(), getJobs(), getNActiveConsScore(), getNextFlowrow(), getNodeSimilarityScore(), getResourcesCapacities(), getResourcesNames(), getSimplexCoefficients(), getVarRank(), graph_mincut_exec(), heurdataEnsureArraySize(), identifySourcesTargets(), improvePoint(), invertCommodity(), lpCleanupRows(), lpDelRowset(), lpFlushAddRows(), lpLexDualSimplex(), lpRemoveObsoleteRows(), main(), markRelaxsUnsolved(), mcfnetworkFill(), polyscip::global::narrow_cast(), parseDetails(), ObjPricerVRP::pricing(), profilesFindEarliestFeasibleStart(), profilesFindLatestFeasibleStart(), propVariables(), pseudoforkAddLP(), reduce_bound(), relaxVar(), runBoundHeuristic(), runPacking(), SCIP_DECL_COMPREXIT(), SCIP_DECL_CONSGETNVARS(), SCIP_DECL_CONSGETVARS(), SCIP_DECL_HEUREXEC(), SCIP_DECL_PRESOLEXEC(), SCIP_DECL_READERREAD(), SCIPapplyLockFixings(), SCIPcolPrint(), SCIPconflictAnalyzeLP(), SCIPcreateSchedulingProblem(), SCIPdummyDebugMethodForSun(), SCIPexprintHessianSparsityDense(), SCIPfreeRepresentation(), SCIPgetRandomSubset(), SCIPgetReadingTime(), SCIPinitRepresentation(), SCIPlinkcuttreeEvert(), SCIPlpEndDive(), SCIPlpGetDualfarkas(), SCIPlpGetSol(), SCIPlpGetUnboundedSol(), SCIPlpRemoveRedundantRows(), SCIPlpShrinkRows(), SCIPlpStartDive(), SCIPlpSumRows(), SCIPlpUpdateAges(), SCIPmatrixGetParallelCols(), SCIPrandomGetSubset(), SCIPreoptApplyCompression(), SCIPreoptGetNSols(), SCIPreoptMergeVarHistory(), SCIPresetRepresentation(), SCIPsolAdjustImplicitSolVals(), SCIPsolveProbingRelax(), SCIPStpHeurTMCompStarts(), SCIPStpHeurTMRun(), separateCons(), separateConsBinaryRepresentation(), separateCoverCutsCons(), separateCuts(), setupProblem(), solveNodeRelax(), solveRowEcholonGF2(), storeCuts(), strenghtenOrbitopeConstraint(), subrootConstructLP(), univariate_for_sparse_jac(), univariate_rev_sparse_jac(), updateActivities(), updateLoopStatus(), utdist(), varProcessBoundChanges(), visualizeSolutionAscii(), visualizeSolutionGnuplot(), visualizeSolutionMatplotlib(), writeOpbRelevantAnds(), xmlFindNode(), and xmlFindNodeMaxdepth().
◆ rsize
int rsize = 0 |
Definition at line 51 of file circlepacking.c.
Referenced by main().
◆ x
SCIP_VAR** x |
x coordinates
Definition at line 54 of file circlepacking.c.
Referenced by addConcaveEstimatorBivariate(), addLinearization(), atomic_userexpr< Type >::atomic_userexpr(), checkSystemGF2(), cleanCycle(), compEdgesObst(), computeBoundsZ(), computeCut(), computeGradient(), computeViolation(), createConsFromQuadTerm(), createExprtreeFromMonomial(), drawScaledImage(), ecaggrAddBilinTerm(), ecaggrAddQuadvar(), F77_FUNC(), generate1ConvexIndefiniteUnderestimator(), generate1ConvexIndefiniteUnderestimatorAtBoundary(), generate1ConvexIndefiniteUnderestimatorInTheInteriorPatternA(), generate1ConvexIndefiniteUnderestimatorInTheInteriorPatternB(), generateConvexConcaveUnderestimator(), generateCut(), generateCutNonConvex(), generateCutPoint(), generateEstimatingHyperplane(), generateLinearizationCut(), generateOrthogonal_lx_ly_Underestimator(), generateOrthogonal_lx_uy_Underestimator(), generateSparseCut(), generateUnderestimatorParallelYFacets(), getX(), graph_mincut_exec(), hessLagSparsitySetNzFlagForExprtree(), isCandidate(), isConvexLocal(), nlrowaggrAddRemBilinTerm(), nlrowaggrCreate(), presolveDisaggregate(), presolveRemoveFixedVariables(), presolveSolve(), printRow(), proposeBranchingPoint(), provedBound(), rbRotate(), read_problem(), registerBranchingCandidatesCentrality(), registerBranchingCandidatesGap(), registerBranchingCandidatesViolation(), roundFixingValue(), runBrachistochrone(), SCIP_DECL_CONSCOPY(), SCIP_DECL_CONSINITLP(), SCIP_DECL_CONSPARSE(), SCIP_DECL_EVENTEXEC(), SCIP_DECL_EXPRGRAPHNODEREFORM(), SCIP_DECL_NONLINCONSUPGD(), SCIP_DECL_QUADCONSUPGD(), SCIP_DECL_READERREAD(), SCIP_NlpiProblem::SCIP_NlpiProblem(), SCIPcalcGreComDiv(), SCIPcomputeConvexEnvelopeFacet(), SCIPcreateConsAbspower(), SCIPerf(), SCIPintervalGetRoundingMode(), SCIPintervalQuadBivar(), SCIPintervalQuadUpperBound(), SCIPintervalSquare(), SCIPnegateReal(), SCIPpatternAddElement(), SCIPpatternSetElementPos(), SCIPrbtreeDelete_call(), SCIPsetModifiedDefaultSettingsIpopt(), SCIPwritePip(), setupProblem(), solvePricingMINLP(), solveRowEcholonGF2(), storeAllBilinearTerms(), and updateBestCandidate().
◆ y
SCIP_VAR** y |
y coordinates
Definition at line 55 of file circlepacking.c.
Referenced by addConcaveEstimatorBivariate(), azmul(), compEdgesObst(), computeCut(), computeViolation(), createConsFromQuadTerm(), createExprtreeFromMonomial(), drawScaledImage(), ecaggrAddBilinTerm(), generate1ConvexIndefiniteUnderestimator(), generate1ConvexIndefiniteUnderestimatorAtBoundary(), generate1ConvexIndefiniteUnderestimatorInTheInteriorPatternA(), generate1ConvexIndefiniteUnderestimatorInTheInteriorPatternB(), generateConvexConcaveUnderestimator(), generateCut(), generateCutNonConvex(), generateEstimatingHyperplane(), generateLinearizationCut(), generateOrthogonal_lx_ly_Underestimator(), generateOrthogonal_lx_uy_Underestimator(), generateUnderestimatorParallelYFacets(), isCandidate(), isConvexLocal(), nlrowaggrAddRemBilinTerm(), nlrowaggrCreate(), presolveDisaggregate(), presolveSolve(), presolveTryAddLinearReform(), printRow(), propagateVarbounds(), provedBound(), rbInsertFixup(), rbRotate(), read_problem(), registerBranchingCandidatesGap(), runBrachistochrone(), SCIP_DECL_EVENTEXEC(), SCIP_DECL_EXPRGRAPHNODEREFORM(), SCIP_DECL_QUADCONSUPGD(), SCIP_DECL_READERREAD(), SCIPcalcGreComDiv(), SCIPerf(), SCIPintervalQuadBivar(), SCIPintervalSquare(), SCIPpatternAddElement(), SCIPpatternSetElementPos(), SCIPrbtreeDelete_call(), SCIPrbtreePredecessor_call(), SCIPrbtreeSuccessor_call(), SCIPregressionAddObservation(), SCIPregressionRemoveObservation(), SCIPsolveLinearProb(), SCIPsolveLinearProb3(), setupProblem(), solvePricingMINLP(), and updateBestCandidate().
◆ b
SCIP_VAR** b |
whether circle is placed into rectangle
Definition at line 56 of file circlepacking.c.
Referenced by aggregateConstraints(), alnsFixMoreVariables(), polyscip::Polyscip::boundedCEnd(), cancelRow(), checkBlocking(), checkCons(), checkSystemGF2(), collectBinaryVars(), computePosCircleCircle(), computePosRingCircle(), computeRowEcholonGF2(), consdataCreateBinvars(), consdataLinearize(), consdataPrint(), createCapacityRestriction(), createCoverCutsTimepoint(), createRows(), exprgraphNodePropagateBounds(), generateCutLTIfindIntersection(), getlecloseterms(), isNeighbor(), lockRounding(), nlrowCalcActivityBounds(), polyscip::Polyscip::numberofUnboundedResults(), optimize(), presolveDual(), presolveSolve(), processIntegerBoundChg(), projectVbd(), propagateBounds(), propagateVarbounds(), provedBound(), removeFixedBinvars(), SCIP_DECL_CONSCHECK(), SCIP_DECL_CONSLOCK(), SCIP_DECL_CONSRESPROP(), SCIP_DECL_HEUREXEC(), SCIP_DECL_QUADCONSUPGD(), SCIPintervalCos(), SCIPintervalQuadUpperBound(), SCIPintervalSin(), SCIPintervalSolveBivariateQuadExpressionAllScalar(), SCIPintervalSolveUnivariateQuadExpressionPositiveAllScalar(), SCIPrealToRational(), SCIPsetSortProps(), SCIPvisualizeConsCumulative(), setupProblem(), solveRowEcholonGF2(), SORTTPL_NAME(), strengthenVarbounds(), tightenedIntvar(), tightenWeights(), tryAggregateIntVars(), and xmlFreeAttr().
◆ a
SCIP_VAR* a |
area of rectangle
Definition at line 57 of file circlepacking.c.
Referenced by aggregateConstraints(), applyOptcumulative(), polyscip::Polyscip::boundedCEnd(), cancelRow(), checkBlocking(), checkFactorable(), cleanCycle(), cleanupNetwork(), computeAndConstraintInfos(), computeObjWeightSize(), computePosCircleCircle(), computePosRingCircle(), createEdgesFromRow(), createInitialColumns(), createLP(), createVariables(), dualascent_init(), dualCostIsValid(), exprgraphNodePropagateBounds(), F77_FUNC(), generateClusterCuts(), generateCutLTIfindIntersection(), getlecloseterms(), graph_mincut_exec(), graph_sol_reroot(), graph_trail(), graph_trail_arr(), identifyComponent(), identifySourcesTargets(), isNeighbor(), mcfnetworkExtract(), mcfnetworkFill(), mcfnetworkFree(), nlrowCalcActivityBounds(), nodepairqueueCreate(), nodepartitionIsConnected(), polyscip::Polyscip::numberofUnboundedResults(), performAggregations(), presolveDual(), presolveSolve(), printNLRow(), propagateBounds(), propagateBoundsQuadVar(), propagateVarbounds(), provedBound(), reduce_daSlackPrune(), reinitialise(), SCIP_DECL_HEURFREE(), SCIP_DECL_QUADCONSUPGD(), SCIPintervalCos(), SCIPintervalSin(), SCIPrealToRational(), SCIPStpDualAscent(), SCIPStpDualAscentPcMw(), SCIPStpHeurAscendPruneRun(), SCIPStpHeurTMBuildTreePcMw(), SCIPStpHeurTMPrunePc(), SCIPwriteCcg(), selectBranchingVertexByLp(), selectBranchingVertexByLp2Flow(), setupProblem(), SORTTPL_NAME(), tryAggregateIntVars(), writeOpbObjective(), writeOpbRelevantAnds(), xmlAddAttr(), xmlFreeAttr(), xmlGetAttrval(), xmlNewAttr(), and xmlShowNode().
◆ w
SCIP_VAR* w |
width of rectangle
Definition at line 58 of file circlepacking.c.
Referenced by branchBalancedCardinality(), cliqueCleanup(), collectBinaryCliqueData(), collectNonBinaryImplicationData(), collectNonBinaryVBoundData(), polyscip::doubledescription::DoubleDescriptionMethod::computeVRep_Var1(), correctConshdlrdata(), createEdgesFromRow(), deleteRedundantVars(), detectRedundantVars(), dualWeightsTightening(), enforceConssSOS1(), enforceSOS2(), extensionOperatorSOS1(), ProbDataObjectives::getWeightedObjVal(), graph_next_term(), polyscip::doubledescription::V_RepT::hasNonUnitAndNonZeroWeight(), polyscip::doubledescription::DoubleDescriptionMethod::moveVRep(), polyscip::Polyscip::numberofUnboundedResults(), rbDeleteFixup(), removeConstraintsDueToNegCliques(), SCIP_DECL_BANDITRESET(), SCIP_DECL_HEUREXEC(), SCIPapplyLockFixings(), SCIPcliquetableAdd(), SCIPlinkcuttreeLink(), SCIPshrinkDisjunctiveVarSet(), SCIPwriteCcg(), sep_2cut(), separateSolution(), sequentialUpAndDownLifting(), sequentialUpAndDownLiftingGUB(), polyscip::WeightSpaceVertex::setStatus(), solvePricingMINLP(), sortAndMergeClique(), tarjan(), tightenVarsBoundsSOS1(), tightenWeights(), updateConsanddataUses(), updateImplicationGraphSOS1(), and writeOpbObjective().
◆ h
SCIP_VAR* h |
height of rectangle
Definition at line 59 of file circlepacking.c.
Referenced by calcNonZeros(), checkFeasSubtree(), checkParameters(), checkSolOrig(), computePosCircleCircle(), computePosRingCircle(), computeRowEcholonGF2(), polyscip::WeightSpaceVertex::computeSlack(), polyscip::doubledescription::DoubleDescriptionMethod::computeVRep_Var1(), conflictAddConflictCons(), createMipCpFormulation(), createMipFormulation(), createPresoldata(), enforceConstraints(), enforceViolatedFixedNonlinear(), graph_edge_printInfo(), hessLagAddExprtree(), multihashlistRetrieve(), multihashlistRetrieveNext(), quadelemsQuickSort(), replaceByLinearConstraints(), replaceViolatedByLinearConstraints(), SCIPdialoghdlrAddHistory(), SCIPinitConssLP(), SCIPparamsetSetEmphasis(), SCIPpresolve(), SCIPprimalHeuristics(), SCIPprobFree(), SCIPprobTransform(), SCIPreadProb(), SCIPsolCheck(), SCIPtransformProb(), polyscip::WeightSpaceVertex::setStatus(), SORTTPL_NAME(), superadditiveUpLifting(), and polyscip::WeightSpacePolyhedron::WeightSpacePolyhedron().
◆ minarea
SCIP_Bool minarea |
whether we minimize the area (TRUE) or maximize the number of circles in the rectangle (FALSE)
Definition at line 60 of file circlepacking.c.
Referenced by runPacking(), setupProblem(), visualizeSolutionAscii(), visualizeSolutionGnuplot(), and visualizeSolutionMatplotlib().