-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeightSolver.c
More file actions
828 lines (814 loc) · 38.3 KB
/
Copy pathWeightSolver.c
File metadata and controls
828 lines (814 loc) · 38.3 KB
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
// CLASCAL (WeightSolver.c)
//
// Copyright (c) 2010 by John Ashley Burgoyne and the Royal Institute for the
// Advancement of Learning (McGill University). All rights reserved.
//
// This source is adapted from Suzanne Winsberg's CLASCAL, version 7.01 (May
// 1993), written in FORTRAN 77.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions, and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions, and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of McGill University nor the names of its contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
#include <float.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <Accelerate/Accelerate.h>
#include "inlines.h"
#include "StimulusSet.h"
#include "_StimulusSet.h"
#include "SubjectSet.h"
#include "Experiment.h"
#include "Model.h"
#include "_Model.h"
#include "ClassAssignment.h"
#include "ModelSpace.h"
#include "_ModelSpace.h"
#include "Solution.h"
#include "_Solution.h"
#include "_WeightSolver.h"
typedef __CLPK_integer LPInteger;
struct _WeightSolver {
Solution * solution; // non-const to allow for lazy initialisation
size_t gradientSize;
double * restrict gradient;
size_t hessianSize;
double * restrict hessian;
};
typedef struct _WeightSolver WeightSolver;
/*
* Returns the gradient of q1 for the weights.
*/
static double * NewGradient(const WeightSolver * restrict self)
{
const double * restrict weightedRelativeErrors;
weightedRelativeErrors = WeightedRelativeErrors(self->solution);
if (!weightedRelativeErrors) return NULL;
const ModelSpace * restrict space = SolutionModelSpace(self->solution);
const double * restrict squaredDifferences = SquaredDifferences(space);
if (!squaredDifferences) return NULL;
const StimulusSet * restrict stimulusSet = ModelSpaceStimulusSet(space);
const size_t pairCount = StimulusPairCount(stimulusSet);
if (!pairCount) return NULL;
const Model * restrict model = ModelSpaceModel(space);
const size_t dimensionCount = DimensionCount(model);
if (!dimensionCount) return NULL;
const size_t classCount = ClassCount(model);
if (classCount < 2) return NULL;
const SpecificityType specificityType = ModelSpecificityType(model);
const double * restrict pairwiseSpecificities;
pairwiseSpecificities = PairwiseSpecificities(space);
if (specificityType == GlobalSpecificities && !pairwiseSpecificities)
return NULL;
const size_t weightsSize = WeightsSize(model);
if (specificityType == GlobalSpecificities && !weightsSize) return NULL;
double * restrict gradient = SafeCalloc(self->gradientSize,
sizeof(double));
cblas_dgemm(CblasRowMajor,
CblasTrans,
CblasTrans,
(int)dimensionCount,
(int)classCount,
(int)pairCount,
-1.0,
squaredDifferences,
(int)dimensionCount,
weightedRelativeErrors,
(int)pairCount,
0.0,
gradient,
(int)classCount);
if (specificityType == GlobalSpecificities)
cblas_dgemv(CblasRowMajor,
CblasNoTrans,
(int)classCount,
(int)pairCount,
-1.0,
weightedRelativeErrors,
(int)pairCount,
pairwiseSpecificities,
1,
0.0,
gradient + weightsSize,
1);
return gradient;
}
/*
* Returns the expected Hessian of coordinate weights against coordinate
* weights. The order of the indices for rows and columns corresponds to that
* of the coordinate gradient in row-major order. The matrix is unpacked. Note
* that Hessian factors must be initialised before calling this function.
*/
static double *
NewExpectedCoordinateHessian(const WeightSolver * restrict self)
{
const double * restrict hessianFactors = HessianFactors(self->solution);
if (!hessianFactors) return NULL;
const ModelSpace * restrict space = SolutionModelSpace(self->solution);
const double * restrict squaredDifferences = SquaredDifferences(space);
if (!squaredDifferences) return NULL;
const StimulusSet * restrict stimulusSet = ModelSpaceStimulusSet(space);
const size_t pairCount = StimulusPairCount(stimulusSet);
if (!pairCount) return NULL;
const Model * restrict model = ModelSpaceModel(space);
const size_t dimensionCount = DimensionCount(model);
if (!dimensionCount) return NULL;
const size_t classCount = ClassCount(model);
if (classCount < 2) return NULL;
const size_t weightsSize = WeightsSize(model);
if (!weightsSize) return NULL;
const size_t hessianSize = SizeProduct(weightsSize, weightsSize);
double * restrict hessian = SafeCalloc(hessianSize, sizeof(double));
double * restrict accumulator = SafeMalloc(pairCount, sizeof(double));
for (size_t t = 0; t < classCount; t++) {
for (size_t r1 = 0; r1 < dimensionCount; r1++) {
for (size_t r2 = 0; r2 < dimensionCount; r2++) {
cblas_dcopy((int)pairCount,
squaredDifferences + r1,
(int)dimensionCount,
accumulator,
1);
cblas_dtbmv(CblasRowMajor,
CblasUpper,
CblasNoTrans,
CblasNonUnit,
(int)pairCount,
0,
squaredDifferences + r2,
(int)dimensionCount,
accumulator,
1); // elementwise multiplication
const double h = (0.5
* cblas_ddot((int)pairCount,
(hessianFactors
+ (pairCount
* t)),
1,
accumulator,
1));
const size_t tr1 = classCount * r1 + t;
const size_t tr2 = classCount * r2 + t;
#ifdef WINSBERG_LEGACY
// Winsberg inadvertently doubles the values of
// coordinate-coordinate Hessian entries because
// she effectively uses += rather than =.
hessian[weightsSize * tr1 + tr2] += h;
hessian[weightsSize * tr2 + tr1] += h;
#else
hessian[weightsSize * tr1 + tr2] = h;
hessian[weightsSize * tr2 + tr1] = h;
#endif
}
}
}
FreeAndClear(accumulator);
return hessian;
}
/*
* Returns the expected Hessian of coordinate weights against specificity
* weights. The order of the indices for rows and columns corresponds to that
* of the coordinate gradient in row-major order. The matrix is unpacked. Note
* that Hessian factors must be initialised before calling this function.
*/
static double *
NewExpectedCoordinateSpecificityHessian(const WeightSolver * restrict self)
{
const double * restrict hessianFactors = HessianFactors(self->solution);
if (!hessianFactors) return NULL;
const ModelSpace * restrict space = SolutionModelSpace(self->solution);
const double * restrict squaredDifferences = SquaredDifferences(space);
if (!squaredDifferences) return NULL;
const StimulusSet * restrict stimulusSet = ModelSpaceStimulusSet(space);
const size_t pairCount = StimulusPairCount(stimulusSet);
if (!pairCount) return NULL;
const Model * restrict model = ModelSpaceModel(space);
const size_t dimensionCount = DimensionCount(model);
if (!dimensionCount) return NULL;
const size_t classCount = ClassCount(model);
if (classCount < 2) return NULL;
const size_t weightsSize = WeightsSize(model);
if (!weightsSize) return NULL;
const SpecificityType specificityType = ModelSpecificityType(model);
if (specificityType != GlobalSpecificities) return NULL;
const double * restrict pairwiseSpecificities;
pairwiseSpecificities = PairwiseSpecificities(space);
if (!pairwiseSpecificities) return NULL;
const size_t hessianSize = SizeProduct(weightsSize, classCount);
double * restrict hessian = SafeCalloc(hessianSize, sizeof(double));
double * restrict accumulator = SafeMalloc(pairCount, sizeof(double));
for (size_t t = 0; t < classCount; t++) {
for (size_t r = 0; r < dimensionCount; r++) {
cblas_dcopy((int)pairCount,
squaredDifferences + r,
(int)dimensionCount,
accumulator,
1);
cblas_dtbmv(CblasRowMajor,
CblasUpper,
CblasNoTrans,
CblasNonUnit,
(int)pairCount,
0,
pairwiseSpecificities,
1,
accumulator,
1); // trick for elementwise multiplication
const double h = 0.5 * cblas_ddot((int)pairCount,
(hessianFactors
+ pairCount * t),
1,
accumulator,
1);
const size_t tr = classCount * r + t;
hessian[classCount * tr + t] = h;
}
}
FreeAndClear(accumulator);
return hessian;
}
/*
* Returns the expected Hessian of coordinate weights against specificity
* weights. The order of the indices for rows and columns corresponds to that
* of the coordinate gradient in row-major order. The matrix is unpacked. Note
* that Hessian factors must be initialised before calling this function.
*/
static double *
NewExpectedSpecificityHessian(const WeightSolver * restrict self)
{
const double * restrict hessianFactors = HessianFactors(self->solution);
if (!hessianFactors) return NULL;
const ModelSpace * restrict space = SolutionModelSpace(self->solution);
const StimulusSet * restrict stimulusSet = ModelSpaceStimulusSet(space);
const size_t pairCount = StimulusPairCount(stimulusSet);
if (!pairCount) return NULL;
const Model * restrict model = ModelSpaceModel(space);
const size_t classCount = ClassCount(model);
if (classCount < 2) return NULL;
const SpecificityType specificityType = ModelSpecificityType(model);
if (specificityType != GlobalSpecificities) return NULL;
const double * restrict pairwiseSpecificities;
pairwiseSpecificities = PairwiseSpecificities(space);
if (!pairwiseSpecificities) return NULL;
const size_t hessianSize = SizeProduct(classCount, classCount);
double * restrict hessian = SafeCalloc(hessianSize, sizeof(double));
double * restrict squaredPairwiseSpecificities;
squaredPairwiseSpecificities = SafeCalloc(pairCount, sizeof(double));
double * restrict accumulator = SafeCalloc(classCount, sizeof(double));
cblas_dcopy((int)pairCount,
pairwiseSpecificities,
1,
squaredPairwiseSpecificities,
1);
cblas_dtbmv(CblasRowMajor,
CblasUpper,
CblasNoTrans,
CblasNonUnit,
(int)pairCount,
0,
pairwiseSpecificities,
1,
squaredPairwiseSpecificities,
1); // trick for elementwise multiplication
cblas_dgemv(CblasRowMajor,
CblasNoTrans,
(int)classCount,
(int)pairCount,
0.5,
hessianFactors,
(int)pairCount,
squaredPairwiseSpecificities,
1,
0.0,
accumulator,
1);
for (size_t t = 0; t < classCount; t++)
hessian[classCount * t + t] = accumulator[t];
FreeAndClear(accumulator);
FreeAndClear(squaredPairwiseSpecificities);
return hessian;
}
static WeightSolver * NewWeightSolver(Solution * restrict solution)
{
if (!solution) return NULL;
const ModelSpace * restrict space = SolutionModelSpace(solution);
const Model * restrict model = ModelSpaceModel(space);
const size_t classCount = ClassCount(model);
if (classCount < 2) return NULL; // weights are all 1 for a single class
const size_t gradientSize = ExtendedWeightsSize(model);
size_t hessianSize = SizeProduct(gradientSize, gradientSize);
if (!IsConvertibleToInt(hessianSize))
ExitWithError("Model too large to solve for weights");
WeightSolver * restrict self;
if ((self = malloc(sizeof(WeightSolver)))) {
self->solution = solution;
self->gradientSize = gradientSize;
self->gradient = NewGradient(self);
self->hessianSize = hessianSize;
self->hessian = NULL;
}
return self;
}
static void DeleteWeightSolver(WeightSolver * restrict self)
{
if (self) {
FreeAndClear(self->hessian);
FreeAndClear(self->gradient);
FreeAndClear(self);
}
}
/*
* Returns the full expected Hessian, with indices corresponding to the
* full gradient. The matrix is unpacked.
*/
static double * ExpectedHessian(WeightSolver * restrict self)
{
if (self->hessian) return self->hessian;
const ModelSpace * restrict space = SolutionModelSpace(self->solution);
const Model * restrict model = ModelSpaceModel(space);
const size_t classCount = ClassCount(model);
if (classCount < 2) return NULL;
const size_t weightsSize = WeightsSize(model);
if (!weightsSize) return NULL;
double * restrict coordinateHessian;
coordinateHessian = NewExpectedCoordinateHessian(self);
if (!coordinateHessian) return NULL;
double * restrict jointHessian;
jointHessian = NewExpectedCoordinateSpecificityHessian(self);
if (!jointHessian) return self->hessian = coordinateHessian;
double * restrict specificityHessian;
specificityHessian = NewExpectedSpecificityHessian(self);
if (!specificityHessian) {
FreeAndClear(jointHessian);
return self->hessian = coordinateHessian;
}
self->hessian = SafeMalloc(self->hessianSize, sizeof(double));
for (size_t x = 0; x < weightsSize; x++) {
cblas_dcopy((int)weightsSize,
coordinateHessian + weightsSize * x,
1,
self->hessian + self->gradientSize * x,
1);
cblas_dcopy((int)classCount,
jointHessian + classCount * x,
1,
(self->hessian
+ self->gradientSize * x
+ weightsSize),
1);
}
for (size_t s = 0; s < classCount; s++) {
cblas_dcopy((int)weightsSize,
jointHessian + s,
(int)classCount,
(self->hessian
+ self->gradientSize * (weightsSize + s)),
1);
cblas_dcopy((int)classCount,
specificityHessian + classCount * s,
1,
(self->hessian
+ self->gradientSize * (weightsSize + s)
+ weightsSize),
1);
}
FreeAndClear(specificityHessian);
FreeAndClear(jointHessian);
FreeAndClear(coordinateHessian);
return self->hessian;
}
/**
* Returns true if the gradient is negative for a frozen weight.
*/
static bool FrozenGradientIsNegative(const WeightSolver * restrict self,
Solution * restrict solution0)
{
if (!self) return false;
const Parameters * restrict parameters;
parameters = SolutionParameters(self->solution);
const ModelSpace * restrict space = SolutionModelSpace(solution0);
const double * restrict weights = Weights(space);
for (size_t w = 0; w < self->gradientSize; w++)
if (isless(weights[w],
parameters->minWeightValue + parameters->margin)
#ifdef WINSBERG_LEGACY
&& isless(self->gradient[w], -1e-3))
#else
&& isless(self->gradient[w], 0.0))
#endif
return true;
return false;
}
/**
* Returns the search direction for a line search for coordinates: in this case,
* the (Moore-Penrose pseudo)-inverse Hessian times the gradient.
*/
static double * NewSearchDirection(WeightSolver * restrict self,
Solution * restrict solution0)
{
if (!self || !self->gradient) return NULL;
double * hessian = ExpectedHessian(self);
if (!hessian) return NULL;
const Parameters * restrict params = SolutionParameters(solution0);
const ModelSpace * restrict space = SolutionModelSpace(solution0);
const double * restrict weights = Weights(space);
size_t reducedGradientSize = self->gradientSize;
// The following allocation is possibly too large, but it allows the for
// loop following to fill the permutation at the same time as it
// determines the reduced size.
size_t * restrict permutation = SafeCalloc(self->gradientSize,
sizeof(size_t));
size_t nextIndex = 0;
double minGradValue = 0.0;
size_t minGradIndex = self->gradientSize;
for (size_t w = 0; w < self->gradientSize; w++) {
if (isless(weights[w],
params->minWeightValue + params->margin)) {
reducedGradientSize--;
if (isless(self->gradient[w], minGradValue)) {
minGradIndex = w;
minGradValue = self->gradient[minGradIndex];
}
}
else permutation[nextIndex++] = w;
}
if (isless(minGradValue, 0.0)
// Check the gradient size just in case something went wrong.
&& reducedGradientSize < self->gradientSize) {
permutation[nextIndex] = minGradIndex;
reducedGradientSize++;
}
const size_t reducedHessianSize = SizeProduct(reducedGradientSize,
reducedGradientSize);
double * restrict invReducedGradient;
invReducedGradient = SafeMalloc(reducedGradientSize, sizeof(double));
double * restrict reducedHessian;
reducedHessian = SafeMalloc(reducedHessianSize, sizeof(double));
if (reducedGradientSize == self->gradientSize) {
cblas_dcopy((int)reducedGradientSize,
self->gradient,
1,
invReducedGradient,
1);
cblas_dscal((int)reducedGradientSize,
-1.0,
invReducedGradient,
1);
cblas_dcopy((int)reducedHessianSize,
hessian,
1,
reducedHessian,
1);
} else {
for (size_t a = 0; a < reducedGradientSize; a++) {
invReducedGradient[a] = -self->gradient[permutation[a]];
for (size_t b = 0; b < reducedGradientSize; b++)
reducedHessian[reducedGradientSize * a + b]
= hessian[self->gradientSize * permutation[a]
+ permutation[b]];
}
}
// LAPACK is all call-by-reference.
LPInteger m = (LPInteger)reducedGradientSize;
LPInteger n = (LPInteger)reducedGradientSize;
LPInteger nrhs = 1;
LPInteger lda = (LPInteger)reducedGradientSize;
LPInteger ldb = (LPInteger)reducedGradientSize;
double * s = SafeMalloc(reducedGradientSize, sizeof(double));
double rcond = params->invConditionNumber;
LPInteger rank;
double * work = SafeMalloc(1, sizeof(double));
LPInteger lwork = -1;
LPInteger info = 0;
// This call determines work size only.
// DGELSD refuses to compute LIWORK on OS X, so we use DGELSS.
dgelss_(&m,
&n,
&nrhs,
reducedHessian,
&lda,
invReducedGradient,
&ldb,
s,
&rcond,
&rank,
work,
&lwork,
&info);
lwork = (LPInteger)lround(work[0]);
FreeAndClear(work);
work = SafeMalloc((size_t)lwork, sizeof(double));
// This call is the real thing.
dgelss_(&m,
&n,
&nrhs,
reducedHessian,
&lda,
invReducedGradient,
&ldb,
s,
&rcond,
&rank,
work,
&lwork,
&info);
FreeAndClear(work);
FreeAndClear(s);
double * restrict searchDirection;
searchDirection = SafeCalloc(self->gradientSize, sizeof(double));
for (size_t a = 0; a < reducedGradientSize; a++)
// LAPACK has overwritten invReducedGradient with the solution.
searchDirection[permutation[a]] = invReducedGradient[a];
FreeAndClear(reducedHessian);
FreeAndClear(invReducedGradient);
FreeAndClear(permutation);
return searchDirection;
}
// This routine performs the line search. It follows Winsberg directly.
// N.B.: It may return the same solution as the one passed in.
// WARNING: This is a simple line search that relies on starting with a
// negative slope. With an insufficient number of iterations, it can
// return a solution with positive slope.
#ifdef WINSBERG_LEGACY
static Solution * NewLineSearchSolution(WeightSolver * restrict self,
Solution * restrict solution0,
double SSR)
#else
static Solution * NewLineSearchSolution(WeightSolver * restrict self,
Solution * restrict solution0)
#endif
{
if (!solution0) return NULL;
const Parameters * restrict parameters = SolutionParameters(solution0);
fprintf(parameters->logFile,
" \n"
" Line search\n");
// We alias solution = solution0 early to faciliate the goto escape.
Solution * restrict solution = solution0;
double * restrict searchDirection0;
searchDirection0 = NewSearchDirection(self, solution0);
if (!searchDirection0) return NULL;
const double norm = cblas_dnrm2((int)self->gradientSize,
searchDirection0,
1);
cblas_dscal((int)self->gradientSize,
1.0 / norm,
searchDirection0,
1);
const double slope = cblas_ddot((int)self->gradientSize,
self->gradient,
1,
searchDirection0,
1);
if (isgreaterequal(slope, 0.0))
// ExitWithError("Optimisation slope is non-negative");
goto escape;
#ifdef WINSBERG_LEGACY
// Winsberg curiously only normalises the direction for coordinates
// but still uses the normalised slope for computing search directions.
// The following code is just so that we match.
cblas_dscal((int)self->gradientSize, norm, searchDirection0, 1);
#else
const double SSR = SumOfSquaredModelError(self->solution);
#endif
if (parameters->verbosity >= VERY_VERY_VERY_VERBOSE)
fprintf(parameters->logFile,
" \n"
" Squared model error after"
" iteration 0: %e\n",
SSR);
double stepSizes[5] = {0.0, 0.0, 0.0, NAN, NAN};
double errors[5] = {SSR, SSR, SSR, NAN, NAN};
double slopes[5] = {slope, slope, slope, NAN, NAN};
stepSizes[4] = parameters->firstStepSize;
const ModelSpace * restrict space0 = SolutionModelSpace(solution0);
const double * restrict weights = Weights(space0);
double maxStepSize = INFINITY;
for (size_t w = 0; w < self->gradientSize; w++) {
if (isgreaterequal(searchDirection0[w], 0.0)) continue;
const double thisMax = ((parameters->minWeightValue
- weights[w])
/ searchDirection0[w]);
maxStepSize = fmin(maxStepSize, thisMax);
}
if (islessequal(maxStepSize, 0.0)) goto escape;
stepSizes[4] = fmin(stepSizes[4], maxStepSize);
// Solution * restrict solution = solution0;
WeightSolver * restrict solver = NULL;
double * restrict nextWeights = SafeMalloc(self->gradientSize,
sizeof(double));
for (size_t i = 0; i < parameters->maxSearchIterationCount; i++) {
cblas_dcopy((int)self->gradientSize,
weights,
1,
nextWeights,
1);
cblas_daxpy((int)self->gradientSize,
stepSizes[4],
searchDirection0,
1,
nextWeights,
1);
Solution * restrict newSolution;
newSolution = NewSolutionByUpdatingWeights(solution0,
nextWeights);
if (solution != solution0)
DeleteSolutionPreservingClassAssignment(solution);
solution = newSolution;
errors[4] = SumOfSquaredModelError(solution);
if (parameters->verbosity >= VERY_VERY_VERY_VERBOSE)
fprintf(parameters->logFile,
" Squared model error"
" after iteration %zu: %e\n",
SizeSum(i, 1),
errors[4]);
DeleteWeightSolver(solver);
solver = NewWeightSolver(solution);
if (!solver)
ExitWithError("Unexpected NULL during optimisation");
slopes[4] = cblas_ddot((int)solver->gradientSize,
solver->gradient,
1,
searchDirection0,
1);
const bool hasWolfeCurvature = isless(fabs(slopes[4]),
(parameters
->slopeImprovementFactor)
* fabs(slopes[0]));
const bool errorIsWorse = isgreater(errors[4], errors[0]);
const bool slopeIsPositive = isgreater(slopes[4], 0.0);
if (errorIsWorse && (hasWolfeCurvature || !slopeIsPositive)) {
// Backtracking
stepSizes[4] *= parameters->stepDecreaseFactor;
stepSizes[2] = stepSizes[1] = stepSizes[0];
errors[2] = errors[1] = errors[0];
slopes[2] = slopes[1] = slopes[0];
} else {
if (hasWolfeCurvature) break;
if (!slopeIsPositive) { // Non-neg slope & better error
stepSizes[4] *= parameters->stepIncreaseFactor;
} else { // Positive slope regardless of error.
stepSizes[3] = stepSizes[4];
errors[3] = errors[4];
slopes[3] = slopes[4];
double z = (((3.0
/ (stepSizes[4] - stepSizes[2]))
* (errors[2] - errors[4]))
+ slopes[2]
+ slopes[4]);
double w1 = z * z - slopes[2] * slopes[4];
if (isless(fabs(slopes[2]
+ slopes[4]
+ 2.0 * z),
1e-5)
|| isless(w1, 0.0)) {
stepSizes[4] = (stepSizes[2]
- (slopes[2]
* ((stepSizes[4]
- stepSizes[2])
/ (slopes[4]
- slopes[2])))
);
} else {
w1 = sqrt(w1);
stepSizes[4] = (stepSizes[2]
+ ((1.0
- ((slopes[4]
+ w1
- z)
/ (slopes[4]
- slopes[2]
+ 2.0 * w1)))
* (stepSizes[4]
- stepSizes[2])));
}
}
}
if (islessequal(stepSizes[4], 0.0)) break; // convergence
if (stepSizes[4]
== maxStepSize * parameters->stepIncreaseFactor)
break;
stepSizes[4] = fmin(stepSizes[4], maxStepSize);
}
if (parameters->verbosity >= VERY_VERY_VERY_VERBOSE)
fprintf(parameters->logFile, "\n");
FreeAndClear(nextWeights);
DeleteWeightSolver(solver);
escape:
FreeAndClear(searchDirection0);
return solution;
}
Solution * NewWeightSolution(Solution * restrict solution0)
{
if (!solution0) return NULL;
const ModelSpace * restrict space = SolutionModelSpace(solution0);
const Model * restrict model = ModelSpaceModel(space);
const size_t classCount = ClassCount(model);
if (classCount < 2) return solution0;
const Parameters * restrict params = SolutionParameters(solution0);
WeightSolver * restrict solver = NewWeightSolver(solution0);
Solution * restrict soln = solution0;
double SSR = SumOfSquaredModelError(soln);
if (params->verbosity >= VERY_VERY_VERBOSE)
fprintf(params->logFile,
" \n"
" Optimisation of weights\n"
" \n"
" Squared model error after iteration"
" 0: %e\n",
SSR);
#ifdef WINSBERG_LEGACY
WeightSolver * restrict solver0 = solver;
size_t iccount = 0;
#endif
for (size_t i = 0; i < params->maxWeightIterationCount; i++) {
#ifdef WINSBERG_LEGACY
Winsberg:
if (++iccount >= 5) break;
#endif
Solution * restrict newSolution;
#ifdef WINSBERG_LEGACY
// For the first iteration only (and repeats of it due to goto),
// the solver is not overwritten by solver0 in Winsberg's code.
if (i) {
newSolution = NewLineSearchSolution(solver0, soln, SSR);
} else {
newSolution = NewLineSearchSolution(solver, soln, SSR);
}
#else
newSolution = NewLineSearchSolution(solver, soln);
#endif
if (newSolution == soln) break; // convergence
double newSSR = SumOfSquaredModelError(newSolution);
if (params->verbosity >= VERY_VERY_VERBOSE)
fprintf(params->logFile,
" Squared model error after"
" iteration %zu: %e\n",
SizeSum(i, 1),
newSSR);
double relativeImprovement = (SSR - newSSR) / SSR;
if (isgreater(relativeImprovement, 0.0)) {
if (soln != solution0)
DeleteSolutionPreservingClassAssignment(soln);
soln = newSolution;
#ifdef WINSBERG_LEGACY
if (solver != solver0) DeleteWeightSolver(solver);
#else
DeleteWeightSolver(solver);
#endif
solver = NewWeightSolver(soln);
if (isless(relativeImprovement,
params->weightImprovementFactor)) {
bool isAtBound;
isAtBound = FrozenGradientIsNegative(solver,
soln);
#ifdef WINSBERG_LEGACY
if (isAtBound) goto Winsberg; else break;
#else
if (!isAtBound) break;
#endif
}
} else {
// We would have hit break if newSln == solution0
DeleteSolutionPreservingClassAssignment(newSolution);
break;
}
#ifndef WINSBERG_LEGACY
SSR = newSSR;
#endif
}
if (params->verbosity >= VERY_VERY_VERBOSE)
fprintf(params->logFile, "\n");
#ifdef WINSBERG_LEGACY
if (solver0 != solver) DeleteWeightSolver(solver0);
#endif
DeleteWeightSolver(solver);
Solution * finalSolution = NewSolutionByNormalisingWeights(soln);
if (soln != solution0)
DeleteSolutionPreservingClassAssignment(soln);
return finalSolution;
}