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

Use os.makedirs to create parent dirs for solution_fn #203

Merged
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
5 changes: 3 additions & 2 deletions linopy/solvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,7 @@ def run_cbc(
if basis_fn:
command += f"-basisO {basis_fn} "

if not os.path.exists(solution_fn):
os.mknod(solution_fn)
os.makedirs(os.path.dirname(solution_fn), exist_ok=True)

command = command.strip()

Expand Down Expand Up @@ -263,6 +262,8 @@ def run_glpk(
problem_fn = model.to_file(problem_fn)
suffix = problem_fn.suffix[1:]

os.makedirs(os.path.dirname(solution_fn), exist_ok=True)

# TODO use --nopresol argument for non-optimal solution output
command = f"glpsol --{suffix} {problem_fn} --output {solution_fn} "
if log_fn is not None:
Expand Down
7 changes: 7 additions & 0 deletions test/test_optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,13 @@ def test_basis_and_warmstart(tmp_path, model, solver, io_api):
model.solve(solver, warmstart_fn=basis_fn)


@pytest.mark.parametrize("solver,io_api", params)
def test_solution_fn_parent_dir_doesnt_exist(model, solver, io_api, tmp_path):
solution_fn = tmp_path / "non_existent_dir" / "non_existent_file"
status, condition = model.solve(solver, io_api=io_api, solution_fn=solution_fn)
assert status == "ok"


# def init_model_large():
# m = Model()
# time = pd.Index(range(10), name="time")
Expand Down
Loading