Skip to content

Commit 7c82a06

Browse files
committed
file overwriting problem
1 parent 9759100 commit 7c82a06

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

modules/experiment/experiment.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ def run_ql(self, num_episodes, alpha, decay):
357357
self.TABLE_INITIAL_STATE, MINI=self.mini, MAX=self.maxi, fixed=self.fixed) # Change for "coupling" to use TABLE_FILL
358358

359359
filename, path, headerstr = self.createStringArgumentsQL(len(self.drivers))
360-
filename = filename + ".txt"
360+
filename = appendTag(filename)
361361

362362
if os.path.isdir(path) is False:
363363
os.makedirs(path)
@@ -393,7 +393,7 @@ def run_ga_ql(self, useQL, useInt, generations, population, crossover, mutation,
393393
MAX=self.maxi, fixed=self.fixed)
394394

395395
filename, path, headerstr = self.createStringArguments(useQL, useInt)
396-
filename = filename + ".txt"
396+
filename = appendTag(filename)
397397

398398
##creates file
399399
if os.path.isdir(path) is False:

modules/functions/functions.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import string
2+
import os
23
from modules.experiment.classes import *
34

45
def is_number(arg):
@@ -215,6 +216,20 @@ def nd(drivers, group_size):
215216
"""
216217
return len(drivers) * group_size
217218

219+
def appendTag(filenamewithtag):
220+
"""
221+
Test if there isn't already a file with the desired name,
222+
paralellization of experiments may result in filename conflict.
223+
"""
224+
append_number = ''
225+
while(os.path.isfile(filenamewithtag + append_number + ".txt")):
226+
if(append_number == ''):
227+
append_number = "-1"
228+
else:
229+
append_number = "-" + str(int(append_number[1:]) + 1)
230+
filenamewithtag += append_number + ".txt"
231+
return filenamewithtag
232+
218233
# return a list with the K shortest paths for the given origin-destination pair,
219234
# given the lists of nodes and edges (this function was created to be called
220235
# externally by another applications)

0 commit comments

Comments
 (0)