1 Introduction
The Stable Marriage Problem (SMP) is a matching problem first introduced and solved by Gale and Shapley [SMPGale62]. Starting from (i) a set of men and women, (ii) for each man a ranking of the women as preferred partners, and (iii) for each woman a ranking of the men as preferred partners, the SMP searches for a set of couples (marriages) such that there are no man and woman who are in different marriages but both prefer each other to their actual partners. Such a man and woman are called a blocking pair and a matching without blocking pairs forms a stable set of marriages. Due to its practical relevance, countless variants on the SMP have been investigated, making the problem assumptions more applicable to a wider range of applications, such as kidneyexchange [SMPIrv07] and the hospitalresident problem [SMPMan02]. Recently Roth and Shapley won the Nobel Prize for Economics for their theory of stable allocations and the practice of market design, work that has directly resulted from an application of the SMP.
In the literature, typically each time a new variant on the SMP is considered, a new algorithm is developed (see e.g. [SMPGus87, SMPIrv87, SMPMc12]). In this paper, we propose to use Answer Set Programming (ASP) as a general vehicle for modeling a large class of extensions and variations of the SMP. We show how an ASP encoding allows us to express in a natural way ties in the preferences of men and women, as well as unacceptability constraints (where certain people prefer to remain single over being coupled to undesirable partners). Furthermore, we illustrate how we can use our ASP encoding to find stable matchings that are optimal according to a certain criterion. Although the SMP has been widely investigated, and efficient approximation or exact algorithms are available for several of its variants (see e.g. [SMPMc12]), to the best of our knowledge, our encoding offers the first exact implementation to find sexequal, minimum regret, egalitarian or maximum cardinality stable sets for SMP instances with unacceptability and ties.
The paper is structured as follows. In Section 2 we give some background about the SMP and ASP. We introduce our encoding of the SMP with ASP and prove its correctness in the third section. In Section 4, we extend our encoding enabling it to find optimal stable sets. We explore several notions of optimality for stable matchings and show how optimal stable matchings can be found by solving the corresponding disjunctive ASP program. Finally we draw our conclusions.
2 Background
2.1 The Stable Marriage Problem
To solve the standard SMP, Gale and Shapley [SMPGale62] constructed an iterative algorithm —known as the GaleShapley algorithm, GS algorithm or deferredacceptance algorithm— to compute a particular solution of an SMP instance. The algorithm works as follows: in round 1 every man proposes to his first choice of all women. A woman, when being proposed, then rejects all men but her first choice among the subset of men who proposed to her. That first choice becomes her temporary husband. In the next rounds, all rejected men propose to their first choice of the subset of women by whom they were not rejected yet, regardless of whether this woman already has a temporary husband. Each woman, when being proposed, then rejects all men but her first choice among the subset of men who just proposed to her and her temporary mate. This process continues until all women have a husband. This point, when everyone has a partner, is always reached after a polynomial number of steps and the corresponding set of marriages is stable [SMPGale62]. It should be noted, however, that only one of the potentially exponentially many stable matchings is found in this way. We formally define the SMP and introduce two variants that will be considered in this paper. We denote a set of men as and a set of women , with for the classical SMP. A set of marriages is a set of manwoman pairs such that each man and each woman occurs in just one pair.
Definition 1 (Classical SMP)
An instance of the classical SMP is a pair , with and sets of permutations of the integers . The permutations and are the preferences of man and woman respectively. If , we say that woman is the most preferred woman for man , and similarly for . Man and woman form a blocking pair in a set of marriages if prefers to his partner in and prefers to her partner in . A solution of an instance is a stable set of marriages, i.e. a set of marriages without blocking pairs.
A first variant of the classical SMP allows men and women to point out unacceptable partners by not including them in their preference list. The number of men can differ from the number of women since men and women can remain single. A set of marriages is a set of singles (i.e. persons paired to themselves) and manwoman pairs such that every man and woman occurs in just one pair.
Definition 2 (SMP with unacceptability)
An instance of the SMP with unacceptability is a pair , , and , , , with each a permutation of a subset of and each a permutation of a subset of . If , woman is the most preferred woman for man , and similarly for . If there is no such that , woman is an unacceptable partner for man , and similarly for no such that . A person forms a blocking individual in a set of marriages if prefers being single to being paired with his or her partner in . A solution of an instance is a stable set of marriages, i.e. a set of marriages without blocking pairs or individuals.
The length of the permutation is denoted as . A stable matching for an SMP instance with unacceptability always exists and can be found in polynomial time [SMPRoth90] by a slightly modified GS algorithm.
Example 1
Suppose , , and . Hence woman prefers man to man while man is unacceptable. In this setting, there is exactly one stable set of marriages [SMPRoth90]: . Thus woman stays single.
The second variant of the SMP allows unacceptability and ties, i.e. the preferences do not have to be strict. For this variant there are several ways to define stability, but we will use the notion of weak stability [SMPIrv94].
Definition 3 (SMP with unacceptability and ties)
An instance of the SMP with unacceptability and ties is a pair , and . For every , is a list of disjoint subsets of . Symmetrically is a list of disjoint subsets of for every . We call and the preferences of man and woman respectively. If , woman is in man ’s most preferred group of women. All the women in that group are equally preferred by . The case is similar. If there is no such that , woman is an unacceptable partner for man , and similar for no such that . For every in the set^{1}^{1}1 denotes the length of the list . , man equally prefers staying single to being paired to woman , and symmetrically for the preferences of a woman . This is the only set in that might be empty, and similar for . Man and woman form a blocking pair in a set of marriages if strictly prefers to his partner in and strictly prefers to her partner in . A blocking individual in is a person who stricly prefers being single to being paired to his partner in . A solution of an instance is a weakly stable set of marriages, i.e. a set of marriages without blocking pairs or individuals.
A weakly stable matching always exists for an instance of the SMP with unacceptability and ties and it can be found in polynomial time by arbitrarily breaking the ties [SMPIwa08]. However, as opposed to the previous variant, the number of matched persons is no longer constant for every stable set in this variant. Note that the setting of Definition 3 generalizes the setting of Definition 2, which generalizes the setting of Definition 1. We introduce the notations
Furthermore . We define the ordening on as iff prefers person at least as much as person . The strict ordening is defined in the obvious way and analogous notations are used for .
Example 2
Suppose , , , , , and = , = . Hence man prefers women and to woman . There is a tie between woman and as well as between woman and staying single. Woman is unacceptable for man . Man prefers woman and to staying single, but finds and unacceptable. It holds that , i.e. prefers marrying over staying single, acceptable, preferred, and .
2.2 Answer Set Programming
Answer Set Programming or ASP is a form of declarative programming [ASPBrew11]. Its transparence, elegance and ability to deal with complete problems make it an attractive method for solving combinatorial search and optimization problems. An ASP program is a finite collection of firstorder rules
with predicates. The semantics are defined by the ground version of the program, consisting of all ground instantiations of the rules w.r.t. the constants that appear in it (see e.g. [ASPBrew11] for a good overview). This grounded program is a propositional ASP program. The building blocks of these programs are atoms, literals and rules. The most elementary are atoms, which are propositional variables that can be true or false. A literal is an atom or a negated atom. Beside strong negation, ASP uses a special kind of negation, namely negationasfailure (naf), denoted with ‘’. For a literal we call ‘’ the nafliteral associated with . The extended literals consist of all literals and their associated nafliterals. A disjunctive rule has the following form
where are literals from a fixed set , determined by a fixed set of atoms. We call the head of the rule while the set of extended literals , is called the body. The rule above intuitively encodes that , , or is true when we have evidence that are true and we have no evidence that at least one of are true. When a rule has an empty body, we call it a fact; when the head is empty, we speak of a constraint. A rule without occurrences of is called a simple disjunctive rule. A simple disjunctive ASP program is a finite collection of simple disjunctive rules and similarly a disjunctive ASP program is a finite collection of disjunctive rules. If each rule head consists of at most one literal, we speak of a normal ASP program.
We define an interpretation of a disjunctive ASP program as a subset of . An interpretation satisfies a simple disjunctive rule when or . An interpretation which satisfies all rules of a simple disjunctive program is called a model of that program. An interpretation is an answer set of a simple disjunctive program iff it is a minimal model of , i.e. no strict subset of is a model of [ASPGel88]. The reduct of a disjunctive ASP program w.r.t. an interpretation is defined as the simple disjunctive ASP program . An interpretation of a disjunctive ASP program is an answer set of iff is an answer set of .
Example 3
Let be the ASP program with the following 4 rules:
The last rule is grounded to 2 rules in which is resp. replaced by and by . We check that the interpretation , , is an answer set of the ground version of by computing the reduct. The grounded rule with is deleted since is in . The reduct is:
The first 3 rules are facts, hence their heads will be in any answer set. The fourth rule encodes that any person who is not a man, is a woman or child. It is clear that is a minimal model of this simple program, so is an answer set of . By replacing by in , another answer set is obtained.
To automatically compute the answer sets of the programs in this paper, we have used the ASP solver DLV^{2}^{2}2Available from www.dlvsystems.com, due to its ability to handle predicates, disjunction and numeric values (with some builtin aggregate functions). The numeric values are only used for grounding.
3 Modeling the Stable Marriage Problem in ASP
In this section we model variations and generalizations of the SMP with ASP. A few proposals of using nonmonotonic reasoning for modeling the SMP have already been described in the literature. For instance in [ASPMa90] a specific variant of the SMP is mentioned (in which boys each know a subset of a set of girls and want to be matched to a girl they know) and in [SMPDung95] an abductive program is used to find a stable set of marriages in which two fixed persons are paired, with strict, complete preference lists. To the best of our knowledge, beyond a few specific examples, no comprehensive study has been made of using ASP or related paradigms in this context. In particular, the generality of our ASP framework for weakly stable sets of SMP instances with unacceptablity and/or ties is a significant advantage. The expression denotes that a man and a woman accept each other as partners. The predicate expresses that man is willing to propose to woman and analogously expresses that woman is willing to propose to man . Inspired by the GaleShapley algorithm, we look for an ASP formalisation to find the stable sets.
Definition 4 (ASP program induced by SMP with unacc. and ties)
The ASP program induced by an instance of the classical SMP with unacceptability and ties is the program containing for every the following rules:
(1)  
(2)  
(3) 
and for every , :
(4) 
and for every , :
(5) 
Intuitively (1) means that a man and woman accept each other as partners if they propose to each other. Due to (2), a man accepts himself as a partner (i.e. stays single) if no woman in his preference list is prepared to propose to him. Rule (4) states that a man proposes to a woman if he is not paired to a more or equally preferred woman. For the body of (4) contains . No explicite rules are stated about the number of persons someone can propose to or accept but Proposition 1 implies that this is unnecessary.
We illustrate the induced ASP program with an example.
Example 4
Consider the following instance of the SMP with unacceptability and ties. Let and . Furthermore:
The ASP program induced by this SMP instance is:
Notice that we use the facts and to capture all the rules of the form (1) at once. If we run this program in DLV, we get three answer sets containing respectively:

,

,

.
These correspond to the three weakly stable set of marriages of this SMP instance, namely , , , , , and , , .
Proposition 1
Let be an instance of the SMP with unacceptability and ties and let be the corresponding ASP program. If is an answer set of , then a weakly stable matching for is given by .
Proof.
Let and be as described in the proposition. Because of the symmetry between the men and the women we restrict ourselves to the male case when possible. We prove this proposition in 4 steps.

For every , every and for every answer set of , it holds that implies that and .
This can be proved by contradiction. We will prove that for every man and every , is in no answer set of the induced ASP program . For to be in an answer set , the reduct must contain some rule with this literal in the head and a true body. The only rule which can make this happen is the one of the form (1), implying that should be in . But since is not in there is no rule with in the head and so can never be in . 
For every answer set of and every man , there exists at most one woman such that . Similarly, for every woman there exists at most one man such that . Moreover, if then for any , and likewise when then for any .
This can be proved by contradiction. Suppose first that there is an answer set of that contains and for some man and two different women and . The first step implies that and are elements of . Either man prefers woman to woman () or the other way around () or man has no preference among them ( and ). The first two cases are symmetrical and can be handled analogously. The last case follows from the first case because it has stronger assumptions. We prove the first case and assume that man prefers woman to woman . The rules (4) imply the presence of a rule , and this is the only rule which can make true (the only rule with this literal in the head). However, since is also in the answer set, this rule has a false body so can never be in . Consequently can never be in since the only rule with this literal in the head is of the form (1) and this body can never be true, which leads to a contradiction.
Secondly assume that and are both in an answer set of . Again step 1 implies that . Because of the rules (2) will contain the rule . An analogous reasoning as above implies that since is in the answer set , can never be in . 
For every man , in every answer set of exactly one of the following conditions is satisfied:

there exists a woman such that ,

,
and similarly for every woman .
Suppose is an arbitrary answer set of and is an arbitrary man. We already know from step 2 that a man cannot be paired to a woman while being single, so both possibilities are disjoint. So suppose there is no woman such that is in . will contain the rule (2). Because of our assumptions and the definition of the reduct, this rule will be reduced to , and so will be in . 

For an arbitrary answer set of the previous steps imply that produces a set of marriages without blocking individuals. Weak stability also demands the absence of blocking pairs. Suppose by contradiction that there is a blocking pair , implying that there exist and such that and while and . The rules of the form (1), the only ones with the literals and in the head, imply that literals and should be in . But since and because of the form of the rules (4) there are fewer conditions to be fulfilled for to be in than for to be in . So should be in as well. A similar reasoning implies that should be in . But now the rules of the form (1) imply that should be in , contradicting step 2 since and are already in .
∎
Proposition 2
Let be an instance of the SMP with unacceptability and ties, and let be the corresponding ASP program. If , , is a weakly stable matching for then has the following answer set :
Proof.
Suppose we have a stable set of marriages , implying that every is an acceptable partner of and the other way around. The rules of the form (1) do not alter when forming the reduct, but the other rules do as those contain nafliterals. Notice first that the stability of implies that there cannot be an unmarried couple , with a man and a woman, such that is in and is in . By definition of this would mean that they both strictly prefer each other to their current partner in . This means they would form a blocking pair, but since was stable, that is impossible. So the rules of the form (1) will be applied exactly for married couples , since by definition of and are both in under these conditions. For other cases the rule will also be fulfilled since the body will be false. This reasoning implies that the unique minimal model of the reduct w.r.t. should indeed contain for every married couple in . Since is a stable set of marriages, every person is either married or single. If a man is single, there will be no other literal of the form in , so rule (2) will reduce to a fact , which is obviously fulfilled by . Similarly if a woman is single. Any other rule of the form (2) or (3) is deleted because or is not single in that case and thus there is some literal of the form for some woman and some literal of the form for some man in , falsifying the body of the rules. If is single, then is in and this is the only literal of the form in , so the rules of the form (4) will all be reduced to facts. The rule heads of these facts should be in the minimal model of the reduct and are indeed in since the women for which is in are exactly those who are strictly preferred to staying single. The rules of the form (4) for women in will all be deleted in this case, because is in . If man is married to a certain woman in the stable set then the rules of the form (4) will reduce to facts of the form for every woman who is strictly preferred to and will be deleted for every other woman appearing in the head, because those rules will contain in the body. Again contains these facts by definition, as the minimal model of the reduct should. We can use an analogous reasoning for the women. So the presence of the literals of the form , and in is required in the unique minimal model of the reduct w.r.t. . We have proved that every literal in should be the minimal model of the reduct and that every rule of the reduct is fulfilled by , implying that is an answer set of . ∎
In [SMPMan02] it is shown that the decision problem ‘is the pair stable?’ for a given SMP instance with unacceptablity and ties is an NPcomplete problem, even in the absence of unacceptability. A pair is stable if there exists a stable set that contains . It is straightforward to see that we can reformulate this decision problem as ‘does there exist an answer set of the induced normal ASP program which contains the literal ?’ (i.e. brave reasoning), which is known to be an NPcomplete problem [ASPBa03]. So our model forms a suitable framework for these kind of decision problems concerning the SMP.
4 Selecting Preferred Stable Sets
4.1 Notions of Optimality of Stable Sets
When several stable matchings can be found for an instance of the SMP, some may be more interesting than others. The stable set found by the GS algorithm is Moptimal [SMPRoth90], i.e. every man likes this set at least as well as any other stable set. Exchanging the roles of men and women in the GS algorithm yields a Woptimal stable set [SMPGale62], optimal from the point of view of the women.
While some applications may require us to favour either the men or the women, in others it makes more sense to treat both parties equally. To formalize some commonly considered notions of fairness and optimality w.r.t. the SMP, we define the cost of a stable set to an individual , where if has been matched with his or her preferred partner. More precisely, for a man, we define