-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswi_lbfgs.c
868 lines (744 loc) · 23.9 KB
/
swi_lbfgs.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
#include <string.h>
#include <SWI-Prolog.h>
#include <lbfgs.h>
#include <stdio.h>
/*
This file is part of YAP-LBFGS.
Copyright (C) 2009 Bernd Gutmann
YAP-LBFGS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
YAP-LBFGS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with YAP-LBFGS. If not, see <http://www.gnu.org/licenses/>.
*/
#define RETURN_IF_FAIL if (ret!=TRUE) return ret;
// These constants describe the internal state
#define OPTIMIZER_STATUS_NONE 0
#define OPTIMIZER_STATUS_INITIALIZED 1
#define OPTIMIZER_STATUS_RUNNING 2
#define OPTIMIZER_STATUS_CB_EVAL 3
#define OPTIMIZER_STATUS_CB_PROGRESS 4
install_t init_lbfgs_predicates( void ) ;
typedef struct
{
int optimizer_status; // the internal state
int n; // the size of the parameter vector
lbfgsfloatval_t *x; // pointer to the parameter vector x[0],...,x[n-1]
lbfgsfloatval_t *g; // pointer to the gradient vector g[0],...,g[n-1]
lbfgs_parameter_t param; // the parameters used for lbfgs
module_t module;
functor_t fcall3, fprogress8;
record_t extra_arg;
} environment;
static lbfgsfloatval_t evaluate(
void *instance,
const lbfgsfloatval_t *x,
lbfgsfloatval_t *g_tmp,
const int n,
const lbfgsfloatval_t step
)
{
environment *env=(environment*)instance;
term_t call=PL_new_term_ref();
term_t env_term=PL_new_term_ref();
term_t nterm=PL_new_term_ref();
term_t stepterm=PL_new_term_ref();
term_t var=PL_new_term_ref();
double floatres;
int result,intres,ret;
PL_put_variable(var);
ret=PL_put_integer(nterm,env->n);
RETURN_IF_FAIL
ret=PL_put_float(stepterm,step);
RETURN_IF_FAIL
term_t extra_arg_term=PL_new_term_ref();
PL_recorded(env->extra_arg,extra_arg_term);
ret=PL_put_pointer(env_term,(void *) env);
RETURN_IF_FAIL
ret=PL_cons_functor(call,env->fcall3,env_term,var,nterm,stepterm,extra_arg_term);
RETURN_IF_FAIL
env->g=g_tmp;
env->optimizer_status=OPTIMIZER_STATUS_CB_EVAL;
result=PL_call(call,env->module);
env->optimizer_status=OPTIMIZER_STATUS_RUNNING;
if (result==FALSE) {
printf("ERROR: Calling the evaluate call back function in YAP.\n");
// Goal did not succeed
return FALSE;
}
// call = YAP_GetFromSlot( s1 );
// a1 = YAP_ArgOfTerm(1,call);
if (PL_is_float(var)) {
// YAP_ShutdownGoal( TRUE );
ret=PL_get_float(var,&floatres);
RETURN_IF_FAIL
// printf("float %f\n",floatres);
return (lbfgsfloatval_t) floatres;
} else if (PL_is_integer(var)) {
// YAP_ShutdownGoal( TRUE );
// printf("int\n");
ret=PL_get_integer(var,&intres);
RETURN_IF_FAIL
return (lbfgsfloatval_t) intres;
}
fprintf(stderr, "ERROR: The evaluate call back function did not return a number as first argument.\n");
return 0;
}
static int progress(
void *instance,
const lbfgsfloatval_t *local_x,
const lbfgsfloatval_t *local_g,
const lbfgsfloatval_t fx,
const lbfgsfloatval_t xnorm,
const lbfgsfloatval_t gnorm,
const lbfgsfloatval_t step,
int n,
int k,
int ls
)
{
environment *env=(environment*)instance;
term_t call=PL_new_term_ref();
term_t env_term=PL_new_term_ref();
term_t fxterm=PL_new_term_ref();
term_t xnormterm=PL_new_term_ref();
term_t gnormterm=PL_new_term_ref();
term_t stepterm=PL_new_term_ref();
term_t nterm=PL_new_term_ref();
term_t kterm=PL_new_term_ref();
term_t lsterm=PL_new_term_ref();
term_t var=PL_new_term_ref();
int result,intres,ret;
ret=PL_put_float(fxterm,fx);
RETURN_IF_FAIL
ret=PL_put_float(xnormterm,xnorm);
RETURN_IF_FAIL
ret=PL_put_float(gnormterm,gnorm);
RETURN_IF_FAIL
ret=PL_put_float(stepterm,step);
RETURN_IF_FAIL
ret=PL_put_integer(nterm,n);
RETURN_IF_FAIL
ret=PL_put_integer(kterm,k);
RETURN_IF_FAIL
ret=PL_put_integer(lsterm,ls);
RETURN_IF_FAIL
ret=PL_put_variable(var);
RETURN_IF_FAIL
ret=PL_put_pointer(env_term,(void *) env);
RETURN_IF_FAIL
term_t extra_arg_term=PL_new_term_ref();
PL_recorded(env->extra_arg,extra_arg_term);
// extra_arg=PL_new_term_ref();
// PL_put_term_from_chars(extra_arg,REP_UTF8,(size_t)-1,"plunit_bongard");
ret=PL_cons_functor(call,env->fprogress8,env_term,fxterm,xnormterm,gnormterm,stepterm,
nterm,kterm,lsterm,var,extra_arg_term);
RETURN_IF_FAIL
env->optimizer_status=OPTIMIZER_STATUS_CB_PROGRESS;
result=PL_call(call,env->module);
env->optimizer_status=OPTIMIZER_STATUS_RUNNING;
if (result==FALSE) {
printf("ERROR: Calling the progress call back function in YAP.\n");
// Goal did not succeed
return FALSE;
}
if (PL_is_integer(var))
{
ret=PL_get_integer(var,&intres);
RETURN_IF_FAIL
return intres;
}
fprintf(stderr, "ERROR: The progress call back function did not return an integer as last argument\n");
return 1;
}
static foreign_t set_x_value(term_t t1,term_t t2, term_t t3) {
int i=0,xiint;
double xi;
int ret;
environment *env;
ret=PL_get_pointer(t1,(void **)&env);
RETURN_IF_FAIL
if (env->optimizer_status!=OPTIMIZER_STATUS_INITIALIZED) {
printf("ERROR: set_x_value/2 can be called only when the optimizer is initialized and not running.\n");
PL_fail;
}
if (PL_is_integer(t2)) {
ret=PL_get_integer(t2,&i);
RETURN_IF_FAIL
} else {
PL_succeed;
}
if (i<0 || i>=env->n) {
printf("ERROR: invalid index for set_x_value/2.\n");
PL_fail;
}
if (PL_is_float(t3)) {
ret=PL_get_float(t3,&xi);
RETURN_IF_FAIL
env->x[i]=(lbfgsfloatval_t) xi;
} else if (PL_is_integer(t3)) {
ret=PL_get_integer(t3,&xiint);
RETURN_IF_FAIL
env->x[i]=(lbfgsfloatval_t) xiint;
} else {
PL_fail;
}
PL_succeed;
}
static foreign_t get_x_value(term_t t1,term_t t2, term_t t3) {
int i=0;
term_t xi=PL_new_term_ref();
int ret;
environment *env;
ret=PL_get_pointer(t1,(void **)&env);
RETURN_IF_FAIL
if (env->optimizer_status==OPTIMIZER_STATUS_NONE) {
printf("ERROR: set_x_value/2 can be called only when the optimizer is initialized.\n");
PL_fail;
}
if (PL_is_integer(t2)) {
ret=PL_get_integer(t2,&i);
RETURN_IF_FAIL
} else {
PL_fail;
}
if (i<0 || i>=env->n) {
printf("ERROR: invalid index for set_x_value/2.\n");
PL_fail;
}
ret=PL_put_float(xi,env->x[i]);
RETURN_IF_FAIL
return PL_unify(t3,xi);
}
static foreign_t set_g_value(term_t t1,term_t t2, term_t t3) {
int i=0,giint;
double gi;
int ret;
environment *env;
ret=PL_get_pointer(t1,(void **)&env);
RETURN_IF_FAIL
if (env->optimizer_status != OPTIMIZER_STATUS_CB_EVAL) {
printf("ERROR: optimizer_set_g/2 can only be called by the evaluation call back function.\n");
return FALSE;
}
if (PL_is_integer(t2)) {
ret=PL_get_integer(t2,&i);
RETURN_IF_FAIL
} else {
PL_fail;
}
if (i<0 || i>=env->n) {
PL_fail;
}
if (PL_is_float(t3)) {
ret=PL_get_float(t3,&gi);
RETURN_IF_FAIL
env->g[i]=(lbfgsfloatval_t) gi;
} else if (PL_is_integer(t3)) {
ret=PL_get_integer(t3,&giint);
RETURN_IF_FAIL
env->g[i]=(lbfgsfloatval_t) giint;
} else {
PL_fail;
}
PL_succeed;
}
static foreign_t get_g_value(term_t t1,term_t t2, term_t t3) {
int i=0;
term_t gi=PL_new_term_ref();
int ret;
environment *env;
ret=PL_get_pointer(t1,(void **)&env);
RETURN_IF_FAIL
if (env->optimizer_status != OPTIMIZER_STATUS_RUNNING && env->optimizer_status != OPTIMIZER_STATUS_CB_EVAL && env->optimizer_status != OPTIMIZER_STATUS_CB_PROGRESS) {
printf("ERROR: optimizer_get_g/2 can only be called while the optimizer is running.\n");
PL_fail;
}
if (PL_is_integer(t2)) {
ret=PL_get_integer(t2,&i);
RETURN_IF_FAIL
} else {
PL_fail;
}
if (i<0 || i>=env->n) {
PL_fail;
}
ret=PL_put_float(gi,env->g[i]);
RETURN_IF_FAIL
return PL_unify(t3,gi);
}
static foreign_t optimizer_initialize(term_t t1, term_t t2, term_t t3, term_t t4, term_t t5, term_t t6) {
int temp_n=0;
//printf("LBFGSERR_ROUNDING_ERROR %d\n",LBFGSERR_ROUNDING_ERROR);
term_t env_t;
int ret;
if (! PL_is_integer(t1)) {
PL_fail;
}
ret=PL_get_integer(t1,&temp_n);
RETURN_IF_FAIL
if (temp_n<1) {
PL_fail;
}
environment * env;
env=(environment *)malloc(sizeof(environment));
env_t=PL_new_term_ref();
env->x = lbfgs_malloc(temp_n);
//Initialize the parameters for the L-BFGS optimization.
lbfgs_parameter_init(&(env->param));
if (env->x == NULL) {
printf("ERROR: Failed to allocate a memory block for variables.\n");
PL_fail;
}
ret=PL_get_module(t2, &env->module);
RETURN_IF_FAIL
env->n=temp_n;
atom_t fcall3atom;
atom_t fprogress8atom;
ret=PL_get_atom(t3, &fcall3atom);
RETURN_IF_FAIL
ret=PL_get_atom(t4, &fprogress8atom);
RETURN_IF_FAIL
env->fcall3=PL_new_functor(fcall3atom,5);
env->fprogress8=PL_new_functor(fprogress8atom,10);
env->extra_arg=PL_record(t5);
env->optimizer_status=OPTIMIZER_STATUS_INITIALIZED;
ret=PL_put_pointer(env_t,(void *) env);
RETURN_IF_FAIL
return(PL_unify(env_t,t6));
}
static foreign_t optimizer_run(term_t t1,term_t t2, term_t t3) {
int ret = 0;
term_t s1=PL_new_term_ref();
term_t s2=PL_new_term_ref();
lbfgsfloatval_t fx;
lbfgsfloatval_t * tmp_x;
environment *env;
char * status;
ret=PL_get_pointer(t1,(void **)&env);
RETURN_IF_FAIL
tmp_x=env->x;
if (env->optimizer_status == OPTIMIZER_STATUS_NONE) {
printf("ERROR: Memory for parameter vector not initialized, please call optimizer_initialize/1 first.\n");
return FALSE;
}
if (env->optimizer_status != OPTIMIZER_STATUS_INITIALIZED) {
printf("ERROR: Optimizer is running right now. Please wait till it is finished.\n");
return FALSE;
}
// both arguments have to be variables
if (! PL_is_variable(t2) || ! PL_is_variable(t3)) {
return FALSE;
}
env->optimizer_status = OPTIMIZER_STATUS_RUNNING;
ret = lbfgs(env->n, env->x, &fx, evaluate, progress, (void *)env, &(env->param));
env->x=tmp_x;
env->optimizer_status = OPTIMIZER_STATUS_INITIALIZED;
switch(ret)
{
/** L-BFGS reaches convergence. */
case LBFGS_SUCCESS:
status="LBFGS_SUCCESS"; break;
case LBFGS_STOP:
status= "LBFGS_STOP"; break;
/** The initial variables already minimize the objective function. */
case LBFGS_ALREADY_MINIMIZED:
status= "LBFGS_ALREADY_MINIMIZED"; break;
/** Unknown error. */
case LBFGSERR_UNKNOWNERROR:
status= "LBFGSERR_UNKNOWNERROR"; break;
/** Logic error. */
case LBFGSERR_LOGICERROR:
status= "LBFGSERR_LOGICERROR"; break;
/** Insufficient memory. */
case LBFGSERR_OUTOFMEMORY:
status= "LBFGSERR_OUTOFMEMORY"; break;
/** The minimization process has been canceled. */
case LBFGSERR_CANCELED:
status= "LBFGSERR_CANCELED"; break;
/** Invalid number of variables specified. */
case LBFGSERR_INVALID_N:
status= "LBFGSERR_INVALID_N"; break;
/** Invalid number of variables (for SSE) specified. */
case LBFGSERR_INVALID_N_SSE:
status= "LBFGSERR_INVALID_N_SSE"; break;
/** The array x must be aligned to 16 (for SSE). */
case LBFGSERR_INVALID_X_SSE:
status= "LBFGSERR_INVALID_X_SSE"; break;
/** Invalid parameter lbfgs_parameter_t::epsilon specified. */
case LBFGSERR_INVALID_EPSILON:
status= "LBFGSERR_INVALID_EPSILON"; break;
/** Invalid parameter lbfgs_parameter_t::past specified. */
case LBFGSERR_INVALID_TESTPERIOD:
status= "LBFGSERR_INVALID_TESTPERIOD"; break;
/** Invalid parameter lbfgs_parameter_t::delta specified. */
case LBFGSERR_INVALID_DELTA:
status= "LBFGSERR_INVALID_DELTA"; break;
/** Invalid parameter lbfgs_parameter_t::linesearch specified. */
case LBFGSERR_INVALID_LINESEARCH:
status= "LBFGSERR_INVALID_LINESEARCH"; break;
/** Invalid parameter lbfgs_parameter_t::max_step specified. */
case LBFGSERR_INVALID_MINSTEP:
status= "LBFGSERR_INVALID_MINSTEP"; break;
/** Invalid parameter lbfgs_parameter_t::max_step specified. */
case LBFGSERR_INVALID_MAXSTEP:
status= "LBFGSERR_INVALID_MAXSTEP"; break;
/** Invalid parameter lbfgs_parameter_t::ftol specified. */
case LBFGSERR_INVALID_FTOL:
status= "LBFGSERR_INVALID_FTOL"; break;
/** Invalid parameter lbfgs_parameter_t::wolfe specified. */
case LBFGSERR_INVALID_WOLFE:
status= "LBFGSERR_INVALID_WOLFE"; break;
/** Invalid parameter lbfgs_parameter_t::gtol specified. */
case LBFGSERR_INVALID_GTOL:
status= "LBFGSERR_INVALID_GTOL"; break;
/** Invalid parameter lbfgs_parameter_t::xtol specified. */
case LBFGSERR_INVALID_XTOL:
status= "LBFGSERR_INVALID_XTOL"; break;
/** Invalid parameter lbfgs_parameter_t::max_linesearch specified. */
case LBFGSERR_INVALID_MAXLINESEARCH:
status= "LBFGSERR_INVALID_MAXLINESEARCH"; break;
/** Invalid parameter lbfgs_parameter_t::orthantwise_c specified. */
case LBFGSERR_INVALID_ORTHANTWISE:
status= "LBFGSERR_INVALID_ORTHANTWISE"; break;
/** Invalid parameter lbfgs_parameter_t::orthantwise_start specified. */
case LBFGSERR_INVALID_ORTHANTWISE_START:
status= "LBFGSERR_INVALID_ORTHANTWISE_START"; break;
/** Invalid parameter lbfgs_parameter_t::orthantwise_end specified. */
case LBFGSERR_INVALID_ORTHANTWISE_END:
status= "LBFGSERR_INVALID_ORTHANTWISE_END"; break;
/** The line-search step went out of the interval of uncertainty. */
case LBFGSERR_OUTOFINTERVAL:
status= "LBFGSERR_OUTOFINTERVAL"; break;
/** A logic error occurred; alternatively, the interval of uncertainty
became too small. */
case LBFGSERR_INCORRECT_TMINMAX:
status= "LBFGSERR_INCORRECT_TMINMAX"; break;
/** A rounding error occurred; alternatively, no line-search step
satisfies the sufficient decrease and curvature conditions. */
case LBFGSERR_ROUNDING_ERROR:
status= "LBFGSERR_ROUNDING_ERROR"; break;
/** The line-search step became smaller than lbfgs_parameter_t::min_step. */
case LBFGSERR_MINIMUMSTEP:
status= "LBFGSERR_MINIMUMSTEP"; break;
/** The line-search step became larger than lbfgs_parameter_t::max_step. */
case LBFGSERR_MAXIMUMSTEP:
status= "LBFGSERR_MAXIMUMSTEP"; break;
/** The line-search routine reaches the maximum number of evaluations. */
case LBFGSERR_MAXIMUMLINESEARCH:
status= "LBFGSERR_MAXIMUMLINESEARCH"; break;
/** The algorithm routine reaches the maximum number of iterations. */
case LBFGSERR_MAXIMUMITERATION:
status= "LBFGSERR_MAXIMUMITERATION"; break;
/** Relative width of the interval of uncertainty is at most
lbfgs_parameter_t::xtol. */
case LBFGSERR_WIDTHTOOSMALL:
status= "LBFGSERR_WIDTHTOOSMALL"; break;
/** A logic error (negative line-search step) occurred. */
case LBFGSERR_INVALIDPARAMETERS:
status= "LBFGSERR_INVALIDPARAMETERS"; break;
/** The current search direction increases the objective function value. */
case LBFGSERR_INCREASEGRADIENT:
status= "LBFGSERR_INCREASEGRADIENT"; break;
default:
status= "UNKNOWN"; break;
}
ret=PL_put_float(s1,fx);
RETURN_IF_FAIL
ret=PL_put_atom_chars(s2,status);
RETURN_IF_FAIL
ret=PL_unify(t2,s1);
RETURN_IF_FAIL
ret=PL_unify(t3,s2);
RETURN_IF_FAIL
return TRUE;
}
static foreign_t optimizer_finalize( term_t t1 ) {
int ret;
environment *env;
ret=PL_get_pointer(t1,(void **)&env);
RETURN_IF_FAIL
if (env->optimizer_status == OPTIMIZER_STATUS_NONE) {
printf("Error: Optimizer is not initialized.\n");
return FALSE;
}
if (env->optimizer_status == OPTIMIZER_STATUS_INITIALIZED) {
lbfgs_free(env->x);
env->optimizer_status = OPTIMIZER_STATUS_NONE;
PL_erase(env->extra_arg);
free(env);
return TRUE;
}
printf("ERROR: Optimizer is running right now. Please wait till it is finished.\n");
return FALSE;
}
static foreign_t optimizer_set_parameter(term_t t1,term_t t2, term_t t3) {
char * name;
int ret;
environment *env;
ret=PL_get_pointer(t1,(void **)&env);
RETURN_IF_FAIL
if (env->optimizer_status != OPTIMIZER_STATUS_NONE && env->optimizer_status != OPTIMIZER_STATUS_INITIALIZED){
printf("ERROR: Optimizer is running right now. Please wait till it is finished.\n");
return FALSE;
}
if (! PL_is_atom(t2)) {
return FALSE;
}
ret=PL_get_atom_chars(t2,&name);
RETURN_IF_FAIL
if ((strcmp(name, "m") == 0)) {
if (! PL_is_integer(t3)) {
return FALSE;
}
ret=PL_get_integer(t3,&(env->param.m));
RETURN_IF_FAIL
} else if ((strcmp(name, "epsilon") == 0)) {
lbfgsfloatval_t v;
if (PL_is_float(t3)) {
ret=PL_get_float(t3,&v);
RETURN_IF_FAIL
} else if (PL_is_integer(t3)) {
int vi;
ret=PL_get_integer(t3,&vi);
RETURN_IF_FAIL
v=(lbfgsfloatval_t) vi;
} else {
return FALSE;
}
env->param.epsilon=v;
} else if ((strcmp(name, "past") == 0)) {
if (! PL_is_integer(t3)) {
return FALSE;
}
ret=PL_get_integer(t3,&(env->param.past));
RETURN_IF_FAIL
} else if ((strcmp(name, "delta") == 0)) {
lbfgsfloatval_t v;
if (PL_is_float(t3)) {
ret=PL_get_float(t3,&v);
RETURN_IF_FAIL
} else if (PL_is_integer(t3)) {
int vi;
ret=PL_get_integer(t3,&vi);
RETURN_IF_FAIL
v=(lbfgsfloatval_t) vi;
} else {
return FALSE;
}
env->param.delta=v;
} else if ((strcmp(name, "max_iterations") == 0)) {
if (! PL_is_integer(t3)) {
return FALSE;
}
ret=PL_get_integer(t3,&(env->param.max_iterations));
RETURN_IF_FAIL
} else if ((strcmp(name, "linesearch") == 0)) {
if (! PL_is_integer(t3)) {
return FALSE;
}
ret=PL_get_integer(t3,&(env->param.linesearch));
RETURN_IF_FAIL
} else if ((strcmp(name, "max_linesearch") == 0)) {
if (! PL_is_integer(t3)) {
return FALSE;
}
ret=PL_get_integer(t3,&(env->param.max_linesearch));
RETURN_IF_FAIL
} else if ((strcmp(name, "min_step") == 0)) {
lbfgsfloatval_t v;
if (PL_is_float(t3)) {
ret=PL_get_float(t3,&v);
RETURN_IF_FAIL
} else if (PL_is_integer(t3)) {
int vi;
ret=PL_get_integer(t3,&vi);
RETURN_IF_FAIL
v=(lbfgsfloatval_t) vi;
} else {
return FALSE;
}
env->param.min_step=v;
} else if ((strcmp(name, "max_step") == 0)) {
lbfgsfloatval_t v;
if (PL_is_float(t3)) {
ret=PL_get_float(t3,&v);
RETURN_IF_FAIL
} else if (PL_is_integer(t3)) {
int vi;
ret=PL_get_integer(t3,&vi);
RETURN_IF_FAIL
v=(lbfgsfloatval_t) vi;
} else {
return FALSE;
}
env->param.max_step=v;
} else if ((strcmp(name, "ftol") == 0)) {
lbfgsfloatval_t v;
if (PL_is_float(t3)) {
ret=PL_get_float(t3,&v);
RETURN_IF_FAIL
} else if (PL_is_integer(t3)) {
int vi;
ret=PL_get_integer(t3,&vi);
RETURN_IF_FAIL
v=(lbfgsfloatval_t) vi;
} else {
return FALSE;
}
env->param.ftol=v;
} else if ((strcmp(name, "gtol") == 0)) {
lbfgsfloatval_t v;
if (PL_is_float(t3)) {
ret=PL_get_float(t3,&v);
RETURN_IF_FAIL
} else if (PL_is_integer(t3)) {
int vi;
ret=PL_get_integer(t3,&vi);
RETURN_IF_FAIL
v=(lbfgsfloatval_t) vi;
} else {
return FALSE;
}
env->param.gtol=v;
} else if ((strcmp(name, "xtol") == 0)) {
lbfgsfloatval_t v;
if (PL_is_float(t3)) {
ret=PL_get_float(t3,&v);
RETURN_IF_FAIL
} else if (PL_is_integer(t3)) {
int vi;
ret=PL_get_integer(t3,&vi);
RETURN_IF_FAIL
v=(lbfgsfloatval_t) vi;
} else {
return FALSE;
}
env->param.xtol=v;
} else if ((strcmp(name, "orthantwise_c") == 0)) {
lbfgsfloatval_t v;
if (PL_is_float(t3)) {
ret=PL_get_float(t3,&v);
RETURN_IF_FAIL
} else if (PL_is_integer(t3)) {
int vi;
ret=PL_get_integer(t3,&vi);
RETURN_IF_FAIL
v=(lbfgsfloatval_t) vi;
} else {
return FALSE;
}
env->param.orthantwise_c=v;
} else if ((strcmp(name, "orthantwise_start") == 0)) {
if (! PL_is_integer(t3)) {
return FALSE;
}
ret=PL_get_integer(t3,&(env->param.orthantwise_start));
RETURN_IF_FAIL
} else if ((strcmp(name, "orthantwise_end") == 0)) {
if (! PL_is_integer(t3)) {
return FALSE;
}
ret=PL_get_integer(t3,&(env->param.orthantwise_end));
RETURN_IF_FAIL
} else {
printf("ERROR: The parameter %s is unknown.\n",name);
return FALSE;
}
return TRUE;
}
static foreign_t optimizer_get_parameter(term_t t1, term_t t2, term_t t3) {
char * name;
term_t s2=PL_new_term_ref();
int ret;
environment *env;
ret=PL_get_pointer(t1,(void **)&env);
RETURN_IF_FAIL
if (! PL_is_atom(t2)) {
return FALSE;
}
ret=PL_get_atom_chars(t2,&name);
RETURN_IF_FAIL
if ((strcmp(name, "m") == 0)) {
ret=PL_put_integer(s2,env->param.m);
RETURN_IF_FAIL
return PL_unify(t3,s2);
} else if ((strcmp(name, "epsilon") == 0)) {
ret=PL_put_float(s2,env->param.epsilon);
RETURN_IF_FAIL
return PL_unify(t3,s2);
} else if ((strcmp(name, "past") == 0)) {
ret=PL_put_integer(s2,env->param.past);
RETURN_IF_FAIL
return PL_unify(t3,s2);
} else if ((strcmp(name, "delta") == 0)) {
ret=PL_put_float(s2,env->param.delta);
RETURN_IF_FAIL
return PL_unify(t3,s2);
} else if ((strcmp(name, "max_iterations") == 0)) {
ret=PL_put_integer(s2,env->param.max_iterations);
RETURN_IF_FAIL
return PL_unify(t3,s2);
} else if ((strcmp(name, "linesearch") == 0)) {
ret=PL_put_integer(s2,env->param.linesearch);
RETURN_IF_FAIL
return PL_unify(t3,s2);
} else if ((strcmp(name, "max_linesearch") == 0)) {
ret=PL_put_integer(s2,env->param.max_linesearch);
RETURN_IF_FAIL
return PL_unify(t3,s2);
} else if ((strcmp(name, "min_step") == 0)) {
ret=PL_put_float(s2,env->param.min_step);
RETURN_IF_FAIL
return PL_unify(t3,s2);
} else if ((strcmp(name, "max_step") == 0)) {
ret=PL_put_float(s2,env->param.max_step);
RETURN_IF_FAIL
return PL_unify(t3,s2);
} else if ((strcmp(name, "ftol") == 0)) {
ret=PL_put_float(s2,env->param.ftol);
RETURN_IF_FAIL
return PL_unify(t3,s2);
} else if ((strcmp(name, "gtol") == 0)) {
ret=PL_put_float(s2,env->param.gtol);
RETURN_IF_FAIL
return PL_unify(t3,s2);
} else if ((strcmp(name, "xtol") == 0)) {
ret=PL_put_float(s2,env->param.xtol);
RETURN_IF_FAIL
return PL_unify(t3,s2);
} else if ((strcmp(name, "orthantwise_c") == 0)) {
ret=PL_put_float(s2,env->param.orthantwise_c);
RETURN_IF_FAIL
return PL_unify(t3,s2);
} else if ((strcmp(name, "orthantwise_start") == 0)) {
ret=PL_put_float(s2,env->param.orthantwise_start);
RETURN_IF_FAIL
return PL_unify(t3,s2);
} else if ((strcmp(name, "orthantwise_end") == 0)) {
ret=PL_put_float(s2,env->param.orthantwise_end);
RETURN_IF_FAIL
return PL_unify(t3,s2);
}
printf("ERROR: The parameter %s is unknown.\n",name);
return FALSE;
}
install_t init_lbfgs_predicates( void )
{
// fcall3 = YAP_MkFunctor(YAP_LookupAtom("$lbfgs_callback_evaluate"), 3);
// fprogress8 = YAP_MkFunctor(YAP_LookupAtom("$lbfgs_callback_progress"), 8);
PL_register_foreign("optimizer_reserve_memory",6,optimizer_initialize,0);
PL_register_foreign("optimizer_run",3,optimizer_run,0);
PL_register_foreign("optimizer_free_memory",1,optimizer_finalize,0);
PL_register_foreign("optimizer_set_x",3,set_x_value,0);
PL_register_foreign("optimizer_get_x",3,get_x_value,0);
PL_register_foreign("optimizer_set_g",3,set_g_value,0);
PL_register_foreign("optimizer_get_g",3,get_g_value,0);
PL_register_foreign("optimizer_set_parameter",3,optimizer_set_parameter,0);
PL_register_foreign("optimizer_get_parameter",3,optimizer_get_parameter,0);
}