Skip to content

Commit

Permalink
Adding is_clean() to controllers
Browse files Browse the repository at this point in the history
Checks that a controller has no negotiators and not internal state (i.e.
it is in a blank state)
  • Loading branch information
yasserfarouk committed Apr 15, 2024
1 parent a24ef8f commit 2183eff
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
4 changes: 4 additions & 0 deletions negmas/negotiators/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ def __init__(
self.__parent = parent
self._auto_kill = auto_kill

def is_clean(self) -> bool:
"""Checks that the agent has no negotiators and that all its intermediate data-structures are reset"""
return len(self._negotiators) == 0

def reset(self):
"""
Resets the controller and kills any negotiators it may have
Expand Down
30 changes: 21 additions & 9 deletions negmas/sao/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,25 @@ def reset(self):
self.__first_proposals_collected = False
super().reset()

def _reset_for(self, negotiator_id: str):
self.__offers.pop(negotiator_id, None)
self.__offer_states.pop(negotiator_id, None)
self.__responses.pop(negotiator_id, None)
self.__proposals.pop(negotiator_id, None)
self.__n_waits.pop(negotiator_id, None)

def is_clean(self) -> bool:
"""Checks that the agent has no negotiators and that all its intermediate data-structures are reset"""
return (
super().is_clean()
and not self.__offers
and not self.__responses
and not self.__proposals
and not self.__offer_states
and not self.__n_waits
and not self.__first_proposals_collected
)

def first_offer(self, negotiator_id: str) -> Outcome | None:
"""
Finds the first offer for this given negotiator. By default it will be the best offer
Expand Down Expand Up @@ -313,7 +332,7 @@ def respond(
for nid, ninfo in self._negotiators.items():
s__ = ninfo.negotiator.nmi.state
if s__.ended:
self._reset_internal(nid)
self._reset_for(nid)
# we arrive here if we already have all the offers to counter. WE may though not have proposed yet
if not self.__first_proposals_collected:
self._set_first_proposals()
Expand All @@ -339,15 +358,8 @@ def respond(
return ResponseType.REJECT_OFFER
return self.__responses.pop(negotiator_id, ResponseType.REJECT_OFFER)

def _reset_internal(self, negotiator_id: str):
self.__offers.pop(negotiator_id, None)
self.__offer_states.pop(negotiator_id, None)
self.__responses.pop(negotiator_id, None)
self.__proposals.pop(negotiator_id, None)
self.__n_waits.pop(negotiator_id, None)

def on_negotiation_end(self, negotiator_id: str, state: SAOState) -> None:
self._reset_internal(negotiator_id)
self._reset_for(negotiator_id)
results = super().on_negotiation_end(negotiator_id, state)
if not self.negotiators:
self.reset()
Expand Down

0 comments on commit 2183eff

Please sign in to comment.