1 | // solve_IP.cc |
---|
2 | |
---|
3 | // This file provides the interface to call the implemented IP-algorithms. |
---|
4 | // This includes a short help text on the choice of algorithms and their |
---|
5 | // arguments and the required formats of the input file. |
---|
6 | |
---|
7 | #ifndef SOLVE_IP_CC |
---|
8 | #define SOLVE_IP_CC |
---|
9 | |
---|
10 | #include "IP_algorithms.h" |
---|
11 | |
---|
12 | int main(int argc, char *argv[]) |
---|
13 | { |
---|
14 | |
---|
15 | // initialize arguments for the IP-algorithms (Buchberger) |
---|
16 | // with default settings |
---|
17 | BOOLEAN verbose=FALSE; |
---|
18 | short version=1; |
---|
19 | short S_pair_criteria=11; |
---|
20 | double interreduction_percentage=12.0; |
---|
21 | |
---|
22 | ///////////////////////// parse options ///////////////////////////////////// |
---|
23 | |
---|
24 | short alg_option=0; |
---|
25 | // no algorithm specified yet |
---|
26 | |
---|
27 | if(argc>=3) |
---|
28 | // argc has to be at least 3 (except when help is required, see below): |
---|
29 | // program name, (MATRIX or) GROEBNER file, PROBLEM file |
---|
30 | { |
---|
31 | |
---|
32 | for(short i=1;i<argc-2;i++) |
---|
33 | // these are the input options |
---|
34 | { |
---|
35 | |
---|
36 | if(!strcmp(argv[i],"-v") || !strcmp(argv[i],"--verbose")) |
---|
37 | { |
---|
38 | verbose=TRUE; |
---|
39 | continue; |
---|
40 | // read next argument |
---|
41 | } |
---|
42 | |
---|
43 | if(!strcmp(argv[i],"-V") || !strcmp(argv[i],"-version")) |
---|
44 | { |
---|
45 | // check if next argument is a valid version number |
---|
46 | i++; |
---|
47 | |
---|
48 | if(!strcmp(argv[i],"1")) |
---|
49 | { |
---|
50 | version=1; |
---|
51 | continue; |
---|
52 | } |
---|
53 | if(!strcmp(argv[i],"1a")) |
---|
54 | { |
---|
55 | version=0; |
---|
56 | continue; |
---|
57 | } |
---|
58 | if(!strcmp(argv[i],"2")) |
---|
59 | { |
---|
60 | version=2; |
---|
61 | continue; |
---|
62 | } |
---|
63 | if(!strcmp(argv[i],"3")) |
---|
64 | { |
---|
65 | version=3; |
---|
66 | continue; |
---|
67 | } |
---|
68 | |
---|
69 | // When reaching this line, the version argument was invalid. |
---|
70 | cerr<<"ERROR: invalid version argument"<<endl |
---|
71 | <<"Type `solve_IP --help' for help on options."<<endl; |
---|
72 | return 1; |
---|
73 | } |
---|
74 | |
---|
75 | if(!strcmp(argv[i],"-S")) |
---|
76 | { |
---|
77 | // initialize argument to the use of no criteria |
---|
78 | S_pair_criteria=0; |
---|
79 | |
---|
80 | do |
---|
81 | { |
---|
82 | // Check what S-pair criterion the next argument indicates. |
---|
83 | // If none: The algorithm is called without S-pair criteria. |
---|
84 | i++; |
---|
85 | |
---|
86 | if(i>=argc-2) |
---|
87 | // file arguments reached |
---|
88 | break; |
---|
89 | |
---|
90 | if(!strcmp(argv[i],"RP")) |
---|
91 | { |
---|
92 | S_pair_criteria|=1; |
---|
93 | continue; |
---|
94 | } |
---|
95 | if(!strcmp(argv[i],"M")) |
---|
96 | { |
---|
97 | S_pair_criteria|=2; |
---|
98 | continue; |
---|
99 | } |
---|
100 | if(!strcmp(argv[i],"F")) |
---|
101 | { |
---|
102 | S_pair_criteria|=4; |
---|
103 | continue; |
---|
104 | } |
---|
105 | if(!strcmp(argv[i],"B")) |
---|
106 | { |
---|
107 | S_pair_criteria|=8; |
---|
108 | continue; |
---|
109 | } |
---|
110 | if(!strcmp(argv[i],"2")) |
---|
111 | { |
---|
112 | S_pair_criteria|=16; |
---|
113 | continue; |
---|
114 | } |
---|
115 | |
---|
116 | // option does not belong to an S-pair criterion |
---|
117 | break; |
---|
118 | |
---|
119 | } |
---|
120 | while(1); |
---|
121 | |
---|
122 | i--; // reset counter so that the outer loop |
---|
123 | continue; // continues with the just considered argument |
---|
124 | } |
---|
125 | |
---|
126 | if(!strcmp(argv[i],"-p")) |
---|
127 | { |
---|
128 | // read interreduction percentage |
---|
129 | i++; |
---|
130 | |
---|
131 | char** rest; |
---|
132 | interreduction_percentage=strtod(argv[i],rest); |
---|
133 | // The function strtod converts a string into a double. As the |
---|
134 | // specification is not correct, it may cause trouble... |
---|
135 | // For example, if the argument can be read as a float, the value |
---|
136 | // of rest should be NULL. This is not the case. Therefore, we can |
---|
137 | // only check by the following string comparison if a float has been |
---|
138 | // built. An argument as 15xy is thereby considered as valid |
---|
139 | // (with interreduction_percentage==15.0). |
---|
140 | |
---|
141 | if(!(strcmp(argv[i],*rest))) |
---|
142 | // argument was no float |
---|
143 | { |
---|
144 | cerr<<"ERROR: invalid argument for interreduction percentage"<<endl |
---|
145 | <<"Type `solve_IP --help' for help on options."<<endl; |
---|
146 | return 1; |
---|
147 | } |
---|
148 | |
---|
149 | continue; |
---|
150 | } |
---|
151 | |
---|
152 | if(!strcmp(argv[i],"-alg") || !strcmp(argv[i],"--algorithm")) |
---|
153 | { |
---|
154 | // check if next argument is a valid algorithm specification |
---|
155 | i++; |
---|
156 | |
---|
157 | if(!strcmp(argv[i],"ct") || !strcmp(argv[i],"pct") || |
---|
158 | !strcmp(argv[i],"ect") || !strcmp(argv[i],"pt") || |
---|
159 | !strcmp(argv[i],"hs") || !strcmp(argv[i],"du") || |
---|
160 | !strcmp(argv[i],"blr")) |
---|
161 | { |
---|
162 | alg_option=i; |
---|
163 | continue; |
---|
164 | } |
---|
165 | |
---|
166 | // When reaching this line, the algorithm argument was invalid. |
---|
167 | cerr<<"ERROR: unkwon algorithm"<<endl |
---|
168 | <<"Type `solve_IP --help' for help on options."<<endl; |
---|
169 | return 1; |
---|
170 | } |
---|
171 | |
---|
172 | // When reaching this line, the argument is no legal option. |
---|
173 | cerr<<"ERROR: invalid option "<<argv[i]<<endl |
---|
174 | <<"Type `solve_IP --help' for help on options."<<endl; |
---|
175 | return 1; |
---|
176 | } |
---|
177 | |
---|
178 | |
---|
179 | ///////////////////// call IP-algorithms //////////////////////////////////// |
---|
180 | |
---|
181 | // open first input stream (MATRIX or GROEBNER file) |
---|
182 | ifstream first_input(argv[argc-2]); |
---|
183 | |
---|
184 | if(!first_input) |
---|
185 | { |
---|
186 | cerr<<"ERROR: cannot open first input file, possibly not found"<<endl; |
---|
187 | return 1; |
---|
188 | } |
---|
189 | |
---|
190 | // open second input stream (PROBLEM file) |
---|
191 | ifstream second_input(argv[argc-1]); |
---|
192 | |
---|
193 | if(!second_input) |
---|
194 | { |
---|
195 | cerr<<"ERROR: cannot open second input file, possibly not found"<<endl; |
---|
196 | return 1; |
---|
197 | } |
---|
198 | |
---|
199 | // check if the first input file is a MATRIX file; in that case, |
---|
200 | // we have to compute a Groebner basis first |
---|
201 | char format_string[128]; |
---|
202 | first_input>>format_string; |
---|
203 | |
---|
204 | if(!strcmp(format_string,"MATRIX")) |
---|
205 | { |
---|
206 | if(alg_option==0) |
---|
207 | { |
---|
208 | cerr<<"ERROR: first input file is a MATRIX file;\n" |
---|
209 | "algorithm has to be specified"<<endl; |
---|
210 | return 1; |
---|
211 | } |
---|
212 | |
---|
213 | // perform specified algorithm to compute a Groebner basis of the |
---|
214 | // toric ideal defined by the input matrix |
---|
215 | |
---|
216 | int success; |
---|
217 | if(!strcmp(argv[alg_option],"ct")) |
---|
218 | success=Conti_Traverso(argv[argc-2], version, S_pair_criteria, |
---|
219 | interreduction_percentage, verbose); |
---|
220 | if(!strcmp(argv[alg_option],"pct")) |
---|
221 | success=Positive_Conti_Traverso(argv[argc-2], version, |
---|
222 | S_pair_criteria, |
---|
223 | interreduction_percentage, verbose); |
---|
224 | if(!strcmp(argv[alg_option],"ect")) |
---|
225 | success=Elim_Conti_Traverso(argv[argc-2], version, S_pair_criteria, |
---|
226 | interreduction_percentage, verbose); |
---|
227 | if(!strcmp(argv[alg_option],"pt")) |
---|
228 | success=Pottier(argv[argc-2], version, S_pair_criteria, |
---|
229 | interreduction_percentage, verbose); |
---|
230 | if(!strcmp(argv[alg_option],"hs")) |
---|
231 | success=Hosten_Sturmfels(argv[argc-2], version, S_pair_criteria, |
---|
232 | interreduction_percentage, verbose); |
---|
233 | if(!strcmp(argv[alg_option],"du")) |
---|
234 | success=DiBiase_Urbanke(argv[argc-2], version, S_pair_criteria, |
---|
235 | interreduction_percentage, verbose); |
---|
236 | if(!strcmp(argv[alg_option],"blr")) |
---|
237 | success=Bigatti_LaScala_Robbiano(argv[argc-2], version, |
---|
238 | S_pair_criteria, |
---|
239 | interreduction_percentage, verbose); |
---|
240 | if(!success) |
---|
241 | { |
---|
242 | cerr<<"ERROR: could not perform required IP-algorithm,\n" |
---|
243 | "no GROEBNER file created"<<endl; |
---|
244 | return 1; |
---|
245 | } |
---|
246 | |
---|
247 | // Groebner basis successfully computed |
---|
248 | // now solve problems |
---|
249 | |
---|
250 | char* GROEBNER=new char[128]; |
---|
251 | memset(GROEBNER,0,128); |
---|
252 | short i=0; |
---|
253 | while(argv[argc-2][i]!='\0' && argv[argc-2][i]!='.' && argv[argc-2][i]>' ') |
---|
254 | { |
---|
255 | GROEBNER[i]=argv[argc-2][i]; |
---|
256 | i++; |
---|
257 | } |
---|
258 | strcat(GROEBNER,".GB."); |
---|
259 | strcat(GROEBNER,argv[alg_option]); |
---|
260 | |
---|
261 | success=solve(argv[argc-1],GROEBNER); |
---|
262 | delete[] GROEBNER; |
---|
263 | return(!success); |
---|
264 | // ! because 0 indicates in general a regular program termination |
---|
265 | } |
---|
266 | |
---|
267 | else |
---|
268 | // first input file is no MATRIX file, GROEBNER file assumed |
---|
269 | { |
---|
270 | if(argc>3) |
---|
271 | // input options are ignored when program is called with a |
---|
272 | // GROEBNER file |
---|
273 | cerr<<"WARNING: program called with a GROEBNER file,\n" |
---|
274 | "specified options will not have any effect"<<endl; |
---|
275 | return(!solve(argv[argc-1],argv[argc-2])); |
---|
276 | } |
---|
277 | } |
---|
278 | |
---|
279 | |
---|
280 | /////////////////////// provide help text /////////////////////////////////// |
---|
281 | |
---|
282 | // handle the case of "missing" arguments |
---|
283 | |
---|
284 | if((argc==2) && !strcmp(argv[1],"--help")) |
---|
285 | // help required |
---|
286 | { |
---|
287 | system("less solve_IP.hlp"); |
---|
288 | return 0; |
---|
289 | } |
---|
290 | |
---|
291 | else |
---|
292 | // invalid arguments |
---|
293 | cerr<<"USAGE: solve_IP [options] toric_file problem_file"<<endl |
---|
294 | <<"Type `solve_IP --help' for help on options."<<endl; |
---|
295 | |
---|
296 | return 1; |
---|
297 | |
---|
298 | } |
---|
299 | #endif // SOLVE_IP_CC |
---|