Commit a6b186cb authored by Theo Christiaanse's avatar Theo Christiaanse
Browse files

Merge branch 'cluster-theo' into 'master'

Cluster theo

See merge request !9
parents 78187e76 c4e3641e
Loading
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -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
+1 −1
Original line number Diff line number Diff line
@@ -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:
+33 −4
Original line number Diff line number Diff line
@@ -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.

@@ -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)
+58 −14
Original line number Diff line number Diff line
@@ -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.

@@ -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
@@ -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)
+305 KiB

File added.

No diff preview for this file type.

Loading