Exemplo n.º 1
0
    def test_vertex_cover_basic(self):

        G = dnx.chimera_graph(1, 2, 2)
        cover = dnx.min_vertex_cover(G, ExactSolver())
        self.vertex_cover_check(G, cover)

        G = nx.path_graph(5)
        cover = dnx.min_vertex_cover(G, ExactSolver())
        self.vertex_cover_check(G, cover)

        for __ in range(10):
            G = nx.gnp_random_graph(5, .5)
            cover = dnx.min_vertex_cover(G, ExactSolver())
            self.vertex_cover_check(G, cover)
Exemplo n.º 2
0
    def test_vertex_cover_basic(self):
        """Runs the function on some small and simple graphs, just to make
        sure it works in basic functionality.
        """
        G = dnx.chimera_graph(1, 2, 2)
        cover = dnx.min_vertex_cover(G, ExactSolver())
        self.vertex_cover_check(G, cover)

        G = nx.path_graph(5)
        cover = dnx.min_vertex_cover(G, ExactSolver())
        self.vertex_cover_check(G, cover)

        for __ in range(10):
            G = nx.gnp_random_graph(5, .5)
            cover = dnx.min_vertex_cover(G, ExactSolver())
            self.vertex_cover_check(G, cover)
Exemplo n.º 3
0
    def test_default_sampler(self):
        G = nx.complete_graph(5)

        dnx.set_default_sampler(ExactSolver())
        self.assertIsNot(dnx.get_default_sampler(), None)
        cover = dnx.min_vertex_cover(G)
        dnx.unset_default_sampler()
        self.assertEqual(dnx.get_default_sampler(), None,
                         "sampler did not unset correctly")
Exemplo n.º 4
0
from dwave.system.samplers import DWaveSampler
from dwave.system.composites import EmbeddingComposite

sampler = EmbeddingComposite(DWaveSampler())

# Create empty graph
G = nx.Graph()

# Add edges to graph - this also adds the nodes
G.add_edges_from([(1, 2), (1, 3), (2, 3), (3, 4), (3, 5), (4, 5), (4, 6),
                  (5, 6), (6, 7)])

# Find the minimum vertex cover, S
S = dnx.min_vertex_cover(G,
                         sampler=sampler,
                         lagrange=5,
                         num_reads=10,
                         label='Example - Pipelines')

# Print the solution for the user
print('Minimum vertex cover found is', len(S))
print(S)

# Visualize the results
k = G.subgraph(S)
notS = list(set(G.nodes()) - set(S))
othersubgraph = G.subgraph(notS)
pos = nx.spring_layout(G)
plt.figure()

# Save original problem graph
Exemplo n.º 5
0
import matplotlib.pyplot as plt

a5 = nx.star_graph(4)
plt.subplot(121)

nx.draw(a5, with_labels=True, front_weight='bold')

# In[4]:

#Solve the graph minimum vertex cover on the CPU

from dimod.reference.samplers import ExactSolver
import dwave_networkx as dnx

sampler = ExactSolver()
print(dnx.min_vertex_cover(a5, sampler))

# In[6]:

#step 3 solve the graphs minimum vertex cover on the QPU

from dwave.system.samplers import DWaveSampler
from dwave.system.composites import EmbeddingComposite

sampler = EmbeddingComposite(DWaveSampler())
print(dnx.min_vertex_cover(a5, sampler))

# In[7]:

#Step 4 try with bigger graph
Exemplo n.º 6
0
    def test_dimod_vs_list(self):
        G = nx.path_graph(5)

        cover = dnx.min_vertex_cover(G, ExactSolver())
        cover = dnx.min_vertex_cover(G, SimulatedAnnealingSampler())
Exemplo n.º 7
0
import numpy as np
import networkx as nx
import dwave_networkx as dnx
import matplotlib.pyplot as plt
from dwave.system.samplers import DWaveSampler
from dwave.system.composites import EmbeddingComposite

mygraph = nx.dense_gnm_random_graph(20, 40)

pos = nx.circular_layout(mygraph)

sampler = EmbeddingComposite(DWaveSampler())
result = dnx.min_vertex_cover(mygraph, sampler)

print(result)

nx.draw_networkx_nodes(mygraph, pos, with_labels=True)
nx.draw_networkx_nodes(mygraph,
                       pos,
                       with_labels=True,
                       node_color='r',
                       nodelist=result)
nx.draw_networkx_edges(mygraph, pos)

plt.show()
Exemplo n.º 8
0
# Set the solver we're going to use
from dwave.system.samplers import DWaveSampler
from dwave.system.composites import EmbeddingComposite

sampler = EmbeddingComposite(DWaveSampler())

# Create empty graph
G = nx.Graph()

# Add edges to graph - this also adds the nodes
G.add_edges_from([(1, 2), (1, 3), (2, 3), (3, 4), (3, 5), (4, 5), (4, 6),
                  (5, 6), (6, 7)])

# Find the maximum independent set, S
S = dnx.min_vertex_cover(G, sampler=sampler, num_reads=10)

# Print the solution for the user
print('Minimum vertex cover found is', len(S))
print(S)

# Visualize the results
k = G.subgraph(S)
notS = list(set(G.nodes()) - set(S))
othersubgraph = G.subgraph(notS)
pos = nx.spring_layout(G)
plt.figure()

# Save original problem graph
original_name = "pipelines_plot_original.png"
nx.draw_networkx(G, pos=pos, with_labels=True)
Exemplo n.º 9
0
import networkx as nx
s5 = nx.star_graph(4)

from dimod.reference.samplers import ExactSolver
sampler = ExactSolver()

import dwave_networkx as dnx 
print(dnx.min_vertex_cover(s5, sampler))
Exemplo n.º 10
0
import dwave_networkx as dnx
import networkx as nx

import dimod # シュミレーション
# from dwave.system.samplers import DWaveSampler # 実機
# from dwave.system.composites import EmbeddingComposite # 実機

sampler = dimod.SimulatedAnnealingSampler() # シュミレーション
# sampler = EmbeddingComposite(DWaveSampler()) # 実機


G = nx.Graph()
G.add_edges_from([(0,1),(0,2),(1,3),(2,3),(3,4)])

# 頂点被覆問題をとくためのライブラリ(min_vertex_cover)
candidate = dnx.min_vertex_cover(G, sampler)
print(candidate)
Exemplo n.º 11
0
# https://docs.ocean.dwavesys.com/en/stable/examples/min_vertex.html
import networkx as nx
import dwave_networkx as dnx
from neal import  SimulatedAnnealingSampler
s4 = nx.star_graph(4)
print(dnx.min_vertex_cover(nx.wheel_graph(5), SimulatedAnnealingSampler()))

Exemplo n.º 12
0
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# https://docs.ocean.dwavesys.com/en/latest/examples/min_vertex.html

import networkx as nx
s5 = nx.star_graph(4) # create a star graph where node 0 is hub to four other nodes.

# Solving Classically on a CPU

from dimod.reference.samplers import ExactSolver
sampler = ExactSolver() # returns the BQM's value for every possible assignment of variable values

import dwave_networkx as dnx
print(dnx.min_vertex_cover(s5, sampler)) # produce a BQM for our s5 graph and solve it on our selected sampler

print('################################################################################')

# Solving on a D-Wave System

from dwave.system.samplers import DWaveSampler
from dwave.system.composites import EmbeddingComposite # EmbeddingComposite(), maps unstructured problems to the graph structure of the selected sampler, a process known as minor-embedding
sampler = EmbeddingComposite(DWaveSampler()) # endpoint='https://URL_to_my_D-Wave_system/', token='ABC-123456789012345678901234567890', solver='My_D-Wave_Solver'
print(dnx.min_vertex_cover(s5, sampler))

print('################################################################################')

w5 = nx.wheel_graph(5) # creates a new graph
print(dnx.min_vertex_cover(w5, sampler)) # solves on a D-Wave system
print(dnx.min_vertex_cover(w5, sampler))
Exemplo n.º 13
0
import networkx as nx
import matplotlib.pyplot as plt

# Build the graph using networkx
M = 6
N = 6
G = nx.grid_2d_graph(M, N)

# Store an image of the graph
pos = {(x, y): (y, -x) for x, y in G.nodes()}
nx.draw(G, pos)
plt.savefig('plot.png', bbox_inches='tight')

# Define our sampler
from dwave.system import LeapHybridSampler
sampler = LeapHybridSampler()

# Solve our problem
import dwave_networkx as dnx
answer = dnx.min_vertex_cover(G, sampler)
print("Found cover of size", len(answer))
print("\nCover:\n", answer)

# Visualize solution
plt.clf()
pos = {(x, y): (y, -x) for x, y in G.nodes()}
nx.draw_networkx_nodes(G, pos, nodelist=answer, node_color='r')
nx.draw_networkx_nodes(G, pos, nodelist=G.nodes() - answer, node_color='c')
nx.draw_networkx_edges(G, pos, edgelist=G.edges(), style='solid', width=3)

plt.savefig('answer_plot.png', bbox_inches='tight')
Exemplo n.º 14
0
def min_vertex_cover(graph, sampler, params):
    res = dnx.min_vertex_cover(graph, sampler)
    return res, 'node'
Exemplo n.º 15
0
# Set the solver we're going to use
from dwave.system.samplers import DWaveSampler
from dwave.system.composites import EmbeddingComposite

sampler = EmbeddingComposite(DWaveSampler(solver={'qpu': True}))

# Create empty graph
G = nx.Graph()

# Add edges to graph - this also adds the nodes
G.add_edges_from([(1, 2), (1, 3), (2, 3), (3, 4), (3, 5), (4, 5), (4, 6),
                  (5, 6), (6, 7)])

# Find the minimum vertex cover, S
S = dnx.min_vertex_cover(G, sampler=sampler, lagrange=5, num_reads=10)

# Print the solution for the user
print('Minimum vertex cover found is', len(S))
print(S)

# Visualize the results
k = G.subgraph(S)
notS = list(set(G.nodes()) - set(S))
othersubgraph = G.subgraph(notS)
pos = nx.spring_layout(G)
plt.figure()

# Save original problem graph
original_name = "pipelines_plot_original.png"
nx.draw_networkx(G, pos=pos, with_labels=True)