adasat_24.0.0_46db8c9a/testsuite/tests/wave_function_collapse/wfc.adb

  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
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
with Ada.Calendar;
with Ada.Command_Line;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Numerics.Discrete_Random;

with AdaSAT.Builders;
with AdaSAT.DPLL;
with AdaSAT.Formulas;
with AdaSAT.Theory;

procedure WFC is
   use Ada.Calendar;
   use AdaSAT;
   use AdaSAT.Formulas;

   Print_Steps : constant Boolean := False;
   --  Set to True to print the generated map at each decision routine
   --  invocation. Useful for debug and visualization of the algorithm.

   N : constant := 9;
   --  size of the resulting image

   T : constant := 5;
   --  Number of tile kinds

   type Tile_Kind is ('0', 'x', '^', '_', ' ');

   type Direction_Kind is (Top, Down, Left, Right);

   type Tile_Kind_Array is array (Positive range <>) of Tile_Kind;

   type Adjacency_Matrix is array
     (Tile_Kind, Direction_Kind, Tile_Kind) of Boolean;
   --  encodes which tile kind is allowed or forbidden as the neighbor
   --  of the given tile kind in the given which direction.

   package Natural_Randoms is new Ada.Numerics.Discrete_Random (Natural);
   Rnd : Natural_Randoms.Generator;

   function Transform (I, J : Natural; K : Tile_Kind) return Variable is
     (Variable ((I - 1) * N * T + (J - 1) * T + Tile_Kind'Pos (K) + 1));
   --  The the boolean variable representing 'cell at coords (I, J) is of
   --  tile kind K).

   function Get_Neighbour
     (I, J : Natural;
      D    : Direction_Kind;
      X, Y : out Natural) return Boolean;

   function Get_Neighbour
     (I, J : Natural;
      D    : Direction_Kind;
      X, Y : out Natural) return Boolean
   is
   begin
      case D is
         when Top =>
            if J = 1 then
               return False;
            else
               X := I;
               Y := J - 1;
            end if;
         when Down =>
            if J = N then
               return False;
            else
               X := I;
               Y := J + 1;
            end if;
         when Left =>
            if I = 1 then
               return False;
            else
               X := I - 1;
               Y := J;
            end if;
         when Right =>
            if I = N then
               return False;
            else
               X := I + 1;
               Y := J;
            end if;
      end case;
      return True;
   end Get_Neighbour;

   function Image (K : Tile_Kind) return String;

   function Image (K : Tile_Kind) return String is
      Img : String := K'Image;
   begin
      return Img (2 .. 2);
   end Image;

   type Empty_Context is null record;

   C : Empty_Context;

   function Empty_Theory_Check
     (Ctx : in out Empty_Context;
      M   : Model;
      F   : in out Formula) return Boolean;

   function Empty_Theory_Check
     (Ctx : in out Empty_Context;
      M   : Model;
      F   : in out Formula) return Boolean
   is
      pragma Unreferenced (Ctx, M, F);
   begin
      return True;
   end Empty_Theory_Check;

   package Empty_Theory is new Theory (Empty_Context, Empty_Theory_Check);

   function Least_Entropy_Decision
     (M : Model; First_Unset : in out Variable) return Variable_Or_Null;

   procedure Print_Map (M : Model);
   procedure Print_Map (M : Model) is
      Unset : Boolean;
   begin
      for J in 1 .. N loop
         for I in 1 .. N loop
            Unset := True;
            for K in Tile_Kind loop
               if M (Transform (I, J, K)) in True then
                  Put (Image (K));
                  Unset := False;
                  exit;
               end if;
            end loop;
            if Unset then
               Put ('?');
            end if;
         end loop;
         New_Line;
      end loop;
      New_Line;
   end Print_Map;

   function Least_Entropy_Decision
     (M : Model; First_Unset : in out Variable) return Variable_Or_Null
   is
      Best_Cell_X, Best_Cell_Y : Natural := 1;
      Best_Cell_Unset_Count    : Natural := T + 1;
      Cur_Cell_Unset_Count     : Natural;
      Chosen_Tile              : Natural;
   begin
      if Print_Steps then
         Print_Map (M);
      end if;
      for J in 1 .. N loop
         for I in 1 .. N loop
            Cur_Cell_Unset_Count := 0;
            for K in Tile_Kind loop
               if M (Transform (I, J, K)) in Unset then
                  Cur_Cell_Unset_Count := Cur_Cell_Unset_Count + 1;
               end if;
            end loop;

            if Cur_Cell_Unset_Count > 0 then
               --  todo change <= to < and handle = specially
               if Cur_Cell_Unset_Count <= Best_Cell_Unset_Count then
                  Best_Cell_X := I;
                  Best_Cell_Y := J;
                  Best_Cell_Unset_Count := Cur_Cell_Unset_Count;
               end if;
            end if;

            if Print_Steps then
               Put (Cur_Cell_Unset_Count'Image);
            end if;
         end loop;
         if Print_Steps then
            New_Line;
         end if;
      end loop;
      if Print_Steps then
         New_Line;
         New_Line;
         New_Line;
      end if;
      Chosen_Tile := Natural_Randoms.Random (Rnd) mod Best_Cell_Unset_Count;
      for K in Tile_Kind loop
         if M (Transform (Best_Cell_X, Best_Cell_Y, K)) in Unset then
            if Chosen_Tile = 0 then
               return Transform (Best_Cell_X, Best_Cell_Y, K);
            else
               Chosen_Tile := Chosen_Tile - 1;
            end if;
         end if;
      end loop;
      raise Program_Error;
   end Least_Entropy_Decision;

   package SAT is new DPLL (Empty_Theory, Least_Entropy_Decision);

   F : Builders.Formula_Builder;

   Adjacency_Rules : Adjacency_Matrix :=
     (others => (others => (others => True)));

   Sol : Model := [1 .. N * N * T => Unset];
begin
   --  Setup problem specific adjacency rules
   Adjacency_Rules ('0', Top, '0') := False;
   Adjacency_Rules ('0', Top, 'x') := False;
   Adjacency_Rules ('0', Left, '^') := False;
   Adjacency_Rules ('0', Right, '^') := False;
   Adjacency_Rules ('0', Left, ' ') := False;
   Adjacency_Rules ('0', Right, ' ') := False;
   Adjacency_Rules ('x', Top, '0') := False;
   Adjacency_Rules ('^', Top, '0') := False;
   Adjacency_Rules ('^', Top, 'x') := False;
   Adjacency_Rules ('^', Left, 'x') := False;
   Adjacency_Rules ('^', Right, 'x') := False;
   Adjacency_Rules (' ', Down, '0') := False;
   Adjacency_Rules (' ', Left, 'x') := False;
   Adjacency_Rules (' ', Right, 'x') := False;
   Adjacency_Rules (' ', Top, '0') := False;
   Adjacency_Rules (' ', Top, '^') := False;
   Adjacency_Rules (' ', Top, 'x') := False;
   for K in Tile_Kind loop
      Adjacency_Rules ('_', Down, K) := False;
      Adjacency_Rules ('_', Left, K) := False;
      Adjacency_Rules ('_', Right, K) := False;
   end loop;
   Adjacency_Rules ('_', Left, '_') := True;
   Adjacency_Rules ('_', Right, '_') := True;

   --  Uncomment the following line to get a random output.
   --  Natural_Randoms.Reset (Rnd, Integer (Seconds (Clock)));

   --  Add the constraint that each cell must be only one tile
   for I in 1 .. N loop
      for J in 1 .. N loop
         declare
            C : Builders.Clause_Builder;
         begin
            for K in Tile_Kind loop
               C.Add (+Transform (I, J, K));
            end loop;
            F.Add (C.Build);
            F.Add_At_Most_One
              (Transform (I, J, Tile_Kind'First),
               Transform (I, J, Tile_Kind'Last));
         end;
      end loop;
   end loop;

   --  Setup the adjacency constraints
   for I in 1 .. N loop
      for J in 1 .. N loop
         for D in Direction_Kind loop
            declare
               X, Y : Natural;
               C    : Builders.Clause_Builder;
            begin
               if Get_Neighbour (I, J, D, X, Y) then
                  for K_1 in Tile_Kind loop
                     C.Add (-Transform (I, J, K_1));
                     for K_2 in Tile_Kind loop
                        if Adjacency_Rules (K_1, D, K_2) then
                           C.Add (+Transform (X, Y, K_2));
                        else
                           F.Add (new Literal_Array'
                             (-Transform (I, J, K_1),
                              -Transform (X, Y, K_2)));
                        end if;
                     end loop;
                     F.Add (C.Build);
                  end loop;
               end if;
            end;
         end loop;
      end loop;
   end loop;

   if SAT.Solve (F.Build, C, Sol) then
      Put_Line ("Solved");
   else
      Put_Line ("Failed solving");
   end if;
   Print_Map (Sol);
end WFC;