This commit is contained in:
Vitor Santos Costa 2018-08-21 03:01:03 +01:00
parent d4eea0fbb4
commit 63e4b31787
16 changed files with 236 additions and 182 deletions

View File

@ -1025,8 +1025,39 @@ restart:
return (FALSE);
}
#define CREATE_ARRAY_DEFS() \
PAR("type", isatom, CREATE_ARRAY_TYPE), \
PAR("address", isaddress, CREATE_ARRAY_ADDRESS), \
PAR("int", isnull, CREATE_ARRAY_INT), \
PAR("dbref", isnull, CREATE_ARRAY_DBREF), \
PAR("float", isnull, CREATE_ARRAY_FLOAT), \
PAR("ptr", isnull, CREATE_ARRAY_PTR), \
PAR("atom", isnull, CREATE_ARRAY_ATOM), \
PAR("char", isnull, CREATE_ARRAY_CHAR), \
PAR("unsigned_char", isnull, CREATE_UNSIGNED_CHAR), \
PAR("nb_term", isnull, CREATE_ARRAY_NB_TERM), \
PAR("address", isaddress, CREATE_ARRAY_ADDRESS), \
PAR("address", isaddress, CREATE_ARRAY_ADDRESS), \
PAR("address", isaddress, CREATE_ARRAY_ADDRESS), \
PAR("address", isaddress, CREATE_ARRAY_ADDRESS), \
PAR("address", isaddress, CREATE_ARRAY_ADDRESS), \
PAR("address", isaddress, CREATE_ARRAY_ADDRESS), \
PAR("address", isaddress, CREATE_ARRAY_ADDRESS), \
PAR("close_on_abort", booleanFlag, OPEN_CLOSE_ON_ABORT), \
PAR("create", isatom, OPEN_CREATE), \
PAR("encoding", isatom, OPEN_ENCODING), \
PAR("eof_action", isatom, OPEN_EOF_ACTION), \
PAR("expand_filename", booleanFlag, OPEN_EXPAND_FILENAME), \
PAR("file_name", isatom, OPEN_FILE_NAME), PAR("input", ok, OPEN_INPUT), \
PAR("locale", isatom, OPEN_LOCALE), PAR("lock", isatom, OPEN_LOCK), \
PAR("mode", isatom, OPEN_MODE), PAR("output", ok, OPEN_OUTPUT), \
PAR("representation_errors", booleanFlag, OPEN_REPRESENTATION_ERRORS), \
PAR("reposition", booleanFlag, OPEN_REPOSITION), \
PAR("script", booleanFlag, OPEN_SCRIPT), PAR("type", isatom, OPEN_TYPE), \
PAR("wait", booleanFlag, OPEN_WAIT), PAR(NULL, ok, OPEN_END)
/* create an array (+Name, + Size, +Props) */
static Int
/** @pred static_array(+ _Name_, + _Size_, + _Type_)
@ -1035,6 +1066,7 @@ static Int
integer. The _Type_ must be bound to one of types mentioned
previously.
*/
static Int
create_static_array(USES_REGS1) {
Term ti = Deref(ARG2);
Term t = Deref(ARG1);

View File

@ -580,16 +580,18 @@ break_rationals_complex_term(CELL *pt0, CELL *pt0_end, CELL *ptf, Term *vout, Te
{
if (IsPairTerm(d0)) {
CELL *ap2 = RepPair(d0);
if (IN_BETWEEN(HB, ap2[0],HR)) {
RESET_VARIABLE(pt0);
vin = add_to_list(vin, (CELL)(pt0), *ap2 );
fprintf(stderr, "%ld \n", RepPair(ap2[0])- ptf);
if (IsVarTerm(ap2[0]) && IN_BETWEEN(HB, (ap2[0]),HR)) {
Term v = MkVarTerm();
*ptf = v;
vin = add_to_list(vin, (CELL)(ptf), AbsPair(ptf) );
ptf++;
continue;
}
*ptf = AbsPair(HR);
ptf++;
if (to_visit+1 >= (struct bp_frame *)AuxSp) {
goto heap_overflow;
}
*ptf++ = (CELL)(HR);
to_visit->start_cp = pt0;
to_visit->end_cp = pt0_end;
to_visit->to = ptf;
@ -610,30 +612,35 @@ break_rationals_complex_term(CELL *pt0, CELL *pt0_end, CELL *ptf, Term *vout, Te
ptf++;
continue;
}
deref_head(d0, copy_term_unk);
d0 = Deref(d0);
if (!IsVarTerm(d0)) {
goto copy_term_nvar;
} else {
*ptf++ = d0;
}
continue;
} else if (IsApplTerm(d0)) {
register Functor f;
register CELL *ap2;
/* store the terms to visit */
ap2 = RepAppl(d0);
if (IN_BETWEEN(HB, ap2[1],HR)) {
RESET_VARIABLE(pt0);
vin = add_to_list(vin, (CELL)pt0, ap2[1] );
continue;
}
f = (Functor)(*ap2);
ap2 = RepAppl(d0)+1;
f = (Functor)(ap2[-1]);
if (IsExtensionFunctor(f)) {
*ptf++ = d0; /* you can just copy other extensions. */
continue;
}
arity_t arity = ArityOfFunctor(f);
*ptf = AbsAppl(HR);
if (IsApplTerm(ap2[0]) && IN_BETWEEN(HB, RepAppl(ap2[0]),HR)) {
RESET_VARIABLE(ptf);
vin = add_to_list(vin, (CELL)ptf, ap2[0] );
ptf++;
continue;
}
arity_t arity = ArityOfFunctor(f);
if (to_visit+1 >= (struct bp_frame *)AuxSp) {
goto heap_overflow;
}
ap2++;
*ptf++ = AbsAppl(HR);
to_visit->start_cp = pt0;
to_visit->end_cp = pt0_end;
to_visit->to = ptf;
@ -642,16 +649,26 @@ break_rationals_complex_term(CELL *pt0, CELL *pt0_end, CELL *ptf, Term *vout, Te
/* fool the system into thinking we had a variable there */
to_visit ++;
pt0 = ap2;
pt0_end = ap2 + arity;
pt0_end = ap2 + (arity-1);
ptf = HR;
if (HR > ASP - 2048) {
goto overflow;
}
if (IsVarTerm(d0) && d0 == (CELL)pt0) {
*ptf++ =(CELL)f;
*ap2 = AbsAppl(HR);
HR += (arity+1);
if (IsVarTerm(d0) && d0 == (CELL)(ap2)) {
RESET_VARIABLE(ptf);
ptf++;
continue;
}
deref_head(d0, copy_term_unk);
d0 = Deref(d0);
if (!IsVarTerm(d0)) {
goto copy_term_nvar;
} else {
*ptf++ = d0;
}
continue;
} else {
/* just copy atoms or integers */
*ptf++ = d0;
@ -730,14 +747,12 @@ Yap_BreakRational(Term inp, UInt arity, Term *to, Term ti USES_REGS) {
*to = ti;
return t;
} else if (IsPairTerm(t)) {
Term tf;
CELL *ap;
CELL *Hi;
restart_list:
ap = RepPair(t);
Hi = HR;
tf = AbsPair(HR);
HR += 2;
{
Int res;
@ -755,7 +770,6 @@ Yap_BreakRational(Term inp, UInt arity, Term *to, Term ti USES_REGS) {
}
} else {
Functor f;
Term tf;
CELL *HB0;
CELL *ap;
@ -767,7 +781,6 @@ Yap_BreakRational(Term inp, UInt arity, Term *to, Term ti USES_REGS) {
}
HB0 = HR;
ap = RepAppl(t);
tf = AbsAppl(HR);
HR[0] = (CELL)f;
arity = ArityOfFunctor(f);
HR += 1+arity;

View File

@ -778,6 +778,12 @@ if (WITH_GECODE)
add_subDIRECTORY(packages/gecode)
endif ()
option(WITH_LBFGS
"interface with lbfgs" ON)
if (WITH_LBFGS)
add_subDIRECTORY(packages/yap-lbfgs)
endif ()

View File

@ -238,11 +238,16 @@
logger_define_variable(Name,Type) :-
bb_get(logger_variables,Variables),
member((Name,_),Variables),
member((Name,Type0),Variables),
!,
throw(error(variable_redefined(logger_define_variable(Name,Type)))).
( Type == Type0
->
write('redefining logger variable '),write(Name),write(' of type '), write(Type0), nl
;
throw(error(variable_redefined(logger_define_variable(Name,Type)))
).
logger_define_variable(Name,Type) :-
ground(Name),
ground(Type),
atomic(Name),
!,
logger_define_variable_intern(Type,Name).
@ -301,7 +306,8 @@ logger_set_delimiter(Delimiter) :-
%= +Name, +Value
%========================================================================
logger_set_variable(Name,Value) :-
logger_set_variable(Name,Value) :- logger_set_variable_again(Name,Value).
/*
atom_concat(logger_data_,Name,Key),
(
bb_get(Key,null)
@ -312,11 +318,12 @@ logger_set_variable(Name,Value) :-
bb_get(Key,_)
->
(
write('logger_set_variable, Variable '),
write(Name),
write(' is already set'),
nl,
fail
true
% write('logger_set_variable, Variable '),
% write(Name),
% write(' is already set'),
% nl %,
% fail
) ; (
write('logger_set_variable, unknown variable '),
write(Name),
@ -325,7 +332,7 @@ logger_set_variable(Name,Value) :-
)
)
),!.
*/
%========================================================================
%= Set the value of the variable name. If the value is already set or
%= the old value is overwritten. If the variable does not exists, an

View File

@ -389,6 +389,8 @@ reset_learning :-
do_learning(Iterations) :-
do_learning(Iterations,-1).
:- spy do_learning/2.
do_learning(Iterations,Epsilon) :-
current_predicate(user:example/4),
!,
@ -512,29 +514,31 @@ init_learning :-
% if yes, switch to problog_exact
% continuous facts are not supported yet.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% problog_flag(init_method,(_,_,_,_,OldCall)),
%% (
%% (
%% continuous_fact(_),
%% OldCall\=problog_exact_save(_,_,_,_,_)
%% )
%% ->
%% (
%% format_learning(2,'Theory uses continuous facts.~nWill use problog_exact/3 as initalization method.~2n',[]),
%% set_problog_flag(init_method,(Query,Probability,BDDFile,ProbFile,problog_exact_save(Query,Probability,_Status,BDDFile,ProbFile)))
%% );
%% true
%% ),
problog_flag(continuous_facts, true),
!,
problog_flag(init_method,(_,_,_,_,OldCall)),
(
(
continuous_fact(_),
OldCall\=problog_exact_save(_,_,_,_,_)
)
->
(
format_learning(2,'Theory uses continuous facts.~nWill use problog_exact/3 as initalization method.~2n',[]),
set_problog_flag(init_method,(Query,Probability,BDDFile,ProbFile,problog_exact_save(Query,Probability,_Status,BDDFile,ProbFile)))
);
true
),
%% (
%% problog_tabled(_)
%% ->
%% (
%% format_learning(2,'Theory uses tabling.~nWill use problog_exact/3 as initalization method.~2n',[]),
%% set_problog_flag(init_method,(Query,Probability,BDDFile,ProbFile,problog_exact_save(Query,Probability,_Status,BDDFile,ProbFile)))
%% );
%% true
%% ),
(
problog_tabled(_)
->
(
format_learning(2,'Theory uses tabling.~nWill use problog_exact/3 as initalization method.~2n',[]),
set_problog_flag(init_method,(Query,Probability,BDDFile,ProbFile,problog_exact_save(Query,Probability,_Status,BDDFile,ProbFile)))
);
true
),
succeeds_n_times(user:test_example(_,_,_,_),TestExampleCount),
@ -1001,6 +1005,7 @@ inv_sigmoid(T,InvSig) :-
%========================================================================
save_old_probabilities :-
problog_flag(continous_facts, true),
forall(tunable_fact(FactID,_),
(
continuous_fact(FactID)
@ -1101,10 +1106,14 @@ gradient_descent :-
save_old_probabilities,
update_values,
reset_gradients.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% start set gradient to zero
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
reset_gradients :-
problog_flag(continous_facts, true),
!,
forall(tunable_fact(FactID,_),
(
continuous_fact(FactID)
@ -1487,6 +1496,7 @@ init_flags :-
problog_define_flag(libbdd_init_method,problog_flag_validate_dummy,'ProbLog predicate to search proofs',(Query,Tree,problog:problog_kbest_as_bdd(Query,100,Tree)),learning_general,flags:learning_libdd_init_handler),
problog_define_flag(alpha,problog_flag_validate_number,'weight of negative examples (auto=n_p/n_n)',auto,learning_general,flags:auto_handler),
problog_define_flag(sigmoid_slope,problog_flag_validate_posnumber,'slope of sigmoid function',1.0,learning_general),
problog_define_flag(continuous_facts,problog_flag_validate_boolean,'support parameter learning of continuous distributions',1.0,learning_general),
problog_define_flag(learning_rate,problog_flag_validate_posnumber,'Default learning rate (If line_search=false)',examples,learning_line_search,flags:examples_handler),
problog_define_flag(line_search, problog_flag_validate_boolean,'estimate learning rate by line search',false,learning_line_search),

View File

@ -116,40 +116,34 @@ X_API PyObject *string_to_python(const char *s, bool eval, PyObject *p0) {
return s_to_python(s, eval, p0);
}
static bool copy_to_dictionary(PyObject *dict, term_t targ, term_t taux,
static bool entry_to_dictionary(PyObject *dict, Term targ,
bool eval, bool cvt) {
PyObject *lhs, *rhs;
term_t tleft = PL_new_term_ref(), tright = PL_new_term_ref();
functor_t fun;
AOK(PL_get_functor(targ, &fun), false);
while (fun == FUNCTOR_comma2) {
AOK(PL_get_arg(1, targ, tleft), false);
if (!copy_to_dictionary(dict, tleft, taux, eval, cvt))
PyObject *lhs = NULL, *rhs;
Term t1, t2;
const char *s;
t1 = ArgOfTerm(1,targ);
if (YAP_IsAtomTerm(t1)) {
s = YAP_AtomName(YAP_AtomOfTerm(t1));
} else if (IsStringTerm(t1)) {
s = YAP_StringOfTerm(t1);
} else if (IsIntegerTerm(t1)) {
lhs = PyLong_FromLong(IntegerOfTerm(t1));
} else {
return false;
AOK(PL_get_arg(2, targ, targ), false);
return copy_to_dictionary(dict, tright, taux, eval, cvt);
}
// PyObject_Print(dict, stderr, 0); fprintf(stderr,"\n");
// Py_DECREF(lhs);
// Py_DECREF(rhs);
AOK(PL_get_arg(1, targ, tleft), false);
lhs = atom_to_python_string(tleft);
if (lhs == NULL) {
return FALSE;
lhs = PyUnicode_FromString(s);
}
AOK(PL_get_arg(2, targ, tright), false);
rhs = term_to_python(tright, eval, NULL, cvt);
t2 = ArgOfTerm(2,targ);
rhs = term_to_python(Yap_InitSlot(t2), eval, NULL, cvt);
if (rhs == NULL) {
PyErr_Print();
return false;
}
return PyDict_SetItem(dict, lhs, rhs) >= 0;
// PyObject_Print(dict, stderr, 0); fprintf(stderr,"\n");
// Py_DECREF(lhs);
// Py_DECREF(rhs);
return PyDict_SetItem(dict, lhs, rhs) == 0;
}
/**
@ -266,9 +260,6 @@ PyObject *term_to_python(term_t t, bool eval, PyObject *o, bool cvt) {
{
term_t tail = PL_new_term_ref();
functor_t fun;
atom_t name;
int arity;
PyObject *rc;
if (!PL_get_functor(t, &fun)) {
PL_reset_term_refs(tail);
@ -343,7 +334,6 @@ PyObject *term_to_python(term_t t, bool eval, PyObject *o, bool cvt) {
if (fun == FUNCTOR_brackets1) {
AOK(PL_get_arg(1, t, t), NULL);
PyObject *ys = term_to_python(t, true, o, true), *rc;
PyObject_Print(ys,stderr,0);fprintf(stderr, "--- \n");
CHECK_CALL(ys, PyTuple_New(0), NULL);
return rc;
}
@ -384,33 +374,29 @@ PyObject *term_to_python(term_t t, bool eval, PyObject *o, bool cvt) {
return PyComplex_FromDoubles(d1, d2);
}
if (fun == FUNCTOR_curly1) {
term_t targ = PL_new_term_ref(), taux = PL_new_term_ref();
PyObject *dict;
AOK(PL_get_arg(1, t, t), NULL);
Term yt = ArgOfTerm(1,Yap_GetFromHandle(t));
if (!(dict = PyDict_New()))
return NULL;
Py_INCREF(dict);
DebugPrintf("Dict %p\n", dict);
while (PL_is_functor(t, FUNCTOR_comma2)) {
AOK(PL_get_arg(1, t, targ), NULL);
AOK(PL_is_functor(targ, FUNCTOR_colon2), NULL);
AOK(copy_to_dictionary(dict, targ, taux, eval, cvt), NULL);
AOK(PL_get_arg(2, t, t), NULL);
}
if (PL_is_functor(t, FUNCTOR_colon2)) {
AOK(copy_to_dictionary(dict, t, taux, eval, cvt), NULL);
while (IsApplTerm(yt) && FunctorOfTerm(yt) == FunctorComma) {
if (!entry_to_dictionary(dict, ArgOfTerm(1,yt), eval, cvt))
return NULL;
yt = ArgOfTerm(2,yt);
}
if (entry_to_dictionary(dict, yt, eval, cvt))
return dict;
else
return NULL;
}
atom_t name;
int arity;
AOK(PL_get_name_arity(t, &name, &arity), NULL);
if (name == ATOM_t) {
int i;
rc = PyTuple_New(arity);
PyObject *rc = PyTuple_New(arity);
for (i = 0; i < arity; i++) {
term_t arg = PL_new_term_ref();
if (!PL_get_arg(i + 1, t, arg)) {
@ -428,11 +414,9 @@ PyObject *term_to_python(term_t t, bool eval, PyObject *o, bool cvt) {
}
}
if (eval)
rc = compound_to_pyeval(t, o, cvt);
return compound_to_pyeval(t, o, cvt);
else
rc = compound_to_pytree(t, o, cvt);
PL_reset_term_refs(tail);
return rc;
return compound_to_pytree(t, o, cvt);
}
}
}

View File

@ -133,8 +133,9 @@ else if (PyList_Check(pVal)) {
if (sz == 0)
return repr_term(pVal);
Term t = TermNil;
for (i = sz; i > 0; --i) {
PyObject *p = PyTuple_GetItem(pVal, i);
for (i = sz; i > 0; ) {
-- i;
PyObject *p = PyList_GetItem(pVal, i);
if (p == NULL) {
PyErr_Clear();
return false;
@ -147,33 +148,33 @@ else if (PyList_Check(pVal)) {
return t;
}
else if (PyDict_Check(pVal)) {
Py_ssize_t pos = 0;
int left = PyDict_Size(pVal);
Py_ssize_t pos = 0, tot = PyDict_Size(pVal);
PyObject *key, *value;
Term f, *opt = &f, t;
if (left == 0) {
return repr_term(pVal);
} else {
Term f, *opt = &f, t, to;
while (PyDict_Next(pVal, &pos, &key, &value)) {
Term t0[2], to;
Term t0[2];
t0[0] = python_to_term__(key);
t0[1] = python_to_term__(value);
to = Yap_MkApplTerm(FunctorModule, 2, t0);
if (left--) {
if (pos < tot) {
t = Yap_MkNewApplTerm(FunctorComma, 2);
*opt = t;
CELL *pt = RepAppl(t) + 1;
pt[0] = to;
*opt = t;
opt = pt+1;
} else {
*opt = t = to;
}
}
return Yap_MkApplTerm(FunctorBraces, 1, &t);
}
}else {
if (pos == 0) {
return repr_term(pVal);
}
*opt = to;
break;
}
}
return Yap_MkApplTerm(FunctorBraces, 1, &f);
}
return repr_term(pVal);
}
foreign_t python_to_term(PyObject *pVal, term_t t) {
@ -218,6 +219,7 @@ PyObject *py_Local, *py_Global;
*python_assign.
*/
bool python_assign(term_t t, PyObject *exp, PyObject *context) {
PyErr_Print();
context = find_obj(context, t, false);
// Yap_DebugPlWriteln(yt);
switch (PL_term_type(t)) {
@ -244,7 +246,6 @@ bool python_assign(term_t t, PyObject *exp, PyObject *context) {
context = py_Main;
if (PyObject_SetAttrString(context, s, exp) == 0)
return true;
PyErr_Print();
return false;
}
case PL_INTEGER:

View File

@ -82,15 +82,15 @@ extern functor_t FUNCTOR_dollar1, FUNCTOR_abs1, FUNCTOR_all1, FUNCTOR_any1,
FUNCTOR_dot2;
extern X_API PyObject *py_Main;
extern X_API PyObject *py_Builtin;
extern X_API PyObject *py_Yapex;
extern X_API PyObject *py_Local;
extern X_API PyObject *py_Atoms;
extern X_API PyObject *py_Global;
extern X_API PyObject *py_Context;
extern PyObject *Py_f2p;
extern PyObject *py_Sys;
extern PyObject *py_ModDict;
#define py_ModDict PyImport_GetModuleDict()
#define py_Local PyEval_GetLocals()
#define py_Global PyEval_GetGlobals()
#define py_Builtin PyEval_GetBuiltins()
extern X_API bool python_in_python;
@ -133,8 +133,13 @@ static inline int proper_ascii_string(const char *s) {
static inline PyObject *atom_to_python_string(term_t t) {
// Yap_DebugPlWrite(YAP_GetFromSlot(t)); fprintf(stderr, " here I
// am\n");
char *s = NULL;
if (!PL_get_atom_chars(t, &s))
const char *s = NULL;
Term yapt = Yap_GetFromSlot(t);
if (IsStringTerm(yapt))
s = StringOfTerm(yapt);
else if (IsAtomTerm(yapt))
s = RepAtom(AtomOfTerm(yapt))->StrOfAE;
else
return NULL;
/* return __main__,s */
#if PY_MAJOR_VERSION < 3

View File

@ -59,7 +59,6 @@ PyObject *PythonLookupSpecial(const char *s) {
}
static PyObject *builtin(const char *sp) {
PyObject *py_Builtin = PyEval_GetBuiltins();
return PyDict_GetItemString(py_Builtin, sp);
}
@ -73,7 +72,6 @@ PyObject *lookupPySymbol(const char *sp, PyObject *pContext, PyObject **duc) {
if ((out = finalLookup(py_Context, sp))) {
return out;
}
PyObject *py_Builtin = PyEval_GetBuiltins();
if ((out = finalLookup(py_Builtin, sp))) {
return out;
}
@ -81,11 +79,9 @@ PyObject *lookupPySymbol(const char *sp, PyObject *pContext, PyObject **duc) {
{
return out;
}
PyObject *py_Local = PyEval_GetLocals();
if ((out = finalLookup(py_Local, sp)) && out != Py_None) {
return out;
}
PyObject *py_Global = PyEval_GetGlobals();
if ((out = finalLookup(py_Global, sp))) {
return out;
}
@ -980,6 +976,7 @@ PyObject *compound_to_pyeval(term_t t, PyObject *context, bool cvt) {
AOK(PL_get_arg(1, t, targ), NULL);
ptr = term_to_python(targ, true, NULL, true);
if (!ptr) return NULL;
return PyObject_Dir(ptr);
{}
}
@ -1123,7 +1120,7 @@ PyObject *compound_to_pyeval(term_t t, PyObject *context, bool cvt) {
}
PyObject *rc;
if (ys && PyCallable_Check(ys)) {
if ( PyCallable_Check(ys)) {
//PyObject_Print(ys, stderr, 0);
// PyObject_Print(pArgs, stderr, 0);
// PyObject_Print(pyDict, stderr, 0);

View File

@ -131,7 +131,7 @@ static bool py_close(int sno) {
static bool pygetLine(StreamDesc *rl_iostream, int sno) {
// term_t ctk = python_acquire_GIL();
const char *myrl_line;
PyObject *user_line, *prompt;
PyObject *user_line;
StreamDesc *s = YAP_GetStreamFromId(sno);
//term_t tg = python_acquire_GIL();
if (!strcmp(RepAtom(s->name)->StrOfAE,"input") ) {
@ -147,7 +147,7 @@ static bool pygetLine(StreamDesc *rl_iostream, int sno) {
user_line = PyObject_CallFunctionObjArgs(pystream, PyUnicode_FromString("?- ") , NULL);
} else {
PyObject *readl = PyObject_GetAttrString(s->u.private_data, "readline");
user_line = PyObject_CallFunction(readl, PyUnicode_FromString("?- ") , NULL);
user_line = PyObject_CallFunction(readl, "?- ");
}
myrl_line = PyUnicode_AsUTF8(user_line);
if (myrl_line == NULL)

View File

@ -8,7 +8,7 @@ PyObject *py_Main;
void pyErrorHandler__(int line, const char *file, const char *code) {
// this code is called if a Python error is found.
// int lvl = push_text_stack();
PyObject *type, *val;
PyObject *type;
// PyErr_Fetch(&type, &val, NULL);
// PyErr_Print();
// Yap_ThrowError__(file,code,line,0, SYSTEM_ERROR_RUNTIME_PYTHON ,"Python

View File

@ -21,34 +21,30 @@ functor_t FUNCTOR_dollar1, FUNCTOR_abs1, FUNCTOR_all1, FUNCTOR_any1, FUNCTOR_as2
FUNCTOR_dot2, FUNCTOR_brackets1;
X_API PyObject *py_Atoms;
X_API PyObject *py_Builtin;
X_API PyObject *py_Yapex;
X_API PyObject *py_Sys;
X_API PyObject * pYAPError;
PyObject *py_Context;
PyObject *py_ModDict;
X_API PyObject *Py_f2p;
extern X_API bool python_in_python;
static void add_modules(void) {
py_Main = PyImport_AddModule("__main__");
Py_INCREF(py_Main);
py_Sys = PyImport_AddModule("sys");
py_Atoms= PyDict_New();
Py_INCREF(py_Sys);
py_Builtin = PyImport_AddModule("__builtin__");
Py_INCREF(py_Builtin);
py_ModDict = PyObject_GetAttrString(py_Sys, "modules");
// py_Yapex = PyImport_ImportModule("yap4py.yapi");
// PyObject *py_Yap =
py_Yapex = PyImport_AddModule("yap4py.yapi");
if ( PyDict_Contains(PyImport_GetModuleDict(), PyUnicode_FromString("__main__"))) {
py_Main = PyDict_GetItemString(PyImport_GetModuleDict(),"__main__");
} else {
py_Main = PyImport_ImportModule("__main__");
}
Py_INCREF(py_Main);
py_Yapex = PyImport_ImportModule("yap4py.yapi");
if (py_Yapex)
Py_INCREF(py_Yapex);
Py_f2p = PythonLookup("f2p", NULL);
if (!Py_f2p)
Py_f2p = PyList_New(0);
Py_f2p = PyDict_New();
Py_INCREF(Py_f2p);
init_python_vfs();
}
@ -119,7 +115,7 @@ X_API bool do_init_python(void) {
// PyGILState_STATE gstate = PyGILState_Ensure();
term_t t = PL_new_term_ref();
if (!python_in_python)
if (!Py_IsInitialized())
Py_Initialize();
install_py_constants();
PL_reset_term_refs(t);

View File

@ -16,11 +16,13 @@ ENDIF (NOT YAP_FOUND)
INCLUDE_DIRECTORIES(
${CMAKE_CURRENT_SOURCE_DIR}/${LBFGS_SRC}/include
${CMAKE_CURRENT_BINARY_DIR}
${CMAKE_SOURCE_DIR}/include
)
SET ( LBFGS_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/${LBFGS_SRC}/lib/arithmetic_ansi.h ${CMAKE_CURRENT_SOURCE_DIR}/${LBFGS_SRC}/lib/arithmetic_sse_double.h ${CMAKE_CURRENT_SOURCE_DIR}/${LBFGS_SRC}/lib/arithmetic_sse_float.h ${CMAKE_CURRENT_SOURCE_DIR}/${LBFGS_SRC}/lib/lbfgs.c yap_lbfgs.c)
${CMAKE_CURRENT_SOURCE_DIR}/${LBFGS_SRC}/lib/arithmetic_ansi.h ${CMAKE_CURRENT_SOURCE_DIR}/${LBFGS_SRC}/lib/arithmetic_sse_double.h ${CMAKE_CURRENT_SOURCE_DIR}/${LBFGS_SRC}/lib/arithmetic_sse_float.h ${CMAKE_CURRENT_SOURCE_DIR}/${LBFGS_SRC}/lib/lbfgs.c ${CMAKE_CURRENT_SOURCE_DIR}/yap_lbfgs.c)
add_library(LBFGS
${LBFGS_SOURCES})
@ -37,5 +39,5 @@ ENDIF (NOT YAP_FOUND)
)
INSTALL(FILES lbfgs.yap DESTINATION ${libpl})
INSTALL(FILES lbfgs.pl DESTINATION ${libpl})

View File

@ -21,18 +21,19 @@
% This is the call back function which evaluates F and the gradient of F
evaluate(FX,_N,_Step) :-
optimizer_get_x(0,X0),
evaluate(FX,_N,_Step, Args) :-
X0 <== Args[0],
FX is sin(X0),
G0 is cos(X0),
optimizer_set_g(0,G0).
Args[0] <== G0.
% This is the call back function which is invoked to report the progress
% if the last argument is set to anywhting else than 0, the optimizer will
% stop right now
progress(FX,X_Norm,G_Norm,Step,_N,Iteration,Ls,0) :-
optimizer_get_x(0,X0),
format('~d. Iteration : x0=~4f f(X)=~4f |X|=~4f |X\'|=~4f Step=~4f Ls=~4f~n',[Iteration,X0,FX,X_Norm,G_Norm,Step,Ls]).
progress(FX,X_Norm,G_Norm,Step,_N,Iteration,Ls,Args, 0) :-
X0 <== Args[0],
format('~d. Iteration : x0=~4f f(X)=~4f |X|=~4f |X\'|=~4f Step=~4f Ls=~4f~n',
[Iteration,X0,FX,X_Norm,G_Norm,Step,Ls]).

View File

@ -154,7 +154,7 @@ yes
*/
:- dynamic initialized/0.
:- load_foreign_files(['yap_lbfgs'],[],'init_lbfgs_predicates').
:- load_foreign_files(['libLBFGS'],[],'init_lbfgs_predicates').
/** @pred optimizer_initialize(+N,+Evaluate,+Progress)
The same as before, except that the user module is the default
@ -187,12 +187,12 @@ optimizer_initialize(N,Module,Call_Evaluate,Call_Progress) :-
% install call back predicates in the user module which call
% the predicates given by the arguments
EvalGoal =.. [Call_Evaluate,E1,E2,E3],
EvalGoal =.. lbfgs_callback_evaluate(e(E1,E2,E3)),
ProgressGoal =.. [Call_Progress,P1,P2,P3,P4,P5,P6,P7,P8],
retractall( user:'$lbfgs_callback_evaluate'(_E1,_E2,_E3) ),
retractall( user:'$lbfgs_callback_progress'(_P1,_P2,_P3,_P4,_P5,_P6,_P7,_P8) ),
assert( (user:'$lbfgs_callback_evaluate'(E1,E2,E3) :- Module:EvalGoal, !) ),
assert( (user:'$lbfgs_callback_progress'(P1,P2,P3,P4,P5,P6,P7,P8) :- Module:ProgressGoal, !) ),
retractall( lbfgs_callback_evaluate(_Step ),
retractall( lbfgs_callback_progress(_P1,_P2,_P3,_P4,_P5,_P6,_P7,_P8) ),
assert( ( lbfgs_callback_evaluate(E1,E2,E3) :- Module:EvalGoal, !) ),
assert( ( lbfgs_callback_progress(P1,P2,P3,P4,P5,P6,P7,P8) :- Module:ProgressGoal, !) ),
assert(initialized).
/** @pred optimizer_finalize/0

View File

@ -1,4 +1,4 @@
o#include <string.h>
#include <string.h>
#include "YapInterface.h"
#include <lbfgs.h>
#include <stdio.h>
@ -30,7 +30,7 @@ o#include <string.h>
#define OPTIMIZER_STATUS_CB_EVAL 3
#define OPTIMIZER_STATUS_CB_PROGRESS 4
void init_lbfgs_predicates( void ) ;
X_API void init_lbfgs_predicates( void ) ;
int optimizer_status=OPTIMIZER_STATUS_NONE; // the internal state
int n; // the size of the parameter vector
@ -53,13 +53,13 @@ static lbfgsfloatval_t evaluate(
YAP_Bool result;
YAP_Int s1;
YAP_Term t[3];
YAP_Term t[4];
t[0] = YAP_MkVarTerm();
t[1] = YAP_MkIntTerm(n);
t[2] = YAP_MkFloatTerm(step);
call = YAP_MkApplTerm(fcall3, 3, t);
call = YAP_MkApplTerm(fcall3, 4, t);
g=g_tmp;
@ -168,7 +168,7 @@ static YAP_Bool set_x_value(void) {
} else if (YAP_IsIntTerm(t2)) {
x[i]=(lbfgsfloatval_t) YAP_IntOfTerm(t2);
} else {
return FALSE;
return false;
}
@ -322,7 +322,7 @@ static YAP_Bool optimizer_initialize(void) {
if (! YAP_IsIntTerm(t1)) {
return FALSE;
return false;
}
temp_n=YAP_IntOfTerm(t1);
@ -619,14 +619,14 @@ static YAP_Bool optimizer_get_parameter( void ) {
}
printf("ERROR: The parameter %s is unknown.\n",name);
return FALSE;
return false;
}
void init_lbfgs_predicates( void )
X_API void init_lbfgs_predicates( void )
{
fcall3 = YAP_MkFunctor(YAP_LookupAtom("$lbfgs_callback_evaluate"), 3);
fprogress8 = YAP_MkFunctor(YAP_LookupAtom("$lbfgs_callback_progress"), 8);