Exemplo n.º 1
0
 def __init__(self):
     super().__init__()
     products, profiles = sample_products, sample_profiles
     self.slow_simulator = SlowFactorySimulator(
         initial_wallet=initial_wallet,
         initial_storage=initial_storage,
         n_steps=n_steps,
         n_products=len(products),
         profiles=profiles,
         max_storage=max_storage,
     )
     self.fast_simulator = FastFactorySimulator(
         initial_wallet=initial_wallet,
         initial_storage=initial_storage,
         n_steps=n_steps,
         n_products=len(products),
         profiles=profiles,
         max_storage=max_storage,
     )
     self.factory = Factory(
         initial_wallet=initial_wallet,
         initial_storage=initial_storage,
         profiles=profiles,
         max_storage=max_storage,
     )
     self.profiles = self.factory.profiles
     self._payments = defaultdict(list)
     self._transports = defaultdict(list)
Exemplo n.º 2
0
def factory_with_storage(profiles):
    return Factory(
        id="factory",
        profiles=profiles,
        initial_storage=copy.deepcopy(initial_storage),
        initial_wallet=initial_wallet,
        max_storage=max_storage,
    )
Exemplo n.º 3
0
def empty_factory(profiles):
    return Factory(
        id="factory",
        profiles=profiles,
        initial_wallet=initial_wallet,
        initial_storage={},
        max_storage=max_storage,
    )
Exemplo n.º 4
0
class SimulatorsActAsFactory(RuleBasedStateMachine):
    def __init__(self):
        super().__init__()
        products, profiles = sample_products, sample_profiles
        self.slow_simulator = SlowFactorySimulator(
            initial_wallet=initial_wallet,
            initial_storage=initial_storage,
            n_steps=n_steps,
            n_products=len(products),
            profiles=profiles,
            max_storage=max_storage,
        )
        self.fast_simulator = FastFactorySimulator(
            initial_wallet=initial_wallet,
            initial_storage=initial_storage,
            n_steps=n_steps,
            n_products=len(products),
            profiles=profiles,
            max_storage=max_storage,
        )
        self.factory = Factory(
            initial_wallet=initial_wallet,
            initial_storage=initial_storage,
            profiles=profiles,
            max_storage=max_storage,
        )
        self.profiles = self.factory.profiles
        self._payments = defaultdict(list)
        self._transports = defaultdict(list)

    profile_indices = Bundle("profile_indices")
    payments = Bundle("payments")
    times = Bundle("times")
    transports = Bundle("transports")

    @rule(target=profile_indices, k=st.integers(len(sample_profiles)))
    def choose_profile(self, k):
        return k

    @rule(targe=payments, payment=st.floats(-30, 30))
    def choose_payment(self, payment):
        return payment

    @rule(targe=times, payment=st.integers(0, n_steps))
    def choose_time(self, t):
        return t

    @rule(targe=transports, storage=storage())
    def choose_transport(self, t):
        return t

    @rule(profile_index=profile_indices, t=times)
    def run_profile(self, profile_index, t):
        job = Job(
            profile=profile_index,
            time=t,
            line=self.profiles[profile_index].line,
            action="run",
            contract=None,
            override=False,
        )
        end = t + self.profiles[profile_index].n_steps
        if end > n_steps - 1:
            return
        self.factory.schedule(job=job, override=False)
        self.slow_simulator.schedule(job=job, override=False)
        self.fast_simulator.schedule(job=job, override=False)

    @rule(payment=payments, t=times)
    def pay(self, payment, t):
        self._payments[t].append(payment)
        self.slow_simulator.pay(payment, t)
        self.fast_simulator.pay(payment, t)

    @rule(trans=transports, t=times)
    def transport(self, trans, t):
        p, q = trans
        self._transports[t].append(trans)
        self.slow_simulator.transport_to(p, q, t)
        self.fast_simulator.transport_to(p, q, t)

    @rule()
    def run_and_test(self):
        for _ in range(n_steps):
            for payment in self._payments[_]:
                self.factory.pay(payment)
            for p, q in self._transports[_]:
                self.factory.transport_to(p, q)
            self.factory.step()
            assert (
                self.slow_simulator.wallet_at(_)
                == self.fast_simulator.wallet_at(_)
                == self.factory.wallet
            )
            assert (
                self.slow_simulator.balance_at(_)
                == self.fast_simulator.balance_at(_)
                == self.factory.balance
            )
            assert np.all(
                self.slow_simulator.storage_at(_) == self.fast_simulator.storage_at(_)
            )
            assert np.all(
                self.slow_simulator.storage_at(_)
                == storage_as_array(
                    self.factory.storage, n_products=len(sample_products)
                )
            )
            assert np.all(
                self.slow_simulator.line_schedules_at(_) == self.factory.line_schedules
            )
            assert np.all(
                self.fast_simulator.line_schedules_at(_) == self.factory.line_schedules
            )