Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sourcery refactored master branch #175

Merged
merged 1 commit into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions benchmark/scripts/benchmark_ortools.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,7 @@ def knapsack_model(n, solver):
weight = randint(1, 100, size=n)
value = randint(1, 100, size=n)

# Create variables
x = {}
for i in range(n):
x[i] = solver.BoolVar("x_%d" % i)

x = {i: solver.BoolVar("x_%d" % i) for i in range(n)}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function knapsack_model refactored with the following changes:

This removes the following comments ( why? ):

# Create variables

# Create constraints
solver.Add(solver.Sum([weight[i] * x[i] for i in range(n)]) <= 200)

Expand Down
3 changes: 2 additions & 1 deletion benchmark/scripts/merge-benchmarks.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
@author: fabian
"""

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 27-27 refactored with the following changes:


from pathlib import Path

import pandas as pd
Expand All @@ -24,7 +25,7 @@
solver_time = df.loc[df.API == "Solving Process", "Time"].values

# Make a correction of the memory usage, some APIs use external processes for the solving process
api_with_external_process = set(["pyomo"])
api_with_external_process = {"pyomo"}
api_with_internal_process = set(snakemake.params.apis).difference(
api_with_external_process
)
Expand Down
8 changes: 1 addition & 7 deletions doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,7 @@

# The full version, including alpha/beta/rc tags
version = pkg_resources.get_distribution("linopy").version
if "dev" in version:
# remove the dev description and reduce by minor one release
release = "master"
else:
release = version


release = "master" if "dev" in version else version
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 26-32 refactored with the following changes:

This removes the following comments ( why? ):

# remove the dev description and reduce by minor one release

# -- General configuration ---------------------------------------------------

# Add any Sphinx extension module names here, as strings. They can be
Expand Down
32 changes: 8 additions & 24 deletions linopy/solvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
Linopy module for solving lp files with different solvers.
"""

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 33-67 refactored with the following changes:


import contextlib
import io
import logging
Expand All @@ -30,41 +31,29 @@
which = "where" if os.name == "nt" else "which"

# the first available solver will be the default solver
try:
with contextlib.suppress(ImportError):
import gurobipy

available_solvers.append("gurobi")
except (ModuleNotFoundError, ImportError):
pass

try:
with contextlib.suppress(ImportError):
import highspy

available_solvers.append("highs")
except (ModuleNotFoundError, ImportError):
pass

if sub.run([which, "glpsol"], stdout=sub.DEVNULL, stderr=sub.STDOUT).returncode == 0:
available_solvers.append("glpk")


if sub.run([which, "cbc"], stdout=sub.DEVNULL, stderr=sub.STDOUT).returncode == 0:
available_solvers.append("cbc")

try:
with contextlib.suppress(ImportError):
import cplex

available_solvers.append("cplex")
except (ModuleNotFoundError, ImportError):
pass

try:
with contextlib.suppress(ImportError):
import xpress

available_solvers.append("xpress")
except (ModuleNotFoundError, ImportError):
pass

logger = logging.getLogger(__name__)


Expand All @@ -80,11 +69,9 @@ def safe_get_solution(status, func):
if status.is_ok:
return func()
elif status.status == SolverStatus.unknown:
try:
with contextlib.suppress(Exception):
logger.warning("Solution status unknown. Trying to parse solution.")
return func()
except Exception:
pass
Comment on lines -83 to -87
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function safe_get_solution refactored with the following changes:

return Solution()


Expand Down Expand Up @@ -271,7 +258,7 @@ def run_glpk(
def read_until_break(f):
while True:
line = f.readline()
if line == "\n" or line == "":
if line in ["\n", ""]:
Comment on lines -274 to +261
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function run_glpk refactored with the following changes:

break
yield line

Expand Down Expand Up @@ -462,11 +449,8 @@ def run_cplex(

is_lp = m.problem_type[m.get_problem_type()] == "LP"

try:
with contextlib.suppress(cplex.exceptions.errors.CplexSolverError):
m.solve()
except cplex.exceptions.errors.CplexSolverError as e:
pass

Comment on lines -465 to -469
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function run_cplex refactored with the following changes:

condition = m.solution.get_status_string()
termination_condition = CONDITION_MAP.get(condition, condition)
status = Status.from_termination_condition(termination_condition)
Expand Down
5 changes: 1 addition & 4 deletions test/test_constraint.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,7 @@ def test_anonymous_constraint_sel(x, y):

def test_constraint_from_rule(m, x, y):
def bound(m, i, j):
if i % 2:
return (i - 1) * x[i - 1] + y[j] >= 0
else:
return i * x[i] >= 0
return (i - 1) * x[i - 1] + y[j] >= 0 if i % 2 else i * x[i] >= 0
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function test_constraint_from_rule refactored with the following changes:


coords = [x.coords["first"], y.coords["second"]]
con = Constraint.from_rule(m, bound, coords)
Expand Down
5 changes: 1 addition & 4 deletions test/test_linear_expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,7 @@ def test_linear_expression_with_errors(m, x):

def test_linear_expression_from_rule(m, x, y):
def bound(m, i):
if i == 1:
return (i - 1) * x[i - 1] + y[i] + 1 * x[i]
else:
return i * x[i] - y[i]
return (i - 1) * x[i - 1] + y[i] + 1 * x[i] if i == 1 else i * x[i] - y[i]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function test_linear_expression_from_rule refactored with the following changes:


expr = LinearExpression.from_rule(m, bound, x.coords)
assert isinstance(expr, LinearExpression)
Expand Down
14 changes: 7 additions & 7 deletions test/test_repr.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ def test_variable_repr():

def test_scalar_variable_repr():
for var in [u, v, x, y, z, a, b, c, d]:
coord = tuple([var.indexes[c][0] for c in var.dims])
coord = tuple(var.indexes[c][0] for c in var.dims)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function test_scalar_variable_repr refactored with the following changes:

repr(var[coord])


def test_single_variable_repr():
for var in [u, v, x, y, z, a, b, c, d]:
coord = tuple([var.indexes[c][0] for c in var.dims])
coord = tuple(var.indexes[c][0] for c in var.dims)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function test_single_variable_repr refactored with the following changes:

repr(var.loc[coord])


Expand All @@ -104,13 +104,13 @@ def test_linear_expression_long():

def test_scalar_linear_expression_repr():
for var in [u, v, x, y, z, a, b, c, d]:
coord = tuple([var.indexes[c][0] for c in var.dims])
coord = tuple(var.indexes[c][0] for c in var.dims)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function test_scalar_linear_expression_repr refactored with the following changes:

repr(1 * var[coord])


def test_single_linear_repr():
for var in [u, v, x, y, z, a, b, c, d]:
coord = tuple([var.indexes[c][0] for c in var.dims])
coord = tuple(var.indexes[c][0] for c in var.dims)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function test_single_linear_repr refactored with the following changes:

repr(1 * var.loc[coord])


Expand All @@ -120,13 +120,13 @@ def test_anonymous_constraint_repr():


def test_scalar_constraint_repr():
repr(1 * u[0, 0] >= 0)
repr(u[0, 0] >= 0)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function test_scalar_constraint_repr refactored with the following changes:



def test_single_constraint_repr():
for var in [u, v, x, y, z, a, b, c, d]:
coord = tuple([var.indexes[c][0] for c in var.dims])
repr(1 * var.loc[coord] == 0)
coord = tuple(var.indexes[c][0] for c in var.dims)
repr(var.loc[coord] == 0)
Comment on lines -128 to +129
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function test_single_constraint_repr refactored with the following changes:

repr(1 * var.loc[coord] - var.loc[coord] == 0)


Expand Down
2 changes: 1 addition & 1 deletion test/test_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def test_variable_getter(x, z):
x[0, 0]

with pytest.raises(AssertionError):
x[0:5]
x[:5]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function test_variable_getter refactored with the following changes:


with pytest.raises(AssertionError):
x[[1, 2, 3]]
Expand Down