示例#1
0
def binary_clustering(feature_vecs, feature_index, **kwargs):
    h = {}
    J = {}
    cluster1 = []  #stores the indices for the first cluster
    cluster2 = []  #stores the indices for the second cluster
    #will recognize tabu or hybrid
    if 'sampler' in kwargs:
        sampler_type = kwargs['sampler']
    else:
        sampler_type = "tabu"
    if 'time_limit' in kwargs:
        time_limit = kwargs['time_limit']
    else:
        time_limit = 20
    sampler = TabuSampler()
    sampler1 = LeapHybridSampler()

    for i in feature_index:
        for j in feature_index:
            if i < j:
                J[(i, j)] = np.linalg.norm(feature_vecs[i] - feature_vecs[j])

    #Now use a sampler to solve it
    if sampler_type == "tabu":
        print("Choosing TABU sampler")
        sampler = TabuSampler()
        sampleset = sampler.sample_ising(h,
                                         J,
                                         num_reads=1,
                                         timeout=time_limit * 1000)
        bin_cluster = sampleset.first[0]
    else:

        print("Choosing the hybrid sampler")
        sampler1 = LeapHybridSampler()
        sampleset = sampler1.sample_ising(h, J, time_limit=time_limit)
        bin_cluster = sampleset.first[0]
    # Run the problem on the sampler and print the results

    for key in bin_cluster:
        #put in cluster 1 if -1, else 2
        if bin_cluster[key] == -1:
            cluster1.append(key)
        elif bin_cluster[key] == 1:
            cluster2.append(key)

    return cluster1, cluster2
示例#2
0
#make sure D-wave API and config works
from dwave.system import EmbeddingComposite, DWaveSampler
from tabu import TabuSampler
# Define the problem as two Python dictionaries:
#   h for linear terms, J for quadratic terms
h = {}
J = {(0, 1): 1, (1, 2): 2, (0, 2): 2.5}

# Define the sampler that will be used to run the problem
#sampler = EmbeddingComposite(DWaveSampler())
sampler = TabuSampler()
# Run the problem on the sampler and print the results
sampleset = sampler.sample_ising(h, J, num_reads=10)
print(sampleset)

tree.create_node("Harry", "harry")  # root node
tree.create_node("Jane", "jane", parent="harry")
tree.create_node("Bill", "bill", parent="harry")
tree.create_node("Diane", "diane", parent="jane")
tree.create_node("Mary", "mary", parent="diane")
tree.create_node("Mark", "mark", parent="jane")
tree.show()