-
Notifications
You must be signed in to change notification settings - Fork 14
/
tolmin.c
2168 lines (2000 loc) · 56.1 KB
/
tolmin.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
/*
* tolmin.c --
*
* Implementation of Mike Powell's TOLMIN algorithm for minimizing a
* differentiable function of many variables subject to linear and bound
* constraints. The algorithm is described in:
*
* M.J.D. Powell, "A tolerant algorithm for linearly constrained
* optimization calculations", Math. Programming B, Vol. 45, pp. 547-566
* (1989).
*
* The present code is based on the original FORTRAN version written by Mike
* Powell who released his code under the GNU Lesser General Public License.
* His original code is available at CCPForge
* <https://ccpforge.cse.rl.ac.uk/gf/project/powell/>
*
* ----------------------------------------------------------------------------
*
* Copyright (c) 1989, Mike J. D. Powell (FORTRAN version released under the
* GNU Lesser General Public License).
*
* Copyright (c) 2017, Éric Thiébaut (C version).
*
* ----------------------------------------------------------------------------
*
* History of changes:
* - Convert original FORTRAN code to C.
* - Make all possible arguments passed by address `const`.
* - Constant scalar arguments passed by address now passed by value.
* - Remove all array adjustments and use macros to mimic FORTRAN indexing.
* - Replace FORTRAN intrinsics by fast macros.
* - Replace all FORTRAN i/o by calls to the standard C library (the outputs
* with IPRINT=-1 are identical between the original FORTRAN code and the C
* version for the provided example).
* - Pass objective function as argument.
* - Rename GETMIN as TOLMIN.
* - Write FORTRAN wrapper with the same syntax as the original code.
*
* Things to do:
* - Define status constants and related messages.
* - Cleanup goto statements.
*/
#include <math.h>
#include <stdio.h>
#include "tolmin.h"
#define OUTPUT stdout
/* Fast macros to emulate FORTRAN intrinsics. */
#define sign(x,y) _sign(typeof(x), x, y)
#define abs(x) _abs(typeof(x), x)
#define pow2(x) _pow2(typeof(x), x)
#define min(a,b) _min(typeof((a) + (b)), a, b)
#define max(a,b) _max(typeof((a) + (b)), a, b)
#define clamp(x,lo,hi) _clamp(typeof((x) + (lo) + (hi)), x, lo, hi)
#define _abs(T,x) ({ T _x = (x); _x >= (T)0 ? _x : -_x; })
#define _pow2(T,x) ({ T _x = (x); _x*_x; })
#define _min(T,a,b) ({ T _a = (a), _b = (b); _a < _b ? _a : _b; })
#define _max(T,a,b) ({ T _a = (a), _b = (b); _a > _b ? _a : _b; })
#define _clamp(T,x,lo,hi) _min(T, _max(T, x, lo), hi)
#define _sign(T,x,y) ({ T _x = (x); (_x < 0) != ((y) < 0) ? -_x : _x; })
/*
* The following macros are defined to mimic FORTRAN indexing. All recent
* compilers will notice the constant offsets and make the necessary
* adjustments. So the readability is not to the detriment of efficiency.
*/
#define A(i1,i2) a[(i2)*ia + (i1) - (ia+1)]
#define AM(i1) am[(i1) - 1]
#define B(i1) b[(i1) - 1]
#define BRES(i1) bres[(i1) - 1]
#define CGRAD(i1) cgrad[(i1) - 1]
#define D(i1) d[(i1) - 1]
#define G(i1) g[(i1) - 1]
#define GM(i1) gm[(i1) - 1]
#define GMNEW(i1) gmnew[(i1) - 1]
#define GOPT(i1) gopt[(i1) - 1]
#define GS(i1) gs[(i1) - 1]
#define IACT(i1) iact[(i1) - 1]
#define PAR(i1) par[(i1) - 1]
#define PARNEW(i1) parnew[(i1) - 1]
#define PARW(i1) parw[(i1) - 1]
#define RESKT(i1) reskt[(i1) - 1]
#define RESKTW(i1) resktw[(i1) - 1]
#define U(i1) u[(i1) - 1]
#define W(i1) w[(i1) - 1]
#define X(i1) x[(i1) - 1]
#define XBIG(i1) xbig[(i1) - 1]
#define XL(i1) xl[(i1) - 1]
#define XS(i1) xs[(i1) - 1]
#define XU(i1) xu[(i1) - 1]
#define Z(i1) z[(i1) - 1]
#define ZTC(i1) ztc[(i1) - 1]
#define ZTG(i1) ztg[(i1) - 1]
#define ZZDIAG(i1) zzdiag[(i1) - 1]
static void
prtrvec(FILE* out, const char* prefix, const INTEGER ncols,
const char* format, const INTEGER n, const REAL x[])
{
INTEGER i;
if (prefix != NULL) {
fputs(prefix, out);
}
for (i = 0; i < n; ++i) {
if (i > 0 && i%ncols == 0) {
fputs("\n", out);
if (prefix != NULL) {
fputs(prefix, out);
}
}
fprintf(out, format, (double)x[i]);
}
fputs("\n", out);
}
static void
prtivec(FILE* out, const char* prefix, const INTEGER ncols,
const char* format, const INTEGER n, const INTEGER x[])
{
INTEGER i;
if (prefix != NULL) {
fputs(prefix, out);
}
for (i = 0; i < n; ++i) {
if (i > 0 && i%ncols == 0) {
fputs("\n", out);
if (prefix != NULL) {
fputs(prefix, out);
}
}
fprintf(out, format, (long)x[i]);
}
fputs("\n", out);
}
static void
addcon(const INTEGER n, const INTEGER m, const REAL a[], const INTEGER ia,
INTEGER iact[], INTEGER* nact, REAL z[], REAL u[], const REAL relacc,
const INTEGER indxbd, REAL ztc[], REAL cgrad[])
{
INTEGER i, j, jp, np, iz, icon, inewbd, ipiv, iznbd;
REAL sum, sumabs, temp, tempa, tempb, wcos, wsin, wpiv;
iznbd = 0; /* FIXME: Fix uninitialized variables. */
np = *nact + 1;
icon = IACT(indxbd);
IACT(indxbd) = IACT(np);
IACT(np) = icon;
if (icon > m) {
/* Form ZTC when the new constraint is a bound. */
inewbd = icon - m;
if (inewbd <= n) {
temp = -1.0;
} else {
inewbd -= n;
temp = 1.0;
}
iznbd = inewbd*n - n;
for (j = 1; j <= n; ++j) {
ZTC(j) = temp*Z(iznbd+j);
}
} else {
/* Else form ZTC for an ordinary constraint. */
for (i = 1; i <= n; ++i) {
CGRAD(i) = A(i,icon);
}
for (j = 1; j <= n; ++j) {
ZTC(j) = 0.0;
iz = j;
for (i = 1; i <= n; ++i) {
ZTC(j) += Z(iz)*CGRAD(i);
iz += n;
}
}
}
/* Find any Givens rotations to apply to the last columns of Z. */
j = n;
L40:
jp = j;
--j;
if (j > *nact) {
if (ZTC(jp) == 0.0) {
goto L40;
}
if (abs(ZTC(jp)) <= relacc*abs(ZTC(j))) {
temp = abs(ZTC(j));
} else if (abs(ZTC(j)) <= relacc*abs(ZTC(jp))) {
temp = abs(ZTC(jp));
} else {
temp = abs(ZTC(jp))*sqrt(pow2(ZTC(j)/ZTC(jp)) + 1.0);
}
wcos = ZTC(j)/temp;
wsin = ZTC(jp)/temp;
ZTC(j) = temp;
iz = j;
if (icon > m) {
/* Apply the rotation when the new constraint is a bound. */
for (i = 1; i <= n; ++i) {
temp = wcos*Z(iz+1) - wsin*Z(iz);
Z(iz) = wcos*Z(iz) + wsin*Z(iz+1);
Z(iz+1) = temp;
iz += n;
}
Z(iznbd+jp) = 0.0;
} else {
/* Else apply the rotation for an ordinary constraint. */
wpiv = 0.0;
ipiv = 0;
for (i = 1; i <= n; ++i) {
tempa = wcos*Z(iz+1);
tempb = wsin*Z(iz);
temp = abs(CGRAD(i))*(abs(tempa) + abs(tempb));
if (temp > wpiv) {
wpiv = temp;
ipiv = i;
}
Z(iz) = wcos*Z(iz) + wsin*Z(iz+1);
Z(iz+1) = tempa - tempb;
iz += n;
}
/* Ensure orthogonality of Z(.,JP) to CGRAD. */
sum = 0.0;
iz = jp;
for (i = 1; i <= n; ++i) {
sum += Z(iz)*CGRAD(i);
iz += n;
}
if (sum != 0.0) {
iz = ipiv*n - n + jp;
Z(iz) -= sum/CGRAD(ipiv);
}
}
goto L40;
}
/* Test for linear independence in the proposed new active set. */
if (ZTC(np) == 0.0) {
goto L90;
}
if (icon <= m) {
sum = 0.0;
sumabs = 0.0;
iz = np;
for (i = 1; i <= n; ++i) {
temp = Z(iz)*CGRAD(i);
sum += temp;
sumabs += abs(temp);
iz += n;
}
if (abs(sum) <= relacc*sumabs) {
goto L90;
}
}
/* Set the new diagonal element of U and return. */
U(np) = 1.0/ZTC(np);
*nact = np;
L90:
return;
}
static void
newcon(const INTEGER n, const INTEGER m, const REAL a[], const INTEGER ia,
INTEGER iact[], INTEGER* nact, REAL z[], REAL u[], REAL d[],
const REAL relacc, const INTEGER mdeg, REAL zzdiag[], REAL gmnew[],
REAL cgrad[])
{
INTEGER i, j, k, jm, np, iz, jmv, iadd, khigh;
REAL cviol, cvmax, savabs, sumabs, savsum, sum, temp, sumd;
/* FIXME: Fix uninitialized variables. */
iadd = 0;
savsum = 0.0;
savabs = 0.0;
/* Initialization. */
np = *nact + 1;
khigh = mdeg;
iz = 0;
for (i = 1; i <= n; ++i) {
ZZDIAG(i) = 0.0;
for (j = np; j <= n; ++j) {
ZZDIAG(i) += pow2(Z(iz+j));
}
iz += n;
}
/* Calculate the scalar products of D with its constraints. */
L30:
cvmax = 0.0;
for (k = np; k <= khigh; ++k) {
j = IACT(k);
if (j <= m) {
sum = 0.0;
sumabs = 0.0;
sumd = 0.0;
for (i = 1; i <= n; ++i) {
temp = D(i)*A(i,j);
sum += temp;
sumabs += abs(temp);
sumd += ZZDIAG(i)*pow2(A(i,j));
}
} else {
jm = j - m;
if (jm <= n) {
sum = -D(jm);
} else {
jm -= n;
sum = D(jm);
}
sumabs = abs(sum);
sumd = ZZDIAG(jm);
}
/* Pick out the most violated constraint, or return if the
* violation is negligible. */
if (sum > relacc*sumabs) {
cviol = sum*sum/sumd;
if (cviol > cvmax) {
cvmax = cviol;
iadd = k;
savsum = sum;
savabs = sumabs;
}
}
}
if (cvmax <= 0.0) {
goto L140;
}
if (*nact == 0) {
goto L120;
}
/* Set GMNEW to the gradient of the most violated constraint. */
j = IACT(iadd);
if (j <= m) {
jmv = 0;
for (i = 1; i <= n; ++i) {
GMNEW(i) = A(i,j);
}
} else {
jmv = j - m;
for (i = 1; i <= n; ++i) {
GMNEW(i) = 0.0;
}
if (jmv <= n) {
GMNEW(jmv) = -1.0;
} else {
jmv -= n;
GMNEW(jmv) = 1.0;
}
}
/* Modify GMNEW for the next active constraint. */
k = *nact;
L80:
temp = 0.0;
iz = k;
for (i = 1; i <= n; ++i) {
temp += Z(iz)*GMNEW(i);
iz += n;
}
temp *= U(k);
j = IACT(k);
if (j <= m) {
for (i = 1; i <= n; ++i) {
GMNEW(i) -= temp*A(i,j);
}
} else {
jm = j - m;
if (jm <= n) {
GMNEW(jm) += temp;
} else {
GMNEW(jm-n) -= temp;
}
}
/* Revise the values of SAVSUM and SAVABS. */
sum = 0.0;
sumabs = 0.0;
for (i = 1; i <= n; ++i) {
temp = D(i)*GMNEW(i);
sum += temp;
sumabs += abs(temp);
}
savsum = min(savsum, sum);
savabs = max(savabs, sumabs);
--k;
if (k >= 1) {
goto L80;
}
/* Add the new constraint to the active set if the constraint
* violation is still significant. */
if (jmv > 0) {
D(jmv) = 0.0;
}
if (savsum <= relacc*savabs) {
goto L130;
}
L120:
k = *nact;
addcon(n, m, a, ia, iact, nact, z, u, relacc, iadd, gmnew, cgrad);
if (*nact > k) {
goto L140;
}
/* Seek another constraint violation. */
iadd = np;
L130:
if (np < khigh) {
k = IACT(khigh);
IACT(khigh) = IACT(iadd);
IACT(iadd) = k;
--khigh;
goto L30;
}
L140:
return;
}
static void
delcon(const INTEGER n, const INTEGER m, const REAL a[], const INTEGER ia,
INTEGER iact[], INTEGER* nact, REAL z[], REAL u[], const REAL relacc,
const INTEGER idrop)
{
INTEGER i, j, jp, nm, iz, ibd, icon, izbd, ipiv, isave;
REAL rjjp, temp, tempb, ujp, sum, wcos, wsin, wpiv, denom, tempa;
/* FIXME: Fix uninitialized variables. */
ipiv = 0;
izbd = 0;
nm = *nact - 1;
if (idrop == *nact) {
goto L60;
}
isave = IACT(idrop);
/* Cycle through the constraint exchanges that are needed. */
for (j = idrop; j <= nm; ++j) {
jp = j + 1;
icon = IACT(jp);
IACT(j) = icon;
/* Calculate the (J,JP) element of R. */
if (icon <= m) {
rjjp = 0.0;
iz = j;
for (i = 1; i <= n; ++i) {
rjjp += Z(iz)*A(i,icon);
iz += n;
}
} else {
ibd = icon - m;
if (ibd <= n) {
izbd = ibd*n - n;
rjjp = -Z(izbd+j);
} else {
ibd -= n;
izbd = ibd*n - n;
rjjp = Z(izbd+j);
}
}
/* Calculate the parameters of the next rotation. */
ujp = U(jp);
temp = rjjp*ujp;
denom = abs(temp);
if (denom*relacc < 1.0) {
denom = sqrt(denom*denom + 1.0);
}
wcos = temp/denom;
wsin = 1.0/denom;
/* Rotate Z when a bound constraint is promoted. */
iz = j;
if (icon > m) {
for (i = 1; i <= n; ++i) {
temp = wcos*Z(iz+1) - wsin*Z(iz);
Z(iz) = wcos*Z(iz) + wsin*Z(iz+1);
Z(iz+1) = temp;
iz += n;
}
Z(izbd+jp) = 0.0;
/* Rotate Z when an ordinary constraint is promoted. */
} else {
wpiv = 0.0;
for (i = 1; i <= n; ++i) {
tempa = wcos*Z(iz+1);
tempb = wsin*Z(iz);
temp = abs(A(i,icon))*(abs(tempa) + abs(tempb));
if (temp > wpiv) {
wpiv = temp;
ipiv = i;
}
Z(iz) = wcos*Z(iz) + wsin*Z(iz+1);
Z(iz+1) = tempa - tempb;
iz += n;
}
/* Ensure orthogonality to promoted constraint. */
sum = 0.0;
iz = jp;
for (i = 1; i <= n; ++i) {
sum += Z(iz)*A(i,icon);
iz += n;
}
if (sum != 0.0) {
iz = ipiv*n - n + jp;
Z(iz) -= sum/A(ipiv,icon);
}
}
/* Set the new diagonal elements of U. */
U(jp) = -denom*U(j);
U(j) = ujp/denom;
}
/* Return. */
IACT(*nact) = isave;
L60:
*nact = nm;
return;
}
static void
sdirn(const INTEGER n, const INTEGER nact, const REAL z[], REAL d[],
REAL ztg[], const REAL gm[], const REAL relacc, REAL* ddotgm)
{
INTEGER i, j, np, iz;
REAL sum, temp, sumabs;
*ddotgm = 0.0;
if (nact >= n) {
goto L60;
}
/* Premultiply GM by the transpose of Z. */
np = nact + 1;
for (j = np; j <= n; ++j) {
sum = 0.0;
sumabs = 0.0;
iz = j;
for (i = 1; i <= n; ++i) {
temp = Z(iz)*GM(i);
sum += temp;
sumabs += abs(temp);
iz += n;
}
if (abs(sum) <= relacc*sumabs) {
sum = 0.0;
}
ZTG(j) = sum;
}
/* Form D by premultiplying ZTG by -Z. */
iz = 0;
for (i = 1; i <= n; ++i) {
sum = 0.0;
sumabs = 0.0;
for (j = np; j <= n; ++j) {
temp = Z(iz+j)*ZTG(j);
sum -= temp;
sumabs += abs(temp);
}
if (abs(sum) <= relacc*sumabs) {
sum = 0.0;
}
D(i) = sum;
iz += n;
}
/* Test that the search direction is downhill. */
sumabs = 0.0;
for (i = 1; i <= n; ++i) {
temp = D(i)*GM(i);
*ddotgm += temp;
sumabs += abs(temp);
}
if (*ddotgm + relacc*sumabs >= 0.0) {
*ddotgm = 0.0;
}
L60:
return;
}
static void
sdegen(const INTEGER n, const INTEGER m, const REAL a[], const INTEGER ia,
INTEGER iact[], INTEGER* nact, REAL par[], REAL z[], REAL u[],
REAL d[], REAL ztg[], REAL gm[], const REAL relacc, REAL* ddotgm,
const INTEGER meql, const INTEGER mdeg, REAL gmnew[], REAL parnew[],
REAL cgrad[])
{
INTEGER i, j, k, jm, mp, np, ku, iz, idrop, itest, n1;
REAL dtest, sum, temp, theta, ratio;
/* FIXME: Fix uninitialized variables. */
idrop = 0;
itest = 0;
mp = meql + 1;
dtest = 0.0;
/* Calculate the search direction and branch if it is not downhill. */
L10:
sdirn(n, *nact, z, d, ztg, gm, relacc, ddotgm);
if (*ddotgm == 0.0) {
goto L120;
}
/* Branch if there is no need to consider any degenerate constraints.
* The test gives termination if two consecutive additions to the
* active set fail to increase the predicted new value of F. */
if (*nact == mdeg) {
goto L120;
}
np = *nact + 1;
sum = 0.0;
for (j = np; j <= n; ++j) {
sum += pow2(ZTG(j));
}
if (dtest > 0.0 && sum >= dtest) {
if (itest == 1) {
goto L120;
}
itest = 1;
} else {
dtest = sum;
itest = 0;
}
/* Add a constraint to the active set if there are any significant
* violations of degenerate constraints. */
k = *nact;
newcon(n, m, a, ia, iact, nact, z, u, d, relacc, mdeg, gmnew, parnew, cgrad);
if (*nact == k) {
goto L120;
}
PAR(*nact) = 0.0;
/* Calculate the new reduced gradient and Lagrange parameters. */
L30:
for (i = 1; i <= n; ++i) {
GMNEW(i) = GM(i);
}
k = *nact;
L50:
temp = 0.0;
iz = k;
for (i = 1; i <= n; ++i) {
temp += Z(iz)*GMNEW(i);
iz += n;
}
temp *= U(k);
PARNEW(k) = PAR(k) + temp;
if (k == *nact) {
PARNEW(k) = min(PARNEW(k), 0.0);
}
j = IACT(k);
if (j <= m) {
for (i = 1; i <= n; ++i) {
GMNEW(i) -= temp*A(i,j);
}
} else {
jm = j - m;
if (jm <= n) {
GMNEW(jm) += temp;
} else {
GMNEW(jm-n) -= temp;
}
}
--k;
if (k > meql) {
goto L50;
}
/* Set RATIO for linear interpolation between PAR and PARNEW. */
ratio = 0.0;
if (mp < *nact) {
ku = *nact - 1;
for (k = mp; k <= ku; ++k) {
if (PARNEW(k) > 0.0) {
ratio = PARNEW(k)/(PARNEW(k) - PAR(k));
idrop = k;
}
}
}
/* Apply the linear interpolation. */
theta = 1.0 - ratio;
n1 = *nact;
for (k = mp; k <= n1; ++k) {
PAR(k) = min(theta*PARNEW(k) + ratio*PAR(k) ,0.0);
}
for (i = 1; i <= n; ++i) {
GM(i) = theta*GMNEW(i) + ratio*GM(i);
}
/* Drop a constraint if RATIO is positive. */
if (ratio > 0.0) {
delcon(n, m, a, ia, iact, nact, z, u, relacc, idrop);
n1 = *nact;
for (k = idrop; k <= n1; ++k) {
PAR(k) = PAR(k+1);
}
goto L30;
}
/* Return if there is no freedom for a new search direction. */
if (*nact < n) {
goto L10;
}
*ddotgm = 0.0;
L120:
return;
}
static void
getd(const INTEGER n, const INTEGER m, const REAL a[], const INTEGER ia,
INTEGER iact[], INTEGER* nact, REAL par[], const REAL g[], REAL z[],
REAL u[], REAL d[], REAL ztg[], const REAL relacc, REAL* ddotg,
const INTEGER meql, const INTEGER mdeg, REAL gm[], REAL gmnew[],
REAL parnew[], REAL cgrad[])
{
INTEGER i, j, k, jm, iz;
REAL temp, ddotgm;
/* Initialize GM and cycle backwards through the active set. */
L10:
for (i = 1; i <= n; ++i) {
GM(i) = G(i);
}
k = *nact;
L30:
if (k > 0) {
/* Set TEMP to the next multiplier, but reduce the active set if
* TEMP has an unacceptable sign. */
temp = 0.0;
iz = k;
for (i = 1; i <= n; ++i) {
temp += Z(iz)*GM(i);
iz += n;
}
temp *= U(k);
if (k > meql && temp > 0.0) {
delcon(n, m, a, ia, iact, nact, z, u, relacc, k);
goto L10;
}
/* Update GM using the multiplier that has just been calculated. */
j = IACT(k);
if (j <= m) {
for (i = 1; i <= n; ++i) {
GM(i) -= temp*A(i,j);
}
} else {
jm = j - m;
if (jm <= n) {
GM(jm) += temp;
} else {
GM(jm-n) -= temp;
}
}
PAR(k) = temp;
--k;
goto L30;
}
/* Calculate the search direction and DDOTG. */
*ddotg = 0.0;
if (*nact < n) {
sdegen(n, m, a, ia, iact, nact, par, z, u, d, ztg, gm, relacc,
&ddotgm, meql, mdeg, gmnew, parnew, cgrad);
if (ddotgm < 0.0) {
for (i = 1; i <= n; ++i) {
*ddotg += D(i)*G(i);
}
}
}
}
static void
stepbd(const INTEGER n, const INTEGER m, const REAL a[], const INTEGER ia,
INTEGER iact[], REAL bres[], const REAL d[], REAL* stepcb, REAL* ddotg,
const INTEGER mdeg, INTEGER* msat, const INTEGER mtot, INTEGER* indxbd)
{
INTEGER i, j, k, jm, kl;
int iflag;
REAL sp, temp;
/* Set steps to constraint boundaries and find the least positive one. */
iflag = 0;
*stepcb = 0.0;
*indxbd = 0;
k = mdeg;
L10:
++k;
if (k > mtot) {
goto L40;
}
/* Form the scalar product of D with the current constraint normal. */
L20:
j = IACT(k);
if (j <= m) {
sp = 0.0;
for (i = 1; i <= n; ++i) {
sp += D(i)*A(i,j);
}
} else {
jm = j - m;
if (jm <= n) {
sp = -D(jm);
} else {
sp = D(jm-n);
}
}
/* The next branch is taken if label 20 was reached via label 50.0 */
if (iflag == 1) {
goto L60;
}
/* Set BRES(J) to indicate the status of the j-th constraint. */
if (sp*BRES(j) <= 0.0) {
BRES(j) = 0.0;
} else {
BRES(j) /= sp;
if (*stepcb == 0.0 || BRES(j) < *stepcb) {
*stepcb = BRES(j);
*indxbd = k;
}
}
goto L10;
L40:
/* Try to pass through the boundary of a violated constraint. */
L50:
if (*indxbd <= *msat) {
goto L80;
}
iflag = 1;
k = *indxbd;
goto L20;
L60:
++(*msat);
IACT(*indxbd) = IACT(*msat);
IACT(*msat) = j;
BRES(j) = 0.0;
*indxbd = *msat;
*ddotg -= sp;
if (*ddotg < 0.0 && *msat < mtot) {
/* Seek the next constraint boundary along the search direction. */
temp = 0.0;
kl = mdeg + 1;
for (k = kl; k <= mtot; ++k) {
j = IACT(k);
if (BRES(j) > 0.0) {
if (temp == 0.0 || BRES(j) < temp) {
temp = BRES(j);
*indxbd = k;
}
}
}
if (temp > 0.0) {
*stepcb = temp;
goto L50;
}
}
L80:
return;
}
static void
conres(const INTEGER n, const INTEGER m, const REAL a[], const INTEGER ia,
const REAL b[], const REAL xl[], const REAL xu[], const REAL x[],
INTEGER iact[], INTEGER* nact, REAL par[], REAL g[], REAL z[], REAL u[],
const REAL xbig[], REAL bres[], REAL d[], REAL ztg[], const REAL relacc,
const REAL tol, REAL* stepcb, REAL* sumres, const INTEGER meql,
INTEGER* msat, const INTEGER mtot, INTEGER* indxbd, REAL gm[],
REAL gmnew[], REAL parnew[], REAL cgrad[])
{
INTEGER i, j, k, kl, jm, idiff, mdeg, msatk;
REAL ddotg, res, sum, resabs, temp;
idiff = mtot - *msat;
/* Calculate and partition the residuals of the inactive constraints,
* and set the gradient vector when seeking feasibility. */
if (idiff > 0) {
for (i = 1; i <= n; ++i) {
G(i) = 0.0;
}
*sumres = 0.0;
}
msatk = *msat;
mdeg = *nact;
*msat = *nact;
kl = meql + 1;
for (k = kl; k <= mtot; ++k) {
j = IACT(k);
/* Calculate the residual of the current constraint. */
if (j <= m) {
res = B(j);
resabs = abs(B(j));
for (i = 1; i <= n; ++i) {
res -= X(i)*A(i,j);
resabs += abs(XBIG(i)*A(i,j));
}
} else {
jm = j - m;
if (jm <= n) {
res = X(jm) - XL(jm);
resabs = abs(XBIG(jm)) + abs(XL(jm));
} else {
jm -= n;
res = XU(jm) - X(jm);
resabs = abs(XBIG(jm)) + abs(XU(jm));
}
}
BRES(j) = res;
/* Set TEMP to the relative residual. */
temp = 0.0;
if (resabs != 0.0) {
temp = res/resabs;
}
if (k > msatk && temp < 0.0) {
if (temp + relacc >= 0.0) {
if (j <= m) {
sum = abs(B(j));
for (i = 1; i <= n; ++i) {
sum += abs(X(i)*A(i,j));
}
} else {
jm = j - m;
if (jm <= n) {
sum = abs(X(jm)) + abs(XL(jm));
} else {
sum = abs(X(jm-n)) + abs(XU(jm-n));
}
}
if (abs(res) <= sum*relacc) {
temp = 0.0;
}
}
}
/* Place the residual in the appropriate position. */
if (k <= *nact) {
goto L50;
}
if (k <= msatk || temp >= 0.0) {
++(*msat);
if (*msat < k) {
IACT(k) = IACT(*msat);
}
if (temp > tol) {
IACT(*msat) = j;
} else {
++mdeg;
IACT(*msat) = IACT(mdeg);
IACT(mdeg) = j;
}
/* Update the gradient and SUMRES if the constraint is violated when
* seeking feasibility. */
} else {
if (j <= m) {
for (i = 1; i <= n; ++i) {
G(i) += A(i,j);
}
} else {
j -= m;
if (j <= n) {