diff --git a/csp++/csp++-def.h b/csp++/csp++-def.h index 07fa431..9888cef 100644 --- a/csp++/csp++-def.h +++ b/csp++/csp++-def.h @@ -6,7 +6,7 @@ * Description: Header file containing the CSP class definition. Never include this * file directly in your sources. Include csp++.h instead. * - * Version: 0.1 + * Version: 0.1.1 * Created: 16/05/2010 23:16:42 * Revision: none * Compiler: gcc @@ -25,7 +25,7 @@ #error "csp++-def.h must not be included directly - include csp++.h instead" #endif -#define __CSPPP_VERSION "0.1" +#define __CSPPP_VERSION "0.1.1" #include #include @@ -172,21 +172,29 @@ public: /** * \brief Updates the domains of the variables. Any constraint or node fixed value is applied + * \param max_iterations Maximum number of iterations, after which the function will quit. This + * can be used to break endless loops in domains assignations. */ - void refreshDomains( void ); + void refreshDomains ( void ); + + /** + * \brief Get, if it exists, the solution of the CSP, calling refreshDomains until a fixed point + * is reached + */ + void solve ( size_t maxIterations = 0 ); /** * \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 ); + std::vector domain ( size_t index ); /** * \brief Get the number of variables in the current CSP * \return Size of the CSP */ - size_t getSize ( void ); + size_t size ( void ); /** * \brief Set the value of a variable as a constraint diff --git a/csp++/csp++.cpp b/csp++/csp++.cpp index ba6a4e2..42ee57d 100644 --- a/csp++/csp++.cpp +++ b/csp++/csp++.cpp @@ -5,7 +5,7 @@ * * Description: * - * Version: 0.1 + * Version: 0.1.1 * Created: 17/05/2010 09:17:13 * Revision: none * Compiler: gcc @@ -209,7 +209,7 @@ CSP::refreshDomains ( void ) template std::vector -CSP::getDomain ( size_t index ) +CSP::domain ( size_t index ) { if (index >= variables.size()) throw CSPexception("Index out of range"); @@ -219,7 +219,7 @@ CSP::getDomain ( size_t index ) template size_t -CSP::getSize( void ) +CSP::size( void ) { return variables.size(); } @@ -275,9 +275,9 @@ template void CSP::assignUniqueDomains ( void ) { - for ( int i=0; i < getSize(); i++ ) { - if (getDomain(i).size() == 1) - setValue( i, getDomain(i)[0] ); + for ( int i=0; i < size(); i++ ) { + if (domain(i).size() == 1) + setValue( i, domain(i)[0] ); } } @@ -301,3 +301,41 @@ CSP::value ( size_t index ) return variables[index].value; } +template +void +CSP::solve ( size_t max_iterations ) +{ + bool changed = false; + size_t steps = 1; + vector< vector > oldDomains(size()); + + do { + if (max_iterations != 0) { + if (steps++ > max_iterations) + break; + } + + for ( size_t i=0; i < size(); i++ ) { + oldDomains[i] = variables[i].domain; + } + + refreshDomains(); + assignUniqueDomains(); + + if (hasUniqueSolution()) + break; + + changed = false; + + for ( size_t i=0; i < size(); i++ ) { + if (domain(i).size() != oldDomains[i].size()) + changed = true; + + for ( size_t j=0; j < domain(i).size() && !changed; j++ ) { + if ( domain(i)[j] != oldDomains[i][j] ) + changed = true; + } + } + } while (changed); +} + diff --git a/csp++/csp++.h b/csp++/csp++.h index ea40e5f..a33d972 100644 --- a/csp++/csp++.h +++ b/csp++/csp++.h @@ -5,7 +5,7 @@ * * Description: * - * Version: 0.1 + * Version: 0.1.1 * Created: 23/05/2010 23:27:43 * Revision: none * Compiler: gcc diff --git a/doc/html/annotated.html b/doc/html/annotated.html index 1b38619..96e873b 100644 --- a/doc/html/annotated.html +++ b/doc/html/annotated.html @@ -63,7 +63,7 @@ var searchBox = new SearchBox("searchBox", "search",false,'Search'); -
Generated on Mon May 24 01:58:01 2010 for libCSP++ by  +
Generated on Thu May 27 11:41:31 2010 for libCSP++ by  doxygen 1.6.3
diff --git a/doc/html/classCSP-members.html b/doc/html/classCSP-members.html index 43f6cec..f8c2586 100644 --- a/doc/html/classCSP-members.html +++ b/doc/html/classCSP-members.html @@ -49,9 +49,8 @@ var searchBox = new SearchBox("searchBox", "search",false,'Search'); CSP()CSP< T > [inline] CSP(int n, bool(*c)(std::vector< CSPvariable< T > >)=__default_constraint)CSP< T > CSP(int n, T default_value, bool set_variables=false, bool(*c)(std::vector< CSPvariable< T > >)=__default_constraint)CSP< T > [inline] + domain(size_t index)CSP< T > [inline] dropConstraint(size_t index)CSP< T > [inline] - getDomain(size_t index)CSP< T > [inline] - getSize(void)CSP< T > [inline] hasUniqueSolution(void)CSP< T > [inline] isSatisfiable(void)CSP< T > [inline] isSet(size_t i)CSP< T > [inline] @@ -61,6 +60,8 @@ var searchBox = new SearchBox("searchBox", "search",false,'Search'); setDomain(size_t index, std::vector< T > domain)CSP< T > setDomain(size_t index, T domain[], int size)CSP< T > [inline] setValue(size_t index, T value)CSP< T > [inline] + size(void)CSP< T > [inline] + solve(size_t maxIterations=0)CSP< T > [inline] unsetValue(size_t index)CSP< T > [inline] value(size_t i)CSP< T > [inline] @@ -78,7 +79,7 @@ var searchBox = new SearchBox("searchBox", "search",false,'Search'); -
Generated on Mon May 24 01:58:02 2010 for libCSP++ by  +
Generated on Thu May 27 11:41:31 2010 for libCSP++ by  doxygen 1.6.3
diff --git a/doc/html/classCSP.html b/doc/html/classCSP.html index 30231d0..4d2240b 100644 --- a/doc/html/classCSP.html +++ b/doc/html/classCSP.html @@ -73,13 +73,15 @@ var searchBox = new SearchBox("searchBox", "search",false,'Search');  Drops a constraint from the CSP.
void appendConstraint (bool(*c)(std::vector< CSPvariable< T > >))  Append a constraint to the list of the constraint of the CSP.
- -void refreshDomains (void) - Updates the domains of the variables. Any constraint or node fixed value is applied.
-std::vector< T > getDomain (size_t index) - Get the domain of the i-th variable.
-size_t getSize (void) - Get the number of variables in the current CSP.
+void refreshDomains (void) + Updates the domains of the variables. Any constraint or node fixed value is applied.
+ +void solve (size_t maxIterations=0) + Get, if it exists, the solution of the CSP, calling refreshDomains until a fixed point is reached.
+std::vector< T > domain (size_t index) + Get the domain of the i-th variable.
+size_t size (void) + Get the number of variables in the current CSP.
void setValue (size_t index, T value)  Set the value of a variable as a constraint.
void unsetValue (size_t index) @@ -219,6 +221,35 @@ template<class T > + + + +
+
+
+template<class T >
+ + + + + + + + + +
std::vector< T > CSP< T >::domain (size_t  index )  [inline]
+
+
+ +

Get the domain of the i-th variable.

+
Parameters:
+ + +
index Variable for which we're going to get the domain
+
+
+
Returns:
The domain of the i-th variable as a vector of T
+
@@ -247,58 +278,6 @@ template<class T > - - - -
-
-
-template<class T >
- - - - - - - - - -
std::vector< T > CSP< T >::getDomain (size_t  index )  [inline]
-
-
- -

Get the domain of the i-th variable.

-
Parameters:
- - -
index Variable for which we're going to get the domain
-
-
-
Returns:
The domain of the i-th variable as a vector of T
- -
-
- -
-
-
-template<class T >
- - - - - - - - - -
size_t CSP< T >::getSize (void  )  [inline]
-
-
- -

Get the number of variables in the current CSP.

-
Returns:
Size of the CSP
-
@@ -374,6 +353,34 @@ template<class T >
Returns:
true if the i-th variable has a fixed value, false otherwise
+ + + +
+
+
+template<class T >
+ + + + + + + + + +
void CSP< T >::refreshDomains (void  )  [inline]
+
+
+ +

Updates the domains of the variables. Any constraint or node fixed value is applied.

+
Parameters:
+ + +
max_iterations Maximum number of iterations, after which the function will quit. This can be used to break endless loops in domains assignations.
+
+
+
@@ -551,6 +558,29 @@ template<class T > + + + +
+
+
+template<class T >
+ + + + + + + + + +
size_t CSP< T >::size (void  )  [inline]
+
+
+ +

Get the number of variables in the current CSP.

+
Returns:
Size of the CSP
+
@@ -629,7 +659,7 @@ template<class T > -
Generated on Mon May 24 01:58:02 2010 for libCSP++ by  +
Generated on Thu May 27 11:41:31 2010 for libCSP++ by  doxygen 1.6.3
diff --git a/doc/html/classCSPexception-members.html b/doc/html/classCSPexception-members.html index 3b1aeea..deb616e 100644 --- a/doc/html/classCSPexception-members.html +++ b/doc/html/classCSPexception-members.html @@ -61,7 +61,7 @@ var searchBox = new SearchBox("searchBox", "search",false,'Search'); -
Generated on Mon May 24 01:58:02 2010 for libCSP++ by  +
Generated on Thu May 27 11:41:31 2010 for libCSP++ by  doxygen 1.6.3
diff --git a/doc/html/classCSPexception.html b/doc/html/classCSPexception.html index 214da05..df19d4b 100644 --- a/doc/html/classCSPexception.html +++ b/doc/html/classCSPexception.html @@ -105,7 +105,7 @@ virtual const char *  -
Generated on Mon May 24 01:58:02 2010 for libCSP++ by  +
Generated on Thu May 27 11:41:31 2010 for libCSP++ by  doxygen 1.6.3
diff --git a/doc/html/classes.html b/doc/html/classes.html index 21b6686..f34098a 100644 --- a/doc/html/classes.html +++ b/doc/html/classes.html @@ -62,7 +62,7 @@ var searchBox = new SearchBox("searchBox", "search",false,'Search'); -
Generated on Mon May 24 01:58:01 2010 for libCSP++ by  +
Generated on Thu May 27 11:41:31 2010 for libCSP++ by  doxygen 1.6.3
diff --git a/doc/html/csp_09_09-def_8h_source.html b/doc/html/csp_09_09-def_8h_source.html index bf0848e..23a9629 100644 --- a/doc/html/csp_09_09-def_8h_source.html +++ b/doc/html/csp_09_09-def_8h_source.html @@ -48,112 +48,117 @@ var searchBox = new SearchBox("searchBox", "search",false,'Search'); 00006 * Description: Header file containing the CSP class definition. Never include this 00007 * file directly in your sources. Include csp++.h instead. 00008 * -00009 * Version: 1.0 +00009 * Version: 0.1 00010 * Created: 16/05/2010 23:16:42 00011 * Revision: none 00012 * Compiler: gcc 00013 * 00014 * Author: BlackLight (http://0x00.ath.cx), <blacklight@autistici.org> -00015 * Company: lulz -00016 * -00017 * ===================================================================================== -00018 */ -00019 -00020 #ifndef __CSPPP_H -00021 #define __CSPPP_H -00022 -00023 #ifndef __CSPPP_CPP -00024 #error "csp++-def.h must not be included directly - include csp++.h instead" -00025 #endif -00026 -00027 #include <vector> -00028 #include <exception> -00029 -00034 template<class T> -00035 struct CSPvariable { -00037 int index; -00038 -00040 bool fixed; +00015 * Licence: GNU GPL v.3 +00016 * Company: lulz +00017 * +00018 * ===================================================================================== +00019 */ +00020 +00021 #ifndef __CSPPP_H +00022 #define __CSPPP_H +00023 +00024 #ifndef __CSPPP_CPP +00025 #error "csp++-def.h must not be included directly - include csp++.h instead" +00026 #endif +00027 +00028 #define __CSPPP_VERSION "0.1" +00029 +00030 #include <vector> +00031 #include <exception> +00032 +00037 template<class T> +00038 struct CSPvariable { +00040 int index; 00041 -00043 T value; +00043 bool fixed; 00044 -00046 std::vector<T> domain; -00047 }; -00048 -00053 class CSPexception : public std::exception { -00054 const char* message; -00055 -00056 public: -00061 CSPexception (const char *m) { -00062 message = m; -00063 } -00064 -00068 virtual const char* what() { -00069 return message; -00070 } -00071 }; -00072 -00077 template<class T> -00078 class CSP { -00079 private: -00080 template<class TT> -00081 struct arc { -00082 CSPvariable<TT> var[2]; -00083 TT value[2]; -00084 }; -00085 -00086 std::vector< CSPvariable<T> > variables; -00087 std::vector< bool (*)(std::vector< CSPvariable<T> >) > constraints; -00088 #define constraint constraints[0] -00089 -00090 std::vector< std::vector<T> > __default_domains; -00091 T __default_value; -00092 bool __has_default_value; -00093 static bool __default_constraint ( std::vector< CSPvariable<T> > v ) { return true; } -00094 void __init ( int n, bool (*c)(std::vector< CSPvariable<T> >) ); -00095 void restoreDomains ( void ); -00096 -00097 public: -00102 CSP() {} -00103 -00113 CSP ( int n, bool (*c)(std::vector< CSPvariable<T> >) = __default_constraint ); -00114 -00128 CSP ( int n, T default_value, bool set_variables = false, bool (*c)(std::vector< CSPvariable<T> >) = __default_constraint ); -00129 -00135 void setDomain ( size_t index, std::vector<T> domain ); -00136 -00143 void setDomain ( size_t index, T domain[], int size ); -00144 -00149 void setConstraint ( bool (*c)(std::vector< CSPvariable<T> >) ); -00150 -00156 void setConstraint ( std::vector< bool(*)(std::vector< CSPvariable<T> >) > c ); -00157 -00162 void dropConstraint ( size_t index ); -00163 -00168 void appendConstraint ( bool (*c)(std::vector< CSPvariable<T> >) ); -00169 -00173 void refreshDomains( void ); -00174 -00180 std::vector<T> getDomain ( size_t index ); -00181 -00186 size_t getSize ( void ); -00187 -00193 void setValue ( size_t index, T value ); -00194 -00202 void unsetValue ( size_t index ); -00203 -00208 bool isSatisfiable ( void ); -00209 -00216 bool hasUniqueSolution ( void ); -00217 -00223 void assignUniqueDomains ( void ); -00229 bool isSet ( size_t i ); -00230 -00240 T value ( size_t i ); -00241 }; -00242 -00243 #endif -00244 +00046 T value; +00047 +00049 std::vector<T> domain; +00050 }; +00051 +00056 class CSPexception : public std::exception { +00057 const char* message; +00058 +00059 public: +00064 CSPexception (const char *m) { +00065 message = m; +00066 } +00067 +00071 virtual const char* what() { +00072 return message; +00073 } +00074 }; +00075 +00080 template<class T> +00081 class CSP { +00082 private: +00083 template<class TT> +00084 struct arc { +00085 CSPvariable<TT> var[2]; +00086 TT value[2]; +00087 }; +00088 +00089 std::vector< CSPvariable<T> > variables; +00090 std::vector< bool (*)(std::vector< CSPvariable<T> >) > constraints; +00091 #define constraint constraints[0] +00092 +00093 std::vector< std::vector<T> > __default_domains; +00094 T __default_value; +00095 bool __has_default_value; +00096 static bool __default_constraint ( std::vector< CSPvariable<T> > v ) { return true; } +00097 void __init ( int n, bool (*c)(std::vector< CSPvariable<T> >) ); +00098 void restoreDomains ( void ); +00099 +00100 public: +00105 CSP() {} +00106 +00116 CSP ( int n, bool (*c)(std::vector< CSPvariable<T> >) = __default_constraint ); +00117 +00131 CSP ( int n, T default_value, bool set_variables = false, bool (*c)(std::vector< CSPvariable<T> >) = __default_constraint ); +00132 +00138 void setDomain ( size_t index, std::vector<T> domain ); +00139 +00146 void setDomain ( size_t index, T domain[], int size ); +00147 +00152 void setConstraint ( bool (*c)(std::vector< CSPvariable<T> >) ); +00153 +00159 void setConstraint ( std::vector< bool(*)(std::vector< CSPvariable<T> >) > c ); +00160 +00165 void dropConstraint ( size_t index ); +00166 +00171 void appendConstraint ( bool (*c)(std::vector< CSPvariable<T> >) ); +00172 +00178 void refreshDomains ( void ); +00179 +00184 void solve ( size_t maxIterations = 0 ); +00185 +00191 std::vector<T> domain ( size_t index ); +00192 +00197 size_t size ( void ); +00198 +00204 void setValue ( size_t index, T value ); +00205 +00213 void unsetValue ( size_t index ); +00214 +00219 bool isSatisfiable ( void ); +00220 +00227 bool hasUniqueSolution ( void ); +00228 +00234 void assignUniqueDomains ( void ); +00240 bool isSet ( size_t i ); +00241 +00251 T value ( size_t i ); +00252 }; +00253 +00254 #endif +00255
-
Generated on Mon May 24 01:58:01 2010 for libCSP++ by  +
Generated on Thu May 27 11:41:31 2010 for libCSP++ by  doxygen 1.6.3
diff --git a/doc/html/csp_09_09_8h_source.html b/doc/html/csp_09_09_8h_source.html index c04c37c..d69ee26 100644 --- a/doc/html/csp_09_09_8h_source.html +++ b/doc/html/csp_09_09_8h_source.html @@ -47,27 +47,28 @@ var searchBox = new SearchBox("searchBox", "search",false,'Search'); 00005 * 00006 * Description: 00007 * -00008 * Version: 1.0 +00008 * Version: 0.1 00009 * Created: 23/05/2010 23:27:43 00010 * Revision: none 00011 * Compiler: gcc 00012 * 00013 * Author: BlackLight (http://0x00.ath.cx), <blacklight@autistici.org> -00014 * Company: lulz -00015 * -00016 * ===================================================================================== -00017 */ -00018 -00019 // Yes, I know it looks stupid, but ask C++ standard developers about this. -00020 // C++ can't expand template class code until a type is explicitely specified. -00021 // This is nearly like not using templates at all, but join me in asking C++ -00022 // compilers developers to massively allow the export keyword and change the -00023 // way templates are managed. Until that moment, you must always include the -00024 // .cpp file of the library inside your source code as well, and specify later -00025 // the types you're going to use this template class for -00026 -00027 #include "csp++.cpp" -00028 +00014 * Licence: GNU GPL v.3 +00015 * Company: lulz +00016 * +00017 * ===================================================================================== +00018 */ +00019 +00020 // Yes, I know it looks stupid, but ask C++ standard developers about this. +00021 // C++ can't expand template class code until a type is explicitely specified. +00022 // This is nearly like not using templates at all, but join me in asking C++ +00023 // compilers developers to massively allow the export keyword and change the +00024 // way templates are managed. Until that moment, you must always include the +00025 // .cpp file of the library inside your source code as well, and specify later +00026 // the types you're going to use this template class for +00027 +00028 #include "csp++.cpp" +00029
-
Generated on Mon May 24 01:58:01 2010 for libCSP++ by  +
Generated on Thu May 27 11:41:31 2010 for libCSP++ by  doxygen 1.6.3
diff --git a/doc/html/files.html b/doc/html/files.html index 17d73a3..c197545 100644 --- a/doc/html/files.html +++ b/doc/html/files.html @@ -61,7 +61,7 @@ var searchBox = new SearchBox("searchBox", "search",false,'Search'); -
Generated on Mon May 24 01:58:02 2010 for libCSP++ by  +
Generated on Thu May 27 11:41:31 2010 for libCSP++ by  doxygen 1.6.3
diff --git a/doc/html/functions.html b/doc/html/functions.html index 9b6a007..366b581 100644 --- a/doc/html/functions.html +++ b/doc/html/functions.html @@ -65,6 +65,7 @@ Here is a list of all documented class members with links to the class documenta
  • domain : CSPvariable< T > +, CSP< T >
  • dropConstraint() : CSP< T > @@ -72,12 +73,6 @@ Here is a list of all documented class members with links to the class documenta
  • fixed : CSPvariable< T >
  • -
  • getDomain() -: CSP< T > -
  • -
  • getSize() -: CSP< T > -
  • hasUniqueSolution() : CSP< T >
  • @@ -102,6 +97,12 @@ Here is a list of all documented class members with links to the class documenta
  • setValue() : CSP< T >
  • +
  • size() +: CSP< T > +
  • +
  • solve() +: CSP< T > +
  • unsetValue() : CSP< T >
  • @@ -128,7 +129,7 @@ Here is a list of all documented class members with links to the class documenta -
    Generated on Mon May 24 01:58:01 2010 for libCSP++ by  +
    Generated on Thu May 27 11:41:31 2010 for libCSP++ by  doxygen 1.6.3
    diff --git a/doc/html/functions_func.html b/doc/html/functions_func.html index 7ca69f6..b67385a 100644 --- a/doc/html/functions_func.html +++ b/doc/html/functions_func.html @@ -63,15 +63,12 @@ var searchBox = new SearchBox("searchBox", "search",false,'Search');
  • CSPexception() : CSPexception
  • +
  • domain() +: CSP< T > +
  • dropConstraint() : CSP< T >
  • -
  • getDomain() -: CSP< T > -
  • -
  • getSize() -: CSP< T > -
  • hasUniqueSolution() : CSP< T >
  • @@ -93,6 +90,12 @@ var searchBox = new SearchBox("searchBox", "search",false,'Search');
  • setValue() : CSP< T >
  • +
  • size() +: CSP< T > +
  • +
  • solve() +: CSP< T > +
  • unsetValue() : CSP< T >
  • @@ -118,7 +121,7 @@ var searchBox = new SearchBox("searchBox", "search",false,'Search'); -
    Generated on Mon May 24 01:58:01 2010 for libCSP++ by  +
    Generated on Thu May 27 11:41:31 2010 for libCSP++ by  doxygen 1.6.3
    diff --git a/doc/html/functions_vars.html b/doc/html/functions_vars.html index b8c4548..d09738c 100644 --- a/doc/html/functions_vars.html +++ b/doc/html/functions_vars.html @@ -79,7 +79,7 @@ var searchBox = new SearchBox("searchBox", "search",false,'Search'); -
    Generated on Mon May 24 01:58:01 2010 for libCSP++ by  +
    Generated on Thu May 27 11:41:31 2010 for libCSP++ by  doxygen 1.6.3
    diff --git a/doc/html/index.html b/doc/html/index.html index e33ed5d..9ccc6ff 100644 --- a/doc/html/index.html +++ b/doc/html/index.html @@ -52,7 +52,7 @@ var searchBox = new SearchBox("searchBox", "search",false,'Search'); -
    Generated on Mon May 24 01:58:01 2010 for libCSP++ by  +
    Generated on Thu May 27 11:41:31 2010 for libCSP++ by  doxygen 1.6.3
    diff --git a/doc/html/search/all_64.html b/doc/html/search/all_64.html index 9b38704..cae1c68 100644 --- a/doc/html/search/all_64.html +++ b/doc/html/search/all_64.html @@ -9,8 +9,11 @@
    Loading...
    - domain - CSPvariable + domain +
    diff --git a/doc/html/search/all_73.html b/doc/html/search/all_73.html index feac672..5726fc4 100644 --- a/doc/html/search/all_73.html +++ b/doc/html/search/all_73.html @@ -31,6 +31,18 @@ CSP
    +
    +
    + size + CSP +
    +
    +
    +
    + solve + CSP +
    +
    Searching...
    No Matches