def generate_random_topology_BA(N_nodes, n):
    """Generate a Barabasi-Albert random graph with N_nodes nodes, and new link parameter n
	   Return the triplet (Graph, adjacancy matrix, Laplacian matrix)
    """
    G = nx.barabasi_albert_graph(N_nodes, n)
    adja = np.array(nx.to_numpy_matrix(G))
    L = control2.get_Laplacian(adja)
    return (G, adja, L)
def generate_random_topology_ER(N_nodes, p):
    """Generate a Erdos-Renyi random graph with N_nodes nodes, and link probability p
	   Return the triplet (Graph, adjacancy matrix, Laplacian matrix)
    """
    G = nx.erdos_renyi_graph(N_nodes, p)
    adja = np.array(nx.to_numpy_matrix(G))
    L = control2.get_Laplacian(adja)
    return (G, adja, L)
def main():
    N_nodes = 200
    p_out = .1
    monte_carlo = 100
    results = []

    for N_clusters in clusters_generator():
        results.append([])
	print N_clusters
        for p_in in np.linspace(0,1,20):
            AVG = []
            for mc in range( monte_carlo ):

                #Random Initial state
                theta0  = np.random.normal( loc = 0, scale = .01, size = (1,N_nodes) )[0] #Random small phase angles
                dtheta0 = np.random.normal( loc = 0, scale = .01, size = (1,N_nodes) )[0] #Random small frequencies
                Y0 = np.append( theta0, dtheta0 )
                Y0 = np.append( Y0, 1 )

                #Random Initial state
                thetaf  = np.random.normal( loc = 0, scale = .01, size = (1,N_nodes) )[0] #Random small phase angles
                dthetaf = np.random.normal( loc = 0, scale = .01, size = (1,N_nodes) )[0] #Random small frequencies
                Yf = np.append( thetaf, dthetaf )
                Yf = np.append( Yf, 1 )

                G = nx.planted_partition_graph(N_clusters, N_nodes/N_clusters, p_in, p_out)
                adja = np.array( nx.to_numpy_matrix( G ) )
                L = control2.get_Laplacian( adja )
                Psource       = generate_random_power_distribution( N_nodes )
                Pmax          = generate_random_capacity_distribution( N_nodes, adja )
                K             =  Pmax  / float( I * Omega )
                A             = control2.build_transition_matrix( Psource, L, Omega, I, K, alpha, Dt )
                A_powers = [ np.identity( A.shape[0] ), A ]
                for k in range( control_time + 1 )[2:]:
                    A_powers.append( np.dot( A, A_powers[-1] ) )
                storage_level = 100 * np.ones((1,N_nodes))[0]

                drivers = control2.rank_submodular_greedy_lazy( A_powers, adja, range( N_nodes ), control_time, storage_level, max_capacity, r, I, Omega, Y0, Yf, True )
                AVG.append( len(drivers) )
            results[-1].append( AVG  )
			
    with open('./results3.json', 'w') as f:
        json.dump(results,f)
def main():

    N_nodes = 200
    p_out = 0.05
    monte_carlo = 100
    results = []

    p_values = [0.1, 0.3, 0.5, 0.7, 0.9]
    c_values = [2, 4, 5, 8, 10, 20, 25, 40, 50]
    val = 0

    with progress.Bar(label="Progress: ", expected_size=len(p_values) * len(c_values) * monte_carlo) as bar:
        for p_in in [0.1, 0.3, 0.5, 0.7, 0.9]:
            results.append([])

            for N_clusters in [2, 4, 5, 8, 10, 20, 25, 40, 50]:
                AVG = []

                for mc in range(monte_carlo):

                    # Random Initial state
                    theta0 = np.random.normal(loc=0, scale=0.01, size=(1, N_nodes))[0]  # Random small phase angles
                    dtheta0 = np.random.normal(loc=0, scale=0.01, size=(1, N_nodes))[0]  # Random small frequencies
                    Y0 = np.append(theta0, dtheta0)
                    Y0 = np.append(Y0, 1)

                    # Random Initial state
                    thetaf = np.random.normal(loc=0, scale=0.01, size=(1, N_nodes))[0]  # Random small phase angles
                    dthetaf = np.random.normal(loc=0, scale=0.01, size=(1, N_nodes))[0]  # Random small frequencies
                    Yf = np.append(thetaf, dthetaf)
                    Yf = np.append(Yf, 1)

                    G = nx.planted_partition_graph(N_clusters, N_nodes / N_clusters, p_in, p_out)
                    adja = np.array(nx.to_numpy_matrix(G))
                    L = control2.get_Laplacian(adja)
                    Psource = generate_random_power_distribution(N_nodes)
                    Pmax = generate_random_capacity_distribution(N_nodes, adja)
                    K = Pmax / float(I * Omega)
                    A = control2.build_transition_matrix(Psource, L, Omega, I, K, alpha, Dt)
                    A_powers = [np.identity(A.shape[0]), A]
                    for k in range(control_time + 1)[2:]:
                        A_powers.append(np.dot(A, A_powers[-1]))
                    storage_level = 100 * np.ones((1, N_nodes))[0]

                    drivers = control2.rank_submodular_greedy_lazy(
                        A_powers,
                        adja,
                        range(N_nodes),
                        control_time,
                        storage_level,
                        max_capacity,
                        r,
                        I,
                        Omega,
                        Y0,
                        Yf,
                        True,
                    )
                    AVG.append(len(drivers))
                    val += 1
                    bar.show(val)

                results[-1].append(np.mean(AVG))

    with open("./results.json", "w") as f:
        json.dump(results, f)