1 /* Frobby: Software for monomial ideal computations.
2    Copyright (C) 2007 Bjarke Hammersholt Roune (www.broune.com)
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2 of the License, or
7    (at your option) any later version.
8 
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see http://www.gnu.org/licenses/.
16 */
17 #include "stdinc.h"
18 #include "FrobeniusAction.h"
19 
20 #include "BigIdeal.h"
21 #include "IOFacade.h"
22 #include "SliceFacade.h"
23 #include "SliceParams.h"
24 #include "BigTermRecorder.h"
25 #include "Scanner.h"
26 #include "display.h"
27 
FrobeniusAction()28 FrobeniusAction::FrobeniusAction():
29   Action
30 (staticGetName(),
31  "Compute Frobenius number using a Grobner basis algorithm.",
32  "Compute the Frobenius number of the passed-in Frobenius instance. This "
33  "instance\n"
34  "must be preceded in the input by a deg-rev-lex lattice ideal Grobner basis "
35  "as\n"
36  "produced by the program 4ti2.\n\n"
37  "The algorithm for this uses irreducible decomposition to compute the "
38  "Frobenius\n"
39  "number, which is why this action accepts parameters related to that. See "
40  "the\n"
41  "paper \"Solving Thousand Digit Frobenius Problems Using Grobner Bases\"\n"
42  "at www.broune.com for more details.",
43  false),
44 
45   _sliceParams(true, false),
46   _displaySolution
47 ("vector",
48  "Display the vector that achieves the optimal value.",
49  false) {
50   _sliceParams.setSplit("frob");
51 }
52 
obtainParameters(vector<Parameter * > & parameters)53 void FrobeniusAction::obtainParameters(vector<Parameter*>& parameters) {
54   Action::obtainParameters(parameters);
55   _sliceParams.obtainParameters(parameters);
56 
57   parameters.push_back(&_displaySolution);
58 }
59 
perform()60 void FrobeniusAction::perform() {
61   displayNote
62     ("The action frobgrob is DEPRECATED, and will be removed in a future "
63      "release of Frobby. Use the action optimize with options "
64      "-chopFirstAndSubtract and -maxStandard instead to get the same effect.");
65 
66   SliceParams params(_params);
67   validateSplit(params, true, true);
68 
69   vector<mpz_class> instance;
70   BigIdeal ideal;
71 
72   IOFacade ioFacade(_printActions);
73   Scanner in("", stdin);
74   ioFacade.readFrobeniusInstanceWithGrobnerBasis(in, ideal, instance);
75   in.expectEOF();
76 
77   vector<mpz_class> shiftedDegrees(instance.begin() + 1, instance.end());
78   vector<mpz_class> bigVector;
79 
80   BigTermRecorder recorder;
81 
82   SliceFacade facade(params, ideal, recorder);
83   mpz_class dummy;
84   facade.solveStandardProgram(shiftedDegrees, dummy, false);
85 
86   BigIdeal maxSolution = *(recorder.releaseIdeal());
87 
88   ASSERT(maxSolution.getGeneratorCount() == 1);
89   bigVector = maxSolution[0];
90 
91   mpz_class frobeniusNumber = -instance[0];
92   for (size_t i = 1; i < instance.size(); ++i)
93     frobeniusNumber += bigVector[i - 1] * instance[i];
94 
95   if (_displaySolution) {
96     fputs("(-1", stdout);
97     for (size_t i = 0; i < bigVector.size(); ++i)
98       gmp_fprintf(stdout, ", %Zd", bigVector[i].get_mpz_t());
99     fputs(")\n", stdout);
100   }
101 
102   gmp_fprintf(stdout, "%Zd\n", frobeniusNumber.get_mpz_t());
103 }
104 
staticGetName()105 const char* FrobeniusAction::staticGetName() {
106   return "frobgrob";
107 }
108 
displayAction() const109 bool FrobeniusAction::displayAction() const {
110   return false;
111 }
112