-
Notifications
You must be signed in to change notification settings - Fork 367
/
Copy pathosqp.c
1472 lines (1169 loc) · 41.6 KB
/
osqp.c
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
#include "osqp.h"
#include "auxil.h"
#include "util.h"
#include "scaling.h"
#include "glob_opts.h"
#ifndef EMBEDDED
# include "polish.h"
#endif /* ifndef EMBEDDED */
#ifdef CTRLC
# include "ctrlc.h"
#endif /* ifdef CTRLC */
#ifndef EMBEDDED
# include "lin_sys.h"
#endif /* ifndef EMBEDDED */
/**********************
* Main API Functions *
**********************/
void osqp_set_default_settings(OSQPSettings *settings) {
settings->scaling = SCALING; /* heuristic problem scaling */
#if EMBEDDED != 1
settings->adaptive_rho = ADAPTIVE_RHO;
settings->adaptive_rho_interval = ADAPTIVE_RHO_INTERVAL;
settings->adaptive_rho_tolerance = (c_float)ADAPTIVE_RHO_TOLERANCE;
# ifdef PROFILING
settings->adaptive_rho_fraction = (c_float)ADAPTIVE_RHO_FRACTION;
# endif /* ifdef PROFILING */
#endif /* if EMBEDDED != 1 */
settings->rho = (c_float)RHO; /* ADMM step */
settings->sigma = (c_float)SIGMA; /* ADMM step */
settings->max_iter = MAX_ITER; /* maximum iterations to
take */
settings->eps_abs = (c_float)EPS_ABS; /* absolute convergence
tolerance */
settings->eps_rel = (c_float)EPS_REL; /* relative convergence
tolerance */
settings->eps_prim_inf = (c_float)EPS_PRIM_INF; /* primal infeasibility
tolerance */
settings->eps_dual_inf = (c_float)EPS_DUAL_INF; /* dual infeasibility
tolerance */
settings->alpha = (c_float)ALPHA; /* relaxation parameter */
settings->linsys_solver = LINSYS_SOLVER; /* relaxation parameter */
#ifndef EMBEDDED
settings->delta = DELTA; /* regularization parameter
for polish */
settings->polish = POLISH; /* ADMM solution polish: 1
*/
settings->polish_refine_iter = POLISH_REFINE_ITER; /* iterative refinement
steps in polish */
settings->verbose = VERBOSE; /* print output */
#endif /* ifndef EMBEDDED */
settings->scaled_termination = SCALED_TERMINATION; /* Evaluate scaled
termination criteria*/
settings->check_termination = CHECK_TERMINATION; /* Interval for evaluating
termination criteria */
settings->warm_start = WARM_START; /* warm starting */
#ifdef PROFILING
settings->time_limit = TIME_LIMIT;
#endif /* ifdef PROFILING */
}
#ifndef EMBEDDED
OSQPWorkspace* osqp_setup(const OSQPData *data, OSQPSettings *settings) {
OSQPWorkspace *work; // Workspace
// Validate data
if (validate_data(data)) {
# ifdef PRINTING
c_eprint("Data validation returned failure");
# endif /* ifdef PRINTING */
return OSQP_NULL;
}
// Validate settings
if (validate_settings(settings)) {
# ifdef PRINTING
c_eprint("Settings validation returned failure");
# endif /* ifdef PRINTING */
return OSQP_NULL;
}
// Allocate empty workspace
work = c_calloc(1, sizeof(OSQPWorkspace));
if (!work) {
# ifdef PRINTING
c_eprint("allocating work failure");
# endif /* ifdef PRINTING */
return OSQP_NULL;
}
// Start and allocate directly timer
# ifdef PROFILING
work->timer = c_malloc(sizeof(OSQPTimer));
tic(work->timer);
# endif /* ifdef PROFILING */
// Copy problem data into workspace
work->data = c_malloc(sizeof(OSQPData));
work->data->n = data->n; // Number of variables
work->data->m = data->m; // Number of linear constraints
work->data->P = csc_to_triu(data->P); // Cost function matrix
work->data->q = vec_copy(data->q, data->n); // Linear part of cost function
work->data->A = copy_csc_mat(data->A); // Linear constraints matrix
work->data->l = vec_copy(data->l, data->m); // Lower bounds on constraints
work->data->u = vec_copy(data->u, data->m); // Upper bounds on constraints
// Vectorized rho parameter
work->rho_vec = c_malloc(work->data->m * sizeof(c_float));
work->rho_inv_vec = c_malloc(work->data->m * sizeof(c_float));
// Type of constraints
work->constr_type = c_calloc(work->data->m, sizeof(c_int));
/*
* Allocate internal solver variables (ADMM steps)
*/
work->x = c_calloc(work->data->n, sizeof(c_float));
work->z = c_calloc(work->data->m, sizeof(c_float));
work->xz_tilde = c_calloc((work->data->n + work->data->m), sizeof(c_float));
work->x_prev = c_calloc(work->data->n, sizeof(c_float));
work->z_prev = c_calloc(work->data->m, sizeof(c_float));
work->y = c_calloc(work->data->m, sizeof(c_float));
// Primal and dual residuals variables
work->Ax = c_calloc(work->data->m, sizeof(c_float));
work->Px = c_calloc(work->data->n, sizeof(c_float));
work->Aty = c_calloc(work->data->n, sizeof(c_float));
// Primal infeasibility variables
work->delta_y = c_calloc(work->data->m, sizeof(c_float));
work->Atdelta_y = c_calloc(work->data->n, sizeof(c_float));
// Dual infeasibility variables
work->delta_x = c_calloc(work->data->n, sizeof(c_float));
work->Pdelta_x = c_calloc(work->data->n, sizeof(c_float));
work->Adelta_x = c_calloc(work->data->m, sizeof(c_float));
// Copy settings
work->settings = copy_settings(settings);
// Perform scaling
if (settings->scaling) {
// Allocate scaling structure
work->scaling = c_malloc(sizeof(OSQPScaling));
work->scaling->D = c_malloc(work->data->n * sizeof(c_float));
work->scaling->Dinv = c_malloc(work->data->n * sizeof(c_float));
work->scaling->E = c_malloc(work->data->m * sizeof(c_float));
work->scaling->Einv = c_malloc(work->data->m * sizeof(c_float));
// Allocate workspace variables used in scaling
work->D_temp = c_malloc(work->data->n * sizeof(c_float));
work->D_temp_A = c_malloc(work->data->n * sizeof(c_float));
work->E_temp = c_malloc(work->data->m * sizeof(c_float));
// Scale data
scale_data(work);
}
else {
work->scaling = OSQP_NULL;
}
// Set type of constraints
set_rho_vec(work);
// Load linear system solver
if (load_linsys_solver(work->settings->linsys_solver)) {
# ifdef PRINTING
c_eprint(
"%s linear system solver not available.\nTried to obtain it from shared library",
LINSYS_SOLVER_NAME[work->settings->linsys_solver]);
# endif /* ifdef PRINTING */
osqp_cleanup(work);
return OSQP_NULL;
}
// Initialize linear system solver structure
work->linsys_solver = init_linsys_solver(work->data->P, work->data->A,
work->settings->sigma, work->rho_vec,
work->settings->linsys_solver, 0);
if (!work->linsys_solver) {
# ifdef PRINTING
c_eprint("Linear systems solver initialization failure");
# endif /* ifdef PRINTING */
osqp_cleanup(work);
return OSQP_NULL;
}
// Initialize active constraints structure
work->pol = c_malloc(sizeof(OSQPPolish));
work->pol->Alow_to_A = c_malloc(work->data->m * sizeof(c_int));
work->pol->Aupp_to_A = c_malloc(work->data->m * sizeof(c_int));
work->pol->A_to_Alow = c_malloc(work->data->m * sizeof(c_int));
work->pol->A_to_Aupp = c_malloc(work->data->m * sizeof(c_int));
work->pol->x = c_malloc(work->data->n * sizeof(c_float));
work->pol->z = c_malloc(work->data->m * sizeof(c_float));
work->pol->y = c_malloc(work->data->m * sizeof(c_float));
// Allocate solution
work->solution = c_calloc(1, sizeof(OSQPSolution));
work->solution->x = c_calloc(1, work->data->n * sizeof(c_float));
work->solution->y = c_calloc(1, work->data->m * sizeof(c_float));
// Allocate and initialize information
work->info = c_calloc(1, sizeof(OSQPInfo));
work->info->status_polish = 0; // Polishing not performed
update_status(work->info, OSQP_UNSOLVED);
# ifdef PROFILING
work->info->solve_time = 0.0; // Solve time to zero
work->info->polish_time = 0.0; // Polish time to zero
work->info->run_time = 0.0; // Total run time to zero
work->info->setup_time = toc(work->timer); // Updater timer information
work->first_run = 1;
# endif /* ifdef PROFILING */
# if EMBEDDED != 1
work->info->rho_updates = 0; // Rho updates set to 0
work->info->rho_estimate = work->settings->rho; // Best rho estimate
# endif /* if EMBEDDED != 1 */
// Print header
# ifdef PRINTING
if (work->settings->verbose) print_setup_header(work);
work->summary_printed = 0; // Initialize last summary to not printed
# endif /* ifdef PRINTING */
# if EMBEDDED != 1
// If adaptive rho and automatic interval, but profiling disabled, we need to
// set the interval to a default value
# ifndef PROFILING
if (work->settings->adaptive_rho && !work->settings->adaptive_rho_interval) {
if (work->settings->check_termination) {
// If check_termination is enabled, we set it to a multiple of the check
// termination interval
work->settings->adaptive_rho_interval = ADAPTIVE_RHO_MULTIPLE_TERMINATION *
work->settings->check_termination;
} else {
// If check_termination is disabled we set it to a predefined fix number
work->settings->adaptive_rho_interval = ADAPTIVE_RHO_FIXED;
}
}
# endif /* ifndef PROFILING */
# endif /* if EMBEDDED != 1 */
// Return workspace structure
return work;
}
#endif // #ifndef EMBEDDED
c_int osqp_solve(OSQPWorkspace *work) {
c_int exitflag;
c_int iter;
c_int compute_cost_function; // Boolean whether to compute the cost function
// in the loop
c_int can_check_termination; // Boolean whether to check termination
#ifdef PROFILING
c_float temp_run_time; // Temporary variable to store current run time
#endif /* ifdef PROFILING */
#ifdef PRINTING
c_int can_print; // Boolean whether you can print
#endif /* ifdef PRINTING */
// Check if workspace has been initialized
if (!work) {
#ifdef PRINTING
c_eprint("Workspace not initialized");
#endif /* ifdef PRINTING */
return -1;
}
// Initialize variables
exitflag = 0;
can_check_termination = 0;
#ifdef PRINTING
can_print = work->settings->verbose;
#endif /* ifdef PRINTING */
#ifdef PRINTING
compute_cost_function = work->settings->verbose; // Compute cost function only
// if verbose is on
#else /* ifdef PRINTING */
compute_cost_function = 0; // Never compute cost
// function during the
// iterations if no printing
// enabled
#endif /* ifdef PRINTING */
#ifdef PROFILING
tic(work->timer); // Start timer
#endif /* ifdef PROFILING */
#ifdef PRINTING
if (work->settings->verbose) {
// Print Header for every column
print_header();
}
#endif /* ifdef PRINTING */
#ifdef CTRLC
// initialize Ctrl-C support
startInterruptListener();
#endif /* ifdef CTRLC */
// Initialize variables (cold start or warm start depending on settings)
if (!work->settings->warm_start) cold_start(work); // If not warm start ->
// set x, z, y to zero
// Main ADMM algorithm
for (iter = 1; iter <= work->settings->max_iter; iter++) {
// Update x_prev, z_prev (preallocated, no malloc)
swap_vectors(&(work->x), &(work->x_prev));
swap_vectors(&(work->z), &(work->z_prev));
/* ADMM STEPS */
/* Compute \tilde{x}^{k+1}, \tilde{z}^{k+1} */
update_xz_tilde(work);
/* Compute x^{k+1} */
update_x(work);
/* Compute z^{k+1} */
update_z(work);
/* Compute y^{k+1} */
update_y(work);
/* End of ADMM Steps */
#ifdef CTRLC
// Check the interrupt signal
if (isInterrupted()) {
update_status(work->info, OSQP_SIGINT);
# ifdef PRINTING
c_print("Solver interrupted\n");
# endif /* ifdef PRINTING */
exitflag = 1;
goto exit;
}
#endif /* ifdef CTRLC */
#ifdef PROFILING
// Check if solver time_limit is enabled. In case, check if the current run
// time
// is more than the time_limit option.
if (work->first_run) {
temp_run_time = work->info->setup_time + toc(work->timer);
}
else {
temp_run_time = toc(work->timer);
}
if (work->settings->time_limit &&
(temp_run_time >= work->settings->time_limit)) {
update_status(work->info, OSQP_TIME_LIMIT_REACHED);
# ifdef PRINTING
if (work->settings->verbose) c_print("Run time limit reached\n");
# endif /* ifdef PRINTING */
exitflag = 1;
goto exit;
}
#endif /* ifdef PROFILING */
// Can we check for termination ?
can_check_termination = work->settings->check_termination &&
(iter % work->settings->check_termination == 0);
#ifdef PRINTING
// Can we print ?
can_print = work->settings->verbose &&
((iter % PRINT_INTERVAL == 0) || (iter == 1));
if (can_check_termination || can_print) { // Update status in either of
// these cases
// Update information
update_info(work, iter, compute_cost_function, 0);
if (can_print) {
// Print summary
print_summary(work);
}
if (can_check_termination) {
// Check algorithm termination
if (check_termination(work, 0)) {
// Terminate algorithm
break;
}
}
}
#else /* ifdef PRINTING */
if (can_check_termination) {
// Update information and compute also objective value
update_info(work, iter, compute_cost_function, 0);
// Check algorithm termination
if (check_termination(work, 0)) {
// Terminate algorithm
break;
}
}
#endif /* ifdef PRINTING */
#if EMBEDDED != 1
# ifdef PROFILING
// If adaptive rho with automatic interval, check if the solve time is a
// certain fraction
// of the setup time.
if (work->settings->adaptive_rho && !work->settings->adaptive_rho_interval) {
// Check time
if (toc(work->timer) >
work->settings->adaptive_rho_fraction * work->info->setup_time) {
// Enough time has passed. We now get the number of iterations between
// the updates.
if (work->settings->check_termination) {
// If check_termination is enabled, we round the number of iterations
// between
// rho updates to the closest multiple of check_termination
work->settings->adaptive_rho_interval = (c_int)c_roundmultiple(iter,
work->settings->check_termination);
} else {
// If check_termintion is disabled, we round the number of iterations
// between
// updates to the closest multiple of the default check_termination
// interval.
work->settings->adaptive_rho_interval = (c_int)c_roundmultiple(iter,
CHECK_TERMINATION);
}
// Make sure the interval is not 0 and at least check_termination times
work->settings->adaptive_rho_interval = c_max(
work->settings->adaptive_rho_interval,
work->settings->check_termination);
} // If time condition is met
} // If adaptive rho enabled and interval set to auto
# endif // PROFILING
// Adapt rho
if (work->settings->adaptive_rho &&
work->settings->adaptive_rho_interval &&
(iter % work->settings->adaptive_rho_interval == 0)) {
// Update info with the residuals if it hasn't been done before
# ifdef PRINTING
if (!can_check_termination && !can_print) {
// Information has not been computed neither for termination or printing
// reasons
update_info(work, iter, compute_cost_function, 0);
}
# else /* ifdef PRINTING */
if (!can_check_termination) {
// Information has not been computed before for termination check
update_info(work, iter, compute_cost_function, 0);
}
# endif /* ifdef PRINTING */
// Actually update rho
if (adapt_rho(work)) {
# ifdef PRINTING
c_eprint("Failed rho update");
# endif // PRINTING
exitflag = 1;
goto exit;
}
}
#endif // EMBEDDED != 1
} // End of ADMM for loop
// Update information and check termination condition if it hasn't been done
// during last iteration (max_iter reached or check_termination disabled)
if (!can_check_termination) {
/* Update information */
#ifdef PRINTING
if (!can_print) {
// Update info only if it hasn't been updated before for printing
// reasons
update_info(work, iter - 1, compute_cost_function, 0);
}
#else /* ifdef PRINTING */
// If no printing is enabled, update info directly
update_info(work, iter - 1, compute_cost_function, 0);
#endif /* ifdef PRINTING */
#ifdef PRINTING
/* Print summary */
if (work->settings->verbose && !work->summary_printed) print_summary(work);
#endif /* ifdef PRINTING */
/* Check whether a termination criterion is triggered */
check_termination(work, 0);
}
// Compute objective value in case it was not
// computed during the iterations
if (!compute_cost_function) {
work->info->obj_val = compute_obj_val(work, work->x);
}
/* Print summary for last iteration */
#ifdef PRINTING
if (work->settings->verbose && !work->summary_printed) {
print_summary(work);
}
#endif /* ifdef PRINTING */
/* if max iterations reached, change status accordingly */
if (work->info->status_val == OSQP_UNSOLVED) {
if (!check_termination(work, 1)) { // Try to check for approximate
// termination
update_status(work->info, OSQP_MAX_ITER_REACHED);
}
}
#if EMBEDDED != 1
/* Update rho estimate */
work->info->rho_estimate = compute_rho_estimate(work);
#endif /* if EMBEDDED != 1 */
/* Update solve time */
#ifdef PROFILING
work->info->solve_time = toc(work->timer);
#endif /* ifdef PROFILING */
// Polish the obtained solution
#ifndef EMBEDDED
if (work->settings->polish && (work->info->status_val == OSQP_SOLVED)) polish(
work);
#endif /* ifndef EMBEDDED */
/* Update total time */
#ifdef PROFILING
if (work->first_run) {
// total time: setup + solve + polish
work->info->run_time = work->info->setup_time +
work->info->solve_time +
work->info->polish_time;
} else {
// total time: solve + polish
work->info->run_time = work->info->solve_time +
work->info->polish_time;
}
// Indicate that the solve function has already been executed
if (work->first_run) work->first_run = 0;
#endif /* ifdef PROFILING */
/* Print final footer */
#ifdef PRINTING
if (work->settings->verbose) print_footer(work->info, work->settings->polish);
#endif /* ifdef PRINTING */
// Store solution
store_solution(work);
// Define exit flag for quitting function
#if defined(PROFILING) || defined(CTRLC) || EMBEDDED != 1
exit:
#endif /* if defined(PROFILING) || defined(CTRLC) || EMBEDDED != 1 */
#ifdef CTRLC
// Restore previous signal handler
endInterruptListener();
#endif /* ifdef CTRLC */
return exitflag;
}
#ifndef EMBEDDED
c_int osqp_cleanup(OSQPWorkspace *work) {
c_int exitflag = 0;
if (work) { // If workspace has been allocated
// Free Data
if (work->data) {
if (work->data->P) csc_spfree(work->data->P);
if (work->data->A) csc_spfree(work->data->A);
if (work->data->q) c_free(work->data->q);
if (work->data->l) c_free(work->data->l);
if (work->data->u) c_free(work->data->u);
c_free(work->data);
}
// Free scaling
if (work->settings->scaling) {
if (work->scaling->D) c_free(work->scaling->D);
if (work->scaling->Dinv) c_free(work->scaling->Dinv);
if (work->scaling->E) c_free(work->scaling->E);
if (work->scaling->Einv) c_free(work->scaling->Einv);
c_free(work->scaling);
// Free workspace variables
if (work->D_temp) c_free(work->D_temp);
if (work->D_temp_A) c_free(work->D_temp_A);
if (work->E_temp) c_free(work->E_temp);
}
// Free linear system solver structure
if (work->linsys_solver) {
if (work->linsys_solver->free) {
work->linsys_solver->free(work->linsys_solver);
}
}
// Unload linear system solver
exitflag = unload_linsys_solver(work->settings->linsys_solver);
// Free active constraints structure
if (work->pol) {
if (work->pol->Alow_to_A) c_free(work->pol->Alow_to_A);
if (work->pol->Aupp_to_A) c_free(work->pol->Aupp_to_A);
if (work->pol->A_to_Alow) c_free(work->pol->A_to_Alow);
if (work->pol->A_to_Aupp) c_free(work->pol->A_to_Aupp);
if (work->pol->x) c_free(work->pol->x);
if (work->pol->z) c_free(work->pol->z);
if (work->pol->y) c_free(work->pol->y);
c_free(work->pol);
}
// Free other Variables
if (work->constr_type) c_free(work->constr_type);
if (work->rho_vec) c_free(work->rho_vec);
if (work->rho_inv_vec) c_free(work->rho_inv_vec);
if (work->x) c_free(work->x);
if (work->z) c_free(work->z);
if (work->xz_tilde) c_free(work->xz_tilde);
if (work->x_prev) c_free(work->x_prev);
if (work->z_prev) c_free(work->z_prev);
if (work->y) c_free(work->y);
if (work->Ax) c_free(work->Ax);
if (work->Px) c_free(work->Px);
if (work->Aty) c_free(work->Aty);
if (work->delta_y) c_free(work->delta_y);
if (work->Atdelta_y) c_free(work->Atdelta_y);
if (work->delta_x) c_free(work->delta_x);
if (work->Pdelta_x) c_free(work->Pdelta_x);
if (work->Adelta_x) c_free(work->Adelta_x);
// Free Settings
if (work->settings) c_free(work->settings);
// Free solution
if (work->solution) {
if (work->solution->x) c_free(work->solution->x);
if (work->solution->y) c_free(work->solution->y);
c_free(work->solution);
}
// Free information
if (work->info) c_free(work->info);
// Free timer
# ifdef PROFILING
if (work->timer) c_free(work->timer);
# endif /* ifdef PROFILING */
// Free work
c_free(work);
}
return exitflag;
}
#endif // #ifndef EMBEDDED
/************************
* Update problem data *
************************/
c_int osqp_update_lin_cost(OSQPWorkspace *work, c_float *q_new) {
// Replace q by the new vector
prea_vec_copy(q_new, work->data->q, work->data->n);
// Scaling
if (work->settings->scaling) {
vec_ew_prod(work->scaling->D, work->data->q, work->data->q, work->data->n);
vec_mult_scalar(work->data->q, work->scaling->c, work->data->n);
}
// Reset solver information
reset_info(work->info);
return 0;
}
c_int osqp_update_bounds(OSQPWorkspace *work, c_float *l_new, c_float *u_new) {
c_int i, exitflag = 0;
// Check if lower bound is smaller than upper bound
for (i = 0; i < work->data->m; i++) {
if (l_new[i] > u_new[i]) {
#ifdef PRINTING
c_eprint("lower bound must be lower than or equal to upper bound");
#endif /* ifdef PRINTING */
return 1;
}
}
// Replace l and u by the new vectors
prea_vec_copy(l_new, work->data->l, work->data->m);
prea_vec_copy(u_new, work->data->u, work->data->m);
// Scaling
if (work->settings->scaling) {
vec_ew_prod(work->scaling->E, work->data->l, work->data->l, work->data->m);
vec_ew_prod(work->scaling->E, work->data->u, work->data->u, work->data->m);
}
// Reset solver information
reset_info(work->info);
#if EMBEDDED != 1
// Update rho_vec and refactor if constraints type changes
exitflag = update_rho_vec(work);
#endif // EMBEDDED != 1
return exitflag;
}
c_int osqp_update_lower_bound(OSQPWorkspace *work, c_float *l_new) {
c_int i, exitflag = 0;
// Replace l by the new vector
prea_vec_copy(l_new, work->data->l, work->data->m);
// Scaling
if (work->settings->scaling) {
vec_ew_prod(work->scaling->E, work->data->l, work->data->l, work->data->m);
}
// Check if lower bound is smaller than upper bound
for (i = 0; i < work->data->m; i++) {
if (work->data->l[i] > work->data->u[i]) {
#ifdef PRINTING
c_eprint("upper bound must be greater than or equal to lower bound");
#endif /* ifdef PRINTING */
return 1;
}
}
// Reset solver information
reset_info(work->info);
#if EMBEDDED != 1
// Update rho_vec and refactor if constraints type changes
exitflag = update_rho_vec(work);
#endif // EMBEDDED ! =1
return exitflag;
}
c_int osqp_update_upper_bound(OSQPWorkspace *work, c_float *u_new) {
c_int i, exitflag = 0;
// Replace u by the new vector
prea_vec_copy(u_new, work->data->u, work->data->m);
// Scaling
if (work->settings->scaling) {
vec_ew_prod(work->scaling->E, work->data->u, work->data->u, work->data->m);
}
// Check if upper bound is greater than lower bound
for (i = 0; i < work->data->m; i++) {
if (work->data->u[i] < work->data->l[i]) {
#ifdef PRINTING
c_eprint("lower bound must be lower than or equal to upper bound");
#endif /* ifdef PRINTING */
return 1;
}
}
// Reset solver information
reset_info(work->info);
#if EMBEDDED != 1
// Update rho_vec and refactor if constraints type changes
exitflag = update_rho_vec(work);
#endif // EMBEDDED != 1
return exitflag;
}
c_int osqp_warm_start(OSQPWorkspace *work, c_float *x, c_float *y) {
// Update warm_start setting to true
if (!work->settings->warm_start) work->settings->warm_start = 1;
// Copy primal and dual variables into the iterates
prea_vec_copy(x, work->x, work->data->n);
prea_vec_copy(y, work->y, work->data->m);
// Scale iterates
if (work->settings->scaling) {
vec_ew_prod(work->scaling->Dinv, work->x, work->x, work->data->n);
vec_ew_prod(work->scaling->Einv, work->y, work->y, work->data->m);
vec_mult_scalar(work->y, work->scaling->c, work->data->m);
}
// Compute Ax = z and store it in z
mat_vec(work->data->A, work->x, work->z, 0);
return 0;
}
c_int osqp_warm_start_x(OSQPWorkspace *work, c_float *x) {
// Update warm_start setting to true
if (!work->settings->warm_start) work->settings->warm_start = 1;
// Copy primal variable into the iterate x
prea_vec_copy(x, work->x, work->data->n);
// Scale iterate
if (work->settings->scaling) {
vec_ew_prod(work->scaling->Dinv, work->x, work->x, work->data->n);
}
// Compute Ax = z and store it in z
mat_vec(work->data->A, work->x, work->z, 0);
// Cold start y
vec_set_scalar(work->y, 0., work->data->m);
return 0;
}
c_int osqp_warm_start_y(OSQPWorkspace *work, c_float *y) {
// Update warm_start setting to true
if (!work->settings->warm_start) work->settings->warm_start = 1;
// Copy primal variable into the iterate y
prea_vec_copy(y, work->y, work->data->m);
// Scale iterate
if (work->settings->scaling) {
vec_ew_prod(work->scaling->Einv, work->y, work->y, work->data->m);
vec_mult_scalar(work->y, work->scaling->c, work->data->m);
}
// Cold start x and z
vec_set_scalar(work->x, 0., work->data->n);
vec_set_scalar(work->z, 0., work->data->m);
return 0;
}
#if EMBEDDED != 1
/**
* Update elements of matrix P (upper-diagonal)
* without changing sparsity structure.
*
*
* If Px_new_idx is OSQP_NULL, Px_new is assumed to be as long as P->x
* and the whole P->x is replaced.
*
* @param work Workspace structure
* @param Px_new Vector of new elements in P->x (upper triangular)
* @param Px_new_idx Index mapping new elements to positions in P->x
* @param P_new_n Number of new elements to be changed
* @return output flag: 0: OK
* 1: P_new_n > nnzP
* <0: error in update_matrices()
*/
c_int osqp_update_P(OSQPWorkspace *work,
c_float *Px_new,
c_int *Px_new_idx,
c_int P_new_n) {
c_int i; // For indexing
c_int exitflag; // Exit flag
c_int nnzP; // Number of nonzeros in P
nnzP = work->data->P->p[work->data->P->n];
if (Px_new_idx) { // Passing the index of elements changed
// Check if number of elements is less or equal than the total number of
// nonzeros in P
if (P_new_n > nnzP) {
# ifdef PRINTING
c_eprint("new number of elements (%i) greater than elements in P (%i)",
(int)P_new_n,
(int)nnzP);
# endif /* ifdef PRINTING */
return 1;
}
}
if (work->settings->scaling) {
// Unscale data
unscale_data(work);
}
// Update P elements
if (Px_new_idx) { // Change only Px_new_idx
for (i = 0; i < P_new_n; i++) {
work->data->P->x[Px_new_idx[i]] = Px_new[i];
}
}
else // Change whole P
{
for (i = 0; i < nnzP; i++) {
work->data->P->x[i] = Px_new[i];
}
}
if (work->settings->scaling) {
// Scale data
scale_data(work);
}
// Update linear system structure with new data
exitflag = work->linsys_solver->update_matrices(work->linsys_solver,
work->data->P,
work->data->A,
work->settings);
// Reset solver information
reset_info(work->info);
# ifdef PRINTING
if (exitflag < 0) {