-
Notifications
You must be signed in to change notification settings - Fork 24
/
inferGraphLaplacian.py
1314 lines (1219 loc) · 56.7 KB
/
inferGraphLaplacian.py
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (c) 2015, Stanford University. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from snap import *
from cvxpy import *
import math
import multiprocessing
import numpy
from scipy.sparse import lil_matrix
import sys
import time
import __builtin__
import code
# File format: One edge per line, written as "srcID dstID"
# Commented lines that start with '#' are ignored
# Returns a TGraphVX object with the designated edges and nodes
def LoadEdgeList(Filename):
gvx = TGraphVX()
nids = set()
infile = open(Filename, 'r')
with open(Filename) as infile:
for line in infile:
if line.startswith('#'): continue
[src, dst] = line.split()
if int(src) not in nids:
gvx.AddNode(int(src))
nids.add(int(src))
if int(dst) not in nids:
gvx.AddNode(int(dst))
nids.add(int(dst))
gvx.AddEdge(int(src), int(dst))
return gvx
# TGraphVX inherits from the TUNGraph object defined by Snap.py
class TGraphVX(TUNGraph):
__default_objective = norm(0)
__default_constraints = []
# Data Structures
# ---------------
# node_objectives = {int NId : CVXPY Expression}
# node_constraints = {int NId : [CVXPY Constraint]}
# edge_objectives = {(int NId1, int NId2) : CVXPY Expression}
# edge_constraints = {(int NId1, int NId2) : [CVXPY Constraint]}
# all_variables = set(CVXPY Variable)
#
# ADMM-Specific Structures
# ------------------------
# node_variables = {int NId :
# [(CVXPY Variable id, CVXPY Variable name, CVXPY Variable, offset)]}
# node_values = {int NId : numpy array}
# node_values points to the numpy array containing the value of the entire
# variable space corresponding to then node. Use the offset to get the
# value for a specific variable.
#
# Constructor
# If Graph is a Snap.py graph, initializes a SnapVX graph with the same
# nodes and edges.
def __init__(self, Graph=None):
# Initialize data structures
self.node_objectives = {}
self.node_variables = {}
self.node_constraints = {}
self.edge_objectives = {}
self.edge_constraints = {}
self.node_values = {}
self.all_variables = set()
self.status = None
self.value = None
# Initialize superclass
nodes = 0
edges = 0
if Graph != None:
nodes = Graph.GetNodes()
edges = Graph.GetEdges()
TUNGraph.__init__(self, nodes, edges)
# Support for constructor with Snap.py graph argument
if Graph != None:
for ni in Graph.Nodes():
self.AddNode(ni.GetId())
for ei in Graph.Edges():
self.AddEdge(ei.GetSrcNId(), ei.GetDstNId())
# Simple iterator to iterator over all nodes in graph. Similar in
# functionality to Nodes() iterator of PUNGraph in Snap.py.
def Nodes(self):
ni = TUNGraph.BegNI(self)
for i in xrange(TUNGraph.GetNodes(self)):
yield ni
ni.Next()
# Simple iterator to iterator over all edge in graph. Similar in
# functionality to Edges() iterator of PUNGraph in Snap.py.
def Edges(self):
ei = TUNGraph.BegEI(self)
for i in xrange(TUNGraph.GetEdges(self)):
yield ei
ei.Next()
# Adds objectives together to form one collective CVXPY Problem.
# Option of specifying Maximize() or the default Minimize().
# Graph status and value properties will also be set.
# Individual variable values can be retrieved using GetNodeValue().
# Option to use serial version or distributed ADMM.
# maxIters optional parameter: Maximum iterations for distributed ADMM.
def Solve(self, M=Minimize, UseADMM=True, NumProcessors=0, Rho=1.0,
MaxIters=250, EpsAbs=0.01, EpsRel=0.01, Verbose=False,
UseClustering = False, ClusterSize = 1000 ):
global m_func
m_func = M
# Use ADMM if the appropriate parameter is specified and if there
# are edges in the graph.
#if __builtin__.len(SuperNodes) > 0:
if UseClustering and ClusterSize > 0:
SuperNodes = self.__ClusterGraph(ClusterSize)
self.__SolveClusterADMM(M,UseADMM,SuperNodes, NumProcessors, Rho, MaxIters,\
EpsAbs, EpsRel, Verbose)
return
if UseADMM and self.GetEdges() != 0:
self.__SolveADMM(NumProcessors, Rho, MaxIters, EpsAbs, EpsRel,
Verbose)
return
if Verbose:
print 'Serial ADMM'
objective = 0
constraints = []
# Add all node objectives and constraints
for ni in self.Nodes():
nid = ni.GetId()
objective += self.node_objectives[nid]
constraints += self.node_constraints[nid]
# Add all edge objectives and constraints
for ei in self.Edges():
etup = self.__GetEdgeTup(ei.GetSrcNId(), ei.GetDstNId())
objective += self.edge_objectives[etup]
constraints += self.edge_constraints[etup]
# Solve CVXPY Problem
objective = m_func(objective)
problem = Problem(objective, constraints)
try:
problem.solve()
except SolverError:
problem.solve(solver=SCS)
if problem.status in [INFEASIBLE_INACCURATE, UNBOUNDED_INACCURATE]:
problem.solve(solver=SCS)
# Set TGraphVX status and value to match CVXPY
self.status = problem.status
self.value = problem.value
# Insert into hash to support ADMM structures and GetNodeValue()
for ni in self.Nodes():
nid = ni.GetId()
variables = self.node_variables[nid]
value = None
for (varID, varName, var, offset) in variables:
if var.size[0] == 1:
val = numpy.array([var.value])
else:
val = numpy.array(var.value).reshape(-1,)
if value is None:
value = val
else:
value = numpy.concatenate((value, val))
self.node_values[nid] = value
"""Function to solve cluster wise optimization problem"""
def __SolveClusterADMM(self,M,UseADMM,superNodes,numProcessors, rho_param,
maxIters, eps_abs, eps_rel,verbose):
#initialize an empty supergraph
supergraph = TGraphVX()
nidToSuperidMap = {}
edgeToClusterTupMap = {}
for snid in xrange(__builtin__.len(superNodes)):
for nid in superNodes[snid]:
nidToSuperidMap[nid] = snid
"""collect the entities for the supergraph. a supernode is a subgraph. a superedge
is a representation of a graph cut"""
superEdgeObjectives = {}
superEdgeConstraints = {}
superNodeObjectives = {}
superNodeConstraints = {}
superNodeVariables = {}
superNodeValues = {}
varToSuperVarMap = {}
"""traverse through the list of edges and add each edge's constraint and objective to
either the supernode to which it belongs or the superedge which connects the ends
of the supernodes to which it belongs"""
for ei in self.Edges():
etup = self.__GetEdgeTup(ei.GetSrcNId(), ei.GetDstNId())
supersrcnid,superdstnid = nidToSuperidMap[etup[0]],nidToSuperidMap[etup[1]]
if supersrcnid != superdstnid: #the edge is a part of the cut
if supersrcnid > superdstnid:
supersrcnid,superdstnid = superdstnid,supersrcnid
if (supersrcnid,superdstnid) not in superEdgeConstraints:
superEdgeConstraints[(supersrcnid,superdstnid)] = self.edge_constraints[etup]
superEdgeObjectives[(supersrcnid,superdstnid)] = self.edge_objectives[etup]
else:
superEdgeConstraints[(supersrcnid,superdstnid)] += self.edge_constraints[etup]
superEdgeObjectives[(supersrcnid,superdstnid)] += self.edge_objectives[etup]
else: #the edge is a part of some supernode
if supersrcnid not in superNodeConstraints:
superNodeConstraints[supersrcnid] = self.edge_constraints[etup]
superNodeObjectives[supersrcnid] = self.edge_objectives[etup]
else:
superNodeConstraints[supersrcnid] += self.edge_constraints[etup]
superNodeObjectives[supersrcnid] += self.edge_objectives[etup]
for ni in self.Nodes():
nid = ni.GetId()
supernid = nidToSuperidMap[nid]
value = None
for (varID, varName, var, offset) in self.node_variables[nid]:
if var.size[0] == 1:
val = numpy.array([var.value])
else:
val = numpy.array(var.value).reshape(-1,)
if not value:
value = val
else:
value = numpy.concatenate((value, val))
if supernid not in superNodeConstraints:
superNodeObjectives[supernid] = self.node_objectives[nid]
superNodeConstraints[supernid] = self.node_constraints[nid]
else:
superNodeObjectives[supernid] += self.node_objectives[nid]
superNodeConstraints[supernid] += self.node_constraints[nid]
for ( varId, varName, var, offset) in self.node_variables[nid]:
superVarName = varName+str(varId)
varToSuperVarMap[(nid,varName)] = (supernid,superVarName)
if supernid not in superNodeVariables:
superNodeVariables[supernid] = [(varId, superVarName, var, offset)]
superNodeValues[supernid] = value
else:
superNodeOffset = sum([superNodeVariables[supernid][k][2].size[0]* \
superNodeVariables[supernid][k][2].size[1]\
for k in xrange(__builtin__.len(superNodeVariables[supernid])) ])
superNodeVariables[supernid] += [(varId, superVarName, var, superNodeOffset)]
superNodeValues[supernid] = numpy.concatenate((superNodeValues[supernid],value))
#add all supernodes to the supergraph
for supernid in superNodeConstraints:
supergraph.AddNode(supernid, superNodeObjectives[supernid], \
superNodeConstraints[supernid])
supergraph.node_variables[supernid] = superNodeVariables[supernid]
supergraph.node_values[supernid] = superNodeValues[supernid]
#add all superedges to the supergraph
for superei in superEdgeConstraints:
superSrcId,superDstId = superei
supergraph.AddEdge(superSrcId, superDstId, None,\
superEdgeObjectives[superei],\
superEdgeConstraints[superei])
#call solver for this supergraph
if UseADMM and supergraph.GetEdges() != 0:
supergraph.__SolveADMM(numProcessors, rho_param, maxIters, eps_abs, eps_rel, verbose)
else:
supergraph.Solve(M, False, numProcessors, rho_param, maxIters, eps_abs, eps_rel, verbose,
UseClustering=False)
self.status = supergraph.status
self.value = supergraph.value
for ni in self.Nodes():
nid = ni.GetId()
snid = nidToSuperidMap[nid]
self.node_values[nid] = []
for ( varId, varName, var, offset) in self.node_variables[nid]:
superVarName = varToSuperVarMap[(nid,varName)]
self.node_values[nid] = numpy.concatenate((self.node_values[nid],\
supergraph.GetNodeValue(snid, superVarName[1])))
# Implementation of distributed ADMM
# Uses a global value of rho_param for rho
# Will run for a maximum of maxIters iterations
def __SolveADMM(self, numProcessors, rho_param, maxIters, eps_abs, eps_rel,
verbose):
global node_vals, edge_z_vals, edge_u_vals, rho
global getValue, rho_update_func
if numProcessors <= 0:
num_processors = multiprocessing.cpu_count()
else:
num_processors = numProcessors
rho = rho_param
if verbose:
print 'Distributed ADMM (%d processors)' % num_processors
# Organize information for each node in helper node_info structure
node_info = {}
# Keeps track of the current offset necessary into the shared node
# values Array
length = 0
for ni in self.Nodes():
nid = ni.GetId()
deg = ni.GetDeg()
obj = self.node_objectives[nid]
variables = self.node_variables[nid]
con = self.node_constraints[nid]
neighbors = [ni.GetNbrNId(j) for j in xrange(deg)]
# Node's constraints include those imposed by edges
for neighborId in neighbors:
etup = self.__GetEdgeTup(nid, neighborId)
econ = self.edge_constraints[etup]
con += econ
# Calculate sum of dimensions of all Variables for this node
size = sum([var.size[0] for (varID, varName, var, offset) in variables])
# Nearly complete information package for this node
node_info[nid] = (nid, obj, variables, con, length, size, deg,\
neighbors)
length += size
node_vals = multiprocessing.Array('d', [0.0] * length)
x_length = length
# Organize information for each node in final edge_list structure and
# also helper edge_info structure
edge_list = []
edge_info = {}
# Keeps track of the current offset necessary into the shared edge
# values Arrays
length = 0
for ei in self.Edges():
etup = self.__GetEdgeTup(ei.GetSrcNId(), ei.GetDstNId())
obj = self.edge_objectives[etup]
con = self.edge_constraints[etup]
con += self.node_constraints[etup[0]] +\
self.node_constraints[etup[1]]
# Get information for each endpoint node
info_i = node_info[etup[0]]
info_j = node_info[etup[1]]
ind_zij = length
ind_uij = length
length += info_i[X_LEN]
ind_zji = length
ind_uji = length
length += info_j[X_LEN]
# Information package for this edge
tup = (etup, obj, con,\
info_i[X_VARS], info_i[X_LEN], info_i[X_IND], ind_zij, ind_uij,\
info_j[X_VARS], info_j[X_LEN], info_j[X_IND], ind_zji, ind_uji)
edge_list.append(tup)
edge_info[etup] = tup
edge_z_vals = multiprocessing.Array('d', [0.0] * length)
edge_u_vals = multiprocessing.Array('d', [0.0] * length)
z_length = length
# Populate sparse matrix A.
# A has dimensions (p, n), where p is the length of the stacked vector
# of node variables, and n is the length of the stacked z vector of
# edge variables.
# Each row of A has one 1. There is a 1 at (i,j) if z_i = x_j.
A = lil_matrix((z_length, x_length), dtype=numpy.int8)
for ei in self.Edges():
etup = self.__GetEdgeTup(ei.GetSrcNId(), ei.GetDstNId())
info_edge = edge_info[etup]
info_i = node_info[etup[0]]
info_j = node_info[etup[1]]
for offset in xrange(info_i[X_LEN]):
row = info_edge[Z_ZIJIND] + offset
col = info_i[X_IND] + offset
A[row, col] = 1
for offset in xrange(info_j[X_LEN]):
row = info_edge[Z_ZJIIND] + offset
col = info_j[X_IND] + offset
A[row, col] = 1
A_tr = A.transpose()
# Create final node_list structure by adding on information for
# node neighbors
node_list = []
for nid, info in node_info.iteritems():
entry = [nid, info[X_OBJ], info[X_VARS], info[X_CON], info[X_IND],\
info[X_LEN], info[X_DEG]]
# Append information about z- and u-value indices for each
# node neighbor
for i in xrange(info[X_DEG]):
neighborId = info[X_NEIGHBORS][i]
indices = (Z_ZIJIND, Z_UIJIND) if nid < neighborId else\
(Z_ZJIIND, Z_UJIIND)
einfo = edge_info[self.__GetEdgeTup(nid, neighborId)]
entry.append(einfo[indices[0]])
entry.append(einfo[indices[1]])
node_list.append(entry)
pool = multiprocessing.Pool(num_processors)
num_iterations = 0
z_old = getValue(edge_z_vals, 0, z_length)
# Proceed until convergence criteria are achieved or the maximum
# number of iterations has passed
while num_iterations <= maxIters:
# Check convergence criteria
if num_iterations != 0:
x = getValue(node_vals, 0, x_length)
z = getValue(edge_z_vals, 0, z_length)
u = getValue(edge_u_vals, 0, z_length)
# Determine if algorithm should stop. Retrieve primal and dual
# residuals and thresholds
stop, res_pri, e_pri, res_dual, e_dual =\
self.__CheckConvergence(A, A_tr, x, z, z_old, u, rho,\
x_length, z_length,
eps_abs, eps_rel, verbose)
if stop: break
z_old = z
# Update rho and scale u-values
rho_new = rho_update_func(rho, res_pri, e_pri, res_dual, e_dual)
scale = float(rho) / rho_new
edge_u_vals[:] = [i * scale for i in edge_u_vals]
rho = rho_new
num_iterations += 1
if verbose:
# Debugging information prints current iteration #
print 'Iteration %d' % num_iterations
pool.map(ADMM_x, node_list)
pool.map(ADMM_z, edge_list)
pool.map(ADMM_u, edge_list)
pool.close()
pool.join()
# Insert into hash to support GetNodeValue()
for entry in node_list:
nid = entry[X_NID]
index = entry[X_IND]
size = entry[X_LEN]
self.node_values[nid] = getValue(node_vals, index, size)
# Set TGraphVX status and value to match CVXPY
if num_iterations <= maxIters:
self.status = 'Optimal'
else:
self.status = 'Incomplete: max iterations reached'
self.value = self.GetTotalProblemValue()
# Iterate through all variables and update values.
# Sum all objective values over all nodes and edges.
def GetTotalProblemValue(self):
global getValue
result = 0.0
for ni in self.Nodes():
nid = ni.GetId()
for (varID, varName, var, offset) in self.node_variables[nid]:
var.value = self.GetNodeValue(nid, varName)
for ni in self.Nodes():
result += self.node_objectives[ni.GetId()].value
for ei in self.Edges():
etup = self.__GetEdgeTup(ei.GetSrcNId(), ei.GetDstNId())
result += self.edge_objectives[etup].value
return result
# Returns True if convergence criteria have been satisfied
# eps_abs = eps_rel = 0.01
# r = Ax - z
# s = rho * (A^T)(z - z_old)
# e_pri = sqrt(p) * e_abs + e_rel * max(||Ax||, ||z||)
# e_dual = sqrt(n) * e_abs + e_rel * ||rho * (A^T)u||
# Should stop if (||r|| <= e_pri) and (||s|| <= e_dual)
# Returns (boolean shouldStop, primal residual value, primal threshold,
# dual residual value, dual threshold)
def __CheckConvergence(self, A, A_tr, x, z, z_old, u, rho, p, n,
e_abs, e_rel, verbose):
norm = numpy.linalg.norm
Ax = A.dot(x)
r = Ax - z
s = rho * A_tr.dot(z - z_old)
# Primal and dual thresholds. Add .0001 to prevent the case of 0.
e_pri = math.sqrt(p) * e_abs + e_rel * max(norm(Ax), norm(z)) + .0001
e_dual = math.sqrt(n) * e_abs + e_rel * norm(rho * A_tr.dot(u)) + .0001
# Primal and dual residuals
res_pri = norm(r)
res_dual = norm(s)
if verbose:
# Debugging information to print convergence criteria values
print ' r:', res_pri
print ' e_pri:', e_pri
print ' s:', res_dual
print ' e_dual:', e_dual
stop = (res_pri <= e_pri) and (res_dual <= e_dual)
return (stop, res_pri, e_pri, res_dual, e_dual)
# API to get node Variable value after solving with ADMM.
def GetNodeValue(self, NId, Name):
self.__VerifyNId(NId)
for (varID, varName, var, offset) in self.node_variables[NId]:
if varName == Name:
offset = offset
value = self.node_values[NId]
return value[offset:(offset + var.size[0])]
return None
# Prints value of all node variables to console or file, if given
def PrintSolution(self, Filename=None):
numpy.set_printoptions(linewidth=numpy.inf)
out = sys.stdout if (Filename == None) else open(Filename, 'w+')
out.write('Status: %s\n' % self.status)
out.write('Total Objective: %f\n' % self.value)
for ni in self.Nodes():
nid = ni.GetId()
s = 'Node %d:\n' % nid
out.write(s)
for (varID, varName, var, offset) in self.node_variables[nid]:
val = numpy.transpose(self.GetNodeValue(nid, varName))
s = ' %s %s\n' % (varName, str(val))
out.write(s)
# Helper method to verify existence of an NId.
def __VerifyNId(self, NId):
if not TUNGraph.IsNode(self, NId):
raise Exception('Node %d does not exist.' % NId)
# Helper method to determine if
def __UpdateAllVariables(self, NId, Objective):
if NId in self.node_objectives:
# First, remove the Variables from the old Objective.
old_obj = self.node_objectives[NId]
self.all_variables = self.all_variables - set(old_obj.variables())
# Check that the Variables of the new Objective are not currently
# in other Objectives.
new_variables = set(Objective.variables())
if __builtin__.len(self.all_variables.intersection(new_variables)) != 0:
raise Exception('Objective at NId %d shares a variable.' % NId)
self.all_variables = self.all_variables | new_variables
# Helper method to get CVXPY Variables out of a CVXPY Objective
def __ExtractVariableList(self, Objective):
l = [(var.name(), var) for var in Objective.variables()]
# Sort in ascending order by name
l.sort(key=lambda t: t[0])
l2 = []
offset = 0
for (varName, var) in l:
# Add tuples of the form (id, name, object, offset)
l2.append((var.id, varName, var, offset))
offset += var.size[0]
return l2
# Adds a Node to the TUNGraph and stores the corresponding CVX information.
def AddNode(self, NId, Objective=__default_objective,\
Constraints=__default_constraints):
self.__UpdateAllVariables(NId, Objective)
self.node_objectives[NId] = Objective
self.node_variables[NId] = self.__ExtractVariableList(Objective)
self.node_constraints[NId] = Constraints
return TUNGraph.AddNode(self, NId)
def SetNodeObjective(self, NId, Objective):
self.__VerifyNId(NId)
self.__UpdateAllVariables(NId, Objective)
self.node_objectives[NId] = Objective
self.node_variables[NId] = self.__ExtractVariableList(Objective)
def GetNodeObjective(self, NId):
self.__VerifyNId(NId)
return self.node_objectives[NId]
def SetNodeConstraints(self, NId, Constraints):
self.__VerifyNId(NId)
self.node_constraints[NId] = Constraints
def GetNodeConstraints(self, NId):
self.__VerifyNId(NId)
return self.node_constraints[NId]
# Helper method to get a tuple representing an edge. The smaller NId
# goes first.
def __GetEdgeTup(self, NId1, NId2):
return (NId1, NId2) if NId1 < NId2 else (NId2, NId1)
# Helper method to verify existence of an edge.
def __VerifyEdgeTup(self, ETup):
if not TUNGraph.IsEdge(self, ETup[0], ETup[1]):
raise Exception('Edge {%d,%d} does not exist.' % ETup)
# Adds an Edge to the TUNGraph and stores the corresponding CVX information.
# obj_func is a function which accepts two arguments, a dictionary of
# variables for the source and destination nodes
# { string varName : CVXPY Variable }
# obj_func should return a tuple of (objective, constraints), although
# it will assume a singleton object will be an objective and will use
# the default constraints.
# If obj_func is None, then will use Objective and Constraints, which are
# parameters currently set to defaults.
def AddEdge(self, SrcNId, DstNId, ObjectiveFunc=None,
Objective=__default_objective, Constraints=__default_constraints):
ETup = self.__GetEdgeTup(SrcNId, DstNId)
if ObjectiveFunc != None:
src_vars = self.GetNodeVariables(SrcNId)
dst_vars = self.GetNodeVariables(DstNId)
ret = ObjectiveFunc(src_vars, dst_vars)
if type(ret) is tuple:
# Tuple = assume we have (objective, constraints)
self.edge_objectives[ETup] = ret[0]
self.edge_constraints[ETup] = ret[1]
else:
# Singleton object = assume it is the objective
self.edge_objectives[ETup] = ret
self.edge_constraints[ETup] = self.__default_constraints
else:
self.edge_objectives[ETup] = Objective
self.edge_constraints[ETup] = Constraints
return TUNGraph.AddEdge(self, SrcNId, DstNId)
def SetEdgeObjective(self, SrcNId, DstNId, Objective):
ETup = self.__GetEdgeTup(SrcNId, DstNId)
self.__VerifyEdgeTup(ETup)
self.edge_objectives[ETup] = Objective
def GetEdgeObjective(self, SrcNId, DstNId):
ETup = self.__GetEdgeTup(SrcNId, DstNId)
self.__VerifyEdgeTup(ETup)
return self.edge_objectives[ETup]
def SetEdgeConstraints(self, SrcNId, DstNId, Constraints):
ETup = self.__GetEdgeTup(SrcNId, DstNId)
self.__VerifyEdgeTup(ETup)
self.edge_constraints[ETup] = Constraints
def GetEdgeConstraints(self, SrcNId, DstNId):
ETup = self.__GetEdgeTup(SrcNId, DstNId)
self.__VerifyEdgeTup(ETup)
return self.edge_constraints[ETup]
# Returns a dictionary of all variables corresponding to a node.
# { string name : CVXPY Variable }
# This can be used in place of bulk loading functions to recover necessary
# Variables for an edge.
def GetNodeVariables(self, NId):
self.__VerifyNId(NId)
d = {}
for (varID, varName, var, offset) in self.node_variables[NId]:
d[varName] = var
return d
# Bulk loading for nodes
# ObjFunc is a function which accepts one argument, an array of strings
# parsed from the given CSV filename
# ObjFunc should return a tuple of (objective, constraints), although
# it will assume a singleton object will be an objective
# Optional parameter NodeIDs allows the user to pass in a list specifying,
# in order, the node IDs that correspond to successive rows
# If NodeIDs is None, then the file must have a column denoting the
# node ID for each row. The index of this column (0-indexed) is IdCol.
# If NodeIDs and IdCol are both None, then will iterate over all Nodes, in
# order, as long as the file lasts
def AddNodeObjectives(self, Filename, ObjFunc, NodeIDs=None, IdCol=None):
infile = open(Filename, 'r')
if NodeIDs == None and IdCol == None:
stop = False
for ni in self.Nodes():
nid = ni.GetId()
while True:
line = infile.readline()
if line == '': stop = True
if not line.startswith('#'): break
if stop: break
data = [x.strip() for x in line.split(',')]
ret = ObjFunc(data)
if type(ret) is tuple:
# Tuple = assume we have (objective, constraints)
self.SetNodeObjective(nid, ret[0])
self.SetNodeConstraints(nid, ret[1])
else:
# Singleton object = assume it is the objective
self.SetNodeObjective(nid, ret)
if NodeIDs == None:
for line in infile:
if line.startswith('#'): continue
data = [x.strip() for x in line.split(',')]
ret = ObjFunc(data)
if type(ret) is tuple:
# Tuple = assume we have (objective, constraints)
self.SetNodeObjective(int(data[IdCol]), ret[0])
self.SetNodeConstraints(int(data[IdCol]), ret[1])
else:
# Singleton object = assume it is the objective
self.SetNodeObjective(int(data[IdCol]), ret)
else:
for nid in NodeIDs:
while True:
line = infile.readline()
if line == '':
raise Exception('File %s is too short.' % filename)
if not line.startswith('#'): break
data = [x.strip() for x in line.split(',')]
ret = ObjFunc(data)
if type(ret) is tuple:
# Tuple = assume we have (objective, constraints)
self.SetNodeObjective(nid, ret[0])
self.SetNodeConstraints(nid, ret[1])
else:
# Singleton object = assume it is the objective
self.SetNodeObjective(nid, ret)
infile.close()
# Bulk loading for edges
# If Filename is None:
# ObjFunc is a function which accepts three arguments, a dictionary of
# variables for the source and destination nodes, and an unused param
# { string varName : CVXPY Variable } x2, None
# ObjFunc should return a tuple of (objective, constraints), although
# it will assume a singleton object will be an objective
# If Filename exists:
# ObjFunc is the same, except the third param will be be an array of
# strings parsed from the given CSV filename
# Optional parameter EdgeIDs allows the user to pass in a list specifying,
# in order, the EdgeIDs that correspond to successive rows. An edgeID is
# a tuple of (srcID, dstID).
# If EdgeIDs is None, then the file may have columns denoting the srcID and
# dstID for each row. The indices of these columns are 0-indexed.
# If EdgeIDs and id columns are None, then will iterate through all edges
# in order, as long as the file lasts.
def AddEdgeObjectives(self, ObjFunc, Filename=None, EdgeIDs=None,\
SrcIdCol=None, DstIdCol=None):
if Filename == None:
for ei in self.Edges():
src_id = ei.GetSrcNId()
src_vars = self.GetNodeVariables(src_id)
dst_id = ei.GetDstNId()
dst_vars = self.GetNodeVariables(dst_id)
ret = ObjFunc(src_vars, dst_vars, None)
if type(ret) is tuple:
# Tuple = assume we have (objective, constraints)
self.SetEdgeObjective(src_id, dst_id, ret[0])
self.SetEdgeConstraints(src_id, dst_id, ret[1])
else:
# Singleton object = assume it is the objective
self.SetEdgeObjective(src_id, dst_id, ret)
return
infile = open(Filename, 'r')
if EdgeIDs == None and (SrcIdCol == None or DstIdCol == None):
stop = False
for ei in self.Edges():
src_id = ei.GetSrcNId()
src_vars = self.GetNodeVariables(src_id)
dst_id = ei.GetDstNId()
dst_vars = self.GetNodeVariables(dst_id)
while True:
line = infile.readline()
if line == '': stop = True
if not line.startswith('#'): break
if stop: break
data = [x.strip() for x in line.split(',')]
ret = ObjFunc(src_vars, dst_vars, data)
if type(ret) is tuple:
# Tuple = assume we have (objective, constraints)
self.SetEdgeObjective(src_id, dst_id, ret[0])
self.SetEdgeConstraints(src_id, dst_id, ret[1])
else:
# Singleton object = assume it is the objective
self.SetEdgeObjective(src_id, dst_id, ret)
if EdgeIDs == None:
for line in infile:
if line.startswith('#'): continue
data = [x.strip() for x in line.split(',')]
src_id = int(data[SrcIdCol])
dst_id = int(data[DstIdCol])
src_vars = self.GetNodeVariables(src_id)
dst_vars = self.GetNodeVariables(dst_id)
ret = ObjFunc(src_vars, dst_vars, data)
if type(ret) is tuple:
# Tuple = assume we have (objective, constraints)
self.SetEdgeObjective(src_id, dst_id, ret[0])
self.SetEdgeConstraints(src_id, dst_id, ret[1])
else:
# Singleton object = assume it is the objective
self.SetEdgeObjective(src_id, dst_id, ret)
else:
for edgeID in EdgeIDs:
etup = self.__GetEdgeTup(edgeID[0], edgeID[1])
while True:
line = infile.readline()
if line == '':
raise Exception('File %s is too short.' % Filename)
if not line.startswith('#'): break
data = [x.strip() for x in line.split(',')]
src_vars = self.GetNodeVariables(etup[0])
dst_vars = self.GetNodeVariables(etup[1])
ret = ObjFunc(src_vars, dst_vars, data)
if type(ret) is tuple:
# Tuple = assume we have (objective, constraints)
self.SetEdgeObjective(etup[0], etup[1], ret[0])
self.SetEdgeConstraints(etup[0], etup[1], ret[1])
else:
# Singleton object = assume it is the objective
self.SetEdgeObjective(etup[0], etup[1], ret)
infile.close()
"""return clusters of nodes of the original graph.Each cluster corresponds to
a supernode in the supergraph"""
def __ClusterGraph(self,clusterSize):
#obtain a random shuffle of the nodes
nidArray = [ni.GetId() for ni in self.Nodes()]
numpy.random.shuffle(nidArray)
visitedNode = {}
for nid in nidArray:
visitedNode[nid] = False
superNodes = []
superNode,superNodeSize = [],0
for nid in nidArray:
if not visitedNode[nid]:
oddLevel, evenLevel, isOdd = [],[],True
oddLevel.append(nid)
visitedNode[nid] = True
#do a level order traversal and add nodes to the superNode until the
#size of the supernode variables gets larger than clusterSize
while True:
if isOdd:
if __builtin__.len(oddLevel) > 0:
while __builtin__.len(oddLevel) > 0:
topId = oddLevel.pop(0)
node = TUNGraph.GetNI(self,topId)
varSize = sum([variable[2].size[0]* \
variable[2].size[1]\
for variable in self.node_variables[topId]])
if varSize + superNodeSize <= clusterSize:
superNode.append(topId)
superNodeSize = varSize + superNodeSize
else:
if __builtin__.len(superNode) > 0:
superNodes.append(superNode)
superNodeSize = varSize
superNode = [topId]
neighbors = [node.GetNbrNId(j) \
for j in xrange(node.GetDeg())]
for nbrId in neighbors:
if not visitedNode[nbrId]:
evenLevel.append(nbrId)
visitedNode[nbrId] = True
isOdd = False
#sort the nodes according to their variable size
if __builtin__.len(evenLevel) > 0:
evenLevel.sort(key=lambda nid : sum([variable[2].size[0]* \
variable[2].size[1] for variable \
in self.node_variables[nid]]))
else:
break
else:
if __builtin__.len(evenLevel) > 0:
while __builtin__.len(evenLevel) > 0:
topId = evenLevel.pop(0)
node = TUNGraph.GetNI(self,topId)
varSize = sum([variable[2].size[0]* \
variable[2].size[1]\
for variable in self.node_variables[topId]])
if varSize + superNodeSize <= clusterSize:
superNode.append(topId)
superNodeSize = varSize + superNodeSize
else:
if __builtin__.len(superNode) > 0:
superNodes.append(superNode)
superNodeSize = varSize
superNode = [topId]
neighbors = [node.GetNbrNId(j) \
for j in xrange(node.GetDeg())]
for nbrId in neighbors:
if not visitedNode[nbrId]:
oddLevel.append(nbrId)
visitedNode[nbrId] = True
isOdd = True
#sort the nodes according to their variable size
if __builtin__.len(oddLevel) > 0:
oddLevel.sort(key=lambda nid : sum([variable[2].size[0]* \
variable[2].size[1] for variable \
in self.node_variables[nid]]))
else:
break
if superNode not in superNodes:
superNodes.append(superNode)
return superNodes
## ADMM Global Variables and Functions ##
# By default, the objective function is Minimize().
__default_m_func = Minimize
m_func = __default_m_func
# By default, rho is 1.0. Default rho update is identity function and does not
# depend on primal or dual residuals or thresholds.
__default_rho = 1.0
__default_rho_update_func = lambda rho, res_p, thr_p, res_d, thr_d: rho
rho = __default_rho
# Rho update function takes 5 parameters
# - Old value of rho
# - Primal residual and threshold
# - Dual residual and threshold
rho_update_func = __default_rho_update_func
def SetRho(Rho=None):
global rho
rho = Rho if Rho else __default_rho
# Rho update function should take one parameter: old_rho
# Returns new_rho
# This function will be called at the end of every iteration
def SetRhoUpdateFunc(Func=None):
global rho_update_func
rho_update_func = Func if Func else __default_rho_update_func
# Tuple of indices to identify the information package for each node. Actual
# length of specific package (list) may vary depending on node degree.
# X_NID: Node ID
# X_OBJ: CVXPY Objective
# X_VARS: CVXPY Variables (entry from node_variables structure)
# X_CON: CVXPY Constraints
# X_IND: Starting index into shared node_vals Array
# X_LEN: Total length (sum of dimensions) of all variables
# X_DEG: Number of neighbors
# X_NEIGHBORS: Placeholder for information about each neighbors
# Information for each neighbor is two entries, appended in order.
# Starting index of the corresponding z-value in edge_z_vals. Then for u.
(X_NID, X_OBJ, X_VARS, X_CON, X_IND, X_LEN, X_DEG, X_NEIGHBORS) = range(8)
# Tuple of indices to identify the information package for each edge.
# Z_EID: Edge ID / tuple
# Z_OBJ: CVXPY Objective
# Z_CON: CVXPY Constraints
# Z_[IJ]VARS: CVXPY Variables for Node [ij] (entry from node_variables)
# Z_[IJ]LEN: Total length (sum of dimensions) of all variables for Node [ij]
# Z_X[IJ]IND: Starting index into shared node_vals Array for Node [ij]
# Z_Z[IJ|JI]IND: Starting index into shared edge_z_vals Array for edge [ij|ji]
# Z_U[IJ|JI]IND: Starting index into shared edge_u_vals Array for edge [ij|ji]
(Z_EID, Z_OBJ, Z_CON, Z_IVARS, Z_ILEN, Z_XIIND, Z_ZIJIND, Z_UIJIND,\
Z_JVARS, Z_JLEN, Z_XJIND, Z_ZJIIND, Z_UJIIND) = range(13)
# Contain all x, z, and u values for each node and/or edge in ADMM. Use the
# given starting index and length with getValue() to get individual node values
node_vals = None
edge_z_vals = None
edge_u_vals = None
# Extract a numpy array value from a shared Array.
# Give shared array, starting index, and total length.
def getValue(arr, index, length):
return numpy.array(arr[index:(index + length)])
# Write value of numpy array nparr (with given length) to a shared Array at
# the given starting index.
def writeValue(sharedarr, index, nparr, length):
if length == 1:
nparr = [nparr]
sharedarr[index:(index + length)] = nparr
# Write the values for all of the Variables involved in a given Objective to
# the given shared Array.
# variables should be an entry from the node_values structure.
def writeObjective(sharedarr, index, objective, variables):
for v in objective.variables():
vID = v.id
value = v.value
# Find the tuple in variables with the same ID. Take the offset.
# If no tuple exists, then silently skip.
for (varID, varName, var, offset) in variables:
if varID == vID:
writeValue(sharedarr, index + offset, value, var.size[0])
break
# Proximal operators
def Prox_logdet(S, A, eta):
global rho
d, q = numpy.linalg.eigh(eta*A-S)
q = numpy.matrix(q)
X_var = ( 1/(2*eta) )*q*( numpy.diag(d + numpy.sqrt(numpy.square(d) + (4*eta)*numpy.ones(d.shape))) )*q.T
x_var = X_var[numpy.triu_indices(S.shape[1])] # extract upper triangular part as update variable
# print 'x_update = ',x_var
return numpy.matrix(x_var).T
def Prox_lasso(a_ij, a_ji, eta, NID_diff):
z_ij = numpy.copy(a_ij)
z_ji = numpy.copy(a_ji)
k = 0
ind = range(a_ij.shape[0])
n = int((-1 + numpy.sqrt(1+ 8*a_ij.shape[0]))/2)
for i in range(n,0,-1):
ind.remove(k)
k = k + i
if (NID_diff > 1):
z_ij[ind] = Prox_onenorm(a_ij[ind], eta)