diff --git a/csp++.cpp b/csp++.cpp deleted file mode 100644 index b6ec806..0000000 --- a/csp++.cpp +++ /dev/null @@ -1,276 +0,0 @@ -/* - * ===================================================================================== - * - * Filename: csp++.cpp - * - * Description: - * - * Version: 1.0 - * Created: 17/05/2010 09:17:13 - * Revision: none - * Compiler: gcc - * - * Author: BlackLight (http://0x00.ath.cx), - * Company: lulz - * - * ===================================================================================== - */ - - -#include -#include -#include "csp++.h" -using namespace std; -// using std::vector; - - -template -void -CSP::__init (int n, bool (*c)(vector< CSPvariable >)) -{ - variables = vector< CSPvariable >(n); - fixed = vector(n); - __default_domains = vector< vector >(n); - - for (size_t i=0; i < variables.size(); i++) { - variables[i].index = i; - variables[i].fixed = false; - // TODO Remove this line - fixed[i] = false; - } - - constraints = vector< bool (*)(std::vector< CSPvariable >) >(1); - constraint = c; - __has_default_value = false; -} - -template -CSP::CSP (int n, bool (*c)(vector< CSPvariable >)) -{ - __init (n, c); -} - -template -CSP::CSP ( int n, T default_value, bool set_value, bool (*c)(std::vector< CSPvariable >) ) -{ - __init (n, c); - - for ( size_t i=0; i < variables.size(); i++ ) { - variables[i].value = default_value; - variables[i].fixed = set_value; - // TODO Remove this line - fixed[i] = set_value; - } - - __default_value = default_value; - __has_default_value = true; -} - -template -void -CSP::setDomain (size_t index, vector domain) -{ - if (index >= variables.size()) - throw CSPexception("Index out of range"); - - variables[index].domain = domain; - __default_domains[index] = domain; -} - -template -void -CSP::setDomain (size_t index, T domain[], int size) -{ - if (index >= variables.size()) - throw CSPexception("Index out of range"); - - if (size < 0) - throw CSPexception("Invalid domain size"); - - variables[index].domain = vector(size); - - for (int i=0; i < size; i++) { - variables[index].domain[i] = domain[i]; - __default_domains[index].push_back(domain[i]); - } -} - -template -void -CSP::setConstraint ( bool (*c)(vector< CSPvariable >)) -{ - constraints = vector< bool(*)(vector< CSPvariable >) >(1); - constraint = c; -} - -template -void -CSP::setConstraint ( std::vector< bool(*)(std::vector< CSPvariable >) > c ) -{ - constraints = c; -} - -template -void -CSP::appendConstraint ( bool (*c)(vector< CSPvariable >)) -{ - constraints.push_back(c); -} - -template -void -CSP::dropConstraint ( size_t index ) -{ - if (index >= constraints.size()) - throw CSPexception("Index out of range"); - - constraints.erase( constraints.begin() + index ); -} - -template -void -CSP::restoreDomains ( void ) -{ - for (int i=0; i < variables.size(); i++) - variables[i].domain = __default_domains[i]; -} - -template -void -CSP::refreshDomains ( void ) -{ - vector< arc > arcs; - restoreDomains(); - - for (typename vector< CSPvariable >::iterator x = variables.begin(); x != variables.end(); x++) { - for (typename vector< CSPvariable >::iterator y = variables.begin(); y != variables.end(); y++) { - for (size_t i=0; i < x->domain.size(); i++) { - if ( x->fixed ) { - // TODO Remove this line - // if ( fixed[ x - variables.begin() ] ) { - if ( x->domain[i] != x->value ) - continue; - } - - x->value = x->domain[i]; - - for (size_t j=0; j < y->domain.size(); j++) { - if ( y->fixed ) { - // TODO Remove this line - // if ( fixed[ y - variables.begin() ] ) { - if ( y->domain[j] != y->value ) - continue; - } - - y->value = y->domain[j]; - bool isConfigurationValid = true; - - for ( typename std::vector< bool (*)(std::vector< CSPvariable >) >::iterator c = constraints.begin(); - c != constraints.end() && isConfigurationValid; - c++ ) { - if (!(*c)(variables)) - isConfigurationValid = false; - } - - if (isConfigurationValid) { - arc a; - a.var[0] = *x; a.var[1] = *y; - a.value[0] = x->value; a.value[1] = y->value; - arcs.push_back(a); - } - } - } - } - - vector domain = vector(); - - for (size_t i=0; i < arcs.size(); i++) { - if (arcs[i].var[0].index == x->index || - arcs[i].var[1].index == x->index) { - T value = (arcs[i].var[0].index == x->index) ? arcs[i].value[0] : arcs[i].value[1]; - bool found = false; - - for (size_t j=0; j < domain.size() && !found; j++) { - if (domain[j] == value) - found = true; - } - - if (!found) { - domain.push_back(value); - } - } - } - - sort(domain.begin(), domain.end()); - x->domain = domain; - } -} - -template -std::vector -CSP::getDomain ( size_t index ) -{ - if (index >= variables.size()) - throw CSPexception("Index out of range"); - - return variables[index].domain; -} - -template -size_t -CSP::getSize( void ) -{ - return variables.size(); -} - -template -void -CSP::setValue ( size_t index, T value ) -{ - if (index >= variables.size()) - throw CSPexception("Index out of range"); - - variables[index].value = value; - variables[index].fixed = true; - // TODO Remove this line - // fixed[index] = true; -} - -template -void -CSP::unsetValue ( size_t index ) -{ - if (index >= variables.size()) - throw CSPexception("Index out of range"); - - if (__has_default_value) - variables[index].value = __default_value; - variables[index].fixed = false; - // TODO Remove this line - // fixed[index] = false; -} - -template -bool -CSP::isSatisfiable ( void ) -{ - for ( size_t i=0; i < variables.size(); i++ ) { - if ( variables[i].domain.empty() ) - return false; - } - - return true; -} - -template -bool -CSP::hasUniqueSolution ( void ) -{ - for ( size_t i=0; i < variables.size(); i++ ) { - if (variables[i].domain.size() != 1) - return false; - } - - return true; -} - diff --git a/csp++.h b/csp++.h deleted file mode 100644 index 0c3f643..0000000 --- a/csp++.h +++ /dev/null @@ -1,206 +0,0 @@ -/* - * ===================================================================================== - * - * Filename: csp++.h - * - * Description: - * - * Version: 1.0 - * Created: 16/05/2010 23:16:42 - * Revision: none - * Compiler: gcc - * - * Author: BlackLight (http://0x00.ath.cx), - * Company: lulz - * - * ===================================================================================== - */ - -#ifndef __CSPPP_H -#define __CSPPP_H - -#include -#include - -template -struct CSPvariable { - int index; - bool fixed; - T value; - std::vector domain; -}; - -/** - * \class CSPexception csp++.h - * \brief Class for managing exception in CSP - */ -class CSPexception : public std::exception { - const char* message; - -public: - CSPexception (const char *m) { - message = m; - } - - virtual const char* what() { - return message; - } -}; - -/** - * \class CSP csp++.h - * \brief Main class for managing a CSP - */ -template -class CSP { -private: - template - struct arc { - CSPvariable var[2]; - TT value[2]; - }; - - std::vector fixed; - std::vector< CSPvariable > variables; - std::vector< bool (*)(std::vector< CSPvariable >) > constraints; - #define constraint constraints[0] - - std::vector< std::vector > __default_domains; - T __default_value; - bool __has_default_value; - static bool __default_constraint ( std::vector< CSPvariable > v ) { return true; } - void __init ( int n, bool (*c)(std::vector< CSPvariable >) ); - void restoreDomains ( void ); - -public: - /** - * \brief Class constructor - * \param n Number of variables in the CSP - * \param c Boolean function representing the constraint of the CSP - * If no constraint function is set in the constructor or - * using the applyConstraint() method, a default function - * always returning true will be used, that allows any - * domain for any variable to be valid (no constraint) - */ - CSP ( int n, bool (*c)(std::vector< CSPvariable >) = __default_constraint ); - - /** - * \brief Class constructor - * \param n Number of variables in the CSP - * \param default_value Default value for the variables in the CSP - * when initialized - * \param set_variables Decide whether mark the variables set with - * default_value as "set" or "not set" (default: not set) - * \param c Boolean function representing the constraint of the CSP - * If no constraint function is set in the constructor or - * using the applyConstraint() method, a default function - * always returning true will be used, that allows any - * domain for any variable to be valid (no constraint) - */ - CSP ( int n, T default_value, bool set_variables = false, bool (*c)(std::vector< CSPvariable >) = __default_constraint ); - - /** - * METHOD: setDomain - * \brief Set the domain for the i-th variable - * \param index Variable for which we're setting the domain - * \param domain Vector containing the possible values for that variable - */ - void setDomain ( size_t index, std::vector domain ); - - /** - * METHOD: setDomain - * \brief Set the domain for the i-th variable - * \param index Variable for which we're setting the domain - * \param domain Array containing the possible values for that variable - * \param size Size of "domain" array - */ - void setDomain ( size_t index, T domain[], int size ); - - /** - * METHOD: setConstraint - * \brief Apply the constraint to the CSP as a boolean function - * \param c Boolean function representing the constraint of the CSP - */ - void setConstraint ( bool (*c)(std::vector< CSPvariable >) ); - - /** - * METHOD: setConstraint - * \brief Apply the constraints to the CSP as vector of boolean functions - * \param c Vector containing pointers to boolean functions representing - * the constraints of the CSP - */ - void setConstraint ( std::vector< bool(*)(std::vector< CSPvariable >) > c ); - - /** - * METHOD: dropConstraint - * \brief Drops a constraint from the CSP - * \param index Index of the constraint to be dropped - */ - void dropConstraint ( size_t index ); - - /** - * METHOD: appendConstraint - * \brief Append a constraint to the list of the constraint of the CSP - * \param c A function pointer returning a boolean value representing the new constraint - */ - void appendConstraint ( bool (*c)(std::vector< CSPvariable >) ); - - /** - * METHOD: refreshDomains - * \brief Updates the domains of the variables. Any constraint or node fixed value is applied - */ - void refreshDomains( void ); - - /** - * METHOD: getDomain - * \brief Get the domain of the i-th variable - * \param index Variable for which we're going to get the domain - * \return The domain of the i-th variable as a vector of T - */ - std::vector getDomain ( size_t index ); - - /** - * METHOD: getSize - * \brief Get the number of variables in the current CSP - * \return Size of the CSP - */ - size_t getSize ( void ); - - /** - * METHOD: setValue - * \brief Set the value of a variable as a constraint - * \param index Index of the parameter to be set - * \param value Value to be set - */ - void setValue ( size_t index, T value ); - - /** - * METHOD: unsetValue - * \brief Marks a variable as not set, and if a default value was assigned - * in the CSP constructor, this value will be set. By default, unless - * specified in the constructor, all the variables are considered as - * not set - * \param index Index of the variable to be unset - */ - void unsetValue ( size_t index ); - - /** - * METHOD: isSatisfiable - * \brief Check if the current CSP, with the applied constraints, is satisfiable - * \return true if the CSP has at least a possible solution, false otherwise - */ - bool isSatisfiable ( void ); - - - /** - * METHOD: hasUniqueSolution - * \brief Check if the CSP, with the given variables, domains and constraints, - * admits a unique solution, i.e. each domain of each variable contains - * an only element - * \return true if the CSP has an only possible solution, false otherwise - */ - bool hasUniqueSolution ( void ); -}; - -#endif -