def study(name, theta_init=None): # graphe = nx.read_edgelist("Internet_hyperbolic_map/AS_ARK200906_topology.txt") graphe = nx.read_weighted_edgelist(name) gamma = exponent.get_exponent(graphe) # gamma = 2.1 # Verify gamma : # exponent.plot_gamma(graphe) Nobs = graphe.number_of_nodes() k_bar_obs = np.mean(graphe.degree().values()) k_max_obs = max(graphe.degree().values()) C_obs = nx.average_clustering(graphe, count_zeros=False) print Nobs, k_bar_obs, k_max_obs, C_obs alpha, kappa0, kappaC, P0, k_bar = equations.solve(gamma, k_bar_obs, k_max_obs) # Verify equations #equations.verify(alpha, kappa0, kappaC, P0, k_bar, gamma, k_bar_obs, k_max_obs) #N = Nobs / (1 - P0) #print gamma, alpha, kappa0, kappaC, P0, k_bar, N """ alpha = 0.58 kappa0 = 0.9 kappaC = 4790 P0 = 0.33 N = 35685 k_bar = 9.86 """ # Estimating beta ''' beta,path_followed = generate_networks.approximated_beta(N,k_bar,kappa0,gamma,C_obs, start=1.01, step = 0.01,nb_networks=100) print beta ''' # Verify beta: # print path_followed beta = 1.02 #beta =1.45 # Creating map: graphe = nx.convert_node_labels_to_integers(graphe) constants = kappa0, gamma, beta, Nobs, k_bar r_opt = loglikelihood.rayon(graphe, constants) if theta_init is None: theta_init = 2 * math.pi * np.random.randint(0, Nobs, Nobs) / Nobs theta_opt = loglikelihood.optimisation_likelihood( graphe, constants, theta_init=theta_init.copy()) #theta_opt = loglikelihood.optimisation_complete_par_etapes_likelihood(graphe,[5,4,3,2,0],theta_init.copy(),constants) #theta_opt = loglikelihood.optimisation_par_etapes_likelihood(graphe,"",14,theta_init.copy(),constants,method="core_number") with open('test50.txt', 'w') as f: f.write(str(theta_opt)) print "saved" return r_opt, theta_opt
def study(name,theta_init = None) : # graphe = nx.read_edgelist("Internet_hyperbolic_map/AS_ARK200906_topology.txt") graphe = nx.read_weighted_edgelist(name) gamma = exponent.get_exponent(graphe) # gamma = 2.1 # Verify gamma : # exponent.plot_gamma(graphe) Nobs = graphe.number_of_nodes() k_bar_obs = np.mean(graphe.degree().values()) k_max_obs = max(graphe.degree().values()) C_obs = nx.average_clustering(graphe, count_zeros=False) print Nobs, k_bar_obs, k_max_obs, C_obs alpha, kappa0, kappaC, P0, k_bar = equations.solve(gamma, k_bar_obs, k_max_obs) # Verify equations #equations.verify(alpha, kappa0, kappaC, P0, k_bar, gamma, k_bar_obs, k_max_obs) #N = Nobs / (1 - P0) #print gamma, alpha, kappa0, kappaC, P0, k_bar, N """ alpha = 0.58 kappa0 = 0.9 kappaC = 4790 P0 = 0.33 N = 35685 k_bar = 9.86 """ # Estimating beta ''' beta,path_followed = generate_networks.approximated_beta(N,k_bar,kappa0,gamma,C_obs, start=1.01, step = 0.01,nb_networks=100) print beta ''' # Verify beta: # print path_followed beta = 1.02 #beta =1.45 # Creating map: graphe = nx.convert_node_labels_to_integers(graphe) constants = kappa0,gamma,beta,Nobs,k_bar r_opt = loglikelihood.rayon(graphe,constants) if theta_init is None : theta_init = 2*math.pi*np.random.randint(0,Nobs,Nobs)/Nobs theta_opt = loglikelihood.optimisation_likelihood(graphe,constants,theta_init=theta_init.copy()) #theta_opt = loglikelihood.optimisation_complete_par_etapes_likelihood(graphe,[5,4,3,2,0],theta_init.copy(),constants) #theta_opt = loglikelihood.optimisation_par_etapes_likelihood(graphe,"",14,theta_init.copy(),constants,method="core_number") with open('test50.txt', 'w') as f: f.write(str(theta_opt)) print "saved" return r_opt,theta_opt
async def do_roll(expression, session, character=None, output=[]): ''' Does the variable replacement and dice rolling ''' expression = expression.strip() match = re.match(r'^(.*)\s+((?:dis)?adv|dis|(?:dis)?advantage)$', expression) if match: expression = match.group(1) if match.group(2) in ['adv', 'advantage']: adv = 1 elif match.group(2) in ['dis', 'disadv', 'disadvantage']: adv = -1 else: raise Exception('Invalid adv/disadv operator') else: adv = 0 original_expression = expression # Set up operations def roll_dice(a, b, *, silent=False): rolls = [] for _ in range(a): if b > 0: n = random.randint(1, b) elif b < 0: n = random.randint(b, -1) else: n = 0 rolls.append(n) value = sum(rolls) if not silent: output.append('{}d{}: {} = {}'.format(a, b, ' + '.join(map(str, rolls)), value)) return value def great_weapon_fighting(a, b, low=2, *, silent=False): rolls = [] rerolls = [] value = 0 for _ in range(a): n = roll_dice(1, b, silent=True) rolls.append(n) if n <= low: n2 = random.randint(1, b) rerolls.append(n2) value += n2 else: value += n if not silent: rolled = ' + '.join(map(str, rolls)) if rerolls: rerolled = list(filter(lambda a: a > low, rolls)) rerolled.extend(rerolls) rerolled = ' + '.join(map(str, rerolled)) output.append('{}g{}: {}, rerolled: {} = {}'.format( a, b, rolled, rerolled, value)) else: output.append('{}g{}: {} = {}'.format(a, b, rolled, value)) return value def roll_advantage(a, b, *, silent=False): if a == 1 and b == 20: first = roll_dice(a, b, silent=True) second = roll_dice(a, b, silent=True) out = max(first, second) if not silent: output.append('{}d{}: max({}, {}) = {}'.format( a, b, first, second, out)) else: out = roll_dice(a, b, silent=silent) return out def roll_disadvantage(a, b, *, silent=False): if a == 1 and b == 20: first = roll_dice(a, b, silent=True) second = roll_dice(a, b, silent=True) out = min(first, second) if not silent: output.append('{}d{}: min({}, {}) = {}'.format( a, b, first, second, out)) else: out = roll_dice(a, b, silent=silent) return out operations = equations.operations.copy() operations.append({'>': max, '<': min}) dice = {} if adv == 0: dice['d'] = roll_dice elif adv > 0: dice['d'] = roll_advantage else: dice['d'] = roll_disadvantage dice['D'] = dice['d'] dice['g'] = great_weapon_fighting dice['G'] = dice['g'] operations.append(dice) unary = equations.unary.copy() unary['!'] = lambda a: a // 2 - 5 output.append('`{}`'.format(expression)) if character: # replace rolls rolls = session.query(m.Roll)\ .filter_by(character=character)\ .order_by(func.char_length(m.Roll.name).desc()) rep = {roll.name: '({})'.format(roll.expression) for roll in rolls} expr = re.compile('|'.join( map(re.escape, sorted(rep.keys(), key=len, reverse=True)))) for _ in range(3): expression = expr.sub(lambda m: rep[m.group(0)], expression) temp = '`{}`'.format(expression) if temp != output[-1]: output.append(temp) else: break # replace variables variables = session.query(m.Variable)\ .filter_by(character=character)\ .order_by(func.char_length(m.Variable.name).desc()) rep = {var.name: '({})'.format(var.value) for var in variables} expr = re.compile('|'.join( map(re.escape, sorted(rep.keys(), key=len, reverse=True)))) expression = expr.sub(lambda m: rep[m.group(0)], expression) temp = '`{}`'.format(expression) if temp != output[-1]: output.append(temp) # validate for token in re.findall(r'[a-zA-Z]+', expression): if token not in chain(*operations) and token not in unary: search = r'[a-zA-Z]*({})[a-zA-Z]*'.format(re.escape(token)) search = re.search(search, original_expression) if search: token = search.group(1) raise equations.EquationError('\n{}\nCould not find: `{}`'.format( '\n'.join(output), token)) # do roll roll = equations.solve(expression, operations=operations, unary=unary) if roll % 1 == 0: roll = int(roll) if character: output.append('{} rolled {}'.format(str(character), roll)) else: output.append('You rolled {}'.format(roll)) return roll
def test_give_1_should_be_1(self): n = 1 res = eq.solve(n) self.assertEqual(res, 1)
def test_give_32327_should_be_656502(self): n = 32327 res = eq.solve(n) self.assertEqual(res, 656502)
def test_give_2415_should_be_922287(self): n = 2415 res = eq.solve(n) self.assertEqual(res, 922287)
def test_give_4_should_be_21(self): n = 4 res = eq.solve(n) self.assertEqual(res, 21)
def test_give_3_should_be_9(self): n = 3 res = eq.solve(n) self.assertEqual(res, 9)
def test_give_2_should_be_3(self): n = 2 res = eq.solve(n) self.assertEqual(res, 3)