Пример #1
0
def SmallWorld(n):
    # slow
    graph = SparseGraph(n)
    generator = SmallWorldGenerator(0.3, 50)
    graph = generator.generate(graph)
    l, _ = graph.adjacencyList()
    return convertAdjListToEdgeList(l)
Пример #2
0
def SmallWorld(n):
    # slow
    graph = SparseGraph(n)
    generator = SmallWorldGenerator(0.3, 50)
    graph = generator.generate(graph)
    l, _ = graph.adjacencyList()
    return convertAdjListToEdgeList(l)
Пример #3
0
    def setUp(self):
        numVertices = 500
        numFeatures = 49 
        
        self.means = rand.randn(numFeatures)
        self.vars = rand.randn(numFeatures, numFeatures)
        self.vars = self.vars + self.vars.T #Make vars symmetric
        p1 = 0.1
        
        self.egoGenerator = EgoGenerator()
        vList = self.egoGenerator.generateIndicatorVertices(numVertices, self.means, self.vars, p1)
        sGraph = SparseGraph(vList)
        
        p2 = 0.1 
        k = 5 
        
        #Create the graph edges according to the small world model 
        graphGen = SmallWorldGenerator(p2, k)
        self.sGraph = graphGen.generate(sGraph)

        dataDir = PathDefaults.getDataDir() + "infoDiffusion/"
        matFileName = dataDir + "EgoAlterTransmissions1000.mat"
        sampleSize = 100
        egoAlterExamplesList = ExamplesList.readFromMatFile(matFileName)
        egoAlterExamplesList.setDefaultExamplesName("X")
        egoAlterExamplesList.setLabelsName("y")
        egoAlterExamplesList.randomSubData(sampleSize)

        X = egoAlterExamplesList.getDataField("X")
        y = egoAlterExamplesList.getDataField("y")

        #Now learn using NaiveBayes
        self.nb = NaiveBayes()
        self.nb.learnModel(X, y)
    
        self.egoSimulator = EgoSimulator(self.sGraph, self.nb)

        #Define a classifier which predicts transfer if gender is female
        class DummyClassifier(object):
            def __init(self):
                pass

            def classify(self, X):
                y = numpy.zeros((X.shape[0]))

                for i in range(X.shape[0]):
                    if X[i, 0] == 0:
                        y[i] = 1
                    else:
                        y[i] = -1
                return y

        self.dc = DummyClassifier()
Пример #4
0
        loc_p = 1  # assume it is
        for c4 in np.arange(N):  # check for the neighbourhood
            new_comb = Comb_and_value[c3, :N].copy()
            new_comb[c4] = abs(new_comb[c4] - 1)
            if ((Comb_and_value[c3, 2*N] <
                 Comb_and_value[np.sum(new_comb*Power_key), 2*N])):
                loc_p = 0  # if smaller than the neighbour then not peak
        Comb_and_value[c3, 2*N+1] = loc_p
    return(Comb_and_value)



# Generate smallworld graph
graph = SparseGraph(VertexList(people, 1))
generator = SmallWorldGenerator(p, k)
graph = generator.generate(graph)
am = graph.adjacencyMatrix()
print(graph.degreeSequence())
# print(am)


# Generate NK Space
Power_key = powerkey(N)
NK = np.zeros((landscapes, 2**N, N*2+2))
start = time()
for i_1 in np.arange(landscapes):
    NK_land = nkland(N)
    Int_matrix = matrix_rand(N, K)
    NK[i_1] = comb_and_values(N, NK_land, Power_key, Int_matrix)
NKend = time()
NKb = copy.copy(NK) #copy to preserve the original values
Пример #5
0
class SmallWorldGeneratorTest(unittest.TestCase):
    def setUp(self):
        self.numVertices = 100
        self.numFeatures = 2

        p = 0.1
        k = 10

        self.vList = VertexList(self.numVertices, self.numFeatures)
        self.graph = SparseGraph(self.vList)
        self.swg = SmallWorldGenerator(p, k)

    def testgenerate(self):
        p = 0.0
        k = 1

        self.swg.setP(p)
        self.swg.setK(k)
        sGraph = self.swg.generate(self.graph)
        self.assertEquals(sGraph.getNumEdges(), sGraph.getNumVertices())

        for i in range(self.numVertices):
            for j in range(k):
                self.assertEquals(
                    sGraph.getEdge(i, (i + j + 1) % self.numVertices), 1)

        k = 3
        sGraph.removeAllEdges()
        self.swg.setP(p)
        self.swg.setK(k)
        sGraph = self.swg.generate(self.graph)

        self.assertEquals(sGraph.getNumEdges(), sGraph.getNumVertices() * k)

        for i in range(self.numVertices):
            for j in range(k):
                self.assertEquals(
                    sGraph.getEdge(i, (i + j + 1) % self.numVertices), 1)

        p = 0.5
        k = 1
        sGraph.removeAllEdges()
        self.swg.setP(p)
        self.swg.setK(k)
        sGraph = self.swg.generate(self.graph)
        self.assertEquals(sGraph.getNumEdges(), sGraph.getNumVertices())

        p = 0.1
        k = 2
        sGraph.removeAllEdges()
        self.swg.setP(p)
        self.swg.setK(k)
        sGraph = self.swg.generate(self.graph)
        self.assertEquals(sGraph.getNumEdges(), sGraph.getNumVertices() * k)

    def tearDown(self):
        pass

    def testInit(self):
        pass

    def testGetClusteringCoefficient(self):
        p = 0.0
        k = 10

        self.swg.setP(p)
        self.swg.setK(k)

        cc = 3 * (k - 1) * (1 - p)**3 / (2 * (2 * k - 1))
        self.assertEquals(self.swg.clusteringCoefficient(), cc)

        p = 0.5
        self.swg.setP(p)
        cc = 3 * (k - 1) * (1 - p)**3 / (2 * (2 * k - 1))
        self.assertEquals(self.swg.clusteringCoefficient(), cc)

        k = 1
        self.swg.setK(k)
        self.assertEquals(self.swg.clusteringCoefficient(), 0)
class SmallWorldGeneratorTest(unittest.TestCase):
    def setUp(self):    
        self.numVertices = 100; 
        self.numFeatures = 2; 

        p = 0.1
        k = 10 

        self.vList = VertexList(self.numVertices, self.numFeatures)
        self.graph = SparseGraph(self.vList)
        self.swg = SmallWorldGenerator(p, k)
        
    def testgenerate(self):
        p = 0.0
        k = 1

        self.swg.setP(p)
        self.swg.setK(k)
        sGraph = self.swg.generate(self.graph)
        self.assertEquals(sGraph.getNumEdges(), sGraph.getNumVertices())
        
        for i in range(self.numVertices): 
            for j in range(k):
                self.assertEquals(sGraph.getEdge(i, (i+j+1)%self.numVertices), 1)
        
        k = 3
        sGraph.removeAllEdges()
        self.swg.setP(p)
        self.swg.setK(k)
        sGraph = self.swg.generate(self.graph)
        
        self.assertEquals(sGraph.getNumEdges(), sGraph.getNumVertices()*k)
        
        for i in range(self.numVertices): 
            for j in range(k):
                self.assertEquals(sGraph.getEdge(i, (i + j + 1) % self.numVertices), 1)
           
        p = 0.5 
        k = 1    
        sGraph.removeAllEdges()
        self.swg.setP(p)
        self.swg.setK(k)
        sGraph = self.swg.generate(self.graph)
        self.assertEquals(sGraph.getNumEdges(), sGraph.getNumVertices())
        
        p = 0.1 
        k = 2    
        sGraph.removeAllEdges()
        self.swg.setP(p)
        self.swg.setK(k)
        sGraph = self.swg.generate(self.graph)
        self.assertEquals(sGraph.getNumEdges(), sGraph.getNumVertices()*k)

    def tearDown(self):
        pass

    def testInit(self):
        pass     

    def testGetClusteringCoefficient(self):
        p = 0.0
        k = 10

        self.swg.setP(p)
        self.swg.setK(k)

        cc = 3*(k-1)*(1-p)**3/(2*(2*k-1))
        self.assertEquals(self.swg.clusteringCoefficient(), cc)

        p = 0.5
        self.swg.setP(p)
        cc = 3*(k-1)*(1-p)**3/(2*(2*k-1))
        self.assertEquals(self.swg.clusteringCoefficient(), cc)

        k = 1
        self.swg.setK(k)
        self.assertEquals(self.swg.clusteringCoefficient(), 0)