1 Introduction
Nowadays, a growing number of real world problems are successfully tackled using constraint solvers. The hybridization of constraint solvers with other combinatorial technologies such as mixed integer programming [1, 3, 14], local search [12, 15, 17], and particularly conflict driven clause learning [6, 7, 16] has been at the center of substantial advances in many domains.
Unfortunately, while many opensource constraint solvers exist [4, 8, 9, 10, 11], modifying these solvers to hybridize them with other technologies, to extend them with specific structured domains, or to enhance them with new functionalities, may prove to be a time consuming and discouraging adventure due to the impressive number of lines of code involved in those opensource projects. Also starting the development of a constraint solver from scratch may also prove itself to be quite a challenge even if one has a deep understanding of the mechanisms and techniques at play in constraint solvers and constraint propagators.
The goal of this paper is to present Kiwi — a minimalist and extendable constraint solver. The source code of the core components of Kiwi is under 200 lines of Scala code and is the result of rethinking and simplifying the architecture of the opensource OscaR solver [11] to propose what we believe to be a good tradeoff between performance, clarity, and conciseness.
By developing Kiwi, the author does not aim to provide an alternative to full featured constraint solvers but rather to provide students with a basic architecture that will (hopefully) help them to understand the core mechanisms hidden under the hood of constraint solvers, to develop their own extended constraint solver, or to test innovative ideas.^{1}^{1}1In this regard, Kiwi can be seen as an attempt to follow the initiative of minisat [5] but in the context of constraint programming.
2 Overview of the Solver
We start the presentation of Kiwi by briefly describing its three main components: propagation, search, and state restoration.
Propagation
The propagation system reduces the domain of the variables by filtering values that are part of no solution according to the constraints. The architecture of this system is described in section 4.
Search
Unfortunately, propagation alone is usually not sufficient to solve a constraint satisfaction problem. Constraint solvers thus rely on a divideandconquer procedure that implicitly develops a search tree in which each node is a subproblem of its ancestors. Leaves of this searchtree are either failed nodes – i.e. inconsistent subproblems – or solutions. Propagation is used at the beginning of each node to reduce the domain of the variables and thus to prune fruitless branches of the search tree. The search procedure of Kiwi is described in section 5.
State restoration
The state restoration system manages the different search nodes explored during search.
Precisely, it is the component that restores the domain of the variables to their previous state each time a backtrack occurs.
Its main purpose is to reduce the cost of copying the entire state of each node to provide users with an efficient tradeoff between memory and processing costs.
State restoration mechanisms are presented in the next section.
More than a constraint solver, Kiwi must be seen as a set of the components involved in the core of classic constraint solvers. Indeed, Kiwi does not provide users with abstraction to help them model and solve combinatorial problems. Also, we only give little attention to the filtering procedures involved in constraint propagation – which are arguably the most complex parts of every constraint solver. While the simplest binary constraints may only require a few lines of code, more complex global constraints usually rely on sophisticated algorithms and data structures. Scheduling constraints are a perfect example of such complex global constraints. The reader that is interested by global constraints and propagators may refer to [2] for a wide catalogue of global constraints and links towards the relevant literature.
3 State restoration
Copying and storing the whole state of each search node is usually too memory consuming for a real constraint solver. The state restoration system thus has to rely on different tradeoffs to restore the domain of the variables as efficiently as possible.^{2}^{2}2State restoration is of course not limited to domains and may also be used to maintain incremental data structures or other components. There are three main approaches to implements such a system:

Copying. A complete copy of the domains is done and stored before changing their state.

Recomputation. Domains are recomputed from scratch when required.

Trailing. Domain changes are recorded incrementally and undone when required.
Trailing is the prominent approach used in many constraint solvers [4, 5, 9, 10, 11].^{3}^{3}3Although, some solvers such as Gecode [8] rely on an hybrid state restoration mechanism based on both copying and recomputation. The idea behind trailing is to maintain the sequence of changes that occured on a given branch since the root of the search tree. We call such a sequence the trail. Each node can be represented by a prefix of the trail that corresponds to the sequence of changes that lead to this node. Particularly, the sequence of changes that lead to a node is always an extension of the sequence that lead to its direct ancestor and so on (the root node being represented by the empty sequence). This incremental representation is convenient for backtracks as they can be performed by undoing the last changes of the trail until restoring the sequence of the desired node.
For instance, let us consider the sequence of changes depicted in Figure 1. The first node has been computed from the root node by applying 3 changes , the second node has been computed from the first node by applying 4 changes , and the third node has been computed from the second node by applying changes. We can easily restore the state of the second node by undoing the last changes of the trail until , i.e., .
In Kiwi, we chose to manage state restoration with an extended trailing mechanism. The particularity of our system is that it allows each stateful object (e.g. the domain of a variable) to manage its own restoration mechanism internally. This mechanism can be based on trailing, copying, or even recomputation. The remainder of this section is dedicated to the implementation of this system.
3.1 Changes as an Abstraction
Each time a state changing operation is performed, the necessary information to undo this operation is stored on the trail. Such undo information can have many forms but is usually represented by a pair made of a memory location and of its corresponding value.
In Kiwi, we chose to directly store the functions responsible of undoing state changes as first class objects, i.e., as closures. Each stateful object thus relies on its own restoration mechanism which is handled by the closure itself.
The abstract class of an undo operation, namely Change, is presented in Code 3.1. It contains a single function undo which, as its name suggests, is responsible for undoing the change.
The complete implementation of a stateful mutable integer is presented in section 3.3.
3.2 The Trail
Our trailing system is implemented with two stacks:

The first is a stack of Change objects that represents the trail. It is sorted chronologically such that the most recent change is on top of the stack (the root node being the empty stack).

The second stack maps each node of the current branch to its corresponding prefix in the trail. Thanks to the incrementality of the trail, only the position of the last change that lead to a node needs to be stored to characterize the whole sequence of changes that lead to this node.
Figure 2 illustrates the relation between both stacks.
The whole implementation of Trail, our trailing system, is presented in Code 3.2. Changes are registered using the store function that pushes them on top of trail (line 6). The newNode function registers the state of the current node by storing the current size of the trail (line 8). Conversely, the undoNode function restores the previous node by undoing the changes on top of the trail until its size corresponds to the size stored with the previous node (lines 10 to 12 and 15 to 17).
3.3 Trailed Integer
We now have all the pieces required to build our first stateful object: a stateful integer variable^{4}^{4}4Integer variable here refers to a mutable integer. called TrailedInt. As for classic integer variables, the value of TrailedInt can be accessed and updated. It however keeps track of the different values it was assigned to in order to restore them each time a backtrack occurs.
Similarly to most stateful objects in Kiwi, TrailedInt implements its own internal state restoration mechanism. It is based on a stack of integers that represents the sequence of values that were assigned to this TrailedInt since its initialization. Restoring the previous state of TrailedInt thus amounts to update its current value to the last entry of the stack (which is then removed from the stack).
The implementation of TrailedInt is presented in Code 3.3. The current value of the object is stored in the private variable currentValue. It is accessed with the getValue function. The setValue function is the one used to modify the value of the object. It pushes the current value on top of the stack of old values (line 7), updates currentValue (line 8), and notifies Trail that a change occurred (line 9).
The particularity of this implementation is that TrailedInt directly implements the Change abstract class and its undo operation (lines 12 to 14). This has the advantage of reducing the overhead of instantiating a new Change object each time a state changing operation is performed on TrailedInt.
3.4 Improvements
The trailing system we presented suffers from a major weakness. For instance, TrailedInt keeps track of all the values it was assigned to. However, only the values it was assigned to at the beginning of each state are required for state restoration. Hopefuly, we can easily fix this problem with timestamps. The idea is to associate a timestamp to each search node and to only register the current value of TrailedInt if it has not yet been registered at a given timestamp. The interested reader can refer to the 12th chapter of [13] for detailed explanations on how to improve trailing systems.
4 The Propagation System
Propagation is the process of using the filtering procedure embedded in the constraints to reduce the domain of the variables. To achieve this, the propagation system of Kiwi relies on the following entities:

The propagators which are responsible of performing constraint propagation by updating the domain of the variables, or to notify that a conflict occured. They contain the actual filtering procedures of their respective constraint. Propagators are registered on different types of events, such as the reduction of a variable’s domain, and need to be propagated each time one of these events occurs.

The propagation queue that synchronizes propagators to perform propagation until no further filtering can be achieved or a conflict has been detected.

The variables which are updated by the propagators and are responsible for notifying the propagation queue if a propagator needs to be considered for further propagation.
The interaction between those abstractions is illustrated in Figure 3. Observe that the propagation queue, PropagQueue, does not directly interact with the variables.
4.1 Propagators
Kiwi does not contain any object that actually represents a constraint. Instead, a constraint is implicitly defined by the set of propagators which must ensure that the constraint’s logical relation holds. A constraint can thus be composed of a single propagator registered on all the variables in the constraint’s scope, or by a set of propagators, each one being registered on a subset of the constraint’s scope.
The abstract class Propagator is presented in Code 4.1. It contains the following functions:

The enqueued boolean is a flag used by the propagation queue to indicate that the propagator is awake and waiting for propagation.

The init function registers the propagator on the events of interest and perform its initial propagation. It return true if the propagation suceed and false if it lead to a conflict.

The propagate function performs the actual filtering. Like init, it returns false if and only if propagation lead to a conflict.
As an example, we present the complete implementation of a simple binary constraint in section 4.4.
4.2 The Propagation Queue
The propagation queue, PropagQueue, contains all the propagators that are waiting for propagation. It is responsible for synchronizing these propagators until no further filtering can be achieved, or until a conflict has been detected. When running, the propagation process dequeues one propagator from the propagation queue and calls its propagate function to reduce the domain of some variables. Of course, reducing the domain of a variable usually awakes new propagators that are then enqueued in the propagation queue. This process is repeated until either the propagation queue becomes empty, meaning that no further propagation is possible, or until a conflict occured.
The implementation of PropagQueue is presented in code 4.2. The enqueue function enqueues a propagator in the propagation queue but only if it is not already contained in it (lines 5 to 10). The propagate function contains the main loop of the propagation process (lines 12 to 22). It empties the queue by dequeuing each propagator and calling their propagate function if no conflict has occurred yet. The function concludes by returning true if no conflict occurred; it returns false otherwise. Observe that the propagation queue is emptied even if a conflict occured.
4.3 Variables
Variables are the last piece of our propagation system. Interestingly enough, Kiwi does not provide variables with an interface to implement. This design choice is one of the reasons that facilitates the extension of Kiwi with additional structured domain representations [13]. While variables does not have to respect any requirements, they usually offer the following functionalities:

A variable has a domain that represents the set of all possible values it could be assigned to.

A variable offers functions to remove possible values from its domain until it becomes a singleton, in which case the variable is assigned, or it becomes empty, meaning that a conflict occured. This domain must be restored to its previous state when a backtrack occurs.

A variable allows propagators to watch particular modifications of its domain. The role of the variable is then to enqueue these propagators in the propagation queue when one of these modifications occur.
As an example, we focus on a particular type of integer variable that we call interval variable.^{5}^{5}5Interval variables are commonly used by boundconsistent propagators [13]. The domain of an interval variable is characterized by its minimal value and its maximal value . It contains all the values contained in the interval . Particularly, the domain of an interval variable can only be reduced by increasing its minimal value or by decreasing its maximal value. We subsequently refer to the minimal and maximal values of the domain as the bounds of the variables.
The implementation of IntervalVar is presented in Code 4.3. As expected, it is made of two TrailedInt that represent the minimum and maximum value of the variable’s domain. The minWatchers and maxWatchers queues are used to store propagators that must respectively be awaken when the minimum value of the variable is increased or when the maximum value of the variable is decreased. These queues are filled using the watchMin and watchMax functions. The updateMin function is the one responsible for increasing the minimum value of the variable. It operates as follows:

If the new minimum value exceeds the maximum value of the variable, then the domain becomes empty and the function returns false to notify this conflict (line 14).^{6}^{6}6We do not actually empty the domain of the variable because this change will be directly undone by backtracking.

If the new minimum value is lower or equal to the current minimum value, then nothing happens and the function returns true (line 15).

Otherwise, the function updates the minimum value to its new value, awakes all the propagators contained in the minWatchers queue and returns true to notify that no conflict occured (lines 17 to 19).
For the sake of conciseness, we do not describe the implementation of watchMax and updateMax as they are symmetric to their min version.
4.4 The Lower or Equal Constraint
We now have all the components at hand to understand and implement our first constraint. We focus on the simple binary constraint where both and are interval variables (see Code 4.3).
The constraint is made of a single propagator called LowerEqual (see Code 4.4). Its propagate function ensures the following rules:

The maximum value of is always lower or equal to the maximum value of (line 12).

The minimum value of is always greater or equal to the minimum value of (line 13).
The propagate function returns false if ensuring these rules empties the domain of either or ; it returns true otherwise. The init function performs the initial propagation (line 4) and registers the propagator on its variables (lines 6 and 7). Like propagate, it returns true if the propagation succeeded and false if a conflict occurred. Note that the propagator does not have to be awaken when the maximum value of or the minimum value of change since they have no impact on both previous rules.
4.5 Improvement and Discussion
There are many ways to improve our propagation system. For instance, let us consider the LowerEqual propagator from the previous section. If the maximal value of becomes lower or equal to the minimum value of , then no further filtering is possible. We say that the propagator is entailed and thus does not have to be propagated anymore. We could implement this additional condition by adding a stateful boolean to the Propagator interface and by checking the value of this boolean each time the propagator is enqueued in the propagation queue. Another improvement is to add different levels of priority to propagators. Indeed, some propagators have a much faster filtering procedure than others. It is therefore useful to propagate these propagators first in order to give as much information as possible to slower propagators. The interested reader can refer to the 4th and 12th chapters of [13] for detailed additional improvements.
5 Search
The final part of our solver is the search system. Its aim is to explore the whole search space of the problem looking for solutions or proving that no solution exists. It uses trailing to manage the nodes of the search tree and applies propagation to prune fruitless branches. The search system is based on a depthfirstsearch algorithm that relies on heuristics to determine the order in which nodes are explored. The following sections are dedicated to the description of these components.
5.1 Heuristics and Decisions
The aim of the heuristic is to define and order the children of any internal node of the search tree. It does so by associating each node with a sequence of decisions, where a decision is any operation that transforms a node into one of its children – e.g. assigning a variable to a value, or removing that value from the domain of the variable. The order in which decisions are returned corresponds to the order in which children have to be visited in the search tree. Particularly, the heuristic returns an empty sequence of decisions if the current node is a valid leaf, i.e., a solution.
Let us first take a look at the interface of a Decision (see Code 5.1). It contains the single function apply that is responsible of applying the decision. It returns false if applying the decisions directly lead to a conflict (e.g. if we try to assign a variable to a value that is not part of its domain for instance); it returns true otherwise.
As an example, we consider the implementation of both decisions in Code 5.2. The GreaterEqual decision updates the minimal value of to . It returns true if is lower or equal to the maximal value of ; it returns false otherwise. The Assign decision assign the variable to the value . It returns true if is part of the domain of ; it returns false otherwise.
Code 5.3 presents the interface of Heuristic. It contains the single function nextDecisions which, as mentioned above, returns a sequence – an iterator precisely – of Decision objets that will be used by the search system to build the search tree.
We illustrate the use of this interface with the StaticMin heuristic (see Code 5.4). It builds a binary search tree in which each left branch tries to assign a variable to its minimal value and each right branch tries to remove that value from the domain of its variable (see Figure 4). The order in which StaticMin tries to assign variables is statically determined by the order of the variables in the vars array. The first part of the heuristic (lines 6 to 7) searches for the index of the first unassigned variable in vars. This index is stored in the nextUnassigned stateful integer (line 8) – the use of a stateful integer is not required here but reduces the overhead of systematically scanning the first already assigned variables. If all variables have been assigned, the heuristic returns an empty iterator to inform the search system that the current node is a leaf (line 10). Otherwise, it selects the next unassigned variable and tries to assign it with its minimum value – left branch – or to remove that value from the variable’s domain – right branch – (lines 12 to 15).^{7}^{7}7The LowerEqual and the GreaterEqual decisions have similar implementations.
5.2 The DepthFirstSearch
Our depthfirstsearch algorithm explores the search tree defined by the heuristic. It relies on propagation to reduce the domain of the variables and thus pruning fruitless branches of the search tree.
The particularity of our implementation is that it manages its own stack of nodes instead of relying on recursion. The link between the stack and the actual search tree is illustrated in Figure 5 (note that, by abuse of language, we subsequently refer to ‘decisions’ as ‘nodes’ and viceversa). The stack (depicted on the left) contains the iterator of decisions of each node in the currently explored branch of the search tree. Precisely, the first iterator corresponds to the children of the root node, the second iterator corresponds to the children of node , and so on. The gray element of each iterator is its current element. Hence, dashed elements have already been read while white elements still have to be. The search tree (depicted on the right) follows the same color code. Dashed nodes are the root of already explored subtrees, gray nodes are part of the branch that is currently explored by the search, and white nodes still have to be visited. Node is the node that is currently visited by the search.
The stack starts empty at the beginning of the search to indicate that the root node has not yet been visited. The search then uses the heuristic to expand the root node by pushing its sequence of children on the stack. The first unvisited child on top of the stack is then selected and expanded in the same way. This process is repeated until, eventually, the search reaches a node with an empty sequence of decisions, i.e., a leaf. It then operates as follows:

If the iterator on top of the stack still contains unvisited nodes, then the search backtracks by visiting the next element of this iterator – the next sibling of the current node.

Otherwise, we know that the iterator on top of the stack does no contain any unvisited nodes. The search thus removes this iterator from the top of the stack and repeats this process.
Figure 6 illustrates this operation where steps (a) and (b) respectively correspond to the second and the first above situations. The search keeps exploring the search tree by pushing and popping iterators on the stack until it becomes empty, meaning that the whole search tree has been explored.
The implementation of our depthfirstsearch algorithm is presented in Codes 5.5 and 5.6. As expected, it contains an internal stack of iterators that we call decisions (line 2). The onSolution function (line 4) is a hook to be overridden by the user. It defines the action to perform each time the search encounter a solution. The propagateAndExpand function (lines 5 to 15) is called each time a new node is visited. It returns false if and only if the currently visited node is a leaf (either it is a solution or not); it returns true otherwise. The first part of that function is to propagate the visited node (line 6). This operation may lead to a conflict in which case false is returned to indicate that the node is a failed leaf. Otherwise, we call the nextDecisions function of heuristic to get the children of the node as a sequence of decisions (line 7). If this sequence is empty, then we know that the node is a solution, we call the onSolution function, and returns false (lines 8 to 10). If the node actually has children, its iterator of decisions is pushed on the stack and the function returns true (lines 11 to 13).
The search
function is probably the most complex piece of code of
Kiwi (see Code 5.6). It is the one that is actually responsible of exploring the search tree. It takes a maximum number of nodes to visit in parameter to limit the time spent exploring the search tree – which grows exponentially with the size of the problem to solve. The function returns true if it was able to explore the whole search tree without exceeding the maximum number of nodes; it returns false otherwise. The first part of the search is to call propagateAndExpand to perform the initial root propagation and expand the root node (line 4). If that call returns false, then we know that the root node is already a leaf (either it is a solution or not). The search thus returns true to notify the user that the whole search tree – which is made of a single node – has been explored. Otherwise, we know that the decisions stack contains its first nonempty iterator (i.e. the children of the root) and that we can start exploring the search tree. The actual search starts if the decisions stack contains at least one iterator – which is always true at this step – and if the maximum number of nodes is not exceeded (line 6). The first step of the loop is to check that the iterator on top of the stack still contains unapplied decisions, i.e., unvisited nodes (lines 7 and 8). The next step is one of the following:
If the iterator still contains some decisions, then the search visits the next node by notifying the trailing system that we are building a new node (line 10), and then by applying the next decision of the top iterator (lines 11 and 12). The search undo the state of this new node if it is a leaf (line 13), i.e., if applying the decision directly lead to a conflict or if the propagateAndExpand function returned false.

If the iterator does not contain any unapplied decision, then it is removed from the decisions stack (line 15) and the state of the current node is undone (line 16) (see Figure 6).
The main loop is repeated until either the decisions stack becomes empty or until the maximum number of visited nodes has been exceeded. The search then concludes by undoing all the not yet undone nodes and by removing all the remaining iterators from decisions (lines 20 and 21). It finally returns true if and only if it was able to explore the whole search tree (line 23).
References
 [1] J Christopher Beck and Philippe Refalo. A hybrid approach to scheduling with earliness and tardiness costs. Annals of Operations Research, 118(14):49–71, 2003.
 [2] Nicolas Beldiceanu, Mats Carlsson, Sophie Demassey, and Thierry Petit. Global constraint catalogue: Past, present and future. Constraints, 12(1):21–62, 2007.

[3]
Alexander Bockmayr and Nicolai Pisaruk.
Detecting infeasibility and generating cuts for mip using cp.
In
Proceedings of the 5th International Workshop on Integration of AI and OR Techniques in Constraint Programming for Combinatorial Optimization Problems, CPAIOR
, volume 3, 2003.  [4] Xavier Lorca Charles Prud’homme, JeanGuillaume Fages. Choco3 documentation. TASC, INRIA Rennes, LINA CNRS UMR 6241, COSLING S.A.S., 2014.
 [5] Niklas Eén and Niklas Sörensson. An extensible satsolver. In Theory and applications of satisfiability testing, pages 502–518. Springer, 2003.
 [6] Thibaut Feydy, Andreas Schutt, and Peter J Stuckey. Semantic learning for lazy clause generation. In Proceedings of TRICS Workshop: Techniques foR Implementing Constraint programming Systems, TRICS’13, Uppsala, Sweden, 2013.
 [7] Thibaut Feydy and Peter J Stuckey. Lazy clause generation reengineered. In Principles and Practice of Constraint ProgrammingCP 2009, pages 352–366. Springer, 2009.
 [8] Gecode Team. Gecode: Generic constraint development environment, 2006. Available from http://www.gecode.org.
 [9] IBM ILOG CPLEX CP Optimizer. V12.6.
 [10] Ortools Team. ortools: Google optimization tools, 2015. Available from https://developers.google.com/optimization/.

[11]
OscaR Team.
OscaR: Scala in OR, 2012.
Available from
https://bitbucket.org/oscarlib/oscar.  [12] Laurent Perron, Paul Shaw, and Vincent Furnon. Propagation guided large neighborhood search. Principles and Practice of Constraint Programming–CP 2004, pages 468–481, 2004.
 [13] Francesca Rossi, Peter Van Beek, and Toby Walsh. Handbook of constraint programming. Elsevier Science, 2006.
 [14] Domenico Salvagnin and Toby Walsh. A hybrid mip/cp approach for multiactivity shift scheduling. In Principles and practice of constraint programming, pages 633–646. Springer, 2012.
 [15] Pierre Schaus. Variable objective large neighborhood search. Submitted to CP13, 2013.
 [16] Andreas Schutt, Thibaut Feydy, Peter J Stuckey, and Mark G Wallace. Why cumulative decomposition is not as bad as it sounds. In Principles and Practice of Constraint ProgrammingCP 2009, pages 746–761. Springer, 2009.
 [17] P. Shaw. Using constraint programming and local search methods to solve vehicle routing problems. Principles and Practice of Constraint Programming—CP98, pages 417–431, 1998.
Comments
There are no comments yet.