Main Page | Class Hierarchy | Class List | File List | Class Members

Constraint.h

00001 // Constraint.h -- The interface of the abstract super class of all
00002 // CSP constraints.
00003 
00004 /*
00005  * Copyright (C) 1997, 1998 Tudor Hulubei <tudor@hulubei.net>.
00006  *
00007  * This library is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU Lesser General Public License as
00009  * published by the Free Software Foundation; either version 2, or (at
00010  * your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA,
00020  * 02111-1307, USA.
00021  */
00022 
00023 /*
00024  * NOTE: This is an interal header file.
00025  * You should not attempt to use it directly.
00026  */
00027 
00028 // $Id: Constraint_8h-source.html,v 1.1 2005/05/25 12:37:18 tudor Exp $
00029 
00030 #ifndef _CSP_KERNEL_Constraint_H
00031 #define _CSP_KERNEL_Constraint_H
00032 
00033 
00034 CSP_NAMESPACE_BEGIN(csp);
00035 
00036 
00040 class CSP_API Constraint
00041 {
00042   public:
00043     typedef FastVector<Variable*>::const_iterator variable_iterator;
00044 
00048     Constraint();
00049 
00051     virtual ~Constraint();
00052 
00054     variable_iterator add(Variable& variable)
00055     {
00056         return m_variables.insert(&variable);
00057     }
00058 
00060     void remove(Variable& variable, bool recursive = false);
00061 
00063     void setId(id_t id) { m_id = id; }
00064 
00066     id_t id() const { return m_id; }
00067 
00075     virtual wstring name() const;
00076 
00083     virtual wstring color() const { return L"black"; }
00084 
00091     virtual wstring textColor() const { return L"black"; }
00092 
00098     bool isActive() const { return m_active; }
00099 
00101     void activate() { assert(!isActive()); m_active = true; }
00102 
00104     void deactivate() { assert(isActive()); m_active = false; }
00105 
00112     bool isComplete() const
00113     {
00114         return m_instantiatedVariables == m_variables.size();
00115     }
00116 
00125     bool involves(const Variable& variable) const
00126     {
00127         return find(m_variables.begin(), m_variables.end(), &variable) !=
00128             m_variables.end();
00129     }
00130 
00138     Variable& pair(const Variable& variable) const
00139     {
00140         assert(variables() == 2);
00141         assert(m_variables[0]);
00142         assert(m_variables[1]);
00143         assert(m_variables[0] != m_variables[1]);
00144         assert(&variable == m_variables[0] || &variable == m_variables[1]);
00145         return (&variable == m_variables[0]) ?
00146             *m_variables[1] : *m_variables[0];
00147     }
00148 
00154     size_t variables() const { return m_variables.size(); }
00155 
00157     size_t order() const { return variables(); }
00158 
00160     const variable_iterator beginVariables() const
00161     {
00162         return m_variables.begin();
00163     }
00164 
00166     const variable_iterator endVariables() const
00167     {
00168         return m_variables.end();
00169     }
00170 
00178     const variable_iterator secondVariable() const
00179     {
00180         assert(order() >= 2);
00181         variable_iterator first = m_variables.begin();
00182         return ++first;
00183     }
00184 
00194     Variable& findVariable(bool instantiated = false) const;
00195 
00207     bool isSatisfied() const
00208     {
00209         if (!g_debugging) m_checks++;
00210         if (check()) return true;
00211         if (!g_debugging) m_failures++;
00212         return false;
00213     }
00214 
00215     /*
00216      * This method must be implemented by all constraint classes.  It
00217      * should check whether or not the constraint is satisfied by the
00218      * current assignments of its variables.  The implementation
00219      * should work if the variables involved have had their domain
00220      * reduced to a single value, regardless of wether they have been
00221      * instantiated or not.
00222      *
00223      * @return `true' if the constraint is satisfied, `false' otherwise.
00224      */
00225     virtual bool check() const = 0;
00226 
00238     virtual bool propagate(Variable& /*modifiedVariable*/) { return true; }
00239 
00244     bool tryOut(
00245         Variable& variable0,
00246         Value& value0,
00247         Variable& variable1,
00248         Value& value1) const;
00249 
00253     virtual ulonglong validTuples() const;
00254 
00260     ulonglong maxTuples() const;
00261 
00265     long double tightness() const
00266     {
00267         return 1 - static_cast<long double>(validTuples()) / maxTuples();
00268     }
00269 
00274     bool isLoose() const;
00275 
00279     virtual ulonglong invalidTuples() const
00280     {
00281         return maxTuples() - validTuples();
00282     }
00283 
00285     ulonglong checks() const { return m_checks; }
00286 
00288     ulonglong failures() const { return m_failures; }
00289 
00295     virtual void init() { m_checks = 0; m_failures = 0; m_weight = 0; }
00296 
00298     virtual void done() {}
00299 
00304     size_t instantiatedVariables() const { return m_instantiatedVariables; }
00305 
00310     size_t uninstantiatedVariables() const
00311     {
00312         return variables() - m_instantiatedVariables;
00313     }
00314 
00316     void inc() { m_instantiatedVariables++; }
00317 
00319     void dec() { m_instantiatedVariables--; }
00320 
00322     void incrementWeight() { m_weight++; }
00323 
00325     ulonglong weight() const { return m_weight; }
00326 
00333     virtual wostream& print(wostream& wos) const;
00334 
00336     friend CSP_API wostream& operator<<(
00337         wostream& wos, const Constraint& constraint)
00338     {
00339         return constraint.print(wos);
00340     }
00341 
00342   private:
00344     Constraint(const Constraint&);
00345 
00347     Constraint& operator=(const Constraint&);
00348 
00349     // The constraint's id.
00350     int m_id;
00351 
00352     // `true' if the constraint is active, `false' otherwise.
00353     bool m_active;
00354 
00355     // The variables involved in the constraint.
00356     FastVector<Variable*> m_variables;
00357 
00358     // The number of instantiated variables in `m_variables'.
00359     size_t m_instantiatedVariables;
00360 
00367     mutable ulonglong m_checks;
00368 
00375     mutable ulonglong m_failures;
00376 
00384     ulonglong m_weight;
00385 };
00386 
00387 
00388 CSP_NAMESPACE_END(csp);
00389 
00390 
00391 #endif // _CSP_KERNEL_Constraint_H
00392 
00393 // Local Variables:
00394 // mode: C++
00395 // End:

Generated on Wed May 25 12:21:13 2005 for csp.kdevelop by  doxygen 1.3.9.1