forked from PSORLab/SourceCodeMcCormick.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubroutines.jl
2547 lines (2151 loc) · 126 KB
/
subroutines.jl
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 EAGO: optimize_hook!
# Now on to the overloading of the B&B algorithm.
function EAGO.optimize_hook!(t::ExtendGPU, m::Optimizer)
m._global_optimizer._parameters.branch_variable = m._parameters.branch_variable
EAGO.initial_parse!(m)
optimize_gpu!(m)
end
function optimize_gpu!(m::Optimizer{R,S,Q}) where {R,S,Q<:EAGO.ExtensionType}
solve_gpu!(m._global_optimizer)
EAGO.unpack_global_solution!(m)
return
end
"""
global_solve_gpu_multi!
This version gets information about multiple nodes before running the lower problem solves
in parallel on the GPU. Information is successively added to a buffer in the DynamicExtGPU
extension which eventually solves the lower problems in parallel.
Upper problems are still solved using an ODERelaxProb and following the same techniques
as in the "normal" DynamicExt extension.
"""
function solve_gpu!(m::EAGO.GlobalOptimizer)
# Turn off garbage collection
GC.enable(false)
# Identify the extension
ext = EAGO._ext(m)
# Set node count to 1
m._node_count = 1
# Prepare to run branch-and-bound
EAGO.parse_global!(m)
EAGO.presolve_global!(m)
EAGO.print_preamble!(m)
# Run the NLP solver to get a start-point upper bound with multi-starting (In development)
multistart_upper!(m)
# Fill the stack with multiple nodes for the GPU to parallelize
prepopulate!(m)
# Pre-allocate storage vectors
ext.node_storage = Vector{EAGO.NodeBB}(undef, ext.node_limit)
ext.lower_bound_storage = Vector{Float64}(undef, ext.node_limit)
# Run branch and bound; terminate when the stack is empty or when some
# tolerance or limit is hit
while !EAGO.termination_check(m)
# Update iteration counter
m._iteration_count += 1
# Garbage collect every gc_freq iterations
if mod(m._iteration_count, EAGO._ext(m).gc_freq)==0
GC.enable(true)
GC.gc(false)
GC.enable(false)
end
# Fathoming step
EAGO.fathom!(m)
# Extract up to `node_limit` nodes from the main problem stack
count = min(ext.node_limit, m._node_count)
ext.node_storage[1:count] .= EAGO.popmin!(m._stack, count)
for i = 1:count
ext.all_lvbs[i,:] .= ext.node_storage[i].lower_variable_bounds
ext.all_uvbs[i,:] .= ext.node_storage[i].upper_variable_bounds
end
ext.node_len = count
m._node_count -= count
# Solve all the nodes in parallel
lower_and_upper_problem!(m)
EAGO.print_results!(m, true)
for i in 1:ext.node_len
make_current_node!(m)
# Check for infeasibility and store the solution
if m._lower_feasibility && !EAGO.convergence_check(m)
EAGO.print_results!(m, false)
EAGO.store_candidate_solution!(m)
# Perform post processing on each node I'm keeping track of
# postprocess_total += @elapsed EAGO.postprocess!(m)
# EAGO.postprocess!(m)
# m._last_postprocessing_time += @elapsed EAGO.postprocess!(m)
# Branch the nodes if they're feasible
# if m._postprocess_feasibility
EAGO.branch_node!(m)
# end
end
end
EAGO.set_global_lower_bound!(m)
m._run_time = time() - m._start_time
m._time_left = m._parameters.time_limit - m._run_time
EAGO.log_iteration!(m)
EAGO.print_iteration!(m, false)
end
EAGO.print_iteration!(m, true)
EAGO.set_termination_status!(m)
EAGO.set_result_status!(m)
EAGO.print_solution!(m)
# Turn back on garbage collection
GC.enable(true)
GC.gc()
end
# Helper functions here
function prepopulate!(t::ExtendGPU, m::EAGO.GlobalOptimizer)
if t.prepopulate == true
# println("Prepopulating with $(t.node_limit) total nodes")
# Calculate the base number of splits we need for each parameter
splits = floor(t.node_limit^(1/t.np))
# Add splits to individual parameters as long as we don't go over
# the limit
split_groups = splits*ones(t.np)
tracker = splits^(t.np)
for i = 1:t.np
if tracker * (split_groups[i]+1)/(split_groups[i]) < t.node_limit
tracker = tracker * (split_groups[i]+1)/(split_groups[i])
split_groups[i] += 1
end
end
# Now we have the correct number of split groups, but we still have to
# find the break points and add in additional nodes
main_node = EAGO.popmin!(m._stack)
lvbs = main_node.lower_variable_bounds
uvbs = main_node.upper_variable_bounds
split_vals = (uvbs .- lvbs)./(split_groups)
# Now we create nodes, adding in additional points as necessary
split_tracker = zeros(t.np)
lbd = copy(lvbs)
ubd = copy(uvbs)
additional_points = t.node_limit - tracker
for i = 1:tracker
for j = 1:t.np
lbd[j] = lvbs[j] + (split_tracker[j])*split_vals[j]
ubd[j] = lvbs[j] + (split_tracker[j]+1)*split_vals[j]
end
if additional_points > 0
# For additional points, just split the node in half at an arbitrary
# parameter (and use modulo so it cycles through parameters)
adjust = Int((i % t.np)+1)
midL = copy(lbd)
midU = copy(ubd)
midL[adjust] = (lbd[adjust] + ubd[adjust])/2
midU[adjust] = (lbd[adjust] + ubd[adjust])/2
push!(m._stack, EAGO.NodeBB(copy(lbd), copy(midU), main_node.is_integer, main_node.continuous,
max(main_node.lower_bound, m._lower_objective_value),
min(main_node.upper_bound, m._upper_objective_value),
1, main_node.cont_depth, i, EAGO.BD_NEG, 1, 0.0))
push!(m._stack, EAGO.NodeBB(copy(midL), copy(ubd), main_node.is_integer, main_node.continuous,
max(main_node.lower_bound, m._lower_objective_value),
min(main_node.upper_bound, m._upper_objective_value),
1, main_node.cont_depth, i, EAGO.BD_POS, 1, 0.0))
additional_points -= 1
else
pos_or_neg = (i - (tracker/2)) < 0 ? EAGO.BD_NEG : EAGO.BD_POS
push!(m._stack, EAGO.NodeBB(copy(lbd), copy(ubd), main_node.is_integer, main_node.continuous,
max(main_node.lower_bound, m._lower_objective_value),
min(main_node.upper_bound, m._upper_objective_value),
1, main_node.cont_depth, i, pos_or_neg, 1, 0.0))
end
split_tracker[1] += 1
for j = 1:(t.np-1)
if split_tracker[j] == split_groups[j]
split_tracker[j] = 0
split_tracker[j+1] += 1
end
end
end
m._node_count = t.node_limit
end
end
prepopulate!(m::EAGO.GlobalOptimizer{R,S,Q}) where {R,S,Q<:EAGO.ExtensionType} = prepopulate!(EAGO._ext(m), m)
"""
$(TYPEDSIGNATURES)
Adds the global optimizer's current node to the lower problem, and
extracts information so that the extension can solve the Ensemble
Problem.
"""
function add_to_substack!(t::ExtendGPU, m::EAGO.GlobalOptimizer)
# For now, we'll assume that all points are feasible. This can be
# changed in the future, once non-trivial constraints are considered
# Add the current node to ExtendGPU's internal stack
t.node_storage[t.node_len+1] = m._current_node
# Add the current node's lower and upper variable bounds to the storage
# to pass to the gpu
t.all_lvbs[t.node_len+1, :] = m._current_node.lower_variable_bounds
t.all_uvbs[t.node_len+1, :] = m._current_node.upper_variable_bounds
# Increment the node length tracker
t.node_len += 1
return nothing
end
add_to_substack!(m::EAGO.GlobalOptimizer{R,S,Q}) where {R,S,Q<:EAGO.ExtensionType} = add_to_substack!(EAGO._ext(m), m)
# Solve the lower and upper problem for all nodes simultaneously, using the convex_func
# function from the ExtendGPU extension
function lower_and_upper_problem!(t::PointwiseGPU, m::EAGO.GlobalOptimizer)
# Step 1) Bring the bounds into the GPU
lvbs_d = CuArray(t.all_lvbs)
uvbs_d = CuArray(t.all_uvbs) # [points x num_vars]
# Step 2) Preallocate points to evaluate
l, w = size(t.all_lvbs) #points, num_vars
np = 2*w+2 #Adding an extra for upper bound calculations
eval_points = Vector{CuArray{Float64}}(undef, 3*w)
for i = 1:w
eval_points[3i-2] = CuArray{Float64}(undef, l*np)
eval_points[3i-1] = repeat(lvbs_d[:,i], inner=np)
eval_points[3i] = repeat(uvbs_d[:,i], inner=np)
end
evals_d = CuArray{Float64}(undef, l*np)
results_d = CuArray{Float64}(undef, l)
# Step 3) Fill in each of these points
for i = 1:w
eval_points[3i-2][1:np:end] .= (lvbs_d[:,i].+uvbs_d[:,i])./2
for j = 2:np-1
if j==2i
eval_points[3i-2][j:np:end] .= (lvbs_d[:,i].+uvbs_d[:,i])./2 .+ t.α.*(uvbs_d[:,i].-lvbs_d[:,i])./2
elseif j==2i+1
eval_points[3i-2][j:np:end] .= (lvbs_d[:,i].+uvbs_d[:,i])./2 .- t.α.*(uvbs_d[:,i].-lvbs_d[:,i])./2
else
eval_points[3i-2][j:np:end] .= (lvbs_d[:,i].+uvbs_d[:,i])./2
end
end
# Now we do np:np:end. Each one is set to the center of the variable bounds,
# creating a degenerate interval. This gives us the upper bound.
eval_points[3i-2][np:np:end] .= (lvbs_d[:,i].+uvbs_d[:,i])./2
eval_points[3i-1][np:np:end] .= (lvbs_d[:,i].+uvbs_d[:,i])./2
eval_points[3i][np:np:end] .= (lvbs_d[:,i].+uvbs_d[:,i])./2
end
# Step 4) Prepare the input vector for the convex function
input = Vector{CuArray{Float64}}(undef, 0)
for i = 1:w
push!(input, [eval_points[3i-2], eval_points[3i-2], eval_points[3i-1], eval_points[3i]]...)
end
# Step 5) Perform the calculations
evals_d .= t.convex_func(input...) #This should return a CuArray of the evaluations, same length as each input
# Step 6) Extract the results and return to the CPU by filling in the lower/upper bound storages
results_d .= evals_d[1:np:end]
for i = 1:w
results_d .-= (max.(evals_d[2i:np:end], evals_d[2i+1:np:end]).-evals_d[1:np:end])./t.α
end
t.lower_bound_storage .= Array(results_d)
t.upper_bound_storage .= Array(evals_d[np:np:end])
less_val = t.upper_bound_storage .< m._global_upper_bound
if any(==(true), less_val)
println("Should be lower")
@show t.upper_bound_storage[less_val]
end
return nothing
end
lower_and_upper_problem!(m::EAGO.GlobalOptimizer{R,S,Q}) where {R,S,Q<:EAGO.ExtensionType} = lower_and_upper_problem!(EAGO._ext(m), m)
# A separate version of the lower_and_upper_problem! function that uses subgradients
function lower_and_upper_problem!(t::SubgradGPU, m::EAGO.GlobalOptimizer)
# Step 1) Bring the bounds into the GPU
lvbs_d = CuArray(t.all_lvbs)
uvbs_d = CuArray(t.all_uvbs) # [points x num_vars]
# Step 2) Preallocate points to evaluate
l, w = size(t.all_lvbs) #points, num_vars
np = 2*w+2 #Adding an extra for upper bound calculations
eval_points = Vector{CuArray{Float64}}(undef, 3*w) #Only 3x because one is repeated
for i = 1:w
eval_points[3i-2] = CuArray{Float64}(undef, l*np)
eval_points[3i-1] = repeat(lvbs_d[:,i], inner=np)
eval_points[3i] = repeat(uvbs_d[:,i], inner=np)
end
bounds_d = CuArray{Float64}(undef, l*np)
# Step 3) Fill in each of these points
for i = 1:w #1-3
eval_points[3i-2][1:np:end] .= (lvbs_d[:,i].+uvbs_d[:,i])./2
for j = 2:np-1 #2-7
if j==2i
eval_points[3i-2][j:np:end] .= (lvbs_d[:,i].+uvbs_d[:,i])./2 .+ t.α.*(uvbs_d[:,i].-lvbs_d[:,i])./2
elseif j==2i+1
eval_points[3i-2][j:np:end] .= (lvbs_d[:,i].+uvbs_d[:,i])./2 .- t.α.*(uvbs_d[:,i].-lvbs_d[:,i])./2
else
eval_points[3i-2][j:np:end] .= (lvbs_d[:,i].+uvbs_d[:,i])./2
end
end
# Now we do np:np:end. Each one is set to the center of the variable bounds,
# creating a degenerate interval. This gives us the upper bound.
eval_points[3i-2][np:np:end] .= (lvbs_d[:,i].+uvbs_d[:,i])./2
eval_points[3i-1][np:np:end] .= (lvbs_d[:,i].+uvbs_d[:,i])./2
eval_points[3i][np:np:end] .= (lvbs_d[:,i].+uvbs_d[:,i])./2
end
# Step 4) Prepare the input vector for the convex function
input = Vector{CuArray{Float64}}(undef, 0)
for i = 1:w
push!(input, [eval_points[3i-2], eval_points[3i-2], eval_points[3i-1], eval_points[3i]]...)
end
# Step 5) Perform the calculations
func_output = t.convex_func_and_subgrad(input...) # n+2-dimensional
# Step 6) Use values and subgradients to calculate lower bounds
bounds_d .= func_output[1]
for i = 1:w
bounds_d .+= -(func_output[i+2] .>= 0.0).*func_output[i+2].*(eval_points[3i-2][:] .- eval_points[3i-1][:]) .-
(func_output[i+2] .<= 0.0).*func_output[i+2].*(eval_points[3i-2][:] .- eval_points[3i][:])
end
# Add to lower and upper bound storage
t.lower_bound_storage .= max.(Array(func_output[2][1:np:end]), [maximum(bounds_d[i:i+np-2]) for i in 1:np:l*np])
t.upper_bound_storage .= Array(bounds_d[np:np:end])
return nothing
end
# A third version of lower_and_upper_problem! that uses the new GPU Simplex algorithm
function lower_and_upper_problem_old!(t::SimplexGPU_OnlyObj, m::EAGO.GlobalOptimizer)
# Step 1) Bring the bounds into the GPU
lvbs_d = CuArray(t.all_lvbs)
uvbs_d = CuArray(t.all_uvbs) # [points x num_vars]
# Step 2) Preallocate points to evaluate
l, w = size(t.all_lvbs) #points, num_vars
np = 2*w+2 #Number of points; Adding an extra for upper bound calculations
eval_points = Vector{CuArray{Float64}}(undef, 3*w) #Only 3x because one is repeated
for i = 1:w
eval_points[3i-2] = CuArray{Float64}(undef, l*np)
eval_points[3i-1] = repeat(lvbs_d[:,i], inner=np)
eval_points[3i] = repeat(uvbs_d[:,i], inner=np)
end
bounds_d = CuArray{Float64}(undef, l)
# Step 3) Fill in each of these points
for i = 1:w #1-3
eval_points[3i-2][1:np:end] .= (lvbs_d[:,i].+uvbs_d[:,i])./2
for j = 2:np-1 #2-7
if j==2i
eval_points[3i-2][j:np:end] .= (lvbs_d[:,i].+uvbs_d[:,i])./2 .- t.α.*(uvbs_d[:,i].-lvbs_d[:,i])./2
elseif j==2i+1
eval_points[3i-2][j:np:end] .= (lvbs_d[:,i].+uvbs_d[:,i])./2 .+ t.α.*(uvbs_d[:,i].-lvbs_d[:,i])./2
else
eval_points[3i-2][j:np:end] .= (lvbs_d[:,i].+uvbs_d[:,i])./2
end
end
# Now we do np:np:end. Each one is set to the center of the variable bounds,
# creating a degenerate interval. This gives us the upper bound.
eval_points[3i-2][np:np:end] .= (lvbs_d[:,i].+uvbs_d[:,i])./2
eval_points[3i-1][np:np:end] .= (lvbs_d[:,i].+uvbs_d[:,i])./2
eval_points[3i][np:np:end] .= (lvbs_d[:,i].+uvbs_d[:,i])./2
end
# Step 4) Prepare the input vector for the convex function
input = Vector{CuArray{Float64}}(undef, 0)
for i = 1:w
push!(input, [eval_points[3i-2], eval_points[3i-2], eval_points[3i-1], eval_points[3i]]...)
end
# Step 5) Perform the calculations
func_output = t.convex_func_and_subgrad(input...) # n+2-dimensional
# Also need whatever constraints!!
# Step 6) Use values and subgradients to prepare the stacked Simplex tableau
# First things first, we can prepare the "b" vector and see if we need any auxiliary systems.
# This step calculates the intercept of b at x=x_lo, which is equivalent to calculating
# the intercept at x=0 and then later shifting x_lo to 0, but without the extra re-calculation
# steps
b_start = func_output[1]
for i = 1:w
b_start -= func_output[i+2].*(eval_points[3i-2] .- eval_points[3i-1])
# func_output[i+2] is the subgradient of the convex relaxation in the i'th dimension
# eval_points[3i-2] is the cv/cc point used to obtain the relaxation
# eval_points[3i-1] is the lower bound for this relaxation
# eval_points[3i] is the upper bound (which isn't used here)
# Note that <= [upper bound] will change to <= [upper bound] - [lower bound]
# for each variable, later
end
if all(<=(0.0), b_start)
#If b_start is all nonpositive, we don't need any auxiliary systems
# Start making the tableau as normal, since we have a basic feasible solution at the start.
# Create an extended b_array
b_array = vcat([vcat(-b_start[np*(j-1)+1:np*j-1], # First "1:(np-1)" points for each node
uvbs_d[j,:].-lvbs_d[j,:], # Upper bound minus lower bound
0.0) # 0.0 for the objective function row
for j=1:l]...) # Repeat for every node
# Prepare the epigraph variable columns. We're minimizing "Z", but since "Z" is unbounded, we
# convert it to Z = Z_pos - Z_neg, where Z_pos, Z_neg >= 0.0. The first column will be Z_pos,
# and the second column will be Z_neg. The upper bound rows and auxiliary system objective
# function row will have these as 0; the objective function row will be [1, -1] (minimizing
# Z_pos - Z_neg); and the constraints associated with Z will be [-1, 1] (-Z = -Z_pos + Z_neg)
epigraph = hcat(-CUDA.ones(Float64, length(b_array)), CUDA.ones(Float64, length(b_array)))
for i = 1:w
# Starting at the first upper bound, repeat for every tableau
epigraph[np+i-1 : np+w : end, :] .= 0.0
end
epigraph[np+w : np+w : end, :] .*= -1.0 # The main objective function is opposite the other rows (minimizing Z)
epigraph[np+w+1 : np+w : end, :] .= 0.0 # The epigraph column is 0 in the auxiliary objective row
# Combine the epigraph columns, "A" matrix, slack variable columns, and "b" array into the stacked tableaus
tableaus = hcat(epigraph, # Epigraph variable columns
[vcat([vcat(func_output[i+2][np*(j-1)+1:np*j-1], # >>Subgradient values for the i'th variable for the j'th node
CUDA.zeros(Float64, w), # >>Zeros for upper bound constraints (will fill in later with 1.0s)
0.0) # >>0.0 for the objective function row
for j = 1:l]...) # Repeat for every j'th node vertically
for i = 1:w]..., # Add a column for every i'th variable
[CUDA.zeros(Float64, length(b_array)) for _ = 1:(np-1)+w]..., # Slack variables (will fill in later with 1.0s)
b_array) # The array of b's
# Fill in the upper bound constraint indices and the slack variables
for i = 1:w
tableaus[np+i-1 : np+w : end, i+2] .= 1.0
end
for i = 1:(np-1)+w
tableaus[i:np+w:end, (w+2)+i] .= 1.0
end
tableaus .= parallel_simplex(tableaus, np+w)
else
# It was detected that some slack variable coefficient would be negative, so we need to make auxiliary systems.
# Note: It's probably worth it to only do auxiliary systems for the tableaus that will need it. At least check
# to see how common this is, and whether it'll be necessary...
# Create the extended b_array
b_array = vcat([vcat(-b_start[np*(j-1)+1:np*j-1], # First "1:(np-1)" points for each node
uvbs_d[j,:].-lvbs_d[j,:], # Upper bound minus lower bound
0.0, # 0.0 for the objective function row
0.0) # 0.0 for the auxiliary system objective function row
for j=1:l]...) # Repeat for every node
# (NOTE: Should we be scaling all the variables/subgradients so that the variables are bounded on [0, 1]?)
# Prepare the epigraph variable columns. We're minimizing "Z", but since "Z" is unbounded, we
# convert it to Z = Z_pos - Z_neg, where Z_pos, Z_neg >= 0.0. The first column will be Z_pos,
# and the second column will be Z_neg. The upper bound rows and auxiliary system objective
# function row will have these as 0; the objective function row will be [1, -1] (minimizing
# Z_pos - Z_neg); and the constraints associated with Z will be [-1, 1] (-Z = -Z_pos + Z_neg)
epigraph = hcat(-CUDA.ones(Float64, length(b_array)), CUDA.ones(Float64, length(b_array)))
for i = 1:w
# Starting at the first upper bound, repeat for every tableau
# (which has np+w+1 rows thanks to the auxiliary row)
epigraph[np+i-1 : np+w+1 : end, :] .= 0.0
end
epigraph[np+w : np+w+1 : end, :] .*= -1.0 # The main objective function is opposite the other rows (minimizing Z)
epigraph[np+w+1 : np+w+1 : end, :] .= 0.0 # The epigraph column is 0 in the auxiliary objective row
# Combine the epigraph columns, "A" matrix, slack variable columns, and "b" array into the stacked tableaus
tableaus = hcat(epigraph, # Epigraph variable columns
[vcat([vcat(func_output[i+2][np*(j-1)+1:np*j-1], # >>Subgradient values for the i'th variable for the j'th node
CUDA.zeros(Float64, w), # >>Zeros for upper bound constraints (will fill in later with 1.0s)
0.0, # >>0.0 for the objective function row
0.0) # >>0.0 for the auxiliary objective function row
for j = 1:l]...) # Repeat for every j'th node vertically
for i = 1:w]..., # Add a column for every i'th variable
[CUDA.zeros(Float64, length(b_array)) for _ = 1:2*((np-1)+w)]..., # Slack and auxiliary variables (will fill in later with 1.0s)
b_array) # The array of b's
# Fill in the upper bound constraint indices
for i = 1:w
tableaus[np+i-1 : np+w+1 : end, i+2] .= 1.0 #np+w+1 length now, because of the auxiliary row
end
# Fill in the slack variables like normal, and then add auxiliary variables as needed
signs = sign.(tableaus[:,end])
signs[signs.==0] .= 1.0
for i = 1:np+w-1
tableaus[i:np+w+1:end, (w+2)+i] .= 1.0 #np+w+1 length now, because of the auxiliary row
# If the "b" row is negative, do the following:
# 1) Flip the row so that "b" is positive
# 2) Subtract the entire row FROM the auxiliary objective row
# 3) Add an auxiliary variable for this row
tableaus[i:np+w+1:end, :] .*= signs[i:np+w+1:end] #Flipped the row if b was negative
tableaus[np+w+1 : np+w+1 : end, :] .-= (signs[i:np+w+1:end].<0.0).*tableaus[i:np+w+1:end, :] #Row subtracted from auxiliary objective row
tableaus[i:np+w+1:end, (w+2)+np+w-1+i] .+= (signs[i:np+w+1:end].<0.0).*1.0
end
# Send the tableaus to the parallel_simplex algorithm, with the "aux" flag set to "true"
tableaus .= parallel_simplex(tableaus, np+w+1, aux=true)
if all(abs.(tableaus[np+w+1:np+w+1:end,end]).<=1E-10)
# Delete the [np+w+1 : np+w+1 : end] rows and the [w+1+(np+w-1) + 1 : end-1] columns
# Note: is it faster to NOT remove the rows/columns and just have an adjusted simplex
# algorithm that ignores them? Maybe, maybe not. I'll test later.
tableaus = tableaus[setdiff(1:end, np+w+1:np+w+1:end), setdiff(1:end, w+2+(np+w-1):end-1)]
tableaus .= parallel_simplex(tableaus, np+w)
else
warn = true
end
end
# display(Array(func_output[2]))
# display(Array(tableaus))
# display(Array(-tableaus[np+w:np+w:end,end]))
# display(Array(func_output[2][1:np:end]))
# display(Array(max.(func_output[2][1:np:end], -tableaus[np+w:np+w:end,end])))
# Step 8) Add results to lower and upper bound storage
t.lower_bound_storage .= Array(max.(func_output[2][1:np:end], -tableaus[np+w:np+w:end,end]))
t.upper_bound_storage .= Array(func_output[1][np:np:end])
return nothing
end
# An even newer Simplex
function lower_and_upper_problem_slightly_old!(t::SimplexGPU_OnlyObj, m::EAGO.GlobalOptimizer)
# Step 1) Bring the bounds into the GPU
lvbs_d = CuArray(t.all_lvbs)
uvbs_d = CuArray(t.all_uvbs) # [points x num_vars]
# Step 2) Set up points to evaluate, which are the centers of every node
l, w = size(t.all_lvbs) #points, num_vars
np = 1 #Number of evaluations per node; Adding an extra for upper bound calculations
eval_points = Vector{CuArray{Float64}}(undef, 3*w) #Only 3x because cv is the same as cc
temp_lvbs = CuArray{Float64}(undef, l) #Pre-allocate slices of lvbs
temp_uvbs = CuArray{Float64}(undef, l) #Pre-allocate slices of uvbs
for i = 1:w
# Temporarily hold slices of variable bounds
temp_lvbs .= lvbs_d[:,i]
temp_uvbs .= uvbs_d[:,i]
# Set up bounds to evaluate
eval_points[3i-1] = repeat(temp_lvbs, inner=np)
eval_points[3i] = repeat(temp_uvbs, inner=np)
# Calculate midpoints of the bounds
eval_points[3i-2] = (eval_points[3i-1].+eval_points[3i])./2
# Correct the bounds for the upper bound calculation (every 2 evaluations)
# eval_points[3i-1][np:np:end] .= eval_points[3i-2][np:np:end]
# eval_points[3i][np:np:end] .= eval_points[3i-2][np:np:end]
end
# println("After initial setup:")
# CUDA.memory_status();println("")
# Step 3) Perform the calculations (Note: also need to add in constraint handling. Perhaps
# that will be a different function, so that this one can stay as-is?
func_output = t.convex_func_and_subgrad(
([[eval_points[3i-2],eval_points[3i-2],eval_points[3i-1],eval_points[3i]] for i=1:w]...)...) # n+2-dimensional
# println("After calling the function:")
# CUDA.memory_status();println("")
# Might as well save the upper bound right now, since we have it
t.upper_bound_storage .= Array(func_output[1][np:np:end])
# println("Saving upper bound results:")
# CUDA.memory_status();println("")
# Step 4) Use values and subgradients to prepare the stacked Simplex tableau.
# Based on this procedure, we should never need an auxiliary system? Check on that to
# be sure, because if we don't need an auxiliary system, that's much easier
# Preallocate subgradient matrices and the b vector
subgradients = CuArray{Float64}(undef, l, w)
corrected_subgradients = CuArray{Float64}(undef, l, w)
b_val = CuArray{Float64}(undef, l, 1)
# println("More preallocations:")
# CUDA.memory_status();println("")
# Extract subgradients and apply a correction for shifting variables to [0,1]
subgradients .= hcat([func_output[i+2][1:np:end] for i in 1:w]...) #Only for the lower bound, not upper bound
corrected_subgradients .= (subgradients).*(uvbs_d .- lvbs_d)
# Calculate corrected "b" values based on the intercept at the evaluation point
# and the corrections for shifted variables
b_val .= sum(hcat([eval_points[3i-2][1:np:end] for i=1:w]...).*subgradients, dims=2) .- func_output[1][1:np:end] .- sum(lvbs_d.*subgradients, dims=2)
# If there are any negative values in b, we can simply change the epigraph
# variable to be that much higher to make the minimum 0. Note that this
# won't work for constraints, it only works because it's for the objective
# function and we have "z" in the tableau.
add_val = 0.0
if any(<(0.0), b_val)
add_val = -minimum(b_val)
b_val .+= add_val
end
# Free up eval_points since we no longer need it
CUDA.unsafe_free!.(eval_points)
# Preemptively determine how many slack variables we'll need. This is going to be
# the total number of cuts (an input to this function), plus one for the lower bound
# value, plus the number of variables (since each has an upper bound of 1)
slack_count = t.max_cuts+1+w # n_cuts, lower bound, w [upper bounds]
# Create the stacked tableau as a big array of 0's, and then we fill it in as necessary.
tableau = CUDA.zeros(Float64, l*(slack_count+1), 1+w+slack_count+1)
solution_tableau = similar(tableau)
# println("Tableau prepared:")
# CUDA.memory_status();println("")
# Fill in the first column, corresponding to the epigraph variable
tableau[1:(slack_count+1):end,1] .= 1.0 # Lower bound constraint
tableau[2:(slack_count+1):end,1] .= 1.0 # Only one cut to consider for now
tableau[(slack_count+1):(slack_count+1):end,1] .= -1.0 # Objective row
# Fill in the corrected subgradients in their respective columns
tableau[2:(slack_count+1):end,2:1+w] .= corrected_subgradients
# Fill in the slack variables (besides the rows for cuts we haven't done yet)
for i = 1:slack_count
if (i<=t.max_cuts+1) && (t.max_cuts>1) && (i>2) #Reserving rows 3 to max_cuts+1 for future cuts
continue
end
tableau[i:(slack_count+1):end,1+w+i] .= 1.0
end
# Fill in the lower bound for the epigraph variable
tableau[1:(slack_count+1):end,end] .= -func_output[2][1:np:end] .+ add_val
# Fill in the value for the first cut (b)
tableau[2:(slack_count+1):end,end] .= b_val
# Fill in the upper bounds for all the variables, which are all 1's
for i = 1:w
tableau[t.max_cuts+1+i:(slack_count+1):end,1+i] .= 1.0 # The variable itself
tableau[t.max_cuts+1+i:(slack_count+1):end,end] .= 1.0 # The variable's upper bound (always 1 because we shifted it)
end
# Make sure all the rightmost column values are positive
if any(<(0.0), tableau[:,end])
# display(tableau[:,end])
error("Check b_val, might need an auxiliary system (or more creativity)")
end
# println("Tableau filled:")
# CUDA.memory_status();println("")
# Free up the func outputs since we no longer need them
CUDA.unsafe_free!.(func_output)
# Pass the tableau through the simplex algorithm and see what we get out of it
# (Note that the solution values will be negated. That's fine, we don't actually
# care what they are just yet.)
solution_tableau .= tableau
# println("Right before simplex:")
# CUDA.memory_status();println("")
parallel_simplex(solution_tableau, slack_count+1)
# println("Right after simplex:")
# CUDA.memory_status();println("")
# Now we need to add another cut, which means we have to extract out the solution
# from the tableau, convert back into un-shifted variables, pass it back through
# the convex evaluator, and add rows to the tableau.
# Preallocate some arrays checks we'll be using
tableau_vals = CuArray{Float64}(undef, l,(slack_count+1))
variable_vals = CuArray{Float64}(undef, l,(slack_count+1))
bool_check = CuArray{Bool}(undef, l,(slack_count+1))
zero_check = CuArray{Bool}(undef, l,(slack_count+1))
short_eval_points = Vector{CuArray{Float64}}(undef, w) # Only need pointwise evaluations
# println("Preallocations for next cuts:")
# CUDA.memory_status();println("")
for cut = 1:t.max_cuts-1 # Two additional cuts.
# Extract solution values from the tableau to decide where the evaluations are.
# How to do this... Search through [2:w+1] columns to find columns that are all 0's with
# single 1's.
# Maybe think about this for an individual block. We have a block of size (slack_count+1, w),
# and we need to identify which rows are relevant. We could go by row, but then we'd have
# to get to the end before we could identify anything... we could preallocate space for
# the variable values, at least---or, wait, that's already done with eval_points[3i-2].
# So that's nice. Uhhh... okay...
# Figure out which ones are "correct" for each variable?
for i = 1:w
temp_lvbs .= lvbs_d[:,i]
temp_uvbs .= uvbs_d[:,i]
tableau_vals .= reshape(solution_tableau[:,end], l,(slack_count+1))
variable_vals .= reshape(solution_tableau[:,1+i], l,(slack_count+1))
bool_check .= (variable_vals .== 1.0)
zero_check .= (variable_vals .== 0.0)
bool_check .&= (count(bool_check, dims=2).==1)
bool_check .&= (count(zero_check, dims=2).==slack_count)
tableau_vals .*= bool_check
short_eval_points[i] = min.(0.95, max.(0.05, sum(tableau_vals, dims=2))).*(temp_uvbs .- temp_lvbs).+temp_lvbs
end
# println("Cut $cut, found short eval points:")
# CUDA.memory_status();println("")
# Okay, so now we have the points to evaluate, we need to call the function again
func_output = t.convex_func_and_subgrad(
([[short_eval_points[i],short_eval_points[i],lvbs_d[:,i],uvbs_d[:,i]] for i=1:w]...)...) # n+2-dimensional
# println("Function called again:")
# CUDA.memory_status();println("")
# As before, calculate corrected subgradients and the new b_values
subgradients .= hcat([func_output[i+2] for i in 1:w]...) #Only for the lower bound, not upper bound
corrected_subgradients .= (subgradients).*(uvbs_d .- lvbs_d)
b_val .= sum(hcat([short_eval_points[i] for i=1:w]...).*subgradients, dims=2) .- func_output[1] .- sum(lvbs_d.*subgradients, dims=2)
# Add in the extra factor
b_val .+= add_val
# If any of b_val is [still] negative, update add_val and the rest of the tableau
if any(<(0.0), b_val)
update = -minimum(b_val)
b_val .+= update
tableau[1:slack_count+1:end, end] .+= update
for i = 2:(2+cut-1)
tableau[i:slack_count+1:end, end] .+= update
end
add_val += update
end
# Clear the short_eval_points from memory
CUDA.unsafe_free!.(short_eval_points)
# We can now place in these values into the tableau in the spots we left open earlier
tableau[2+cut:slack_count+1:end, 1] .= 1.0
tableau[2+cut:slack_count+1:end, 2:1+w] .= corrected_subgradients
tableau[2+cut:slack_count+1:end, 1+w+2+cut] .= 1.0
tableau[2+cut:slack_count+1:end, end] .= b_val
# Adjust the final line of each problem to be the original minimization problem
tableau[(slack_count+1):(slack_count+1):end,:] .= hcat(-CUDA.one(Float64), CUDA.zeros(Float64, 1, w+slack_count+1))
# Run the simplex algorithm again
solution_tableau .= tableau
# println("Everything until simplex again")
# CUDA.memory_status();println("")
parallel_simplex(solution_tableau, slack_count+1)
# println("Right after simplex again:")
# CUDA.memory_status();println("")
end
# println("End of simplexing:")
# CUDA.memory_status();println("")
# Save the lower bounds
t.lower_bound_storage .= Array(-(solution_tableau[slack_count+1:slack_count+1:end,end] .- add_val))
# println("After lower bounds saved:")
# CUDA.memory_status();println("")
# Free variables we're finally done with
for i in [lvbs_d, uvbs_d, temp_lvbs, temp_uvbs, subgradients, corrected_subgradients, b_val,
tableau, solution_tableau, tableau_vals, variable_vals, bool_check, zero_check]
CUDA.unsafe_free!(i)
end
# println("Freed up storage, and done.:")
# CUDA.memory_status();println("")
# error()
return nothing
end
function lower_and_upper_problem_split!(t::SimplexGPU_OnlyObj, m::EAGO.GlobalOptimizer)
# Step 1) Bring the bounds into the GPU
lvbs_d = CuArray(t.all_lvbs)
uvbs_d = CuArray(t.all_uvbs) # [points x num_vars]
# Step 2) Set up points to evaluate, which are the centers of every node
l, w = size(t.all_lvbs) #points, num_vars
eval_points = (lvbs_d .+ uvbs_d)./2
# Step 3) Perform the calculations (Note: also need to add in constraint handling. Perhaps
# that will be a different function, so that this one can stay as-is?
# Upper bound calculations first; lower and upper bounds, and cv/cc, are the midpoints of nodes
# NOTE: Speed can be improved by 75% for this call if you switch it out with a separate
# function that only calculates the lower bound, for example. Or maybe more if you can
# make a normal GPU-compatible version of the objective function.
func_output = @views t.convex_func_and_subgrad(
([[eval_points[:,i],eval_points[:,i],eval_points[:,i],eval_points[:,i]] for i=1:w]...)...) # n+2-dimensional
t.upper_bound_storage .= Array(func_output)
# Free up the func outputs
CUDA.unsafe_free!(func_output)
# Now lower bound calculations. It's the same as upper bounds, but we use lvbs and uvbs.
func_output = @views t.convex_func_and_subgrad(
([[eval_points[:,i],eval_points[:,i],lvbs_d[:,i],uvbs_d[:,i]] for i=1:w]...)...) # n+2-dimensional
# Step 4) Use values and subgradients to prepare the stacked Simplex tableau.
# Based on this procedure, we should never need an auxiliary system? Check on that to
# be sure, because if we don't need an auxiliary system, that's much easier
# Preallocate subgradient matrices and the b vector
subgradients = CuArray{Float64}(undef, l, w)
corrected_subgradients = CuArray{Float64}(undef, l, w)
mid_times_sub = CuArray{Float64}(undef, l, w)
low_times_sub = CuArray{Float64}(undef, l, w)
b_val = CuArray{Float64}(undef, l, 1)
# Extract subgradients and apply a correction for shifting variables to [0,1]
subgradients .= hcat([func_output[i+2] for i in 1:w]...)
corrected_subgradients .= (subgradients).*(uvbs_d .- lvbs_d)
# Calculate corrected "b" values based on the intercept at the evaluation point
# and the corrections for shifted variables
mid_times_sub .= eval_points.*subgradients
low_times_sub .= lvbs_d.*subgradients
b_val .= sum(mid_times_sub, dims=2) .- func_output[1] .- sum(low_times_sub, dims=2)
# If there are any negative values in b, we can simply change the epigraph
# variable to be that much higher to make the minimum 0. Note that this
# won't work for constraints, it only works because it's for the objective
# function and we have "z" in the tableau.
add_val = 0.0
if any(<(0.0), b_val)
add_val = -minimum(b_val)
b_val .+= add_val
end
# Preemptively determine how many slack variables we'll need. This is going to be
# the total number of cuts (an input to this function), plus one for the lower bound
# value, plus the number of variables (since each has an upper bound of 1)
slack_count = t.max_cuts+1+w # n_cuts, lower bound, w [upper bounds]
# Create the stacked tableau as a big array of 0's, and then we fill it in as necessary.
tableau = CUDA.zeros(Float64, l*(slack_count+1), 1+w+slack_count+1)
solution_tableau = similar(tableau)
# Fill in the first column, corresponding to the epigraph variable
tableau[1:(slack_count+1):end,1] .= 1.0 # Lower bound constraint
tableau[2:(slack_count+1):end,1] .= 1.0 # Only one cut to consider for now
tableau[(slack_count+1):(slack_count+1):end,1] .= -1.0 # Objective row
# Fill in the corrected subgradients in their respective columns
tableau[2:(slack_count+1):end,2:1+w] .= corrected_subgradients
# Fill in the slack variables (besides the rows for cuts we haven't done yet)
for i = 1:slack_count
if (i<=t.max_cuts+1) && (t.max_cuts>1) && (i>2) #Reserving rows 3 to max_cuts+1 for future cuts
continue
end
tableau[i:(slack_count+1):end,1+w+i] .= 1.0
end
# Fill in the lower bound for the epigraph variable
tableau[1:(slack_count+1):end,end] .= -func_output[2] .+ add_val
# Fill in the value for the first cut (b)
tableau[2:(slack_count+1):end,end] .= b_val
# Fill in the upper bounds for all the variables, which are all 1's
for i = 1:w
tableau[t.max_cuts+1+i:(slack_count+1):end,1+i] .= 1.0 # The variable itself
tableau[t.max_cuts+1+i:(slack_count+1):end,end] .= 1.0 # The variable's upper bound (always 1 because we shifted it)
end
# Make sure all the rightmost column values are positive (not necessary, and for some
# reason this eats up GPU memory?)
# if any((@view tableau[:,end]) .< 0.0)
# error("Check b_val, might need an auxiliary system (or more creativity)")
# end
# Free up the func outputs since we no longer need them
CUDA.unsafe_free!.(func_output)
# Pass the tableau through the simplex algorithm and see what we get out of it
# (Note that the solution values will be negated. That's fine, we don't actually
# care what they are just yet.)
solution_tableau .= tableau
parallel_simplex(solution_tableau, slack_count+1)
# Now we need to add another cut, which means we have to extract out the solution
# from the tableau, convert back into un-shifted variables, pass it back through
# the convex evaluator, and add rows to the tableau.
# Preallocate some arrays checks we'll be using
tableau_vals = CuArray{Float64}(undef, l,(slack_count+1))
variable_vals = CuArray{Float64}(undef, l,(slack_count+1))
bool_check = CuArray{Bool}(undef, l,(slack_count+1))
zero_check = CuArray{Bool}(undef, l,(slack_count+1))
for cut = 1:t.max_cuts-1 # Two additional cuts.
# Extract solution values from the tableau to decide where the evaluations are.
# Figure out which ones are "correct" for each variable?
for i = 1:w
tableau_vals .= reshape((@view solution_tableau[:,end]), l,(slack_count+1))
variable_vals .= reshape((@view solution_tableau[:,1+i]), l,(slack_count+1))
bool_check .= (variable_vals .== 1.0)
zero_check .= (variable_vals .== 0.0)
bool_check .&= (count(bool_check, dims=2).==1)
bool_check .&= (count(zero_check, dims=2).==slack_count)
tableau_vals .*= bool_check
eval_points[:,i] .= @views min.(0.95, max.(0.05, sum(tableau_vals, dims=2))).*(uvbs_d[:,i] .- lvbs_d[:,i]).+lvbs_d[:,i]
end
# Okay, so now we have the points to evaluate, we need to call the function again
func_output = @views t.convex_func_and_subgrad(
([[eval_points[:,i],eval_points[:,i],lvbs_d[:,i],uvbs_d[:,i]] for i=1:w]...)...) # n+2-dimensional
# As before, calculate corrected subgradients and the new b_values
subgradients .= hcat([func_output[i+2] for i in 1:w]...) #Only for the lower bound, not upper bound
corrected_subgradients .= (subgradients).*(uvbs_d .- lvbs_d)
mid_times_sub .= eval_points.*subgradients
low_times_sub .= lvbs_d.*subgradients
b_val .= sum(mid_times_sub, dims=2) .- func_output[1] .- sum(low_times_sub, dims=2)
# Add in the extra factor
b_val .+= add_val
# If any of b_val is [still] negative, update add_val and the rest of the tableau
if any(<(0.0), b_val)
update = -minimum(b_val)
b_val .+= update
tableau[1:slack_count+1:end, end] .+= update
for i = 2:(2+cut-1)
tableau[i:slack_count+1:end, end] .+= update
end
add_val += update
end
# Free up the func outputs since we no longer need them
CUDA.unsafe_free!.(func_output)
# We can now place in these values into the tableau in the spots we left open earlier
tableau[2+cut:slack_count+1:end, 1] .= 1.0
tableau[2+cut:slack_count+1:end, 2:1+w] .= corrected_subgradients
tableau[2+cut:slack_count+1:end, 1+w+2+cut] .= 1.0
tableau[2+cut:slack_count+1:end, end] .= b_val
# Adjust the final line of each problem to be the original minimization problem
tableau[(slack_count+1):(slack_count+1):end,:] .= hcat(-CUDA.one(Float64), CUDA.zeros(Float64, 1, w+slack_count+1))
# Run the simplex algorithm again
solution_tableau .= tableau
parallel_simplex(solution_tableau, slack_count+1)
end
# Save the lower bounds (remembering to negate the values)
t.lower_bound_storage .= @views Array(-(solution_tableau[slack_count+1:slack_count+1:end,end] .- add_val))
for i in [lvbs_d, uvbs_d, eval_points, subgradients, corrected_subgradients, b_val,
tableau, solution_tableau, tableau_vals, variable_vals, bool_check, zero_check]
CUDA.unsafe_free!(i)
end
return nothing
end
function lower_and_upper_problem!(t::SimplexGPU_OnlyObj, m::EAGO.GlobalOptimizer)
t.lower_counter += 1
# Step 1) Bring the bounds into the GPU
lvbs_d = CuArray(t.all_lvbs[1:t.node_len,:])
uvbs_d = CuArray(t.all_uvbs[1:t.node_len,:]) # [points x num_vars]
# Step 2) Set up points to evaluate, which are the centers of every node
# l, w = size(t.all_lvbs) #points, num_vars
w = t.np
l = t.node_len
eval_points = (lvbs_d .+ uvbs_d)./2
# Step 3) Perform the calculations (Note: also need to add in constraint handling. Perhaps
# that will be a different function, so that this one can stay as-is?
# Perform both lower and upper bound calculations, stacked on top of one another. This
# is faster than splitting lower and upper bounding problems and calling the convex
# function twice,
t.relax_time += @elapsed CUDA.@sync func_output = @views t.convex_func_and_subgrad(
([[[eval_points[:,i];eval_points[:,i]],[eval_points[:,i];eval_points[:,i]],[lvbs_d[:,i];eval_points[:,i]],[uvbs_d[:,i];eval_points[:,i]]] for i=1:w]...)...) # n+2-dimensional
t.upper_bound_storage[1:t.node_len] .= @views Array(func_output[1][l+1:end])
# Step 4) Use values and subgradients to prepare the stacked Simplex tableau.
# Based on this procedure, we should never need an auxiliary system? Check on that to
# be sure, because if we don't need an auxiliary system, that's much easier
# Preallocate subgradient matrices and the b vector
subgradients = CuArray{Float64}(undef, l, w)
corrected_subgradients = CuArray{Float64}(undef, l, w)
mid_times_sub = CuArray{Float64}(undef, l, w)
low_times_sub = CuArray{Float64}(undef, l, w)