def parallel_onecommitted(number_opinion, N, interaction_number, data_point, seed_list, pA): """TODO: Docstring for parallel_actual_simlation. :arg1: TODO :returns: TODO """ nA = int(N * pA) xB = int(N - nA) state_list = all_state(number_opinion) state_single = state_list[0:number_opinion] initial_state = ['a'] * nA + ['B'] * xB des = f'../data/actual_simulation/onecommitted/number_opinion={number_opinion}/N={N}_interaction_number={interaction_number}_pA={pA}/' if not os.path.exists(des): os.makedirs(des) p = mp.Pool(cpu_number) p.starmap_async(actual_simulation, [(N, interaction_number, data_point, initial_state, state_single, seed, des) for seed in seed_list]).get() p.close() p.join() return None
def approximation_to_original(number_opinion, x): """TODO: Docstring for function. :number_opinion: TODO :x: TODO :returns: TODO """ y = np.zeros(2**number_opinion - 1 + number_opinion) state_list = all_state(number_opinion) state_list_approximation = all_state_approximation_three(number_opinion) state_convert_index = [] convert = [] for i in state_list: new = '' for j in i: if j == 'A' or j == 'B' or j == 'a' or j == 'b': new += j elif j.islower(): new += 'c' else: new += 'C' convert.append(new) if new == 'b': state_convert_index.append(-1) else: state_convert_index.append(state_list_approximation.index(new)) for i in range(np.size(y)): if state_convert_index[i] == -1: y[i] = 0 else: y[i] = x[state_convert_index[i]] / np.sum( state_convert_index[i] == np.array(state_convert_index)) return y
def parallel_actual_simulation(number_opinion, N, interaction_number, data_point, seed_list, pA, p): """TODO: Docstring for parallel_actual_simlation. :arg1: TODO :returns: TODO """ nA = int(N * pA) nC = int(N * p) nB = int(N - nA - nC * (number_opinion - 2)) state_list = all_state(number_opinion) state_single = state_list[0:number_opinion] state_committed = state_list[number_opinion:2 * number_opinion] state_Atilde = state_committed[2:] initial_state = ['a'] * nA + ['B'] * nB + functools.reduce( operator.iconcat, [[i] * nC for i in state_Atilde], []) """ nA = int(N * pA) nB = int(N * p) xB = int(N - nA - nB) xA = int(N/2) xB = int(N/2) state_list = all_state(number_opinion) state_single = state_list[0: number_opinion] #initial_state = ['a'] * nA + ['b'] * nB + ['B'] * xB initial_state = ['a'] * nA + ['b'] * nB + ['A'] * xA + ['B'] * xB """ des = f'../data/actual_simulation/number_opinion={number_opinion}/N={N}_interaction_number={interaction_number}_pA={pA}_p={p}/' if not os.path.exists(des): os.makedirs(des) p = mp.Pool(cpu_number) p.starmap_async(actual_simulation, [(N, interaction_number, data_point, initial_state, state_single, seed, des) for seed in seed_list]).get() p.close() p.join() return None
def parallel_actual_simulation_multi_opinion_switch(number_opinion, N, interaction_number, data_point, seed_list, pA, p0, switch_direction, approx_integer): """TODO: Docstring for parallel_actual_simlation. :arg1: TODO :returns: TODO """ """ committed_fraction = np.hstack(( np.array([pA, 0]), np.ones(number_opinion - 2) * p0 )) xA_dominate = np.hstack(( np.array([1-sum(committed_fraction)]), np.zeros(number_opinion - 1) )) xB_dominate = np.hstack(( np.array([0, 1-sum(committed_fraction)]), np.zeros(number_opinion - 2) )) xC_dominate = np.hstack(( np.array([0, 0, 1-sum(committed_fraction)]), np.zeros(number_opinion - 3) )) result_A_dominate = mft_evolution(number_opinion, committed_fraction, xA_dominate)[-1] result_B_dominate = mft_evolution(number_opinion, committed_fraction, xB_dominate)[-1] result_C_dominate = mft_evolution(number_opinion, committed_fraction, xC_dominate)[-1] """ committed_fraction = np.array([pA, p0 * (number_opinion - 2)]) xA_dominate = np.array([1 - sum(committed_fraction), 0, 0]) xB_dominate = np.array([0, 1 - sum(committed_fraction), 0]) xC_dominate = np.array([0, 0, 1 - sum(committed_fraction)]) result_A_dominate = mft_evolution_approximation(number_opinion, committed_fraction, xA_dominate)[-1] result_B_dominate = mft_evolution_approximation(number_opinion, committed_fraction, xB_dominate)[-1] result_C_dominate = mft_evolution_approximation(number_opinion, committed_fraction, xC_dominate)[-1] if np.sum(np.abs(result_A_dominate - result_B_dominate)) < 1e-5: result_B_dominate = np.hstack( (np.array([0, 1 - sum(committed_fraction)]), np.zeros(len(result_B_dominate) - 2))) if switch_direction == 'A-B': switch_threshold = result_B_dominate[1] x_simulation = result_A_dominate elif switch_direction == 'B-A': switch_threshold = result_A_dominate[0] x_simulation = result_B_dominate elif switch_direction == 'B-C': switch_threshold = result_C_dominate[2] x_simulation = result_B_dominate x_simulation = approximation_to_original(number_opinion, x_simulation) n_all_opinions = np.round(x_simulation * N, 10) if approx_integer == 'round': n_all_integer = np.array(np.round(n_all_opinions), dtype=int) elif approx_integer == 'floor': n_all_integer = np.array(n_all_opinions, dtype=int) elif approx_integer == 'ceil': n_all_integer = np.array(np.ceil(n_all_opinions), dtype=int) if sum(n_all_integer[:-1]) <= N: n_all_integer[-1] = N - sum(n_all_integer[:-1]) else: index = np.where(np.cumsum(n_all_integer) > N)[0] n_all_integer[index] = N - sum(n_all_integer[:index - 1]) n_all_integer[index + 1:] = 0 state_list = all_state(number_opinion) initial_state = [] for i, j in zip(state_list, n_all_integer): initial_state += [i] * j state_single = state_list[0:number_opinion] des = f'../data/actual_simulation/number_opinion={number_opinion}/approx_integer=' + approx_integer + f'/N={N}_pA={pA}_p0={p0}_switch_direction=' + switch_direction + '/' if not os.path.exists(des): os.makedirs(des) p = mp.Pool(cpu_number) p.starmap_async( simulation_onetime, [(N, interaction_number, data_point, initial_state, state_single, seed, des, switch_direction, switch_threshold) for seed in seed_list]).get() p.close() p.join() return None
def parallel_actual_simulation_two_opinion_switch(number_opinion, N, interaction_number, data_point, seed_list, pA, pB, switch_direction, approx_integer): """TODO: Docstring for parallel_actual_simlation. :arg1: TODO :returns: TODO """ committed_fraction = np.array([pA, pB]) xA_dominate = np.array([1 - sum(committed_fraction), 0]) xB_dominate = np.array([0, 1 - sum(committed_fraction)]) result_A_dominate = mft_evolution(number_opinion, committed_fraction, xA_dominate) result_B_dominate = mft_evolution(number_opinion, committed_fraction, xB_dominate) xA_A_dominate, xB_A_dominate = result_A_dominate[-1, :2] xA_B_dominate, xB_B_dominate = result_B_dominate[-1, :2] if abs(xA_A_dominate - xA_B_dominate) < 1e-5: xA_B_dominate = 0 xB_B_dominate = 1 - sum(committed_fraction) if switch_direction == 'A-B': switch_threshold = xB_B_dominate xA_simulation = xA_A_dominate xB_simulation = xB_A_dominate elif switch_direction == 'B-A': switch_threshold = xA_A_dominate xA_simulation = xA_B_dominate xB_simulation = xB_B_dominate nA_com = round(N * pA, 10) nB_com = round(N * pB, 10) nA_uncom = round(N * xA_simulation, 10) nB_uncom = round(N * xB_simulation, 10) if approx_integer == 'round': nA_com = int(round(nA_com)) nB_com = int(round(nB_com)) nA_uncom = int(round(nA_uncom)) nB_uncom = int(round(nB_uncom)) elif approx_integer == 'floor': nA_com = int(nA_com) nB_com = int(nB_com) nA_uncom = int(nA_uncom) nB_uncom = int(nB_uncom) elif approx_integer == 'ceil': nA_com = int(np.ceil(nA_com)) nB_com = int(np.ceil(nB_com)) nA_uncom = int(np.ceil(nA_uncom)) nB_uncom = int(np.ceil(nB_uncom)) nAB_uncom = N - nA_com - nB_com - nA_uncom - nB_uncom state_list = all_state(number_opinion) state_single = state_list[0:number_opinion] initial_state = ['a'] * nA_com + ['b'] * nB_com + ['A'] * nA_uncom + [ 'B' ] * nB_uncom + ['AB'] * nAB_uncom des = f'../data/actual_simulation/number_opinion={number_opinion}/approx_integer=' + approx_integer + f'/N={N}_pA={pA}_pB={pB}_switch_direction=' + switch_direction + '/' if not os.path.exists(des): os.makedirs(des) p = mp.Pool(cpu_number) p.starmap_async( simulation_onetime, [(N, interaction_number, data_point, initial_state, state_single, seed, des, switch_direction, switch_threshold) for seed in seed_list]).get() p.close() p.join() return None