-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgensfnet.cpp
More file actions
156 lines (127 loc) · 2.99 KB
/
gensfnet.cpp
File metadata and controls
156 lines (127 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//============================================================================
// Name : gensfnet.cpp
// Author : Mushthofa
// Version :
// Copyright :
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include <cstdlib>
#include <string>
#include <vector>
#include <sstream>
#include <ctime>
#include <iostream>
using namespace std;
int adjmat[50][50];
bool randomBool(float percentage)
{
return (rand()%1000)<(percentage*1000);
}
void gensfmat(int sz, int m, float alpha)
{
/* Connect the first m genes */
for (int i=0; i<sz; i++)
for (int j=i+1; j<sz; j++)
adjmat[i][j] = adjmat[j][i] = 0;
for (int i=0; i<m; i++)
for (int j=i+1; j<m; j++)
adjmat[i][j] = adjmat[j][i] = 1;
/* Add genes one by one, and connect with probability proportional to current degrees */
for (int i=m+1; i<sz; i++)
{
float degree[50];
for (int k=0; k<sz; k++)
degree[k] = 0;
for (int k=0; k<i; k++)
for(int l=0; l<sz; l++)
degree[k] += adjmat[k][l];
float sumdegree = 0;
for(int k=0; k<i; k++)
sumdegree += degree[k];
for(int k=0; k<i; k++)
degree[k] /= sumdegree;
for(int j=0; j<i; j++)
{
if(randomBool(degree[j] + alpha))
adjmat[j][i] = adjmat[i][j] = 1;
}
}
/* randomly select direction of edges (either one or both directions) */
for(int i=0; i<sz; i++)
{
for(int j=i+1; j<sz; j++)
{
/* Left */
if((adjmat[i][j]==1) && randomBool(0.5))
adjmat[i][j] = 0;
if((adjmat[i][j]==1) && randomBool(0.5))
adjmat[j][i] = 0;
}
}
}
void enumexprec(vector<string> nodes, int headnode, string pref, vector<int> restnode, int k)
{
if(restnode.size()==1)
{
for(int i=0; i<=k; i++)
{
int rv = (rand())%(k+1);
cout << nodes[headnode] <<" "<< rv <<" "<<pref<<" "<<nodes[restnode[0]]<<" "<< i << std::endl;
}
}
else
{
int curnode = restnode.back();
restnode.pop_back();
for(int i=0; i<=k; i++)
{
ostringstream oss;
oss<<nodes[curnode]<<" "<<i<<" ";
string curpref;
curpref = pref + oss.str();
enumexprec(nodes, headnode, curpref, restnode, k);
}
}
}
void enumexp(vector<string> nodes, int headnode, vector<int> incoming, int k)
{
string empty;
enumexprec(nodes, headnode, empty, incoming, k);
}
int main(int argc, char** argv)
{
srand(time(0));
int n = atoi(argv[1]);
int k = atoi(argv[2]);
int i,j;
cout<<n<<" "<<k<<endl;
vector<string> nodes;
for(i=0; i<n; i++)
{
std::ostringstream oss;
oss << i;
nodes.push_back(string("node_") + oss.str());
}
gensfmat(n, 3, 0.1);
int degree[50];
for (i=0; i<n; i++)
degree[i] = 0;
for (i=0; i<n; i++)
for(j=0; j<n; j++)
degree[i] += adjmat[j][i];
for(i=0; i<n; i++)
cout<<nodes[i]<<endl;
for(i=0; i<n; i++)
{
//cout<<nodes[i] << degree[i]<<endl;
vector<int> incoming;
for (j=0; j<n; j++)
if(adjmat[j][i] > 0 && incoming.size()<4)
incoming.push_back(j);
int deg = incoming.size();
if(deg > 0)
enumexp(nodes, i, incoming, k);
}
return 0;
}