-
Notifications
You must be signed in to change notification settings - Fork 36
/
setup.py
626 lines (539 loc) · 20.9 KB
/
setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
'''
Numberjack is a constraint satisfaction and optimisation library
Copyright (C) 2009-2013 Cork Constraint Computation Center, UCC
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
The authors can be contacted electronically at
'''
from __future__ import print_function
from setuptools import setup
from setuptools.extension import Extension
from setuptools.command.build_ext import build_ext as _build_ext
from distutils.errors import CCompilerError
from distutils.spawn import find_executable
import subprocess as sp
import sys
import os
EXTRA_COMPILE_ARGS = [
'-O3',
# ignore warnings about swig code
'-Wno-shadow', '-Wno-unused-label',
]
EXTRA_LINK_ARGS = []
extensions = [] # List of solver interfaces (C extensions) to be built.
disabled_extensions = [] # The names of solver interfaces which have been
# disabled because the solver was not found on the system.
if sys.platform == 'darwin':
EXTRA_COMPILE_ARGS.extend([
'-stdlib=libc++', '-Wno-shorten-64-to-32',
'-arch', 'x86_64', # force 64bit only builds on Mac
# ignore warning about swig code
'-Wno-self-assign', '-Wno-unused-const-variable',
])
EXTRA_LINK_ARGS.extend(['-arch', 'x86_64', '-stdlib=libc++'])
elif sys.platform.startswith('linux'):
EXTRA_COMPILE_ARGS.extend([
'-Wno-narrowing',
# ignore warning about swig code
'-Wno-unused-but-set-variable',
])
class njbuild_ext(_build_ext):
# Allow for C extension compilation to fail.
def __init__(self, *args, **kwargs):
self.builtsolvernames = []
self.failedsolvernames = []
_build_ext.__init__(self, *args, **kwargs)
def run(self):
_build_ext.run(self)
if disabled_extensions:
print("The following solvers could not be located "
"on your system so their interface has been disabled:",
", ".join(disabled_extensions), file=sys.stderr)
if self.failedsolvernames:
print("Failed to build the following interfaces "
"(details are above):", ", ".join(self.failedsolvernames), file=sys.stderr)
if self.builtsolvernames:
print("Successfully built solver interfaces for",
", ".join(self.builtsolvernames))
def build_extension(self, ext):
try:
_build_ext.build_extension(self, ext)
# Record the names of extensions which were successful, trim '_'
self.builtsolvernames.append(ext.name.split('.')[-1][1:])
except CCompilerError:
self.failedsolvernames.append(ext.name.split('.')[-1][1:])
# ------------------------------ Helper Methods ------------------------------
def xml2config(option, path="xml2-config"):
import shlex
if not find_executable(path):
raise RuntimeError("Could not find %s" % path)
cmd = '%s %s' % (path, option)
p = sp.Popen(cmd, stdout=sp.PIPE, shell=True)
stdout, stderr = p.communicate()
return shlex.split(stdout.decode("utf-8").strip())
CPLEX, GUROBI, SCIP = "CPLEX", "Gurobi", "SCIP"
def get_solver_home(solvername):
# Helps to find the path to a particular third party solver
if solvername == CPLEX:
# Try for environmental variable first
env_path = os.getenv('CPLEXDIR')
if env_path and len(env_path.strip()) > 0:
return env_path
# Try to find the cplex binary in the PATH
ex_path = find_executable('cplex')
if ex_path:
ex_path = os.path.realpath(ex_path) # Expand symbolic links if any
ex_dir = os.path.dirname(ex_path) # Path to the bin directory
return os.path.abspath(os.path.join(ex_dir, os.pardir, os.pardir))
elif solvername == GUROBI:
# Try for environmental variable first
env_path = os.getenv('GUROBI_HOME')
if env_path and len(env_path.strip()) > 0:
return env_path
# Try to find the gurobi_cl binary in the PATH
ex_path = find_executable('gurobi_cl')
if ex_path:
ex_path = os.path.realpath(ex_path) # Expand symbolic links if any
ex_dir = os.path.dirname(ex_path) # Path to the bin directory
return os.path.abspath(os.path.join(ex_dir, os.pardir))
elif solvername == SCIP:
# Try for environmental variable first
env_path = os.getenv('ZIBPATH')
if env_path and len(env_path.strip()) > 0:
return env_path
# Otherwise, if the tgz has been placed in this directory, extract it
# and return that folder's path
scipoptfolder = None
for basename in os.listdir(os.getcwd()):
if os.path.isfile(basename) and \
basename.startswith("scipoptsuite-") and \
basename.endswith(".tgz"):
scipoptfolder = os.path.join(os.getcwd(), basename[:-4])
if not os.path.isdir(scipoptfolder):
sp.call('tar zxf %s' % basename,
cwd=os.getcwd(), shell=True)
break
return scipoptfolder
else:
raise RuntimeError("Error unknown solver name '%s'" % solvername)
return None
# ------------------------------ Extensions ------------------------------
def listextfiles(folder, extension=".cpp"):
ret = []
for bn in os.listdir(folder):
if os.path.splitext(bn)[1] == extension:
ret.append(os.path.join(folder, bn))
return ret
mistralsrc = 'Numberjack/solvers/Mistral/mistral/lib/src'
mistral = Extension(
'Numberjack.solvers._Mistral',
sources=[
'Numberjack/solvers/Mistral.i',
'Numberjack/solvers/Mistral/Mistral.cpp',
] + listextfiles(mistralsrc),
swig_opts=[
'-modern', '-c++',
'-INumberjack/solvers/Mistral',
],
include_dirs=[
'Numberjack/solvers/Mistral',
'Numberjack/solvers/Mistral/mistral/include',
],
libraries=['m'],
language='c++',
define_macros=[('_UNIX', None)],
extra_compile_args=EXTRA_COMPILE_ARGS + xml2config('--cflags') +
['-fPIC', '-Wno-unused-label', '-fexceptions', '-Wno-sequence-point',
'-Wno-overloaded-virtual', '-Wno-narrowing', '-Wno-unused-variable',
'-Wno-unused-but-set-variable'],
extra_link_args=EXTRA_LINK_ARGS + xml2config('--libs'),
)
extensions.append(mistral)
mistral2src = 'Numberjack/solvers/Mistral2/mistral/src/lib'
mistral2 = Extension(
'Numberjack.solvers._Mistral2',
sources=[
'Numberjack/solvers/Mistral2.i',
'Numberjack/solvers/Mistral2/Mistral2.cpp',
] + listextfiles(mistral2src),
swig_opts=[
'-modern', '-c++',
'-INumberjack/solvers/Mistral2',
],
include_dirs=[
'Numberjack/solvers/Mistral2',
'Numberjack/solvers/Mistral2/mistral/src/include',
'Numberjack/solvers/Mistral2/mistral/tools/tclap/include'
],
libraries=['m'],
language='c++',
# define_macros=[('_UNIX', None)],
extra_compile_args=EXTRA_COMPILE_ARGS +
['-fPIC', '-Wno-unused-label', '-fexceptions', '-Wno-overloaded-virtual',
'-Wno-unused-variable', '-Wno-parentheses', '-Wno-reorder',
'-Wno-delete-non-virtual-dtor'],
extra_link_args=EXTRA_LINK_ARGS,
)
extensions.append(mistral2)
toulbar2src = 'Numberjack/solvers/Toulbar2/lib/src'
toulbar2incopsrc = 'Numberjack/solvers/Toulbar2/lib/src/incop'
toulbar2 = Extension(
'Numberjack.solvers._Toulbar2',
sources=[
'Numberjack/solvers/Toulbar2.i',
'Numberjack/solvers/Toulbar2/Toulbar2.cpp',
] + listextfiles(toulbar2src) + listextfiles(toulbar2incopsrc),
swig_opts=[
'-modern', '-c++',
'-INumberjack/solvers/Toulbar2',
],
include_dirs=[
'Numberjack/solvers/Toulbar2',
'Numberjack/solvers/Toulbar2/include',
'Numberjack/solvers/Toulbar2/include/incop',
],
libraries=['gmp'],
language='c++',
define_macros=[
('NUMBERJACK', None),
# ('BOOST', None), # requires libboost-graph-dev installed
('NDEBUG', None),
('LINUX', None),
('LONGLONG_COST', None),
('WIDE_STRING', None),
('LONGDOUBLE_PROB', None),
('NARYCHAR', None),
],
extra_compile_args=EXTRA_COMPILE_ARGS + [
'-Wno-overloaded-virtual', '-Wno-mismatched-tags', '-Wno-unused-private-field',
],
extra_link_args=EXTRA_LINK_ARGS,
)
extensions.append(toulbar2)
mip = Extension(
'Numberjack.solvers._MipWrapper',
sources=[
'Numberjack/solvers/MipWrapper.i',
'Numberjack/solvers/MipWrapper/MipWrapper.cpp',
],
swig_opts=[
'-modern', '-c++',
'-INumberjack/solvers/MipWrapper',
],
include_dirs=['Numberjack/solvers/MipWrapper'],
language='c++',
extra_compile_args=EXTRA_COMPILE_ARGS,
extra_link_args=EXTRA_LINK_ARGS,
)
extensions.append(mip)
sat = Extension(
'Numberjack.solvers._SatWrapper',
sources=[
'Numberjack/solvers/SatWrapper.i',
'Numberjack/solvers/SatWrapper/SatWrapper.cpp',
],
swig_opts=[
'-modern', '-c++',
'-INumberjack/solvers/SatWrapper',
'-INumberjack/solvers/MiniSat/minisat_src/core',
'-INumberjack/solvers/MiniSat/minisat_src/mtl',
],
include_dirs=[
'Numberjack/solvers/SatWrapper/',
'Numberjack/solvers/MiniSat/minisat_src/core',
'Numberjack/solvers/MiniSat/minisat_src/mtl'
],
language='c++',
extra_compile_args=EXTRA_COMPILE_ARGS,
extra_link_args=EXTRA_LINK_ARGS,
)
extensions.append(sat)
minisat = Extension(
'Numberjack.solvers._MiniSat',
sources=[
'Numberjack/solvers/MiniSat.i',
'Numberjack/solvers/MiniSat/MiniSat.cpp',
'Numberjack/solvers/SatWrapper/SatWrapper.cpp',
'Numberjack/solvers/MiniSat/SimpSolver.cpp',
'Numberjack/solvers/MiniSat/minisat_src/core/Solver.C',
],
swig_opts=[
'-modern', '-c++',
'-INumberjack/solvers/MiniSat',
'-INumberjack/solvers/SatWrapper',
'-INumberjack/solvers/MiniSat/minisat_src/core',
'-INumberjack/solvers/MiniSat/minisat_src/mtl',
],
include_dirs=[
'Numberjack/solvers/MiniSat',
'Numberjack/solvers/SatWrapper',
'Numberjack/solvers/MiniSat/minisat_src/core',
'Numberjack/solvers/MiniSat/minisat_src/mtl'
],
language='c++',
extra_compile_args=EXTRA_COMPILE_ARGS,
extra_link_args=EXTRA_LINK_ARGS,
)
extensions.append(minisat)
walksat = Extension(
'Numberjack.solvers._Walksat',
sources=[
'Numberjack/solvers/Walksat.i',
'Numberjack/solvers/Walksat/Walksat.cpp',
'Numberjack/solvers/Walksat/walksat_src/cpp_walksat.cpp',
'Numberjack/solvers/SatWrapper/SatWrapper.cpp',
],
swig_opts=[
'-modern', '-c++',
'-INumberjack/solvers/Walksat',
'-INumberjack/solvers/SatWrapper',
'-INumberjack/solvers/Walksat/walksat_src',
'-INumberjack/solvers/MiniSat/minisat_src/core',
'-INumberjack/solvers/MiniSat/minisat_src/mtl',
],
include_dirs=[
'Numberjack/solvers/Walksat',
'Numberjack/solvers/SatWrapper',
'Numberjack/solvers/Walksat/walksat_src',
'Numberjack/solvers/MiniSat/minisat_src/core',
'Numberjack/solvers/MiniSat/minisat_src/mtl'
],
language='c++',
extra_compile_args=EXTRA_COMPILE_ARGS +
['-ffloat-store', '-Wno-format', '-Wno-unused-variable'],
extra_link_args=EXTRA_LINK_ARGS,
)
extensions.append(walksat)
cplexhome = get_solver_home(CPLEX)
if cplexhome:
concertdir = os.path.abspath(os.path.join(cplexhome, os.pardir, "concert"))
def get_cplex_includes():
cplexincdir = os.path.join(cplexhome, "include")
concertincdir = os.path.join(concertdir, "include")
return [cplexincdir, concertincdir]
def get_cplex_lib_dirs():
"""
CPLEX lib structure: cplexdir/lib/ARCHITECTURE/BUILDTYPE/
This function will walk the cplexdir/lib folder to find the
folders containing static libraries.
"""
def getlibdirs(libfolder):
for dirpath, dirnames, filenames in os.walk(libfolder):
for f in filenames:
if f.endswith(".a"):
return str(dirpath)
raise RuntimeError(
"Error could not find the lib folder in %s" % cplexhome)
cplexlibfolder = os.path.join(cplexhome, "lib")
concertlibfolder = os.path.join(concertdir, "lib")
return [getlibdirs(cplexlibfolder), getlibdirs(concertlibfolder)]
cplex = Extension(
'Numberjack.solvers._CPLEX',
sources=[
'Numberjack/solvers/CPLEX.i',
'Numberjack/solvers/CPLEX/CPLEX.cpp',
'Numberjack/solvers/MipWrapper/MipWrapper.cpp',
],
swig_opts=[
'-modern', '-c++',
'-INumberjack/solvers/CPLEX',
'-INumberjack/solvers/MipWrapper',
],
include_dirs=[
'Numberjack/solvers/CPLEX',
'Numberjack/solvers/MipWrapper',
] + get_cplex_includes(),
library_dirs=get_cplex_lib_dirs(),
language='c++',
define_macros=[('_UNIX', None), ('NDEBUG', None), ('IL_STD', None)],
extra_compile_args=EXTRA_COMPILE_ARGS +
['-O', '-fPIC', '-fexceptions'] +
["-fno-strict-aliasing"] if sys.platform.startswith('linux') else [],
libraries=['concert', 'ilocplex', 'cplex', 'm', 'pthread'],
extra_link_args=EXTRA_LINK_ARGS,
)
extensions.append(cplex)
else:
disabled_extensions.append(CPLEX)
gurobihome = get_solver_home(GUROBI)
if gurobihome:
gurobiincdir = os.path.join(gurobihome, "include")
gurobilibdir = os.path.join(gurobihome, "lib")
def get_gurobi_libs():
def get_gurobi_libname():
import re
libre = re.compile("lib(?P<libname>gurobi\d+)\.so")
for dirpath, dirnames, filenames in os.walk(gurobilibdir):
for f in filenames:
match = libre.match(f)
if match:
return match.groupdict()["libname"]
raise RuntimeError(
"Error could not find the Gurobi library in '%s'" %
gurobilibdir)
return ['gurobi_c++', get_gurobi_libname()]
gurobi = Extension(
'Numberjack.solvers._Gurobi',
sources=[
'Numberjack/solvers/Gurobi.i',
'Numberjack/solvers/Gurobi/Gurobi.cpp',
'Numberjack/solvers/MipWrapper/MipWrapper.cpp',
],
swig_opts=[
'-modern', '-c++',
'-INumberjack/solvers/Gurobi',
'-INumberjack/solvers/MipWrapper',
],
include_dirs=[
'Numberjack/solvers/Gurobi',
'Numberjack/solvers/MipWrapper',
gurobiincdir,
],
library_dirs=[gurobilibdir],
libraries=get_gurobi_libs(),
extra_compile_args=EXTRA_COMPILE_ARGS +
['-fPIC', '-fexceptions'],
extra_link_args=EXTRA_LINK_ARGS,
language='c++',
)
extensions.append(gurobi)
else:
disabled_extensions.append(GUROBI)
scipopthome = get_solver_home(SCIP)
if scipopthome:
scipoptlibfolder = os.path.join(scipopthome, "lib")
def get_scip_libs():
import re
libre = re.compile("lib(?P<libname>scipopt.*)\.a")
for dirpath, dirnames, filenames in os.walk(scipoptlibfolder):
for f in filenames:
match = libre.match(f)
if match:
return [match.groupdict()["libname"]]
raise RuntimeError(
"Error could not find the SCIP library in '%s'" %
scipoptlibfolder)
def compile_scip_ifneeded():
# Check if the static lib already exists and if not compile it.
# There may be a better place to do this other, e.g. only for build_ext
try:
get_scip_libs()
return
except RuntimeError:
makecmd = "make scipoptlib ZIMPL=false ZLIB=false READLINE=false" \
" GAMS=false GMP=false LEGACY=true SPX_LEGACY=true"
print("Compiling SCIP library...")
returncode = sp.call(makecmd, cwd=scipopthome, shell=True)
if returncode != 0:
sys.exit(1)
def get_scipcomponent_folder(comp):
for basename in os.listdir(scipopthome):
fullpath = os.path.join(scipopthome, basename)
if os.path.isdir(fullpath) and basename.startswith(comp):
return fullpath
raise RuntimeError("Error could not find the SCIP %s folder." % comp)
compile_scip_ifneeded()
scipfolder = get_scipcomponent_folder("scip")
scipincfolder = os.path.join(scipfolder, "src")
scip = Extension(
'Numberjack.solvers._SCIP',
sources=[
'Numberjack/solvers/SCIP.i',
'Numberjack/solvers/SCIP/SCIP.cpp',
'Numberjack/solvers/MipWrapper/MipWrapper.cpp',
],
swig_opts=[
'-modern', '-c++',
'-INumberjack/solvers/SCIP',
'-INumberjack/solvers/MipWrapper',
],
include_dirs=[
'Numberjack/solvers/SCIP',
'Numberjack/solvers/MipWrapper',
scipincfolder,
],
library_dirs=[scipoptlibfolder],
libraries=get_scip_libs(),
extra_compile_args=EXTRA_COMPILE_ARGS,
extra_link_args=EXTRA_LINK_ARGS,
language='c++',
)
extensions.append(scip)
else:
disabled_extensions.append(SCIP)
# ------------------------------ End Extensions ------------------------------
# Possibly compile only a subset of the solvers. This can be specified as a CSV
# list on the command line or by passing -solver multiple times, e.g:
# python setup.py build -solver Mistral,Mistral2 -solver SCIP
allsolvers = dict((e.name.split('.')[-1][1:], e) for e in extensions)
SOLVERARG = "-solver"
solversubsetnames = set()
while SOLVERARG in sys.argv:
pos = sys.argv.index(SOLVERARG)
if pos + 1 >= len(sys.argv):
print("Error: you should specify a solver or list of "
"solvers to compile with the %s option." % SOLVERARG, file=sys.stderr)
sys.exit(1)
solvernames = [x.strip() for x in sys.argv[pos + 1].split(",")]
for s in solvernames:
if s not in allsolvers:
print("Error: the solver '%s' is not known, please"
" use one of: %s" % (s, ", ".join(list(allsolvers.keys()))), file=sys.stderr)
sys.exit(1)
solversubsetnames.add(s)
del sys.argv[pos:pos + 2]
if solversubsetnames:
# Ensure MipWrapper is included if needed
if 'CPLEX' in solversubsetnames or 'Gurobi' in solversubsetnames or \
'SCIP' in solversubsetnames:
solversubsetnames.add("MipWrapper")
# Ensure SatWrapper is included if needed
if 'MiniSat' in solversubsetnames or 'Walksat' in solversubsetnames:
solversubsetnames.add("SatWrapper")
extensions = [allsolvers[s] for s in solversubsetnames]
long_desc = """Numberjack is a modelling package written in Python for
constraint programming and combinatorial optimization. Python benefits from a
large and active programming community, Numberjack is therefore a perfect tool
to embed CP technology into larger applications. It is designed to support a
number of efficient underlying C/C++ solvers seamlessly and efficiently.
"""
lic = "License :: OSI Approved :: " \
"GNU General Public License v2 or later (GPLv2+)"
setup(
name='Numberjack',
version='1.2.1',
author='Numberjack Developers',
packages=['Numberjack', 'Numberjack.solvers'],
ext_modules=extensions,
author_email='[email protected]',
url='http://numberjack.ucc.ie/',
license=lic,
description='A Python platform for combinatorial optimization.',
long_description=long_desc,
cmdclass={'build_ext': njbuild_ext},
classifiers=[
"Development Status :: 5 - Production/Stable",
"Environment :: Console",
"Environment :: MacOS X",
"Intended Audience :: Science/Research",
"Natural Language :: English",
"Operating System :: POSIX :: Linux",
"Operating System :: MacOS :: MacOS X",
"Programming Language :: C",
"Programming Language :: C++",
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 3.4",
"Topic :: Scientific/Engineering",
lic,
],
)