Frobby  0.9.5
OptimizeAction.cpp
Go to the documentation of this file.
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 "OptimizeAction.h"
19 
20 #include "DataType.h"
21 #include "IOFacade.h"
22 #include "SliceFacade.h"
23 #include "Scanner.h"
24 #include "BigIdeal.h"
25 #include "BigTermConsumer.h"
26 #include "NullTermConsumer.h"
27 #include "SliceParams.h"
28 #include "error.h"
29 
30 #include <algorithm>
31 #include <iterator>
32 
34  Action
35 (staticGetName(),
36  "Solve optimization problems related to the input ideal.",
37  "Solves an optimization program defined by the input monomial ideal I, and "
38  "an\ninput vector of integers v. The optimization program is \n"
39  "\n"
40  " maximize v * e such that e encodes an irreducible component of I,\n"
41  "\n"
42  "where * is dot product and e is a vector of integers that uniquely encodes "
43  "an\nirreducible ideal by being the exponent vector of the product of the\n"
44  "minimal generators.\n"
45  "\n"
46  "The input is composed of the ideal I in any format, optionally followed by "
47  "the\nentries of v in a space separated list. If v is not explicitly "
48  "specified,\nthen every entry is assumed to 1, i.e. then v is of the form "
49  "(1, ..., 1).\n"
50  "\n"
51  "This action has options for displaying the optimal value or not and for\n"
52  "displaying zero, one or all of the optimal solutions. The algorithm used "
53  "to\nsolve the optimization program is the Slice Algorithm using the bound\n"
54  "optimization. Thus this action also has options related to that.",
55  false),
56 
57  _sliceParams(true, false),
58 
59  _displayLevel
60  ("displayLevel",
61  "Controls how many optimal solutions to display. If the value is 0 or "
62  "1,\nFrobby displays 0 or 1 solutions respectively. If the value is 2 or "
63  "more,\nall solutions are displayed. The output is presented as generators "
64  "of a\nmonomial ideal.",
65  0),
66 
67  _displayValue
68  ("displayValue",
69  "Display the optimal value of the optimization program.",
70  true),
71 
72  _maxStandard
73  ("maxStandard",
74  "Solve the optimization program for maximal standard monomials instead "
75  "of\nfor monomials representing irreducible components.",
76  false),
77 
78  _chopFirstAndSubtract
79  ("chopFirstAndSubtract",
80  "Remove the first variable from generators, from the ring and from v, "
81  "and\nsubtract the value of the first entry of v from the reported "
82  "optimal value.\nThis is useful for Frobenius number calculations.",
83  false),
84 
85  _minimizeValue
86  ("minValue",
87  "Minimize the value of v * e above. If this option is not set, maximize "
88  "v * e\ninstead, as is the stated default above.",
89  false),
90 
91  _io(DataType::getMonomialIdealType(), DataType::getMonomialIdealType()) {
92  _sliceParams.setSplit("degree");
93 }
94 
95 void OptimizeAction::obtainParameters(vector<Parameter*>& parameters) {
96  parameters.push_back(&_displayLevel);
97  parameters.push_back(&_displayValue);
98  parameters.push_back(&_maxStandard);
99  parameters.push_back(&_chopFirstAndSubtract);
100  parameters.push_back(&_minimizeValue);
101  _io.obtainParameters(parameters);
102  _sliceParams.obtainParameters(parameters);
103  Action::obtainParameters(parameters);
104 }
105 
107  SliceParams params(_params);
108  validateSplit(params, true, true);
109 
110  BigIdeal ideal;
111  vector<mpz_class> v;
112  {
113  Scanner in(_io.getInputFormat(), stdin);
116 
117  IOFacade ioFacade(_printActions);
118  ioFacade.readIdeal(in, ideal);
119  if (!in.matchEOF())
120  ioFacade.readVector(in, v, ideal.getVarCount());
121  else
122  fill_n(back_inserter(v), ideal.getVarCount(), 1);
123  in.expectEOF();
124  }
125 
126  mpz_class subtract = 0;
127  if (_chopFirstAndSubtract) {
128  if (v.empty()) {
129  _chopFirstAndSubtract = false;
130  } else {
131  subtract = v[0];
132 
133  v.erase(v.begin());
134  ideal.eraseVar(0);
135  }
136  }
137 
138  if (_minimizeValue) {
139  for (size_t var = 0; var < v.size(); ++var)
140  v[var] = -v[var];
141  }
142 
143  auto_ptr<IOHandler> handler;
144  auto_ptr<BigTermConsumer> output;
145  if (_displayLevel > 0) {
146  handler = _io.createOutputHandler();
147  output = handler->createIdealWriter(stdout);
148  } else
149  output.reset(new NullTermConsumer());
150 
151  SliceFacade facade(params, ideal, *output);
152 
153  mpz_class optimalValue = 0;
154 
155  bool displayAll = (_displayLevel >= 2);
156  bool anySolution;
157  if (_maxStandard)
158  anySolution = facade.solveStandardProgram
159  (v, optimalValue, displayAll);
160  else
161  anySolution = facade.solveIrreducibleDecompositionProgram
162  (v, optimalValue, displayAll);
163 
164  if (_displayValue) {
165  if (!anySolution)
166  fputs("no solution.\n", stdout);
167  else {
168  if (_minimizeValue) {
169  // We flipped the sign of the vector to optimize before, so we
170  // need to flip the sign of the value again.
171  optimalValue = -optimalValue;
172  }
174  optimalValue -= subtract;
175  gmp_fprintf(stdout, "%Zd\n", optimalValue.get_mpz_t());
176  }
177  }
178 }
179 
181  return "optimize";
182 }
void validateSplit(const SliceParams &params, bool allowLabel, bool allowDegree)
Definition: SliceParams.cpp:61
Definition: Action.h:25
CliParams _params
Definition: Action.h:59
BoolParameter _printActions
Definition: Action.h:68
virtual void obtainParameters(vector< Parameter * > &parameters)
Definition: Action.cpp:133
void eraseVar(size_t var)
Definition: BigIdeal.cpp:233
size_t getVarCount() const
Definition: BigIdeal.h:148
The intention of this class is to describe the different kinds of mathematical structures that Frobby...
Definition: DataType.h:29
A facade for input and output of mathematical objects.
Definition: IOFacade.h:39
void readIdeal(Scanner &in, BigTermConsumer &consumer)
Read an ideal from in and feed it to consumer.
Definition: IOFacade.cpp:81
void readVector(Scanner &in, vector< mpz_class > &v, size_t integerCount)
Definition: IOFacade.cpp:256
void autoDetectInputFormat(Scanner &in)
If using the input format, this must be called before validating the ideals, since the auto detect fo...
auto_ptr< IOHandler > createOutputHandler() const
const string & getInputFormat() const
void validateFormats() const
This follows the null object pattern.
IntegerParameter _displayLevel
SliceParameters _sliceParams
BoolParameter _minimizeValue
BoolParameter _chopFirstAndSubtract
IOParameters _io
static const char * staticGetName()
virtual void obtainParameters(vector< Parameter * > &parameters)
BoolParameter _maxStandard
virtual void perform()
BoolParameter _displayValue
void obtainParameters(vector< Parameter * > &parameters)
This class offers an input interface which is more convenient and for some purposes more efficient th...
Definition: Scanner.h:50
void expectEOF()
Require that there is no more input.
Definition: Scanner.cpp:77
bool matchEOF()
Return true if no more input.
Definition: Scanner.h:210
A facade for operations on monomial ideals using the Slice Algorithm.
Definition: SliceFacade.h:44
bool solveStandardProgram(const vector< mpz_class > &grading, mpz_class &value, bool reportAllSolutions)
Solve an optimization program over maximal standard monomials.
bool solveIrreducibleDecompositionProgram(const vector< mpz_class > &grading, mpz_class &optimalValue, bool reportAllSolutions)
Solve an optimization program over irreducible components.
void setSplit(const string &split)
Set the value of the option for choosing the split selection strategy.