Exemple #1
0
    def initialize(self, guidance: 'Guidance', halt_event: Event = None):
        super().initialize(guidance, halt_event)

        # create agent
        self.agent = Agent(in_neighbors=self.guidance.in_neighbors,
                           out_neighbors=self.guidance.out_neighbors,
                           communicator=self.guidance.communicator)
Exemple #2
0
constr = []
for idx in np.arange(F.shape[1]):
    constr.append(F[:, idx][:, None] @ z >= 0)

for j in range(sum(nsamp)):
    constr.append(
        float(labels[j]) * (points[:, j].reshape(2, 1) @ (A @ z) + D @ z) >=
        1 - E @ z)  # j-th point

#####################
# Distributed algorithms
#####################

# local agent and problem
agent = Agent(in_neighbors=np.nonzero(Adj[agent_id, :])[0].tolist(),
              out_neighbors=np.nonzero(Adj[:, agent_id])[0].tolist())
pb = Problem(obj_func, constr)
agent.set_problem(pb)
# instantiate the algorithm
constrcons = ConstraintsConsensus(agent=agent, enable_log=True)

n_iter = NN * 3

# run the algorithm
constrcons_seq = constrcons.run(iterations=n_iter, verbose=True)

# print results
constrcons_x = constrcons.get_result()

print("Agent {}: {}".format(agent_id,
                            constrcons_x.flatten()))  # save information
Exemple #3
0
from disropt.functions import QuadraticForm, Variable
from disropt.utils.graph_constructor import MPIgraph
from disropt.problems import Problem

# generate communication graph (everyone uses the same seed)
comm_graph = MPIgraph('random_binomial', 'metropolis')
agent_id, in_nbrs, out_nbrs, in_weights, _ = comm_graph.get_local_info()

# size of optimization variable
n = 5

# generate quadratic cost function
np.random.seed()
Q = np.random.randn(n, n)
Q = Q.transpose() @ Q
x = Variable(n)
func = QuadraticForm(x - np.random.randn(n, 1), Q)

# create Problem and Agent
agent = Agent(in_nbrs, out_nbrs, in_weights=in_weights)
agent.set_problem(Problem(func))

# run the algorithm
x0 = np.random.rand(n, 1)
algorithm = GradientTracking(agent, x0)
algorithm.run(iterations=1000, stepsize=0.01)

print("Agent {} - solution estimate: {}".format(
    agent_id,
    algorithm.get_result().flatten()))
Exemple #4
0
from disropt.problems import Problem

# get MPI info
comm = MPI.COMM_WORLD
nproc = comm.Get_size()
local_rank = comm.Get_rank()

# Generate a common graph (everyone use the same seed)
Adj = binomial_random_graph(nproc, p=0.3, seed=1)
W = metropolis_hastings(Adj)

# reset local seed
np.random.seed(local_rank)

agent = Agent(in_neighbors=np.nonzero(Adj[local_rank, :])[0].tolist(),
              out_neighbors=np.nonzero(Adj[:, local_rank])[0].tolist(),
              in_weights=W[local_rank, :].tolist())

# variable dimension
n = 6

# generate a positive definite matrix
P = np.random.randn(n, n)
P = P.transpose() @ P
bias = np.random.randn(n, 1)
# declare a variable
x = Variable(n)

# define the local objective function
fn = QuadraticForm(x - bias, P)
Exemple #5
0
from disropt.agents import Agent
from disropt.algorithms.misc import MaxConsensus
from disropt.utils.graph_constructor import ring_graph

# get MPI info
comm = MPI.COMM_WORLD
nproc = comm.Get_size()
local_rank = comm.Get_rank()

# Generate a common graph (everyone use the same seed)
Adj = ring_graph(nproc)
graph_diam = nproc - 1
n_vars = 3

# create local agent
agent = Agent(in_neighbors=np.nonzero(Adj[local_rank, :])[0].tolist(),
              out_neighbors=np.nonzero(Adj[:, local_rank])[0].tolist())

# instantiate the max-consensus algorithm
np.random.seed(local_rank * 5)

x0 = np.random.rand(n_vars) * 10
algorithm = MaxConsensus(agent, x0, graph_diam, True)

print('Initial value of agent {}:\n {}'.format(local_rank, x0))

# execute algorithm
x_sequence = algorithm.run(iterations=100)

# get result
x_final = algorithm.get_result()
Exemple #6
0
edges_03 = pd.read_csv(r'../Network/network_03_new.csv',
                       delimiter=" ",
                       sep="\n",
                       header=None)
nodes = 20

comm = MPICommunicator()

for i in range(nodes):
    in_neighb = []
    out_neighb = []

    for j in range(edges_03.shape[0]):
        if edges_03.iloc[j, 1] == i:
            in_neighb.append(edges_03.iloc[j, 0])

    for j in range(edges_03.shape[0]):

        if edges_03.iloc[j, 0] == i:
            out_neighb.append(edges_03.iloc[j, 1])

    print(out_neighb)
    vect = np.random.rand(2, 1)

    globals()['agent-%s' % i] = Agent(in_neighb, out_neighb)

    globals()['exch_data_%s' % i] = comm.neighbors_exchange(vect,
                                                            in_neighb,
                                                            out_neighb,
                                                            dict_neigh=False)