/
optim_equality.R
318 lines (232 loc) · 9.76 KB
/
optim_equality.R
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#load library
#library(Rsolnp)
library(igraph)
library(expm) #matrix power
library(RColorBrewer)
#library(LCA)
#library(compositions)
library(ineq)
#------- Finding trees in a random sample graph--------
#generate full network
set.seed(1)
N<-100 # no of nodes
network<-sample_forestfire(nodes=N, fw.prob=0.37, bw.factor=0.32/0.37,directed=TRUE)
#network<-sample_pa(N,power=2)
network<-set.vertex.attribute(network, "name", value=1:N)
E(tree)$curved<-TRUE
plot(network,edge.curved=0.2) # it is actually quite connected
#randomly finding a tree within a graph
sub_N<-as.integer(N/2)
f <- function(graph, data, extra) {
data['rank'] == sub_N
}
bfs_tree<-bfs(network,root=V(network)[sample(1:N,1)], neimode="out",father=TRUE,order=TRUE,unreachable = FALSE,callback=f)
sub_tree<-rbind( bfs_tree$father[bfs_tree$order[1:sub_N,na_ok=TRUE], na_ok = TRUE],bfs_tree$order[1:sub_N,na_ok=TRUE])[,-1] #edge_list
sub_tree<-sub_tree[,!is.na(colSums(sub_tree))] #remove NA's
sub_tree<-graph(sub_tree)
nV_sub_tree<-length(V(sub_tree))
sub_tree<-set.vertex.attribute(sub_tree, "name", value=1:nV_sub_tree) #remember to always add attributes
bfs_tree$father[bfs_tree$order[1:sub_N,na_ok=TRUE],na_ok=TRUE]
plot(sub_tree)
sub_tree<-delete.vertices(sub_tree, degree(sub_tree)==0)
plot(sub_tree,layout=layout_as_tree)
#randomly finding many trees within a network
#-note this tree is a full breadth search with transversal order reduced to sub_N
find_tree<-function(network,bench_wealth=1000){
sub_N<-as.integer(N/2)
N_tree<-sub_N #N_tree<N
f <- function(graph, data, extra) {
data['rank'] == sub_N
}
mylist.names <- paste("t",seq(1,N_tree), sep="" )
master_list<-sapply(mylist.names, assign,NULL)
rand_vert<-V(network)[sample(1:N,N_tree)]
for(i in 1:length(rand_vert) ){
print(i) #remove this when ready
bfs_tree<-bfs(network,root=V(network)[rand_vert[i]], neimode="out",father=TRUE,order=TRUE,unreachable = FALSE,callback=f)
sub_tree<-rbind( bfs_tree$father[bfs_tree$order[1:sub_N,na_ok=TRUE], na_ok = TRUE],bfs_tree$order[1:sub_N,na_ok=TRUE])[,-1] #edge_list
if(all(is.na(sub_tree))){sub_tree<-NULL} #if all nodes are NA
else{
sub_tree<-sub_tree[,!is.na(colSums(sub_tree))] #remove NA's
sub_tree<-graph(sub_tree)
nV_sub_tree<-length(V(sub_tree))
sub_tree<-set.vertex.attribute(sub_tree, "name", value=1:nV_sub_tree) #remember to always add attributes
sub_tree<-delete.vertices(sub_tree, degree(sub_tree)==0)
}
master_list[[i]]<-sub_tree
attributes(master_list[[i]])$"root_wealth"<-rpois(1,max(degree(sub_tree)))*runif(1,0,bench_wealth)
}
return(master_list)
}
#main iteration using filtering
master_out<-find_tree(network)
for(i in 1:N_tree){
#if(!is.null(master_out[[i]])){
print(wealth_trickle(master_out[[i]],root_wealth=attributes(master_out[[i]])$"root_wealth"))
#}
}
#----trying to isolate each branch-------------------------------------------------------------------
obj
obj
obj$father[obj$order]
obj
for(i in obj$order){
print(i)
}
obj_mat<-rbind( obj$father[obj$order],obj$order) #edge_list
obj$father
obj$order
leaves= which(degree(tree, v = V(tree), mode = "out")==0, useNames = T)
root= which(degree(tree, v = V(tree), mode = "in")==0, useNames = T)
paths<-all_simple_paths(tree, from = root, to = leaves)
all_simple_paths(tree, from = 3, to = leaves)
#-------------
#----wealth trickle----
wealth_trickle<-function(tree,root_wealth=100,tau=0.2){
#adjacency matrix
tree_mat<-as_adjacency_matrix(tree)
tree_mat<-as.matrix(tree_mat)
#print(tree_mat)
#make weights
tree_mat[which(rowSums(tree_mat)!=0),]=tree_mat[which(rowSums(tree_mat)!=0),]/rowSums(tree_mat[which(rowSums(tree_mat)!=0),,drop=FALSE])
tree<-graph.adjacency(tree_mat,mode="directed",weighted=TRUE)
plot(tree,layout=layout_as_tree,weights=TRUE,edge.label=round(E(tree)$weight,2))
#find all simple paths from root to leaves
leaves= which(degree(tree, v = V(tree), mode = "out")==0, useNames = T)
root= which(degree(tree, v = V(tree), mode = "in")==0, useNames = T)
paths<-all_simple_paths(tree, from = root, to = leaves)
#length of each path(also can use diameter)
len_tree<-sapply(paths,function(x) length(x)) #length of all paths from root to leaves
max_len_tree<-max(len_tree) #longest length of all paths from root to leaves
#return local-wealth of a layer
local_wealth<-function(layer,mat,tau,root_wealth){
out<-tau*(1-tau)^(layer)*(tree_mat %^% (layer))[root,]
return(out[which(out!=0)]*root_wealth)#
}
#iterate through values(replace 0.2 with tau and root_wealth should be an attribute of a vertex)
mylist <- as.list(1:(max_len_tree-1) ) #different values of layer
return(sapply(mylist,FUN=local_wealth,mat=tree_mat,tau=tau,root_wealth=root_wealth))
#recycle wealth until it is used up(not particularly important)
}
wealth_trickle(tree)
plot(make_tree(10),layout=layout_as_tree)
#until less than epsilon(not particularly important)
eps_stop<-function(eps){
mylist <- as.list(1:(max_len_tree-1) )
for(i in mylist){
if(sum(local_wealth(layer=i,mat=tree_mat,tau=0.2,root_wealth=100))<eps*100){
return(paste("finished at",i))
}
}
}
local_wealth<-function(layer,mat,tau,root_wealth){
out<-tau*(1-tau)^(layer)*(tree_mat %^% (layer))[-1,]
return(out[which(out!=0)]*root_wealth)#
}
#----------------------optimisation-------------------
#specify fairness function
fair<-function(x,w){
top<-sum(w*x)^2
bottom<-length(x)*sum((x*w)^2)
return(top/bottom)
}
fair(c(2,1,0),c(1/2,1,0))
#specify your function
#opt_func <- function(x) {
# 10 - 5*x[1] + 2 * x[2] - x[3]
#}
#specify the equality function. The number 15 (to which the function is equal)
#is specified as an additional argument
#equal <- function(x) {
# x[1] + x[2] + x[3]
#}
equal<-function(M){
p1<-M[1,1]+M[1,2]+M[1,3]
p2<-M[2,2]
p3<-M[3,1]+M[3,2]
return(c(p1,p2,p3))
}
#example
#goeqfn = function(dat, n)
#{
# x = dat[1:n]
# y = dat[(n+1):(2*n)]
# z = dat[(2*n+1):(3*n)]
# apply(cbind(x^2, y^2, z^2), 1, "sum")
#}
#the optimiser - minimises by default
solnp(c(5,5,5), #starting values (random - obviously need to be positive and sum to 15)
fair, #function to optimise
eqfun=equal, #equality function
eqB=1, #the equality constraint
LB=c(0,0,0), #lower bound for parameters i.e. greater than zero
UB=c(100,100,100))#upper bound for parameters (I just chose 100 randomly)
#-----------Curiosity------------
#what is a consensus tree
#consensus_tree(tree_union, hrg = NULL, start = FALSE, num.samples = 10000)
#--- useful list arguments----
#master_list<-vector("list", N_tree)
#c(master_list, paste("t",i,sep="")= sub_tree)
mylist.names <- paste("t",seq(1,N_tree), sep="" )
#master_list<-vector("list", length(mylist.names))
master_list<-sapply(mylist.names, assign,NULL)
#---------------
#network<-sample_smallworld(dim=1, size=100, nei=5, p=0.05, loops = FALSE, multiple = FALSE)
#network<-simplify(network) #remove loops and multiple edges
#plot(rewire(graph.lattice(10, nei=2, dir=TRUE, mutual=TRUE),each_edge(8/100)))
#E(h)
#E(h) %>% rev() #reversing sequence of edge
#wealth trickle edit
#should change edit of the vertex attributes
wealth_trickle<-function(tree,root_wealth=100,tau=0.2){
#moving wealth attributes forward(make as class)
#adjacencymatrix removes attributes(probably can remove effects of adjacency matrix)
wealth_vector_place<-V(tree)$wealth_vector
wealth_vector_trickle_place<-V(tree)$wealth_vector_trickle
root_name_place<-tree$root_name
#V(tree)$wealth_vector_trickle move forward
#adjacency matrix
tree_mat<-as_adjacency_matrix(tree)
tree_mat<-as.matrix(tree_mat)
#print(tree_mat)
#make weights
tree_mat[which(rowSums(tree_mat)!=0),]=tree_mat[which(rowSums(tree_mat)!=0),]/rowSums(tree_mat[which(rowSums(tree_mat)!=0),,drop=FALSE])
tree<-graph.adjacency(tree_mat,mode="directed",weighted=TRUE)
tree<-set.vertex.attribute(tree,"wealth_vector",value=wealth_vector_place)
tree<-set.vertex.attribute(tree,"wealth_vector_trickle",value=wealth_vector_trickle_place)
tree$root_name<-root_name_place
V(tree)$size<-V(tree)$wealth_vector_trickle
plot(tree,layout=layout_as_tree,weights=TRUE,edge.label=round(E(tree)$weight,2))
#find all simple paths from root to leaves
leaves= which(degree(tree, v = V(tree), mode = "out")==0, useNames = T)
root= which(degree(tree, v = V(tree), mode = "in")==0, useNames = T)
paths<-all_simple_paths(tree, from = root, to = leaves)
#length of each path(also can use diameter)
len_tree<-sapply(paths,function(x) length(x)) #length of all paths from root to leaves
max_len_tree<-max(len_tree) #longest length of all paths from root to leaves
#return local-wealth of a layer
local_wealth<-function(layer,mat,tau,root_wealth){
out<-tau*(1-tau)^(layer)*(mat %^% (layer))[root,]
return(out[which(out!=0)]*root_wealth)#
}
#iterate through values(replace 0.2 with tau and root_wealth should be an attribute of a vertex)
#you can make this more elegant, also profile extending list and all
wealth_list <- as.list(1:(max_len_tree)-1 ) #different values of layer
#subtract<-function(x){return(x-1)}
#wealth_list<-lapply(wealth_list,subtract)
wealth_layers<-sapply(wealth_list[-1],FUN=local_wealth,mat=tree_mat,tau=tau,root_wealth=root_wealth)
x<-tau*root_wealth
names(x)<-V(tree)$name[1] #root can also use tree$root_name
wealth_list[-1]<-wealth_layers
wealth_list[[1]]<-x
tree$wealth_list<-wealth_list
if(is.null(V(tree)$wealth_vector_trickle)){
V(tree)$wealth_vector_trickle<-unlist(wealth_list)
}else{
V(tree)$wealth_vector_trickle<-V(tree)$wealth_vector_trickle+unlist(wealth_list)
}
tree$wealth_resid<-root_wealth-sum(V(tree)$wealth_vector_trickle)
return(tree)
#recycle wealth until it is used up(not particularly important)
#target branch attributions
}