Skip to content

Commit

Permalink
New feat: optional dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasBoTang committed Dec 25, 2024
1 parent 708aa05 commit 03eff89
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 52 deletions.
27 changes: 12 additions & 15 deletions pkg/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

package:
name: pyepo
version: 0.4.1
version: 0.4.2

source:
path: ./
Expand All @@ -15,32 +15,29 @@ build:

channels:
- conda-forge
- defaults
- pytorch
- gurobi
- defaults

requirements:
host:
- python >=3.7
- python >=3.7,<=3.12
- pip
- numpy >=1.22
- scipy
- pathos
- numpy
- tqdm
- pyomo >=6.1.2
- gurobi >=9.1.2
- scikit-learn
- pytorch >=1.13.1
run:
- python >=3.7
- numpy >=1.22
- python >=3.7,<=3.12
- numpy
- scipy
- pathos
- tqdm
- pyomo >=6.1.2
- gurobi >=9.1.2
- scikit-learn
- pytorch >=1.13.1
- pytorch >=1.13

extra:
optional-dependencies:
- gurobi >=9.1.2
- pyomo >=6.1.2

about:
home: https://github.com/khalil-research/PyEPO
Expand Down
11 changes: 9 additions & 2 deletions pkg/pyepo/model/grb/grbmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@
from copy import copy

import numpy as np
import gurobipy as gp
from gurobipy import GRB
try:
import gurobipy as gp
from gurobipy import GRB
_HAS_GUROBI = True
except ImportError:
_HAS_GUROBI = False

from pyepo import EPO
from pyepo.model.opt import optModel
Expand All @@ -24,6 +28,9 @@ class optGrbModel(optModel):

def __init__(self):
super().__init__()
# error
if not _HAS_GUROBI:
raise ImportError("Gurobi is not installed. Please install gurobipy to use this feature.")
# model sense
self._model.update()
if self._model.modelSense == GRB.MINIMIZE:
Expand Down
8 changes: 6 additions & 2 deletions pkg/pyepo/model/grb/knapsack.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
"""

import numpy as np
import gurobipy as gp
from gurobipy import GRB
try:
import gurobipy as gp
from gurobipy import GRB
_HAS_GUROBI = True
except ImportError:
_HAS_GUROBI = False

from pyepo.model.grb.grbmodel import optGrbModel

Expand Down
8 changes: 6 additions & 2 deletions pkg/pyepo/model/grb/portfolio.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
"""

import numpy as np
import gurobipy as gp
from gurobipy import GRB
try:
import gurobipy as gp
from gurobipy import GRB
_HAS_GUROBI = True
except ImportError:
_HAS_GUROBI = False

from pyepo.model.grb.grbmodel import optGrbModel

Expand Down
8 changes: 6 additions & 2 deletions pkg/pyepo/model/grb/shortestpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
Shortest path problem
"""

import gurobipy as gp
from gurobipy import GRB
try:
import gurobipy as gp
from gurobipy import GRB
_HAS_GUROBI = True
except ImportError:
_HAS_GUROBI = False

from pyepo.model.grb.grbmodel import optGrbModel

Expand Down
8 changes: 6 additions & 2 deletions pkg/pyepo/model/grb/tsp.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@
from collections import defaultdict
from itertools import combinations

import gurobipy as gp
import numpy as np
from gurobipy import GRB
try:
import gurobipy as gp
from gurobipy import GRB
_HAS_GUROBI = True
except ImportError:
_HAS_GUROBI = False

from pyepo.model.grb.grbmodel import optGrbModel

Expand Down
20 changes: 12 additions & 8 deletions pkg/pyepo/model/omo/knapsack.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
"""

import numpy as np
from pyomo import environ as pe

from pyepo import EPO
from pyepo.model.omo.omomodel import optOmoModel
try:
from pyomo import environ as pe
from pyepo import EPO
from pyepo.model.omo.omomodel import optOmoModel
_HAS_PYOMO = True
except ImportError:
_HAS_PYOMO = False


class knapsackModel(optOmoModel):
Expand Down Expand Up @@ -98,15 +102,15 @@ def relax(self):


if __name__ == "__main__":

import random
# random seed
random.seed(42)
# set random cost for test
cost = [random.random() for _ in range(16)]
weights = np.random.choice(range(300, 800), size=(2,16)) / 100
capacity = [20, 20]

# solve model
optmodel = knapsackModel(weights=weights, capacity=capacity, solver="gurobi") # init model
optmodel = optmodel.copy()
Expand All @@ -117,7 +121,7 @@ def relax(self):
for i in range(16):
if sol[i] > 1e-3:
print(i)

# relax
optmodel = optmodel.relax()
optmodel.setObj(cost) # set objective function
Expand All @@ -127,7 +131,7 @@ def relax(self):
for i in range(16):
if sol[i] > 1e-3:
print(i)

# add constraint
optmodel = optmodel.addConstr([weights[0,i] for i in range(16)], 10)
optmodel.setObj(cost) # set objective function
Expand All @@ -136,4 +140,4 @@ def relax(self):
print('Obj: {}'.format(obj))
for i in range(16):
if sol[i] > 1e-3:
print(i)
print(i)
15 changes: 11 additions & 4 deletions pkg/pyepo/model/omo/omomodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
"""

from copy import copy
from pyomo import opt as po
from pyomo import environ as pe

from pyepo import EPO
from pyepo.model.opt import optModel
try:
from pyomo import opt as po
from pyomo import environ as pe
from pyepo import EPO
from pyepo.model.opt import optModel
_HAS_PYOMO = True
except ImportError:
_HAS_PYOMO = False


class optOmoModel(optModel):
Expand All @@ -27,6 +31,9 @@ def __init__(self, solver="glpk"):
solver (str): optimization solver in the background
"""
super().__init__()
# error
if not _HAS_PYOMO:
raise ImportError("Pyomo is not installed. Please install pyomo to use this feature.")
# init obj
if self.modelSense == EPO.MINIMIZE:
self._model.obj = pe.Objective(sense=pe.minimize, expr=0)
Expand Down
9 changes: 6 additions & 3 deletions pkg/pyepo/model/omo/shortestpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
Shortest path problem
"""

from pyomo import environ as pe

from pyepo.model.omo.omomodel import optOmoModel
try:
from pyomo import environ as pe
from pyepo.model.omo.omomodel import optOmoModel
_HAS_PYOMO = True
except ImportError:
_HAS_PYOMO = False


class shortestPathModel(optOmoModel):
Expand Down
5 changes: 1 addition & 4 deletions pkg/pyepo/twostage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,4 @@
"""

from pyepo.twostage.sklearnpred import sklearnPred
try:
from pyepo.twostage.autosklearnpred import autoSklearnPred
except:
print("Auto-Sklearn cannot be imported.")
from pyepo.twostage.autosklearnpred import autoSklearnPred
18 changes: 13 additions & 5 deletions pkg/pyepo/twostage/autosklearnpred.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@
"""

import numpy as np
from autosklearn.regression import AutoSklearnRegressor
from autosklearn.metrics import mean_squared_error
from autosklearn.pipeline.components import data_preprocessing
try:
from autosklearn.regression import AutoSklearnRegressor
from autosklearn.metrics import mean_squared_error
from autosklearn.pipeline.components import data_preprocessing
from autosklearn.pipeline.components.base import AutoSklearnPreprocessingAlgorithm
from autosklearn.pipeline.constants import SPARSE, DENSE, UNSIGNED_DATA, INPUT
_HAS_AUTO = True
except ImportError:
_HAS_AUTO = False
from ConfigSpace.configuration_space import ConfigurationSpace
from autosklearn.pipeline.components.base import AutoSklearnPreprocessingAlgorithm
from autosklearn.pipeline.constants import SPARSE, DENSE, UNSIGNED_DATA, INPUT


from pyepo.metric import makeAutoSkScorer

Expand Down Expand Up @@ -62,6 +67,9 @@ def autoSklearnPred(optmodel, seed, timelimit, metric="mse"):
Returns:
AutoSklearnRegressor: Auto-SKlearn multi-output regression model
"""
# error
if not _HAS_AUTO:
raise ImportError("Autosklearn is not installed. Please install autosklearn to use this feature.")
# add NoPreprocessing component to auto-sklearn.
data_preprocessing.add_preprocessor(NoPreprocessing)
# get metrics
Expand Down
9 changes: 6 additions & 3 deletions pkg/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
long_description=long_description,
long_description_content_type="text/markdown",
# version
version = "0.4.1",
version = "0.4.2",
# Github repo
url = "https://github.com/khalil-research/PyEPO",
# author name
Expand All @@ -32,10 +32,13 @@
"scipy",
"pathos",
"tqdm",
"Pyomo>=6.1.2",
"gurobipy>=9.1.2",
"scikit_learn",
"torch>=1.13.1"],
extras_require={
"pyomo": ["pyomo>=6.1.2"],
"gurobi": ["gurobipy>=9.1.2"],
"all": ["pyomo>=6.1.2", "gurobipy>=9.1.2"],
},
# classifiers
classifiers = [
"Programming Language :: Python :: 3",
Expand Down

0 comments on commit 03eff89

Please sign in to comment.