graph_generation.cc 3.52 KB
Newer Older
Tiago Peixoto's avatar
Tiago Peixoto committed
1
2
// graph-tool -- a general graph modification and manipulation thingy
//
Tiago Peixoto's avatar
Tiago Peixoto committed
3
// Copyright (C) 2007  Tiago de Paula Peixoto <tiago@forked.de>
Tiago Peixoto's avatar
Tiago Peixoto committed
4
5
6
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
Tiago Peixoto's avatar
Tiago Peixoto committed
7
// as published by the Free Software Foundation; either version 3
Tiago Peixoto's avatar
Tiago Peixoto committed
8
9
10
11
12
13
14
15
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
16
17
// along with this program. If not, see <http://www.gnu.org/licenses/>.

Tiago Peixoto's avatar
Tiago Peixoto committed
18
#include "graph.hh"
19
20
21
22
#include "graph_util.hh"
#include "graph_filtering.hh"
#include "graph_generation.hh"
#include <boost/python.hpp>
Tiago Peixoto's avatar
Tiago Peixoto committed
23
24
25
26
27
28

using namespace std;
using namespace boost;
using namespace boost::lambda;
using namespace graph_tool;

29
typedef tr1::mt19937 rng_t;
Tiago Peixoto's avatar
Tiago Peixoto committed
30

31
class PythonFuncWrap
Tiago Peixoto's avatar
Tiago Peixoto committed
32
{
33
public:
34
    PythonFuncWrap(python::object o): _o(o) {}
35

36
    pair<size_t, size_t> operator()() const
Tiago Peixoto's avatar
Tiago Peixoto committed
37
    {
38
39
        python::object ret = _o();
        return python::extract<pair<size_t,size_t> >(ret);
Tiago Peixoto's avatar
Tiago Peixoto committed
40
41
    }

42
    size_t operator()(bool) const
Tiago Peixoto's avatar
Tiago Peixoto committed
43
    {
44
45
        python::object ret = _o();
        return python::extract<size_t>(ret);
46
47
    }

48
    double operator()(pair<size_t, size_t> deg, pair<size_t, size_t> degl) const
49
    {
50
51
52
        python::object ret = _o(python::make_tuple(deg.first, deg.second),
                                python::make_tuple(degl.first, degl.second));
        return python::extract<double>(ret);
Tiago Peixoto's avatar
Tiago Peixoto committed
53
54
    }

55
private:
56
57
58
    python::object _o;
};

59
60
61
62
63
64
void generate_random_graph(GraphInterface& gi, size_t N,
                           python::object deg_sample,
                           python::object corr_deg_sample,
                           bool uncorrelated, bool no_parallel,
                           bool no_self_loops, bool undirected,
                           size_t seed, bool verbose)
Tiago Peixoto's avatar
Tiago Peixoto committed
65
{
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
    typedef graph_tool::detail::get_all_graph_views::apply<
    graph_tool::detail::scalar_pairs, mpl::bool_<false>,
        mpl::bool_<false>, mpl::bool_<false>,
        mpl::bool_<true>, mpl::bool_<true> >::type graph_views;

    if (undirected)
        gi.SetDirected(false);

    if (uncorrelated)
    {
        run_action<graph_views>()
            (gi, lambda::bind<void>(gen_random_graph<mpl::bool_<false> >(N),
                                    lambda::_1,
                                    PythonFuncWrap(deg_sample),
                                    PythonFuncWrap(corr_deg_sample),
                                    no_parallel, no_self_loops,
                                    undirected, seed, verbose))();
    }
    else
    {
        run_action<graph_views>()
            (gi, lambda::bind<void>(gen_random_graph<mpl::bool_<true> >(N),
                                    lambda::_1,
                                    PythonFuncWrap(deg_sample),
                                    PythonFuncWrap(corr_deg_sample),
                                    no_parallel, no_self_loops,
                                    undirected, seed, verbose))();
    }
    gi.ReIndexEdges();
}
96

97
98
99
100
101
void random_rewire(GraphInterface& gi, string strat, bool self_loops,
                   bool parallel_edges, size_t seed);

using namespace boost::python;

102
103
104
BOOST_PYTHON_MODULE(libgraph_tool_generation)
{
    def("gen_random_graph", &generate_random_graph);
105
    def("random_rewire", &random_rewire);
Tiago Peixoto's avatar
Tiago Peixoto committed
106
}