Loading .gitlab-ci.yml +1 −0 Original line number Diff line number Diff line Loading @@ -21,6 +21,7 @@ before_script: - mkdir -p $GLPK_CACHE_DIR - "[ -e $GLPK_CACHE_DIR/glpk] || (curl -SLO http://ftp.gnu.org/gnu/glpk/glpk-4.65.tar.gz -o $GLPK_CACHE_DIR/glpk && tar -xzf glpk-4.65.tar.gz && cd glpk-4.65 && ./configure && make && make check && make install && cd ..)" test: script: - pytest README.md +1 −1 Original line number Diff line number Diff line Loading @@ -47,7 +47,7 @@ pip install pyehub Download the repo: ``` git clone https://gitlab.com/energyincities/pyehub.git git clone https://gitlab.com/energyincities/python-ehub ``` Install the libraries needed for PyEHub to run: Loading pyehub/energy_hub/ehub_model.py +33 −4 Original line number Diff line number Diff line Loading @@ -75,7 +75,10 @@ class EHubModel: else: raise RuntimeError("Can't create a hub with no data.") def solve(self, solver_settings: dict = None, is_verbose: bool = False): def solve(self, solver_settings: dict = None, is_verbose: bool = False ): """ Solve the model. Loading @@ -90,18 +93,44 @@ class EHubModel: solver_settings = DEFAULT_SOLVER_SETTINGS solver = solver_settings["name"] if "options" in solver_settings: options = solver_settings["options"] else: options = None if options is None: options = {} if not self._compiled: self.compile() if "solver_path" in solver_settings: solver_path = solver_settings["solver_path"] else: solver_path = None # try: # Use of the solver path is optional, however, # needed when doing cluster submission. print(solver_path) status = pylp.solve(objective=self.objective, constraints=self.constraints, minimize=True, solver=solver, verbose=is_verbose) verbose=is_verbose, options=options, solver_path=solver_path,) # except: # print("did not set path or options") # status = pylp.solve(objective=self.objective, # constraints=self.constraints, # minimize=True, # solver=solver, # verbose=is_verbose) attributes = self._public_attributes() return response_format.create_response(status, attributes) Loading pyehub/pylp/problem.py +58 −14 Original line number Diff line number Diff line Loading @@ -14,16 +14,27 @@ import warnings if pulp.__version__ >= '2.1': import pulp.apis.cplex_api as cplex import pulp.apis.glpk_api as glpk import pulp.apis.choco_api as choco import pulp.apis.gurobi_api as gurobi import pulp.apis.coin_api as coin else: import pulp.solvers as solvers warnings.warn('You are using pulp 2.0 or lower, pulp.apis.core has been changed to pulp.sovers automatically') Status = namedtuple('Status', ['status', 'time']) Status = namedtuple("Status", ["status", "time"]) def solve(*, objective=None, constraints: Iterable[Constraint] = None, minimize: bool = False, solver: str = 'glpk', verbose: bool = False) -> Status: def solve( *, objective=None, constraints: Iterable[Constraint] = None, minimize: bool = False, solver: str = "glpk", verbose: bool = False, options: list = None, solver_path: str = None, **kwargs, ) -> Status: """ Solve the linear programming problem. Loading @@ -31,12 +42,17 @@ def solve(*, objective=None, constraints: Iterable[Constraint] = None, objective: The objective function constraints: The collection of constraints minimize: True for minimizing; False for maximizing solver: The solver to use. Current supports 'glpk' and 'cplex'. solver: The solver to use. Current supports 'glpk', 'theo-cluster' and 'cplex'. verbose: If True, output the results of the solver options list: add options to the (glpk) solver **kwargs: is used to set the cluster path Returns: A tuple of the status (eg: Optimal, Unbounded, etc.) and the elapsed time solver: theo-cluster This is a specific version of the code to do cluster submission. """ if minimize: sense = pulp.LpMinimize Loading @@ -52,16 +68,44 @@ def solve(*, objective=None, constraints: Iterable[Constraint] = None, for constraint in constraints: problem += constraint.construct() if solver == 'glpk' and pulp.__version__ == '2.1': solver = glpk.GLPK_CMD(msg=verbose) elif solver == 'glpk' and pulp.__version__ != '2.1': solver = solvers.GLPK(msg=verbose) elif solver == 'cplex' and pulp.__version__ == '2.1': solver = cplex.CPLEX_CMD(msg=verbose) elif solver == 'cplex' and pulp.__version__ != '2.1': solver = solvers.CPLEX(msg=verbose) if solver == "glpk": if solver_path!=None: solver = glpk.GLPK(msg=verbose, path=solver_path, options=options) else: print("solver_path is not set, going to default, without options") # This catches the error if glpk_path is not set solver = glpk.GLPK(msg=verbose) elif solver == "glpk-cluster": if solver_path!=None: solver = glpk.GLPK(msg=verbose, path=solver_path, options=options) else: print("solver_path is not set, going to default.") # This catches the error if glpk_path is not set solver = glpk.GLPK(msg=verbose, path="/home/theochri/ENV/bin/glpsol") elif solver == "cplex": solver = cplex.CPLEX(msg=verbose) elif solver == "gurobi": if solver_path!=None: solver = gurobi.GUROBI_CMD(msg=verbose,path=solver_path) else: solver = gurobi.GUROBI_CMD(msg=verbose) elif solver == "cbc": if solver_path!=None: solver = coin.PULP_CBC_CMD(msg=verbose,path=solver_path) else: solver = coin.PULP_CBC_CMD(msg=verbose) elif solver == "coin": if solver_path!=None: solver = coin.COIN_CMD(msg=verbose,path=solver_path) else: solver = coin.COIN_CMD(msg=verbose) elif solver == "choco": if solver_path!=None: solver = choco.PULP_CHOCO_CMD(msg=verbose,path=solver_path) else: solver = choco.PULP_CHOCO_CMD(msg=verbose) else: raise ValueError(f'Unsupported solver: {solver}') raise ValueError(f"Unsupported solver: {solver}") with Timer() as time: results = problem.solve(solver) Loading pyehub/simple_example.xlsx 0 → 100644 +305 KiB File added.No diff preview for this file type. View file Loading
.gitlab-ci.yml +1 −0 Original line number Diff line number Diff line Loading @@ -21,6 +21,7 @@ before_script: - mkdir -p $GLPK_CACHE_DIR - "[ -e $GLPK_CACHE_DIR/glpk] || (curl -SLO http://ftp.gnu.org/gnu/glpk/glpk-4.65.tar.gz -o $GLPK_CACHE_DIR/glpk && tar -xzf glpk-4.65.tar.gz && cd glpk-4.65 && ./configure && make && make check && make install && cd ..)" test: script: - pytest
README.md +1 −1 Original line number Diff line number Diff line Loading @@ -47,7 +47,7 @@ pip install pyehub Download the repo: ``` git clone https://gitlab.com/energyincities/pyehub.git git clone https://gitlab.com/energyincities/python-ehub ``` Install the libraries needed for PyEHub to run: Loading
pyehub/energy_hub/ehub_model.py +33 −4 Original line number Diff line number Diff line Loading @@ -75,7 +75,10 @@ class EHubModel: else: raise RuntimeError("Can't create a hub with no data.") def solve(self, solver_settings: dict = None, is_verbose: bool = False): def solve(self, solver_settings: dict = None, is_verbose: bool = False ): """ Solve the model. Loading @@ -90,18 +93,44 @@ class EHubModel: solver_settings = DEFAULT_SOLVER_SETTINGS solver = solver_settings["name"] if "options" in solver_settings: options = solver_settings["options"] else: options = None if options is None: options = {} if not self._compiled: self.compile() if "solver_path" in solver_settings: solver_path = solver_settings["solver_path"] else: solver_path = None # try: # Use of the solver path is optional, however, # needed when doing cluster submission. print(solver_path) status = pylp.solve(objective=self.objective, constraints=self.constraints, minimize=True, solver=solver, verbose=is_verbose) verbose=is_verbose, options=options, solver_path=solver_path,) # except: # print("did not set path or options") # status = pylp.solve(objective=self.objective, # constraints=self.constraints, # minimize=True, # solver=solver, # verbose=is_verbose) attributes = self._public_attributes() return response_format.create_response(status, attributes) Loading
pyehub/pylp/problem.py +58 −14 Original line number Diff line number Diff line Loading @@ -14,16 +14,27 @@ import warnings if pulp.__version__ >= '2.1': import pulp.apis.cplex_api as cplex import pulp.apis.glpk_api as glpk import pulp.apis.choco_api as choco import pulp.apis.gurobi_api as gurobi import pulp.apis.coin_api as coin else: import pulp.solvers as solvers warnings.warn('You are using pulp 2.0 or lower, pulp.apis.core has been changed to pulp.sovers automatically') Status = namedtuple('Status', ['status', 'time']) Status = namedtuple("Status", ["status", "time"]) def solve(*, objective=None, constraints: Iterable[Constraint] = None, minimize: bool = False, solver: str = 'glpk', verbose: bool = False) -> Status: def solve( *, objective=None, constraints: Iterable[Constraint] = None, minimize: bool = False, solver: str = "glpk", verbose: bool = False, options: list = None, solver_path: str = None, **kwargs, ) -> Status: """ Solve the linear programming problem. Loading @@ -31,12 +42,17 @@ def solve(*, objective=None, constraints: Iterable[Constraint] = None, objective: The objective function constraints: The collection of constraints minimize: True for minimizing; False for maximizing solver: The solver to use. Current supports 'glpk' and 'cplex'. solver: The solver to use. Current supports 'glpk', 'theo-cluster' and 'cplex'. verbose: If True, output the results of the solver options list: add options to the (glpk) solver **kwargs: is used to set the cluster path Returns: A tuple of the status (eg: Optimal, Unbounded, etc.) and the elapsed time solver: theo-cluster This is a specific version of the code to do cluster submission. """ if minimize: sense = pulp.LpMinimize Loading @@ -52,16 +68,44 @@ def solve(*, objective=None, constraints: Iterable[Constraint] = None, for constraint in constraints: problem += constraint.construct() if solver == 'glpk' and pulp.__version__ == '2.1': solver = glpk.GLPK_CMD(msg=verbose) elif solver == 'glpk' and pulp.__version__ != '2.1': solver = solvers.GLPK(msg=verbose) elif solver == 'cplex' and pulp.__version__ == '2.1': solver = cplex.CPLEX_CMD(msg=verbose) elif solver == 'cplex' and pulp.__version__ != '2.1': solver = solvers.CPLEX(msg=verbose) if solver == "glpk": if solver_path!=None: solver = glpk.GLPK(msg=verbose, path=solver_path, options=options) else: print("solver_path is not set, going to default, without options") # This catches the error if glpk_path is not set solver = glpk.GLPK(msg=verbose) elif solver == "glpk-cluster": if solver_path!=None: solver = glpk.GLPK(msg=verbose, path=solver_path, options=options) else: print("solver_path is not set, going to default.") # This catches the error if glpk_path is not set solver = glpk.GLPK(msg=verbose, path="/home/theochri/ENV/bin/glpsol") elif solver == "cplex": solver = cplex.CPLEX(msg=verbose) elif solver == "gurobi": if solver_path!=None: solver = gurobi.GUROBI_CMD(msg=verbose,path=solver_path) else: solver = gurobi.GUROBI_CMD(msg=verbose) elif solver == "cbc": if solver_path!=None: solver = coin.PULP_CBC_CMD(msg=verbose,path=solver_path) else: solver = coin.PULP_CBC_CMD(msg=verbose) elif solver == "coin": if solver_path!=None: solver = coin.COIN_CMD(msg=verbose,path=solver_path) else: solver = coin.COIN_CMD(msg=verbose) elif solver == "choco": if solver_path!=None: solver = choco.PULP_CHOCO_CMD(msg=verbose,path=solver_path) else: solver = choco.PULP_CHOCO_CMD(msg=verbose) else: raise ValueError(f'Unsupported solver: {solver}') raise ValueError(f"Unsupported solver: {solver}") with Timer() as time: results = problem.solve(solver) Loading
pyehub/simple_example.xlsx 0 → 100644 +305 KiB File added.No diff preview for this file type. View file