示例#1
0
def simulate(c_switch):
    data = []
    in_house = 1
    third_party = in_house * 0.80
    switching = in_house * c_switch
    weights = np.linspace(0.1, 1, 100)
    for weight in weights:
        g = Graph()
        g.from_dict({
              1: {'payoff': 0, 'after': []},
              2: {'payoff': 0, 'after': [{'node_id': 1, 'cost': in_house}]},
              3: {'payoff': 0, 'after': [{'node_id': 2, 'cost': in_house}]},
              4: {'payoff': 0, 'after': [{'node_id': 1, 'cost': third_party}]},
              5: {'payoff': 0, 'after': [{'node_id': 4, 'cost': third_party}]},
              6: {'payoff': 0, 'after': [{'node_id': 4, 'cost': switching, 'weight': weight}]},
              7: {'payoff': 0, 'after': [{'node_id': 6, 'cost': in_house}, {'node_id': 5, 'cost': 2 * switching, 'weight': weight}]},
              8: {'payoff': 0, 'after': [{'node_id': 3, 'cost': in_house}]},
              9: {'payoff': 0, 'after': [{'node_id': 5, 'cost': third_party}]},
              10: {'payoff': 0, 'after': [{'node_id': 7, 'cost': in_house}, {'node_id': 5, 'cost': 3 * switching, 'weight': weight}]},
              11: {'payoff': 0, 'after': [{'node_id': 8, 'cost': 0}]},
              12: {'payoff': 0, 'after': [{'node_id': 9, 'cost': 0}]},
              13: {'payoff': 0, 'after': [{'node_id': 10, 'cost': 0}]},
        })

        options = g.get_options(iters=1000)
        data.append([weight / (1.0 + weight), options[2] - options[4]])

    return data
if __name__ == '__main__':
    g = Graph()

    # necktie paradox
    g.from_dict({
        1: {'payoff': 0, 'after': []},
        2: {'payoff': 0, 'after': [{'node_id': 1}]},
        3: {'payoff': 0, 'after': [{'node_id': 1}]},
        4: {'payoff': 100, 'after': [{'node_id': 2, 'cost': 0}, {'node_id': 3, 'cost': 0}]},
        5: {'payoff': 50, 'after': [{'node_id': 3, 'cost': 0}, {'node_id': 2, 'cost': 0}]},
    })

    data_2 = []
    data_3 = []
    for iter in [5, 10, 50, 100, 500, 1000, 5000, 10000, 50000, 1000000]:
        outcomes = g.get_options(iters=iter, extended_stats=True)
        data_2.append([
            outcomes[2]['count'],
            outcomes[2]['mean'],
            outcomes[2]['min'],
            outcomes[2]['max']
        ])

        data_3.append([
            outcomes[3]['count'],
            outcomes[3]['mean'],
            outcomes[3]['min'],
            outcomes[3]['max']
        ])

    df = pd.DataFrame(data_2, columns=['iters', 'outcome', 'min', 'max'])