Exemplo n.º 1
0
def get_simulation_results(c):
    df = run_simulation(c)
    df_final = df[df.substep.eq(2)]
    random_func = new_random_number_func(None)

    last_network = df_final.iloc[-1, 0]
    candidates = len(
        get_proposals(last_network, status=ProposalStatus.CANDIDATE))
    actives = len(get_proposals(last_network, status=ProposalStatus.ACTIVE))
    completed = len(
        get_proposals(last_network, status=ProposalStatus.COMPLETED))
    failed = len(get_proposals(last_network, status=ProposalStatus.FAILED))
    participants = len(get_participants(last_network))

    score = CommonsScore(params=c, df_final=df_final)

    result = {
        "timestep": list(df_final["timestep"]),
        "funding_pool": list(df_final["funding_pool"]),
        "token_price": list(df_final["token_price"]),
        "sentiment": list(df_final["sentiment"]),
        "score": score.eval(),
        "participants": participants,
        "proposals": {
            "candidates": candidates,
            "actives": actives,
            "completed": completed,
            "failed": failed,
            "total": candidates + actives + completed + failed
        }
    }
    return result, df_final
    def test_get_proposals(self):
        res = get_proposals(self.network)
        self.assertEqual(len(res), 5)

        proposal = Proposal(10, 5)
        proposal.status = ProposalStatus.ACTIVE
        self.network.add_node(len(self.network.nodes), item=proposal)

        res = get_proposals(self.network, status=ProposalStatus.ACTIVE)
        self.assertEqual(len(res), 1)
Exemplo n.º 3
0
    def test_su_update_age_and_conviction_thresholds(self):
        """
        Test that Proposal's age and conviction threshold are being updated.
        The initial Proposal's age is 0, so it expects that both proposals increased
        by 1 after the state update function.
        """
        _input = {}
        ProposalFunding.su_update_age_and_conviction_thresholds(
            self.params, 0, 0, {
                "commons": self.commons,
                "network": copy.copy(self.network),
                "funding_pool": 1000,
                "token_supply": 1000
            }, _input)
        proposals = get_proposals(self.network,
                                  status=ProposalStatus.CANDIDATE)

        proposal_ages = []
        proposal_thresholds = []
        for _, proposal in proposals:
            proposal_ages.append(proposal.age)
            proposal_thresholds.append(proposal.trigger)

        self.assertEqual(proposal_ages, [1, 1])
        self.assertEqual(proposal_thresholds, [np.inf, 2000.0])
Exemplo n.º 4
0
    def p_compare_conviction_and_threshold(params, step, sL, s, **kwargs):
        """
        This policy simply goes through the Proposals to see if their thresholds
        are smaller than their gathered conviction
        """
        network = s["network"]
        funding_pool = s["funding_pool"]
        token_supply = s["token_supply"]

        proposals_w_enough_conviction = []
        proposals = get_proposals(network, status=ProposalStatus.CANDIDATE)
        for idx, proposal in proposals:
            total_conviction = calc_total_conviction(network, idx)
            proposal.conviction = total_conviction
            res = proposal.has_enough_conviction(
                funding_pool, token_supply, params["max_proposal_request"])

            if params.get("debug"):
                print(
                    "ProposalFunding: Proposal {} has {} conviction, and needs {} to pass"
                    .format(idx, proposal.conviction, proposal.trigger))
            if res:
                proposals_w_enough_conviction.append(idx)

        return {
            "proposal_idxs_with_enough_conviction":
            proposals_w_enough_conviction
        }
Exemplo n.º 5
0
    def eval(self) -> float:
        '''
            Calculates the final score using all the defined metrics methods in this class
        '''
        last_network = self.df_final.iloc[-1, 0]
        p_candidates = get_proposals(last_network,
                                     status=ProposalStatus.CANDIDATE)
        candidates = len(p_candidates)
        p_actives = get_proposals(last_network, status=ProposalStatus.ACTIVE)
        actives = len(p_actives)
        p_completed = get_proposals(last_network,
                                    status=ProposalStatus.COMPLETED)
        completed = len(p_completed)
        p_failed = get_proposals(last_network, status=ProposalStatus.FAILED)
        failed = len(p_failed)
        participants = len(get_participants(last_network))

        funds_candidates = sum([p.funds_requested for _, p in p_candidates])
        funds_actives = sum([p.funds_requested for _, p in p_actives])
        funds_completed = sum([p.funds_requested for _, p in p_completed])
        funds_failed = sum([p.funds_requested for _, p in p_failed])

        self.metrics = Metrics(participants=participants,
                               candidates=candidates,
                               funds_candidates=funds_candidates,
                               actives=actives,
                               funds_actives=funds_actives,
                               completed=completed,
                               funds_completed=funds_completed,
                               failed=failed,
                               funds_failed=funds_failed)
        methods = [
            attr for attr in dir(self)
            if callable(getattr(self, attr)) and attr.startswith('calc_')
        ]
        return min(
            round(
                sum([getattr(self, method)()
                     for method in methods]) * self.sigma), 1000)
Exemplo n.º 6
0
    def su_update_age_and_conviction_thresholds(params, step, sL, s, _input, **kwargs):
        network = s["network"]
        funding_pool = s["funding_pool"]
        token_supply = s["token_supply"]

        proposals = get_proposals(
            s["network"], status=ProposalStatus.CANDIDATE)
        for _, proposal in proposals:
            proposal.update_age()
            proposal.update_threshold(
                funding_pool, token_supply, max_proposal_request=params["max_proposal_request"])

        return "network", network
    def test_bootstrap_network(self):
        """
        Tests that the network was created and that the subcomponents work too.
        """
        token_batches = [TokenBatch(1000, VestingOptions(10, 30))
                         for _ in range(4)]
        network = bootstrap_network(token_batches, 1, 3000, 4e6, 0.2)

        edges = list(network.edges(data="type"))
        _, _, edge_types = list(zip(*edges))

        self.assertEqual(edge_types.count('support'), 4)
        self.assertEqual(len(get_participants(network)), 4)
        self.assertEqual(len(get_proposals(network)), 1)
    def test_setup_conflict_edges_single(self):
        """
        Test that the code works, and that edges are created between DIFFERENT
        nodes. Also ensures that edges refer to the node index, not the Proposal
        object stored within the node.
        """
        proposal_count = len(get_proposals(self.network))

        self.network = setup_conflict_edges(self.network, 1, rate=1)
        edges = get_edges_by_type(self.network, "conflict")
        self.assertEqual(len(edges), proposal_count - 1)
        for e in edges:
            self.assertIsInstance(e[0], int)
            self.assertIsInstance(e[1], int)
            self.assertNotEqual(e[0], e[1])
Exemplo n.º 9
0
    def p_participant_votes_on_proposal_according_to_affinity(
            params, step, sL, s, **kwargs):
        """
        This policy collects data from the DiGraph to tell the Participant class
        which candidate proposals it supports, and
        Participant.vote_on_candidate_proposals() will decide which proposals it
        will take action on.

        Then, Participant.stake_across_all_supported_proposals() will tell us
        how much it will stake on each of them.
        """
        network = s["network"]
        participants = get_participants(network)
        candidate_proposals = get_proposals(network,
                                            status=ProposalStatus.CANDIDATE)

        participants_stakes = {}
        for participant_idx, participant in participants:
            proposal_idx_affinity = {}  # {4: 0.9, 5: 0.9}
            for proposal_idx, _ in candidate_proposals:
                proposal_idx_affinity[proposal_idx] = network[participant_idx][
                    proposal_idx]["affinity"]
            proposals_that_participant_cares_enough_to_vote_on = participant.vote_on_candidate_proposals(
                proposal_idx_affinity)

            stake_across_all_supported_proposals_input = []
            for proposal_idx, affinity in proposals_that_participant_cares_enough_to_vote_on.items(
            ):
                stake_across_all_supported_proposals_input.append(
                    (affinity, proposal_idx))
            stakes = participant.stake_across_all_supported_proposals(
                stake_across_all_supported_proposals_input)

            participants_stakes[participant_idx] = stakes

            if params.get("debug"):
                if proposals_that_participant_cares_enough_to_vote_on:
                    print(
                        "ParticipantVoting: Participant {} was given Proposals with corresponding affinities {} and he decided to vote on {}, distributing his tokens thusly {}"
                        .format(
                            participant_idx, proposal_idx_affinity,
                            proposals_that_participant_cares_enough_to_vote_on,
                            stakes))

        return {"participants_stake_on_proposals": participants_stakes}
Exemplo n.º 10
0
    def p_influenced_by_grant_size(params, step, sL, s, **kwargs):
        network = s["network"]
        probability_func = params["probability_func"]

        active_proposals = get_proposals(network, status=ProposalStatus.ACTIVE)
        proposals_that_will_fail = []
        proposals_that_will_succeed = []

        for idx, proposal in active_proposals:
            r_failure = 1/(config.base_failure_rate +
                           np.log(proposal.funds_requested))
            r_success = 1/(config.base_success_rate +
                           np.log(proposal.funds_requested))
            if probability_func(r_failure):
                proposals_that_will_fail.append(idx)
            elif probability_func(r_success):
                proposals_that_will_succeed.append(idx)
        return {"failed": proposals_that_will_fail, "succeeded": proposals_that_will_succeed}