-
Notifications
You must be signed in to change notification settings - Fork 567
/
cobyla.c
1872 lines (1720 loc) · 54.7 KB
/
cobyla.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
/* cobyla : contrained optimization by linear approximation */
/*
* Copyright (c) 1992, Michael J. D. Powell (M.J.D.Powell@damtp.cam.ac.uk)
* Copyright (c) 2004, Jean-Sebastien Roy (js@jeannot.org)
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/*
* This software is a C version of COBYLA2, a contrained optimization by linear
* approximation package developed by Michael J. D. Powell in Fortran.
*
* The original source code can be found at :
* http://plato.la.asu.edu/topics/problems/nlores.html
*
* Original RCS id
* static char const rcsid[] =
* " @(#) $Jeannot: cobyla.c,v 1.11 2004/04/18 09:51:36 js Exp $";
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "cobyla.h"
/* SGJ, 2008: modified COBYLA code to take explicit account of bound
constraints. Since bound constraints are linear, these should
already be handled exactly when COBYLA optimizes it linear model.
However, they were not handled properly when COBYLA created its
initial simplex, or when COBYLA updated unacceptable simplices.
Since NLopt guarantees that the objective will not be evaluated
outside the bound constraints, this required us to handle such
points by putting a slope discontinuity into the objective &
constraints (below), which slows convergence considerably for
smooth functions. Instead, handling them explicitly prevents this
problem */
#define ENFORCE_BOUNDS 1
#define MIN2(a,b) ((a) <= (b) ? (a) : (b))
#define MAX2(a,b) ((a) >= (b) ? (a) : (b))
#define U(n) ((unsigned) (n))
/**************************************************************************/
/* SGJ, 2008: NLopt-style interface function: */
typedef struct {
nlopt_func f;
void *f_data;
unsigned m_orig;
nlopt_constraint *fc;
unsigned p;
nlopt_constraint *h;
double *xtmp;
double *lb, *ub;
double *con_tol, *scale;
nlopt_stopping *stop;
} func_wrap_state;
static int func_wrap(int ni, int mi, double *x, double *f, double *con,
func_wrap_state *s)
{
unsigned n = U(ni);
unsigned i, j, k;
double *xtmp = s->xtmp;
const double *lb = s->lb, *ub = s->ub;
(void) mi; /* unused */
/* in nlopt, we guarante that the function is never evaluated outside
the lb and ub bounds, so we need force this with xtmp ... note
that this leads to discontinuity in the first derivative, which
slows convergence if we don't enable the ENFORCE_BOUNDS feature
above. */
for (j = 0; j < n; ++j) {
if (x[j] < lb[j]) xtmp[j] = lb[j];
else if (x[j] > ub[j]) xtmp[j] = ub[j];
else xtmp[j] = x[j];
}
nlopt_unscale(n, s->scale, xtmp, xtmp);
*f = s->f(n, xtmp, NULL, s->f_data);
if (nlopt_stop_forced(s->stop)) return 1;
i = 0;
for (j = 0; j < s->m_orig; ++j) {
nlopt_eval_constraint(con + i, NULL, s->fc+j, n, xtmp);
if (nlopt_stop_forced(s->stop)) return 1;
for (k = 0; k < s->fc[j].m; ++k)
con[i + k] = -con[i + k];
i += s->fc[j].m;
}
for (j = 0; j < s->p; ++j) {
nlopt_eval_constraint(con + i, NULL, s->h+j, n, xtmp);
if (nlopt_stop_forced(s->stop)) return 1;
for (k = 0; k < s->h[j].m; ++k)
con[(i + s->h[j].m) + k] = -con[i + k];
i += 2 * s->h[j].m;
}
for (j = 0; j < n; ++j) {
if (!nlopt_isinf(lb[j]))
con[i++] = x[j] - lb[j];
if (!nlopt_isinf(ub[j]))
con[i++] = ub[j] - x[j];
}
return 0;
}
/*
* Verbosity level
*/
typedef enum {
COBYLA_MSG_NONE = 0, /* No messages */
COBYLA_MSG_EXIT = 1, /* Exit reasons */
COBYLA_MSG_ITER = 2, /* Rho and Sigma changes */
COBYLA_MSG_INFO = 3 /* Informational messages */
} cobyla_message;
/*
* A function as required by cobyla
* state is a void pointer provided to the function at each call
*
* n : the number of variables
* m : the number of constraints
* x : on input, then vector of variables (should not be modified)
* f : on output, the value of the function
* con : on output, the value of the constraints (vector of size m)
* state : on input, the value of the state variable as provided to cobyla
*
* COBYLA will try to make all the values of the constraints positive.
* So if you want to input a constraint j such as x[i] <= MAX, set:
* con[j] = MAX - x[i]
* The function must returns 0 if no error occurs or 1 to immediately end the
* minimization.
*
*/
typedef int cobyla_function(int n, int m, double *x, double *f, double *con,
func_wrap_state *state);
/*
* cobyla : minimize a function subject to constraints
*
* n : number of variables (>=0)
* m : number of constraints (>=0)
* x : on input, initial estimate ; on output, the solution
* minf : on output, minimum objective function
* rhobeg : a reasonable initial change to the variables
* stop : the NLopt stopping criteria
* lb, ub : lower and upper bounds on x
* message : see the cobyla_message enum
* calcfc : the function to minimize (see cobyla_function)
* state : used by function (see cobyla_function)
*
* The cobyla function returns the usual nlopt_result codes.
*
*/
extern nlopt_result cobyla(int n, int m, double *x, double *minf, double rhobeg, double rhoend, nlopt_stopping *stop, const double *lb, const double *ub,
int message, cobyla_function *calcfc, func_wrap_state *state);
nlopt_result cobyla_minimize(unsigned n, nlopt_func f, void *f_data,
unsigned m, nlopt_constraint *fc,
unsigned p, nlopt_constraint *h,
const double *lb, const double *ub, /* bounds */
double *x, /* in: initial guess, out: minimizer */
double *minf,
nlopt_stopping *stop,
const double *dx)
{
unsigned i, j;
func_wrap_state s;
nlopt_result ret;
double rhobeg, rhoend;
s.f = f; s.f_data = f_data;
s.m_orig = m;
s.fc = fc;
s.p = p;
s.h = h;
s.stop = stop;
s.lb = s.ub = s.xtmp = s.con_tol = s.scale = NULL;
s.scale = nlopt_compute_rescaling(n, dx);
if (!s.scale) { ret = NLOPT_OUT_OF_MEMORY; goto done; }
for (j = 0; j < n; ++j)
if (s.scale[j] == 0 || !nlopt_isfinite(s.scale[j])) {
nlopt_stop_msg(stop, "invalid scaling %g of dimension %d: possible over/underflow?", s.scale[j], j);
ret = NLOPT_INVALID_ARGS; goto done;
}
s.lb = nlopt_new_rescaled(n, s.scale, lb);
if (!s.lb) { ret = NLOPT_OUT_OF_MEMORY; goto done; }
s.ub = nlopt_new_rescaled(n, s.scale, ub);
if (!s.ub) { ret = NLOPT_OUT_OF_MEMORY; goto done; }
nlopt_reorder_bounds(n, s.lb, s.ub);
s.xtmp = (double *) malloc(sizeof(double) * n);
if (!s.xtmp) { ret = NLOPT_OUT_OF_MEMORY; goto done; }
/* SGJ, 2008: compute rhoend from NLopt stop info */
rhobeg = fabs(dx[0] / s.scale[0]);
rhoend = stop->xtol_rel * (rhobeg);
if (stop->xtol_abs)
for (j = 0; j < n; ++j)
if (rhoend < stop->xtol_abs[j] / fabs(s.scale[j]))
rhoend = stop->xtol_abs[j] / fabs(s.scale[j]);
/* each equality constraint gives two inequality constraints */
m = nlopt_count_constraints(m, fc) + 2 * nlopt_count_constraints(p, h);
/* add constraints for lower/upper bounds (if any) */
for (j = 0; j < n; ++j) {
if (!nlopt_isinf(lb[j]))
++m;
if (!nlopt_isinf(ub[j]))
++m;
}
s.con_tol = (double *) malloc(sizeof(double) * m);
if (m && !s.con_tol) { ret = NLOPT_OUT_OF_MEMORY; goto done; }
for (j = 0; j < m; ++j) s.con_tol[j] = 0;
for (j = i = 0; i < s.m_orig; ++i) {
unsigned ji = j, jnext = j + fc[i].m;
for (; j < jnext; ++j) s.con_tol[j] = fc[i].tol[j - ji];
}
for (i = 0; i < s.p; ++i) {
unsigned ji = j, jnext = j + h[i].m;
for (; j < jnext; ++j) s.con_tol[j] = h[i].tol[j - ji];
ji = j; jnext = j + h[i].m;
for (; j < jnext; ++j) s.con_tol[j] = h[i].tol[j - ji];
}
nlopt_rescale(n, s.scale, x, x);
ret = cobyla((int) n, (int) m, x, minf, rhobeg, rhoend,
stop, s.lb, s.ub, COBYLA_MSG_NONE,
func_wrap, &s);
nlopt_unscale(n, s.scale, x, x);
/* make sure e.g. rounding errors didn't push us slightly out of bounds */
for (j = 0; j < n; ++j) {
if (x[j] < lb[j]) x[j] = lb[j];
if (x[j] > ub[j]) x[j] = ub[j];
}
done:
free(s.con_tol);
free(s.xtmp);
free(s.ub);
free(s.lb);
free(s.scale);
return ret;
}
/**************************************************************************/
/* SGJ, 2010: I find that it seems to increase robustness of the algorithm
if, on "simplex" steps (which are intended to improve the linear
independence of the simplex, not improve the objective), we multiply
the steps by pseudorandom numbers in [0,1). Intuitively, pseudorandom
steps are likely to avoid any accidental dependency in the simplex
vertices. However, since I still want COBYLA to be a deterministic
algorithm, and I don't care all that much about the quality of the
randomness here, I implement a simple linear congruential generator
rather than use nlopt_urand, and set the initial seed deterministically. */
#if defined(HAVE_STDINT_H)
# include <stdint.h>
#endif
#ifndef HAVE_UINT32_T
# if SIZEOF_UNSIGNED_LONG == 4
typedef unsigned long uint32_t;
# elif SIZEOF_UNSIGNED_INT == 4
typedef unsigned int uint32_t;
# else
# error No 32-bit unsigned integer type
# endif
#endif
/* a simple linear congruential generator */
static uint32_t lcg_rand(uint32_t *seed)
{
return (*seed = *seed * 1103515245 + 12345);
}
static double lcg_urand(uint32_t *seed, double a, double b)
{
return a + lcg_rand(seed) * (b - a) / ((uint32_t) -1);
}
/**************************************************************************/
static nlopt_result cobylb(int *n, int *m, int *mpp, double *x, double *minf, double *rhobeg, double rhoend,
nlopt_stopping *stop, const double *lb, const double *ub, int *iprint, double *con, double *sim,
double *simi, double *datmat, double *a, double *vsig, double *veta,
double *sigbar, double *dx, double *w, int *iact, cobyla_function *calcfc,
func_wrap_state *state);
static nlopt_result trstlp(int *n, int *m, double *a, double *b, double *rho,
double *dx, int *ifull, int *iact, double *z__, double *zdota, double *vmultc,
double *sdirn, double *dxnew, double *vmultd);
/* ------------------------------------------------------------------------ */
nlopt_result cobyla(int n, int m, double *x, double *minf, double rhobeg, double rhoend, nlopt_stopping *stop, const double *lb, const double *ub, int iprint,
cobyla_function *calcfc, func_wrap_state *state)
{
int icon, isim, isigb, idatm, iveta, isimi, ivsig, iwork, ia, idx, mpp;
int *iact;
double *w;
nlopt_result rc;
/*
* This subroutine minimizes an objective function F(X) subject to M
* inequality constraints on X, where X is a vector of variables that has
* N components. The algorithm employs linear approximations to the
* objective and constraint functions, the approximations being formed by
* linear interpolation at N+1 points in the space of the variables.
* We regard these interpolation points as vertices of a simplex. The
* parameter RHO controls the size of the simplex and it is reduced
* automatically from RHOBEG to RHOEND. For each RHO the subroutine tries
* to achieve a good vector of variables for the current size, and then
* RHO is reduced until the value RHOEND is reached. Therefore RHOBEG and
* RHOEND should be set to reasonable initial changes to and the required
* accuracy in the variables respectively, but this accuracy should be
* viewed as a subject for experimentation because it is not guaranteed.
* The subroutine has an advantage over many of its competitors, however,
* which is that it treats each constraint individually when calculating
* a change to the variables, instead of lumping the constraints together
* into a single penalty function. The name of the subroutine is derived
* from the phrase Constrained Optimization BY Linear Approximations.
*
* The user must set the values of N, M, RHOBEG and RHOEND, and must
* provide an initial vector of variables in X. Further, the value of
* IPRINT should be set to 0, 1, 2 or 3, which controls the amount of
* printing during the calculation. Specifically, there is no output if
* IPRINT=0 and there is output only at the end of the calculation if
* IPRINT=1. Otherwise each new value of RHO and SIGMA is printed.
* Further, the vector of variables and some function information are
* given either when RHO is reduced or when each new value of F(X) is
* computed in the cases IPRINT=2 or IPRINT=3 respectively. Here SIGMA
* is a penalty parameter, it being assumed that a change to X is an
* improvement if it reduces the merit function
* F(X)+SIGMA*MAX(0.0,-C1(X),-C2(X),...,-CM(X)),
* where C1,C2,...,CM denote the constraint functions that should become
* nonnegative eventually, at least to the precision of RHOEND. In the
* printed output the displayed term that is multiplied by SIGMA is
* called MAXCV, which stands for 'MAXimum Constraint Violation'. The
* argument MAXFUN is an int variable that must be set by the user to a
* limit on the number of calls of CALCFC, the purpose of this routine being
* given below. The value of MAXFUN will be altered to the number of calls
* of CALCFC that are made. The arguments W and IACT provide real and
* int arrays that are used as working space. Their lengths must be at
* least N*(3*N+2*M+11)+4*M+6 and M+1 respectively.
*
* In order to define the objective and constraint functions, we require
* a subroutine that has the name and arguments
* SUBROUTINE CALCFC (N,M,X,F,CON)
* DIMENSION X(*),CON(*) .
* The values of N and M are fixed and have been defined already, while
* X is now the current vector of variables. The subroutine should return
* the objective and constraint functions at X in F and CON(1),CON(2),
* ...,CON(M). Note that we are trying to adjust X so that F(X) is as
* small as possible subject to the constraint functions being nonnegative.
*
* Partition the working space array W to provide the storage that is needed
* for the main calculation.
*/
*(stop->nevals_p) = 0;
if (n == 0)
{
if (iprint>=1) fprintf(stderr, "cobyla: N==0.\n");
return NLOPT_SUCCESS;
}
if (n < 0 || m < 0)
{
if (iprint>=1) fprintf(stderr, "cobyla: N<0 or M<0.\n");
return NLOPT_INVALID_ARGS;
}
/* workspace allocation */
w = (double*) malloc(U(n*(3*n+2*m+11)+4*m+6)*sizeof(*w));
if (w == NULL)
{
if (iprint>=1) fprintf(stderr, "cobyla: memory allocation error.\n");
return NLOPT_OUT_OF_MEMORY;
}
iact = (int*)malloc(U(m+1)*sizeof(*iact));
if (iact == NULL)
{
if (iprint>=1) fprintf(stderr, "cobyla: memory allocation error.\n");
free(w);
return NLOPT_OUT_OF_MEMORY;
}
/* Parameter adjustments */
--iact;
--w;
--x;
--lb; --ub;
/* Function Body */
mpp = m + 2;
icon = 1;
isim = icon + mpp;
isimi = isim + n * n + n;
idatm = isimi + n * n;
ia = idatm + n * mpp + mpp;
ivsig = ia + m * n + n;
iveta = ivsig + n;
isigb = iveta + n;
idx = isigb + n;
iwork = idx + n;
rc = cobylb(&n, &m, &mpp, &x[1], minf, &rhobeg, rhoend, stop, &lb[1], &ub[1], &iprint,
&w[icon], &w[isim], &w[isimi], &w[idatm], &w[ia], &w[ivsig], &w[iveta],
&w[isigb], &w[idx], &w[iwork], &iact[1], calcfc, state);
/* Parameter adjustments (reverse) */
++iact;
++w;
free(w);
free(iact);
return rc;
} /* cobyla */
/* ------------------------------------------------------------------------- */
static nlopt_result cobylb(int *n, int *m, int *mpp,
double *x, double *minf, double *rhobeg, double rhoend,
nlopt_stopping *stop, const double *lb, const double *ub,
int *iprint, double *con, double *sim, double *simi,
double *datmat, double *a, double *vsig, double *veta,
double *sigbar, double *dx, double *w, int *iact, cobyla_function *calcfc,
func_wrap_state *state)
{
/* System generated locals */
int sim_dim1, sim_offset, simi_dim1, simi_offset, datmat_dim1,
datmat_offset, a_dim1, a_offset, i__1, i__2, i__3;
double d__1, d__2;
/* Local variables */
double alpha, delta, denom, tempa, barmu;
double beta, cmin = 0.0, cmax = 0.0;
double cvmaxm, dxsign, prerem = 0.0;
double edgmax, pareta, prerec = 0.0, phimin, parsig = 0.0;
double gamma_;
double phi, rho, sum = 0.0;
double ratio, vmold, parmu, error, vmnew;
double resmax, cvmaxp;
double resnew, trured;
double temp, wsig, f;
double weta;
int i__, j, k, l;
int idxnew;
int iflag = 0;
int iptemp;
int isdirn, izdota;
int ivmc;
int ivmd;
int mp, np, iz, ibrnch;
int nbest, ifull = 0, iptem, jdrop;
nlopt_result rc = NLOPT_SUCCESS;
uint32_t seed = (uint32_t) (*n + *m); /* arbitrary deterministic LCG seed */
int feasible;
/* SGJ, 2008: added code to keep track of minimum feasible function val */
*minf = HUGE_VAL;
/* Set the initial values of some parameters. The last column of SIM holds */
/* the optimal vertex of the current simplex, and the preceding N columns */
/* hold the displacements from the optimal vertex to the other vertices. */
/* Further, SIMI holds the inverse of the matrix that is contained in the */
/* first N columns of SIM. */
/* Parameter adjustments */
a_dim1 = *n;
a_offset = 1 + a_dim1 * 1;
a -= a_offset;
simi_dim1 = *n;
simi_offset = 1 + simi_dim1 * 1;
simi -= simi_offset;
sim_dim1 = *n;
sim_offset = 1 + sim_dim1 * 1;
sim -= sim_offset;
datmat_dim1 = *mpp;
datmat_offset = 1 + datmat_dim1 * 1;
datmat -= datmat_offset;
--x;
--con;
--vsig;
--veta;
--sigbar;
--dx;
--w;
--iact;
--lb; --ub;
/* Function Body */
iptem = MIN2(*n,4);
iptemp = iptem + 1;
np = *n + 1;
mp = *m + 1;
alpha = .25;
beta = 2.1;
gamma_ = .5;
delta = 1.1;
rho = *rhobeg;
parmu = 0.;
if (*iprint >= 2) {
fprintf(stderr,
"cobyla: the initial value of RHO is %12.6E and PARMU is set to zero.\n",
rho);
}
temp = 1. / rho;
i__1 = *n;
for (i__ = 1; i__ <= i__1; ++i__) {
double rhocur;
sim[i__ + np * sim_dim1] = x[i__];
i__2 = *n;
for (j = 1; j <= i__2; ++j) {
sim[i__ + j * sim_dim1] = 0.;
simi[i__ + j * simi_dim1] = 0.;
}
rhocur = rho;
#if ENFORCE_BOUNDS
/* SGJ: make sure step rhocur stays inside [lb,ub] */
if (x[i__] + rhocur > ub[i__]) {
if (x[i__] - rhocur >= lb[i__])
rhocur = -rhocur;
else if (ub[i__] - x[i__] > x[i__] - lb[i__])
rhocur = 0.5 * (ub[i__] - x[i__]);
else
rhocur = 0.5 * (x[i__] - lb[i__]);
}
#endif
sim[i__ + i__ * sim_dim1] = rhocur;
simi[i__ + i__ * simi_dim1] = 1.0 / rhocur;
}
jdrop = np;
ibrnch = 0;
/* Make the next call of the user-supplied subroutine CALCFC. These */
/* instructions are also used for calling CALCFC during the iterations of */
/* the algorithm. */
/* SGJ comment: why the hell couldn't he just use a damn subroutine?
#*&!%*@ Fortran-66 spaghetti code */
L40:
if (nlopt_stop_forced(stop)) rc = NLOPT_FORCED_STOP;
else if (*(stop->nevals_p) > 0) {
if (nlopt_stop_evals(stop)) rc = NLOPT_MAXEVAL_REACHED;
else if (nlopt_stop_time(stop)) rc = NLOPT_MAXTIME_REACHED;
}
if (rc != NLOPT_SUCCESS) goto L600;
++ *(stop->nevals_p);
if (calcfc(*n, *m, &x[1], &f, &con[1], state))
{
if (*iprint >= 1) {
fprintf(stderr, "cobyla: user requested end of minimization.\n");
}
rc = NLOPT_FORCED_STOP;
goto L600;
}
resmax = 0.;
feasible = 1; /* SGJ, 2010 */
if (*m > 0) {
i__1 = *m;
for (k = 1; k <= i__1; ++k) {
d__1 = resmax, d__2 = -con[k];
resmax = MAX2(d__1,d__2);
if (d__2 > state->con_tol[k-1])
feasible = 0; /* SGJ, 2010 */
}
}
/* SGJ, 2008: check for minf_max reached by feasible point */
if (f < stop->minf_max && feasible) {
rc = NLOPT_MINF_MAX_REACHED;
goto L620; /* not L600 because we want to use current x, f, resmax */
}
if (*(stop->nevals_p) == *iprint - 1 || *iprint == 3) {
fprintf(stderr, "cobyla: NFVALS = %4d, F =%13.6E, MAXCV =%13.6E\n",
*(stop->nevals_p), f, resmax);
i__1 = iptem;
fprintf(stderr, "cobyla: X =");
for (i__ = 1; i__ <= i__1; ++i__) {
if (i__>1) fprintf(stderr, " ");
fprintf(stderr, "%13.6E", x[i__]);
}
if (iptem < *n) {
i__1 = *n;
for (i__ = iptemp; i__ <= i__1; ++i__) {
if (!((i__-1) % 4)) fprintf(stderr, "\ncobyla: ");
fprintf(stderr, "%15.6E", x[i__]);
}
}
fprintf(stderr, "\n");
}
con[mp] = f;
con[*mpp] = resmax;
if (ibrnch == 1) {
goto L440;
}
/* Set the recently calculated function values in a column of DATMAT. This */
/* array has a column for each vertex of the current simplex, the entries of */
/* each column being the values of the constraint functions (if any) */
/* followed by the objective function and the greatest constraint violation */
/* at the vertex. */
i__1 = *mpp;
for (k = 1; k <= i__1; ++k) {
datmat[k + jdrop * datmat_dim1] = con[k];
}
if (*(stop->nevals_p) > np) {
goto L130;
}
/* Exchange the new vertex of the initial simplex with the optimal vertex if */
/* necessary. Then, if the initial simplex is not complete, pick its next */
/* vertex and calculate the function values there. */
if (jdrop <= *n) {
if (datmat[mp + np * datmat_dim1] <= f) {
x[jdrop] = sim[jdrop + np * sim_dim1];
} else { /* improvement in function val */
double rhocur = x[jdrop] - sim[jdrop + np * sim_dim1];
/* SGJ: use rhocur instead of rho. In original code, rhocur == rho
always, but I want to change this to ensure that simplex points
fall within [lb,ub]. */
sim[jdrop + np * sim_dim1] = x[jdrop];
i__1 = *mpp;
for (k = 1; k <= i__1; ++k) {
datmat[k + jdrop * datmat_dim1] = datmat[k + np * datmat_dim1]
;
datmat[k + np * datmat_dim1] = con[k];
}
i__1 = jdrop;
for (k = 1; k <= i__1; ++k) {
sim[jdrop + k * sim_dim1] = -rhocur;
temp = 0.f;
i__2 = jdrop;
for (i__ = k; i__ <= i__2; ++i__) {
temp -= simi[i__ + k * simi_dim1];
}
simi[jdrop + k * simi_dim1] = temp;
}
}
}
if (*(stop->nevals_p) <= *n) { /* evaluating initial simplex */
jdrop = *(stop->nevals_p);
/* SGJ: was += rho, but using sim[jdrop,jdrop] enforces consistency
if we change the stepsize above to stay in [lb,ub]. */
x[jdrop] += sim[jdrop + jdrop * sim_dim1];
goto L40;
}
L130:
ibrnch = 1;
/* Identify the optimal vertex of the current simplex. */
L140:
phimin = datmat[mp + np * datmat_dim1] + parmu * datmat[*mpp + np *
datmat_dim1];
nbest = np;
i__1 = *n;
for (j = 1; j <= i__1; ++j) {
temp = datmat[mp + j * datmat_dim1] + parmu * datmat[*mpp + j *
datmat_dim1];
if (temp < phimin) {
nbest = j;
phimin = temp;
} else if (temp == phimin && parmu == 0.) {
if (datmat[*mpp + j * datmat_dim1] < datmat[*mpp + nbest *
datmat_dim1]) {
nbest = j;
}
}
}
/* Switch the best vertex into pole position if it is not there already, */
/* and also update SIM, SIMI and DATMAT. */
if (nbest <= *n) {
i__1 = *mpp;
for (i__ = 1; i__ <= i__1; ++i__) {
temp = datmat[i__ + np * datmat_dim1];
datmat[i__ + np * datmat_dim1] = datmat[i__ + nbest * datmat_dim1]
;
datmat[i__ + nbest * datmat_dim1] = temp;
}
i__1 = *n;
for (i__ = 1; i__ <= i__1; ++i__) {
temp = sim[i__ + nbest * sim_dim1];
sim[i__ + nbest * sim_dim1] = 0.;
sim[i__ + np * sim_dim1] += temp;
tempa = 0.;
i__2 = *n;
for (k = 1; k <= i__2; ++k) {
sim[i__ + k * sim_dim1] -= temp;
tempa -= simi[k + i__ * simi_dim1];
}
simi[nbest + i__ * simi_dim1] = tempa;
}
}
/* Make an error return if SIGI is a poor approximation to the inverse of */
/* the leading N by N submatrix of SIG. */
error = 0.;
i__1 = *n;
for (i__ = 1; i__ <= i__1; ++i__) {
i__2 = *n;
for (j = 1; j <= i__2; ++j) {
temp = 0.;
if (i__ == j) {
temp += -1.;
}
i__3 = *n;
for (k = 1; k <= i__3; ++k) if (sim[k + j * sim_dim1] != 0) {
temp += simi[i__ + k * simi_dim1] * sim[k + j * sim_dim1];
}
d__1 = error, d__2 = fabs(temp);
error = MAX2(d__1,d__2);
}
}
if (error > .1) {
if (*iprint >= 1) {
fprintf(stderr, "cobyla: rounding errors are becoming damaging.\n");
}
rc = NLOPT_ROUNDOFF_LIMITED;
goto L600;
}
/* Calculate the coefficients of the linear approximations to the objective */
/* and constraint functions, placing minus the objective function gradient */
/* after the constraint gradients in the array A. The vector W is used for */
/* working space. */
i__2 = mp;
for (k = 1; k <= i__2; ++k) {
con[k] = -datmat[k + np * datmat_dim1];
i__1 = *n;
for (j = 1; j <= i__1; ++j) {
w[j] = datmat[k + j * datmat_dim1] + con[k];
}
i__1 = *n;
for (i__ = 1; i__ <= i__1; ++i__) {
temp = 0.;
i__3 = *n;
for (j = 1; j <= i__3; ++j) {
temp += w[j] * simi[j + i__ * simi_dim1];
}
if (k == mp) {
temp = -temp;
}
a[i__ + k * a_dim1] = temp;
}
}
/* Calculate the values of sigma and eta, and set IFLAG=0 if the current */
/* simplex is not acceptable. */
iflag = 1;
parsig = alpha * rho;
pareta = beta * rho;
i__1 = *n;
for (j = 1; j <= i__1; ++j) {
wsig = 0.;
weta = 0.;
i__2 = *n;
for (i__ = 1; i__ <= i__2; ++i__) {
d__1 = simi[j + i__ * simi_dim1];
wsig += d__1 * d__1;
d__1 = sim[i__ + j * sim_dim1];
weta += d__1 * d__1;
}
vsig[j] = 1. / sqrt(wsig);
veta[j] = sqrt(weta);
if (vsig[j] < parsig || veta[j] > pareta) {
iflag = 0;
}
}
/* If a new vertex is needed to improve acceptability, then decide which */
/* vertex to drop from the simplex. */
if (ibrnch == 1 || iflag == 1) {
goto L370;
}
jdrop = 0;
temp = pareta;
i__1 = *n;
for (j = 1; j <= i__1; ++j) {
if (veta[j] > temp) {
jdrop = j;
temp = veta[j];
}
}
if (jdrop == 0) {
i__1 = *n;
for (j = 1; j <= i__1; ++j) {
if (vsig[j] < temp) {
jdrop = j;
temp = vsig[j];
}
}
}
/* Calculate the step to the new vertex and its sign. */
temp = gamma_ * rho * vsig[jdrop];
i__1 = *n;
for (i__ = 1; i__ <= i__1; ++i__) {
dx[i__] = temp * simi[jdrop + i__ * simi_dim1];
}
cvmaxp = 0.;
cvmaxm = 0.;
i__1 = mp;
for (k = 1; k <= i__1; ++k) {
sum = 0.;
i__2 = *n;
for (i__ = 1; i__ <= i__2; ++i__) {
sum += a[i__ + k * a_dim1] * dx[i__];
}
if (k < mp) {
temp = datmat[k + np * datmat_dim1];
d__1 = cvmaxp, d__2 = -sum - temp;
cvmaxp = MAX2(d__1,d__2);
d__1 = cvmaxm, d__2 = sum - temp;
cvmaxm = MAX2(d__1,d__2);
}
}
dxsign = 1.;
if (parmu * (cvmaxp - cvmaxm) > sum + sum) {
dxsign = -1.;
}
/* Update the elements of SIM and SIMI, and set the next X. */
temp = 0.;
i__1 = *n;
for (i__ = 1; i__ <= i__1; ++i__) {
/* SGJ, 2010: pseudo-randomize simplex steps (see LCG comments above) */
dx[i__] = dxsign * dx[i__] * lcg_urand(&seed, 0.01, 1);
/* SGJ: make sure dx step says in [lb,ub] */
#if ENFORCE_BOUNDS
{
double xi = sim[i__ + np * sim_dim1];
fixdx:
if (xi + dx[i__] > ub[i__])
dx[i__] = -dx[i__];
if (xi + dx[i__] < lb[i__]) {
if (xi - dx[i__] <= ub[i__])
dx[i__] = -dx[i__];
else { /* try again with halved step */
dx[i__] *= 0.5;
goto fixdx;
}
}
}
#endif
sim[i__ + jdrop * sim_dim1] = dx[i__];
temp += simi[jdrop + i__ * simi_dim1] * dx[i__];
}
i__1 = *n;
for (i__ = 1; i__ <= i__1; ++i__) {
simi[jdrop + i__ * simi_dim1] /= temp;
}
i__1 = *n;
for (j = 1; j <= i__1; ++j) {
if (j != jdrop) {
temp = 0.;
i__2 = *n;
for (i__ = 1; i__ <= i__2; ++i__) {
temp += simi[j + i__ * simi_dim1] * dx[i__];
}
i__2 = *n;
for (i__ = 1; i__ <= i__2; ++i__) {
simi[j + i__ * simi_dim1] -= temp * simi[jdrop + i__ *
simi_dim1];
}
}
x[j] = sim[j + np * sim_dim1] + dx[j];
}
goto L40;
/* Calculate DX=x(*)-x(0). Branch if the length of DX is less than 0.5*RHO. */
L370:
iz = 1;
izdota = iz + *n * *n;
ivmc = izdota + *n;
isdirn = ivmc + mp;
idxnew = isdirn + *n;
ivmd = idxnew + *n;
rc = trstlp(n, m, &a[a_offset], &con[1], &rho, &dx[1], &ifull, &iact[1], &w[
iz], &w[izdota], &w[ivmc], &w[isdirn], &w[idxnew], &w[ivmd]);
if (rc != NLOPT_SUCCESS) goto L600;
#if ENFORCE_BOUNDS
/* SGJ: since the bound constraints are linear, we should never get
a dx that lies outside the [lb,ub] constraints here, but we'll
enforce this anyway just to be paranoid */
i__1 = *n;
for (i__ = 1; i__ <= i__1; ++i__) {
double xi = sim[i__ + np * sim_dim1];
if (xi + dx[i__] > ub[i__]) dx[i__] = ub[i__] - xi;
if (xi + dx[i__] < lb[i__]) dx[i__] = xi - lb[i__];
}
#endif
if (ifull == 0) {
temp = 0.;
i__1 = *n;
for (i__ = 1; i__ <= i__1; ++i__) {
d__1 = dx[i__];
temp += d__1 * d__1;
}
if (temp < rho * .25 * rho) {
ibrnch = 1;
goto L550;
}
}
/* Predict the change to F and the new maximum constraint violation if the */
/* variables are altered from x(0) to x(0)+DX. */
resnew = 0.;
con[mp] = 0.;
i__1 = mp;
for (k = 1; k <= i__1; ++k) {
sum = con[k];
i__2 = *n;
for (i__ = 1; i__ <= i__2; ++i__) {
sum -= a[i__ + k * a_dim1] * dx[i__];
}
if (k < mp) {
resnew = MAX2(resnew,sum);
}
}
/* Increase PARMU if necessary and branch back if this change alters the */
/* optimal vertex. Otherwise PREREM and PREREC will be set to the predicted */
/* reductions in the merit function and the maximum constraint violation */
/* respectively. */
barmu = 0.;
prerec = datmat[*mpp + np * datmat_dim1] - resnew;
if (prerec > 0.) {
barmu = sum / prerec;
}
if (parmu < barmu * 1.5) {
parmu = barmu * 2.;
if (*iprint >= 2) {
fprintf(stderr, "cobyla: increase in PARMU to %12.6E\n", parmu);
}
phi = datmat[mp + np * datmat_dim1] + parmu * datmat[*mpp + np *
datmat_dim1];
i__1 = *n;
for (j = 1; j <= i__1; ++j) {
temp = datmat[mp + j * datmat_dim1] + parmu * datmat[*mpp + j *
datmat_dim1];
if (temp < phi) {
goto L140;
}
if (temp == phi && parmu == 0.f) {
if (datmat[*mpp + j * datmat_dim1] < datmat[*mpp + np *
datmat_dim1]) {
goto L140;
}
}
}
}