graph_generation.cc 5.23 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) 2006-2014 Tiago de Paula Peixoto <tiago@skewed.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
#include "graph_util.hh"
#include "graph_filtering.hh"
#include "graph_generation.hh"
22
#include "sampler.hh"
23
#include "dynamic_sampler.hh"
24
#include <boost/python.hpp>
Tiago Peixoto's avatar
Tiago Peixoto committed
25
26
27
28
29

using namespace std;
using namespace boost;
using namespace graph_tool;

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

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

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

47
private:
Tiago Peixoto's avatar
Tiago Peixoto committed
48
    boost::python::object _o;
49
50
};

Tiago Peixoto's avatar
Tiago Peixoto committed
51
void generate_graph(GraphInterface& gi, size_t N, boost::python::object deg_sample,
52
53
                    bool no_parallel, bool no_self_loops, bool undirected,
                    rng_t& rng, bool verbose, bool verify)
Tiago Peixoto's avatar
Tiago Peixoto committed
54
{
55
    typedef graph_tool::detail::get_all_graph_views::apply<
Tiago Peixoto's avatar
Tiago Peixoto committed
56
57
58
    graph_tool::detail::scalar_pairs, boost::mpl::bool_<false>,
        boost::mpl::bool_<false>, boost::mpl::bool_<false>,
        boost::mpl::bool_<true>, boost::mpl::bool_<true> >::type graph_views;
59
60
61
62

    if (undirected)
        gi.SetDirected(false);

63
    run_action<graph_views>()
Tiago Peixoto's avatar
Tiago Peixoto committed
64
65
66
67
        (gi, std::bind(gen_graph(), placeholders::_1, N,
                       PythonFuncWrap(deg_sample),
                       no_parallel, no_self_loops,
                       std::ref(rng), verbose, verify))();
68
}
69

70
71
size_t random_rewire(GraphInterface& gi, string strat, size_t niter,
                     bool no_sweep, bool self_loops, bool parallel_edges,
72
                     bool alias, bool traditional, bool persist,
Tiago Peixoto's avatar
Tiago Peixoto committed
73
                     boost::python::object corr_prob, boost::any block, bool cache,
74
                     rng_t& rng, bool verbose);
Tiago Peixoto's avatar
Tiago Peixoto committed
75
76
void predecessor_graph(GraphInterface& gi, GraphInterface& gpi,
                       boost::any pred_map);
77
78
void line_graph(GraphInterface& gi, GraphInterface& lgi,
                boost::any edge_index);
Tiago Peixoto's avatar
Tiago Peixoto committed
79
boost::python::tuple graph_union(GraphInterface& ugi, GraphInterface& gi,
80
                          boost::any avprop);
Tiago Peixoto's avatar
Tiago Peixoto committed
81
82
83
84
85
86
void vertex_property_union(GraphInterface& ugi, GraphInterface& gi,
                           boost::any p_vprop, boost::any p_eprop,
                           boost::any uprop, boost::any prop);
void edge_property_union(GraphInterface& ugi, GraphInterface& gi,
                         boost::any p_vprop, boost::any p_eprop,
                         boost::any uprop, boost::any prop);
Tiago Peixoto's avatar
Tiago Peixoto committed
87
void triangulation(GraphInterface& gi, boost::python::object points, boost::any pos,
88
                   string type, bool periodic);
Tiago Peixoto's avatar
Tiago Peixoto committed
89
90
91
void lattice(GraphInterface& gi, boost::python::object oshape, bool periodic);
void geometric(GraphInterface& gi, boost::python::object opoints, double r,
               boost::python::object orange, bool periodic, boost::any pos);
92
void price(GraphInterface& gi, size_t N, double gamma, double c, size_t m,
93
           rng_t& rng);
Tiago Peixoto's avatar
Tiago Peixoto committed
94
void complete(GraphInterface& gi, size_t N, bool directed, bool self_loops);
Tiago Peixoto's avatar
Tiago Peixoto committed
95
void circular(GraphInterface& gi, size_t N, size_t k, bool directed, bool self_loops);
96
97
98

using namespace boost::python;

99
100
BOOST_PYTHON_MODULE(libgraph_tool_generation)
{
101
    def("gen_graph", &generate_graph);
102
    def("random_rewire", &random_rewire);
Tiago Peixoto's avatar
Tiago Peixoto committed
103
    def("predecessor_graph", &predecessor_graph);
104
    def("line_graph", &line_graph);
Tiago Peixoto's avatar
Tiago Peixoto committed
105
106
107
    def("graph_union", &graph_union);
    def("vertex_property_union", &vertex_property_union);
    def("edge_property_union", &edge_property_union);
108
    def("triangulation", &triangulation);
109
110
    def("lattice", &lattice);
    def("geometric", &geometric);
111
    def("price", &price);
Tiago Peixoto's avatar
Tiago Peixoto committed
112
    def("complete", &complete);
Tiago Peixoto's avatar
Tiago Peixoto committed
113
    def("circular", &circular);
114
115
116
117
118

    class_<Sampler<int, boost::mpl::false_>>("Sampler",
                                             init<const vector<int>&, const vector<double>&>())
        .def("sample", &Sampler<int, boost::mpl::false_>::sample<rng_t>,
             return_value_policy<copy_const_reference>());
119
120
121
122
123
124
125
126
127
128

    class_<DynamicSampler<int>>("DynamicSampler",
                                init<const vector<int>&,
                                     const vector<double>&>())
        .def("sample", &DynamicSampler<int>::sample<rng_t>,
             return_value_policy<copy_const_reference>())
        .def("insert", &DynamicSampler<int>::insert)
        .def("remove", &DynamicSampler<int>::remove)
        .def("reset", &DynamicSampler<int>::reset)
        .def("rebuild", &DynamicSampler<int>::rebuild);
Tiago Peixoto's avatar
Tiago Peixoto committed
129
}