phcpack_2.4.88_e448e94a/src/Ada/Main/standard_blackbox_solvers.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
with Timing_Package,Time_Stamps;         use Timing_Package,Time_Stamps;
with Communications_with_User;
with File_Scanning;                      use File_Scanning;
with Standard_Natural_Numbers_io;        use Standard_Natural_Numbers_io;
with Standard_Integer_Numbers_io;        use Standard_Integer_Numbers_io;
with Write_Seed_Number;
with Write_Number_of_Tasks;
with Standard_Complex_Laur_Systems_io;   use Standard_Complex_Laur_Systems_io;
with Standard_Laur_Poly_Convertors;
with Standard_Poly_Laur_Convertors;
with Standard_System_Readers;
with Standard_Binomial_Varieties;
with Standard_Binomial_Varieties_io;
with Standard_Monomial_Maps_io;          use Standard_Monomial_Maps_io;
with Black_Box_Binomial_Solvers;         use Black_Box_Binomial_Solvers;
with Greeting_Banners;
with Black_Box_Helpers;
with Black_Box_Linear_Solvers;
with Black_Box_Single_Solvers;
with Black_Box_Square_Solvers;
with bablsolve;

package body Standard_Blackbox_Solvers is

  procedure Write_Toric_Binomial_Solutions
               ( file : in file_type; d : in natural32;
                 M : in Standard_Integer_Matrices.Matrix;
                 c : in Solution_List ) is

    tmp : Solution_List := c;
    ls : Link_to_Solution;

  begin
    while not Is_Null(tmp) loop
      ls := Head_Of(tmp);
      Standard_Binomial_Varieties_io.Write_Header(file,natural32(M'last(1)),d);
      Standard_Binomial_Varieties_io.Write_Solution(file,d,M,ls.v);
      tmp := Tail_Of(tmp);
    end loop;
  end Write_Toric_Binomial_Solutions;

  procedure Append_Toric_Binomial_Solutions_to_Input_File
              ( name : in string; d : in natural32;
                M : in Standard_Integer_Matrices.Matrix;
                c : in Solution_List ) is

    file : file_type;

  begin
    if not Is_Null(c) then
      Communications_with_User.Open_Append_File(file,name);
      new_line(file);
      put_line(file,"THE SOLUTIONS :");
      Write_Toric_Binomial_Solutions(file,d,M,c);
      close(file);
    end if;
  end Append_Toric_Binomial_Solutions_to_Input_File;

  procedure Append_Affine_Binomial_Solutions_to_Input_File
              ( name : in string;
                c : in Link_to_Array_of_Monomial_Map_Lists ) is

    file : file_type;

  begin
    if c /= null then
      Communications_with_User.Open_Append_File(file,name);
      new_line(file);
      put_line(file,"THE SOLUTIONS :");
      put(file,c.all);
      close(file);
    end if;
  end Append_Affine_Binomial_Solutions_to_Input_File;

  procedure Toric_Binomial_Solver
              ( nt : in natural32; start_moment : in Ada.Calendar.Time;
                p : in Laur_Sys; append_sols : in boolean;
                infilename,outfilename : in string; outfile : out file_type;
                to_file,fail : out boolean; v : in integer32 := 0 ) is

    timer : Timing_Widget;
    d : natural32;
    M : Standard_Integer_Matrices.Link_to_Matrix;
    c : Solution_List;
    ended_moment : Ada.Calendar.Time;

  begin
    if v > 0
     then put_line("-> in bablphc.Toric_Binomial_Solver 1 ...");
    end if;
    tstart(timer);
    Standard_Binomial_Varieties.Black_Box_Solver(p,fail,integer32(d),M,c);
    tstop(timer);
    fail := (d = 0) or fail;  -- binomial varieties not for isolated case!
    if not fail then
      if append_sols then
        Append_Toric_Binomial_Solutions_to_Input_File(infilename,d,M.all,c);
      end if;
      Black_Box_Helpers.Ask_Output_File(outfile,outfilename,to_file);
      ended_moment := Ada.Calendar.Clock;
      if to_file then
        Write_Toric_Binomial_Solutions(outfile,d,M.all,c);
        new_line(outfile);
        print_times(outfile,timer,"solving the binomial system");
        new_line(outfile);
        put(outfile,"PHC ran from ");
        Write_Time_Stamp(outfile,start_moment);
        put(outfile," till "); Write_Time_Stamp(outfile,ended_moment);
        put_line(outfile,".");
        Write_Elapsed_Time(outfile,start_moment,ended_moment);
        Write_Number_of_Tasks(outfile,nt);
        Write_Seed_Number(outfile);
        put_line(outfile,Greeting_Banners.Version);
        close(outfile);
      else
        new_line;
        put_line("THE SOLUTIONS :");
        Write_Toric_Binomial_Solutions(standard_output,d,M.all,c);
        new_line;
        print_times(standard_output,timer,"solving the binomial system");
        new_line;
        put("PHC ran from ");
        Write_Time_Stamp(standard_output,start_moment);
        put(" till "); Write_Time_Stamp(standard_output,ended_moment);
        put_line(".");
        Write_Elapsed_Time(standard_output,start_moment,ended_moment);
        Write_Number_of_Tasks(standard_output,nt);
        Write_Seed_Number(standard_output);
        put_line(Greeting_Banners.Version);
      end if;
    end if;
  end Toric_Binomial_Solver;

  procedure Affine_Binomial_Solver
              ( nt : in natural32; start_moment : in Ada.Calendar.Time;
                p : in Laur_Sys; append_sols : in boolean;
                infilename,outfilename : in string;
                outfile : out file_type; outnewname : out Link_to_String;
                to_file,fail : out boolean; v : in integer32 := 0 ) is

    timer : Timing_Widget;
    sols : Link_to_Array_of_Monomial_Map_Lists;
    ended_moment : Ada.Calendar.Time;

  begin
    if v > 0
     then put_line("-> in bablphc.Affine_Binomial_Solver ...");
    end if;
    Black_Box_Helpers.Ask_Output_File
      (outfile,outfilename,to_file,outnewname);
    if to_file then
      put(outfile,p'last,1); put(outfile," ");
      put(outfile,Number_of_Unknowns(p(p'first)),1); new_line(outfile);
      put(outfile,p);
    end if;
    tstart(timer);
    Black_Box_Binomial_Solver(p,false,sols,fail,v-1);
    tstop(timer);
    if not fail and sols /= null then
      if append_sols
       then Append_Affine_Binomial_Solutions_to_Input_File(infilename,sols);
      end if;
      ended_moment := Ada.Calendar.Clock;
      if to_file then
        new_line(outfile);
        put_line(outfile,"THE SOLUTIONS :");
        put(outfile,sols.all);
        new_line(outfile);
        Show_Degrees(outfile,sols.all);
        new_line(outfile);
        print_times(outfile,timer,"solving the binomial system");
        new_line(outfile);
        put(outfile,"PHC ran from ");
        Write_Time_Stamp(outfile,start_moment);
        put(outfile," till "); Write_Time_Stamp(outfile,ended_moment);
        put_line(outfile,".");
        Write_Elapsed_Time(outfile,start_moment,ended_moment);
        Write_Number_of_Tasks(outfile,nt);
        Write_Seed_Number(outfile);
        close(outfile);
      else
        new_line;
        put_line("THE SOLUTIONS :");
        put(standard_output,sols.all);
        new_line;
        Show_Degrees(sols.all);
        new_line;
        print_times(standard_output,timer,"solving the binomial system");
        new_line;
        put("PHC ran from ");
        Write_Time_Stamp(standard_output,start_moment);
        put(" till "); Write_Time_Stamp(standard_output,ended_moment);
        put_line(".");
        Write_Elapsed_Time(standard_output,start_moment,ended_moment);
        Write_Number_of_Tasks(standard_output,nt);
        Write_Seed_Number(standard_output);
      end if;
    end if;
  end Affine_Binomial_Solver;

  procedure Toric_Binomial_Solver
              ( nt : in natural32; start_moment : in Ada.Calendar.Time;
                p : in Poly_Sys; append_sols : in boolean;
                infilename,outfilename : in string; outfile : out file_type;
                outnewname : out Link_to_String;
                to_file,fail : out boolean; v : in integer32 := 0 ) is

    q : constant Laur_Sys(p'range)
      := Standard_Poly_Laur_Convertors.Polynomial_to_Laurent_System(p);

  begin
    if v > 0
     then put_line("-> in bablphc.Toric_Binomial_Solver 2 ...");
    end if;
   -- Toric_Binomial_Solver(q,append_sols,fail);
    Affine_Binomial_Solver
      (nt,start_moment,q,append_sols,infilename,outfilename,outfile,
       outnewname,to_file,fail,v-1);
  end Toric_Binomial_Solver;

  procedure Solve ( nt : in natural32; infilename,outfilename : in string;
                    start_moment : in Ada.Calendar.Time;
                    p : in Link_to_Poly_Sys; append_sols : in boolean;
                    v : in integer32 := 0 ) is

    n : constant natural32
      := Standard_Complex_Polynomials.Number_of_Unknowns(p(p'first));
    to_file,fail : boolean;
    outfile : file_type;
    outnewname : Link_to_String;

  begin
    if v > 0
     then put_line("-> in standard_blackbox_solvers.Solve 1 ...");
    end if;
    if p'last = p'first then
      Black_Box_Single_Solvers.Solve
        (infilename,outfilename,p(p'first),append_sols,v-1);
    elsif p'last = integer32(n) then
      Black_Box_Linear_Solvers.Solve
        (infilename,outfilename,p,n,append_sols,fail,v-1);
      if fail then
        Black_Box_Square_Solvers.Solve
          (nt,infilename,outfilename,start_moment,p,true,append_sols,v-1);
      end if;
    else
      Toric_Binomial_Solver
        (nt,start_moment,p.all,append_sols,infilename,outfilename,
         outfile,outnewname,to_file,fail,v-1);
      if fail then
        if outnewname = null
         then bablsolve(p.all,outfilename,outfile,to_file,v-1);
         else bablsolve(p.all,outnewname.all,outfile,to_file,v-1);
        end if;
      end if;
    end if;
  end Solve;

  procedure Solve ( nt : in natural32; infilename,outfilename : in string;
                    start_moment : in Ada.Calendar.Time;
                    p : in Link_to_Laur_Sys; append_sols : in boolean;
                    v : in integer32 := 0 ) is

    to_file,fail : boolean;
    outfile : file_type;
    outnewname : Link_to_String;

  begin
    if v > 0
     then put_line("-> in standard_blackbox_solvers.Solve 2 ...");
    end if;
    if Standard_Laur_Poly_Convertors.Is_Genuine_Laurent(p.all) then
     -- put_line("calling Toric_Binomial_Solver ...");
      Toric_Binomial_Solver
        (nt,start_moment,p.all,append_sols,infilename,outfilename,
         outfile,to_file,fail,v-1);
    else
      Affine_Binomial_Solver
        (nt,start_moment,p.all,append_sols,infilename,outfilename,
         outfile,outnewname,to_file,fail,v-1);
    end if;
    if fail then
      Black_Box_Square_Solvers.Solve
        (nt,infilename,outfilename,start_moment,p,append_sols,v-1);
    end if;
  end Solve;

  procedure Main ( nt : in natural32; infilename,outfilename : in string;
                   verbose : in integer32 := 0 ) is

    start_moment : constant Ada.Calendar.Time := Ada.Calendar.Clock;
    infile : file_type;
    append_sols : boolean := false;
    p : Link_to_Poly_Sys;
    q : Link_to_Laur_Sys;

  begin
    if verbose > 0 then
      put("At verbose level "); put(verbose,1);
      put_line(", in standard_blackbox_solvers.Main ...");
    end if;
    Standard_System_Readers.Read_System(infile,infilename,q);
    if q = null then
      put_line(Greeting_Banners.welcome & ".");
      put("Running the blackbox solver");
      if nt = 0
       then put(", no tasking");
       else put(", with "); put(nt,1); put(" tasks");
      end if;
      put_line(", in double precision.");
      new_line; get(q);
    else
      Scan_and_Skip(infile,"SOLUTIONS",append_sols);
      append_sols := not append_sols;
      close(infile);
    end if;
    if Standard_Laur_Poly_Convertors.Is_Genuine_Laurent(q.all) then
      Solve(nt,infilename,outfilename,start_moment,q,append_sols,verbose-1);
    else
      declare
        use Standard_Laur_Poly_Convertors;
        t : constant Poly_Sys(q'range)
          := Positive_Laurent_Polynomial_System(q.all);
      begin
        p := new Poly_Sys'(t);
        Solve(nt,infilename,outfilename,start_moment,p,append_sols,verbose-1);
      end;
    end if;
  end Main;

end Standard_Blackbox_Solvers;