c++ - How to read value from `Link` between two nodes using omnet++ -
i'm trying implement ford fulkerson algorithm in omnet++ using c++. took c++ class article couldn't read values network should come link
s
my class algorithm :
// c++ program implementation of ford fulkerson algorithm #include <iostream> #include <limits.h> #include <string.h> #include <queue> using namespace std; // number of vertices in given graph #define v 6 class node : public csimplemodule { protected: // following redefined virtual function holds algorithm. virtual void initialize(); virtual void handlemessage(cmessage *msg); }; define_module(node); void node::initialize() { // these values should come links int graph[v][v] = { {0, 16, 13, 0, 0, 0}, {0, 0, 10, 12, 0, 0}, {0, 4, 0, 0, 14, 0}, {0, 0, 9, 0, 0, 20}, {0, 0, 0, 7, 0, 4}, {0, 0, 0, 0, 0, 0} }; fordfulkerson(graph, 0, 5); } void node::handlemessage(cmessage *msg) { send(msg, "out"); } /* returns true if there path source 's' sink 't' in residual graph. fills parent[] store path */ bool bfs(int rgraph[v][v], int s, int t, int parent[]) { // create visited array , mark vertices not visited bool visited[v]; memset(visited, 0, sizeof(visited)); // create queue, enqueue source vertex , mark source vertex // visited queue <int> q; q.push(s); visited[s] = true; parent[s] = -1; // standard bfs loop while (!q.empty()) { int u = q.front(); q.pop(); (int v=0; v<v; v++) { if (visited[v]==false && rgraph[u][v] > 0) { q.push(v); parent[v] = u; visited[v] = true; } } } // if reached sink in bfs starting source, return // true, else false return (visited[t] == true); } // returns tne maximum flow s t in given graph int fordfulkerson(int graph[v][v], int s, int t) { int u, v; // create residual graph , fill residual graph // given capacities in original graph residual capacities // in residual graph int rgraph[v][v]; // residual graph rgraph[i][j] indicates // residual capacity of edge j (if there // edge. if rgraph[i][j] 0, there not) (u = 0; u < v; u++) (v = 0; v < v; v++) rgraph[u][v] = graph[u][v]; int parent[v]; // array filled bfs , store path int max_flow = 0; // there no flow // augment flow while tere path source sink while (bfs(rgraph, s, t, parent)) { // find minimum residual capacity of edhes along // path filled bfs. or can find maximum flow // through path found. int path_flow = int_max; (v=t; v!=s; v=parent[v]) { u = parent[v]; path_flow = min(path_flow, rgraph[u][v]); } // update residual capacities of edges , reverse edges // along path (v=t; v != s; v=parent[v]) { u = parent[v]; rgraph[u][v] -= path_flow; rgraph[v][u] += path_flow; } // add path flow overall flow max_flow += path_flow; } // return overall flow return max_flow; }
network :
/ // program free software: can redistribute and/or modify // under terms of gnu lesser general public license published // free software foundation, either version 3 of license, or // (at option) later version. // // program distributed in hope useful, // without warranty; without implied warranty of // merchantability or fitness particular purpose. see // gnu lesser general public license more details. // // should have received copy of gnu lesser general public license // along program. if not, see http://www.gnu.org/licenses/. // import node; import link; // // generated network random topology (6 nodes, 8 edges, seed=100). // network network { @display("bgb=478,329"); submodules: s: node { @display("p=19,87;is=s"); } n1: node { @display("p=130,142;is=s"); } n2: node { @display("p=130,36;is=s"); } n3: node { @display("p=262,142;is=s"); } n4: node { @display("p=262,36;is=s"); } t: node { @display("p=364,87;is=s"); } connections: s.g++ <--> link { @display("t=13"); cost = 13; } <--> n1.g++; s.g++ <--> link { cost = 16;@display("t=16"); } <--> n2.g++; n1.g++ <--> link { cost = 1;@display("t=1"); } <--> n2.g++; n1.g++ <--> link { cost = 14;@display("t=14"); } <--> n3.g++; n1.g++ <--> link { cost = 9;@display("t=9"); } <--> n4.g++; n2.g++ <--> link { cost = 12;@display("t=12"); } <--> n4.g++; n4.g++ <--> link { cost = 20;@display("t=20"); } <--> t.g++; n3.g++ <--> link { cost = 4;@display("t=4"); } <--> t.g++; n3.g++ <--> link { cost = 7;@display("t=7"); } <--> n4.g++; }
it seems using custom module called link
channel, seem have given cost
parameter. getting cost
parameter node
requires 3 steps:
for getting link
channels connecting node
neighbors, need first query node's connections using omnet++ api. see section accessing gates , connections in user manual details. each connection, can pointer associated channel (your link
module) using getchannel
method.
using pointer link
module, can query value of parameters using omnet++'s par()
method.
i see setting link
modules' display string (in particular, t
tag annotate text). see user manual's chapter on display strings information on how read (or write to) tag.
Comments
Post a Comment