Scippy

SCIP

Solving Constraint Integer Programs

circle.c
Go to the documentation of this file.
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2 /* */
3 /* This file is part of the program and library */
4 /* SCIP --- Solving Constraint Integer Programs */
5 /* */
6 /* Copyright (C) 2002-2017 Konrad-Zuse-Zentrum */
7 /* fuer Informationstechnik Berlin */
8 /* */
9 /* SCIP is distributed under the terms of the ZIB Academic License. */
10 /* */
11 /* You should have received a copy of the ZIB Academic License */
12 /* along with SCIP; see the file COPYING. If not email to scip@zib.de. */
13 /* */
14 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15 
16 /**@file circle.c
17  * @brief Solving the Circle Enclosing Points Problem
18  * @author Stefan Vigerske
19  *
20  * This example shows how to setup second-order-cone constraints in SCIP when using SCIP as callable library.
21  * The example implements a model for the computation of a smallest circle that contains a number of given points
22  * in the plane.
23  *
24  * The model is taken from the GAMS model library:
25  * http://www.gams.com/modlib/libhtml/circle.htm
26  *
27  * See also: http://en.wikipedia.org/wiki/Smallest_circle_problem
28  *
29  * Given n points in the plane with coordinates \f$(x_i, y_i)\f$, the task is to find a coordinates \f$(a,b)\f$
30  * and a minimal radius \f$r \geq 0\f$, such that \f$\sqrt{(x_i-a)^2 + (y_i-b)^2} \leq r\f$.
31  * The latter are second-order-cone constraints.
32  */
33 
34 /*--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
35 
36 #include <stdio.h>
37 
38 #include "scip/pub_misc.h"
39 #include "scip/scip.h"
40 #include "scip/scipdefplugins.h"
41 
42 /** number of points to enclose by a circle */
43 static const int npoints = 10;
44 
45 /** seed for random number generator */
46 static const unsigned int randseed = 42;
47 
48 /** sets up problem */
49 static
51  SCIP* scip, /**< SCIP data structure */
52  SCIP_RANDNUMGEN* randnumgen /**< random number generator */
53  )
54 {
55  SCIP_VAR* a;
56  SCIP_VAR* b;
57  SCIP_VAR* r;
58 
59  char name[SCIP_MAXSTRLEN];
60  int i;
61 
62  /* create empty problem */
63  SCIP_CALL( SCIPcreateProbBasic(scip, "circle") );
64 
65  /* create variables and add to problem */
66  SCIP_CALL( SCIPcreateVarBasic(scip, &a, "a", -SCIPinfinity(scip), SCIPinfinity(scip), 0.0, SCIP_VARTYPE_CONTINUOUS) );
67  SCIP_CALL( SCIPcreateVarBasic(scip, &b, "b", -SCIPinfinity(scip), SCIPinfinity(scip), 0.0, SCIP_VARTYPE_CONTINUOUS) );
68  SCIP_CALL( SCIPcreateVarBasic(scip, &r, "r", 0.0, SCIPinfinity(scip), 1.0, SCIP_VARTYPE_CONTINUOUS) );
69 
70  SCIP_CALL( SCIPaddVar(scip, a) );
71  SCIP_CALL( SCIPaddVar(scip, b) );
72  SCIP_CALL( SCIPaddVar(scip, r) );
73 
74  /* create soc constraints, add to problem, and forget about them */
75  for( i = 0; i < npoints; ++i )
76  {
77  SCIP_CONS* cons;
78  SCIP_VAR* ab[2];
79  SCIP_Real xy[2];
80 
81  ab[0] = a;
82  ab[1] = b;
83  xy[0] = -SCIPrandomGetReal(randnumgen, 1.0, 10.0);
84  xy[1] = -SCIPrandomGetReal(randnumgen, 1.0, 10.0);
85 
86  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "point%d", i);
87  SCIP_CALL( SCIPcreateConsBasicSOC(scip, &cons, name, 2, ab, NULL, xy, 0.0, r, 1.0, 0.0) );
88  SCIP_CALL( SCIPaddCons(scip, cons) );
89  SCIP_CALL( SCIPreleaseCons(scip, &cons) );
90  }
91 
92  /* release variables */
93  SCIP_CALL( SCIPreleaseVar(scip, &a) );
94  SCIP_CALL( SCIPreleaseVar(scip, &b) );
95  SCIP_CALL( SCIPreleaseVar(scip, &r) );
96 
97  return SCIP_OKAY;
98 }
99 
100 /* runs circle enclosing example */
101 static
103 {
104  SCIP* scip;
105  SCIP_RANDNUMGEN* randnumgen;
106 
107  SCIP_CALL( SCIPcreate(&scip) );
109 
110  SCIPinfoMessage(scip, NULL, "\n");
111  SCIPinfoMessage(scip, NULL, "*********************************************\n");
112  SCIPinfoMessage(scip, NULL, "* Running Smallest Enclosing Circle Problem *\n");
113  SCIPinfoMessage(scip, NULL, "*********************************************\n");
114  SCIPinfoMessage(scip, NULL, "\n");
115 
116  /* create random number generator */
117  SCIP_CALL( SCIPcreateRandom(scip, &randnumgen, randseed) );
118 
119  SCIP_CALL( setupProblem(scip, randnumgen) );
120 
121  SCIPinfoMessage(scip, NULL, "Original problem:\n");
122  SCIP_CALL( SCIPprintOrigProblem(scip, NULL, "cip", FALSE) );
123 
124  SCIPinfoMessage(scip, NULL, "\nSolving...\n");
125  SCIP_CALL( SCIPsolve(scip) );
126 
127  SCIP_CALL( SCIPfreeTransform(scip) );
128 
129  if( SCIPgetNSols(scip) > 0 )
130  {
131  SCIPinfoMessage(scip, NULL, "\nSolution:\n");
132  SCIP_CALL( SCIPprintSol(scip, SCIPgetBestSol(scip), NULL, FALSE) );
133  }
134 
135  /* free random number generator */
136  SCIPfreeRandom(scip, &randnumgen);
137 
138  SCIP_CALL( SCIPfree(&scip) );
139 
140  return SCIP_OKAY;
141 }
142 
143 /** main method starting SCIP */
144 int main(
145  int argc, /**< number of arguments from the shell */
146  char** argv /**< array of shell arguments */
147  ) /*lint --e{715}*/
148 {
149  SCIP_RETCODE retcode;
150 
151  retcode = runCircle();
152 
153  /* evaluate return code of the SCIP process */
154  if( retcode != SCIP_OKAY )
155  {
156  /* write error back trace */
157  SCIPprintError(retcode);
158  return -1;
159  }
160 
161  return 0;
162 }
void SCIPfreeRandom(SCIP *scip, SCIP_RANDNUMGEN **randnumgen)
Definition: scip.c:48620
SCIP_RETCODE SCIPcreateConsBasicSOC(SCIP *scip, SCIP_CONS **cons, const char *name, int nvars, SCIP_VAR **vars, SCIP_Real *coefs, SCIP_Real *offsets, SCIP_Real constant, SCIP_VAR *rhsvar, SCIP_Real rhscoeff, SCIP_Real rhsoffset)
Definition: cons_soc.c:5298
#define SCIP_MAXSTRLEN
Definition: def.h:259
SCIP_RETCODE SCIPcreateRandom(SCIP *scip, SCIP_RANDNUMGEN **randnumgen, unsigned int initialseed)
Definition: scip.c:48602
SCIP_RETCODE SCIPreleaseVar(SCIP *scip, SCIP_VAR **var)
Definition: scip.c:18760
#define FALSE
Definition: def.h:64
SCIP_Real SCIPinfinity(SCIP *scip)
Definition: scip.c:47022
int SCIPsnprintf(char *t, int len, const char *s,...)
Definition: misc.c:10011
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:53
static const int npoints
Definition: circle.c:43
SCIP_RETCODE SCIPcreateVarBasic(SCIP *scip, SCIP_VAR **var, const char *name, SCIP_Real lb, SCIP_Real ub, SCIP_Real obj, SCIP_VARTYPE vartype)
Definition: scip.c:17693
SCIP_RETCODE SCIPcreate(SCIP **scip)
Definition: scip.c:748
void SCIPinfoMessage(SCIP *scip, FILE *file, const char *formatstr,...)
Definition: scip.c:1343
SCIP_RETCODE SCIPcreateProbBasic(SCIP *scip, const char *name)
Definition: scip.c:9997
SCIP_RETCODE SCIPsolve(SCIP *scip)
Definition: scip.c:16109
SCIP_RETCODE SCIPaddCons(SCIP *scip, SCIP_CONS *cons)
Definition: scip.c:12585
static SCIP_RETCODE runCircle(void)
Definition: circle.c:102
SCIP_RETCODE SCIPprintOrigProblem(SCIP *scip, FILE *file, const char *extension, SCIP_Bool genericnames)
Definition: scip.c:44212
int main(int argc, char **argv)
Definition: circle.c:144
#define SCIP_CALL(x)
Definition: def.h:350
public data structures and miscellaneous methods
SCIP_RETCODE SCIPfreeTransform(SCIP *scip)
Definition: scip.c:17246
SCIP_RETCODE SCIPincludeDefaultPlugins(SCIP *scip)
static SCIP_RETCODE setupProblem(SCIP *scip, SCIP_RANDNUMGEN *randnumgen)
Definition: circle.c:50
static const unsigned int randseed
Definition: circle.c:46
int SCIPgetNSols(SCIP *scip)
Definition: scip.c:39777
SCIP_Real SCIPrandomGetReal(SCIP_RANDNUMGEN *randnumgen, SCIP_Real minrandval, SCIP_Real maxrandval)
Definition: misc.c:9388
SCIP_SOL * SCIPgetBestSol(SCIP *scip)
Definition: scip.c:39876
SCIP_RETCODE SCIPaddVar(SCIP *scip, SCIP_VAR *var)
Definition: scip.c:11486
SCIP_RETCODE SCIPreleaseCons(SCIP *scip, SCIP_CONS **cons)
Definition: scip.c:27755
#define SCIP_Real
Definition: def.h:149
void SCIPprintError(SCIP_RETCODE retcode)
Definition: scip.c:676
default SCIP plugins
SCIP callable library.
SCIP_RETCODE SCIPfree(SCIP **scip)
Definition: scip.c:780
SCIP_RETCODE SCIPprintSol(SCIP *scip, SCIP_SOL *sol, FILE *file, SCIP_Bool printzeros)
Definition: scip.c:39319