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

fix some bugs when using in multithread #32

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
30 changes: 2 additions & 28 deletions chocolate/connection/mongodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,34 +59,8 @@ def lock(self, timeout=-1, poll_interval=0.05):
Raises:
TimeoutError: Raised if the lock could not be acquired.
"""
if self.hold_lock:
yield
else:
start_time = time.time()
l = self._lock.find_one_and_update({"name" : "lock"},
{"$set" : {"lock" : True}},
upsert=True)

while l is not None and l["lock"] != False and timeout != 0:
time.sleep(poll_interval)
l = self._lock.find_one_and_update({"name" : "lock"},
{"$set" : {"lock" : True}},
upsert=True)

if time.time() - start_time > timeout:
break

if l is None or l["lock"] == False:
# The lock is acquired
try:
self.hold_lock = True
yield
finally:
l = self._lock.find_one_and_update({"name" : "lock"},
{"$set" : {"lock" : False}})
self.hold_lock = False
else:
raise TimeoutError("Could not acquire MongoDB lock")
self.hold_lock = True
yield

def all_results(self):
"""Get all entries of the result table as a list. The order is
Expand Down
9 changes: 6 additions & 3 deletions chocolate/sample/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,13 @@ def _next(self, token=None):
# Restore state
self.random_state.rand(i - self.rndrawn)

drawn = [doc["_chocolate_id"] for doc in self.conn.all_results()]
drawn = [int(doc["_chocolate_id"]) for doc in self.conn.all_results()]

while True:
sample = self.random_state.randint(0, l)
if sample not in set(drawn):
break

choices = sorted(set(range(l)) - set(drawn))
sample = self.random_state.choice(choices)
self.rndrawn += i - self.rndrawn + 1

# Some dbs don't like numpy.int64
Expand Down
19 changes: 10 additions & 9 deletions chocolate/search/cmaes.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,15 +190,16 @@ def _load_ancestors(self, results):
ancestors = list()
ancestors_ids = set()
for c in sorted(self.conn.all_complementary(), key=itemgetter("_chocolate_id")):
candidate = dict()
candidate["step"] = numpy.array([c[str(k)] for k in self.space.names()])
candidate["chocolate_id"] = c["_chocolate_id"]
candidate["ancestor_id"] = c["_ancestor_id"]
candidate["X"] = numpy.array([results[c["_chocolate_id"]][str(k)] for k in self.space.names()])
candidate["loss"] = results[c["_chocolate_id"]].get("_loss", None)

ancestors.append(candidate)
ancestors_ids.add(candidate["chocolate_id"])
if c["_chocolate_id"] in results:
candidate = dict()
candidate["step"] = numpy.array([c[str(k)] for k in self.space.names()])
candidate["chocolate_id"] = c["_chocolate_id"]
candidate["ancestor_id"] = c["_ancestor_id"]
candidate["X"] = numpy.array([results[c["_chocolate_id"]][str(k)] for k in self.space.names()])
candidate["loss"] = results[c["_chocolate_id"]].get("_loss", None)

ancestors.append(candidate)
ancestors_ids.add(candidate["chocolate_id"])

return ancestors, ancestors_ids

Expand Down