This is the mail archive of the guile@cygnus.com mailing list for the guile project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

wrapper for gsl_siman



This is where I got so far with my attempt to wrap the simulated-anealing
stuff in the GNU scientific lib.
/***************************************************************/
/* GNU GPL stuff */
#include <libguile.h>
#include <guile/gh.h>
#include <libguile/dynl.h>
#include <gsl_siman.h>


static SCM s_solve (SCM, SCM, SCM, SCM, SCM);


SCM
s_solve (SCM state0, SCM s_cost, SCM s_step, SCM s_metric, SCM s_params)
{

  SCM ret;
  void *X0 = (void *) (&state0);


  double
    cost (void *state)
  {
    return gh_scm2double (gh_call1 (s_cost, (*((SCM *) state))));
  }

  double
    metric (void *ptr1, void *ptr2)
  {
    return gh_scm2double (gh_call2 (s_metric, *((SCM *) ptr1), *((SCM *) ptr2)));
  }

  void
    step (void *state, double stepsize)
  {
    SCM newpos;
      newpos = gh_call2 (s_step, *((SCM *) state), gh_double2scm (stepsize));
      memcpy (state, &newpos, sizeof (SCM));
  }

  void
    print (void *state)
  {
    ret = *((SCM *) state);
    gh_display (ret);
  }

  gsl_siman_params_t params =
  {
    gh_scm2int (scm_assoc_ref (s_params, gh_symbol2scm ("n-tries"))),
    gh_scm2int (scm_assoc_ref (s_params, gh_symbol2scm ("iters-fixed-T"))),
    gh_scm2double (scm_assoc_ref (s_params, gh_symbol2scm ("step-size"))),
    gh_scm2double (scm_assoc_ref (s_params, gh_symbol2scm ("k"))),
    gh_scm2double (scm_assoc_ref (s_params, gh_symbol2scm ("t-initial"))),
    gh_scm2double (scm_assoc_ref (s_params, gh_symbol2scm ("mu-t"))),
    gh_scm2double (scm_assoc_ref (s_params, gh_symbol2scm ("t-min")))
  };



  gsl_siman_solve (X0, cost, step, metric, print, sizeof (SCM), params);

  return ret;
}

void
init_gsl_siman ()
{
  gh_new_procedure ("solve", s_solve, 5, 0, 0);
}

void
scm_init_gsl_siman_module ()
{
  scm_register_module_xxx ("gsl siman", init_gsl_siman);
}

/********************************************************/

It needs to be linked with libgslsiman, libgslrandist and libgslrand for 
providing a shared extension module, to be placed in gsl/siman somewhere in 
the guile loadpath. 
When using the gsl_ran rodule in addition to this one (which might be useful
for writing the step procedure), best load gsl_ran module before gsl_siman
(whenever I tried the other way round, guile hanged itself up in an endless
loop, maybe some stack overflow. 
Unfortunately my copy of gsl provides no shared libs, so the whole thing is
damn' bloated.

Klaus Schilling