Skip to content

Commit

Permalink
fixed a bug that crashed the check_flex function
Browse files Browse the repository at this point in the history
  • Loading branch information
MalteUniOldenburg committed Apr 3, 2024
1 parent 6bcb4f9 commit f8e170b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 12 deletions.
38 changes: 28 additions & 10 deletions mango_library/negotiation/winzent/winzent_base_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ async def start_negotiation(self, start_dates, values):
"""
self.governor.power_balance_strategy.start_time = start_dates[0]
values = [math.ceil(value) for value in values]
# print(values)
self._solution_found = False
requirement = xboole.Requirement(
xboole.Forecast((start_dates, values)), ttl=self._current_ttl)
Expand Down Expand Up @@ -456,17 +455,24 @@ async def check_flex(self, reply, flex_to_pick, it):
It is False when the flexibility is not valid anymore.
"""
distributed_value = 0
print(len(self._list_of_acknowledgements_sent))
# print(len(self._list_of_acknowledgements_sent))
for ack in self._list_of_acknowledgements_sent:
print(ack)
# if self.aid == "agent18":
# print(ack)
#if self.aid == "agent5":
#print(f"got this ack: {ack}")
if reply.time_span[it] in ack.time_span:
value_index = ack.time_span.index(reply.time_span[it])
value_index = None
for index, element in enumerate(ack.time_span):
if element == reply.time_span[it]:
value_index = index
break
# value_index = ack.time_span.index(reply.time_span[it])
distributed_value += ack.value[value_index]
logger.info(f"{self.aid} promised {ack.value[0]} to {ack.receiver}")
if self.aid == "agent5":
print(f"distzributed value is: {distributed_value}")
if self.original_flex[reply.time_span[it]][flex_to_pick] - distributed_value == self.flex[reply.time_span[it]][
flex_to_pick]:
print("return true")
return True
else:
logger.info(
Expand Down Expand Up @@ -495,18 +501,26 @@ async def flexibility_valid(self, reply):
flexibility value for the given interval).
:param reply: The reply that the validity of the flexibility is checked for.
"""
if self.aid == "agent5":
print(f"{self.aid}: flex valid check for {reply.sender}")
valid_array = []
for it in range(len(reply.time_span)):
if reply.value[it] > 0:
flex_to_pick = 1
else:
flex_to_pick = 0
valid_array.append(
abs(self.flex[reply.time_span[it]][flex_to_pick]) >= abs(reply.value[it]) and await self.check_flex(
reply, flex_to_pick, it))
print(f"now awaiting check flex for {reply.sender}")
try:
valid_array.append(
abs(self.flex[reply.time_span[it]][flex_to_pick]) >= abs(reply.value[it]) and await self.check_flex(
reply, flex_to_pick, it))
except Exception as e:
print(f"EXCEPTION:{e}")
print(f"array valid for {reply.sender}")
if valid_array[it]:
self.flex[reply.time_span[it]][flex_to_pick] = \
self.flex[reply.time_span[it]][flex_to_pick] - reply.value[it]
print(f"flex adjusted to: {self.flex[reply.time_span[it]][flex_to_pick]} because of {reply.sender}")
return True if all(valid_array) else False

async def handle_initial_reply(self, requirement, message_path):
Expand Down Expand Up @@ -542,6 +556,8 @@ async def handle_acceptance_reply(self, reply):
:return:
"""
# First, check whether the AcceptanceNotification is still valid
if self.aid == "agent5":
print(f"{self.aid} received acceptance reply from {reply.sender}.")
if self.acceptance_valid(reply):
async with self._lock:
flex_valid = await self.flexibility_valid(reply)
Expand Down Expand Up @@ -913,8 +929,10 @@ async def solve(self):
if final:
logger.debug(f"{self.aid} found solution.")
await self.answer_requirements(final, afforded_values, initial_req)
if self.aid == "agent0":
print("found a final")
print(final)
return

if self.governor.triggered_due_to_timeout:
self.governor.triggered_due_to_timeout = False
await self.no_solution_after_timeout()
Expand Down
9 changes: 9 additions & 0 deletions mango_library/negotiation/winzent/winzent_ethical_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ async def handle_initial_reply(self, requirement, message_path):
# The agent received an offer or demand notification as reply.
# If the power_balance is empty, the reply is not considered
# because the negotiation is already done.
# TODO: make this to len(ledger)==1
if self.governor.power_balance.empty():
return
# If there is no solution found already, the reply is considered
Expand All @@ -130,6 +131,10 @@ async def handle_initial_reply(self, requirement, message_path):
if not self.first_initial_reply_received:
self.first_initial_reply_received = True
await asyncio.sleep(self.reply_processing_waiting_time)
if self.aid == "agent0":
print(f"{self.aid}: Solver triggered with following offers:")
for req in self.governor.power_balance._ledger[0]:
print(req.message.values)
logger.debug(f"{self.aid}: Solver triggered!")
await self.solve()
self.first_initial_reply_received = False
Expand Down Expand Up @@ -235,6 +240,8 @@ async def send_initial_replies_to_highest_ethics_scores(self, request_list):
for time_slot in initial_request.time_span:
if time_slot not in temp_flex:
temp_flex[time_slot] = self.get_flexibility_for_interval(time_slot)
if self.aid == "agent5":
print(f"{self.aid} has flex of {temp_flex[time_slot]}")
try:
if abs(initial_request.value[len(specific_request_values)]) >= abs(
temp_flex[time_slot][flex_to_choose]):
Expand All @@ -257,6 +264,8 @@ async def send_initial_replies_to_highest_ethics_scores(self, request_list):
value=specific_request_values, ttl=self._current_ttl,
id=str(uuid.uuid4()),
ethics_score=self.ethics_score)
if self.aid == "agent5":
print(f"{self.aid} sending offer over {specific_request_values[0]} to {initial_request.sender}")
self._current_inquiries_from_agents[reply.id] = reply
await self.send_message(reply)
specific_request_values.clear()
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def find_initial_requirement(power_balance, initiator):
criteria from the power balance list.
"""
print(f"power balance is: {power_balance.ledger}")
# print(f"power balance is: {power_balance.ledger}")
r = None
if initiator == InitiatingParty.Local:
for i in power_balance:
Expand Down
2 changes: 1 addition & 1 deletion tests/unit_test/winzent/test_ethics_single_interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ async def test_one_agent_provides_most_flexibility_and_has_to_prioritise():
first_interval = 0
time_span = [first_interval]
# this variable controls the amount of allowed restarts
number_of_restarted_negotiations_allowed = 3
number_of_restarted_negotiations_allowed = 5

agent_a, agent_b, agent_c, agent_d, agent_e, agent_f, container = await create_six_ethical_agents(
agent_a_ethics_score=2,
Expand Down

0 comments on commit f8e170b

Please sign in to comment.