\ignore{
\documentstyle[11pt]{report}
\textwidth 13.7cm
\textheight 21.5cm
\newcommand{\myimp}{\verb+ :- +}
\newcommand{\ignore}[1]{}
\def\definitionname{Definition}

\makeindex
\begin{document}

}
\chapter{\label{ch:constraints}Constraints}
Picat provides four solver modules, including \texttt{cp}, \texttt{sat}, \texttt{smt}, and \texttt{mip}, for modeling and solving constraint\index{constraint} satisfaction and optimization problems (CSPs).  All four of these modules implement the same set of constraints on integer-domain variables. The \texttt{mip} module also supports real-domain variables. In order to use a solver, users must first import the module. In order to make the symbols defined in a module available to the top level of the interpreter, users can use the built-in {\tt import} to import the module. The \texttt{mip} module requires Gurobi (\url{https://www.gurobi.com/}), Cbc (\url{ https://github.com/coin-or/Cbc}), or GLPK (\url{https://www.gnu.org/software/glpk/}). The \texttt{smt} module requires Z3 (\url{https://github.com/Z3Prover/z3/}) or CVC4 (\url{https://cvc4.github.io/}). Users can specify, as a solving option, a solver to be used if the module is \texttt{mip} or \texttt{smt}.

As the four modules have the same interface, this chapter describes the four modules together. Figure \ref{tab:cons_ops} shows the constraint\index{constraint} operators that are provided by Picat. Unless it is explicitly specified otherwise, the built-ins that are described in this chapter appear in all four modules. In the built-ins that are presented in this chapter, an integer-domain variable can also be an integer, unless it is explicitly specified to only be a variable.

\begin{table}[ht]
\caption{\label{tab:cons_ops}Constraint operators in Picat}
\begin{center}
\begin{tabular}{ |c|c| } \hline
Precedence & Operators  \\ \hline \hline
Highest    & \verb+::+, \verb+notin+,  \verb+#=+,  \verb+#!=+, \verb+#<+,  \verb+#=<+, \verb+#<=+, \verb+#>+, \verb+#>=+ \\ \hline 
           & \verb+#~+ \\ \hline 
           & \verb+#/\+ \\ \hline 
           & \verb+#^+ \\ \hline 
           & \verb+#\/+ \\ \hline 
           & \verb+#=>+ \\ \hline 
Lowest     & \verb+#<=>+ \\ \hline 
\end{tabular}
\end{center}
\end{table}

A constraint program normally poses a problem in three steps: (1) generate variables; (2) generate constraints over the variables; and (3) call \texttt{solve}\index{\texttt{solve/1}}\index{\texttt{solve/2}} to find a valuation for the variables that satisfies the constraints and possibly optimizes an objective function.

\subsection*{Example}
\begin{figure}[ht]
\begin{center}
\begin{verbatim}
    import cp.

    queens(N) =>
        Qs = new_array(N),
        Qs :: 1..N,
        foreach (I in 1..N-1, J in I+1..N)
            Qs[I] #!= Qs[J],
            abs(Qs[I]-Qs[J]) #!= J-I
        end,
        solve(Qs),
        writeln(Qs).
\end{verbatim}
\end{center}
\caption{\label{fig:nqueens} A program for N-queens.}
\end{figure}

This program in Figure \ref{fig:nqueens} imports the \texttt{cp} module in order to solve the $N$-queens problem. The same program runs with the SAT solver if \texttt{sat} is imported, runs with the SMT solver if \texttt{smt} is imported, and runs with the LP/MIP solver if \texttt{mip} is imported. The predicate \texttt{Qs :: 1..N} declares the domains of the variables. The operator \verb+#!=+ is used for inequality constraints. In arithmetic constraints, expressions are treated as terms, and it is unnecessary to enclose them with dollar-signs. The predicate \texttt{solve(Qs)}\index{\texttt{solve/1}} calls the solver in order to solve the array of variables \texttt{Qs}. For \texttt{cp}, \texttt{solve([ff],Qs)}\index{\texttt{solve/2}}, which always selects a variable that has the smallest domain (the so-called \emph{first-fail principle}\index{first-fail principle}), can be more efficient than \texttt{solve(Qs)}\index{\texttt{solve/1}}.


\section{Domain Variables}
A domain variable is an attributed variable that has a domain attribute. The Boolean domain is treated as a special integer domain where 1 denotes \texttt{true} and 0 denotes \texttt{false}. Domain variables are declared with the built-in predicate \texttt{$Vars$ :: $Exp$}. 

\begin{itemize}
\item \texttt{$Vars$ :: $Exp$}: This predicate restricts the domain or domains of $Vars$ to $Exp$.  $Vars$ can be either a single variable or a collection of variables.\footnote{A collection can be either a list or an array, where elements can be collections.} For integer-domain variables, $Exp$ must result in a list of integer values. For real-domain variables for the {\tt mip} module, $Exp$ must be an interval in the form \texttt{$L$..$U$}, where $L$ and $U$ are real values.
\end{itemize}

Domain variables, when being created, are usually represented internally by using intervals. An
interval turns to a bit vector when a hole occurs in the interval. The following built-in predicate can be used to reset the range or access the current range.
\begin{itemize}
\item \texttt{fd\_vector\_min\_max($Min$,$Max$)}: When the arguments are integers, this predicate specifies the range of bit vectors; when the arguments are variables, this predicate binds them to the current bounds of the range. The default range is {\tt -3200..3200}.
\index{\texttt{fd\_vector\_min\_max/2}}
\end{itemize}

The following built-ins are provided for domain variables.
\begin{itemize}
\item \texttt{$Vars$ notin $Exp$}: This predicate excludes values $Exp$ from the domain or domains of $Vars$, where $Vars$ and $Exp$ are the same as in \texttt{$Vars$ :: $Exp$}. This constraint cannot be applied to real-domain variables.
\item \texttt{fd\_degree($FDVar$) = $Degree$}\index{\texttt{fd\_degree/1}}: This function returns the number of propagators that are attached to $FDVar$. This built-in is only provided by {\tt cp}.
\item \texttt{fd\_disjoint($FDVar1$,$FDVar2$)}\index{\texttt{fd\_disjoint/2}}: This predicate is true if $FDVar1$'s domain and $FDVar2$'s domain are disjoint.
\item \texttt{fd\_dom($FDVar$) = $List$}\index{\texttt{fd\_dom/1}}: This function returns the domain of $FDVar$ as a list,  where $FDVar$ is an integer-domain variable. If $FDVar$ is an integer, then the returned list contains the integer itself.
\item \texttt{fd\_false($FDVar$,$Elm$)}\index{\texttt{fd\_false/2}}: This predicate is true if the integer $Elm$ is not an element in the domain of $FDVar$.
\item \texttt{fd\_max($FDVar$) = $Max$}\index{\texttt{fd\_max/1}}: This function returns the upper bound of the domain of $FDVar$, where $FDVar$ is an integer-domain variable.
\item \texttt{fd\_min($FDVar$) = $Min$}\index{\texttt{fd\_min/1}}: This function returns the lower bound of the domain of \\ $FDVar$, where $FDVar$ is an integer-domain variable.
\item \texttt{fd\_min\_max($FDVar$,$Min$,$Max$)}\index{\texttt{fd\_min\_max/3}}: This predicate binds $Min$ to the lower bound of the domain of $FDVar$, and binds $Max$ to the upper bound of the domain of $FDVar$, where $FDVar$ is an integer-domain variable.
\item \texttt{fd\_next($FDVar$,$Elm$) = $NextElm$}\index{\texttt{fd\_next/2}}: This function returns the next element of $Elm$ in $FDVar$'s domain. It throws an exception if $Elm$ has no next element in $FDVar$'s domain.
\item \texttt{fd\_prev($FDVar$,$Elm$) = $PrevElm$}\index{\texttt{fd\_prev/2}}: This function returns the previous element of $Elm$ in $FDVar$'s domain. It throws an exception if $Elm$ has no previous element in $FDVar$'s domain.
\item \texttt{fd\_set\_false($FDVar$,$Elm$)}\index{\texttt{fd\_set\_false/2}}: This predicate excludes the element $Elm$ from the domain of $FDVar$. If this operation results in a hole in the domain, then the domain changes from an interval representation into a bit-vector representation, no matter how big the domain is. This built-in is only provided by {\tt cp}.
\item \texttt{fd\_size($FDVar$) = $Size$}\index{\texttt{fd\_size/1}}: This function returns the size of the domain of $FDVar$,  where $FDVar$ is an integer-domain variable.
\item \texttt{fd\_true($FDVar$,$Elm$)}\index{\texttt{fd\_true/2}}: This predicate is true if the integer $Elm$ is an element in the domain of $FDVar$.
\item \texttt{new\_dvar() = $FDVar$}\index{\texttt{new\_dvar/0}}: This function creates a new domain variable with the default domain, which has the bounds \texttt{-72057594037927935..72057594037927935} on 64-bit computers and \texttt{-268435455..268435455} on 32-bit computers.\footnote{Note that incorrect results may be caused by overflows or underflows.}
\end{itemize}

\section{Table constraints}
A \emph{table constraint}\index{table constraint}, or an \emph{extensional constraint}\index{extensional constraint}, over a tuple of variables specifies a set of tuples that are allowed (called \emph{positive}) or disallowed (called \emph{negative}) for the variables. A positive constraint takes the form \texttt{table\_in($DVars$,$R$)}\index{\texttt{table\_in/2}}, where $DVars$ is either a tuple of variables $\{X_1,\ldots,X_n\}$ or a list of tuples of variables, and $R$ is a list of tuples in which each tuple takes the form $\{a_1,\ldots,a_n\}$, where $a_i$ is an integer or the \textit{don't-care} symbol $*$\index{don't-care symbol}.  A negative constraint takes the form \texttt{table\_notin($DVars$,$R$)}\index{\texttt{table\_notin/2}}.

\subsection*{Example}
The following example solves a toy crossword puzzle. One variable is used for each cell in the grid, so each slot corresponds to a tuple of variables. Each word is represented as a tuple of integers, and each slot takes on a set of words of the same length as the slot. Recall that the function \texttt{ord($Char$)}\index{\texttt{ord/1}} returns the code of $Char$, and that the function \texttt{chr($Code$)}\index{\texttt{chr/1}} returns the character of $Code$.

\begin{verbatim}
    import cp.

    crossword(Vars) =>
        Vars = [X1,X2,X3,X4,X5,X6,X7], 
        Words2 = [{ord('I'),ord('N')},
                  {ord('I'),ord('F')},
                  {ord('A'),ord('S')},
                  {ord('G'),ord('O')},
                  {ord('T'),ord('O')}],
        Words3 = [{ord('F'),ord('U'),ord('N')},
                  {ord('T'),ord('A'),ord('D')},
                  {ord('N'),ord('A'),ord('G')},
                  {ord('S'),ord('A'),ord('G')}],
        table_in([{X1,X2},{X1,X3},{X5,X7},{X6,X7}], Words2),
        table_in([{X3,X4,X5},{X2,X4,X6}], Words3),
        solve(Vars),
        writeln([chr(Code) : Code in Vars]).
\end{verbatim}

\section{Arithmetic Constraints}
An arithmetic constraint takes the form
\begin{tabbing}
aa \= aaa \= aaa \= aaa \= aaa \= aaa \= aaa \kill
\> \texttt{$Exp1$ $Rel$ $Exp2$} 
\end{tabbing}
where $Exp1$ and $Exp2$ are arithmetic expressions, and $Rel$ is one of the constraint operators:  \verb+#=+, \verb+#!=+, \verb+#<+, \verb+#=<+, \verb+#<=+, \verb+#>+, or \verb+#>=+. The operators \verb+#=<+ and \verb+#<=+ are the same, meaning less than or equal to. An arithmetic expression is made from integers, variables, arithmetic functions, and constraints. The following arithmetic functions are allowed: \texttt{+} (addition), \texttt{-} (subtraction), \texttt{*} (multiplication), \texttt{/} (truncated integer division), \texttt{//} (truncated integer division), \texttt{count}, \texttt{div} (floored integer division), \texttt{mod}, \texttt{**} (power), \texttt{abs}, \texttt{min}, \texttt{max}, and \texttt{sum}.  Except for index notations, array comprehensions and list comprehensions, which are interpreted as function calls as in normal expressions, expressions in arithmetic constraints are treated as terms, and it is unnecessary to enclose them with dollar-signs. In addition to the numeric operators, the following functions are allowed in constraints:\footnote{Dot notation should not be used for functions in constraint expressions, as such functions are always evaluated eagerly.}
\begin{itemize}
\item \texttt{cond($BoolConstr$,$ThenExp$,$ElseExp$)}: This expression is the same as\\ \texttt{$BoolConstr$*$ThenExp$+(1-$BoolConstr$)*$ElseExp$}.
\item \texttt{count($V$,$Collection$)}: The number of times $V$ occurs in $Collection$, where $Collection$ is a nested collection of domain variables.\footnote{A nested collection is recursively defined. It can be variable, a number, a list, or an array. If it is a list or an array, then the elements can be nested collections.}
\item \texttt{max($Collection$)}: The maximum of $Collection$, where $Collection$ is a nested collection of domain variables.
\item \texttt{max($Exp1$,$Exp2$)}: The maximum of $Exp1$ and $Exp2$.
\item \texttt{min($Collection$)}: The minimum of $Collection$, where $Collection$ is a nested collection of domain variables.
\item \texttt{min($Exp1$,$Exp2$)}: The minimum of $Exp1$ and $Exp2$.
\item \texttt{prod($Collection$)}: The product of $Collection$, where $Collection$ is a nested collection of domain variables.
\item \texttt{sum($Collection$)}: The sum of $Collection$, where $Collection$ is a nested collection of domain variables.
\end{itemize}
When a constraint occurs in an arithmetic expression, it is evaluated to 1 if it is satisfied and 0 if it is not satisfied.

\subsection*{Example}
\begin{verbatim}
    import mip.

    go =>
        M = {{0,3,2,3,0,0,0,0},
             {0,0,0,0,0,0,5,0},
             {0,1,0,0,0,1,0,0},
             {0,0,2,0,2,0,0,0},
             {0,0,0,0,0,0,0,5},
             {0,4,0,0,2,0,0,1},
             {0,0,0,0,0,2,0,3},
             {0,0,0,0,0,0,0,0}},
        maxflow(M,1,8).

    maxflow(M,Source,Sink) =>
        N = M.length,
        X = new_array(N,N),
        foreach (I in 1..N, J in 1..N)
            X[I,J] :: 0..M[I,J]
        end,
        foreach (I in 1..N, I != Source, I != Sink)
            sum([X[J,I] : J in 1..N]) #= sum([X[I,J] : J in 1..N]) 
        end,
        Total #= sum([X[Source,I] : I in 1..N]),
        Total #= sum([X[I,Sink] : I in 1..N]),
        solve([$max(Total)],X),
        writeln(Total),
        writeln(X).
\end{verbatim}
This program uses MIP to solve the maximum integer flow problem. Given the capacity matrix \texttt{M} of a directed graph, the start vertex \texttt{Source}, and the destination vertex \texttt{Sink}, the predicate \texttt{maxflow(M,Source,Sink)} finds a maximum flow from \texttt{Source} to \texttt{Sink} over the graph. When two vertices are not connected by an arc, the capacity is given as 0. The first foreach loop specifies the domains of the variables. For each variable \texttt{X[I,J]}, the domain is restricted to integers between 0 and the capacity, \texttt{M[I,J]}. If the capacity is 0, then the variable is immediately instantiated to 0. The next foreach loop posts the conservation constraints. For each vertex \texttt{I}, if it is neither the source nor the sink, then its total incoming flow amount 
\begin{verbatim}
    sum([X[J,I] : J in 1..N])
\end{verbatim}
is equal to the total outgoing flow amount 
\begin{verbatim}
    sum([X[I,J] : J in 1..N]).
\end{verbatim}
The total flow amount is the total outgoing amount from the source, which is the same as the total incoming amount to the sink.

\section{Boolean Constraints}
A Boolean constraint takes one of the following forms:
\begin{tabbing}
aa \= aaa \= aaa \= aaa \= aaa \= aaa \= aaa \kill
\> \verb+#~+ $BoolExp$ \\
\>   $BoolExp$ \verb+#/\+ $BoolExp$ \\
\>   $BoolExp$ \verb+#^+ $BoolExp$ \\
\>   $BoolExp$ \verb+#\/+ $BoolExp$ \\
\>   $BoolExp$ \verb+#=>+ $BoolExp$ \\
\>   $BoolExp$ \verb+#<=>+ $BoolExp$ \\
\end{tabbing}
$BoolExp$ is either a Boolean constant (0 or 1), a Boolean variable (an integer-domain variable with the domain [0,1]), an arithmetic constraint, a domain constraint (in the form of \texttt{$Var$ :: $Domain$} or \texttt{$Var$ notin $Domain$}), or a Boolean constraint. As shown in Table \ref{tab:cons_ops}, the operator \verb+#~+ has the highest precedence, and the operator \verb+#<=>+ has the lowest precedence. Note that the Boolean constraint operators have lower precedence than the arithmetic constraint operators. So the constraint 
\begin{verbatim}
     X #!= 3 #/\ X#!= 5 #<=> B
\end{verbatim}
is interpreted as
\begin{verbatim}
     ((X #!= 3) #/\ (X#!= 5)) #<=> B.
\end{verbatim}
The Boolean constraint operators are defined as follows.
\begin{itemize}
\item {\tt \verb+#~+ $BoolExp$}: This constraint is 1 iff $BoolExp$ is equal to 0.
\item {\tt $BoolExp1$ \verb+#/\+ $BoolExp2$}: This constraint is 1 iff both $BoolExp1$ and $BoolExp2$ are 1.
\item {\tt $BoolExp1$ \verb+#^+ $BoolExp2$}:  This constraint is 1 iff exactly one of $BoolExp1$ and $BoolExp2$ is 1.
\item {\tt $BoolExp1$ \verb+#\/+ $BoolExp2$}:  This constraint is 1 iff $BoolExp1$ or $BoolExp2$ is 1.
\item {\tt $BoolExp1$ \verb+#=>+ $BoolExp2$}: This constraint is 1 iff $BoolExp1$ implies $BoolExp2$.
\item {\tt $BoolExp1$ \verb+#<=>+ $BoolExp2$}: This constraint is 1 iff $BoolExp1$ and $BoolExp2$ are equivalent.
\end{itemize}

\section{Global Constraints}
A global constraint is a constraint over multiple variables. A global constraint can normally be translated into a set of smaller constraints, such as arithmetic and Boolean constraints. If the \texttt{cp} module is used, then global constraints are not translated into smaller constraints; rather, they are compiled into special propagators that maintain a certain level of consistency for the constraints. In Picat, constraint propagators are encoded as action rules\index{action rule}. If the \texttt{sat} module is used, then global constraints are translated into smaller constraints before being translated further into conjunctive normal form. If the \texttt{mip} module is used, then global constraints are decomposed into equality and disequality constraints.

Picat provides the following global constraints.

\begin{itemize}
\item \texttt{acyclic($Vs$,$Es$)}\index{\texttt{acyclic/2}}: This constraint ensures that the \textit{undirected}\index{undirected graph} graph represented by $Vs$ and $Es$ contains no cycles, where $Vs$ is a list of pairs of the form $\{V,B\}$ and $Es$ is a list of triplets of the form $\{V_1,V_2,B\}$. A pair $\{V,B\}$ in $Vs$, where $V$ is a ground term and $B$ is a Boolean variable, denotes that $V$ is in the graph if and only if $B = 1$. A triplet $\{V_1,V_2,B\}$ denotes that $V_1$ is connected with $V_2$  by an edge in the graph if and only if $B = 1$ and both $V_1$ and $V_2$ are in the graph. Note that the graph to be constructed is assumed to be undirected. If there exists a triplet $\{V_1,V_2,B\}$ in $Es$, then the triplet $\{V_2,V_1,B\}$ will be added to $Es$ if it is not specified.

\item \texttt{acyclic\_d($Vs$,$Es$)}\index{\texttt{acyclic\_d/2}}: This constraint ensures that the \textit{directed}\index{directed graph} graph represented by $Vs$ and $Es$ contains no cycles, where $Vs$ and $Es$ are the same as those in \texttt{acyclic($Vs$,$Es$)}, except that the graph is directed.
    
\item \texttt{all\_different($FDVars$)}\index{\texttt{all\_different/1}}: This constraint ensures that each pair of variables in the list or array $FDVars$ is different. This constraint is compiled into a set of inequality constraints. For each pair of variables $V1$ and $V2$ in $FDVars$, \texttt{all\_different($FDVars$)}\index{\texttt{all\_different/1}} generates the constraint {\tt $V1$ \verb+#!=+ $V2$}.

\item \texttt{all\_distinct($FDVars$)}\index{\texttt{all\_distinct/1}}: This constraint is the same as \texttt{all\_different}\index{\texttt{all\_different/1}}, but for the \texttt{cp} module it maintains a higher level of consistency. For some problems, this constraint is faster and requires fewer backtracks than \texttt{all\_different}\index{\texttt{all\_different/1}}, and, for some other problems, this constraint is slower due to the overhead of consistency checking.

\item \texttt{all\_different\_except\_0($FDVars$)}\index{\texttt{all\_different\_except\_0/1}}: This constraint is true if all non-zero values in $FDVars$ are different.
\item \texttt{assignment($FDVars1$,$FDVars2$)}\index{\texttt{assignment/2}}: This constraint ensures that $FDVars2$ is a \emph{dual assignment} of $FDVars1$, i.e., if the $i$th element of $FDVars1$ is $j$, then the $j$th element of $FDVars2$ is $i$. The constraint can be defined as:
\begin{verbatim}
    assignment(Xs,Ys) =>
        N = Xs.length,
        (var(Ys) -> Ys = new_list(N); true),
        Xs :: 1..N,
        Ys :: 1..N,
        foreach (I in 1..N, J in 1..N)
            X[I] #= J #<=> Y[J] #= I
        end.
\end{verbatim}

\item \texttt{at\_least($N$,$L$,$V$)}:\index{\texttt{atleast/3}} This constraint succeeds if there are at least $N$ elements in $L$ that are equal to $V$, where $N$ and $V$ must be integer-domain variables, and $L$ must be a list of integer-domain variables.

\item \texttt{at\_most($N$,$L$,$V$)}:\index{\texttt{atmost/3}} This constraint succeeds if there are at most $N$ elements in $L$ that are equal to $V$, where $N$ and $V$ must be integer-domain variables, and $L$ must be a list of integer-domain variables.

\item \texttt{circuit($FDVars$)}\index{\texttt{circuit/1}}: Let $FDVars$ be a list of variables \texttt{[$X_1,X_2,\ldots,X_N$]}, where each \texttt{$X_i$} has the domain $1..N$. A valuation $X_1 = v_1$, $X_2 = v_2$, $\ldots$, $X_n = v_n$ satisfies the constraint if \texttt{1 -> $v_1$, 2 -> $v_2$, ..., n -> $v_n$} forms a Hamiltonian cycle\index{Hamiltonian cycle}. This constraint ensures that each variable has a different value, and that the graph that is formed by the assignment does not contain any sub-cycles. For example, for the constraint\index{\texttt{circuit/1}} 
\begin{verbatim}
    circuit([X1,X2,X3,X4])
\end{verbatim}
\texttt{[3,4,2,1]} is a solution, but \texttt{[2,1,4,3]} is not, because the graph \texttt{1 -> 2, 2 -> 1, 3 -> 4, 4 -> 3} contains two sub-cycles.
\item \texttt{count($V$,$FDVars$,$Rel$,$N$)}\index{\texttt{count/4}}:  In this constraint, $V$ and $N$ are integer-domain variables, $FDVars$ is a list of integer-domain variables, and $Rel$ is an arithmetic constraint operator (\verb+#=+,  \verb+#!=+,  \verb+#>+,  \verb+#>=+,  \verb+#<+,  \verb+#=<+, or \verb+#<=+). Let $Count$ be the number of elements in $FDVars$ that are equal to $V$. The constraint is true iff $Count$ $Rel$ $N$ is true. This constraint can be defined as follows:
\begin{verbatim}
    count(V,L,Rel,N) =>
       sum([V #= E : E in L]) #= Count,
       call(Rel,Count,N).
\end{verbatim}
\item \texttt{count($V$,$FDVars$,$N$)}\index{\texttt{count/3}}: This constraint is the same as \texttt{count}($V$,$FDVars$,\verb+#=+,$N$).

\item \texttt{cumulative($Starts$,$Durations$,$Resources$,$Limit$)}\index{\texttt{cumulative/4}}: This constraint is useful for describing and solving scheduling problems. The arguments $Starts$, $Durations$, and $Resources$ are lists of integer-domain variables of the same length, and $Limit$ is an integer-domain variable. Let $Starts$ be \texttt{[$S_1$, $S_2$, $\ldots$, $S_n$]}, $Durations$ be \texttt{[$D_1$, $D_2$, $\ldots$, $D_n$]}, and $Resources$ be \texttt{[$R_1$, $R_2$, $\ldots$, $R_n$]}. For each job \texttt{$i$}, \texttt{$S_i$} represents the start time, \texttt{$D_i$} represents the duration, and \texttt{$R_i$} represents the units of resources needed. $Limit$ is the limit on the units of resources available at any time. This constraint ensures that the limit cannot be exceeded at any time.

\item \texttt{decreasing($L$)}\index{\texttt{decreasing/1}}: The sequence (an array or a list) $L$ is in (non-strictly) decreasing order.

\item \texttt{decreasing\_strict($L$)}\index{\texttt{decreasing\_strict/1}}: The sequence (an array or a list) $L$ is in strictly decreasing order.

\item \texttt{diffn($RectangleList$)}\index{\texttt{diffn/1}}: This constraint ensures that no two rectangles in $RectangleList$ overlap with each other. A rectangle in an $n$-dimensional space is represented by a list of $2\times n$ elements \texttt{[$X_1$, $X_2$, $\ldots$, $X_n$, $S_1$, $S_2$, $\ldots$, $S_n$]}, where \texttt{$X_i$} is the starting coordinate of the edge in the $i$th dimension, and \texttt{$S_i$} is the size of the edge.

\item \texttt{disjunctive\_tasks($Tasks$)}\index{\texttt{disjunctive\_tasks/1}}: $Tasks$ is a list of terms.  Each term has the form \\ \texttt{disj\_tasks($S_1$,$D_1$,$S_2$,$D_2$)}, where \texttt{$S_1$} and \texttt{$S_2$} are two integer-domain variables, and \texttt{$D_1$} and \texttt{$D_2$} are two positive integers. This constraint is equivalent to posting the disjunctive constraint $S_1$+$D_1$ \verb+#=<+ $S_2$ \verb+#\/+ $S_2$+$D_2$ \verb+#=<+ $S_1$ for each term in $Tasks$; however the constraint may be more efficient, because it converts the disjunctive tasks into global constraints. 

\item \texttt{element($I$,$List$,$V$)}\index{\texttt{element/3}}: This constraint is true if the $I$th element of $List$ is $V$, where $I$ and $V$ are integer-domain variables, and $List$ is a list of integer-domain variables.

\item \texttt{element0($I$,$List$,$V$)}\index{\texttt{element0/3}}: This constraint is the same as \texttt{element($I$,$List$,$V$)}, except that 0-based, rather than 1-based, indexing is used.

\item \texttt{exactly($N$,$L$,$V$)}\index{\texttt{exactly/3}}: This constraint succeeds if there are exactly $N$ elements in $L$ that are equal to $V$, where $N$ and $V$ must be integer-domain variables, and $L$ must be a list of integer-domain variables.

\item \texttt{global\_cardinality($List$,$Pairs$)}\index{\texttt{global\_cardinality/2}}: Let $List$ be a list of integer-domain variables \texttt{[$X_1$, $\ldots$, $X_d$]}, and $Pairs$ be a list of pairs \texttt{[$K_1$-$V_1$, $\ldots$, $K_n$-$V_n$]}, where each key \texttt{$K_i$} is a unique integer, and each \texttt{$V_i$} is an integer-domain variable. The constraint is true if every element of $List$ is equal to some key, and, for each pair \texttt{$K_i$-$V_i$}, exactly \texttt{$V_i$} elements of $List$ are equal to \texttt{$K_i$}. This constraint can be defined as follows:
\begin{verbatim}
    global_cardinality(List,Pairs) =>
        foreach ($Key-V in Pairs) 
            sum([B : E in List, B#<=>(E#=Key)]) #= V
        end.
\end{verbatim}

\item \texttt{hcp($Vs$,$Es$)}\index{\texttt{hcp/2}}: This constraint ensures that the \textit{directed}\index{directed graph} graph represented by $Vs$ and $Es$ forms a Hamiltonian cycle\index{Hamiltonian cycle}, where $Vs$ is a list of pairs of the form $\{V,B\}$, and $Es$ is a list of triplets of the form $\{V_1,V_2,B\}$. A pair $\{V,B\}$ in $Vs$, where $V$ is a ground term and $B$ is a Boolean variable, denotes that $V$ is in the graph if and only if $B = 1$. A triplet $\{V_1,V_2,B\}$ denotes that $V_1$ is connected to $V_2$  by an edge in the graph if and only if $B = 1$. The \texttt{circuit} and \texttt{subcircuit} constraints can be implemented as follows by using \texttt{hcp}:
\begin{verbatim}
    circuit(L) =>
        N = len(L),
        L :: 1..N,
        Vs = [{I,1} : I in 1..N],
        Es = [{I,J,B} : I in 1..N, 
                        J in fd_dom(L[I]), 
                        J !== I, 
                        B #<=> L[I] #= J],
        hcp(Vs,Es).

    subcircuit(L) =>
        N = len(L),
        L :: 1..N,
        Vs = [{I,B} : I in 1..N, 
                      B #<=> L[I] #!= I],
        Es = [{I,J,B} : I in 1..N, 
                        J in fd_dom(L[I]), 
                        J !== I, 
                        B #<=> L[I] #= J],
        hcp(Vs,Es).
\end{verbatim}

\item \texttt{hcp($Vs$,$Es$,$K$)}\index{\texttt{hcp/3}}:  This constraint is the same as \texttt{hcp($Vs$,$Es$)}, except that it also constrains the number of vertices in the graph to be $K$. 

\item \texttt{hcp\_grid($A$)}\index{\texttt{hcp\_grid/1}}:  This constraint ensures that the \textit{grid}\index{grid graph} graph represented by $A$, which is a two-dimensional array of Boolean (0/1) variables, forms a Hamiltonian cycle. In a grid graph, each cell is directly connected horizontally and vertically, but not diagonally, to its neighbors. Only cells labeled 1 are considered as vertices of the graph. This constraint is implemented as follows by using \texttt{hcp}:
\begin{verbatim}
    hcp_grid(A) =>
        NRows = len(A),
        NCols = len(A[1]),
        Vs = [{(R,C), A[R,C]} : 
                    R in 1..NRows, 
                    C in 1..NCols],
        Es = [{(R,C), (R1,C1), _} : 
                    R in 1..NRows, 
                    C in 1..NCols,
                    (R1,C1) in neibs(A,NRows,NCols,R,C)],
        hcp(Vs,Es).

    neibs(A,NRows,NCols,R,C) = 
        [(R1,C1) : (R1,C1) in [(R-1,C), (R+1,C), 
                               (R,C-1), (R,C+1)],
                    R1 >= 1, R1 =< NRows,
                    C1 >= 1, C1 =< NCols, 
                    A[R1,C1] !== 0].
\end{verbatim}

\item \texttt{hcp\_grid($A$,$Es$)}\index{\texttt{hcp\_grid/2}}: This constraint is the same as \texttt{hcp\_grid($A$)}, except that it also restricts the edges to $Es$, which consists of triplets of the form $\{V_1,V_2,B\}$. In a triplet in $Es$, $V_1$ and $V_2$ take the form $(R,C)$, where $R$ is a row number and $C$ is a column number, and $B$ is a Boolean variable, which denotes that $V_1$ is connected to $V_2$  by an edge in the graph if and only if $B = 1$. If $Es$ is a variable, then it is bound to the edges of the grid graph.  

\item \texttt{hcp\_grid($A$,$Es$,$K$)}\index{\texttt{hcp\_grid/3}}: This constraint is the same as\\ \texttt{hcp\_grid($A$,$Es$)}, except that it also constrains the number of vertices in the graph to be $K$. 

\item \texttt{increasing($L$)}\index{\texttt{increasing/1}}: The sequence (an array or a list) $L$ is in (non-strictly) increasing order.

\item \texttt{increasing\_strict($L$)}\index{\texttt{increasing\_strict/1}}: The sequence (an array or a list) $L$ is in strictly increasing order.

\item \texttt{lex\_le($L_1$,$L_2$)}\index{\texttt{lex\_le/2}}: The sequence (an array or a list) $L_1$ is lexicographically less than or equal to $L_2$.

\item \texttt{lex\_lt($L_1$,$L_2$)}\index{\texttt{lex\_lt/2}}: The sequence (an array or a list) $L_1$ is lexicographically less than $L_2$.

\item \texttt{matrix\_element($Matrix$,$I$,$J$,$V$)}\index{\texttt{matrix\_element/4}}: This constraint is true if the entry at $<$$I$,$J$$>$ in $Matrix$ is $V$, where $I$, $J$, and $V$ are integer-domain variables, and $Matrix$ is an two-dimensional array of integer-domain variables.

\item \texttt{matrix\_element0($Matrix$,$I$,$J$,$V$)}\index{\texttt{matrix\_element0/4}}: This constraint is the same as the above, except that it uses 0-based, rather than 1-based, indexing for the entries of the matrix. 

\item \texttt{neqs($NeqList$)}\index{\texttt{neqs/1}}: $NeqList$ is a list of inequality constraints of the form $X$ \verb+#!=+ $Y$, where $X$ and $Y$ are integer-domain variables. This constraint is equivalent to the conjunction of the inequality constraints in $NeqList$, but it extracts \texttt{all\_distinct}\index{\texttt{all\_distinct/1}} constraints from the inequality constraints. 

\item \texttt{nvalue($N$,$List$)}\index{\texttt{nvalue/2}}: The number of distinct values in $List$ is $N$, where $List$ is a list of integer-domain variables.

\item \texttt{path($Vs$,$Es$,$Src$,$Dest$)}\index{\texttt{path/4}}: This constraint ensures that the \textit{undirected}\index{undirected graph} graph represented by $Vs$ and $Es$ is a path from $Src$ to $Dest$, where $Vs$ is a list of pairs of the form $\{V,B\}$, $Es$ is a list of triplets of the form $\{V_1,V_2,B\}$, $Src$ is a vertex, and $Dest$ is a vertex or a list of vertices. A pair $\{V,B\}$ in $Vs$, where $V$ is a ground term representing a vertex and $B$ is a Boolean variable, denotes that $V$ is in the graph if and only if $B = 1$. A triplet $\{V_1,V_2,B\}$ denotes that $V_1$ is connected with $V_2$  by an edge in the graph if and only if $B = 1$ and both $V_1$ and $V_2$ are in the graph. Note that the graph to be constructed is assumed to be undirected. If there exists a triplet $\{V_1,V_2,B\}$ in $Es$, then the triplet $\{V_2,V_1,B\}$ will be added to $Es$ if it is not specified.

\item \texttt{path\_d($Vs$,$Es$,$Src$,$Dest$)}\index{\texttt{path\_d/4}}: This constraint ensures that the \textit{directed}\index{directed graph} graph represented by $Vs$ and $Es$ is a path from $Src$ to $Dest$, where $Vs$ and $Es$ are the same as those in \texttt{path($Vs$,$Es$,$Src$,$Dest$)}, except that the graph is directed.

\item \texttt{regular$(L, Q, S, M, Q0, F)$}\index{\texttt{regular/6}}:  Given a finite automaton (DFA or NFA) of $Q$ states numbered 1, 2, $\ldots$, $Q$ with input $1..S$, transition matrix $M$, initial state $Q0$ (1 $\le\ Q0\ \le Q$), and a list of accepting states $F$, this constraint is true if the list $L$ is accepted by the automaton. The transition matrix $M$ represents a mapping from $1..Q\times 1..S$ to $0..Q$, where $0$ denotes the {\it error} state.  For a DFA, every entry in $M$ is an integer, and for an NFA, entries can be a list of integers.

\item \texttt{scalar\_product($A$,$X$,$Product$)}\index{\texttt{scalar\_product/3}}: The scalar product of $A$ and $X$ is $Product$, where $A$ and $X$ are lists or arrays of integer-domain variables, and $Product$ is an integer-domain variable. $A$ and $X$ must have the same length.

\item \texttt{scalar\_product($A$,$X$,$Rel$,$Product$)}\index{\texttt{scalar\_product/4}}: The scalar product of $A$ and $X$ has the relation $Rel$ with $Product$, where $Rel$ is one of the following operators: \verb+#=+, \verb+#!=+, \verb+#>=+, \verb+#>+, \verb+#=<+ (\verb+#<=+), and \verb+#<+. 

\item \texttt{scc($Vs$,$Es$)}\index{\texttt{scc/2}}: This constraint ensures that the \textit{undirected}\index{undirected graph} graph represented by $Vs$ and $Es$ is strongly connected, where $Vs$ is a list of pairs of the form $\{V,B\}$, and $Es$ is a list of triplets of the form $\{V_1,V_2,B\}$. A pair $\{V,B\}$ in $Vs$, where $V$ is a ground term and $B$ is a Boolean variable, denotes that $V$ is in the graph if and only if $B = 1$. A triplet $\{V_1,V_2,B\}$ denotes that $V_1$ is connected with $V_2$  by an edge in the graph if and only if $B = 1$ and both $V_1$ and $V_2$ are in the graph. Note that the graph to be constructed is assumed to be undirected. If there exists a triplet $\{V_1,V_2,B\}$ in $Es$, then the triplet $\{V_2,V_1,B\}$ will be added to $Es$ if it is not specified.

\item \texttt{scc($Vs$,$Es$,$K$)}\index{\texttt{scc/3}}:  This constraint is the same as \texttt{scc($Vs$,$Es$)}, except that it also constrains the number of vertices in the graph to be $K$. 

\item \texttt{scc\_grid($A$)}\index{\texttt{scc\_grid/1}}:  This constraint ensures that the grid\index{grid graph} graph represented by $A$, which is a two-dimensional array of Boolean variables, forms a strongly connected undirected graph. In a grid graph, each cell is directly connected horizontally and vertically, but not diagonally, to its neighbors. Only cells labeled 1 are considered as vertices of the graph. This constraint is implemented as follows by using \texttt{scc}:
\begin{verbatim}
    scc_grid(A) =>
        NRows = len(A),
        NCols = len(A[1]),
        Vs = [{(R,C), A[R,C]} : 
                    R in 1..NRows, 
                    C in 1..NCols],
        Es = [{(R,C), (R1,C1), _} : 
                    R in 1..NRows, 
                    C in 1..NCols,
                    (R1,C1) in neibs(A,NRows,NCols,R,C),
                    (R,C) @< (R1,C1)],
        scc(Vs,Es).

    neibs(A,NRows,NCols,R,C) = 
        [(R1,C1) : (R1,C1) in [(R-1,C), (R+1,C), 
                               (R,C-1), (R,C+1)],
                    R1 >= 1, R1 =< NRows,
                    C1 >= 1, C1 =< NCols, 
                    A[R1,C1] !== 0].
\end{verbatim}
Note that there is an edge between each pair of neighboring cells in the resulting graph as long as the cells are in the graph.

\item \texttt{scc\_grid($A$,$K$)}\index{\texttt{scc\_grid/2}}: This constraint is the same as \texttt{scc\_grid($A$,$Es$)}, except that it also constrains the number of vertices in the graph to be $K$. 

\item \texttt{scc\_d($Vs$,$Es$)}\index{\texttt{scc\_d/2}}: This constraint ensures that the \textit{directed}\index{directed graph} graph represented by $Vs$ and $Es$ is strongly connected, where $Vs$ and $Es$ are the same as those in \texttt{scc($Vs$,$Es$)}, except that the graph is directed.

\item \texttt{scc\_d($Vs$,$Es$,$K$)}\index{\texttt{scc\_d/3}}: This constraint is the same as \texttt{scc\_d($Vs$,$Es$)}, except that it also constrains the number of vertices in the graph to be $K$. 

\item \texttt{serialized($Starts$,$Durations$)}\index{\texttt{serialized/2}}: This constraint describes a set of non-overlapping tasks, where $Starts$ and $Durations$ are lists of integer-domain variables, and the lists have the same length. Let $Os$ be a list of 1s that has the same length as $Starts$. This constraint is equivalent to \texttt{cumulative($Starts$,$Durations$,$Os$,1)}\index{\texttt{cumulative/4}}.

\item \texttt{subcircuit($FDVars$)}\index{\texttt{subcircuit/1}}: This constraint is the same as \texttt{circuit($FDVars$)}\index{\texttt{circuit/1}}, except that not all of the vertices are required to be in the circuit. If the $i$th element of $FDVars$ is $i$, then the vertex $i$ is not part of the circuit. 

\item \texttt{subcircuit\_grid($A$)}\index{\texttt{subcircuit\_grid/1}}: This constraint ensures that the grid\index{grid graph} graph represented by $A$, which is a two-dimensional array of Boolean (0/1) variables, forms a Hamiltonian cycle\index{Hamiltonian cycle}. In a grid graph, each cell is directly connected horizontally and vertically, but not diagonally, to its neighbors. Only non-zero cells are considered as vertices of the graph.

\item \texttt{subcircuit\_grid($A$,$K$)}\index{\texttt{subcircuit\_grid/2}}: This constraint is the same as \\\texttt{subcircuit\_grid($A$)}, except that it also constrains the number of vertices in the graph to be $K$, 

\item \texttt{tree($Vs$,$Es$)}\index{\texttt{tree/2}}: This constraint ensures that the \textit{undirected}\index{undirected graph} graph represented by $Vs$ and $Es$ is a tree, where $Vs$ is a list of pairs of the form $\{V,B\}$, and $Es$ is a list of triplets of the form $\{V_1,V_2,B\}$. A pair $\{V,B\}$ in $Vs$, where $V$ is a ground term and $B$ is a Boolean variable, denotes that $V$ is in the tree if and only if $B = 1$. A triplet $\{V_1,V_2,B\}$ denotes that $V_1$ is connected with $V_2$  by an edge in the tree if and only if $B = 1$ and both $V_1$ and $V_2$ are in the tree. Note that the graph to be constructed is assumed to be undirected. If there exists a triplet $\{V_1,V_2,B\}$ in $Es$, then the triplet $\{V_2,V_1,B\}$ will be added to $Es$ if it is not specified.

\item \texttt{tree($Vs$,$Es$,$K$)}\index{\texttt{tree/3}}:  This constraint is the same as \texttt{tree($Vs$,$Es$)}, except that it also constrains the number of vertices in the tree to be $K$. 
\end{itemize}

\section{Bit-Vector Constraints (sat only)}
The \texttt{sat} module supports bit-vector constraints, which enhance modeling capabilities, improve solving performance, and support large integers that exceed the bounds of domain values. A bit vector is an array representing the binary form of an unsigned integer. Let $V$ be a bit vector of length $N$. The bit $V[1]$ corresponds to the least significant bit, while $V[N]$ represents the most significant bit. In a bit-vector constraint, a bit-vector argument may also be a non-negative integer. If the argument is a non-negative integer, Picat automatically converts it to its binary representation. 

\begin{itemize}
\item \texttt{bv\_add($A$,$B$,$C$)}\index{\texttt{bv\_add/3}}: Forces bit-vector $C$ to be the sum of bit-vectors $A$ and $B$.
\item \texttt{bv\_and($A$,$B$,$C$)}\index{\texttt{bv\_and/3}}: Forces bit-vector $C$ to be the bitwise \textit{and} of bit-vectors $A$ and $B$.
\item \texttt{bv\_div($A$,$B$,$C$)}\index{\texttt{bv\_div/3}}: Forces bit-vector $C$ to be the quotient of $A$ divided by $B$ ($C\ =\ A\ div\ B$).
\item \texttt{bv\_drop($A$,$N$)}\index{\texttt{bv\_drop/2}}: Returns a bit vector containing the remaining bits of bit-vector $A$ after dropping the lowest $N$ bits.
\item \texttt{bv\_eq($A$,$B$)}\index{\texttt{bv\_eq/2}}: Forces equality between bit-vector $A$ and bit-vector $B$.
\item \texttt{bv\_ge($A$,$B$)}\index{\texttt{bv\_ge/2}}: Forces bit-vector $A$ to be greater than or equal to bit-vector $B$ ($A\ \ge\ B$).
\item \texttt{bv\_gt($A$,$B$)}\index{\texttt{bv\_gt/2}}: Forces bit-vector $A$ to be greater than bit-vector $B$ ($A\ >\ B$).
\item \texttt{bv\_mod($A$,$B$,$C$)}\index{\texttt{bv\_mod/3}}: Forces bit-vector $C$ to be the remainder of $A$ divided by $B$ ($C\ =\ A\ mod\ B$).
\item \texttt{bv\_mul($A$,$B$,$C$)}\index{\texttt{bv\_mul/3}}: Forces bit-vector $C$ to be the product of multiplying $A$ and $B$.
\item \texttt{bv\_neq($A$,$B$)}\index{\texttt{bv\_neq/2}}: Forces inequality between bit-vector $A$ and bit-vector $B$ ($A\ \neq\ B$).
\item \texttt{bv\_or($A$,$B$,$C$)}\index{\texttt{bv\_or/3}}: Forces bit-vector $C$ to be the bitwise \textit{or} of bit-vectors $A$ and $B$.
\item \texttt{bv\_pow($A$,$B$,$C$)}\index{\texttt{bv\_pow/3}}: Forces bit-vector $C$ to be $A$ raised to the power of $B$ ($C\ =\ A^B$).
\item \texttt{bv\_sum($As$,$B$)}\index{\texttt{bv\_sum/2}}: Forces the sum of bit-vectors in $As$ to be equal to bit-vector $B$, where $As$ is a list of bit vectors.
\item \texttt{bv\_take($A$,$N$)}\index{\texttt{bv\_take/2}}: Returns a bit vector containing the lowest $N$ bits of bit-vector $A$.
\item \texttt{bv\_to\_int($A$)}\index{\texttt{bv\_to\_int/1}}: Returns an integer represented by bit-vector $A$. This function throws an exception if $A$ is not ground.
\item \texttt{bv\_xor($A$,$B$,$C$)}\index{\texttt{bv\_xor/3}}: Forces bit-vector $C$ to be the bitwise \textit{xor} of bit-vectors $A$ and $B$.
\item \texttt{int\_to\_bv($C$)}\index{\texttt{int\_to\_bv/1}}: Returns the binary representation of integer $C$ as a bit vector.
\item \texttt{new\_bv($N$)}\index{\texttt{new\_bv/1}}: Returns a bit vector of length $N$.
\end{itemize}

\section{Solver Invocation}
\begin{itemize}
\item \texttt{solve($Opts$,$Vars$)}\index{\texttt{solve/2}}: This predicate calls the imported solver to label the variables $Vars$ with values, where $Opts$ is a list of options for the solver. The options will be detailed below. This predicate can backtrack in order to find multiple solutions. The \texttt{cp} module allows \textit{incremental labeling} of variables, and some variables that occur in constraints but are not passed to \texttt{solve} may remain uninstantiated after a call to \texttt{solve}. The user is responsible for having all the variables that need to be instantiated passed to \texttt{solve}. In constrast, the \texttt{sat} and \texttt{mip} modules do not support incremental labeling of variables.

\item\texttt{solve($Vars$)}\index{\texttt{solve/1}}: This predicate is the same as \texttt{solve([],$Vars$)}\index{\texttt{solve/2}}. 
\item \texttt{indomain($Var$)}\index{\texttt{indomain/1}}: This predicate is only accepted by \texttt{cp}. It is the same as \texttt{solve([], [$Var$])}\index{\texttt{solve/2}}.
\item \texttt{solve\_all($Opts$,$Vars$) = $Solutions$}\index{\texttt{solve\_all/2}}: This function returns all the solutions that satisfy the constraints.
\item \texttt{solve\_all($Vars$) = $Solutions$}\index{\texttt{solve\_all/1}}: This function is the same as \texttt{solve\_all([],$Vars$)}.
\item \texttt{solve\_suspended($Opts$)}\index{\texttt{solve\_suspended/1}}: After \texttt{solve($Vars$)} has successfully labeled $Vars$, some constraints may remain suspended and not completely checked because not all of the decision variables are included in $Vars$. The \texttt{solve\_suspended($Opts$)} predicate labels all remaining variables in the suspended constraints. This predicate is only provided by the \texttt{cp} module. 
\item \texttt{solve\_suspended}\index{\texttt{solve\_suspended/0}}: This predicate is the same as \texttt{solve\_suspended([])}.
\item \texttt{indomain\_down($Var$)}\index{\texttt{indomain\_down/1}}: This predicate is the same as \texttt{solve([down], [$Var$])}\index{\texttt{solve/2}}. It is only accepted by \texttt{cp}. 
\end{itemize}


\subsection{Common Solving Options}
The following options are accepted by all four of the solvers.\footnote{Note that the preceding dollar sign indicates that it is followed by a term, not a function call.}
\begin{itemize}
\item \texttt{\$limit($N$)}: Search up to $N$ solutions.
\item \texttt{\$max($Var$)}: Maximize the variable $Var$.
\item \texttt{\$min($Var$)}: Minimize the variable $Var$.
\item \texttt{\$report($Call$)}: Execute $Call$ each time a better answer is found while searching for an optimal answer. This option cannot be used if the {\tt mip} module is used.
\end{itemize}

\subsection{Solving Options for \texttt{cp}}
The \texttt{cp} module also accepts the following options:
\begin{itemize}
\item \texttt{backward}: The list of variables is reversed first.
\item \texttt{constr}: Variables are first ordered by the number of attached constraints.
\item \texttt{degree}: Variables are first ordered by degree, i.e., the number of connected variables.
\item \texttt{down}: Values are assigned to variables from the largest to the smallest.
\item \texttt{ff}: The first-fail principle\index{first-fail principle} is used: the leftmost variable with the smallest domain is selected.
\item \texttt{ffc}: The same as with the two options: \texttt{ff} and \texttt{constr}.
\item \texttt{ffd}: The same as with the two options: \texttt{ff} and \texttt{degree}.
\item \texttt{forward}: Choose variables in the given order, from left to right.
\item \texttt{inout}: The variables are reordered in an inside-out fashion. For example, the variable list \texttt{[X1,X2,X3,X4,X5]} is rearranged into the list \texttt{[X3,X2,X4,X1,X5]}.
\item \texttt{label($CallName$)}: This option informs the CP solver that once a variable $V$ is selected, the user-defined call \texttt{$CallName$($V$)} is used to label $V$, where $CallName$ must be defined in the same module, an imported module, or the global module.
\item \texttt{leftmost}: The same as \texttt{forward}.
\item \texttt{max}: First, select a variable whose domain has the largest upper bound, breaking ties by selecting a variable with the smallest domain.
\item \texttt{min}: First, select a variable whose domain has the smallest lower bound, breaking ties by selecting a variable with the smallest domain.
\item \texttt{rand}: Both variables and values are randomly selected when labeling.
\item \texttt{rand\_var}: Variables are randomly selected when labeling.
\item \texttt{rand\_val}: Values are randomly selected when labeling.
\item \texttt{reverse\_split}: Bisect the variable's domain, excluding the lower half first.
\item \texttt{split}: Bisect the variable's domain, excluding the upper half first.
\item \texttt{updown}: Values are assigned to variables from the values that are nearest to the middle of the domain.
\end{itemize}

\subsection{Solving Options for \texttt{sat}}
\begin{itemize}
\item \texttt{dump}: Dump the CNF code to \texttt{stdout}.
\item \texttt{dump($File$)}: Dump the CNF code to $File$.
\item \texttt{seq}: Use sequential search to find an optimal answer.
\item \texttt{split}: Use binary search to find an optimal answer (default).
\item \texttt{\$nvars$(NVars)$}: The number of variables in the CNF code is $NVars$.
\item \texttt{\$ncls$(NCls)$}: The number of clauses in the CNF code is $NCls$.
%\item \texttt{\$threads($N$)}: Use $N$ threads to solve the generated CNF code. In the current implementation, the parallel version of lingeling, \texttt{plingeling}, will be used if this option is given.\index{parallel}\index{threads}
%\item \texttt{threads}: The same as \texttt{\$threads(8)}.
\end{itemize}

\subsection{Solving Options for \texttt{mip}}
\begin{itemize}
\item \texttt{cbc}: \index{\texttt{cbc}}Instruct Picat to use the Cbc MIP solver. Picat uses the following command to call the Cbc solver:
\begin{tabbing}
aa \= aaa \= aaa \= aaa \= aaa \= aaa \= aaa \kill
\> \texttt{cbc $TmpFile$ solve -solu $SolFile$ }
\end{tabbing}
where $SolFile$ is a file for the solution, and $TmpFile$ is a file that stores the CPLEX-format constraints. Picat throws \texttt{existence\_error} if the command \texttt{cbc} is not available.
\item \texttt{dump}: Dump the constraints in CPLEX format to \texttt{stdout}.
\item \texttt{dump($File$)}: Dump the CPLEX format to $File$.
\item \texttt{glpk}: \index{\texttt{glpk}}Instruct Picat to use the GLPK MIP solver. Picat uses the following command to call the GLPK solver:
\begin{tabbing}
aa \= aaa \= aaa \= aaa \= aaa \= aaa \= aaa \kill
\> \texttt{glpsol --lp -o $SolFile$ $TmpFile$}
\end{tabbing}
where $SolFile$ is a solution file, and $TmpFile$ is a file that stores the CPLEX-format constraints. Picat throws \texttt{existence\_error} if the command \texttt{glpsol} is not available.
\item \texttt{gurobi}: \index{\texttt{gurobi}}Instruct Picat to use the Gurobi MIP solver. Picat uses the following command to call the Gurobi solver:
\begin{tabbing}
aa \= aaa \= aaa \= aaa \= aaa \= aaa \= aaa \kill
\> \texttt{gurobi\_cl ResultFile=$SolFile$ $TmpFile$}
\end{tabbing}
where $SolFile$ is a file for the solution, and $TmpFile$ is a file that stores the CPLEX-format constraints. Picat throws \texttt{existence\_error} if the command \texttt{gurobi\_cl} is not available.
% \item \texttt{sol($File$)}: Instruct the mip solver to save the solution into $File$ rather than the default file ``\_\_tmp.sol''.
\item \texttt{scip}: \index{\texttt{scip}}Instruct Picat to use the SCIP MIP solver. Picat provides an internal interface to the SCIP solver through the C language.\footnote{The SCIP interface is not included in the pre-made binary executables, and users must build an executable from the source code following the installation instructions.} Note that, unlike other linear programming solvers, SCIP only returns integer solutions although it allows real coefficients and contiguous-domain variables.
\end{itemize}

\subsection{Solving Options for \texttt{smt}}
\begin{itemize}
\item \texttt{cvc4}: \index{\texttt{cvc3}}Instruct Picat to use the CVC4 SMT solver. Picat uses the following command to call the CVC4 solver:
\begin{tabbing}
aa \= aaa \= aaa \= aaa \= aaa \= aaa \= aaa \kill
\> \texttt{cvc4 $TmpFile$ $>$ $SolFile$ }
\end{tabbing}
where $TmpFile$ is a file that stores the SMT-LIB2-format constraints, and $SolFile$ is a solution file. Picat throws \texttt{existence\_error} if the command \texttt{cvc4} is not available in the path.
\item \texttt{dump}: Dump the constraints in SMT-LIB2 format to \texttt{stdout}.
\item \texttt{dump($File$)}: Dump the SMT-LIB2 format to $File$.
\item \texttt{logic($Logic$)}: Instruct the SMT solver to use $Logic$ in the solving, where $Logic$ must be an atom or a string, and the specified logic must be available in the SMT solver. The default logic for Z3 is ``LIA'', and the default logic for CVC4 is ``NIA''.
\item \texttt{tmp($File$)}: Dump the SMT-LIB2 format to $File$ rather than the default file ``\_\_tmp.smt2'', before calling the smt solver. The name $File$  must be a string or an atom that has the extension name ``.smt2''. When this file name is specified, the smt solver will save the solution into a file name that has the same main name as $File$ but the extension name ``.sol''.
\item \texttt{z3}: \index{\texttt{z3}}Instruct Picat to use the z3 SMT solver. When no SMT solver is specified, Picat first searches for the command \texttt{z3}, and when \texttt{z3} cannot be found it continues to search for the command \texttt{cvc4}.
\end{itemize}
\ignore{
\end{document}
}
