Attention une mise à jour du service Gitlab va être effectuée le mardi 18 janvier (et non lundi 17 comme annoncé précédemment) entre 18h00 et 18h30. Cette mise à jour va générer une interruption du service dont nous ne maîtrisons pas complètement la durée mais qui ne devrait pas excéder quelques minutes.

satode.pl 3.57 KB
Newer Older
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
1
2
3
4
5
6
7
8
/** <satode> SAT-solving ODE generation
 *
 * run "sat(NbVars, NbClauses), pl2xpp('outfile.ode')."
 * then call "biocham outfile.ode" and type-in the parameters for clauses
 * parameter(c_1_1=1, c_1_2=1, c_2_1= -1, c_2_2= -1, c_3_1= -1, c_3_2=1).
 * numerical_simulation.
 * plot(show: {x_1, x_2}).
 *
9
10
11
 * Or "read_dimacs('sat.cnf'), pl2xpp('out.ode')." and then you can directly
 * run "numerical_simulation. plot."
 *
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
12
13
14
15
 * @author Sylvain Soliman
 * @license GPL2
 */

16
:- module(satode, [sat/2, read_dimacs/1]).
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
17
:- reexport(minibiocham).
18
:- use_module(symbolic).
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
19
20
21
22
23
24
25
26
27
28
29

%! sat(+N:integer, +M:integer) is det.
%
% Generates the ODE system for a SAT problem with N variables and M clauses
sat(N, M) :-
  new_ode,
  forall(
    between(1, N, I),
    (
      make_name('x', I, Xi),
      assertz(ode_init(Xi, 0.5)),
30
31
32
      big_sum(I, M, N, Gi),
      simplify(Gi, GGi),
      assertz(ode_dxdt(Xi, GGi)),
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
33
34
35
36
37
38
39
40
41
42
43
      make_name('s', I, Si),
      assertz(ode_func(Si, 2 * Xi - 1))
    )
  ),
  forall(
    between(1, M, J),
    (
      make_name('a', J, Aj),
      make_name('k', J, Kj),
      assertz(ode_init(Aj, 1)),
      assertz(ode_dxdt(Aj, Aj * Kj)),
44
45
      big_prod(J, N, 0, Pj),
      simplify(Pj, PPj),
46
47
48
49
50
51
52
53
54
      assertz(ode_func(Kj, PPj)),
      make_name('c', J, Cj),
      forall(
        between(1, N, I),
        (
          make_name(Cj, I, Cji),
          assertz(ode_par(Cji, 0))
        )
      )
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
55
    )
56
  ).
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
57
58
59
60
61
62
63
64
65


%! make_name(+Prefix:atom, +I:integer, -Name:atom) is det.
%
% Make a name from a prefix and a number
make_name(Prefix, I, Name) :-
  format(atom(Name), "~w_~d", [Prefix, I]).


66
%! big_prod(+J:integer, +N:integer, +Exclude, -Pj:term) is det.
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
67
%
68
69
70
71
72
% Pj is the term corresponding to the prod of all 1 - CjiSi except Exclude
% and normalized with 1/2^N
big_prod(J, N, Exclude, Pj) :-
  %% Start is 1 / (2^N),   % Could normalize with 2^k for k-SAT
  Start is 1,
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
73
74
75
76
  findall(
    C,
    (
      between(1, N, I),
77
      I \= Exclude,
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
78
79
80
81
82
83
84
      make_name('c', J, Cj),
      make_name(Cj, I, Cji),
      make_name('s', I, Si),
      C = 1 - Cji * Si
    ),
    L
  ),
85
  foldl(prod, L, Start, Pj).
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
86
87
88
89
90


prod(X, Y, Y*X).


91
%! big_sum(+I:integer, +M:integer, +N:integer, -Gj:term) is det.
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
92
93
%
% Gj is the term corresponding to the sum of all AjCjiKjiKj
94
big_sum(I, M, N, Gi) :-
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
95
96
97
98
99
100
101
102
  findall(
    C,
    (
      between(1, M, J),
      make_name('c', J, Cj),
      make_name(Cj, I, Cji),
      make_name('a', J, Aj),
      make_name('k', J, Kj),
103
104
      big_prod(J, N, I, Kji),
      C = Aj * Cji * Kj * Kji
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
105
106
107
108
109
110
111
    ),
    L
  ),
  foldl(sum, L, 0, Gi).


sum(X, Y, Y+X).
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


read_dimacs(File) :-
  setup_call_cleanup(
    open(File, read, Stream),
    (
      read_header(Stream, N, M),
      sat(N, M),
      forall(
        between(1, M, J),
        read_clause(Stream, J)
      )
    ),
    close(Stream)
  ).


read_header(Stream, N, M) :-
  read_line_to_string(Stream, Line),
  split_string(Line, " ", " ", ["p", "cnf", Nvar, Nclauses]),
  string_to_number(Nvar, N),
  string_to_number(Nclauses, M).


read_clause(Stream, Index) :-
  read_line_to_string(Stream, Line),
  split_string(Line, " ", " ", VarStrings),
  maplist(string_to_number, VarStrings, Vars),
  maplist(set_clause_parameter(Index), Vars).


string_to_number(String, Number) :-
  read_term_from_atom(String, Number, []),
  number(Number).


set_clause_parameter(_Clause, 0) :-
  !.

set_clause_parameter(Clause, N) :-
  N > 0,
  !,
  make_name('c', Clause, Cj),
  make_name(Cj, N, Cji),
156
  retractall(ode_par(Cji, _)),
157
158
159
160
161
162
163
  assertz(ode_par(Cji, 1)).

set_clause_parameter(Clause, N) :-
  N < 0,
  NN is -N,
  make_name('c', Clause, Cj),
  make_name(Cj, NN, Cji),
164
  retractall(ode_par(Cji, _)),
165
  assertz(ode_par(Cji, -1)).