Skip to content

Commit

Permalink
Add automatic detection of dynamic libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
metab0t committed Mar 27, 2024
1 parent 95de5e5 commit 776dfb1
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 30 deletions.
23 changes: 21 additions & 2 deletions src/pyoptinterface/_src/copt.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import platform
import types
from pathlib import Path
import logging

from .copt_model_ext import RawModel, Env, COPT, load_library, is_library_loaded
from .attributes import (
Expand All @@ -20,7 +21,10 @@
)
from .aml import make_nd_variable

if not is_library_loaded():

def detected_libraries():
libs = []

subdir = {
"Linux": "lib",
"Darwin": "lib",
Expand All @@ -32,11 +36,26 @@
"Windows": "copt.dll",
}[platform.system()]

# Environment
home = os.environ.get("COPT_HOME", None)
if home and os.path.exists(home):
lib = Path(home) / subdir / libname
if lib.exists():
ret = load_library(str(lib))
libs.append(str(lib))

# default names
default_libname = libname
libs.append(default_libname)

return libs


libs = detected_libraries()
for lib in libs:
ret = load_library(lib)
if ret:
logging.info(f"Loaded COPT library: {lib}")
break

DEFAULT_ENV = None

Expand Down
46 changes: 39 additions & 7 deletions src/pyoptinterface/_src/gurobi.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import types
from pathlib import Path
import re
import logging

from .gurobi_model_ext import RawModel, RawEnv, GRB, load_library, is_library_loaded
from .attributes import (
Expand All @@ -26,7 +27,10 @@
from .constraint_bridge import bridge_soc_quadratic_constraint
from .aml import make_nd_variable

if not is_library_loaded():

def detected_libraries():
libs = []

subdir = {
"Linux": "lib",
"Darwin": "lib",
Expand All @@ -43,18 +47,46 @@
"Windows": "*.dll",
}[platform.system()]

# Environment
home = os.environ.get("GUROBI_HOME", None)
if home and os.path.exists(home):
dir = Path(home) / subdir
libs = []
for path in dir.glob(suffix_pattern):
match = re.match(libname_pattern, path.name)
if match:
libs.append(path)
for lib in libs:
ret = load_library(str(lib))
if ret:
break
libs.append(str(path))

# gurobipy installation
try:
import gurobipy

dir = Path(gurobipy.__path__[0])
if platform.system() != "Windows":
dir = dir / ".libs"
for path in dir.glob(suffix_pattern):
match = re.match(libname_pattern, path.name)
if match:
libs.append(str(path))
except Exception:
pass

# default names
default_libname = {
"Linux": "libgurobi110.so",
"Darwin": "libgurobi110.dylib",
"Windows": "gurobi110.dll",
}[platform.system()]
libs.append(default_libname)

return libs


libs = detected_libraries()
for lib in libs:
ret = load_library(lib)
if ret:
logging.info(f"Loaded Gurobi library: {lib}")
break

DEFAULT_ENV = None

Expand Down
48 changes: 34 additions & 14 deletions src/pyoptinterface/_src/highs.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import os
import platform
import types
Expand Down Expand Up @@ -26,7 +27,10 @@
)
from .aml import make_nd_variable

if not is_library_loaded():

def detected_libraries():
libs = []

subdir = {
"Linux": "lib",
"Darwin": "lib",
Expand All @@ -38,23 +42,39 @@
"Windows": "highs.dll",
}[platform.system()]

# Environment
home = os.environ.get("HiGHS_HOME", None)
if home and os.path.exists(home):
lib = Path(home) / subdir / libname
if lib.exists():
ret = load_library(str(lib))

if not is_library_loaded():
try:
import highsbox

home = highsbox.highs_dist_dir()
if os.path.exists(home):
lib = Path(home) / subdir / libname
if lib.exists():
ret = load_library(str(lib))
except Exception as e:
pass
libs.append(str(lib))

# HiGHS pypi installation
try:
import highsbox

home = highsbox.highs_dist_dir()

if os.path.exists(home):
lib = Path(home) / subdir / libname
if lib.exists():
libs.append(str(lib))
except Exception:
pass

# default names
default_libname = libname
libs.append(default_libname)

return libs


libs = detected_libraries()
for lib in libs:
ret = load_library(lib)
if ret:
logging.info(f"Loaded HiGHS library: {lib}")
break

variable_attribute_get_func_map = {
VariableAttribute.Value: lambda model, v: model.get_value(v),
Expand Down
56 changes: 49 additions & 7 deletions src/pyoptinterface/_src/mosek.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import types
from pathlib import Path
import re
import logging

from .mosek_model_ext import RawModel, Env, Enum, load_library, is_library_loaded
from .attributes import (
Expand All @@ -23,7 +24,10 @@
from .constraint_bridge import bridge_soc_quadratic_constraint
from .aml import make_nd_variable

if not is_library_loaded():

def detected_libraries():
libs = []

libname_pattern = {
"Linux": r"libmosek64\.so",
"Darwin": r"libmosek64\.dylib",
Expand All @@ -35,18 +39,56 @@
"Windows": "*.dll",
}[platform.system()]

# Environment
home = os.environ.get("MOSEK_10_1_BINDIR", None)
if home and os.path.exists(home):
dir = Path(home)
libs = []
for path in dir.glob(suffix_pattern):
match = re.match(libname_pattern, path.name)
if match:
libs.append(path)
for lib in libs:
ret = load_library(str(lib))
if ret:
break
libs.append(str(path))

# mosekpy installation
try:
import mosek

dir = Path(mosek.__path__[0])

libname_pattern = {
"Linux": r"libmosek64\.so\.*",
"Darwin": r"libmosek64\.*\.dylib",
"Windows": r"mosek64_(\d+)_(\d+)\.dll",
}[platform.system()]
suffix_pattern = {
"Linux": "*.so.*",
"Darwin": "*.dylib",
"Windows": "*.dll",
}[platform.system()]

for path in dir.glob(suffix_pattern):
match = re.match(libname_pattern, path.name)
if match:
libs.append(str(path))
except Exception:
pass

# default names
default_libname = {
"Linux": "libmosek64.so",
"Darwin": "libmosek64.dylib",
"Windows": "mosek64_10_1.dll",
}[platform.system()]
libs.append(default_libname)

return libs


libs = detected_libraries()
for lib in libs:
ret = load_library(lib)
if ret:
logging.info(f"Loaded Mosek library: {lib}")
break

DEFAULT_ENV = None

Expand Down

0 comments on commit 776dfb1

Please sign in to comment.