def testBasicInit(): ss = SimStrategy.SimStrategy() state = SimState.SimState(ss) assert isinstance(state.ss, SimStrategy.SimStrategy) assert state.tick == 0 assert state.numAgents() > 0
def testMoneyFlow2(): ss = SimStrategy.SimStrategy() state = SimState.SimState(ss) state.getAgent("ocean_51")._func = MinterAgents.ExpFunc(H=4.0) #send from money 51% minter -> ocean_dao o51_OCEAN_t0 = state.getAgent("ocean_51").OCEAN() dao_OCEAN_t0 = state.getAgent("ocean_dao").OCEAN() assert o51_OCEAN_t0 == 0.0 assert dao_OCEAN_t0 == 0.0 assert state._total_OCEAN_minted == 0.0 #ocean_51 should disburse at tick=1 state.getAgent("ocean_51").takeStep(state) state.tick += 1 state.getAgent("ocean_51").takeStep(state) state.tick += 1 o51_OCEAN_t1 = state.getAgent("ocean_51").OCEAN() dao_OCEAN_t1 = state.getAgent("ocean_dao").OCEAN() assert o51_OCEAN_t1 == 0.0 assert dao_OCEAN_t1 > 0.0 assert state._total_OCEAN_minted > 0.0 assert state._total_OCEAN_minted == dao_OCEAN_t1
def __init__(self, ss: SimStrategy, output_dir: str, state=None): self.state = state if self.state is None: self.state = SimState.SimState(ss) self.output_dir = output_dir self.output_csv = "data.csv" #magic number assert len(self.state.agents) > 0
def testOCEANLinearMinterAgent(): ss = SimStrategy.SimStrategy() assert hasattr(ss, 'time_step') ss.time_step = 2 state = SimState.SimState(ss) class SimpleAgent(BaseAgent): def takeStep(self, state): pass state.agents["a1"] = a1 = SimpleAgent("a1", 0.0, 0.0) #default minter = OCEANLinearMinterAgent("minter", receiving_agent_name="a1", total_OCEAN_to_mint=20.0, s_between_mints=4, n_mints=2) assert minter.USD() == 0.0 assert minter.OCEAN() == 0.0 assert state._total_OCEAN_minted == 0.0 minter.takeStep(state) state.tick += 1 #tick=1 (2 s elapsed), 1st mint assert minter.OCEAN() == 0.0 assert a1.OCEAN() == 10.0 assert state._total_OCEAN_minted == 10.0 minter.takeStep(state) state.tick += 1 #tick=2 (4 s elapsed), noop assert minter.OCEAN() == 0.0 assert a1.OCEAN() == 10.0 assert state._total_OCEAN_minted == 10.0 minter.takeStep(state) state.tick += 1 #tick=3 (6 s elapsed), 2nd mint assert minter.OCEAN() == 0.0 assert a1.OCEAN() == 20.0 assert state._total_OCEAN_minted == 20.0 minter.takeStep(state) state.tick += 1 #tick=4 (8 s elapsed), noop assert minter.OCEAN() == 0.0 assert a1.OCEAN() == 20.0 assert state._total_OCEAN_minted == 20.0 for i in range(10): minter.takeStep(state) state.tick += 1 #tick=14 (28 s elapsed), noop assert minter.OCEAN() == 0.0 assert a1.OCEAN() == 20.0 assert state._total_OCEAN_minted == 20.0
def testMoneyFlow1(): ss = SimStrategy.SimStrategy() state = SimState.SimState(ss) assert hasattr(state, '_percent_burn') state._percent_burn = 0.20 #opc_address -> (opc_burner, ocean_dao) state.getAgent("opc_address").receiveUSD(100.0) state.getAgent("opc_address").takeStep(state) assert state.getAgent("opc_burner").USD() == (0.20 * 100.0) assert state.getAgent("ocean_dao").USD() == (0.80 * 100.0) #ocean_dao -> opc_workers state.getAgent("ocean_dao").takeStep(state) assert state.getAgent("opc_workers").USD() == (0.80 * 100.0) #ocean_dao spends state.getAgent("opc_workers").takeStep(state) assert state.getAgent("opc_workers").USD() == 0.0
def testGetAgent(): ss = SimStrategy.SimStrategy() state = SimState.SimState(ss) assert id(state.getAgent("ocean_dao")) == id(state.agents["ocean_dao"])
def test1(): #getting "tickOneMonthAgo" is tricky, so test it well ss = SimStrategy.SimStrategy() ss.time_step = S_PER_DAY state = SimState.SimState(ss) class SimpleAgent(BaseAgent): def takeStep(self, state): pass state.agents["a1"] = a1 = SimpleAgent("a1", 0.0, 0.0) state.agents["a2"] = a2 = SimpleAgent("a2", 0.0, 0.0) def perc_f1(): return 0.2 def perc_f2(): return 0.8 am = RouterAgent("moneyrouter", 1.0, 10.0, {"a1": perc_f1, "a2": perc_f2}) assert am._USD_per_tick == ([]) assert am._OCEAN_per_tick == ([]) assert am._tickOneMonthAgo(state) == (0) assert am.monthlyUSDreceived(state) == (0.0) assert am.monthlyOCEANreceived(state) == (0.0) am.takeStep(state) assert a1.USD() == (0.2 * 1.0) assert a2.USD() == (0.8 * 1.0) assert a1.OCEAN() == (0.2 * 10.0) assert a2.OCEAN() == (0.8 * 10.0) assert am._USD_per_tick == ([1.0]) assert am._OCEAN_per_tick == ([10.0]) assert am._tickOneMonthAgo(state) == (0) assert am.monthlyUSDreceived(state) == (1.0) assert am.monthlyOCEANreceived(state) == (10.0) am.takeStep(state) state.tick += 1 am.takeStep(state) state.tick += 1 assert am._USD_per_tick == ([1.0, 0.0, 0.0]) assert am._OCEAN_per_tick == ([10.0, 0.0, 0.0]) assert am._tickOneMonthAgo(state) == (0) assert am.monthlyUSDreceived(state) == (1.0) assert am.monthlyOCEANreceived(state) == (10.0) #make a month pass, give $ ticks_per_mo = int(S_PER_MONTH / float(state.ss.time_step)) for i in range(ticks_per_mo): am.receiveUSD(2.0) am.receiveOCEAN(3.0) am.takeStep(state) state.tick += 1 assert am._tickOneMonthAgo(state) > 1 #should be 2 assert am.monthlyUSDreceived(state) == (2.0 * ticks_per_mo) assert am.monthlyOCEANreceived(state) == (3.0 * ticks_per_mo) #make another month pass, don't give $ this time for i in range(ticks_per_mo + 1): am.takeStep(state) state.tick += 1 assert am._tickOneMonthAgo(state) > (1 + ticks_per_mo) assert am.monthlyUSDreceived(state) == (0.0) assert am.monthlyOCEANreceived(state) == (0.0)
def test1(): ss = SimStrategy.SimStrategy() assert hasattr(ss, 'time_step') ss.time_step = S_PER_DAY state = SimState.SimState(ss) class SimpleAgent(BaseAgent): def takeStep(self, state): pass state.agents["a1"] = a1 = SimpleAgent("a1", 0.0, 0.0) assert a1.OCEAN() == 0.0 g1 = GrantGivingAgent( "g1", USD=0.0, OCEAN=1.0, receiving_agent_name="a1", s_between_grants=S_PER_DAY*3, n_actions=4) assert g1.OCEAN() == 1.0 g1.takeStep(state); state.tick += 1 #tick = 1 #disperse here assert g1.OCEAN() == (1.0 - 1.0*1/4) assert a1.OCEAN() == (0.0 + 1.0*1/4) g1.takeStep(state); state.tick += 1 #tick = 2 assert g1.OCEAN() == (1.0 - 1.0*1/4) assert a1.OCEAN() == (0.0 + 1.0*1/4) g1.takeStep(state); state.tick += 1 #tick = 3 assert g1.OCEAN() == (1.0 - 1.0*1/4) assert a1.OCEAN() == (0.0 + 1.0*1/4) g1.takeStep(state); state.tick += 1 #tick = 4 #disperse here assert g1.OCEAN() == (1.0 - 1.0*2/4) assert a1.OCEAN() == (0.0 + 1.0*2/4) g1.takeStep(state); state.tick += 1 #tick = 5 assert g1.OCEAN() == (1.0 - 1.0*2/4) assert a1.OCEAN() == (0.0 + 1.0*2/4) g1.takeStep(state); state.tick += 1 #tick = 6 assert g1.OCEAN() == (1.0 - 1.0*2/4) assert a1.OCEAN() == (0.0 + 1.0*2/4) g1.takeStep(state); state.tick += 1 #tick = 7 #disperse here assert g1.OCEAN() == (1.0 - 1.0*3/4) assert a1.OCEAN() == (0.0 + 1.0*3/4) g1.takeStep(state); state.tick += 1 #tick = 8 assert g1.OCEAN() == (1.0 - 1.0*3/4) assert a1.OCEAN() == (0.0 + 1.0*3/4) g1.takeStep(state); state.tick += 1 #tick = 9 assert g1.OCEAN() == (1.0 - 1.0*3/4) assert a1.OCEAN() == (0.0 + 1.0*3/4) g1.takeStep(state); state.tick += 1 #tick = 10 #disperse here assert g1.OCEAN() == (1.0 - 1.0*4/4) assert a1.OCEAN() == (0.0 + 1.0*4/4) g1.takeStep(state); state.tick += 1 #tick = 11 g1.takeStep(state); state.tick += 1 #tick = 12 g1.takeStep(state); state.tick += 1 #tick = 13 #don't disperse, 0 left assert g1.OCEAN() == (1.0 - 1.0*4/4) assert a1.OCEAN() == (0.0 + 1.0*4/4)
def _test_funcMinter(func): #Simulate with realistic conditions: half-life, OCEAN to mint, # target num half-lives (34, like Bitcoin). #Main check is to see whether we hit expected # years passed. #Not realistic here: time step and s_between_mints. Both are set higher # for unittest speed. They shouldn't affect the main check. manual_test = False #HACK if True do_log_plot = False #if manual_test, linear or log plot? max_year = 5 #if manual_test, stop earlier? ss = SimStrategy.SimStrategy() assert hasattr(ss, 'time_step') if manual_test: ss.time_step = S_PER_DAY s_between_mints = S_PER_DAY else: ss.time_step = 100 * S_PER_DAY s_between_mints = 100 * S_PER_YEAR state = SimState.SimState(ss) class SimpleAgent2(BaseAgent): def takeStep(self, state): pass state.agents["a1"] = a1 = SimpleAgent2("a1", 0.0, 0.0) #base minter = OCEANFuncMinterAgent("minter", receiving_agent_name="a1", total_OCEAN_to_mint=700e6, s_between_mints=s_between_mints, func=func) assert minter.USD() == 0.0 assert minter.OCEAN() == 0.0 assert minter._receiving_agent_name == "a1" assert minter._total_OCEAN_to_mint == 700e6 assert minter._tick_previous_mint == None assert minter._OCEAN_left_to_mint == 700e6 assert minter._func._H == 4.0 assert state._total_OCEAN_minted == 0.0 #run for full length years, OCEAN_left, OCEAN_minted = [], [], [] years.append(0.0) OCEAN_left.append(minter._OCEAN_left_to_mint) OCEAN_minted.append(minter.OCEANminted()) stopped_bc_inf_loop = False while True: minter.takeStep(state) state.tick += 1 year = state.tick * ss.time_step / S_PER_YEAR years.append(year) OCEAN_left.append(minter._OCEAN_left_to_mint) OCEAN_minted.append(minter.OCEANminted()) mo = state.tick * ss.time_step / S_PER_MONTH if manual_test: print(f"tick=%d (mo=%.2f,yr=%.3f), OCEAN_left=%.4g,minted=%.4f" % (state.tick, mo, year, minter._OCEAN_left_to_mint, minter.OCEANminted())) if minter._OCEAN_left_to_mint == 0.0: break if manual_test and year > max_year: break if year > 1000: #avoid infinite loop stopped_bc_inf_loop = True break assert not stopped_bc_inf_loop if not (manual_test and year > max_year): assert minter._OCEAN_left_to_mint == 0.0 #main check: did we hit target # years? #HACK assert 130.0 <= year <= 140.0 if not manual_test: return #plot from matplotlib import pyplot, ticker fig, ax = pyplot.subplots() ax.set_xlabel("Year") #ax.plot(years, OCEAN_left, label="OCEAN left") #ax.set_ylabel("# OCEAN left") ax.plot(years, OCEAN_minted, label="OCEAN minted") ax.set_ylabel("# OCEAN minted") if do_log_plot: pyplot.yscale('log') ax.get_yaxis().set_major_formatter( ticker.ScalarFormatter()) # turn off exponential notation ax.yaxis.set_major_formatter(ticker.FormatStrFormatter('%.2g')) pyplot.show() #popup
def testGrantGivingAgent(self): ss = SimStrategy.SimStrategy() assert hasattr(ss, 'time_step') ss.time_step = S_PER_DAY state = SimState.SimState(ss) class SimpleAgent(BaseAgent): def takeStep(self, state): pass state.agents["a1"] = a1 = SimpleAgent("a1", 0.0, 0.0) g1 = GrantGivingAgent("g1", USD=0.0, OCEAN=1.0, receiving_agent_name="a1", s_between_grants=S_PER_DAY * 3, n_actions=4) g1.takeStep(state) state.tick += 1 #tick = 1 #disperse here self.assertEqual(g1.OCEAN(), 1.0 - 1.0 * 1 / 4) self.assertEqual(a1.OCEAN(), 0.0 + 1.0 * 1 / 4) g1.takeStep(state) state.tick += 1 #tick = 2 self.assertEqual(g1.OCEAN(), 1.0 - 1.0 * 1 / 4) g1.takeStep(state) state.tick += 1 #tick = 3 self.assertEqual(g1.OCEAN(), 1.0 - 1.0 * 1 / 4) g1.takeStep(state) state.tick += 1 #tick = 4 #disperse here self.assertEqual(g1.OCEAN(), 1.0 - 1.0 * 2 / 4) g1.takeStep(state) state.tick += 1 #tick = 5 self.assertEqual(g1.OCEAN(), 1.0 - 1.0 * 2 / 4) g1.takeStep(state) state.tick += 1 #tick = 6 self.assertEqual(g1.OCEAN(), 1.0 - 1.0 * 2 / 4) g1.takeStep(state) state.tick += 1 #tick = 7 #disperse here self.assertEqual(g1.OCEAN(), 1.0 - 1.0 * 3 / 4) g1.takeStep(state) state.tick += 1 #tick = 8 self.assertEqual(g1.OCEAN(), 1.0 - 1.0 * 3 / 4) g1.takeStep(state) state.tick += 1 #tick = 9 self.assertEqual(g1.OCEAN(), 1.0 - 1.0 * 3 / 4) self.assertEqual(a1.OCEAN(), 0.0 + 1.0 * 3 / 4) g1.takeStep(state) state.tick += 1 #tick = 10 #disperse here self.assertEqual(g1.OCEAN(), 1.0 - 1.0 * 4 / 4) g1.takeStep(state) state.tick += 1 #tick = 11 g1.takeStep(state) state.tick += 1 #tick = 12 g1.takeStep(state) state.tick += 1 #tick = 13 #don't disperse, 0 left self.assertEqual(g1.OCEAN(), 1.0 - 1.0 * 4 / 4) self.assertEqual(a1.OCEAN(), 0.0 + 1.0 * 4 / 4)