Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complexity and csp definitions (and theorems... soon) #69

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions UniversalAlgebra/Complexity.lagda
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
layout: default
title : Complexity module (The Agda Universal Algebra Library)
date : 2021-07-13
author: [agda-algebras development team][]
---

## Complexity Theory

\begin{code}

{-# OPTIONS --without-K --exact-split --safe #-}

module Complexity where

open import Complexity.Basic
open import Complexity.CSP
open import Complexity.FiniteCSP

\end{code}


--------------------------------

[agda-algebras development team]: https://github.com/ualib/agda-algebras#the-agda-algebras-development-team
90 changes: 53 additions & 37 deletions UniversalAlgebra/Complexity/CSP.lagda
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
layout: default
title : Complexity.CSP module (The Agda Universal Algebra Library)
date : 2021-07-13
date : 2021-07-14
author: [agda-algebras development team][]
---

Expand All @@ -15,67 +15,83 @@ module Complexity.CSP where

open import Agda.Primitive using ( _⊔_ ; lsuc ; Level)
renaming ( Set to Type )
open import Data.Nat.Base using ( ℕ )
open import Data.Fin.Base using ( Fin )
open import Function.Base using ( _∘_ )
open import Relation.Unary using ( _∈_; Pred )

open import Relations.Continuous using ( Rel )

\end{code}

#### Finite CSP
#### Constraints

An instance of a (finite) constraint satisfaction problem is a triple 𝑃 = (𝑉, 𝐷, 𝐶) where
A constraint c consists of

* 𝑉 is a finite set of variables,
* 𝐷 is a finite domain,
* 𝐶 is a finite list of constraints, where
each constraint is a pair 𝐶 = (𝑥, 𝑅) in which
* 𝑥 is a tuple of variables of length 𝑛, called the scope of 𝐶, and
* 𝑅 is an 𝑛-ary relation on 𝐷, called the constraint relation of 𝐶.
1. a scope function, s : I → var, and

\begin{code}
2. a constraint relation, i.e., a predicate over the function type I → D

I ···> var
. .
. .
⌟ ⌞
D

private variable χ ρ : Level

record Constraint (∣V∣ ∣D∣ : ℕ) : Type (lsuc (χ ⊔ ρ)) where
field
scope : Fin ∣V∣ → Type χ -- a subset of Fin ∣V∣
The *scope* of a constraint is an indexed subset of the set of variable symbols.
We could define a type for this, e.g.,

relation : ((v : Fin ∣V∣ ) → v ∈ scope → Fin ∣D∣) → Type ρ
-- a subset of "tuples" (i.e., functions 𝑓 : scope → Fin ∣D∣)
```
Scope : Type ν → Type ι → _
Scope V I = I → V
```

-- An assignment 𝑓 : 𝑉 → 𝐷 of values in the domain to variables in 𝑉
-- *satisfies* the constraint 𝐶 = (𝑥, 𝑅) if 𝑓 ↾ 𝑥 ∈ 𝑅.
satisfies : (Fin ∣V∣ → Fin ∣D∣) → Type ρ
satisfies f = (λ v _ → f v) ∈ relation
but we omit this definition because it's so simple; to reiterate,
a scope of "arity" I on "variables" V is simply a map from I to V,
where,

* I denotes the "number" of variables involved in the scope
* V denotes a collection (type) of "variable symbols"

\begin{code}

open Constraint
module _ -- levels for...
{ι : Level} -- ...arity (or argument index) types
{ν : Level} -- ...variable symbol types
{δ : Level} -- ... domain types
-- {ρ : Level} -- ... relation types
where

record CSP : Type (lsuc (χ ⊔ ρ)) where
field
∣V∣ -- # of variables,
∣D∣ -- # of elements in the domain,
∣C∣ -- # of constraints (resp.)
: ℕ
𝐶 : Fin ∣C∣ → Constraint{χ} ∣V∣ ∣D∣
record Constraint (var : Type ν)(dom : Type δ){ρ : Level} : Type (lsuc (ι ⊔ ν ⊔ δ ⊔ ρ)) where
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't the level that you want (lsuc ι) ⊔ ν ⊔ δ ⊔ (lsuc ρ) ? This is lower (in general). The nice thing about parameters is that they don't cause level creep.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct. How could you possibly have noticed that?!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Experience?

field
arity : Type ι -- The "number" of variables involved in the constraint.
scope : arity → var -- Which variables are involved in the constraint.
rel : Rel dom arity {ρ} -- The constraint relation.

-- An assignment 𝑓 *solves* the instance 𝑃 if it satisfies all the constraints.
isSolution : (Fin ∣V∣ → Fin ∣D∣) → Type ρ
isSolution f = ∀ i → (satisfies (𝐶 i)) f
satisfies : (var → dom) → Type ρ -- An assignment 𝑓 : var → dom of values to variables
satisfies f = f ∘ scope ∈ rel -- *satisfies* the constraint 𝐶 = (σ , 𝑅) provided
-- 𝑓 ∘ σ ∈ 𝑅, where σ is the scope of the constraint.

\end{code}

#### CSP Instances

An instance of a constraint satisfaction problem is a triple 𝑃 = (𝑉, 𝐷, 𝐶) where

#### References
* 𝑉 denotes a set of "variables"
* 𝐷 denotes a "domain",
* 𝐶 denotes an indexed collection of constraints.

Some of the informal (i.e., non-agda) material in this module is similar to that presented in
\begin{code}

record CSPInstance (var : Type ν)(dom : Type δ){ρ : Level} : Type (lsuc (ι ⊔ ν ⊔ δ ⊔ ρ)) where
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems oddly formed to me - shouldn't the Constraint be a parameter?

Copy link
Member Author

@williamdemeo williamdemeo Jul 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, there's not a single constraint (though you could view it as such). Rather, there is a tuple of contraints, so I use arity to represent the "number" of constraints, and then the constraints are modeled as a function from arity to the Constraint type. Does it still seem odd to you? Perhaps it would have been easier to comprehend if I had called the arity field ∣constraints∣ or something similar? (and then rename the cs field constraints...?)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I was not precise. Isn't a CSPInstance supposed to be an instance of a CSP? In other words, it should be parametrized by a single "problem". The definition used here only have a very diffuse 'problem' that it depends on, quite indirectly.

field
arity : Type ι -- The "number" of constraints of the instance.
cs : arity → Constraint var dom {ρ} -- The constraints of the instance.

* Ross Willard's slides: [Constraint Satisfaction Problems: a Survey](http://www.math.uwaterloo.ca/~rdwillar/documents/Slides/willard-boulder2016-rev2.pdf)
isSolution : (var → dom) → Type (ι ⊔ ρ) -- An assignment *solves* the instance
isSolution f = ∀ i → (Constraint.satisfies (cs i)) f -- if it satisfies all the constraints.

* [Polymorphisms, and How to Use Them](https://drops.dagstuhl.de/opus/volltexte/2017/6959/pdf/DFU-Vol7-15301-1.pdf
\end{code}



Expand Down
90 changes: 90 additions & 0 deletions UniversalAlgebra/Complexity/FiniteCSP.lagda
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
---
layout: default
title : Complexity.FiniteCSP module (The Agda Universal Algebra Library)
date : 2021-07-14
author: [agda-algebras development team][]
---

### Constraint Satisfaction Problems

\begin{code}

{-# OPTIONS --without-K --exact-split --safe #-}

module Complexity.FiniteCSP where

open import Agda.Primitive using ( _⊔_ ; lsuc ; Level)
renaming ( Set to Type )
open import Data.Product using ( _,_ ; Σ ; Σ-syntax )
open import Data.Nat.Base using ( ℕ )
open import Data.Fin.Base using ( Fin )
open import Function.Base using ( _∘_ )
open import Relation.Unary using ( _∈_; Pred )

open import Relations.Continuous using ( Rel )
open import Algebras.Basic using ( Signature )

\end{code}


#### Finite CSP

An instance of a (finite) constraint satisfaction problem is a triple 𝑃 = (𝑉, 𝐷, 𝐶) where

* 𝑉 is a finite set of variables,
* 𝐷 is a finite domain,
* 𝐶 is a finite list of constraints, where
each constraint is a pair 𝐶 = (𝑥, 𝑅) in which
* 𝑥 is a tuple of variables of length 𝑛, called the scope of 𝐶, and
* 𝑅 is an 𝑛-ary relation on 𝐷, called the constraint relation of 𝐶.

\begin{code}

private variable χ ρ : Level

record Constraint (∣V∣ ∣D∣ : ℕ) : Type (lsuc (χ ⊔ ρ)) where
field
scope : Fin ∣V∣ → Type χ -- a subset of Fin ∣V∣
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems overly general. There is little point in having witnesses to whether something is in scope. You might as well use Bool instead. And then Vec (Fin |V|) Bool might be easier?

Copy link
Member Author

@williamdemeo williamdemeo Jul 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good point. I want to make the FiniteCSP module very special and easy to use, so I'll try the Vec approach you suggest. (That's actually what I started with, but thought it was maybe too special... but probably you're right.)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because Fin |V| has decidable equality (and is finite!), you gain very little from having scope be so very general. You may as well have other things also have decidable equality.


relation : ((v : Fin ∣V∣ ) → v ∈ scope → Fin ∣D∣) → Type ρ
-- a subset of "tuples" (i.e., functions 𝑓 : scope → Fin ∣D∣)

-- An assignment 𝑓 : 𝑉 → 𝐷 of values in the domain to variables in 𝑉
-- *satisfies* the constraint 𝐶 = (𝑥, 𝑅) if 𝑓 ↾ 𝑥 ∈ 𝑅.
satisfies : (Fin ∣V∣ → Fin ∣D∣) → Type ρ
satisfies f = (λ v _ → f v) ∈ relation


open Constraint

record CSPInstance : Type (lsuc (χ ⊔ ρ)) where
field
∣V∣ -- # of variables,
∣D∣ -- # of elements in the domain,
∣C∣ -- # of constraints (resp.)
: ℕ
𝐶 : Fin ∣C∣ → Constraint{χ} ∣V∣ ∣D∣

-- An assignment 𝑓 *solves* the instance 𝑃 if it satisfies all the constraints.
isSolution : (Fin ∣V∣ → Fin ∣D∣) → Type ρ
isSolution f = ∀ i → (satisfies (𝐶 i)) f

\end{code}



#### References

Some of the informal (i.e., non-agda) material in this module is similar to that presented in

* Ross Willard's slides: [Constraint Satisfaction Problems: a Survey](http://www.math.uwaterloo.ca/~rdwillar/documents/Slides/willard-boulder2016-rev2.pdf)

* [Polymorphisms, and How to Use Them](https://drops.dagstuhl.de/opus/volltexte/2017/6959/pdf/DFU-Vol7-15301-1.pdf



--------------------------------------

[agda-algebras development team]: https://github.com/ualib/agda-algebras#the-agda-algebras-development-team


6 changes: 6 additions & 0 deletions UniversalAlgebra/agda-algebras-everything.lagda
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ open import ClosureSystems.Basic using ( ∅ ; 𝒞𝓁 ; ClOp )
open import ClosureSystems.Properties using ( clop→law⇒ ; clop→law⇐ ; clop←law )


-- Complexity ----------------------------------------------------------------------------------------

open import Complexity.CSP using ( Constraint ; CSPInstance )

open import Complexity.FiniteCSP using ( Constraint ; CSPInstance )

-- ALGEBRAS ------------------------------------------------------------------------------------------

open import Algebras.Basic using ( Signature ; signature ; monoid-op ; monoid-sig
Expand Down