Example #1
0
 def on_notification(self, notification: Notification, notifier: str):
     if notification.type == 'end_negotiation':
         self.__end_negotiation = True
     elif notification.type == 'propose' and notifier == self.mechanism_id:
         return self.propose(state=notification.data['state'])
     elif notification.type == 'respond' and notifier == self.mechanism_id:
         return self.respond(state=notification.data['state'],
                             offer=notification.data('offer', None))
     elif notification.type == 'counter' and notifier == self.mechanism_id:
         return self.counter(state=notification.data['state'],
                             offer=notification.data('offer', None))
Example #2
0
 def on_contract_signed(self, contract: Contract):
     if contract.annotation['buyer'] == self.id and self.use_consumer:
         self.consumer.on_contract_signed(contract)
     schedule = self.contract_schedules[contract.id]
     if schedule is not None and schedule.valid:
         self._execute_schedule(schedule=schedule, contract=contract)
     if contract.annotation['buyer'] != self.id or not self.use_consumer:
         for negotiation in self._running_negotiations.values():
             self.notify(negotiation.negotiator,
                         Notification(type='ufun_modified', data=None))
Example #3
0
    def on_negotiation_end(self, negotiator_id: str,
                           state: MechanismState) -> None:
        super().on_negotiation_end(negotiator_id, state)
        agreement = state.agreement
        # mark this negotiation as completed
        self.completed[negotiator_id] = True
        # if there is an agreement increase the secured amount and check if we are done.
        if agreement is not None:
            self.secured += agreement[QUANTITY]
            if self.secured >= self.target:
                self.awi.loginfo(
                    f"Ending all negotiations on controller {str(self)}")
                # If we are done, end all other negotiations
                for k in self.negotiators.keys():
                    if self.completed[k]:
                        continue
                    self.notify(self.negotiators[k][0],
                                Notification("end_negotiation", None))
        self.kill_negotiator(negotiator_id, force=True)
        if all(self.completed.values()):
            # If we secured everything, just return control to the agent
            if self.secured >= self.target:
                self.awi.loginfo(f"Secured Everything: {str(self)}")
                self.negotiations_concluded_callback(self.awi.current_step,
                                                     self.is_seller)
                return
            # If we did not secure everything we need yet and time allows it, create new negotiations
            tmin, tmax = self.time_range(self.awi.current_step, self.is_seller)

            if self.awi.current_step < tmax + 1 and tmin <= tmax:
                # get a good partner: one that was not retired too much
                random.shuffle(self.partners)
                for other in self.partners:
                    if self.retries[other] <= self.max_retries:
                        partner = other
                        break
                else:
                    return
                self.retries[partner] += 1
                neg = self.create_negotiator()
                self.completed[neg.id] = False
                self.awi.loginfo(
                    f"{str(self)} negotiating with {partner} on u={self.urange}"
                    f", q=(1,{self.target-self.secured}), u=({tmin}, {tmax})")
                self.awi.request_negotiation(
                    not self.is_seller,
                    product=self.product,
                    quantity=(1, self.target - self.secured),
                    unit_price=self.urange,
                    time=(tmin, tmax),
                    partner=partner,
                    negotiator=neg,
                    extra=dict(controller_index=self.awi.current_step,
                               is_seller=self.is_seller),
                )
Example #4
0
    def on_contract_signed(self, contract: Contract):

        cfp = contract.annotation["cfp"]
        """print("contract signed. Seller: ", contract.annotation['seller'], " buyer: ", contract.annotation['buyer'],
              " quantity: ", contract.agreement['quantity'], " product: ", cfp.product, " price: ", contract.agreement['unit_price'],
              " time: ", contract.agreement['time'], " step: ", self.awi.current_step)
"""
        if contract.annotation["buyer"] == self.id:
            if cfp.product in self.consuming:
                self.cfp_records[
                    self.cfp_records[cfp.product].
                    source_of].stock += contract.agreement["quantity"]
                if self.firstArrival == -1:
                    self.firstArrival = contract.agreement["time"]
            else:
                self.cfp_records[
                    cfp.product].stock += contract.agreement["quantity"]
        else:
            self.cfp_records[
                cfp.product].stock -= contract.agreement["quantity"]

        self.cfp_records[cfp.product].prevMin = self.cfp_records[
            cfp.product].MinPrice
        self.cfp_records[cfp.product].prevMax = self.cfp_records[
            cfp.product].MaxPrice

        if cfp.min_unit_price not in self.cfp_records[cfp.product].HistoryMin:
            self.cfp_records[cfp.product].HistoryMin.append(cfp.min_unit_price)
        if cfp.max_unit_price not in self.cfp_records[cfp.product].HistoryMax:
            self.cfp_records[cfp.product].HistoryMax.append(cfp.max_unit_price)

        self.recalculate_prices(cfp.product)
        self.cfp_records[cfp.product].lastSigned = self.awi.current_step

        if contract.annotation["buyer"] == self.id and self.use_consumer:
            self.consumer.on_contract_signed(contract)
        schedule = self.contract_schedules[contract.id]
        if schedule is not None and schedule.valid:
            self._execute_schedule(schedule=schedule, contract=contract)
        if contract.annotation["buyer"] != self.id or not self.use_consumer:
            for negotiation in self._running_negotiations.values():
                self.notify(
                    negotiation.negotiator,
                    Notification(type="ufun_modified", data=None),
                )
Example #5
0
 def on_contract_signed(self, contract: Contract):
     if contract is None:
         return
     cfp: CFP = contract.annotation["cfp"]
     agreement = contract.agreement  # type: ignore
     self.secured_quantities[cfp.product] += agreement["quantity"]
     old_quantity = self.profiles[cfp.product].schedule_at(agreement["time"])
     new_quantity = old_quantity - agreement["quantity"]
     t = agreement["time"]
     self.profiles[cfp.product].set_schedule_at(
         time=t, value=new_quantity, n_steps=self.awi.n_steps
     )
     if self.immediate_cfp_update and new_quantity != old_quantity:
         self.register_product_cfps(
             p=cfp.product, t=t, profile=self.profiles[cfp.product]
         )
     for negotiation in self._running_negotiations.values():
         self.notify(
             negotiation.negotiator, Notification(type="ufun_modified", data=None)
         )