mirror of
https://github.com/BlackLight/libCSP--.git
synced 2024-11-27 22:25:12 +01:00
Removing old source files
This commit is contained in:
parent
290f143889
commit
ab2fac600b
2 changed files with 0 additions and 482 deletions
276
csp++.cpp
276
csp++.cpp
|
@ -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), <blacklight@autistici.org>
|
|
||||||
* Company: lulz
|
|
||||||
*
|
|
||||||
* =====================================================================================
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
#include <iostream>
|
|
||||||
#include <algorithm>
|
|
||||||
#include "csp++.h"
|
|
||||||
using namespace std;
|
|
||||||
// using std::vector;
|
|
||||||
|
|
||||||
|
|
||||||
template<class T>
|
|
||||||
void
|
|
||||||
CSP<T>::__init (int n, bool (*c)(vector< CSPvariable<T> >))
|
|
||||||
{
|
|
||||||
variables = vector< CSPvariable<T> >(n);
|
|
||||||
fixed = vector<bool>(n);
|
|
||||||
__default_domains = vector< vector<T> >(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<T> >) >(1);
|
|
||||||
constraint = c;
|
|
||||||
__has_default_value = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<class T>
|
|
||||||
CSP<T>::CSP (int n, bool (*c)(vector< CSPvariable<T> >))
|
|
||||||
{
|
|
||||||
__init (n, c);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<class T>
|
|
||||||
CSP<T>::CSP ( int n, T default_value, bool set_value, bool (*c)(std::vector< CSPvariable<T> >) )
|
|
||||||
{
|
|
||||||
__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<class T>
|
|
||||||
void
|
|
||||||
CSP<T>::setDomain (size_t index, vector<T> domain)
|
|
||||||
{
|
|
||||||
if (index >= variables.size())
|
|
||||||
throw CSPexception("Index out of range");
|
|
||||||
|
|
||||||
variables[index].domain = domain;
|
|
||||||
__default_domains[index] = domain;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<class T>
|
|
||||||
void
|
|
||||||
CSP<T>::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<T>(size);
|
|
||||||
|
|
||||||
for (int i=0; i < size; i++) {
|
|
||||||
variables[index].domain[i] = domain[i];
|
|
||||||
__default_domains[index].push_back(domain[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template<class T>
|
|
||||||
void
|
|
||||||
CSP<T>::setConstraint ( bool (*c)(vector< CSPvariable<T> >))
|
|
||||||
{
|
|
||||||
constraints = vector< bool(*)(vector< CSPvariable<T > >) >(1);
|
|
||||||
constraint = c;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<class T>
|
|
||||||
void
|
|
||||||
CSP<T>::setConstraint ( std::vector< bool(*)(std::vector< CSPvariable<T> >) > c )
|
|
||||||
{
|
|
||||||
constraints = c;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<class T>
|
|
||||||
void
|
|
||||||
CSP<T>::appendConstraint ( bool (*c)(vector< CSPvariable<T> >))
|
|
||||||
{
|
|
||||||
constraints.push_back(c);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<class T>
|
|
||||||
void
|
|
||||||
CSP<T>::dropConstraint ( size_t index )
|
|
||||||
{
|
|
||||||
if (index >= constraints.size())
|
|
||||||
throw CSPexception("Index out of range");
|
|
||||||
|
|
||||||
constraints.erase( constraints.begin() + index );
|
|
||||||
}
|
|
||||||
|
|
||||||
template<class T>
|
|
||||||
void
|
|
||||||
CSP<T>::restoreDomains ( void )
|
|
||||||
{
|
|
||||||
for (int i=0; i < variables.size(); i++)
|
|
||||||
variables[i].domain = __default_domains[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
template<class T>
|
|
||||||
void
|
|
||||||
CSP<T>::refreshDomains ( void )
|
|
||||||
{
|
|
||||||
vector< arc<T> > arcs;
|
|
||||||
restoreDomains();
|
|
||||||
|
|
||||||
for (typename vector< CSPvariable<T> >::iterator x = variables.begin(); x != variables.end(); x++) {
|
|
||||||
for (typename vector< CSPvariable<T> >::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<T> >) >::iterator c = constraints.begin();
|
|
||||||
c != constraints.end() && isConfigurationValid;
|
|
||||||
c++ ) {
|
|
||||||
if (!(*c)(variables))
|
|
||||||
isConfigurationValid = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isConfigurationValid) {
|
|
||||||
arc<T> a;
|
|
||||||
a.var[0] = *x; a.var[1] = *y;
|
|
||||||
a.value[0] = x->value; a.value[1] = y->value;
|
|
||||||
arcs.push_back(a);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
vector<T> domain = vector<T>();
|
|
||||||
|
|
||||||
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<class T>
|
|
||||||
std::vector<T>
|
|
||||||
CSP<T>::getDomain ( size_t index )
|
|
||||||
{
|
|
||||||
if (index >= variables.size())
|
|
||||||
throw CSPexception("Index out of range");
|
|
||||||
|
|
||||||
return variables[index].domain;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<class T>
|
|
||||||
size_t
|
|
||||||
CSP<T>::getSize( void )
|
|
||||||
{
|
|
||||||
return variables.size();
|
|
||||||
}
|
|
||||||
|
|
||||||
template<class T>
|
|
||||||
void
|
|
||||||
CSP<T>::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<class T>
|
|
||||||
void
|
|
||||||
CSP<T>::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<class T>
|
|
||||||
bool
|
|
||||||
CSP<T>::isSatisfiable ( void )
|
|
||||||
{
|
|
||||||
for ( size_t i=0; i < variables.size(); i++ ) {
|
|
||||||
if ( variables[i].domain.empty() )
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<class T>
|
|
||||||
bool
|
|
||||||
CSP<T>::hasUniqueSolution ( void )
|
|
||||||
{
|
|
||||||
for ( size_t i=0; i < variables.size(); i++ ) {
|
|
||||||
if (variables[i].domain.size() != 1)
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
206
csp++.h
206
csp++.h
|
@ -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), <blacklight@autistici.org>
|
|
||||||
* Company: lulz
|
|
||||||
*
|
|
||||||
* =====================================================================================
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef __CSPPP_H
|
|
||||||
#define __CSPPP_H
|
|
||||||
|
|
||||||
#include <vector>
|
|
||||||
#include <exception>
|
|
||||||
|
|
||||||
template<class T>
|
|
||||||
struct CSPvariable {
|
|
||||||
int index;
|
|
||||||
bool fixed;
|
|
||||||
T value;
|
|
||||||
std::vector<T> 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 T>
|
|
||||||
class CSP {
|
|
||||||
private:
|
|
||||||
template<class TT>
|
|
||||||
struct arc {
|
|
||||||
CSPvariable<TT> var[2];
|
|
||||||
TT value[2];
|
|
||||||
};
|
|
||||||
|
|
||||||
std::vector<bool> fixed;
|
|
||||||
std::vector< CSPvariable<T> > variables;
|
|
||||||
std::vector< bool (*)(std::vector< CSPvariable<T> >) > constraints;
|
|
||||||
#define constraint constraints[0]
|
|
||||||
|
|
||||||
std::vector< std::vector<T> > __default_domains;
|
|
||||||
T __default_value;
|
|
||||||
bool __has_default_value;
|
|
||||||
static bool __default_constraint ( std::vector< CSPvariable<T> > v ) { return true; }
|
|
||||||
void __init ( int n, bool (*c)(std::vector< CSPvariable<T> >) );
|
|
||||||
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<T> >) = __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<T> >) = __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<T> 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<T> >) );
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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<T> >) > 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<T> >) );
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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<T> 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
|
|
||||||
|
|
Loading…
Reference in a new issue