Skip to content

Commit

Permalink
Use os.makedirs to create parent dirs for solution_fn since this is o…
Browse files Browse the repository at this point in the history
…s independent
  • Loading branch information
Dan Allford committed Nov 13, 2023
1 parent e642a1f commit 5e3daec
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
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

0 comments on commit 5e3daec

Please sign in to comment.