Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added userdefined statistics #20

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
clasp 3.3.4:
* fixed: Atoms of incremental programs not always marked as frozen.
* added internal interface for user defined statistics

clasp 3.3.3: Sunday, 5th November 2017
* fixed: possible race condition in Windows alarm handling (libpotassco)
Expand Down
72 changes: 72 additions & 0 deletions clasp/clasp_facade.h
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,76 @@ class ClaspFacade : public ModelHandler {
*/
bool read();
//@}

/*!
* \name Userdefimed statistics functions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Userdefimed -> Userdefined

Copy link
Member

@pluehne pluehne Mar 25, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or, even better, “User-defined,” UserDefinedStats, and so on.

* Functions for adding userdefined statistics.
* @{ */

class UserdefinedStats {
public:
UserdefinedStats();
const char* name() const;
size_t root() const;
/// check whether an Object (of a certain type) is already attached to a map
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please be consistent in the use of comment styles and please also document any exceptions and/or preconditions.

/// if yes, target is set to the objects key
bool exists(size_t key, const std::string& name, size_t& target) const;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A more natural interface would be to use a size_t-pointer as return value and then use nullptr to indicate the absence of name.

bool exists(size_t key, const std::string& name, Potassco::Statistics_t type, size_t& target) const;
/// check whether an Object (of a certain type) is already attached to an array
/// if yes, target is set to the objects key
bool exists(size_t key, size_t index, size_t& target) const;
bool exists(size_t key, size_t index, Potassco::Statistics_t type, size_t& target) const;
/// attach a new Object to a Map or Array, or return the old one if if already exists
size_t get(size_t key, const std::string& name, Potassco::Statistics_t type);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having get() to actually create an object feeels kinda wrong.

size_t get(size_t key, size_t index, Potassco::Statistics_t type);

/// (re)sets the value of a value object
void set(size_t key, double v);

StatisticObject toStats();
private:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix indentation

void validKey(size_t key) const ;
Potassco::Statistics_t type(size_t key) const;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The private parts should be "pimpl"'ed out into the cpp file.


StatisticObject getStats(size_t k) const;
size_t toId(const std::string& s);
struct Connector {
Connector(size_t index, size_t target) : index(index), target(target) {}
size_t index; /// index to strings or integer for array
size_t target; /// key of the target that is connected here
};
//! An array of statistic objects.
class SimpleStatsVec : public PodVector<StatisticObject>::type {
public:
StatisticObject toStats() const { return StatisticObject::array(this); }
};

size_t root_; /// root map index
std::unordered_map<size_t, std::vector<Connector>> connections_; /// key -> coneections
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now please use POTASSCO_EXT_NS::unordered_map

std::vector<double> numbers_;
std::vector<std::string> strings_;
std::vector<std::pair<size_t, Potassco::Statistics_t>> mapper_;
std::vector<StatsMap> maps_;
std::vector<SimpleStatsVec> vecs_;

};


//! Sets the callback for updating userdefined statistics.
/*!
* \param cb Callback method to be called whenever an update is needed.
* \param data Userdefined callback data.
*/
typedef void(*UserdefinedStatsCallback)(UserdefinedStats*, void*);
void addUserStatisticsCallback(UserdefinedStatsCallback cb , void* data);
//@}
//! Gets the callbacks for updating userdefined statistics.
/*!
* \param cb returns the callback methods to be called whenever an update is needed.
* \param data returns userdefined callback data.
*/
void getUserStatisticsCallback(std::vector<UserdefinedStatsCallback>& cbs , std::vector<void*>& data);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please be const-correct. Furthermore, I think returning a vector of Closures, where Closure would be a pair<UserdefinedStatsCallback, void*>, would be more natural.

//@}


/*!
Expand Down Expand Up @@ -449,6 +519,8 @@ class ClaspFacade : public ModelHandler {
BuilderPtr builder_;
SummaryPtr accu_;
StatsPtr stats_; // statistics: only if requested
std::vector<UserdefinedStatsCallback> userStatsCbs_;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using a single vector storing pairs of function pointer and corresponding data.

std::vector<void*> userStatsData_;
SolvePtr solve_; // NOTE: last so that it is destroyed first;
};

Expand Down
5 changes: 5 additions & 0 deletions clasp/cli/clasp_output.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ class JsonOutput : public Output, private StatsVisitor {
virtual void visitLogicProgramStats(const Asp::LpStats& stats);
virtual void visitProblemStats(const ProblemStats& stats);
virtual void visitSolverStats(const SolverStats& stats);
virtual void visitUserStats(const StatisticObject& stats);
virtual UPtr doPrint(const OutPair& out, UPtr data);
void printStatisticObject(const StatisticObject& s);
enum ObjType { type_object, type_array };
void pushObject(const char* k = 0, ObjType t = type_object);
char popObject();
Expand All @@ -147,6 +149,7 @@ class JsonOutput : public Output, private StatsVisitor {
void printKeyValue(const char* k, double d);
void printString(const char* s, const char* sep);
void printKey(const char* k);
void printDouble(double d);
void printModel(const OutputTable& out, const Model& m, PrintLevel x);
void printCosts(const SumVec& costs, const char* name = "Costs");
void printCons(const UPair& cons);
Expand Down Expand Up @@ -225,6 +228,7 @@ class TextOutput : public Output, private StatsVisitor {
virtual void visitLogicProgramStats(const Asp::LpStats& stats);
virtual void visitProblemStats(const ProblemStats& stats);
virtual void visitSolverStats(const SolverStats& stats);
virtual void visitUserStats(const StatisticObject& stats);

virtual UPtr doPrint(const OutPair& out, UPtr data);
const char* fieldSeparator() const;
Expand All @@ -239,6 +243,7 @@ class TextOutput : public Output, private StatsVisitor {
void printSolveProgress(const Event& ev);
void printValues(const OutputTable& out, const Model& m);
void printMeta(const OutputTable& out, const Model& m);
void printStatisticObject(const StatisticObject& s, uint32 indent) const;
private:
struct SolveProgress {
int lines;
Expand Down
1 change: 1 addition & 0 deletions clasp/statistics.h
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ class StatsVisitor {
virtual void visitHcc(uint32, const ProblemStats& p, const SolverStats& s);
virtual void visitLogicProgramStats(const Asp::LpStats& stats) = 0;
virtual void visitProblemStats(const ProblemStats& stats) = 0;
virtual void visitUserStats(const StatisticObject& stats) = 0;
virtual void visitSolverStats(const SolverStats& stats) = 0;
};

Expand Down
Loading