summaryrefslogtreecommitdiff
path: root/modules/simulated_annealing/demos/SAIsing2ddemo.sce
blob: 35e2ac942612a65e26830961fd157ab58b5d760b (plain)
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
//////////////////////////////////////////////////////
// example of use of the simulated annealing method //
//////////////////////////////////////////////////////

function demo_sa_2()

    my_handle = scf(100001);
    clf(my_handle,"reset");
    demo_viewCode("SAIsing2ddemo.sce");
    my_handle.info_message = _("Please wait ...");

    lines(0);
    old_funcprot = funcprot();
    funcprot(0);

    // Loading the test problem

    path = get_absolute_file_path("SAIsing2ddemo.sce");

    getd(path + "/Ising");

    // Loading the neighborhood function for the ising problem

    getd(path + "/.");

    Proba_start = 0.8;
    It_intern = 1000;
    It_extern = 30;
    It_Pre  = 100;
    Log     = %T;
    alpha   = 0.9;

    Ising_Dim   = 10;
    Ising_Proba = 0.3;
    J     = 1.1;
    H     = 0.7;
    Magnet  = "%T";
    Connect = "%T";

    // For the minimization case, everything must be at -1 or 1 in the optimal solution
    deff("y=f(x)","y = ising2d(x,"+string(J)+","+string(H)+","+Magnet+","+Connect+")");
    // For the maximization case, we must have a checker board solution (+1 -1 +1 -1 ....)
    //deff("y=f(x)","y = - ising2d(x,"+string(J)+","+string(H)+","+Magnet+","+Connect+")");

    x0 = init_ising2d(Ising_Dim, Ising_Proba);

    /////////////////////////
    // Simulated Annealing //
    /////////////////////////

    printf("\nSA: geometrical decrease temperature law\n");

    sa_params = init_param();
    sa_params = add_param(sa_params,"dimension",10);
    sa_params = add_param(sa_params,"proba",0.05);
    sa_params = add_param(sa_params,"neigh_func", neigh_func_ising2d); // Required because this operator is specific to the ising2d problem
    sa_params = add_param(sa_params,"accept_func", accept_func_default); // Optional
    sa_params = add_param(sa_params,"temp_law", temp_law_default); // Optional
    sa_params = add_param(sa_params,"alpha",alpha); // For the temperature decreasing law

    T0 = compute_initial_temp(x0, f, Proba_start, It_Pre, sa_params);

    printf("Initial temperature T0 = %f\n", T0);

    [x_opt, f_opt, sa_mean_list, sa_var_list, temp_list] = optim_sa(x0, f, It_extern, It_intern, T0, Log, sa_params);

    printf("optimal solution:\n"); disp(x_opt);
    printf("value of the objective function = %f\n", f_opt);

    if is_handle_valid(my_handle) then
        plot_ising2d(x_opt);
        my_handle.info_message = "";
    end

    funcprot(old_funcprot);

endfunction

demo_sa_2();
clear demo_sa_2;