Example #1
0
 def setup_class(cls):
     cnlti = nx.convert_node_labels_to_integers
     Gi = cnlti(grid_2d_graph(5, 5), first_label=1, ordering="sorted")
     cls.Gi = nx.Graph(Gi)
     cls.Gs = nx.Graph()
     nx.add_path(cls.Gs, "abcdef")
     bigG = cnlti(grid_2d_graph(25, 25), first_label=1, ordering="sorted")
     cls.bigG = nx.Graph(bigG)
Example #2
0
    def setup(self):
        """Creates some graphs for use in the unit tests."""
        # NB: graphscope.nx does not support grid_2d_graph(which use tuple as node)
        # we use a tricky way to replace it.
        cnlti = nx.convert_node_labels_to_integers
        grid = cnlti(grid_2d_graph(4, 4), first_label=1, ordering="sorted")
        self.grid = nx.Graph(grid)
        self.cycle = nx.cycle_graph(7)
        self.directed_cycle = nx.cycle_graph(7, create_using=nx.DiGraph())
        self.XG = nx.DiGraph()
        self.XG.add_weighted_edges_from([('s', 'u', 10), ('s', 'x', 5), ('u', 'v', 1),
                                         ('u', 'x', 2), ('v', 'y', 1), ('x', 'u', 3),
                                         ('x', 'v', 5), ('x', 'y', 2), ('y', 's', 7),
                                         ('y', 'v', 6)])
        self.XG2 = nx.DiGraph()
        self.XG2.add_weighted_edges_from([[1, 4, 1], [4, 5, 1], [5, 6, 1], [6, 3, 1],
                                          [1, 3, 50], [1, 2, 100], [2, 3, 100]])

        self.XG3 = nx.Graph()
        self.XG3.add_weighted_edges_from([[0, 1, 2], [1, 2, 12], [2, 3, 1], [3, 4, 5],
                                          [4, 5, 1], [5, 0, 10]])

        self.XG4 = nx.Graph()
        self.XG4.add_weighted_edges_from([[0, 1, 2], [1, 2, 2], [2, 3, 1], [3, 4, 1],
                                          [4, 5, 1], [5, 6, 1], [6, 7, 1], [7, 0, 1]])
        self.G = nx.DiGraph()  # no weights
        self.G.add_edges_from([('s', 'u'), ('s', 'x'), ('u', 'v'), ('u', 'x'),
                               ('v', 'y'), ('x', 'u'), ('x', 'v'), ('x', 'y'),
                               ('y', 's'), ('y', 'v')])
Example #3
0
    def setup_class(cls):
        from networkx import convert_node_labels_to_integers as cnlti

        # NB: graphscope.nx does not support grid_2d_graph(which use tuple as node)
        # we use a tricky way to replace it.
        grid = cnlti(grid_2d_graph(4, 4), first_label=1, ordering="sorted")
        cls.grid = nx.Graph(grid)
        cls.cycle = nx.cycle_graph(7)
        cls.directed_cycle = nx.cycle_graph(7, create_using=nx.DiGraph())
Example #4
0
 def do_initgrid(self, line: str):
     """
     Initialize grid with a particular dimension ie, 5, 8
     :param line:
     :return:
     """
     if ',' not in line:
         print('Please use format: x, y')
         return
     x, y = line.replace(' ', '').split(',')
     x, y = int(x), int(y)
     self.map_service = LocationMapService(
         InMemLocationMapRepository(),
         NetworkXGateway(lattice.grid_2d_graph(x, y)))
Example #5
0
from networkx.generators import lattice

from domain.models import Person, PersonStatus, Location
from domain.services import PeopleService, VehicleService, LocationMapService
from infrastructure.gateways import NetworkXGateway
from infrastructure.persistence import InMemPeopleRepository, InMemVehicleRepository, InMemLocationMapRepository
from usecases.process_time_steps import time_step_single_vehicle


people_service = PeopleService(InMemPeopleRepository())
vehicle_service = VehicleService(InMemVehicleRepository())
map_service = LocationMapService(InMemLocationMapRepository(), NetworkXGateway(lattice.grid_2d_graph(10, 10)))


def gen_steps():
    time_steps = [
        [
            {"name": "Elon", "start": [2, 2], "end": [8, 7]},
            {"name": "George", "start": [1, 2], "end": [4, 3]}
        ],
        [],
        [
            {"name": "Nancy", "start": [9, 9], "end": [1, 3]}
        ],
    ]
    for step in time_steps:
        yield step
    while True:
        yield []

 def setup_method(self):
     # NB: graphscope.nx does not support grid_2d_graph(which use tuple as node)
     # we use a tricky way to replace it.
     H = cnlti(grid_2d_graph(4, 4), first_label=1, ordering="sorted")
     G = nx.Graph(H)
     self.G = G
Example #7
0
import os
import pickle

from parametrization import Parametrization
from networkx.generators.lattice import grid_2d_graph

import config
from infrastructure.gateways import GoogleDirectionsGateway, NetworkXGateway
from interfaces.serializers import json_serialize

gdg = GoogleDirectionsGateway(config.GOOGLE_API_KEY)
ng = NetworkXGateway(grid_2d_graph(10, 10))

origin = gdg.get_address_location('5502 Broken Sound Blvd NW Boca Raton, FL')
destinations = [
    gdg.get_address_location(d) for d in [
        '7841 NW 170th St. Hialeah, FL 33015',
        '1182 NW 162nd Ave. Pembroke Pines, FL 33028',
        '9742 Rennes Lane Delray Beach, FL 33446',
    ]
]


@Parametrization.parameters('actual', 'expected')
@Parametrization.case(
    'test_itinerary_json_serialize',
    actual=json_serialize(gdg.get_next_destination(origin, destinations)),
    expected='interfaces/fixtures/directions_gateway_itinerary.pkl')
def test_custom_json_serializer(actual, expected):
    if os.getenv('GEN_GOLDEN_FILES'):
        with open(expected, 'wb') as fh:
Example #8
0
def test_networkx_gateway_next_destination():
    gateway = NetworkXGateway(lattice.grid_2d_graph(10, 10))
    origin = Location(0, 0)
    destinations = [Location(1, 1), Location(2, 5), Location(2, 4)]
    next_destination = gateway.get_next_destination(origin, destinations)
    assert next_destination == Location(1, 1)
Example #9
0
    def setup_class(cls):
        # NB: graphscope.nx does not support tuple graph, we construct from
        # networkx and then convert to nx.Graph
        H1 = cnlti(grid_2d_graph(2, 2), first_label=0, ordering="sorted")
        G1 = nx.Graph(H1)
        G2 = cnlti(nx.lollipop_graph(3, 3), first_label=4, ordering="sorted")
        G3 = cnlti(nx.house_graph(), first_label=10, ordering="sorted")
        cls.G = nx.union(G1, G2)
        cls.G = nx.union(cls.G, G3)
        cls.DG = nx.DiGraph([(1, 2), (1, 3), (2, 3)])
        grid = cnlti(grid_2d_graph(4, 4), first_label=1)
        cls.grid = nx.Graph(grid)

        cls.gc = []
        G = nx.DiGraph()
        G.add_edges_from([
            (1, 2),
            (2, 3),
            (2, 8),
            (3, 4),
            (3, 7),
            (4, 5),
            (5, 3),
            (5, 6),
            (7, 4),
            (7, 6),
            (8, 1),
            (8, 7),
        ])
        C = [[3, 4, 5, 7], [1, 2, 8], [6]]
        cls.gc.append((G, C))

        G = nx.DiGraph()
        G.add_edges_from([(1, 2), (1, 3), (1, 4), (4, 2), (3, 4), (2, 3)])
        C = [[2, 3, 4], [1]]
        cls.gc.append((G, C))

        G = nx.DiGraph()
        G.add_edges_from([(1, 2), (2, 3), (3, 2), (2, 1)])
        C = [[1, 2, 3]]
        cls.gc.append((G, C))

        # Eppstein's tests
        G = nx.DiGraph({
            0: [1],
            1: [2, 3],
            2: [4, 5],
            3: [4, 5],
            4: [6],
            5: [],
            6: []
        })
        C = [[0], [1], [2], [3], [4], [5], [6]]
        cls.gc.append((G, C))

        G = nx.DiGraph({0: [1], 1: [2, 3, 4], 2: [0, 3], 3: [4], 4: [3]})
        C = [[0, 1, 2], [3, 4]]
        cls.gc.append((G, C))

        G = nx.DiGraph()
        C = []
        cls.gc.append((G, C))
Example #10
0
from networkx.linalg.graphmatrix import adjacency_matrix

#
# Let us define Hamiltonian of an electronic tight-binding model
# on a square lattice.
#

# Number of lattice sites in each direction
# (the total number of sites is N*N).
N = 10

# Electron hopping constant - energy parameter of the TB model.
t = 2.0

# Use NetworkX to construct the periodic square lattice (graph)
lat = grid_2d_graph(N, N, periodic=True)

# Create lists of indices for electronic spin-up and spin-down operators.
# lat.nodes() returns a list of N^2 pairs of site indices (x, y).
indices_up = [(x, y, "up") for x, y in lat.nodes()]
indices_dn = [(x, y, "down") for x, y in lat.nodes()]

# A sum of tight-binding Hamiltonians for both spins.
# The hopping matrix passed to tight_binding() is proportional to the
# adjacency matrix of the lattice.
hopping_matrix = -t * adjacency_matrix(lat).todense()
H_e = tight_binding(hopping_matrix, indices=indices_up) \
    + tight_binding(hopping_matrix, indices=indices_dn)

#
# Hamiltonian of phonons localized at lattice sites.
Example #11
0
#

# Number of lattice sites in each direction
# (the total number of sites is Nx * Ny).
Nx = 4
Ny = 4

# Hopping constant
t = 0.5
# Chemical potential
mu = 1.0
# Coulomb repulsion
U = 2.0

# Use NetworkX to construct the periodic square lattice (graph)
lat = grid_2d_graph(Nx, Ny, periodic=True)

# Create lists of indices for spin-up and spin-down operators.
# lat.nodes() returns a list of Nx * Ny pairs of site indices (x, y).
indices_up = [(x, y, "up") for x, y in lat.nodes()]
indices_dn = [(x, y, "down") for x, y in lat.nodes()]

# A sum of tight-binding Hamiltonians for both spins.
# The hopping matrix passed to tight_binding() is proportional to the
# adjacency matrix of the lattice.
hopping_matrix = -t * adjacency_matrix(lat).todense()
H = tight_binding(hopping_matrix, indices=indices_up) \
    + tight_binding(hopping_matrix, indices=indices_dn)

# Add the chemical potential terms
H += dispersion(-mu * np.ones(len(indices_up)), indices=indices_up)