Skip to content

Commit

Permalink
Combine both GIRG classes into one
Browse files Browse the repository at this point in the history
  • Loading branch information
manpen committed May 25, 2019
1 parent 70efaf2 commit 59bba9b
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 61 deletions.
156 changes: 100 additions & 56 deletions networkit/_NetworKit.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2107,47 +2107,28 @@ cdef class ErdosRenyiGenerator(StaticGraphGenerator):

cdef extern from "cpp/generators/GeometricInhomogenousGenerator.h":
cdef cppclass _GeometricInhomogenousGenerator "NetworKit::GeometricInhomogenousGenerator"(_StaticGraphGenerator):
ctypedef vector[vector[double]] point_vector
ctypedef vector[double] weight_vector

_GeometricInhomogenousGenerator(count n, double avgDegree, double powerlawExp, double alpha, unsigned dim) except +
_GeometricInhomogenousGenerator(vector[vector[double]] points, vector[double] weights, double alpha) except +
_GeometricInhomogenousGenerator(vector[vector[double]] points, vector[double] weights, double avgDegree, double alpha) except +
_Graph generateKeepingInput() except +
const vector[double]& weights()
const vector[vector[double]]& positions()

cdef class _GeometricInhomogenousGeneratorBase(StaticGraphGenerator):
"""
Returns a list of node weights.
This data is automatically deleted during the execution of generate. If you want to access it,
either do so BEFORE calling generate or use generateKeepingInput instead
"""
def getWeights(self):
return (<_GeometricInhomogenousGenerator*>(self._this)).weights()

"""
Returns a list of node positions.
const weight_vector& weights()
const point_vector& positions()

This data is automatically deleted during the execution of generate. If you want to access it,
either do so BEFORE calling generate or use generateKeepingInput instead
"""
def getPositions(self):
return (<_GeometricInhomogenousGenerator*>(self._this)).positions()

"""
Same generate() but keep the input point set.
"""
def generateKeepingInput(self):
return Graph().setThis((<_GeometricInhomogenousGenerator*>(self._this)).generateKeepingInput())

cdef class GeometricInhomogenousGenerator(_GeometricInhomogenousGeneratorBase):
cdef class GeometricInhomogenousGenerator(StaticGraphGenerator):
"""
Creates a Geometric Inhomogenous Random Graph by first samling n random points in a d-dimension space
and then connecting them according to their weights and distances.
and then connecting them according to their weights and distances. It implements the generator of
Blaesius et al. "Efficiently Generating Geometric Inhomogeneous and Hyperbolic Random Graphs" [https://arxiv.org/abs/1905.06706]
GeometricInhomogenousGenerator(count numNodes, double avgDegree, double powerlawExp, double alpha, unsigned dimensions)
Creates G(nNodes, prob) graphs.
see also: GeometricInhomogenousGenerator.fromPoints() to create a graph from an explicitly provided point set.
Parameters
----------
numNodes : count
Expand All @@ -2161,40 +2142,103 @@ cdef class GeometricInhomogenousGenerator(_GeometricInhomogenousGeneratorBase):
dimensions : unsigned
Number of dimensions in the underlying geometric with 1 <= dimensions <= 5
"""
def __cinit__(self, numNodes = 0, avgDegree = 0, powerlawExp = 3, alpha = math.inf, dimensions = 1):
self._numNodes = numNodes
self._avgDegree = avgDegree
self._powerlawExp = powerlawExp
self._alpha = alpha
self._dimensions = dimensions
self._this = None # new _GeometricInhomogenousGenerator(numNodes, avgDegree, powerlawExp, alpha, dimensions)



cdef class GeometricInhomogenousFromPointSetGenerator(_GeometricInhomogenousGeneratorBase):
"""
Creates a Geometric Inhomogenous Random Graph from a point set provided.

# these values are used to delay construction of self._this; do not rely on these values,
# as they are not properly set if constructed via fromPoints!
cdef int _numNodes
cdef double _avgDegree
cdef double _powerlawExp
cdef double _alpha
cdef int _dimensions

def __cinit__(self, numNodes, avgDegree, powerlawExp = 3, alpha = math.inf, dimensions = 1):
# params is only used in _constructOnDemand; it is not well-defined if constructed with fromPoints(..)
self._numNodes = numNodes
self._avgDegree = avgDegree
self._powerlawExp = powerlawExp
self._alpha = alpha
self._dimensions = dimensions

@staticmethod # cannot used @classmethod as we need to bind the cls's type in order for the cdefs to work
def fromPoints(positions, weights, alpha = math.inf, avgDegree = None):
"""
Creates a Geometric Inhomogenous Random Graph from a point set provided.
GeometricInhomogenousFromPointSetGenerator(self, vector[vector[double]] positions, vector[double] weights, double alpha = math.inf, avgDegree = None)
Parameters
----------
positions : vector[ vector[double] ]
A vector of one position for each node. A position is a vector with d entries between 0.0 and 1.0.
All points have to have the same dimension d with 1 <= d <= 5.
weights : vector[double]
Weights for each point
alpha : double
Alpha parameter (1 / Temperature) with alpha > 1
avgDegree : double
Desired average degree, if omitted (= None) the weights provided are used directly without scaling
"""
assert(len(positions) == len(weights))

GeometricInhomogenousFromPointSetGenerator(self, vector[vector[double]] positions, vector[double] weights, double alpha = math.inf, avgDegree = None)
self = GeometricInhomogenousGenerator(len(positions), 0)

Parameters
----------
positions : vector[ vector[double] ]
A vector of one position for each node. A position is a vector with d entries between 0.0 and 1.0.
All points have to have the same dimension d with 1 <= d <= 5.
weights : vector[double]
Weights for each point
alpha : double
Alpha parameter (1 / Temperature) with alpha > 1
avgDegree : double
Desired average degree, if omitted (= None) the weights provided are used directly without scaling
"""
def __cinit__(self, vector[vector[double]] positions, vector[double] weights, double alpha = math.inf, avgDegree = None):
if avgDegree is None:
self._this = new _GeometricInhomogenousGenerator(positions, weights, alpha)
else:
self._this = new _GeometricInhomogenousGenerator(positions, weights, avgDegree, alpha)

return self

def getWeights(self):
"""
Returns a list of node weights.
This data is automatically deleted during the execution of generate. If you want to access it,
either do so BEFORE calling generate or use generateKeepingInput instead
"""
self._constructOnDemand()
return (<_GeometricInhomogenousGenerator*>(self._this)).weights()

def getPositions(self):
"""
Returns a list of node positions.
This data is automatically deleted during the execution of generate. If you want to access it,
either do so BEFORE calling generate or use generateKeepingInput instead
"""
self._constructOnDemand()
return (<_GeometricInhomogenousGenerator*>(self._this)).positions()

def generate(self):
"""
Generates the graph.
Returns
-------
networkit.Graph
"""
self._constructOnDemand()
return Graph().setThis((<_GeometricInhomogenousGenerator*>(self._this)).generate())

def generateKeepingInput(self):
"""
Same generate() but keep the input point set.
"""
self._constructOnDemand()
return Graph().setThis((<_GeometricInhomogenousGenerator*>(self._this)).generateKeepingInput())

cdef _constructOnDemand(self):
"""
INTERNAL METHOD, DO NOT CALL DIRECTLY
Constructs the C++ instance wrapper (if not done so yet)
"""
if self._this != NULL:
return

self._this = new _GeometricInhomogenousGenerator(self._numNodes, self._avgDegree,
self._powerlawExp, self._alpha, self._dimensions)


cdef extern from "cpp/generators/DorogovtsevMendesGenerator.h":
cdef cppclass _DorogovtsevMendesGenerator "NetworKit::DorogovtsevMendesGenerator"(_StaticGraphGenerator):
_DorogovtsevMendesGenerator(count nNodes) except +
Expand Down
9 changes: 9 additions & 0 deletions networkit/cpp/generators/girgs/Generator.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
/*
* Generator.h
*
* Created on: 03. May 2019
* Author: Christopher Weyand <Christopher.Weyand@hpi.de>, Manuel Penschuck <networkit@manuel.jetzt>
*
* Code is adopted from https://github.com/chistopher/girgs
*/

#include <functional>
#include <mutex>

Expand Down
3 changes: 3 additions & 0 deletions networkit/cpp/generators/girgs/Generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
*
* Created on: 03. May 2019
* Author: Christopher Weyand <Christopher.Weyand@hpi.de>, Manuel Penschuck <networkit@manuel.jetzt>
*
* Code is adopted from https://github.com/chistopher/girgs
*
*/

#ifndef GENERATORS_GIRGS_GENERATOR_H_
Expand Down
2 changes: 2 additions & 0 deletions networkit/cpp/generators/girgs/Node.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
*
* Created on: 03. May 2019
* Author: Christopher Weyand <Christopher.Weyand@hpi.de>, Manuel Penschuck <networkit@manuel.jetzt>
*
* Code is adopted from https://github.com/chistopher/girgs
*/

#ifndef GENERATORS_GIRGS_NODE_H_
Expand Down
2 changes: 2 additions & 0 deletions networkit/cpp/generators/girgs/SpatialTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
*
* Created on: 03. May 2019
* Author: Christopher Weyand <Christopher.Weyand@hpi.de>, Manuel Penschuck <networkit@manuel.jetzt>
*
* Code is adopted from https://github.com/chistopher/girgs
*/

#ifndef GENERATORS_GIRGS_SPATIAL_TREE_H_
Expand Down
2 changes: 2 additions & 0 deletions networkit/cpp/generators/girgs/SpatialTreeCoordinateHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
*
* Created on: 03. May 2019
* Author: Christopher Weyand <Christopher.Weyand@hpi.de>, Manuel Penschuck <networkit@manuel.jetzt>
*
* Code is adopted from https://github.com/chistopher/girgs
*/

#ifndef GENERATORS_GIRGS_SPATIAL_TREE_COORDINATE_HELPER_H_
Expand Down
2 changes: 2 additions & 0 deletions networkit/cpp/generators/girgs/WeightLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
*
* Created on: 03. May 2019
* Author: Christopher Weyand <Christopher.Weyand@hpi.de>, Manuel Penschuck <networkit@manuel.jetzt>
*
* Code is adopted from https://github.com/chistopher/girgs
*/

#ifndef GENERATORS_GIRGS_WEIGHT_LAYER_H_
Expand Down
9 changes: 9 additions & 0 deletions networkit/cpp/generators/girgs/WeightScaling.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
/*
* WeightScaling.cpp
*
* Created on: 03. May 2019
* Author: Christopher Weyand <Christopher.Weyand@hpi.de>, Manuel Penschuck <networkit@manuel.jetzt>
*
* Code is adopted from https://github.com/chistopher/girgs
*/

#include <algorithm>
#include <cassert>
#include <functional>
Expand Down
4 changes: 3 additions & 1 deletion networkit/cpp/generators/girgs/WeightScaling.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
/*
* BitManipulation.h
* WeightScaling.h
*
* Created on: 03. May 2019
* Author: Christopher Weyand <Christopher.Weyand@hpi.de>, Manuel Penschuck <networkit@manuel.jetzt>
*
* Code is adopted from https://github.com/chistopher/girgs
*/

#ifndef GENERATORS_GIRGS_WEIGHT_SCALING_H_
Expand Down
2 changes: 1 addition & 1 deletion networkit/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
__author__ = "Christian Staudt"

# extension imports
from _NetworKit import BarabasiAlbertGenerator, PubWebGenerator, ErdosRenyiGenerator, GeometricInhomogenousGenerator, GeometricInhomogenousFromPointSetGenerator, ClusteredRandomGraphGenerator, DorogovtsevMendesGenerator, DynamicPubWebGenerator, DynamicPathGenerator, ChungLuGenerator, HyperbolicGenerator, MocnikGenerator, MocnikGeneratorBasic, DynamicHyperbolicGenerator, HavelHakimiGenerator, DynamicDorogovtsevMendesGenerator, RmatGenerator, DynamicForestFireGenerator, RegularRingLatticeGenerator, WattsStrogatzGenerator, PowerlawDegreeSequence, EdgeSwitchingMarkovChainGenerator, EdgeSwitchingMarkovChainGenerator as ConfigurationModelGenerator, LFRGenerator
from _NetworKit import BarabasiAlbertGenerator, PubWebGenerator, ErdosRenyiGenerator, GeometricInhomogenousGenerator, ClusteredRandomGraphGenerator, DorogovtsevMendesGenerator, DynamicPubWebGenerator, DynamicPathGenerator, ChungLuGenerator, HyperbolicGenerator, MocnikGenerator, MocnikGeneratorBasic, DynamicHyperbolicGenerator, HavelHakimiGenerator, DynamicDorogovtsevMendesGenerator, RmatGenerator, DynamicForestFireGenerator, RegularRingLatticeGenerator, WattsStrogatzGenerator, PowerlawDegreeSequence, EdgeSwitchingMarkovChainGenerator, EdgeSwitchingMarkovChainGenerator as ConfigurationModelGenerator, LFRGenerator

from . import graphio

Expand Down
12 changes: 9 additions & 3 deletions networkit/test/test_generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@ def setUp(self):
pass

def test_GeometricInhomogenous(self):
errorFound = False
# Test that lazy initialization works on all methods
assert(len(nk.generators.GeometricInhomogenousGenerator(100, 10).getPositions()) == 100)
assert(len(nk.generators.GeometricInhomogenousGenerator(100, 10).getWeights()) == 100)
assert(nk.generators.GeometricInhomogenousGenerator(100, 10).generate().numberOfNodes() == 100)
assert(nk.generators.GeometricInhomogenousGenerator(100, 10).generateKeepingInput().numberOfNodes() == 100)

# Test that generator produces graphs as requested
errorFound = False
for n in [100, 200, 500]:
for avgDeg in [n / 10, n / 5]:
for ple in [2.5, 3.0, 4.0]:
Expand Down Expand Up @@ -38,12 +44,12 @@ def test_GeometricInhomogenous(self):
assert(len(gen.getPositions()) == 0)
assert(len(gen.getPositions()) == 0)

gen2 = nk.generators.GeometricInhomogenousFromPointSetGenerator(positions, weights, alpha)
gen2 = nk.generators.GeometricInhomogenousGenerator.fromPoints(positions, weights, alpha)
graphs.append( (gen2.generate(), "FromPSNoScale") )

# now scale weights and let generate scale them back
scaled_weights = [2*x for x in weights]
gen3 = nk.generators.GeometricInhomogenousFromPointSetGenerator(positions, scaled_weights, alpha, avgDeg)
gen3 = nk.generators.GeometricInhomogenousGenerator.fromPoints(positions, scaled_weights, alpha, avgDeg)
graphs.append( (gen3.generate(), "FromPSScale") )

for G, label in graphs:
Expand Down

0 comments on commit 59bba9b

Please sign in to comment.