StatisticsStrategy.cpp
Go to the documentation of this file.
/* Frobby: Software for monomial ideal computations.
Copyright (C) 2007 Bjarke Hammersholt Roune (www.broune.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/.
*/
#include "stdinc.h"
#include "StatisticsStrategy.h"
#include "Slice.h"
#include <cmath>
StatisticsStrategy::StatisticsStrategy(SliceStrategy* strategy, FILE* out):
_strategy(strategy),
_out(out),
_internalTracker("internal nodes"),
_leafTracker("leaf nodes") {
ASSERT(strategy != 0);
}
StatisticsStrategy::~StatisticsStrategy() {
}
void StatisticsStrategy::run(const Ideal& ideal) {
_strategy->run(ideal);
fputs("**** Slice Algorithm Statistics ****\n", _out);
_internalTracker.printReport(_out);
_leafTracker.printReport(_out);
}
bool StatisticsStrategy::processSlice
(TaskEngine& tasks, unique_ptr<Slice> slice) {
_internalTracker.preliminaryRecord(*slice);
_leafTracker.preliminaryRecord(*slice);
bool wasBaseCase = _strategy->processSlice(tasks, std::move(slice));
if (wasBaseCase)
_leafTracker.commitRecord();
else
_internalTracker.commitRecord();
return wasBaseCase;
}
void StatisticsStrategy::setUseIndependence(bool use) {
_strategy->setUseIndependence(use);
}
void StatisticsStrategy::setUseSimplification(bool use) {
_strategy->setUseSimplification(use);
}
bool StatisticsStrategy::getUseSimplification() const {
return _strategy->getUseSimplification();
}
void StatisticsStrategy::freeSlice(unique_ptr<Slice> slice) {
_strategy->freeSlice(std::move(slice));
}
StatisticsStrategy::StatTracker::StatTracker(const string& title):
_title(title) {
}
void StatisticsStrategy::StatTracker::preliminaryRecord(const Slice& slice) {
_prelimIdealGenCount = slice.getIdeal().getGeneratorCount();
_prelimSubGenCount = slice.getSubtract().getGeneratorCount();
_prelimVarCount = slice.getVarCount();
}
void StatisticsStrategy::StatTracker::commitRecord() {
++_nodeCount;
_idealGenSum += _prelimIdealGenCount;
_subGenSum += _prelimSubGenCount;
_varSum += _prelimVarCount;
size_t genCountLog2 = 0;
if (_prelimIdealGenCount > 0)
genCountLog2 = (size_t)(log((double)_prelimIdealGenCount) / log((double)2));
_nodesByGenCount[genCountLog2] += 1;
}
void StatisticsStrategy::StatTracker::printReport(FILE* out) const {
fprintf(out, "|-%s:\n", _title.c_str());
gmp_fprintf(out, " | %Zd nodes\n", getNodeCount().get_mpz_t());
fprintf(out, " | %f generators of ideal on avg.\n", getAvgIdealGenCount());
fprintf(out, " | %f generators of subtract on avg.\n", getAvgSubGenCount());
fprintf(out, " | %f variables of ambient ring on avg.\n", getAvgVarCount());
if (!_nodesByGenCount.empty()) {
fputs(" |- nodes by log base 2 of ideal generator count:\n", out);
size_t genCountLog2 = _nodesByGenCount.rbegin()->first;
while (true) {
mpz_class nodeCount = 0;
map<size_t, mpz_class>::const_iterator it =
_nodesByGenCount.find(genCountLog2);
if (it != _nodesByGenCount.end())
nodeCount = it->second;
gmp_fprintf(out, " | %Zd nodes has log2(#gens) = %Zd.\n",
nodeCount.get_mpz_t(),
mpz_class(genCountLog2).get_mpz_t());
if (genCountLog2 == 0)
break;
--genCountLog2;
}
}
}
const mpz_class& StatisticsStrategy::StatTracker::getNodeCount() const {
return _nodeCount;
}
double StatisticsStrategy::StatTracker::getAvgIdealGenCount() const {
if (_nodeCount == 0)
return 0.0;
else {
mpq_class q = mpq_class(_idealGenSum) / _nodeCount;
return q.get_d();
}
}
double StatisticsStrategy::StatTracker::getAvgSubGenCount() const {
if (_nodeCount == 0)
return 0.0;
else {
mpq_class q = mpq_class(_subGenSum) / _nodeCount;
return q.get_d();
}
}
double StatisticsStrategy::StatTracker::getAvgVarCount() const {
if (_nodeCount == 0)
return 0.0;
else {
mpq_class q = mpq_class(_varSum) / _nodeCount;
return q.get_d();
}
}
Slice.h
StatisticsStrategy.h
Ideal
Represents a monomial ideal with int exponents.
Definition Ideal.h:27
Ideal::getGeneratorCount
size_t getGeneratorCount() const
Definition Ideal.h:57
SliceStrategy
This class describes the interface of a strategy object for the Slice Algorithm.
Definition SliceStrategy.h:33
Slice
This class represents a slice, which is the central data structure of the Slice Algorithm.
Definition Slice.h:77
Slice::getIdeal
const Ideal & getIdeal() const
Returns for a slice .
Definition Slice.h:104
Slice::getVarCount
size_t getVarCount() const
Returns the number of variables in the ambient ring.
Definition Slice.h:96
Slice::getSubtract
Ideal & getSubtract()
Returns for a slice .
Definition Slice.h:107
StatisticsStrategy::_leafTracker
StatTracker _leafTracker
Definition StatisticsStrategy.h:91
StatisticsStrategy::setUseSimplification
virtual void setUseSimplification(bool use)
This method should only be called before calling run().
Definition StatisticsStrategy.cpp:62
StatisticsStrategy::freeSlice
virtual void freeSlice(unique_ptr< Slice > slice)
It is allowed to delete returned slices directly, but it is better to use freeSlice.
Definition StatisticsStrategy.cpp:70
StatisticsStrategy::run
virtual void run(const Ideal &ideal)
Run the Slice algorithm.
Definition StatisticsStrategy.cpp:35
StatisticsStrategy::processSlice
virtual bool processSlice(TaskEngine &tasks, unique_ptr< Slice > slice)
Process the parameter slice.
Definition StatisticsStrategy.cpp:44
StatisticsStrategy::StatisticsStrategy
StatisticsStrategy(SliceStrategy *strategy, FILE *out)
Statistics are written to out, and every call is delegated to strategy.
Definition StatisticsStrategy.cpp:24
StatisticsStrategy::getUseSimplification
virtual bool getUseSimplification() const
Definition StatisticsStrategy.cpp:66
StatisticsStrategy::_internalTracker
StatTracker _internalTracker
Definition StatisticsStrategy.h:90
StatisticsStrategy::setUseIndependence
virtual void setUseIndependence(bool use)
This method should only be called before calling run().
Definition StatisticsStrategy.cpp:58
StatisticsStrategy::_out
FILE * _out
Definition StatisticsStrategy.h:46
StatisticsStrategy::_strategy
SliceStrategy * _strategy
Definition StatisticsStrategy.h:45
StatisticsStrategy::~StatisticsStrategy
virtual ~StatisticsStrategy()
Definition StatisticsStrategy.cpp:32
TaskEngine
TaskEngine handles a list of tasks that are to be carried out.
Definition TaskEngine.h:40
stdinc.h
This header file includes common definitions and is included as the first line of code in every imple...
ASSERT
#define ASSERT(X)
Definition stdinc.h:86
StatisticsStrategy::StatTracker::printReport
void printReport(FILE *out) const
Print a report on statistics of the recorded slices to the file out.
Definition StatisticsStrategy.cpp:97
StatisticsStrategy::StatTracker::_nodeCount
mpz_class _nodeCount
Definition StatisticsStrategy.h:80
StatisticsStrategy::StatTracker::StatTracker
StatTracker(const string &title)
The title parameter indicates what is to be printed when calling printReport().
Definition StatisticsStrategy.cpp:74
StatisticsStrategy::StatTracker::_varSum
mpz_class _varSum
Definition StatisticsStrategy.h:83
StatisticsStrategy::StatTracker::getAvgVarCount
double getAvgVarCount() const
Definition StatisticsStrategy.cpp:148
StatisticsStrategy::StatTracker::_subGenSum
mpz_class _subGenSum
Definition StatisticsStrategy.h:82
StatisticsStrategy::StatTracker::_prelimSubGenCount
size_t _prelimSubGenCount
Definition StatisticsStrategy.h:77
StatisticsStrategy::StatTracker::_prelimVarCount
size_t _prelimVarCount
Definition StatisticsStrategy.h:78
StatisticsStrategy::StatTracker::_idealGenSum
mpz_class _idealGenSum
Definition StatisticsStrategy.h:81
StatisticsStrategy::StatTracker::getNodeCount
const mpz_class & getNodeCount() const
Definition StatisticsStrategy.cpp:126
StatisticsStrategy::StatTracker::getAvgSubGenCount
double getAvgSubGenCount() const
Definition StatisticsStrategy.cpp:139
StatisticsStrategy::StatTracker::commitRecord
void commitRecord()
Commit the most recent argument to preliminaryTrack permanently to the record.
Definition StatisticsStrategy.cpp:84
StatisticsStrategy::StatTracker::_nodesByGenCount
map< size_t, mpz_class > _nodesByGenCount
_nodesByGenCount[l] records how many slices have been recorded whose ideal has approximately 2^l gene...
Definition StatisticsStrategy.h:87
StatisticsStrategy::StatTracker::getAvgIdealGenCount
double getAvgIdealGenCount() const
Definition StatisticsStrategy.cpp:130
StatisticsStrategy::StatTracker::_title
string _title
Definition StatisticsStrategy.h:74
StatisticsStrategy::StatTracker::_prelimIdealGenCount
size_t _prelimIdealGenCount
Definition StatisticsStrategy.h:76
StatisticsStrategy::StatTracker::preliminaryRecord
void preliminaryRecord(const Slice &slice)
Record information about slice, but store it only until this method is next called on this object.
Definition StatisticsStrategy.cpp:78