-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTSDP_optimization.py
1766 lines (1440 loc) · 60.2 KB
/
TSDP_optimization.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
"""
Created on 3/17/2021
Author: Joost Berkhout (VU, email: joost.berkhout@vu.nl)
Description: Module with functions to perform TSDP optimization. Notation from
the article is used as much as possible.
"""
import math
import time
import numpy as np
import pulp
from constants import PRECISION, BINSEARCH_PRECISION, MAX_BINSEARCH_ITER
from utilities import (
check_feasibility_solution, calculate_norm, determine_d,
determine_z, determine_u, determine_l,
)
def find_gamma_for_x_iter_increase(
idx, list_rates, list_rates_thresholds,
list_threshold_deltas, x_iter, increase
):
# gamma is the increase of v[idx] if x_iter[idx] increases with 'increase'
# for notational convenience, get the info for index
rates = list_rates[idx]
thresholds = list_rates_thresholds[idx]
threshold_deltas = list_threshold_deltas[idx]
new_x_iter_val = x_iter[idx, 0] + increase
# find gamma
last_idx_full = np.sum(
thresholds < new_x_iter_val + PRECISION
) - 1 # index of largest threshold that is still < new_x_iter_val
if last_idx_full >= 0:
gamma = np.dot(
threshold_deltas[:last_idx_full + 1],
rates[:last_idx_full + 1]
)
gamma += (new_x_iter_val - thresholds[last_idx_full]) * rates[
last_idx_full + 1]
else:
gamma = increase * rates[last_idx_full + 1]
return gamma
def find_x_iter_increase_for_gamma(
idx, list_rates, list_rates_thresholds,
list_threshold_deltas, x_iter, gamma
):
# finds x_iter_increase that lets v[idx] increase by gamma
# get the index info
old_rates = np.array(list_rates[idx])
thresholds = np.array(list_rates_thresholds[idx])
threshold_deltas = np.array(list_threshold_deltas[idx])
x_iter_val = np.array(x_iter[idx, 0])
# hack so that we can deal with negative areas and positive areas in
# the same way: it is assumed that the gamma always corresponds to
# (or "covers") only positive rectangles, or negative rectangles
# (a rectangle [rect] is the area under the step-wise rates graph)
gamma = np.abs(gamma)
rates = np.abs(old_rates)
# determine x_iter_increase
rect_sizes = threshold_deltas * rates
cumsum_rect_sizes = np.cumsum(rect_sizes)
last_idx_full = np.sum(cumsum_rect_sizes <= gamma) - 1
# process fully covered rectangles
if last_idx_full >= 0:
x_iter_increase = thresholds[
last_idx_full
] - x_iter_val # of fully covered rectangles
remaining_gamma = gamma - cumsum_rect_sizes[last_idx_full]
else:
x_iter_increase = 0 # no fully covered rectangles
remaining_gamma = gamma
# process last partly covered rectangle
last_x_iter_increase = remaining_gamma / rates[last_idx_full + 1]
x_iter_increase += last_x_iter_increase # of the partly covered rectangle
# determine updated (upd) rates, thresholds, and threshold_deltas
# (useful when gamma increase is processed via x_iter_increase)
if last_idx_full >= 0:
upd_rates = old_rates[last_idx_full + 1:]
upd_thresholds = thresholds[last_idx_full + 1:]
upd_threshold_deltas = threshold_deltas[last_idx_full + 1:]
else:
upd_rates = old_rates
upd_thresholds = thresholds
upd_threshold_deltas = threshold_deltas
upd_threshold_deltas[0] -= last_x_iter_increase
return x_iter_increase, upd_rates, upd_thresholds, upd_threshold_deltas
def det_gammas_decreases(v_LB, v_iter, cut):
"""Function needed in Phase 1 of
min_norm_rank_1_pert_pres_stoch_consider_hist() that finds the gamma
decreases so that v_iter is made equal to cut. If v_iter is already smaller
than cut, it is ignored. If cut is smaller than v_LB, then v_iter will be
reduced to v_LB (below v_LB the rate becomes positive).
"""
# init
gammas = np.zeros((len(v_iter), 1))
# determine scenarios
fall_above = cut > v_iter # corresponding gammas remain zero
fall_below = cut < v_LB
fall_in = np.logical_not(np.logical_or(fall_above, fall_below))
# set gamma values
gammas[fall_below] = v_iter[fall_below] - v_LB[fall_below]
gammas[fall_in] = v_iter[fall_in] - cut
return gammas
def det_gammas_increase(v_iter, v_UB, cut):
"""Function needed in Phase 2 of
min_norm_rank_1_pert_pres_stoch_consider_hist() that finds the gamma
increases so that v_iter is made equal to cut. If v_iter is already larger
than cut, it is ignored. If cut is larger than v_LB, then v_iter will be
reduced to v_UB (below v_LB the rate becomes positive).
"""
# init
gammas = np.zeros((len(v_iter), 1))
# determine scenarios
fall_above = cut > v_UB
fall_below = cut < v_iter # corresponding gammas remain zero
fall_in = np.logical_not(np.logical_or(fall_above, fall_below))
# set gamma values
gammas[fall_above] = v_UB[fall_above] - v_iter[fall_above]
gammas[fall_in] = cut - v_iter[fall_in]
return gammas
def min_norm_rank_1_pert_pres_stoch_consider_hist(
G, mu, mu_goal, normtype='inf', u=None, DeltaHist=None
):
"""Under that assumption that
mu_goal.T u >= 1
it finds an explicit solution to:
min || DeltaHist + Delta ||_inf
s.t.
mu_goal^T (P + Delta) = mu_goal^T
P + Delta >= 0,
i.e., Problem (8.3) with the inf-norm. A similar approach is used as the
algorithms in Appendix D, with the difference that now also the "history of
previous perturbations" DeltaHist is taken into account. A Binary search
approach is used to speed up the computation.
Parameters
----------
G : np.array
Stochastic matrix.
mu : np.array
Stationary distribution of G.
mu_goal : np.array
Probability vector of the stationary distribution goal.
normtype : str
String indication the norm type to apply. Options: 'inf'.
u : np.array
The u vector from the article, see determine_u(). This can be
given so that it can be reused for speed reasons.
DeltaHist : np.array
Sum of historical Delta's.
Returns
-------
DeltaSol : np.array
The perturbation matrix.
"""
if DeltaHist is None:
return min_norm_rank_1_pert_pres_stoch(
G, mu, mu_goal, normtype=normtype, u=u
)
if u is None:
u = determine_u(G, mu_goal)
if mu_goal.T.dot(u) < 1 - PRECISION:
raise Exception(
'This function only works when mu_goal.T.dot(u) >= 1,'
f' its value is now {mu_goal.T.dot(u)}'
)
elif mu_goal.T.dot(u) == 1:
z, z_plus, z_min = determine_z(G, mu_goal)
DeltaSol = u.dot(z.T) / (mu_goal.T.dot(u))
return DeltaSol
elif normtype != 'inf':
raise NotImplementedError
# start construction of an optimal solution for inf-norm
# init
n = len(G)
z, z_plus, z_min = determine_z(G, mu_goal)
abs_z = np.abs(z)
z_neg = z < 0
z_pos = z > 0
"""
Notation used in the remaining function:
- x_iter: the current solution x
- v_iter: the current values of the rows in the objective function,
also denoted as v.
- gammas: the (TBD) decreases of v_iter in each iteration
"""
"""The following is a hack to solve the false assumption that x >= 0, which
should be x >= l if we take history into account. The following hack allows
to use a similar format as min_norm_rank_1_pert_pres_stoch().
The x_iter can now be thought as the extra mass that is given to l, i.e.,
"true x solution" = l + x_iter.
"""
l = determine_l(G, mu_goal)
DeltaHist = DeltaHist + l.dot(z.T)
u = u - l
goal_value = 1 - mu_goal.T.dot(l)[0][0] # mu_goal.T.dot(x)'s goal value
# technical init
x_iter = np.zeros((n, 1)) # we start increasing x_iter from zero
val_mu_goal_T_x_iter = 0
v_iter = np.sum(
np.abs(DeltaHist),
axis=1,
keepdims=True
) # v_iter[i] is the current value of the absolute sum of the i-th row of the objective
sum_abs_z = np.sum(abs_z)
list_rates = [] # list_rates[i] are the current and upcoming rates of x_iter[i]
list_rates_thresholds = [] # list_rates_thresholds[i] are the new-rate-thresholds of x_iter[i]
list_threshold_deltas = [] # list_threshold_deltas[i] are the threshold increments of x_iter[i]
# determine the rates of increase and thresholds
for i in range(n):
# determine the idxs that can lower their corresp. absolute value
Delta_row_T = DeltaHist[[i], :].T
diff_signs = np.logical_or(
np.logical_and(Delta_row_T < 0, z_pos),
np.logical_and(Delta_row_T > 0, z_neg)
).flatten()
diff_sign_idxs = np.argwhere(diff_signs).flatten()
if len(diff_sign_idxs) == 0:
# there is only one rate, no new rates
list_rates_thresholds.append(np.array([np.inf]))
list_rates.append(np.array([sum_abs_z]))
list_threshold_deltas.append(np.array([np.inf]))
continue
# determine thresholds for new rates
new_rates_thresholds = - Delta_row_T[diff_sign_idxs,
:] / z[diff_sign_idxs, :]
new_rates_thresholds = new_rates_thresholds.flatten()
argsort_new_rate_threshold = np.argsort(new_rates_thresholds)
new_rates_thresholds = new_rates_thresholds[argsort_new_rate_threshold]
new_rates_thresholds = np.append(
new_rates_thresholds, np.inf
) # to ensure we keep increasing with the last current rate
list_rates_thresholds.append(new_rates_thresholds)
# determine threshold deltas
list_threshold_deltas.append(
new_rates_thresholds - np.append(0, new_rates_thresholds[:-1])
)
# determine current and new rates when threshold is reached
sorted_diff_sign_idxs = diff_sign_idxs[argsort_new_rate_threshold]
rates = [sum_abs_z - 2 * sum(abs_z[sorted_diff_sign_idxs, 0])]
for _idx in sorted_diff_sign_idxs:
rates.append(rates[-1] + 2 * abs_z[_idx, 0])
list_rates.append(np.array(rates))
# determine upperbounds (UBs) for gammas in Phase 1
phase_1_gammas_UBs = np.empty((n, 1))
for idx, rates in enumerate(list_rates):
last_idx_neg_rate = np.argwhere(rates >= 0)[0, 0] - 1
thresholds = list_rates_thresholds[idx]
if last_idx_neg_rate >= 0:
x_iter_upper_bound = min(u[idx, 0], thresholds[last_idx_neg_rate])
max_gamma_decrease = - find_gamma_for_x_iter_increase(
idx, list_rates, list_rates_thresholds,
list_threshold_deltas, x_iter,
x_iter_upper_bound - x_iter[idx]
)
phase_1_gammas_UBs[idx, 0] = max_gamma_decrease
else:
phase_1_gammas_UBs[idx, 0] = 0
# phase 1: decrease v_i's as much as possible in order of v_i
# determine whether all v_i's can be decreased as much as possible
v_LB = v_iter - phase_1_gammas_UBs
gammas = det_gammas_decreases(v_LB, v_iter, np.min(v_LB))
x_iter_increase_info = [
find_x_iter_increase_for_gamma(
i, list_rates, list_rates_thresholds,
list_threshold_deltas, x_iter, gamma
)
for i, gamma in enumerate(gammas)
]
x_iter_increases = np.array([info[0] for info in x_iter_increase_info])
possible_val_increase = np.dot(mu_goal.T, x_iter_increases)[0][0]
possible_value = val_mu_goal_T_x_iter + possible_val_increase
if possible_value < goal_value:
# process decrease and continue to phase 2
x_iter += x_iter_increases
v_iter -= gammas
val_mu_goal_T_x_iter += possible_val_increase
for i in range(n):
list_rates[i] = x_iter_increase_info[i][1]
list_rates_thresholds[i] = x_iter_increase_info[i][2]
list_threshold_deltas[i] = x_iter_increase_info[i][3]
elif possible_value == goal_value:
# goal reached
x_iter += x_iter_increases
return (l + x_iter).dot(z.T)
else:
# do not process decrease and start binary search for right decrease
# init binary search
LB_cut = np.min(v_LB)
UB_cut = np.max(v_iter)
count = 0
while ((goal_value - val_mu_goal_T_x_iter) / goal_value
> BINSEARCH_PRECISION and count < MAX_BINSEARCH_ITER):
count += 1
cut = (LB_cut + UB_cut) / 2
# determine possible value for cut
gammas = det_gammas_decreases(v_LB, v_iter, cut)
x_iter_increase_info = [
find_x_iter_increase_for_gamma(
i, list_rates, list_rates_thresholds,
list_threshold_deltas, x_iter, gamma
)
for i, gamma in enumerate(gammas)
]
x_iter_increases = np.array(
[info[0] for info in x_iter_increase_info]
)
possible_val_increase = np.dot(mu_goal.T, x_iter_increases)[0][0]
possible_value = val_mu_goal_T_x_iter + possible_val_increase
if possible_value < goal_value:
# process decrease
UB_cut = cut
x_iter += x_iter_increases
v_iter -= gammas
val_mu_goal_T_x_iter += possible_val_increase
for i in range(n):
list_rates[i] = x_iter_increase_info[i][1]
list_rates_thresholds[i] = x_iter_increase_info[i][2]
list_threshold_deltas[i] = x_iter_increase_info[i][3]
elif possible_value == goal_value:
# goal reached
x_iter += x_iter_increases
return (l + x_iter).dot(z.T)
else:
# do not process decrease and update lower bound
LB_cut = cut
if count == MAX_BINSEARCH_ITER:
print(
f'Warning: could not reach precision {BINSEARCH_PRECISION}, '
f'after {MAX_BINSEARCH_ITER} iterations. Current relative '
f'difference is: '
f'{(goal_value - val_mu_goal_T_x_iter) / goal_value}'
)
return (l + x_iter).dot(z.T)
# phase 2: all rates are non-negative (for those i: x_iter[i] < u[i]),
# rate-proportionally increase v_i's in order from small to large
# try to increase all v_i's to their maximum (to get an upper bound)
v_max = np.array(
[v_iter[i] + find_gamma_for_x_iter_increase(
i, list_rates, list_rates_thresholds,
list_threshold_deltas, x_iter, u[i] - x_iter[i]
)
for i in range(n)]
)
gammas = det_gammas_increase(v_iter, v_max, np.max(v_max))
x_iter_increase_info = [
find_x_iter_increase_for_gamma(
i, list_rates, list_rates_thresholds,
list_threshold_deltas, x_iter, gamma
)
for i, gamma in enumerate(gammas)]
x_iter_increases = np.array([info[0] for info in x_iter_increase_info])
possible_val_increase = np.dot(mu_goal.T, x_iter_increases)[0][0]
possible_value = val_mu_goal_T_x_iter + possible_val_increase
if possible_value < goal_value:
raise Exception("Impossible...")
elif possible_value == goal_value:
# goal reached
x_iter += x_iter_increases
return (l + x_iter).dot(z.T)
else:
# do not process increase and start binary search
LB_cut = np.min(v_iter)
UB_cut = np.max(v_max)
count = 0
while ((goal_value - val_mu_goal_T_x_iter) / goal_value
> BINSEARCH_PRECISION and count < MAX_BINSEARCH_ITER):
count += 1
cut = (LB_cut + UB_cut) / 2
# determine possible value for cut
gammas = det_gammas_increase(v_iter, v_max, cut)
x_iter_increase_info = [
find_x_iter_increase_for_gamma(
i, list_rates, list_rates_thresholds,
list_threshold_deltas, x_iter, gamma
)
for i, gamma in enumerate(gammas)]
x_iter_increases = np.array(
[info[0] for info in x_iter_increase_info]
)
possible_val_increase = np.dot(mu_goal.T, x_iter_increases)[0][0]
possible_value = val_mu_goal_T_x_iter + possible_val_increase
if possible_value < goal_value:
# process increase
LB_cut = cut
x_iter += x_iter_increases
v_iter += gammas
val_mu_goal_T_x_iter += possible_val_increase
for i in range(n):
list_rates[i] = x_iter_increase_info[i][1]
list_rates_thresholds[i] = x_iter_increase_info[i][2]
list_threshold_deltas[i] = x_iter_increase_info[i][3]
elif possible_value == goal_value:
# goal reached
x_iter += x_iter_increases
return (l + x_iter).dot(z.T)
else:
# do not process increase and update upper bound
UB_cut = cut
if count == MAX_BINSEARCH_ITER:
print(
f'Warning: could not reach precision {BINSEARCH_PRECISION}, '
f'after {MAX_BINSEARCH_ITER} iterations. Current relative '
f'difference is: '
f'{(goal_value - val_mu_goal_T_x_iter) / goal_value},'
f' returning current solution found.'
)
return (l + x_iter).dot(z.T)
def index_set_for_new_edges(G, z_min, z_plus, l, u):
"""Returns a list with set indexes for which x_i (aka x_iter) should be 0
to ensure that no new edges appear. It is also written in the article as
S_0^supp. """
S = []
for i in range(len(G)):
zeros = (G[[i], :] == 0).T
x_increase_slack = (z_plus > 0) * (u[i, 0] > 0)
x_decrease_slack = (z_min > 0) * (l[i, 0] < 0)
option_1 = max(np.logical_and(zeros, x_increase_slack))
option_2 = max(np.logical_and(zeros, x_decrease_slack))
if option_1 or option_2:
S.append(i)
return S
def min_norm_rank_1_pert_pres_stoch(
G, mu, mu_goal, normtype='inf',
u=None, sameSuppPrecision=None
):
"""Under that assumption that
mu_goal.T u >= 1
it finds an explicit solution to:
min || Delta ||
s.t.
mu_goal^T (G + Delta) = mu_goal^T
G + Delta >= 0
rank(Delta) = 1
This is an elaboration of the algorithms from Appendix D to solve the
problems from Theorem 6.2.
Parameters
----------
G : np.array
Stochastic matrix.
mu : np.array
Stationary distribution of G.
mu_goal : np.array
Probability vector of the stationary distribution goal.
normtype : str
String indication the norm type to apply. Options: '1' or 'inf'.
u : np.array
The u vector from the article, see determine_u(). This can be
given so that it can be reused for speed reasons.
sameSuppPrecision : None or float
If this is a float, we will find only solutions that do not change the
support, i.e., for which supp(G) = supp(G + Delta). The float value
is the minimal value of (G + Delta)(i, j) in case G(i,j) > 0. If it is
None, we are allowed to modify the support.
Returns
-------
DeltaSol : np.array
The perturbation matrix.
"""
# init
if u is None:
u = determine_u(G, mu_goal)
z, z_plus, z_min = determine_z(G, mu_goal)
if sameSuppPrecision is not None:
# hack situation so that we only find solutions with same support as G
S_0_supp = index_set_for_new_edges(G, z_min, z_plus, 0 * u, u)
u[u > 0] -= sameSuppPrecision
u[S_0_supp, 0] = 0
if mu_goal.T.dot(u) < 1 - PRECISION:
raise Exception(
f'This function only works when mu_goal.T.dot(u) >= 1, '
f'its value is now {mu_goal.T.dot(u)}'
)
elif mu_goal.T.dot(u) == 1:
DeltaSol = u.dot(z.T) / (mu_goal.T.dot(u))
return DeltaSol
elif normtype == 'inf':
# start construction of an optimal solution for inf-norm
# init
x_iter = np.array(u) # start decreasing from upper bound
val_mu_goal_T_x_iter = mu_goal.T.dot(x_iter)
while val_mu_goal_T_x_iter > 1:
max_val = np.max(x_iter)
max_idxs = np.argwhere(x_iter == max_val)[:, 0]
non_max_x_iter_vals = x_iter[x_iter < max_val]
if len(non_max_x_iter_vals) > 0:
second_max_val = np.max(non_max_x_iter_vals)
else:
second_max_val = 0
total_mu_goal_max = np.sum(mu_goal[max_idxs])
# determine delta: the maximum decrease
delta = max_val - second_max_val
if val_mu_goal_T_x_iter - total_mu_goal_max * delta < 1:
# adjust delta to decrease as much as possible
delta = (val_mu_goal_T_x_iter - 1) / total_mu_goal_max
# update x_iter
x_iter[max_idxs] -= delta
val_mu_goal_T_x_iter -= total_mu_goal_max * delta
elif normtype == '1':
# start construction of an optimal solution for 1-norm
# init
x_iter = np.zeros((len(mu), 1)) # start increasing from zeros vector
val_mu_goal_T_x_iter = 0
mu_goal_sorted = np.argsort(mu_goal, axis=0)[::-1]
count = 0
while val_mu_goal_T_x_iter < 1 - PRECISION:
cur_index = mu_goal_sorted[count]
# determine delta: the maximum increase for current index
delta = u[cur_index, 0]
mu_goal_cur_index = mu_goal[cur_index, 0]
if val_mu_goal_T_x_iter + mu_goal_cur_index * delta > 1:
# adjust delta to decrease as much as possible
delta = (1 - val_mu_goal_T_x_iter) / mu_goal_cur_index
# update x_iter
x_iter[cur_index, 0] += delta
val_mu_goal_T_x_iter += mu_goal_cur_index * delta
count += 1
else:
raise NotImplementedError
DeltaSol = x_iter.dot(z.T)
return DeltaSol
def min_norm_rank_1(P, mu_goal, normtype='inf'):
"""Finds a Delta that solves:
min || Delta ||
s.t.
mu_goal^T (P + Delta) = mu_goal^T
rank(Delta) = 1
Note that there is no guarantee that (P + Delta) >= 0.
This is an elaboration of Corollary 5.3 that follows from Theorem 5.1.
Parameters
----------
P : np.array
Stochastic matrix.
mu_goal : np.array
Goal stationary distribution.
normtype : str
String indication the norm type to apply. Options: '1', '2', or 'inf'.
Returns
-------
DeltaSol : np.array
The perturbation matrix.
"""
# init
n = len(P)
I = np.eye(n)
# determine the x vector
if normtype in ['1', 1, 'one']:
x = np.zeros((n, 1))
x[np.argmax(mu_goal)] = 1
elif normtype in ['2', 2, 'two']:
x = np.array(mu_goal)
elif normtype in ['inf', np.inf]:
x = np.ones((n, 1))
else:
raise TypeError(f'Normtype {normtype} not implemented.')
DeltaSol = x.dot(mu_goal.T).dot(I - P) / (mu_goal.T.dot(x))
return DeltaSol
def goal_MC_optimization(
G, mu_goal, normtype='inf', rankOne=False,
ensureNonNeg=True, onlyRowIdxs=None,
verbose=True, DeltaHist=None, sameSuppPrecision=None,
timeLimit=60
):
"""Find the best perturbation Delta for P so that
mu_goal^T (G + Delta) = mu_goal^T,
i.e., so that mu_goal becomes its new stationary distribution.
The problem is:
min || Delta ||
s.t.
mu_goal^T (G + Delta) = mu_goal^T
(P + Delta) >= 0 [optional: see ensureNonNeg]
rank(Delta) = 1 [optional: see rankOne]
If DeltaHist is given (not None), the optimization will also take the
history of Delta's into account. In particular, the problem solved when
DeltaHist is not None is:
min || DeltaHist + Delta ||
s.t.
mu_goal^T (G + Delta) = mu_goal^T
(P + Delta) >= 0 [optional: see ensureNonNeg]
rank(Delta) = 1 [optional: see rankOne]
Parameters
----------
G : np.array
Stochastic matrix.
mu_goal : np.array
Goal stationary distribution.
normtype : str
String indication the norm type to apply. Options: '1' or 'inf'.
rankOne : bool
If True, a rank 1 Delta will be sought.
ensureNonNeg : bool
If True, ensures non-negativity of G + Delta, else not.
onlyRowIdx : list or None
If a list is given, it will only adjust the corresponding rows in Delta.
verbose : bool
If True, reports are printed, else nothing.
DeltaHist : np.array
Sum of previous Deltas that can be taken into account in the opt.
sameSuppPrecision : None or float
If this is a float, we will only find solutions that do not change the
support, i.e., for which supp(G) = supp(G + Delta). The float value
is the minimal value of (G + Delta)(i, j) in case G(i,j) > 0. If it is
None, we are allowed to modify the support.
Returns
-------
DeltaSol : np.array
The perturbation matrix.
"""
# technical init
n = len(G)
if DeltaHist is None:
DeltaHist = np.zeros((n, n))
# init model
ILP = pulp.LpProblem("ILP_best_perturbation_given_goal", pulp.LpMinimize)
# decision variables
Delta = [[pulp.LpVariable(f'Delta({i},{j})') for j in range(n)]
for i in range(n)] # Delta[i][j] = perturbation of P[i, j]
DeltaAbs = [
[pulp.LpVariable(f'DeltaAbs({i},{j})', lowBound=0) for j in range(n)]
for i in range(n)
] # kind of hacky: DeltaAbs[i][j] = |Delta[i][j] + DeltaHist[i][j]|
objVar = pulp.LpVariable(
f'objective_variable',
lowBound=0
) # varies based on chosen norm
# objective
ILP += objVar, "objective"
# constraints:
# ============
# 1) ensure that mu_goal is reached
for j in range(n):
ILP += sum(
[mu_goal[i, 0] * (G[i, j] + Delta[i][j]) for i in range(n)]
) == mu_goal[j, 0], f'set_mu_goal{j}'
if ensureNonNeg:
# 2) ensure that new Markov chain is non-negative
for i in range(n):
for j in range(n):
ILP += G[i, j] + Delta[i][j] >= 0, f'(P + Delta)({i},{j}) >= 0'
# 3) ensure that DeltaAbs gets its property
for i in range(n):
for j in range(n):
ILP += DeltaAbs[i][j] >= Delta[i][j] + DeltaHist[i][j], \
f'DeltaAbs({i},{j}) >= Delta({i},{j})'
ILP += DeltaAbs[i][j] >= - Delta[i][j] - DeltaHist[i][j], \
f'DeltaAbs({i},{j}) >= - Delta({i},{j})'
# 4) set objective variable based on norm type
if normtype == '1':
# maximum absolute column sum
for j in range(n):
ILP += objVar >= sum([DeltaAbs[i][j] for i in range(n)])
elif normtype == 'inf':
# maximum absolute row sum
for i in range(n):
ILP += objVar >= sum([DeltaAbs[i][j] for j in range(n)])
else:
raise NotImplementedError(f'Normtype {normtype} is not implemented.')
if rankOne:
# ensure that a rank 1 solution is found
colVals = [pulp.LpVariable(f'colVals({i})') for i in range(n)]
rowVals = mu_goal.T.dot(np.eye(n) - G) # ensures constraint 5) holds
for i in range(n):
for j in range(n):
ILP += Delta[i][j] == colVals[i] * rowVals[0, j], \
f'Ensure that Delta is rank 1: ({i},{j})'
else:
# 5) ensure that row sum is 1
for i in range(n):
ILP += sum([G[i, j] + Delta[i][j] for j in range(n)]) == 1, \
f'Row_sum_{i} == 0'
if onlyRowIdxs is not None:
for i in range(n):
if i in onlyRowIdxs:
continue
for j in range(n):
ILP += Delta[i][j] == 0, f'Delta({i},{j}) = 0'
if sameSuppPrecision is not None:
for i in range(n):
for j in range(n):
if G[i, j] > 0:
ILP += G[i, j] + Delta[i][j] >= sameSuppPrecision, \
f'Ensure (P + Delta)({i},{j}) >= sameSuppPrecision'
elif G[i, j] == 0:
ILP += G[i, j] + Delta[i][j] == 0, \
f'Ensure (P + Delta)({i},{j}) == 0'
else:
raise Exception(f'P({i},{j}) < 0!')
# solve problem
ILP.solve(pulp.GUROBI(msg=verbose, timeLimit=timeLimit))
if ILP.status != 1:
print(
'Gurobi did not find an optimum,' +
f' its status number = {ILP.status}' +
f' which means: {pulp.LpStatus[ILP.status]}.'
f' An infeasible solution is returned.'
)
return np.zeros((n, n)) # return an infeasible solution
if verbose:
print("\nMILP report:")
print("Optimization status:", pulp.LpStatus[ILP.status])
print("Objective value =", pulp.value(ILP.objective))
# retrieve and return solution
DeltaSol = np.empty((n, n))
for i in range(n):
for j in range(n):
DeltaSol[i, j] = Delta[i][j].varValue
return DeltaSol
def existence_rank_1_Delta_pres_stochasticity(G, mu_goal, u=None):
"""Checks whether mu_delta^T u >= 1, which means that there exists a rank-1
perturbation Delta that preserves stochasticity.
There is an option to give u to speed up computation.
"""
if u is None:
return mu_goal[:, 0].dot(determine_u(G, mu_goal)[:, 0]) >= 1
else:
return mu_goal[:, 0].dot(u[:, 0]) >= 1
def max_convex_softened_mu_goal(G, mu, mu_goal):
"""Denote the convex softened mu_goal as
mu(alpha) := (1 - alpha) mu + alpha mu_goal.
It will find the maximum alpha and corresponding mu(alpha) for which the
problem from min_norm_rank_1_pert_pres_stoch() can be solved.
"""
max_alpha = min(
1, max_feas_stepsize_pres_stochasticity(G, mu, mu_goal)
) # if > 1 we can even shoot past our mu_goal, which we do not want
return max_alpha, (1 - max_alpha) * mu + max_alpha * mu_goal
def max_feas_stepsize_pres_stochasticity(G, mu, mu_goal):
"""Returns the maximum scaling factor alpha^* which is discussed after
Theorem 6.1, it denotes the maximal step size alpha towards (alpha * mu_goal
+ (1-alpha) * mu) such that there is a rank-1 perturbation Delta for which
(G + Delta) >= 0. """
u = determine_u(G, mu_goal)
d = determine_d(mu, mu_goal)
return (mu.flatten().dot(u) / (1 - d.flatten().dot(u)))[0]
def softened_mu_goal(mu, mu_goal, indexes_set):
"""Softens the mu_goal to soft_mu_goal for which it holds that
soft_mu_goal[indexes_set] = mu_goal[indexes_set]
and the rest of soft_mu_goal is relatively equal to mu. Input
indexes_set can be an int or an iterable. """
if hasattr(indexes_set, '__len__'):
if len(indexes_set) == len(mu):
return mu_goal # assuming that indexes in set are all unique
soft_mu_goal = np.array(mu)
remaining_mass_to_divided = (1 - np.sum(mu_goal[indexes_set]))
soft_mu_goal *= remaining_mass_to_divided / (1 - np.sum(mu[indexes_set]))
soft_mu_goal[indexes_set] = mu_goal[indexes_set]
return soft_mu_goal
def FR1SH(
G, mu, mu_goal, normType='inf', verbose=True,
phi=10 ** (-3), sequence='random',
doMaxConvexJumpToPiGoal=False,
jumpToGoalIfPossible=True,
rankDeltaBeforeDirectJumpToGoal=0,
considerDeltaHistory=False,
trackBestSolutionAlongTheWay=True,
timeLimit=None
):
"""Iteratively optimizes
min || Delta ||
s.t.
mu_goal^T (G + Delta) = mu_goal^T
(G + Delta) >= 0
by taking rank-1 steps towards softened versions of mu_goal. This is the
implementation of FR1SH from the paper.
Parameters
----------
G : np.array
Stochastic matrix.
mu : np.array
Stationary distribution of G.
mu_goal : np.array
Goal stationary distribution.
normtype : str
String indication the norm type to apply. Options: '1' or 'inf'.
verbose : bool
If True, reports are printed, else nothing.
phi : float
Stops when difference between goal and current is smaller than phi.
sequence : string
Type of sequence used for the element-wise update.
doMaxConvexJumpToPiGoal : bool
If True, we will move as much as possible to the softened mu_goal
as described in max_convex_softened_mu_goal(). Note that this ensures
that in case it can be solved directly, it will.
jumpToGoalIfPossible : bool
If True, the optimization will jump directly to the goal when this is
possible with a rank 1 perturbation.
rankDeltaBeforeDirectJumpToGoal : int
If jumpDirectlyToGoalIfPossible = True, we only jump once the current
Delta has a rank of rankDeltaBeforeDirectJumpToGoal. If
jumpDirectlyToGoalIfPossible = False, it is not used.
considerDeltaHistory : bool
If True, also the history of Deltas is taken into account when
finding a minimal norm rank 1 solution.
trackBestSolutionAlongTheWay : bool
If True, keep track of the best solutions along the way by checking at
each step whether we can jump to the goal.
timeLimit : int
The method is stopped after timeLimit seconds.
Returns
-------
DeltaSol : np.array
The perturbation matrix.
"""
if verbose: