-
Notifications
You must be signed in to change notification settings - Fork 0
/
FunctionManager.py
1460 lines (1184 loc) · 42.5 KB
/
FunctionManager.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
import numpy as np
try:
import cupy as cp
gpu_flag = True
except Exception:
gpu_flag = False
class FunctionManager:
def __init__(
self,
c=None,
A=None,
b=None,
C=None,
d=None,
x0=None,
lower_bound=None,
upper_bound=None,
t=1,
use_gpu=False,
n=None,
try_diag=True,
):
# problem specifications
self.A = A
self.b = b
self.C = C
self.d = d
self.c = c
self.lb = lower_bound
self.ub = upper_bound
self.x = x0
self.update_obj = True
self.update_newton_obj = True
self.update_grad = True
self.update_hessian = True
self.update_inv_hessian = True
self.update_slacks = True
self.update_inv_slacks = True
self.is_bounded = self.ub is not None or self.lb is not None
self.is_constrained = self.C is not None or self.is_bounded
start_index = 0
end_index = 0
if self.C is not None:
end_index = start_index + len(self.C)
self.inequality_slack_indices = slice(start_index, len(self.C))
start_index += len(self.C)
if self.ub is not None:
end_index = start_index + len(self.x)
self.ub_slack_indices = slice(start_index, end_index)
start_index = end_index
if self.lb is not None:
end_index = start_index + len(self.x)
self.lb_slack_indices = slice(start_index, end_index)
self.obj = None
self.newton_obj = None
self.grad = None
self.hess = None
self.inv_hess = None
self.slacks = None
self.inv_slacks = None
self.use_gpu = use_gpu and gpu_flag
self.try_diag = try_diag
self.t = t
if self.c is None:
if self.x is not None:
if self.use_gpu:
self.c = cp.ones(len(self.x))
else:
self.c = np.ones(len(self.x))
elif n is None:
raise ValueError(
"If no x vector is provided, you need to pass a value to n (problem dimension)"
)
else:
if self.use_gpu:
self.c = cp.ones(n)
else:
self.c = np.ones(n)
def update_x(self, x):
self.x = x
self.update_obj = True
self.update_newton_obj = True
self.update_grad = True
self.update_hessian = True
self.update_inv_hessian = True
self.update_slacks = True
self.update_inv_slacks = True
def update_t(self, t):
if self.t != t:
self.t = t
self.update_obj = True
self.update_newton_obj = True
self.update_grad = True
# self.update_slacks = True
# t does not affect hessian or inverse hessian for linear problems
# self.update_hessian = True
# self.update_inv_hessian = True
def update_slacks_fxn(self):
initialized = False
if self.d is not None:
if self.use_gpu:
self.slacks = self.d - cp.matmul(self.C, self.x)
else:
self.slacks = self.d - np.matmul(self.C, self.x)
initialized = True
if self.ub is not None:
ub_slacks = self.ub - self.x
if initialized:
if self.use_gpu:
self.slacks = cp.append(self.slacks, ub_slacks)
else:
self.slacks = np.append(self.slacks, ub_slacks)
else:
self.slacks = ub_slacks
initialized = True
if self.lb is not None:
lb_slacks = self.x - self.lb
if initialized:
if self.use_gpu:
self.slacks = cp.append(self.slacks, lb_slacks)
else:
self.slacks = np.append(self.slacks, lb_slacks)
else:
self.slacks = lb_slacks
if self.slacks.ndim > 1:
self.slacks = self.slacks.flatten()
self.update_inv_slacks = True
self.update_slacks = False
def objective(self, x=None):
if x is not None:
self.update_x(x)
elif not self.update_obj:
return self.obj
self.obj = self.c.dot(self.x)
self.update_obj = False
return self.obj
def newton_objective(self, x=None, t=None):
if x is not None:
self.update_x(x)
if t is not None:
self.update_t(t)
if not self.update_newton_obj:
return self.newton_obj
obj = self.objective()
self.newton_obj = self.t * obj
self.update_newton_obj = False
return self.newton_obj
def gradient(self, x=None):
raise NotImplementedError(
"Gradient method needs to be overridden by a child class!"
)
def hessian(self, x=None):
raise NotImplementedError(
"Hessian method needs to be overridden by a child class!"
)
def inv_hessian(self, x=None):
raise NotImplementedError(
"Hessian method needs to be overridden by a child class!"
)
class FunctionManagerLP(FunctionManager):
def update_x(self, x, update_slacks=True):
super().update_x(x)
if self.is_constrained:
if update_slacks:
self.update_slacks_fxn()
else:
self.update_slacks = True
self.update_inv_slacks = True
def newton_objective(self, x=None, t=None):
if x is not None:
self.update_x(x)
if t is not None:
self.update_t(t)
if not self.update_newton_obj:
return self.newton_obj
obj = self.objective(x)
self.newton_obj = self.t * obj
if self.is_constrained:
if self.use_gpu:
self.newton_obj -= cp.log(
self.slacks + 1e-15
).sum() # small addition for numerical stability
else:
self.newton_obj -= np.log(self.slacks + 1e-15).sum()
self.update_newton_obj = False
return self.newton_obj
def gradient(self, x=None, t=None):
if x is not None:
self.update_x(x)
if t is not None:
self.update_t(t)
if not self.update_grad:
return self.grad
if self.is_constrained and self.update_inv_slacks:
self.inv_slacks = 1 / (
self.slacks + 1e-15
) # small addition for numerical stability
self.update_inv_slacks = False
self.grad = self.t * self.c
if self.lb is not None:
self.grad -= self.inv_slacks[self.lb_slack_indices]
if self.ub is not None:
self.grad += self.inv_slacks[self.ub_slack_indices]
if self.C is not None:
if self.use_gpu:
self.grad += cp.matmul(
self.C.T, self.inv_slacks[self.inequality_slack_indices]
)
else:
self.grad += np.matmul(
self.C.T, self.inv_slacks[self.inequality_slack_indices]
)
self.update_grad = False
return self.grad
def hessian(self, x=None):
if x is not None:
self.update_x(x)
if not self.update_hessian:
return self.hess
if self.is_constrained and self.update_inv_slacks:
self.inv_slacks = 1 / (
self.slacks + 1e-15
) # small addition for numerical stability
self.update_inv_slacks = False
if self.C is None:
if self.try_diag and self.is_bounded:
if self.lb is not None:
self.hess = self.inv_slacks[self.lb_slack_indices] ** 2
if self.ub is not None:
self.hess += self.inv_slacks[self.ub_slack_indices] ** 2
elif self.ub is not None:
self.hess = self.inv_slacks[self.ub_slack_indices] ** 2
return self.hess
else:
if self.use_gpu:
self.hess = cp.zeros((self.x.shape[0], self.x.shape[0]))
else:
self.hess = np.zeros((self.x.shape[0], self.x.shape[0]))
else:
if self.use_gpu:
self.hess = cp.matmul(
self.C.T,
(self.inv_slacks[self.inequality_slack_indices] ** 2)[:, None]
* self.C,
)
else:
self.hess = np.matmul(
self.C.T,
(self.inv_slacks[self.inequality_slack_indices] ** 2)[:, None]
* self.C,
)
if self.is_bounded:
if self.use_gpu:
diag = cp.einsum("ii->i", self.hess)
else:
diag = np.einsum("ii->i", self.hess)
if self.lb is not None:
diag += 1 / (self.slacks[self.lb_slack_indices]) ** 2
if self.ub is not None:
diag += 1 / (self.slacks[self.ub_slack_indices]) ** 2
self.update_hessian = False
return self.hess
def inv_hessian(self, x=None):
if x is not None:
self.update_x(x)
if not self.update_inv_hessian:
return self.inv_hess
if self.C is None and self.try_diag:
if self.is_bounded:
hess = self.hessian()
self.inv_hess = 1 / hess
else:
if self.use_gpu:
self.hess = cp.zeros((self.x.shape[0], self.x.shape[0]))
else:
self.hess = np.zeros((self.x.shape[0], self.x.shape[0]))
else:
raise ValueError(
"Hessian is not diagonal, cannot use inv hessian function!"
)
self.update_inv_hessian = False
return self.inv_hess
class FunctionManagerPhase1(FunctionManager):
def __init__(
self,
c=None,
A=None,
b=None,
C=None,
d=None,
x0=None,
lower_bound=None,
upper_bound=None,
t=1,
use_gpu=False,
n=None,
try_diag=False,
suppress_print=True,
):
# problem specifications
self.A = A
self.b = b
self.C = C
self.d = d
self.c = c
self.x = x0
self.lb = lower_bound
self.ub = upper_bound
self.use_gpu = use_gpu
self.s = 0
self.update_slacks_fxn()
self.s = -self.slacks.min() + 1
self.update_slacks_fxn()
if not suppress_print:
if self.use_gpu:
print(f"Starting slack of {cp.round(self.s, 4)}")
else:
print(f"Starting slack of {np.round(self.s, 4)}")
self.update_obj = True
self.update_newton_obj = True
self.update_grad = True
self.update_hessian = True
self.update_inv_hessian = True
self.update_slacks = False
self.update_inv_slacks = True
self.is_bounded = self.ub is not None or self.lb is not None
self.is_constrained = self.C is not None or self.is_bounded
self.inequality_slack_indices = slice(0, len(self.C))
start_index = len(self.C)
if self.ub is not None:
end_index = start_index + len(self.x)
self.ub_slack_indices = slice(start_index, end_index)
start_index = end_index
if self.lb is not None:
end_index = start_index + len(self.x)
self.lb_slack_indices = slice(start_index, end_index)
self.obj = None
self.newton_obj = None
self.grad = None
self.hess = None
self.inv_hess = None
self.inv_slacks = None
self.t = t
def update_slacks_fxn(self):
if self.use_gpu:
self.slacks = self.s + self.d - cp.matmul(self.C, self.x)
else:
self.slacks = self.s + self.d - np.matmul(self.C, self.x)
if self.ub is not None:
if self.use_gpu:
self.slacks = cp.append(self.slacks, self.s + self.ub - self.x)
else:
self.slacks = np.append(self.slacks, self.s + self.ub - self.x)
if self.lb is not None:
if self.use_gpu:
self.slacks = cp.append(self.slacks, self.s + self.x - self.lb)
else:
self.slacks = np.append(self.slacks, self.s + self.x - self.lb)
if self.slacks.ndim > 1:
self.slacks = self.slacks.flatten()
self.update_inv_slacks = True
self.update_slacks = False
def update_x(self, x, update_slacks=True):
if len(x) == len(self.x) + 1:
self.x = x[:-1]
self.s = x[-1]
elif len(x) == len(self.x):
self.x = x
else:
raise ValueError("Provided x does not have the right dimensions!")
if update_slacks:
self.update_slacks_fxn()
else:
self.update_slacks = True
self.update_inv_slacks = True
self.update_obj = True
self.update_newton_obj = True
self.update_grad = True
self.update_hessian = True
self.update_inv_hessian = True
def objective(self, x=None):
if x is not None:
self.update_x(x)
elif not self.update_obj:
return self.obj
self.obj = self.s
self.update_obj = False
return self.obj
def newton_objective(self, x=None, t=None):
if x is not None:
self.update_x(x)
if t is not None:
self.update_t(t)
if not self.update_newton_obj:
return self.newton_obj
obj = self.objective()
self.newton_obj = self.t * obj
if self.use_gpu:
self.newton_obj -= cp.log(
self.slacks + 1e-15
).sum() # small addition for numerical stability
else:
self.newton_obj -= np.log(
self.slacks + 1e-15
).sum() # small addition for numerical stability
self.update_newton_obj = False
return self.newton_obj
def gradient(self, x=None, t=None):
if x is not None:
self.update_x(x)
if t is not None:
self.update_t(t)
if not self.update_grad:
return self.grad
if self.is_constrained and self.update_inv_slacks:
self.inv_slacks = 1 / (
self.slacks + 1e-15
) # small addition for numerical stability
self.update_inv_slacks = False
if self.use_gpu:
grad_x = cp.matmul(self.C.T, self.inv_slacks[self.inequality_slack_indices])
else:
grad_x = np.matmul(self.C.T, self.inv_slacks[self.inequality_slack_indices])
if self.lb is not None:
grad_x -= self.inv_slacks[self.lb_slack_indices]
if self.ub is not None:
grad_x += self.inv_slacks[self.ub_slack_indices]
grad_s = self.t - self.inv_slacks.sum()
if self.use_gpu:
self.grad = cp.append(grad_x, grad_s)
else:
self.grad = np.append(grad_x, grad_s)
self.update_grad = False
return self.grad
def hessian(self, x=None):
if x is not None:
self.update_x(x)
if not self.update_hessian:
return self.hess
if self.is_constrained and self.update_inv_slacks:
self.inv_slacks = 1 / (
self.slacks + 1e-15
) # small addition for numerical stability
self.update_inv_slacks = False
inv_slacks_squared = self.inv_slacks**2
if self.use_gpu:
hess_xx = cp.matmul(
self.C.T,
(inv_slacks_squared[self.inequality_slack_indices])[:, None] * self.C,
)
hess_xs = -cp.matmul(
self.C.T, inv_slacks_squared[self.inequality_slack_indices]
)
diag = cp.einsum("ii->i", hess_xx)
else:
hess_xx = np.matmul(
self.C.T,
(inv_slacks_squared[self.inequality_slack_indices])[:, None] * self.C,
)
hess_xs = -np.matmul(
self.C.T, inv_slacks_squared[self.inequality_slack_indices]
)
diag = np.einsum("ii->i", hess_xx)
if self.lb is not None:
diag += inv_slacks_squared[self.lb_slack_indices]
hess_xs += inv_slacks_squared[self.lb_slack_indices]
if self.ub is not None:
diag += inv_slacks_squared[self.ub_slack_indices]
hess_xs -= inv_slacks_squared[self.ub_slack_indices]
hess_ss = inv_slacks_squared.sum()
if self.use_gpu:
top = cp.hstack([hess_xx, hess_xs.reshape(-1, 1)])
bot = cp.hstack([hess_xs.reshape(1, -1), cp.array(hess_ss).reshape(1, 1)])
self.hess = cp.vstack([top, bot])
#self.hess = cp.bmat(
# [
# [hess_xx, hess_xs.reshape(-1, 1)],
# [hess_xs.reshape(1, -1), cp.array(hess_ss).reshape(1, 1)],
# ],
#)
else:
self.hess = np.bmat(
[
[hess_xx, hess_xs.reshape(-1, 1)],
[hess_xs.reshape(1, -1), np.array(hess_ss).reshape(1, 1)],
],
)
self.update_hessian = False
return self.hess
def inv_hessian(self, x=None):
raise ValueError(
"Hessian is not diagonal, so inverse hessian cannot be directly computed!!"
)
class FunctionManagerQP(FunctionManager):
def __init__(
self,
P=None,
q=None,
A=None,
b=None,
C=None,
d=None,
x0=None,
lower_bound=None,
upper_bound=None,
t=1,
use_gpu=False,
n=None,
):
# problem specifications
self.A = A
self.b = b
self.C = C
self.d = d
self.lb = lower_bound
self.ub = upper_bound
self.P = P
self.q = q
self.x = x0
self.update_obj = True
self.update_newton_obj = True
self.update_grad = True
self.update_hessian = True
self.slacks = None
self.update_slacks = True
self.update_inv_slacks = True
self.is_bounded = self.ub is not None or self.lb is not None
self.is_constrained = self.C is not None or self.is_bounded
start_index = 0
if self.C is not None:
self.inequality_slack_indices = slice(start_index, len(self.C))
start_index += len(self.C)
if self.ub is not None:
end_index = start_index + len(self.x)
self.ub_slack_indices = slice(start_index, end_index)
start_index = end_index
if self.lb is not None:
end_index = start_index + len(self.x)
self.lb_slack_indices = slice(start_index, end_index)
self.obj = None
self.newton_obj = None
self.grad = None
self.hess = None
self.inv_hess = None
self.slacks = None
self.inv_slacks = None
self.use_gpu = use_gpu and gpu_flag
self.t = t
def objective(self, x=None):
if x is not None:
self.update_x(x)
elif not self.update_obj:
return self.obj
self.obj = 0
if self.use_gpu:
if self.P is not None:
self.obj += 1 / 2 * self.x.dot(cp.matmul(self.P, self.x))
if self.q is not None:
self.obj += self.q.dot(self.x)
else:
if self.P is not None:
self.obj += 1 / 2 * self.x.dot(np.matmul(self.P, self.x))
if self.q is not None:
self.obj += self.q.dot(self.x)
self.update_obj = False
return self.obj
def update_x(self, x, update_slacks=True):
super().update_x(x)
if self.is_constrained:
if update_slacks:
self.update_slacks_fxn()
else:
self.update_slacks = True
self.update_inv_slacks = True
def newton_objective(self, x=None, t=None):
if x is not None:
self.update_x(x)
if t is not None:
self.update_t(t)
if not self.update_newton_obj:
return self.newton_obj
obj = self.objective(x)
self.newton_obj = self.t * obj
if self.is_constrained:
if self.use_gpu:
self.newton_obj -= cp.log(
self.slacks + 1e-15
).sum() # small addition for numerical stability
else:
self.newton_obj -= np.log(
self.slacks + 1e-15
).sum() # small addition for numerical stability
self.update_newton_obj = False
return self.newton_obj
def gradient(self, x=None, t=None):
if x is not None:
self.update_x(x)
if t is not None:
self.update_t(t)
if not self.update_grad:
return self.grad
if self.is_constrained and self.update_inv_slacks:
self.inv_slacks = 1 / (
self.slacks + 1e-15
) # small addition for numerical stability
self.update_inv_slacks = False
if self.use_gpu:
self.grad = cp.matmul(self.P, self.x)
else:
self.grad = np.matmul(self.P, self.x)
if self.q is not None:
self.grad += self.q
self.grad *= self.t
if self.lb is not None:
self.grad -= self.inv_slacks[self.lb_slack_indices]
if self.ub is not None:
self.grad += self.inv_slacks[self.ub_slack_indices]
if self.C is not None:
if self.use_gpu:
self.grad += cp.matmul(
self.C.T, self.inv_slacks[self.inequality_slack_indices]
)
else:
self.grad += np.matmul(
self.C.T, self.inv_slacks[self.inequality_slack_indices]
)
self.update_grad = False
return self.grad
def hessian(self, x=None):
if x is not None:
self.update_x(x)
if not self.update_hessian:
return self.hess
if self.is_constrained and self.update_inv_slacks:
self.inv_slacks = 1 / (
self.slacks + 1e-15
) # small addition for numerical stability
self.update_inv_slacks = False
self.hess = self.t * self.P
if self.C is not None:
if self.use_gpu:
self.hess += cp.matmul(
self.C.T,
(self.inv_slacks[self.inequality_slack_indices] ** 2)[:, None]
* self.C,
)
if self.is_bounded:
diag = cp.einsum("ii->i", self.hess)
else:
self.hess += np.matmul(
self.C.T,
(self.inv_slacks[self.inequality_slack_indices] ** 2)[:, None]
* self.C,
)
if self.is_bounded:
if self.use_gpu:
diag = cp.einsum("ii->i", self.hess)
else:
diag = np.einsum("ii->i", self.hess)
if self.lb is not None:
diag += 1 / (self.slacks[self.lb_slack_indices]) ** 2
if self.ub is not None:
diag += 1 / (self.slacks[self.ub_slack_indices]) ** 2
self.update_hessian = False
return self.hess
def inv_hessian(self, x=None):
raise ValueError("Hessian is not diagonal, cannot use inv hessian function!")
class FunctionManagerSOCP(FunctionManager):
def __init__(
self,
P=None,
q=None,
A=None,
b=None,
c=None,
d=None,
F=None,
g=None,
lower_bound=None,
upper_bound=None,
x0=None,
t=1,
use_gpu=False,
n=None,
):
# problem specifications
self.P = P
self.q = q
self.A = A
self.b = b
self.c = c
self.b = b
self.d = d
self.F = F
self.g = g
self.lb = lower_bound
self.ub = upper_bound
self.use_gpu = use_gpu
self.AtA_cache = []
self.Atb_cache = []
for i, A in enumerate(self.A):
if self.b is not None:
b = self.b[i]
if A.ndim > 1:
if self.use_gpu:
self.AtA_cache.append(cp.matmul(A.T, A))
if self.b is not None:
self.Atb_cache.append(cp.matmul(A.T, b))
else:
self.AtA_cache.append(np.matmul(A.T, A))
if self.b is not None:
self.Atb_cache.append(np.matmul(A.T, b))
else:
if self.use_gpu:
self.AtA_cache.append(cp.diag(A**2))
else:
self.AtA_cache.append(np.diag(A**2))
if self.b is not None:
self.Atb_cache.append(A * b)
if self.c is not None:
if self.use_gpu:
self.cct_cache = [cp.outer(c, c) for c in self.c]
else:
self.cct_cache = [np.outer(c, c) for c in self.c]
self.x = x0
self.update_obj = True
self.update_newton_obj = True
self.update_grad = True
self.update_hessian = True
self.slacks = None
self.slack_lhs = None
self.slack_rhs = None
self.update_slacks = True
self.update_inv_slacks = True
self.is_bounded = self.ub is not None or self.lb is not None
self.is_constrained = self.A is not None or self.is_bounded
start_index = 0
end_index = len(self.A)
self.inequality_slack_indices = slice(start_index, end_index)
start_index = end_index
if self.ub is not None:
end_index = start_index + len(self.x)
self.ub_slack_indices = slice(start_index, end_index)
start_index = end_index
if self.lb is not None:
end_index = start_index + len(self.x)
self.lb_slack_indices = slice(start_index, end_index)
self.constraint_indices = slice(0, end_index) # we will add some additional
# variables to the end of the slacks list to force them to be nonnegative.
# Remember the indices for all constraints for gradient and hessian calculations
self.obj = None
self.newton_obj = None
self.grad = None
self.hess = None
self.inv_slacks = None
self.t = t
def update_slacks_fxn(self):
inside_norms = []
for A in self.A:
if A.ndim > 1:
if self.use_gpu:
inside_norms.append(cp.matmul(A, self.x))
else:
inside_norms.append(np.matmul(A, self.x))
else:
inside_norms.append(A * self.x)
if self.b is not None:
for i in range(len(inside_norms)):
inside_norms[i] += self.b[i]
rhss = None
if self.c is not None:
rhss = [c.dot(self.x) for c in self.c]
if self.d is not None:
for i in range(len(rhss)):
rhss[i] += self.d[i]
elif self.d is not None:
rhss = self.d
else:
rhss = 0
self.slack_lhs = inside_norms
self.slack_rhs = rhss
if self.use_gpu:
self.slacks = cp.array(
[
rhs**2 - (inside_norm**2).sum()
for rhs, inside_norm in zip(self.slack_rhs, self.slack_lhs)
]
)
else:
self.slacks = np.array(
[
rhs**2 - (inside_norm**2).sum()
for rhs, inside_norm in zip(self.slack_rhs, self.slack_lhs)
]
)
if self.use_gpu:
if self.ub is not None:
self.slacks = cp.append(self.slacks, self.ub - self.x)
if self.lb is not None:
self.slacks = cp.append(self.slacks, self.x - self.lb)
self.slacks = cp.append(self.slacks, self.slack_rhs)
else:
if self.ub is not None:
self.slacks = np.append(self.slacks, self.ub - self.x)
if self.lb is not None:
self.slacks = np.append(self.slacks, self.x - self.lb)
self.slacks = np.append(self.slacks, self.slack_rhs)
if self.slacks.ndim > 1:
self.slacks = self.slacks.flatten()
self.update_slacks = False
self.update_inv_slacks = True
def objective(self, x=None):
if x is not None:
self.update_x(x)