def init_participants(self): r, t1, t2, rs11, rs12, rs21, rs22 = self.init_response_sets() p111 = Participant.create(response_set=rs11) p112 = Participant.create(response_set=rs11) p121 = Participant.create(response_set=rs12) p122 = Participant.create(response_set=rs12) return r, t1, rs11, rs12, p111, p112, p121, p122
def create_network(token_batches: List[TokenBatch], probability_func, random_number_func) -> nx.DiGraph: """ Creates a new DiGraph with Participants corresponding to the input TokenBatches. """ network = nx.DiGraph() for i, tb in enumerate(token_batches): p_instance = Participant(tb, probability_func, random_number_func) # Make the initial participants have sentiments between 0.5 and 1 p_instance.sentiment = 0.5 + 0.5 * random_number_func() network.add_node(i, item=p_instance) return network
def su_add_to_network(params, step, sL, s, _input): network = s["network"] if _input["new_participant"]: i = len(network.nodes) network.add_node(i, item=Participant( holdings_vesting=None, holdings_nonvesting=TokenBatch(_input["new_participant_tokens"]))) network = setup_influence_edges_single(network, i) network = setup_support_edges(network, i) return "network", network
def create_network(token_batches: List[TokenBatch]) -> nx.DiGraph: """ Creates a new DiGraph with Participants corresponding to the input TokenBatches. """ network = nx.DiGraph() for i, tb in enumerate(token_batches): p_instance = Participant(tb) network.add_node(i, item=p_instance) return network
def create_network(participants: List[TokenBatch]) -> nx.DiGraph: """ Creates a new DiGraph with Participants corresponding to the input TokenBatches. """ network = nx.DiGraph() for i, p in enumerate(participants): p_instance = Participant(holdings_vesting=p, holdings_nonvesting=TokenBatch(0)) network.add_node(i, item=p_instance) return network
def su_add_to_network(params, step, sL, s, _input, **kwargs): network = s["network"] if _input["new_participant"]: network, i = add_participant( network, Participant(TokenBatch(0, _input["new_participant_tokens"]))) if params.get("debug"): print( "GenerateNewParticipant: A new Participant {} invested {}DAI for {} tokens" .format(i, _input['new_participant_investment'], _input['new_participant_tokens'])) return "network", network
def get(self): """Create a bunch of entities so we can practice quering.""" self.write('hi') return # create a researcher out of the current user Researcher.create(users.get_current_user().user_id()) # create some tasks for that researcher t1 = Task.create(name='t1', url_list=[ 'http://www.gentoo.org', 'http://www.xkcd.com']) t2 = Task.create(name='t2', url_list=[ 'http://www.abc.com', 'http://www.hampsterdance.com']) # create response sets in each task rs11 = ResponseSet.create(name='rs11', task=t1) rs12 = ResponseSet.create(name='rs12', task=t1) ResponseSet.create(name='rs21', task=t2) ResponseSet.create(name='rs22', task=t2) # create participants in one of the response sets Participant.create(response_set=rs11) Participant.create(response_set=rs12)
def test_add_participant(self): """ This test ensures that the Participant was added and that setup_influence_edges and setup_support_edges was executed for that particular node. """ n1, j = add_participant(self.network, Participant(TokenBatch(0, 0))) self.assertIsInstance(n1.nodes[j]["item"], Participant) self.assertEqual(len(n1.edges), 5) for u, v, t in n1.edges(data="type"): self.assertEqual(u, 10) self.assertIn(v, [1, 3, 5, 7, 9]) self.assertEqual(t, "support")
def su_add_to_network(params, step, sL, s, _input, **kwargs): network = s["network"] if _input["new_participant"]: i = len(network.nodes) network.add_node(i, item=Participant( TokenBatch(0, _input["new_participant_tokens"]))) network = setup_influence_edges_single(network, i) network = setup_support_edges(network, i) if params[0].get("debug"): print( "GenerateNewParticipant: A new Participant {} invested {}DAI for {} tokens" .format(i, _input['new_participant_investment'], _input['new_participant_tokens'])) return "network", network
def setUp(self): self.network = nx.DiGraph() self.params = { "probability_func": new_probability_func(seed=None), "exponential_func": new_exponential_func(seed=None), "gamma_func": new_gamma_func(seed=None), "random_number_func": new_random_number_func(seed=None) } for i in range(0, 10, 2): self.network.add_node(i, item=Participant( TokenBatch(0, 0), self.params["probability_func"], self.params["random_number_func"])) self.network.add_node(i + 1, item=Proposal(10, 5))
def su_add_to_network(params, step, sL, s, _input, **kwargs): network = s["network"] probability_func = params["probability_func"] exponential_func = params["exponential_func"] random_number_func = params["random_number_func"] for i in _input: ans = _input[i] if ans != 0: network, i = add_participant(network, Participant(TokenBatch( 0, ans["new_participant_tokens"]), probability_func, random_number_func), exponential_func, random_number_func) if params.get("debug"): print("GenerateNewParticipant: A new Participant {} invested {}DAI for {} tokens".format( i, ans['new_participant_investment'], ans['new_participant_tokens'])) return "network", network
def get(self, response_set_id): try: rs = ResponseSet.get_by_id(response_set_id) except (db.BadKeyError, db.KindError, InvalidIdError, EntityDNEError): self.write("Invalid response set id: {}.".format(response_set_id)) return # create a participant to take this task p = Participant.create(response_set=rs) # construct the url that will send the participant to the task request_data = {'pid': str(p.id())} for key in self.request.arguments(): request_data[key] = self.request.get(key) request_pairs = [k + '=' + v for k, v in request_data.items()] url = rs.task.url + '?' + '&'.join(request_pairs) # redirect the user to that url # (was getting some complaints from python about url being unicode...) self.redirect(str(url))
def init(self): # create a dummy user self.testbed.setup_env( USER_EMAIL = '*****@*****.**', USER_ID = '123', USER_IS_ADMIN = '1', overwrite = True) user = users.get_current_user() # create a researcher from that user r = Researcher.create(user) # create a task for the participant t = Task.create(name='t1', url='http://www.a.com') # create a response set for the participant rs = ResponseSet.create(name='rs', task=t) # create a participant p = Participant.create(response_set=rs) # and get the datastore's copy p = db.get(p.key()) return r, t, rs, p
def setUp(self): self.network = nx.DiGraph() for i in range(0, 10, 2): self.network.add_node(i, item=Participant(TokenBatch(0, 0))) self.network.add_node(i + 1, item=Proposal(10, 5))
class TestParticipant(unittest.TestCase): def setUp(self): self.p = Participant() def test_buy(self): """ Test that the function works. If we set the probability to 1, Participant should buy in. If we set the probability to 0, 0 will be returned. """ with patch('entities.probability') as mock: mock.return_value = True # Must set Participant's sentiment artificially high, because of Zargham's force calculation self.p.sentiment = 1 delta_holdings = self.p.buy() self.assertGreater(delta_holdings, 0) with patch('entities.probability') as mock: mock.return_value = False delta_holdings = self.p.buy() self.assertEqual(delta_holdings, 0) def test_sell(self): """ Test that the function works. If we set the probability to 1, Participant should buy in. If we set the probability to 0, 0 will be returned. """ with patch('entities.probability') as mock: mock.return_value = True self.p.sentiment = 0.1 delta_holdings = self.p.sell() self.assertLess(delta_holdings, 0) with patch('entities.probability') as mock: mock.return_value = False delta_holdings = self.p.sell() self.assertEqual(delta_holdings, 0) def test_create_proposal(self): """ Test that the function works. If we set the probability to 1, we should get True. If not, we should get False. """ with patch('entities.probability') as mock: mock.return_value = True self.assertTrue(self.p.create_proposal(10000, 0.5, 100000)) with patch('entities.probability') as mock: mock.return_value = False self.assertFalse(self.p.create_proposal(10000, 0.5, 100000)) def test_vote_on_candidate_proposals(self): """ Test that the function works. If we set the probability to 1, we should get a dict of Proposal UUIDs that the Participant would vote on. If not, we should get an empty dict. """ candidate_proposals = { uuid.UUID(int=179821351946450230734044638685583215499): 1.0, uuid.UUID(int=215071290061070589371009813111193284959): 1.0, uuid.UUID(int=20468923874830131214536379780280861909): 1.0, } with patch('entities.probability') as mock: mock.return_value = False ans = self.p.vote_on_candidate_proposals(candidate_proposals) self.assertFalse(ans) with patch('entities.probability') as mock: mock.return_value = True ans = self.p.vote_on_candidate_proposals(candidate_proposals) self.assertEqual(len(ans), 3) def test_vote_on_candidate_proposals_zargham_algorithm(self): """ Test that Zargham's algorithm works as intended. A cutoff value is set based on your affinity to your favourite Proposal. If there are other Proposals that you like almost as much, you will vote for them too, otherwise if they don't meet the cutoff value you will only vote for your favourite Proposal. """ candidate_proposals = { uuid.UUID(int=179821351946450230734044638685583215499): 1.0, uuid.UUID(int=215071290061070589371009813111193284959): 0.9, uuid.UUID(int=20468923874830131214536379780280861909): 0.8, uuid.UUID(int=268821512376988039567204465930241984322): 0.4, } with patch('entities.probability') as mock: mock.return_value = True ans = self.p.vote_on_candidate_proposals(candidate_proposals) self.assertIn( uuid.UUID(int=179821351946450230734044638685583215499), ans) self.assertIn( uuid.UUID(int=215071290061070589371009813111193284959), ans) self.assertIn( uuid.UUID(int=20468923874830131214536379780280861909), ans) self.assertNotIn( uuid.UUID(int=268821512376988039567204465930241984322), ans) def test_stake_across_all_supported_proposals(self): """ Test that the rebalancing code works as intended. Given 4 Proposals which the Participant has affinity 0.9, 0.9, 0.8, 0.6 to, we should expect an allocation of 0.28125, 0.28125, 02.5, 0.1872 respectively. The calculation should also include vesting and nonvesting TokenBatches. """ p = [Proposal(0, 0) for _ in range(4)] p[0].uuid = uuid.UUID(int=179821351946450230734044638685583215499) p[1].uuid = uuid.UUID(int=215071290061070589371009813111193284959) p[2].uuid = uuid.UUID(int=20468923874830131214536379780280861909) p[3].uuid = uuid.UUID(int=268821512376988039567204465930241984322) supported_proposals = [ (0.9, p[0]), (0.9, p[1]), (0.8, p[2]), (0.6, p[3]), ] self.p.holdings_vesting = TokenBatch(500) self.p.holdings_nonvesting = TokenBatch(500) ans = self.p.stake_across_all_supported_proposals(supported_proposals) print(ans) self.assertEqual( ans[uuid.UUID(int=179821351946450230734044638685583215499)], 281.25000000000006) self.assertEqual( ans[uuid.UUID(int=215071290061070589371009813111193284959)], 281.25000000000006) self.assertEqual( ans[uuid.UUID(int=20468923874830131214536379780280861909)], 250.00000000000006) self.assertEqual( ans[uuid.UUID(int=268821512376988039567204465930241984322)], 187.5)
def setUp(self): self.p = Participant()
def setUp(self): self.params = { "probability_func": new_probability_func(seed=None), "random_number_func": new_random_number_func(seed=None) } self.p = Participant(TokenBatch(100, 100), self.params["probability_func"], self.params["random_number_func"])
class TestParticipant(unittest.TestCase): def setUp(self): self.params = { "probability_func": new_probability_func(seed=None), "random_number_func": new_random_number_func(seed=None) } self.p = Participant(TokenBatch(100, 100), self.params["probability_func"], self.params["random_number_func"]) def test_buy(self): """ Test that the function works. If we set the probability to 1, Participant should buy in. If we set the probability to 0, 0 will be returned. """ # Must set Participant's sentiment artificially high, because of Zargham's force calculation self.p.sentiment = 1 self.p._probability_func = always delta_holdings = self.p.buy() self.assertGreater(delta_holdings, 0) self.p._probability_func = never delta_holdings = self.p.buy() self.assertEqual(delta_holdings, 0) def test_sell(self): """ Test that the function works. If we set the probability to 1, Participant should buy in. If we set the probability to 0, 0 will be returned. """ self.p._probability_func = always self.p.sentiment = 0.1 delta_holdings = self.p.sell() self.assertGreater(delta_holdings, 0) self.p._probability_func = never delta_holdings = self.p.sell() self.assertEqual(delta_holdings, 0) def test_increase_holdings(self): """ Test that the function works. The result nonvesting holdings should be the initial nonvesting holdings plus the added nonvesting holdings. The vesting and the vesting_spent holdings should not me modified. """ added_nonvesting_holdings = 50 inital_nonvesting_holdings = self.p.holdings.nonvesting initial_vesting_holdings = self.p.holdings.vesting initial_vesting_spent_holdings = self.p.holdings.vesting_spent self.p.increase_holdings(added_nonvesting_holdings) self.assertEqual(self.p.holdings.nonvesting, added_nonvesting_holdings + inital_nonvesting_holdings) # Test if vesting and vesting_spent holdings are unchanged self.assertEqual(self.p.holdings.vesting, initial_vesting_holdings) self.assertEqual(self.p.holdings.vesting_spent, initial_vesting_spent_holdings) def test_spend(self): """ Test that the function works. It simply tests it spend() behaviours as a front to TokenBatch.spend() returning a tuple with vesting, vesting_spent and nonvesting. """ self.assertEqual(self.p.spend(50), (self.p.holdings.vesting, self.p.holdings.vesting_spent, self.p.holdings.nonvesting)) def test_create_proposal(self): """ Test that the function works. If we set the probability to 1, we should get True. If not, we should get False. """ self.p._probability_func = always self.assertTrue(self.p.create_proposal(10000, 0.5, 100000)) self.p._probability_func = never self.assertFalse(self.p.create_proposal(10000, 0.5, 100000)) def test_vote_on_candidate_proposals(self): """ Test that the function works. If we set the probability to 1, we should get a dict of Proposal UUIDs that the Participant would vote on. If not, we should get an empty dict. """ candidate_proposals = { 0: 1.0, 1: 1.0, 2: 1.0, } self.p._probability_func = never ans = self.p.vote_on_candidate_proposals(candidate_proposals) self.assertFalse(ans) self.p._probability_func = always ans = self.p.vote_on_candidate_proposals(candidate_proposals) self.assertEqual(len(ans), 3) def test_vote_on_candidate_proposals_zargham_algorithm(self): """ Test that Zargham's algorithm works as intended. A cutoff value is set based on your affinity to your favourite Proposal. If there are other Proposals that you like almost as much, you will vote for them too, otherwise if they don't meet the cutoff value you will only vote for your favourite Proposal. """ candidate_proposals = { 0: 1.0, 1: 0.9, 2: 0.8, 3: 0.4, } self.p._probability_func = always ans = self.p.vote_on_candidate_proposals(candidate_proposals) reference = { 0: 1.0, 1: 0.9, 2: 0.8 } self.assertEqual(ans, reference) def test_stake_across_all_supported_proposals(self): """ Test that the rebalancing code works as intended. Given 4 Proposals which the Participant has affinity 0.9, 0.9, 0.8, 0.6 to, we should expect an allocation of 0.28125, 0.28125, 02.5, 0.1872 respectively. The calculation should also include vesting and nonvesting TokenBatches. """ supported_proposals = [ (0.9, 0), (0.9, 1), (0.8, 2), (0.6, 3), ] self.p.holdings = TokenBatch(500, 500) ans = self.p.stake_across_all_supported_proposals(supported_proposals) reference = { 0: 281.25000000000006, 1: 281.25000000000006, 2: 250.00000000000006, 3: 187.5, } self.assertEqual(ans, reference) def test_update_token_batch_age(self): """ Test that the participant token batch is updated by 1 day after running Participant.update_token_batch_age() """ old_age_days = self.p.holdings.age_days new_age_days = self.p.update_token_batch_age() self.assertEqual(new_age_days, old_age_days + 1) def test_wants_to_exit(self): """ Test that the function works. First force the probability to be True and check if the participant wanted to exit. Then force the probability to be False and check if the participant do not wanted to exit. """ # Set a sentiment below the exit threshold self.p.sentiment = 0.2 self.p._probability_func = always # A participant with vesting should not be able to exit self.assertFalse(self.p.wants_to_exit()) self.p.holdings.vesting = 0 # After the participant has no vesting, he can exit self.assertTrue(self.p.wants_to_exit()) self.p._probability_func = never self.assertFalse(self.p.wants_to_exit())
class TestParticipant(unittest.TestCase): def setUp(self): self.p = Participant(TokenBatch(100, 100)) def test_buy(self): """ Test that the function works. If we set the probability to 1, Participant should buy in. If we set the probability to 0, 0 will be returned. """ with patch('entities.probability') as mock: mock.return_value = True # Must set Participant's sentiment artificially high, because of Zargham's force calculation self.p.sentiment = 1 delta_holdings = self.p.buy() self.assertGreater(delta_holdings, 0) with patch('entities.probability') as mock: mock.return_value = False delta_holdings = self.p.buy() self.assertEqual(delta_holdings, 0) def test_sell(self): """ Test that the function works. If we set the probability to 1, Participant should buy in. If we set the probability to 0, 0 will be returned. """ with patch('entities.probability') as mock: mock.return_value = True self.p.sentiment = 0.1 delta_holdings = self.p.sell() self.assertLess(delta_holdings, 0) with patch('entities.probability') as mock: mock.return_value = False delta_holdings = self.p.sell() self.assertEqual(delta_holdings, 0) def test_create_proposal(self): """ Test that the function works. If we set the probability to 1, we should get True. If not, we should get False. """ with patch('entities.probability') as mock: mock.return_value = True self.assertTrue(self.p.create_proposal(10000, 0.5, 100000)) with patch('entities.probability') as mock: mock.return_value = False self.assertFalse(self.p.create_proposal(10000, 0.5, 100000)) def test_vote_on_candidate_proposals(self): """ Test that the function works. If we set the probability to 1, we should get a dict of Proposal UUIDs that the Participant would vote on. If not, we should get an empty dict. """ candidate_proposals = { 0: 1.0, 1: 1.0, 2: 1.0, } with patch('entities.probability') as mock: mock.return_value = False ans = self.p.vote_on_candidate_proposals(candidate_proposals) self.assertFalse(ans) with patch('entities.probability') as mock: mock.return_value = True ans = self.p.vote_on_candidate_proposals(candidate_proposals) self.assertEqual(len(ans), 3) def test_vote_on_candidate_proposals_zargham_algorithm(self): """ Test that Zargham's algorithm works as intended. A cutoff value is set based on your affinity to your favourite Proposal. If there are other Proposals that you like almost as much, you will vote for them too, otherwise if they don't meet the cutoff value you will only vote for your favourite Proposal. """ candidate_proposals = { 0: 1.0, 1: 0.9, 2: 0.8, 3: 0.4, } with patch('entities.probability') as mock: mock.return_value = True ans = self.p.vote_on_candidate_proposals(candidate_proposals) reference = {0: 1.0, 1: 0.9, 2: 0.8} self.assertEqual(ans, reference) def test_stake_across_all_supported_proposals(self): """ Test that the rebalancing code works as intended. Given 4 Proposals which the Participant has affinity 0.9, 0.9, 0.8, 0.6 to, we should expect an allocation of 0.28125, 0.28125, 02.5, 0.1872 respectively. The calculation should also include vesting and nonvesting TokenBatches. """ supported_proposals = [ (0.9, 0), (0.9, 1), (0.8, 2), (0.6, 3), ] self.p.holdings = TokenBatch(500, 500) ans = self.p.stake_across_all_supported_proposals(supported_proposals) reference = { 0: 281.25000000000006, 1: 281.25000000000006, 2: 250.00000000000006, 3: 187.5, } self.assertEqual(ans, reference)
def setUp(self): self.p = Participant(TokenBatch(100, 100))