• No results found

Developing a Python Package for Reasoning with NNF Sentences

N/A
N/A
Protected

Academic year: 2021

Share "Developing a Python Package for Reasoning with NNF Sentences"

Copied!
31
0
0

Bezig met laden.... (Bekijk nu de volledige tekst)

Hele tekst

(1)

Bachelor Informatica

Developing a Python Package

for Reasoning with NNF

Sentences

Jan Verbeek

June 17, 2019

Inf

orma

tica

Universiteit

v

an

Ams

terd

am

(2)
(3)

Abstract

The Negation Normal Form (NNF) is a limited form for logical sentences. It has several useful subsets (such as DNNF), queries (such as satisfiability), and transformations (such as conditioning). It supports a general technique, algebraic model counting.

python-nnf is a package for handling NNF sentences in the Python programming lan-guage. This paper describes its design considerations, its capabilities and limitations, and an example application in the field of computational social choice.

AMC reduction is a technique for removing parts of a NNF sentence, based on algebraic model counting.

(4)
(5)

Contents

1 Introduction 7

2 Theoretical background 9

2.1 Negation Normal Form . . . 9

2.1.1 Queries and transformations . . . 9

2.1.2 Properties and subsets . . . 9

2.2 Algebraic Model Counting . . . 10

2.3 DIMACS formats . . . 10 2.4 Python . . . 11 3 My work 13 3.1 Features . . . 13 3.2 Design . . . 13 3.2.1 Sentence representation . . . 13 3.2.2 Immutability . . . 15 3.2.3 Deduplication . . . 15

3.2.4 true and false nodes . . . 16

3.2.5 Simplification . . . 16 3.2.6 Algorithm choice . . . 17 3.3 Implementation . . . 18 3.3.1 Object structure . . . 18 3.3.2 Memoization . . . 18 3.4 Testing . . . 18

3.5 Shortcomings and future work . . . 19

3.5.1 Taking advantage of sentence properties . . . 19

3.5.2 More compact serialization . . . 19

3.6 Theoretical work . . . 19

3.6.1 AMC reduction . . . 19

4 Experiments 21 4.1 Applications . . . 21

4.1.1 Computational social choice . . . 21

4.2 Benchmarks . . . 24

4.2.1 Decomposability . . . 24

4.2.2 Smoothness transformation . . . 25

4.2.3 Decomposable model enumeration . . . 26

4.2.4 Deterministic model enumeration . . . 27

(6)
(7)

CHAPTER 1

Introduction

The Negation Normal Form (NNF) is a limited but powerful form for expressing logical sentences in mathematics.

It consists of variables, conjunctions (and operations), disjunctions (or operations), and negation of variables. Depending on whether the variables in the sentence are true or false, the sentence is either true or false.

An example of a NNF sentence is given in Figure 1.1. The sentence means “Either A is true and B is true, or A is false and B is false.”

The sentence contains two variables: A and B. By assigning truth values to both of those variables, the sentence becomes true or false.

and nodes (labeled ∧) are true if all of their children are true. or nodes (labeled ∨) are true if at least one of their children is true.

If A and B are both false, then the nodes labeled “A” and “B” are false, while the nodes labeled “¬A” and “¬B” are true. That makes the left “∧” node false and the right “∧” node true, which in turn makes the “∨” node true. That means the sentence is true.

But if A is true and B is false, then only the nodes labeled “A” and “¬B” are true. Both the “∧” nodes are false, because not all of their children are true, so the “∨” node is false as well. That means the sentence is false.

The sentence is only true if A and B are both true, or if they are both false. We call those sets of values the models of the sentence. In general, such sets of values are called terms.

First, this paper will look at operations that can be performed on NNF sentences, properties sentences can have, and a technique called algebraic model counting. It will then describe a software implementation in the form of a Python library, python-nnf, including design consid-erations and technical challenges. A technique based on algebraic model counting called AMC reduction will be introduced and defined. The utility of the library and the new technique will be demonstrated by applying them to NNF sentences in the field of computational social choice.

(8)

∧ ∧

A B ¬A ¬B

(9)

CHAPTER 2

Theoretical background

2.1

Negation Normal Form

The NNF language can be formally defined as follows (Darwiche and Marquis [3]).

Definition 2.1.1 (NNF). Let PS be a denumerable set of propositional variables. A sentence in NNFPS is a rooted, directed acyclic graph (DAG) where each leaf node is labeled with true,

false, X or ¬X, X ∈ PS ; and each internal node is labeled with ∧ or ∨ and can have arbitrarily many children.

This describes the form of sentences, but not how they can be used.

2.1.1

Queries and transformations

There are many useful operations on NNF sentences. They can be divided into transformations, which output new NNF sentences, and queries, which output something else, sometimes simply “yes” or “no.”

One of the most basic queries is to ask whether a model satisfies a sentence. That is, whether when a value is assigned to all the variables in a sentence according to some model, the sentence is true or false. The introduction gave examples of this query.

Another important query is model enumeration. Given a sentence, what are all the models that satisfy the sentence? While it is easy to determine whether a specific model satisfies a sentence, finding all the models that satisfy a sentence can be hard.

A more limited variant is model counting, which outputs the number of models that satisfy the sentence, without saying anything about what those models are.

A satisfiability (also called consistency) check tells you whether a model that satisfies the sentence exists at all. A sentence without models is called unsatisfiable (inconsistent), and can never be true.

Conditioning is a transformation that fills in values for the variables according to a term. Not all variables need to be filled in. Nodes for variables that occur in the term are replaced by either true or false depending on their value in the term.

2.1.2

Properties and subsets

NNF sentences can have certain properties that make it possible to perform queries and trans-formations more efficiently. Subsets of NNF can be defined based on those properties.

One important property is decomposability. A NNF sentence is decomposable if for each ∧ node, its children do not share variables. That is, if one of the children of a ∧ node mentions a variable X, none of the other children of that ∧ node mention X. The subset of NNF with only decomposable sentences is called DNNF. An example of an efficient operation on DNNF sentences is given in 3.2.5.

(10)

Such restrictions come at the cost of succinctness. There are NNF sentences for which all the equivalent DNNF sentences are larger.

Another subset of NNF is called CNF. A CNF sentence is a conjunction of disjunctions of variables. That is, it’s a ∧ node with clauses as children, where each clause is a ∨ node with variables as children. This subset is somewhat hard to work with. For example, it’s not possible to check satisfiability in polynomial time, although it is for DNNF.

It can be useful to transform sentences into equivalent sentences that belong to a differ-ent subset, to make further operations more efficidiffer-ent to perform. This is known as knowledge compilation.

Several compilers exist that perform knowledge compilation, such as DSHARP, which trans-forms CNF sentences into DNNF sentences (Muise et al. [10]).

2.2

Algebraic Model Counting

Algebraic model counting is a technique that couples the structure of NNF sentences to commutative semirings to perform computations, introduced by Kimmig, Van den Broeck, and De Raedt [8]. They define a commutative semiring as:

Definition 2.2.1 ((Commutative) Semiring). A semiring is a structure (A, ⊕, ⊗, e⊕, e⊗), where addition ⊕ and multiplication ⊗ are associative binary operations over the set A, ⊕ is commu-tative, ⊗ distributes over ⊕, e⊕∈ A is the neutral element of ⊕, e⊗ ∈ A that of ⊗, and for all

a ∈ A, e⊕⊗ a = a ⊗ e⊕= e. In a commutative semiring, ⊗ is commutative as well.

An example of a commutative semiring is (N, +, ·, 0, 1), addition and multiplication over the natural numbers.

Algebraic model counting uses these parameters, as well as a labeling function, to calculate a value for a sentence. It assigns the value e⊕ to false, and e⊗ to true. A labeling function assigns values to nodes with (negated) variables. Values are calculated for ∨ nodes by applying the ⊕ operator to the values of their children, and for ∧ nodes by applying the ⊗ operator.

The function that executes this algorithm is referred to as Eval.

Depending on the semiring and the labeling function chosen, algebraic model counting can perform many different computations, including model counting, satisfiability checking, and sen-sitivity analysis. It is named “algebraic model counting” not because it’s limited to model counting, but because it’s a generalization of weighted model counting.

Algebraic model counting cannot be performed with all semirings and labeling functions on all NNF sentences. Depending on the properties of the semiring, there are constraints on the properties of the sentences. The more specific the properties of the semiring, the more general the sentences that are supported can be.

An example is that sentences have to be decomposable unless the ⊗ operator is idempotent (a ⊗ a = a for all a) and consistency-preserving with the labeling function (with the labeling function α, α(v) ⊗ α(¬v) = e⊕for all variables v). A more complete overview of these properties and their respective subsets of NNF is given by Kimmig, Van den Broeck, and De Raedt [8].

2.3

DIMACS formats

The DIMACS CNF and SAT formats are two formats for serializing CNF and NNF sentences, originally created for the DIMACS Challenge ([2]).

Both formats only support numbered variables. The number of variables is declared upfront in a header. In the CNF format, the number of clauses is declared as well.

(11)

The CNF format represents clauses as series of variables and negated variables, separated by zeroes. A file in the CNF format may look like this:

c Example CNF format file c

p cnf 4 3 1 3 -4 0 4 0 2 -3

In the SAT format, disjunctions (∨ nodes) wrap their children between +() parentheses, while conjunctions (∧ nodes) use *() parentheses. The value false is represented by a disjunction without children, and true by a conjunction without children. A file in the SAT format may look like this:

c Sample SAT format c

p sat 4 (*(+(1 3 -4)

+(4) +(2 3)))

This format is vulnerable to blowup in case of nodes with multiple parents (see 3.2.3). This makes it a very inefficient way to represent certain sentences.

2.4

Python

Python is a popular programming language. It is commonly used in many areas, including scientific computing, and has many packages to implement a lot of different functionality.

Python uses dynamic typing, meaning that variables can hold any type of value. It also uses duck typing, meaning that objects are used based on the operations they support, rather than on the specific type they have.

Types in Python may be mutable or immutable. Objects with immutable types cannot have their values modified in-place. At most, it may be possible to create a new, separate instance with another value. An advantage of immutable objects is that they can be hashable, which allows them to be stored in data structures that use hashing.

Hashing makes it possible to quickly find objects with the same value, without comparing to each candidate object.

Hashing is used by dicts (dictionaries), containers that map keys to values. Keys are hashed to quickly find the value that belongs to a key. It is also used by sets, unordered containers that hold at most one of each value. Contents are hashed to quickly determine whether a set contains a value.

Some of Python’s basic container types have both a mutable and an immutable variant. lists (ordered containers) are mutable, but have an immutable variant called tuple. sets themselves have an immutable variant called frozenset.

The immutability of frozensets allows them to be stored in sets and frozensets themselves. However, they can be burdensome to work with in some cases, because no more values can be added after creation.

(12)
(13)

CHAPTER 3

My work

python-nnf ([11]) is a Python package for working with NNF sentences. It allows creating objects representing NNF sentences and performing certain operations on them.

3.1

Features

A non-comprehensive list of features follows, to outline the scope of the package. • Creating sentences

• Simplifying sentences (see 3.2.5)

• Querying whether a model satisfies a sentence

• Querying properties like smoothness and decomposability • Serializing to and from DIMACS formats (see 2.3)

• Representing sentences in the DOT graph visualization language • Algebraic model counting

• AMC reduction (see 3.6.1)

3.2

Design

The representation of sentences is intended to be simple and easy to work with, while not exactly corresponding to the formal definition of NNF.

3.2.1

Sentence representation

Each node in a sentence is represented by an object, and there is no dedicated object type for entire sentences. A sentence object is simply its root node.

Another approach could have been to encapsulate the entire sentence into a single object that contains a description of the graph. An example of a library that encapsulates an entire graph into a single object is networkx, a Python library for general graph processing (Hagberg, Schult, and Swart [6]).

The node-based approach was chosen over the entire-graph approach for several reasons. • The meaning of a node depends on its children, to the point where two nodes with the

same type and the same children are equivalent, and could be replaced by a single node. The nodes on the second row in Figure 3.1 have the same label and the same children, and as such there is no benefit to treating them as separate nodes. By making such nodes

(14)

A

B

Figure 3.1: A sentence with redundant nodes.

objects that compare equal, this equivalence is reflected on a deep level by the library. In fact, representing the sentence in Figure 3.1 in python-nnf as drawn is impossible. Thanks to the use of frozensets, the sentence is automatically understood as one without the redundancy (see 3.2.2).

• It’s possible to incorporate a sentence into a larger sentence, or study a fragment of a sentence on its own, without creating an entire new sentence object with just the node and its children. This is desirable for many operations and recursive algorithms which can be applied just as well to an entire sentence or its root node as to other nodes in the sentence. There are also drawbacks to the approach.

• Because there is no explicit list of the variables a sentence applies to, operations that depend on that list need to infer it from the variables that actually appear in the sentence. It is in principle possible for operations that preserve the meaning of a sentence to remove some variables from it, if their value never affects the truth value of the root node. This would then change the output of model enumeration, which does not include the removed variable in its models.

This distinction is not consistently made in the literature, however. Kimmig, Van den Broeck, and De Raedt [8] define smoothness based on mentioned children, even though that property is later applied as a criterion for the applicability of a method for model counting. This suggests that leaving variables not mentioned in the sentence out of models is considered acceptable.

• An existing graph library like networkx could have been used for the representation, giving access to the features of that library and allowing immediate interoperability with existing

(15)

∧ ∧

A B ¬A

Figure 3.2: The “B” node in this sentence has multiple parents.

3.2.2

Immutability

The node objects are immutable. They cannot be altered after creation, and they can be hashed so they can be contained in set types and used as keys in dicts.

This has several important advantages:

• Nodes can store their children in a frozenset because they are hashable. This container is unordered and doesn’t store its elements more than once, which is a good match for NNF semantics. There is no order to a node’s children, and a node can’t have the same child more than once. Comparing node types and frozensets is therefore enough to determine whether two nodes are equal.

• If a node is used in multiple sentences, it’s impossible for an operation on one sentence to affect the other sentence. If nodes were mutable then changing a node would change the node in both sentences, which would make in-place operations dangerous. Immutability gives a strong guarantee that sentences aren’t affected by changes elsewhere.

• If two nodes are equal then their objects are perfectly interchangeable. It is possible for two node objects to have the same value. If they were mutable, a change to one object would not affect the other. That means that replacing a node object by another object in the same value would lead to different behavior in the case of changes, making it unsafe. Because of immutability, objects can be replaced, which can be used to significantly reduce memory use without side effects (see 3.2.3).

• Nodes can be used as keys in a cache because they are hashable. This makes it easier to use memoization, speeding up operations on certain sentences considerably (see 3.3.2).

3.2.3

Deduplication

If a single node has multiple parents then it may be represented by multiple object instances. Consider the “B” node in the sentence in Figure 3.2. Both ∧ nodes contain a reference to a node representing the variable “B”, but they may either contain a reference to the same object, or to different objects with equal values.

If they reference different objects with equal values then both of those objects take up memory. The sentence takes less memory if both ∧ nodes are constructed with a reference to the same object.

Sentences with many nodes that have multiple parents may have severely inflated memory use if this occurs, even exponentially so. Consider the sentences described by:

(16)

Ci= Ci−1∧ Ci−2

with C1 = A and C2 = B. If each Ci uses entirely separately constructed instances of

Ci−1 and Ci−2, then it contains two entirely separate instances of Ci−2: one of them referenced

directly, and one of them contained in Ci−1. That means it uses at least twice as much memory

as Ci−2, giving it memory use that’s exponential with regards to i while linear memory use is

possible by reusing instances.

A more realistic example of sentences vulnerable to this issue is given in 4.1.1.

The problem is avoidable by taking care to avoid creating nodes multiple times. It’s non-obvious enough to warn about in the documentation, though, and python-nnf contains tools for addressing it.

Sentence objects have an object count method, that returns the number of distinct ob-jects in the sentence, giving a simple approximation of the memory use. They also have a deduplicate method, that returns a copy of the sentence without redundant object instances. The deduplicate method is usually not a good way to solve the problem, because it can only be used once the (inefficient) original sentence object is already constructed, but in combination with object count it is an easy way to compare the actual memory use to the optimal memory use.

3.2.4

true and false nodes

A ∧ node without children is always true, because it can’t ever have a child that’s false. Fur-thermore, a ∨ node without children is always false, because it can’t ever have a child that’s true.

In python-nnf, true nodes are represented by And objects without children, and false nodes are represented by Or objects without children.

Early in the development there was a dedicated node type for Booleans, but this became a problem when implementing the DIMACS formats. In the DIMACS SAT format, true is represented by *(), the same representation used for ∧ nodes without children, and false is represented by +(), a ∨ node without children. Parsing this is ambiguous when these things are distinct.

Removing the Boolean type reduced complexity at no real loss to expressiveness.

3.2.5

Simplification

python-nnf offers a simplify method for conservatively removing nodes from a sentence without changing its meaning. It returns a copy of the sentence with the following changes applied recursively to all nodes:

• ∧ nodes with false as one of their children are replaced by false themselves, because they can never be true.

• ∨ nodes with true as one of their children are replaced by true, because they are always true.

• true nodes are removed as children from ∧ nodes, because they don’t make a difference. • false nodes are removed as children from ∨ nodes for the same reason.

• ∧ and ∨ nodes with only one child are replaced by that child.

• ∧ nodes with ∧ nodes as children are merged with those children, by replacing those nodes by those nodes’ children, as are ∨ nodes with ∨ nodes as children.

(17)

Theorem 3.2.1 (unsatisfiable DNNF sentences are simplified to false). If the simplify oper-ation is applied to a decomposable NNF sentence, the result is a false node iff the sentence is unsatisfiable.

Proof.

• false is the only unsatisfiable leaf node, as true, X and ¬X are trivially satisfiable. • A ∨ node is unsatisfiable iff all of its children are unsatisfiable.

• A ∧ node is unsatisfiable iff no single model exists that can satisfy all its children at the same time.

In decomposable sentences, the children of a ∧ node do not share variables, meaning that the models of its children also do not share variables. As such, if all of its children have models (are satisfiable), those models can be combined into a single model that satisfies all children.

That means that a decomposable ∧ node is unsatisfiable iff any of its children are unsatis-fiable.

Therefore,

• simplify turns unsatisfiable leaf nodes into false (by not changing them).

• If all of a ∨ node’s children are unsatisfiable, simplify turns them into false and removes them, turning the ∨ node into false.

• If any of a ∧ node’s children are unsatisfiable, simplify turns them into false, and then turns the ∧ node into false.

As this applies not just to full sentences but also to nodes within sentences, simplify can remove parts of sentences that are no longer meaningful, thereby making sentences easier to work with after transformations.

Certain pathological sentences could be made more complex by the merging of nodes, if a node with many children is merged with many parents. This situation wasn’t encountered during development, but it could be useful to offer a version of simplify without that transformation.

3.2.6

Algorithm choice

Some operations have efficient algorithms that only work on sentences with certain properties, but no similarly efficient algorithms that work on all sentences. For such operations it’s beneficial to pick an algorithm depending on the properties of the sentence.

This is not always best done by checking the properties of the sentence, because some proper-ties are themselves inefficient to verify. An example is determinism, the property that all children of ∨ nodes contradict each other.

python-nnf implements multiple algorithms for several operations, most notably model enu-meration. Depending on whether a sentence is decomposable and whether it is deterministic, one of four algorithms is used, two of which are slight variations of each other.

Keyword arguments can be passed to the method to indicate which properties the sentence has. If no keyword argument is passed for decomposability, decomposability is queried, be-cause that can be done efficiently. If determinism isn’t indicated, the sentence is treated as not deterministic, because determinism is hard to decide.

A performance analysis of the algorithms used for model enumeration is given in 4.2.3 and 4.2.4.

(18)

3.3

Implementation

3.3.1

Object structure

python-nnf implements three types to represent nodes: Var, And and Or. All are immutable. Var objects have a name attribute, identifying the variable they represent, and a true at-tribute, which is set to False if the node is a negated variable and to True otherwise. name can be any hashable type.

And and Or nodes only have one attribute each, children. children is a frozenset con-taining other nodes.

For convenience, a childless And node called true and a childless Or node called false are defined on the module level. Those instances aren’t special in any other way.

And and Or nodes can be constructed from iterable objects. They can also be made by using the & (ampersand) and | (pipe) Python operators on nodes, respectively. The ~ (tilde) operator can be used to negate variables.

All of these are ways to create the sentence “A ∧ ¬B”: And({Var(’A’), Var(’B’, False)})

And({Var(’A’), ~Var(’B’)}) Var(’A’) & ~Var(’B’)

Because And and Or can take any iterable object, generator expressions can be used. The sentence “V9

n=0n” can be created like so:

And(Var(n) for n in range(10))

3.3.2

Memoization

A naive approach to many algorithms would use simple recursive evaluation: each internal node performs some computation for each of its children. If nodes have multiple parents, this may lead to them being evaluated many times (see 3.2.3). To avoid unnecessary computation python-nnf uses memoization, temporarily caching the return values of evaluations.

This cache is discarded after the full computation is complete, to avoid unreasonable memory use.

3.4

Testing

Many of the automated tests of python-nnf are carried out using the hypothesis testing library (MacIver [9]).

hypothesis is a library for property-based testing. A typical test written for hypothesis defines some check that should hold for all data of a certain kind, and requests data from hypothesis of that kind. hypothesis then runs the check with many different examples of fitting data, including edge cases that manually designed test cases may not find.

hypothesis comes with many predefined data providers that can be composed, to specify such things as sets of integers, or lists of tuples of strings. It also supports defining entirely new providers with new data types. The tests in python-nnf use custom providers that generate arbitrary NNF sentences, or NNF sentences with specific properties such as decomposability.

This is an example of a test using such a data provider: @given(NNF())

def test_idempotent_simplification(sentence: nnf.NNF): sentence = sentence.simplify()

(19)

• It does not change the models of the sentence.

• It removes all Booleans from the sentence, other than the root node. • It leaves no And or Or nodes that could be merged.

• It turns unsatisfiable DNNF sentences into false.

Other tests are carried out on manually created examples, and some use sentences from the SATLIB benchmark problems (Hoos and St¨utzle [7]).

3.5

Shortcomings and future work

3.5.1

Taking advantage of sentence properties

The current system for deciding which algorithm to use based on sentence properties is compli-cated and cumbersome. The sentence properties need to be manually indicompli-cated for each method call when they aren’t queried automatically, and even when they are queried automatically, manually indicating them lowers overhead.

A way to mark sentences persistently as having properties could alleviate this, but isn’t easily supported by the current architecture. There are a few possible ways to implement it:

• Add attributes to node objects for remembering properties. This would increase the mem-ory use of each node and make node objects with the same values less interchangeable. • Keep a global registry of sentence properties. This could add significant memory overhead

and interfere with garbage collection.

• Encapsulate sentences in a new kind of object that does remember properties. This would be similar to the entire-graph approach discussed in 3.2.1.

3.5.2

More compact serialization

The DIMACS SAT format is vulnerable to blowup if nodes have multiple parents, which makes it unsuitable for representing certain sentences that could be represented compactly by a different format. If no such format already exists, creating one for use just by python-nnf without interoperability could be useful.

3.6

Theoretical work

3.6.1

AMC reduction

Algebraic model counting can be used as a general basis for certain transformations as well as queries. If the ⊕ operator always returns one of its operands, then children of ∨ nodes can be removed without changing the value of the sentence. This is mentioned by De Haan [5], but not elaborated. It can be defined:

Definition 3.6.1 (AMC reduction). An AMC reduction on a semiring removes children with all values but one from disjunctions, in such a way that the result of a regular AMC Eval operation on the complete sentence or other remaining nodes is not affected.

Intuitively, such an operator corresponds to functions like max, which picks the highest ele-ment from a set, according to some total order.

Definition 3.6.2 (max operator). A max operator returns its highest operand, according to some fixed total order.

This definition also covers the operators min (with a reversed total order) and ∨ (with the total order ⊥ < >).

(20)

Theorem 3.6.1 (max operators are required for AMC reduction). All ⊕ operators that support AMC reduction are max operators.

Proof. ⊕ with a single operand returns that same operand. Therefore, for Eval to still return the same value for a disjunction when all children but one have been removed, that value must also belong to the remaining child. Therefore ⊕ always returns one of its operands.

Define a ≤ b to mean that a ⊕ b = b (and therefore b ⊕ a = b, through the commutative property of ⊕).

• If a ≤ b and b ≤ a, then a⊕b = b and b⊕a = a, so a = b. Therefore, ⊕ has antisymmetry. • If a ≤ b and b ≤ c, then a ⊕ b = b and b ⊕ c = c, so (a ⊕ b) ⊕ c = c. It follows from the commutative and associative properties that (a ⊕ c) ⊕ b = c. ⊕ returns one of its operands, so either a ⊕ c = c, or b = c, in which case a ≤ b also means that a ⊕ c = c. Therefore, a ≤ c and ⊕ has transitivity.

• Because ⊕ returns one of its operands, either a ⊕ b = a (so b ≤ a) or a ⊕ b = b (so a ≤ b). Therefore, ⊕ has connexity.

≤ satisfies all the requirements for a total order, so ⊕ is a max operator.

The result of AMC reduction is a sentence where the remaining models are the ones with the highest available value.

AMC reduction is implemented as a submodule in python-nnf, together with ordinary alge-braic model counting.

(21)

CHAPTER 4

Experiments

4.1

Applications

4.1.1

Computational social choice

It is possible to create a DNNF sentence representing the set of linear orders over a set of candidates (De Haan [4]). This provides a good case study for python-nnf, as the sentences allow interesting queries and transformations and are suitable for algebraic model counting. They also contain many nodes with multiple parents, which offers opportunities for deduplication.

A linear order is a ranking of all candidates by preference. In the sentences considered here, it’s expressed by giving each pair of candidates a variable. If candidate “Alice” is preferred over candidate “Bob”, then the variable “(Alice > Bob)” is true. If “Bob” is preferred over “Alice”, then “(Alice > Bob)” is false. This assumes a complete ranking, where candidates can’t be equally preferred.

The sentences are satisfied when the variables express a consistent order. Variables are inconsistent when there’s a circular order, for example when A is preferred over B, B over C, and C over A.

An example sentence for three candidates is shown in Figure 4.1.

The sizes of these sentences grow exponentially with the number of candidates. Conditioning

Conditioning followed by simplification restricts the possibilities into a new appropriately man-ageable sentence. Figure 4.2 shows the result of conditioning the previously used sentence on

∨ ∧ ∧ ∧ (Alice > Bob) (Alice > Eve) ∨ ¬(Alice > Bob) ∨ (Bob > Eve) ∨

¬(Bob > Eve) ¬(Alice > Eve)

(22)

(Bob > Eve)

¬(Alice > Bob) (Alice > Bob)

¬(Bob > Eve)

Figure 4.2: Figure 4.1 after conditioning on (Alice > Eve) and simplifying the result.

2 3 4 5 6 7 8 9 Number of candidates 101 102 103 104 105 106 Object count Best case Worst case

Figure 4.3: Object count in sentences depending on the number of candidates. Note the loga-rithmic scale.

(Alice > Eve) being true, a preference of Alice over Eve. Arrangements of other variables that would produce a circular preference are no longer possible.

Deduplication

Because many nodes have multiple parents the effect of deduplication is dramatic. This is displayed in Figure 4.3, which shows two possible object counts for sentences for each number of candidates. The “Best case” object count uses full deduplication, where no node within a sentence is represented by multiple objects. The “Worst case” object count represents every node as many times as possible. The difference quickly grows to several orders of magnitude. AMC reduction

(23)

rankings that place A higher than B receive 30 points from that pair, and rankings that place B higher than A receive 20 points. This happens with all pairs (Zwicker [12]).

This rule can be executed using the max-plus semiring (Akian, Bapat, and Gaubert [1]): (R ∪ {−∞}, max, +, −∞, 0). The ⊕ operator is max, while the ⊗ operator is +. This means that for ∨ nodes, the child with the highest value is chosen, while for ∧ nodes, the values of the children are added together.

A labeling function can assign scores to leaf nodes based on how many people chose each pairwise ranking.

AMC reduction with that semiring and that labeling function leaves a sentence with only those models that received the maximum score. Those models necessarily each express a consistent ranking, and those rankings give the results of the Kemeny-Young method.

The Slater rule is similar to the Kemeny-Young method. Instead of assigning scores to pairs based on the number of people who prefer the pairwise ranking, it assigns a score of 1 to pairwise rankings that a majority agrees with, and a score of 0 to those that don’t. The same semiring is used, and it is otherwise like the Kemeny-Young method.

Both rules are implemented in example code included with python-nnf. lin-top

As a variant, De Haan [4] also defines a function lin-top for sentences with a narrower selection of candidates. In this model, each vote ranks only k candidates, where k may be less than the total number of candidates.

A variable (A > B) is true if A is included in the selection of k candidates, and B is not also included and preferred over A. Each pair has two variables, so in this example, there is also a variable (B > A). If neither candidate is included in the selection of k candidates then both variables are false.

(24)

10

2

10

3

10

4

Sentence size

10

1

10

0

10

1

10

2

Time (ms)

Decomposability query performance

Fit (k = 0.9674)

Fit (k = 1.168)

Fit (k = 1.209)

uf20

uf50

uf75

CNF

Figure 4.4: Performance of decomposability queries on sentences in d-DNNF and CNF with varying numbers of variables.

4.2

Benchmarks

Benchmarks were performed on Uniform Random-3-SAT SATLIB benchmark problems, compiled by the DSHARP compiler into d-DNNF sentences (deterministic, decomposable). The uf20-91, uf50-218 and uf75-325 sets were used, which have 20, 50 and 75 variables each, respectively.

The compiled sentences have a variety of sizes, which affect performance. The effect of the number of variables on performance was studied by comparing the different sets of problems.

Results are displayed in log-log plots relating the time per operation to the size of the sentence (the number of edges in the sentence). Linear regression is used to fit a line to the points, of which the slope (k) can be used to estimate the complexity of the operation. A straight line on a log-log plot corresponds to a monomial of the form y = axk, which means k = 1 indicates a

linear relationship, k = 2 a quadratic relationship, and so on.

4.2.1

Decomposability

In decomposable sentences, children of ∧ nodes don’t share variables. This query runs through all nodes of sentences which have the property to verify that they all comply. It may take considerably less time to decide that a sentence isn’t decomposable, because a single counterex-ample is enough. The query was tested against the aforementioned d-DNNF sentences as well as against the CNF sentences from which they were compiled, which are typically easy to prove non-decomposable.

Figure 4.4 shows the results. The CNF sentences are clustered into three size groups corre-sponding to the three groups of d-DNNF sentences. They are all fast to prove non-decomposable,

(25)

10

2

10

3

10

4

Sentence size

10

1

10

0

10

1

10

2

Time (ms)

Smoothness transformation performance

Fit (k = 1.886)

Fit (k = 1.752)

Fit (k = 1.675)

uf20

uf50

uf75

Figure 4.5: Performance of smoothness transformation on d-DNNF sentences with varying num-bers of variables.

4.2.2

Smoothness transformation

In smooth sentences, the children of each ∨ node all mention the same variables. Smoothness is a requirement for some algebraic model counting techniques. Sentences can be made smooth in polynomial time by adding X ∨ ¬X nodes as conjunctions with children for all the variables they miss.

Figure 4.5 shows the results. The sentences in sets with more variables tend to have bet-ter performance. This could be caused by particular properties of the sentences generated by DSHARP, or it could be a more general effect. It’s possible that sentences that spread out a smaller number of variables over a sentence of the same size are further from being smooth.

The smallest sentences in each set have even better performance than their size would suggest relative to the others. This might be because they are already smooth, or very close to being smooth.

(26)

10

2

Sentence size

10

0

10

1

10

2

10

3

Time (ms)

Decomposable model counting performance

Fit (k = 1.916)

Fit (k = 2.19)

Fit (k = 2.923)

uf20

uf50

uf75

Figure 4.6: Performance of the model enumeration algorithm for decomposable sentences on d-DNNF sentences with varying numbers of variables.

4.2.3

Decomposable model enumeration

The model enumeration algorithm for decomposable sentences exploits efficient satisfiability checks and conditioning to build a tree of partial models that are consistent with the sentence (Darwiche and Marquis [3], Lemma A.3). The running time of the algorithm depends on the size (for the satisfiability checks), the number of variables (for the height of the tree) and the number of models (for the width of the tree).

Because this algorithm is quite expensive, only the smallest 75 sentences in the uf50 and smallest 50 in the uf75 problem sets were used for benchmarking this algorithm. This means the smallest sentences in those sets disproportionally influence the linear regression compared to the other sentences, making it less informative.

The results are in Figure 4.6. The smallest sentences within each set have low running times, most likely because they have fewer models. Without the bias from excluding the largest sentences in the uf50 and uf75 sets, all sets would likely have very similar values of k, albeit at different heights because of their different numbers of variables.

(27)

10

2

10

3

10

4

Sentence size

10

1

10

0

10

1

10

2

10

3

Time (ms)

Deterministic model counting performance

Fit (k = 0.9925)

Fit (k = 1.559)

Fit (k = 1.931)

uf20

uf50

uf75

Figure 4.7: Performance of the model enumeration algorithm for deterministic decomposable sentences on d-DNNF sentences with varying numbers of variables.

4.2.4

Deterministic model enumeration

Model enumeration of deterministic sentences is done bottom-up. ∧ nodes combine the models of their children as a Cartesian product, excluding combinations that are incompatible. ∨ nodes produce all the models of their children, which are guaranteed to contradict each other. Models of the root node that don’t have all variables are expanded into multiple full models.

The running time of this algorithm depends on the size of the sentence and the number of models, but also on the number of variables, which determines the size of the models.

Figure 4.7 displays the results. Within each set, there is a neat relation between the size and the running time. Sets with more variables have a steeper slope, however. The set with 20 variables has a linear relationship (k ≈ 1), while the set with 75 variables has a quadratic relationship (k ≈ 2).

In the measured sentences the algorithm for deterministic sentences is much faster than the algorithm for decomposable sentences.

(28)
(29)

CHAPTER 5

Conclusions

python-nnf provides a useful basis for working with NNF sentences in the Python programming language. It represents sentences as simple immutable objects without distinguishing between sentences and individual nodes. It’s interoperable with the established DIMACS format for serializing sentences, which helps integrating it with existing software.

A recurring problem when implementing NNF sentences in software is nodes with multiple parents. Many naive approaches to representing or processing NNF treat it as a tree, to be traversed recursively, but NNF sentences are rooted directed acyclic graphs, in which nodes may share children. Naive recursive approaches may store or process the same node many times. This affects the DIMACS SAT format, and can affect the use of python-nnf unless care is taken to prevent it.

AMC reduction is a technique based on algebraic model counting that removes parts of a sentence, leaving only those models with a maximum value. It can be used to implement known voting rules in a novel way, suggesting it could have broader applications.

Some of python-nnf’s operations have multiple implementations for sentences with different properties. Determining whether sentences have those properties is not entirely automatic and not very user-friendly, which could be improved.

An alternative to the DIMACS SAT format that does not suffer from the aforementioned problem with nodes that have multiple parents would be desirable.

(30)
(31)

Bibliography

[1] Marianne Akian, Ravindra Bapat, and St´ephane Gaubert. “Max-plus algebra”. In: (2006). url: http://www.cmap.polytechnique.fr/~gaubert/PAPERS/hla-preprint.pdf. [2] DIMACS Challenge. “Satisfiability: Suggested Format”. In: DIMACS Challenge. DIMACS

(1993). url: https://www.cs.ubc.ca/~hoos/SATLIB/Benchmarks/SAT/satformat.ps. [3] Adnan Darwiche and Pierre Marquis. “A Knowledge Compilation Map”. In: CoRR abs/1106.1819

(2011). arXiv: 1106.1819. url: http://arxiv.org/abs/1106.1819.

[4] Ronald de Haan. “Expressing Linear Orders Requires Exponential-Size DNNFs”. In: CoRR abs/1807.06397 (2018). arXiv: 1807.06397. url: http://arxiv.org/abs/1807.06397. [5] Ronald de Haan. “Hunting for Tractable Languages for Judgment Aggregation”. In:

Pro-ceedings of the 16th International Conference on the Principles of Knowledge Representa-tion and Reasoning (KR 2018). AAAI Press, 2018, pp. 194–203. url: https://arxiv. org/abs/1808.03043.

[6] Aric A. Hagberg, Daniel A. Schult, and Pieter J. Swart. “Exploring network structure, dynamics, and function using NetworkX”. In: Proceedings of the 7th Python in Science Conference (SciPy2008). Pasadena, CA USA, Aug. 2008, pp. 11–15.

[7] Holger H Hoos and Thomas St¨utzle. “SATLIB: An online resource for research on SAT”. In: Sat 2000 (2000), pp. 283–292. url: https://www.cs.ubc.ca/~hoos/Publ/sat2000-satlib.pdf.

[8] Angelika Kimmig, Guy Van den Broeck, and Luc De Raedt. “Algebraic Model Counting”. In: CoRR abs/1211.4475 (2012). arXiv: 1211.4475. url: http://arxiv.org/abs/1211. 4475.

[9] David R. MacIver. Hypothesis 4.17. https://github.com/HypothesisWorks/hypothesis. 2018.

[10] Christian Muise et al. “DSHARP: Fast d-DNNF Compilation with sharpSAT”. In: Cana-dian Conference on Artificial Intelligence. 2012. url: http://www.haz.ca/papers/wara-muise-10.pdf.

[11] Jan Verbeek. python-nnf. GitHub repository. 2019. url: https://github.com/blyxxyz/ python-nnf.

[12] William S. Zwicker. “Introduction to the Theory of Voting”. In: Handbook of Computational Social Choice. Ed. by Felix Brandt et al. Cambridge University Press, Cambridge, 2016.

Referenties

GERELATEERDE DOCUMENTEN

Door het verdwijnen van het bivariate effect van fiscale decentralisatie op politiek vertrouwen ontstaat er zodoende mogelijk een mediërend verband, waarbij een hogere

Robot goes (which shows us that audiences' engagement with critique is distracted from the critical potential of Mr. Robot by narrative excess).. Furthermore, when

There was a main effect found for situation type and score on the GAPS test, which means that children with a higher proficiency level, allowed less non-culmination.. The GAPS was

Therefore, it is argued that the management control mechanism, identified as RPE, affects the relationships between decentralization of decision rights and

2 This platform allows for the systematic assessment of pediatric CLp scal- ing methods by comparing scaled CLp values to “true” pe- diatric CLp values obtained with PBPK-

The focus of this research will be on Dutch entrepreneurial ICT firms residing in the Netherlands that have received venture capital financing from at least one foreign

Kennisverspreiding door lezingen en discussie-bijeenkomsten Naast telers worden stakeholders zoals toeleveranciers en overheden betrokken bij ontwikkeling en implementatie van

(h) Lars werd door Harry bedreigd, maar Mieke redde hem toch*. (a) Samuel trapte Justin, en Marja