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

RLFAP.h

00001 // RLFAP.h: The Radio Link Frequency Assignment Problem as a CSP.
00002 
00003 /*
00004  * Copyright (C) 2004-2005 Tudor Hulubei <tudor@hulubei.net>.
00005  *
00006  * This library is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU Lesser General Public License as
00008  * published by the Free Software Foundation; either version 2, or (at
00009  * your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA,
00019  * 02111-1307, USA.
00020  */
00021 
00022  /*
00023   * Radio Link Frequency Assignment Benchmarks as provided by the
00024   * Centre d'Electronique de l'Armement (France).  See B. Cabon,
00025   * S. de Givry, L. Lobjois, T. Schiex and J.P. Warners, CONSTRAINTS
00026   * journal. 1998.
00027   */
00028 
00029 // $Id: RLFAP_8h-source.html,v 1.1 2005/05/25 12:37:18 tudor Exp $
00030 
00031 #ifndef _CSP_EXAMPLES_RLFAP_H
00032 #define _CSP_EXAMPLES_RLFAP_H
00033 
00034 
00035 #ifndef __cplusplus
00036 #error Must use C++ for the type RLFAP.
00037 #endif
00038 
00039 
00040 #include "values/Int.h"
00041 #include "variables/Int.h"
00042 
00043 
00044 CSP_NAMESPACE_BEGIN(RLFAP)
00045 
00046 
00047 using namespace std;
00048 
00049 
00050 #ifdef __GNUG__
00051 using namespace __gnu_cxx;
00052 #endif
00053 
00054 #if defined(_MSC_VER) && (_MSC_VER == 1310)
00055 using namespace stdext;
00056 #endif
00057 
00058 
00059 typedef csp::values::Int Value;
00060 typedef csp::Domain Domain;
00061 class Variable;
00062 
00063 
00064 class DistanceConstraint :
00065     public csp::Constraint
00066 {
00067   public:
00068     DistanceConstraint(int distance) :
00069         csp::Constraint(),
00070         m_distance(distance) {}
00071 
00072     virtual bool check() const;
00073     virtual wstring name() const { return L">"; }
00074     virtual wstring color() const { return L"red"; }
00075     virtual wstring textColor() const { return L"red"; }
00076 
00077   private:
00078     int m_distance;
00079 };
00080 
00081 
00082 class EqualityConstraint :
00083     public csp::Constraint
00084 {
00085   public:
00086     EqualityConstraint(int distance) :
00087         csp::Constraint(),
00088         m_distance(distance) {}
00089 
00090     virtual bool check() const;
00091     virtual wstring name() const { return L"="; }
00092     virtual wstring color() const { return L"blue"; }
00093     virtual wstring textColor() const { return L"blue"; }
00094 
00095   private:
00096     int m_distance;
00097 };
00098 
00099 
00100 class Problem :
00101     public csp::Problem
00102 {
00103   public:
00104     Problem(
00105         csp::RandomSequence& randomSequence,
00106         const wstring& domainsFileName,
00107         const wstring& variablesFileName,
00108         const wstring& constraintsFileName);
00109     Problem(const Problem& problem);
00110     virtual ~Problem();
00111 
00112     virtual wstring name() const { return L"RLFAP"; }
00113 
00114     void addDistanceConstraint(int index0, int index1, int distance);
00115     void addEqualityConstraint(int index0, int index1, int distance);
00116 
00117     virtual wostream& printSolution(wostream& wos) const;
00118 
00119     virtual wostream& print(wostream& wos) const;
00120 
00121   private:
00122     void constructor(
00123         const wstring& domainsFileName,
00124         const wstring& variablesFileName,
00125         const wstring& constraintsFileName);
00126 
00127     void assignBackdoorVariable(csp::id_t variableId, csp::id_t valueId);
00128     void assignBackdoor();
00129     void unassignBackdoor();
00130 
00131     wstring m_domainsFileName;
00132     wstring m_variablesFileName;
00133     wstring m_constraintsFileName;
00134 
00135     // We keep the nodes in this vector just to be able to find them
00136     // quickly based on their index.
00137     vector<Variable*> m_nodes;
00138 
00139     // Convert a variable index into its position in m_nodes.
00140     hash_map<int, int> m_position;
00141 };
00142 
00143 
00144 class Variable :
00145     public csp::variables::Int
00146 {
00147   public:
00148     Variable(
00149         const Problem& problem,
00150         const vector<csp::values::Int>& array,
00151         const wstring& name);
00152 
00153     virtual wstring name() const { return m_name; }
00154     int index() const { return m_index; }
00155     void setColor(const wstring& color) { m_color = color; }
00156 
00157     virtual wstring color() const
00158     {
00159         // It's important that the isBackdoor test comes last, as both
00160         // branching variables (those involved in branching
00161         // assignments) and backdoor keys are in the backdoor.
00162         if (m_isBranching)
00163             return L"pink";
00164         if (m_isBackdoorKey)
00165             return L"orange";
00166         if (m_isBackdoor)
00167             return L"red";
00168         return m_color;
00169     }
00170     virtual wstring textColor() const { return L"black"; }
00171 
00172     virtual wstring shape() const
00173     {
00174         if (m_isBackdoor || m_isBackdoorKey || m_isBranching)
00175             return L"rectangle";
00176         return csp::Variable::shape();
00177     }
00178 
00179     virtual void setBackdoorStatus(bool status) { m_isBackdoor = status; }
00180     virtual void setBackdoorKeyStatus(bool status) { m_isBackdoorKey = status; }
00181     virtual void setBranchingStatus(bool status) { m_isBranching = status; }
00182 
00183   private:
00184     wstring m_name;
00185     int m_index;
00186     wstring m_color;
00187     bool m_isBackdoor;
00188     bool m_isBackdoorKey;
00189     bool m_isBranching;
00190 };
00191 
00192 
00193 CSP_NAMESPACE_END(RLFAP)
00194 
00195 
00196 #endif // _CSP_EXAMPLES_RLFAP_H
00197 
00198 // Local Variables:
00199 // mode: C++
00200 // End:

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