Beispiel #1
0
    def sign_contract(self, contract: Contract):
        signature = super().sign_contract(contract)
        if signature is None:
            return None
        with temporary_transaction(self.scheduler):
            schedule = self.scheduler.schedule(
                assume_no_further_negotiations=False,
                contracts=[contract],
                ensure_storage_for=self.transportation_delay,
                start_at=self.awi.current_step)

        if self.sign_only_guaranteed_contracts and (not schedule.valid or
                                                    len(schedule.needs) > 1):
            self.awi.logdebug(
                f'{self.name} refused to sign contract {contract.id} because it cannot be scheduled'
            )
            return None
        # if schedule.final_balance <= self.simulator.final_balance:
        #     self.awi.logdebug(f'{self.name} refused to sign contract {contract.id} because it is not expected '
        #                       f'to lead to profit')
        #     return None
        if schedule.valid:
            profit = schedule.final_balance - self.simulator.final_balance
            self.awi.logdebug(
                f'{self.name} singing contract {contract.id} expecting '
                f'{-profit if profit < 0 else profit} {"loss" if profit < 0 else "profit"}'
            )
        else:
            self.awi.logdebug(
                f'{self.name} singing contract {contract.id} expecting breach')

        self.contract_schedules[contract.id] = schedule
        return signature
Beispiel #2
0
 def can_produce(self,
                 cfp: CFP,
                 assume_no_further_negotiations=False) -> bool:
     """Whether or not we can produce the required item in time"""
     if cfp.product not in self.producing.keys():
         return False
     agreement = SCMLAgreement(time=cfp.max_time,
                               unit_price=cfp.max_unit_price,
                               quantity=cfp.min_quantity)
     min_concluded_at = self.awi.current_step + 1 - int(
         self.immediate_negotiations)
     min_sign_at = min_concluded_at + self.awi.default_signing_delay
     if cfp.max_time < min_sign_at + 1:  # 1 is minimum time to produce the product
         return False
     with temporary_transaction(self.scheduler):
         schedule = self.scheduler.schedule(
             contracts=[
                 Contract(partners=[self.id, cfp.publisher],
                          agreement=agreement,
                          annotation=self._create_annotation(cfp=cfp),
                          issues=cfp.issues,
                          signed_at=min_sign_at,
                          concluded_at=min_concluded_at)
             ],
             ensure_storage_for=self.transportation_delay,
             assume_no_further_negotiations=assume_no_further_negotiations,
             start_at=min_sign_at)
     return schedule.valid and self.can_secure_needs(
         schedule=schedule, step=self.awi.current_step)
Beispiel #3
0
 def total_utility(self, contracts: Collection[Contract] = ()) -> float:
     """Calculates the total utility for the agent of a collection of contracts"""
     if self.scheduler is None:
         raise ValueError(
             'Cannot calculate total utility without a scheduler')
     min_concluded_at = self.awi.current_step
     min_sign_at = min_concluded_at + self.awi.default_signing_delay
     with temporary_transaction(self.scheduler):
         schedule = self.scheduler.schedule(
             contracts=contracts,
             assume_no_further_negotiations=False,
             ensure_storage_for=self.transportation_delay,
             start_at=min_sign_at)
     if not schedule.valid:
         return INVALID_UTILITY
     return schedule.final_balance