Skip to content

Commit

Permalink
fix(tweets): market.sim_day tweet, order yielding logic
Browse files Browse the repository at this point in the history
The logic for guarding against Nones in the market.sim_day iterator was flawed and causing only values that had both a tweet and an order to get through, meaning the tables had the same counts. This corrects that and ensures all orders go through, and those that have tweets are only a portion.
  • Loading branch information
gwenwindflower committed Apr 27, 2024
1 parent 12fc3a9 commit 08c2fa0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
8 changes: 4 additions & 4 deletions jafgen/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,13 @@ def run_simulation(self):
):
for market in self.markets:
day = Day(i)
for result in market.sim_day(day):
if result is not None and all(val is not None for val in result):
order, tweet = result
for order, tweet in market.sim_day(day):
if order:
self.orders.append(order)
self.tweets.append(tweet)
if order.customer.customer_id not in self.customers:
self.customers[order.customer.customer_id] = order.customer
if tweet:
self.tweets.append(tweet)

def save_results(self) -> None:
stock: Stock = Stock()
Expand Down
11 changes: 7 additions & 4 deletions jafgen/stores/market.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import random
from typing import Iterator

import numpy as np

from jafgen.customers.customers import (
BrunchCrowd,
Casuals,
Commuter,
HealthNut,
RemoteWorker,
Student,
HealthNut,
)
from jafgen.customers.order import Order
from jafgen.customers.tweet import Tweet


class Market(object):
Expand Down Expand Up @@ -38,10 +41,10 @@ def __init__(self, store, num_customers, days_to_penetration=365):

self.active_customers = []

def sim_day(self, day):
def sim_day(self, day) -> Iterator[tuple[Order | None, Tweet | None]]:
days_since_open = self.store.days_since_open(day)
if days_since_open < 0:
yield None
yield None, None
return
elif days_since_open < 7:
pct_penetration = min(days_since_open / self.days_to_penetration, 1)
Expand All @@ -53,7 +56,7 @@ def sim_day(self, day):
num_desired_customers = market_penetration * len(self.addressable_customers)
customers_to_add = int(num_desired_customers - len(self.active_customers))

for i in range(customers_to_add):
for _ in range(customers_to_add):
customer = self.addressable_customers.pop()
self.active_customers.append(customer)

Expand Down

0 comments on commit 08c2fa0

Please sign in to comment.