Revision cb770240 custompackages/graph-parser/src/sub_component.cpp

View differences:

custompackages/graph-parser/src/sub_component.cpp
9 9
    // do nothing
10 10
}
11 11

  
12
SubComponent::SubComponent(StringSet art_points, Graph sub_graph) : art_points_(art_points), sub_graph_(sub_graph) {
13
// SubComponent::SubComponent(VertexVec aart_points, Graph asubGraph) {
14
    // art_points = aart_points;
15
    // subGraph = asubGraph;
16
    // Deep copy for art_points
17
    // cout << "ABC " << endl;
18
    // for (VertexVecIter vi = aart_points.begin(); vi != aart_points.end(); ++vi) {
19
    //     Vertex v = Vertex(*vi);
20
    //     this->art_points.insert(this->art_points.end(), v);
21
    //     cout << "   asubGraph " << asubGraph[*vi].name << endl;
22
    //     cout << "    subGraph " << subGraph[*vi].name << endl;
23
    // }
12
/* GETTERS & SETTERS & UPDATERS */
13
GraphManager const& SubComponent::gm() const {
14
    return gm_;
15
}
24 16

  
17
StringSet const& SubComponent::all_vertices_id() const {
18
    return all_vertices_id_;
19
}
25 20

  
26
    // init();
21
StringSet const& SubComponent::art_points_id() const {
22
    return art_points_id_;
27 23
}
28 24

  
29
StringSet SubComponent::art_points() const {
30
    return art_points_;
25
StringSet const& SubComponent::non_art_points_id() const {
26
    return non_art_points_id_;
27
}
28
NameToIntMap const& SubComponent::weight_map() const {
29
    // Returns the whole weight_map_, for all the vertices
30
    // This one is different from get_weight_map(name)
31
    return weight_map_;
31 32
}
32 33

  
33
void SubComponent::set_art_points(StringSet& art_points) {
34
    art_points_ = art_points;
34
NameToIntMap const& SubComponent::weight_reversed_map() const {
35
    return weight_reversed_map_;
35 36
}
36 37

  
37
int SubComponent::num_vertices() {
38
    return boost::num_vertices(sub_graph_);
38
vector< vector< int > > const& SubComponent::traffic_matrix() const {
39
    return traffic_matrix_;
39 40
}
40 41

  
42
/* CREATE SUB-COMPONENT */
41 43
void SubComponent::AddEdge(Router r1, Router r2, Link l) {
42
    cout << "add edge " << r1.label << " - " << r2.label << endl;
44
    gm_.AddEdge(r1, r2, l);
45
}
46

  
47
void SubComponent::FinalizeSubComponent(StringSet all_art_points_id) {
48
    // Create set of vertices id
49
    graphext::id_of_all_vertices(gm_.g_, all_vertices_id_);
50

  
51
    // Create set of this sub-component's articulation points id
52
    using namespace setops;
53
    art_points_id_ = all_vertices_id_ / all_art_points_id;
43 54

  
44
    string s1 = r1.id;
45
    string s2 = r2.id;
46
    Vertex v1;
47
    Vertex v2;
55
    // Create set of vertices id (such that those vertices are not articulation points)
56
    non_art_points_id_ = all_vertices_id_ - art_points_id_;
48 57

  
49
    try {
50
        v1 = get_vertex_from_id(s1);
58
    // Create name -> index map
59
    int index = 0;
60
    for (string s : all_vertices_id_) {
61
        name_index_map_[s] = index;
62
        ++index;
51 63
    }
52
    catch (exception& e) {
53
        v1 = boost::add_vertex(r1, sub_graph_);
54
        name_vertex_map_[s1] = v1;
64
}
65

  
66

  
67
/* LINK WEIGHT CALCULATION */
68
void SubComponent::initialize_weight() {
69
    for (string s : art_points_id_) {
70
        update_weight_map(s, -1);
55 71
    }
56
    try {
57
        v2 = get_vertex_from_id(s2);
72
}
73

  
74
int SubComponent::get_weight_map(string name) {
75
    // Return only the weight for the vertex with the given name.
76
    // Check out weight_map()
77
    if (stdhelper::exists(weight_map_, name)) {
78
        return weight_map_[name];
58 79
    }
59
    catch (exception& e) {
60
        v2 = boost::add_vertex(r2, sub_graph_);
61
        name_vertex_map_[s2] = v2;
80
    else {
81
        return 0;
62 82
    }
63
    boost::add_edge(v1, v2, l, sub_graph_);
64 83
}
65 84

  
66
bool SubComponent::vertex_existed(string s) {
67
    std::map<std::string, Vertex>::iterator it;
68
    it = name_vertex_map_.find(s);
69
    return (it != name_vertex_map_.end());
85
int SubComponent::get_weight_reversed_map(string name) {
86
    // Return only the 'weight reversed' for the vertex with the given name.
87
    // Check out weight_reversed_map(). Those 2 functions serve different purpose
88
    if (stdhelper::exists(weight_reversed_map_, name)) {
89
        return weight_reversed_map_[name];
90
    }
91
    else {
92
        return 0;
93
    }
70 94
}
71 95

  
72
const Vertex& SubComponent::get_vertex_from_id(string s) {
73
    if (vertex_existed(s)) {
74
        return name_vertex_map_[s];
96
void SubComponent::update_weight_map(string name, int value) {
97
    // cout << "update " << name << " = " << value << endl;
98
    weight_map_[name] = value;
99
}
100

  
101
/* TRAFFIC MATRIX */
102
void SubComponent::CalculateTrafficMatrix() {
103
    initialize_traffic_matrix();
104

  
105
    // When only one vertex is an articulation point
106
    for (string a : art_points_id_) {
107
        // cout << a << " ";
108
        for (string non_a : non_art_points_id_) {
109
            // cout << non_a << " ";
110
            int communication_intensity = get_weight_reversed_map(a) + 1;
111
            update_traffic_matrix(a, non_a, communication_intensity);
112
        }
113
        // cout << endl;
75 114
    }
76
    else {
77
        throw std::runtime_error("Vertex not found\n");
115

  
116
    // When both vertices are articulation points
117
    int size = art_points_id_.size();
118
    for (string a1 : art_points_id_) {
119
        for (string a2 : art_points_id_) {
120
            if (a1.compare(a2) == 0)
121
                continue;
122

  
123
            int communication_intensity = (
124
                (get_weight_reversed_map(a1) + 1) *
125
                (get_weight_reversed_map(a2) + 1)
126
            );
127
            update_traffic_matrix(a1, a2, communication_intensity);
128
        }
78 129
    }
130
    // cout << "Mark 3\n";
79 131
}
80 132

  
81
std::ostream& operator<<(std::ostream& os, const SubComponent& sc) {
82
    cout << "Sub-component" << endl;
83
    outops::operator<<(cout, sc.sub_graph());
84
    outops::operator<<(cout, sc.art_points());
85
    return os;
133
void SubComponent::initialize_traffic_matrix() {
134
    // generate_empty_traffic_matrix, with 1 every where, and 0 in the main diagonal
135
    int size = num_of_vertices();
136
    traffic_matrix_ = vector< vector<int> >(size);
137
    for (int i = 0; i < size; ++i) {
138
        traffic_matrix_[i] = vector< int >(size, 1);
139
    }
140

  
141
    // Reset the main diagonal to 0
142
    for (int i = 0; i < size; ++i) {
143
        traffic_matrix_[i][i] = 0;
144
    }
145
}
146

  
147
int SubComponent::get_traffic_matrix(string name_1, string name_2) {
148
    int i1 = index_of_vertex_id(name_1);
149
    int i2 = index_of_vertex_id(name_2);
150
    // TODO: exception
151
    return traffic_matrix_[i1][i2];
152
}
153

  
154
void SubComponent::update_traffic_matrix(string name_1, string name_2, int value) {
155
    int i1 = index_of_vertex_id(name_1);
156
    int i2 = index_of_vertex_id(name_2);
157
    cout << i1 << " " << i2 << " = " << value << endl;
158
    traffic_matrix_[i1][i2] = value;
159
    traffic_matrix_[i2][i1] = value; // because Traffic Matrix is symmetric
160
}
161

  
162
/* HELPERS */
163
int SubComponent::num_of_vertices() {
164
    return boost::num_vertices(gm_.g_);
165
}
166

  
167
int SubComponent::index_of_vertex_id(string vertex_id) {
168
    // TODO: might throw exception here
169
    return name_index_map_[vertex_id];
86 170
}
87 171

  
88
Graph const& SubComponent::sub_graph() const {
89
    return sub_graph_;
172
bool SubComponent::vertex_exists(string name) {
173
    stdhelper::exists(art_points_id_, name);
174
}
175

  
176
string SubComponent::first_vertex_id_with_unknown_weight() {
177
    string vertex_id = "";
178
    for (auto &i : weight_map_) {
179
        if (i.second == -1) {
180
            vertex_id = i.first;
181
            break;
182
        }
183
    }
184
    return vertex_id;
90 185
}
91 186

  
187
/* HELPERS FOR OUTPUTTING RESULT */
188
void SubComponent::print_traffic_matrix() {
189

  
190
}
191

  
192
std::ostream& operator<<(std::ostream& os, const SubComponent& sc) {
193
    cout << "Sub-component:" << endl;
194
    cout << sc.gm_;
195

  
196
    cout << "\nArticulation points ID:\n";
197
    outops::operator<<(cout, sc.art_points_id());
198

  
199
    cout << "\nNormal Vertices ID:\n";
200
    outops::operator<<(cout, sc.all_vertices_id());
201

  
202
    cout << "\nLink Weight:\n";
203
    outops::operator<<(cout, sc.weight_map());
204
    // printhelper::for_map<string, int>(sc.weight_map());
205

  
206
    cout << "\nTraffic Matrix:\n";
207
    outops::operator<<(cout, sc.traffic_matrix());
208

  
209
    return os;
210
}

Also available in: Unified diff