int main(void) { int x_initial[N_CITIES]; unsigned int i; const gsl_rng * r = gsl_rng_alloc (gsl_rng_env_setup()) ; gsl_ieee_env_setup (); prepare_distance_matrix(); /* set up a trivial initial route */ printf("# initial order of cities:\n"); for (i = 0; i < N_CITIES; ++i) { printf("# \"%s\"\n", cities[i].name); x_initial[i] = i; } printf("# distance matrix is:\n"); print_distance_matrix(); printf("# initial coordinates of cities (longitude and latitude)\n"); /* this can be plotted with */ /* ./siman_tsp > hhh ; grep city_coord hhh | awk '{print $2 " " $3}' | xyplot -ps -d "xy" > c.eps */ for (i = 0; i < N_CITIES+1; ++i) { printf("###initial_city_coord: %g %g \"%s\"\n", -cities[x_initial[i % N_CITIES]].longitude, cities[x_initial[i % N_CITIES]].lat, cities[x_initial[i % N_CITIES]].name); } /* exhaustive_search(); */ gsl_siman_solve(r, x_initial, Etsp, Stsp, Mtsp, Ptsp, NULL, NULL, NULL, N_CITIES*sizeof(int), params); printf("# final order of cities:\n"); for (i = 0; i < N_CITIES; ++i) { printf("# \"%s\"\n", cities[x_initial[i]].name); } printf("# final coordinates of cities (longitude and latitude)\n"); /* this can be plotted with */ /* ./siman_tsp > hhh ; grep city_coord hhh | awk '{print $2 " " $3}' | xyplot -ps -d "xy" > c.eps */ for (i = 0; i < N_CITIES+1; ++i) { printf("###final_city_coord: %g %g %s\n", -cities[x_initial[i % N_CITIES]].longitude, cities[x_initial[i % N_CITIES]].lat, cities[x_initial[i % N_CITIES]].name); } printf("# "); fflush(stdout); #if 0 system("date"); #endif /* 0 */ fflush(stdout); return 0; }
void simulated_annealing() { const double x_initial = 15.5; gsl_rng_env_setup(); const gsl_rng_type *T = gsl_rng_default; gsl_rng *r = gsl_rng_alloc(T); gsl_siman_solve(r, (void *)&x_initial, local::E1, local::S1, local::M1, local::P1, NULL, NULL, NULL, sizeof(double), local::params); gsl_rng_free(r); }
int main(void) { const gsl_rng_type * T; gsl_rng * r; double x_initial = 15.5; gsl_rng_env_setup(); T = gsl_rng_default; r = gsl_rng_alloc(T); gsl_siman_solve(r, &x_initial, E1, S1, M1, P1, NULL, NULL, NULL, sizeof(double), params); gsl_rng_free (r); return 0; }
double siman(gsl_vector * vec, void * params, gsl_rng * rng) { block * myblock = block_alloc(vec->size); gsl_vector_memcpy(myblock->vec, vec); myblock->ptr = params; gsl_siman_params_t siman_params = {N_TRIES, ITERS_FIXED_T, STEP_SIZE, K, T_INITIAL, MU_T, T_MIN}; gsl_siman_solve(rng, myblock, E1, S1, M1, P1, block_copy, block_copy_construct, block_destroy, 0, siman_params); gsl_vector_fprintf(stdout, myblock->vec, "%g"); printf("siman llik: %g\n", -E1(myblock) ); P1(myblock); return -E1(myblock); }
int main(int argc, char *argv[]) { Element x0; /* initial guess for search */ /* double x_initial = 2.5; */ double x_initial = -10.0; gsl_rng * r = gsl_rng_alloc (gsl_rng_env_setup()) ; gsl_ieee_env_setup (); gsl_siman_solve(r, &x_initial, E1, S1, M1, P1, NULL, NULL, NULL, sizeof(double), params); return 0; if (argc != 2) { /* fprintf(stderr, "usage: %s [D1, D2, D3, CA]\n", argv[0]); */ fprintf(stderr, "usage: %s [D1 | D2 | D3]\n", argv[0]); return 1; } printf("#testing the simulated annealing routines\n"); if (strcmp(argv[1], "D1") == 0) { x0.D1 = 12.0; printf("#one dimensional problem, x0 = %f\n", x0.D1); /* gsl_siman_Usolve(r, &x0, test_E_1D, test_step_1D, distance_1D, */ /* print_pos_1D, params); */ return 0; } if (strcmp(argv[1], "D2") == 0) { x0.D2[0] = 12.0; x0.D2[1] = 5.5; printf("#two dimensional problem, (x0,y0) = (%f,%f)\n", x0.D2[0], x0.D2[1]); /* gsl_siman_Usolve(r, &x0, test_E_2D, test_step_2D, distance_2D, */ /* print_pos_2D, params); */ return 0; } if (strcmp(argv[1], "D3") == 0) { x0.D3[0] = 12.2; x0.D3[1] = 5.5; x0.D3[2] = -15.5; printf("#three dimensional problem, (x0,y0,z0) = (%f,%f,%f)\n", x0.D3[0], x0.D3[1], x0.D3[2]); /* gsl_siman_Usolve(r, &x0, test_E_3D, test_step_3D, distance_3D, */ /* print_pos_3D, params); */ } /* x0.D2[0] = 12.2; x0.D2[1] = 5.5; gsl_siman_solve(r, &x0, test_E_2D, test_step_2D, distance_2D, print_pos_2D, params); */ /* x0.D3[0] = 12.2; x0.D3[1] = 5.5; x0.D3[2] = -15.5; gsl_siman_solve(r, &x0, test_E_3D, test_step_3D, distance_3D, print_pos_3D, params); */ return 0; }
int main(void) { double x_min = 1.36312999455315182 ; double x ; gsl_rng * r = gsl_rng_alloc (gsl_rng_env_setup()) ; gsl_ieee_env_setup (); /* The function tested here has multiple mimima. The global minimum is at x = 1.36312999, (f = -0.87287) There is a local minimum at x = 0.60146196, (f = -0.84893) */ x = -10.0 ; gsl_siman_solve(r, &x, E1, S1, M1, NULL, NULL, NULL, NULL, sizeof(double), params); gsl_test_rel(x, x_min, 1e-3, "f(x)= exp(-(x-1)^2) sin(8x), x0=-10") ; x = +10.0 ; gsl_siman_solve(r, &x, E1, S1, M1, NULL, NULL, NULL, NULL, sizeof(double), params); gsl_test_rel(x, x_min, 1e-3, "f(x)= exp(-(x-1)^2) sin(8x), x0=10") ; /* Start at the false minimum */ x = +0.6 ; gsl_siman_solve(r, &x, E1, S1, M1, NULL, NULL, NULL, NULL, sizeof(double), params); gsl_test_rel(x, x_min, 1e-3, "f(x)= exp(-(x-1)^2) sin(8x), x0=0.6") ; x = +0.5 ; gsl_siman_solve(r, &x, E1, S1, M1, NULL, NULL, NULL, NULL, sizeof(double), params); gsl_test_rel(x, x_min, 1e-3, "f(x)= exp(-(x-1)^2) sin(8x), x0=0.5") ; x = +0.4 ; gsl_siman_solve(r, &x, E1, S1, M1, NULL, NULL, NULL, NULL, sizeof(double), params); gsl_test_rel(x, x_min, 1e-3, "f(x)= exp(-(x-1)^2) sin(8x), x0=0.4") ; gsl_rng_free(r); exit (gsl_test_summary ()); #ifdef JUNK x0.D1 = 12.0; printf("#one dimensional problem, x0 = %f\n", x0.D1); gsl_siman_Usolve(r, &x0, test_E_1D, test_step_1D, distance_1D, print_pos_1D, params); x0.D2[0] = 12.0; x0.D2[1] = 5.5; printf("#two dimensional problem, (x0,y0) = (%f,%f)\n", x0.D2[0], x0.D2[1]); gsl_siman_Usolve(r, &x0, test_E_2D, test_step_2D, distance_2D, print_pos_2D, params); x0.D3[0] = 12.2; x0.D3[1] = 5.5; x0.D3[2] = -15.5; printf("#three dimensional problem, (x0,y0,z0) = (%f,%f,%f)\n", x0.D3[0], x0.D3[1], x0.D3[2]); gsl_siman_Usolve(r, &x0, test_E_3D, test_step_3D, distance_3D, print_pos_3D, params); x0.D2[0] = 12.2; x0.D2[1] = 5.5; gsl_siman_solve(r, &x0, test_E_2D, test_step_2D, distance_2D, print_pos_2D, params); x0.D3[0] = 12.2; x0.D3[1] = 5.5; x0.D3[2] = -15.5; gsl_siman_solve(r, &x0, test_E_3D, test_step_3D, distance_3D, print_pos_3D, params); return 0; #endif }
/***** solver *****/ static VALUE rb_gsl_siman_solver_solve(VALUE obj, VALUE rng, VALUE vx0p, VALUE vefunc, VALUE vstep, VALUE vmetric, VALUE vprint, VALUE vparams) { gsl_rng *r = NULL; siman_solver *ss = NULL; siman_Efunc *efunc = NULL; siman_step *step = NULL; siman_metric *metric = NULL; siman_print *print = NULL; gsl_vector *vtmp = NULL; gsl_siman_params_t *params = NULL, ppp; int flag = 0; /* Data_Get_Struct(obj, siman_solver, ss);*/ CHECK_VECTOR(vx0p); Data_Get_Struct(vx0p, gsl_vector, vtmp); switch (TYPE(obj)) { case T_MODULE: case T_CLASS: case T_OBJECT: ss = gsl_siman_solver_alloc(vtmp->size); flag = 1; break; default: Data_Get_Struct(obj, siman_solver, ss); } if (!rb_obj_is_kind_of(rng, cgsl_rng)) rb_raise(rb_eTypeError, "wrong argument type %s (GSL::Rng expected)", rb_class2name(CLASS_OF(rng))); if (!rb_obj_is_kind_of(vefunc, cgsl_siman_Efunc)) rb_raise(rb_eTypeError, "wrong argument type %s (GSL::Siman::Efunc expected)", rb_class2name(CLASS_OF(vefunc))); if (!rb_obj_is_kind_of(vstep, cgsl_siman_step)) rb_raise(rb_eTypeError, "wrong argument type %s (GSL::Siman::Step expected)", rb_class2name(CLASS_OF(vstep))); if (!rb_obj_is_kind_of(vmetric, cgsl_siman_metric)) rb_raise(rb_eTypeError, "wrong argument type %s (GSL::Siman::Metric expected)", rb_class2name(CLASS_OF(vmetric))); Data_Get_Struct(rng, gsl_rng, r); Data_Get_Struct(vefunc, siman_Efunc, efunc); Data_Get_Struct(vstep, siman_step, step); Data_Get_Struct(vmetric, siman_metric, metric); if (NIL_P(vprint)) { ss->proc_print = Qnil; } else { if (!rb_obj_is_kind_of(vprint, cgsl_siman_print)) rb_raise(rb_eTypeError, "wrong argument type %s (GSL::Siman::Print expected)", rb_class2name(CLASS_OF(vprint))); Data_Get_Struct(vprint, siman_print, print); ss->proc_print = print->proc; } if (!rb_obj_is_kind_of(vparams, cgsl_siman_params)) rb_raise(rb_eTypeError, "wrong argument type %s (GSL::Siman::Params expected)", rb_class2name(CLASS_OF(vparams))); Data_Get_Struct(vparams, gsl_siman_params_t, params); ss->proc_efunc = efunc->proc; ss->proc_step = step->proc; ss->proc_metric = metric->proc; gsl_vector_memcpy(ss->vx, vtmp); ppp = *params; if (NIL_P(vprint)) { gsl_siman_solve(r, ss, rb_gsl_siman_Efunc_t, rb_gsl_siman_step_t, rb_gsl_siman_metric_t, NULL, rb_gsl_siman_copy_t, rb_gsl_siman_copy_construct_t, rb_gsl_siman_destroy_t, 0, *params); } else { gsl_siman_solve(r, ss, rb_gsl_siman_Efunc_t, rb_gsl_siman_step_t, rb_gsl_siman_metric_t, rb_gsl_siman_print_t, rb_gsl_siman_copy_t, rb_gsl_siman_copy_construct_t, rb_gsl_siman_destroy_t, 0, *params); } gsl_vector_memcpy(vtmp, ss->vx); if (flag == 1) gsl_siman_solver_free(ss); return obj; }