slitherlink2formula.cpp File Reference

Contains the functions used to turn a Slitherlink puzzle into a CNF formula. More...

#include <stdlib.h>
#include "../libcnf/libcnf.h"
#include "slitherlink.h"
+ Include dependency graph for slitherlink2formula.cpp:

Go to the source code of this file.

Classes

struct  coord
 Rather self explanatory: it is used to easily store and retrieve coordinates in the grid. More...

Defines

#define HORIZONTAL   0
#define VERTICAL   1
#define HORIZONTAL_EDGE_INDEX   2
#define VERTICAL_EDGE_INDEX   3
#define HORIZONTAL_EDGE_SENSE   4
#define VERTICAL_EDGE_SENSE   5
#define NUMBERING   2
#define SENSE   4

Functions

void getHneighbours (Slitherlink *sl, unsigned int i, unsigned int j, std::vector< coord > *neighbours1, std::vector< coord > *neighbours2)
 Retrieves the codes of the neighbours of a given horizontal edge depending on its position in the grid.
void getVneighbours (Slitherlink *sl, unsigned int i, unsigned int j, std::vector< coord > *neighbours1, std::vector< coord > *neighbours2)
 Retrieves the codes of the neighbours of a given vertical edge depending on its position in the grid.
void addContinuityAxioms (cnf::Formula *form, int edgeCode, std::vector< coord > neighbours)
 Writes axioms enforcing the loop to be continuous, i.e. that if an edge is in the loop then exactly one of its neighbous is.
std::vector< int > hintNeighbours (cnf::VariableSet *vars, unsigned int i, unsigned int j)
 Returns a vector containing the code of the positive literals corresponding to the $x_e$ variables for each $e$ around the cell at $(i,j)$.
void addHintAxioms0 (cnf::Formula *form, Slitherlink *sl, cnf::VariableSet *vars, unsigned int i, unsigned int j)
 Add the clauses corresponding to a hint equal to zero, i.e. that all edges around it should be set to false.
void addHintAxioms1 (cnf::Formula *form, Slitherlink *sl, cnf::VariableSet *vars, unsigned int i, unsigned int j)
 Add the clauses corresponding to a hint equal to 1, i.e. the following set of clauses:
void addHintAxioms2 (cnf::Formula *form, Slitherlink *sl, cnf::VariableSet *vars, unsigned int i, unsigned int j)
 Adds the clauses corresponding to a hint being equal to 2, namely these:
void addHintAxioms3 (cnf::Formula *form, Slitherlink *sl, cnf::VariableSet *vars, unsigned int i, unsigned int j)
 Adds clauses corresponding to a hint being equal to 3, namely:
void addHintAxioms (cnf::Formula *form, Slitherlink *sl, cnf::VariableSet *vars, unsigned int i, unsigned int j)
void addContinuityAndNumberingAxioms (cnf::Formula *form, int orientation, unsigned int i, unsigned int j, unsigned int maxLoopSize)
 Adds axioms stating that if an edge is activated then it has a unique index.
void addIncreasingIndexAxioms (cnf::Formula *form, int orientation, unsigned int i, unsigned int j, unsigned int index, bool isNeighboursGreater, bool sense, std::vector< coord > neighbours)
 Adds axioms stating that the if the edge at $(i,j)$ with orientation $orientation$ has index $index$ then:
void placeZero (cnf::Formula *form, Slitherlink *sl, cnf::VariableSet *vars, unsigned int i, unsigned int j)
 It, well, places the zero obviously. It adds clauses to the formula enforcing that the edge with index zero has to touch a non-zero hint (the first one encountered) and that there can only be one around this hint. It then sets all the other edges to be non-zero.
int main (int argc, char *argv[])

Detailed Description

Contains the functions used to turn a Slitherlink puzzle into a CNF formula.

Author:
Léo Perrin
Date:
Time-stamp: <2012-06-19 16:55:12 leo>

Definition in file slitherlink2formula.cpp.


Define Documentation

#define HORIZONTAL   0

Definition at line 17 of file slitherlink2formula.cpp.

#define HORIZONTAL_EDGE_INDEX   2

Definition at line 19 of file slitherlink2formula.cpp.

#define HORIZONTAL_EDGE_SENSE   4

Definition at line 21 of file slitherlink2formula.cpp.

#define NUMBERING   2

Definition at line 24 of file slitherlink2formula.cpp.

#define SENSE   4

Definition at line 25 of file slitherlink2formula.cpp.

#define VERTICAL   1

Definition at line 18 of file slitherlink2formula.cpp.

#define VERTICAL_EDGE_INDEX   3

Definition at line 20 of file slitherlink2formula.cpp.

#define VERTICAL_EDGE_SENSE   5

Definition at line 22 of file slitherlink2formula.cpp.


Function Documentation

void addContinuityAndNumberingAxioms ( cnf::Formula *  form,
int  orientation,
unsigned int  i,
unsigned int  j,
unsigned int  maxLoopSize 
)

Adds axioms stating that if an edge is activated then it has a unique index.

Parameters:
formThe formula where to put the clauses
orientationThe orientation of the edge (HORIZONTAL or not?)
iAbscissa of the edge
jOrdena of the edge
maxLoopSizeThe maximum index it can have

Definition at line 359 of file slitherlink2formula.cpp.

void addContinuityAxioms ( cnf::Formula *  form,
int  edgeCode,
std::vector< coord neighbours 
)

Writes axioms enforcing the loop to be continuous, i.e. that if an edge is in the loop then exactly one of its neighbous is.

The idea is to run this on the two sets of neighbours of a given edge. It will add the following axioms:

Parameters:
form
edgeCode
neighbours

Definition at line 169 of file slitherlink2formula.cpp.

void addHintAxioms ( cnf::Formula *  form,
Slitherlink sl,
cnf::VariableSet *  vars,
unsigned int  i,
unsigned int  j 
)

Definition at line 329 of file slitherlink2formula.cpp.

void addHintAxioms0 ( cnf::Formula *  form,
Slitherlink sl,
cnf::VariableSet *  vars,
unsigned int  i,
unsigned int  j 
)

Add the clauses corresponding to a hint equal to zero, i.e. that all edges around it should be set to false.

Definition at line 227 of file slitherlink2formula.cpp.

void addHintAxioms1 ( cnf::Formula *  form,
Slitherlink sl,
cnf::VariableSet *  vars,
unsigned int  i,
unsigned int  j 
)

Add the clauses corresponding to a hint equal to 1, i.e. the following set of clauses:

  • at least one of the edge is true.
  • they are mutually exclusive.

Definition at line 247 of file slitherlink2formula.cpp.

void addHintAxioms2 ( cnf::Formula *  form,
Slitherlink sl,
cnf::VariableSet *  vars,
unsigned int  i,
unsigned int  j 
)

Adds the clauses corresponding to a hint being equal to 2, namely these:

  • All 3 tuples among the 4 variables are true
  • So do all the negative 3 tuples.

Definition at line 275 of file slitherlink2formula.cpp.

void addHintAxioms3 ( cnf::Formula *  form,
Slitherlink sl,
cnf::VariableSet *  vars,
unsigned int  i,
unsigned int  j 
)

Adds clauses corresponding to a hint being equal to 3, namely:

  • at least one of the edges is set to false
  • the negativity of the edges are mutually exclusive

Definition at line 307 of file slitherlink2formula.cpp.

void addIncreasingIndexAxioms ( cnf::Formula *  form,
int  orientation,
unsigned int  i,
unsigned int  j,
unsigned int  index,
bool  isNeighboursGreater,
bool  sense,
std::vector< coord neighbours 
)

Adds axioms stating that the if the edge at $(i,j)$ with orientation $orientation$ has index $index$ then:

  • if $neighbours$ is the neighbours being greater than $edge$ according to the variable $sense$, then there must be an edge in $neighbours$ with index equal to $index+1$ or 0. - otherwise and if $index$ is not zero there must an edge in $neighbours$ with index $index-1$.

Definition at line 391 of file slitherlink2formula.cpp.

void getHneighbours ( Slitherlink sl,
unsigned int  i,
unsigned int  j,
std::vector< coord > *  neighbours1,
std::vector< coord > *  neighbours2 
)

Retrieves the codes of the neighbours of a given horizontal edge depending on its position in the grid.

Parameters:
[out]neighbours1When the call is done, contains the coordinates of the variables on the right of the vertex.
[out]neighbours2When the call is done, contains the coordinates of the variables on the left of the vertex.

Definition at line 58 of file slitherlink2formula.cpp.

void getVneighbours ( Slitherlink sl,
unsigned int  i,
unsigned int  j,
std::vector< coord > *  neighbours1,
std::vector< coord > *  neighbours2 
)

Retrieves the codes of the neighbours of a given vertical edge depending on its position in the grid.

Parameters:
[out]neighbours1When the call is done, contains the coordinates of the variables on top of the vertex.
[out]neighbours2When the call is done, contains the coordinates of the variables below the vertex.

Definition at line 112 of file slitherlink2formula.cpp.

std::vector<int> hintNeighbours ( cnf::VariableSet *  vars,
unsigned int  i,
unsigned int  j 
)

Returns a vector containing the code of the positive literals corresponding to the $x_e$ variables for each $e$ around the cell at $(i,j)$.

Parameters:
varsThe variable set.
iThe first coordinate of the cell.
jThe second one.
Returns:
The corresponding variable codes.

Definition at line 211 of file slitherlink2formula.cpp.

int main ( int  argc,
char *  argv[] 
)

Definition at line 475 of file slitherlink2formula.cpp.

void placeZero ( cnf::Formula *  form,
Slitherlink sl,
cnf::VariableSet *  vars,
unsigned int  i,
unsigned int  j 
)

It, well, places the zero obviously. It adds clauses to the formula enforcing that the edge with index zero has to touch a non-zero hint (the first one encountered) and that there can only be one around this hint. It then sets all the other edges to be non-zero.

Definition at line 426 of file slitherlink2formula.cpp.

 All Classes Files Functions Variables Defines