Documentation
¶
Overview ¶
Package ap defines interfaces and data structures common to formulating and solving assignment problems. More details about these can be found in:
Rainer Burkard, Mauro Dell'Amico, and Silvano Martello. "Assignment Problems - Revised Reprint." Society for Industrial and Applied Mathematics (2012).
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Assigner ¶
type Assigner interface {
Assign() Permutation
}
An Assigner creates a mapping between two sets U = V = {0, ..., n-1}.
type Coster ¶ added in v0.3.0
type Coster[T Integer] interface { Cost() T }
A Coster provides an Integer cost value. This is usually the objective of value of a particular assignment.
type Cycle ¶ added in v0.3.0
type Cycle []int
A Cycle is an individual cycle in an assignment, such as [1 2 4]. The cycle is assumed to contain an arc from the final element to the initial element, so this cycle has the arcs (1 2), (2 4) and (4 1). A cycle can contain a a single element with an arc to itself, such as [3].
type Cycles ¶ added in v0.3.0
type Cycles []Cycle
Cycles represents multiple individual cycle structures corresponding to an assignment. For example, the permutation [1 0 2 6 5 3 4] is equivalent to the set of cycles [[0 1] [2] [3 6 4 5]]. If a permutation corresponds to a single cycle, then that cycle is a Hamiltonian cycle.
func (Cycles) Inverse ¶ added in v0.3.0
Inverse changes the direction of a set of cycles representing an assignment.
Example ¶
package main
import (
"fmt"
"github.com/ryanjoneil/ap"
)
func main() {
c := ap.Cycles{{0, 1}, {2}, {3, 6, 4, 5}}
fmt.Println(c.Inverse())
}
Output: [[0 1] [2] [3 5 4 6]]
func (Cycles) Matrix ¶ added in v0.3.0
Matrix converts a cyclic representation of an assignment into a permutation matrix.
Example ¶
package main
import (
"fmt"
"github.com/ryanjoneil/ap"
)
func main() {
c := ap.Cycles{{0, 1}, {2}, {3, 6, 4, 5}}
for _, row := range c.Matrix() {
fmt.Println(row)
}
}
Output: [false true false false false false false] [true false false false false false false] [false false true false false false false] [false false false false false false true] [false false false false false true false] [false false false true false false false] [false false false false true false false]
func (Cycles) Permutation ¶ added in v0.3.0
func (c Cycles) Permutation() Permutation
Permutation converts a cyclic representation of an assignment into a permutation representation.
Example ¶
package main
import (
"fmt"
"github.com/ryanjoneil/ap"
)
func main() {
c := ap.Cycles{{0, 1}, {2}, {3, 6, 4, 5}}
fmt.Println(c.Permutation())
}
Output: [1 0 2 6 5 3 4]
type DualPricer ¶ added in v0.3.0
type DualPricer[T Integer] interface { DualPrices() DualPrices[T] }
A DualPricer provides dual prices on the assignment constraints associated with sets U and V. A dual price is the value of a unit of slack on a binding constraint.
type DualPrices ¶ added in v0.3.0
type DualPrices[T Integer] struct { U []T `json:"u"` V []T `json:"v"` }
DualPrices are dual prices for the assignment constraints corresponding to the U and V sets, respectively.
type Matrix ¶
type Matrix [][]bool
Matrix representation of an assignment. If u is assigned to v, then M[u][v] is true. Each row and each column has exactly one true element.
func (Matrix) Cycles ¶ added in v0.3.0
Cycles converts a permutation matrix to a cyclic representation.
Example ¶
package main
import (
"fmt"
"github.com/ryanjoneil/ap"
)
func main() {
m := ap.Matrix{
[]bool{true, false, false, false, false},
[]bool{false, false, false, false, true},
[]bool{false, false, false, true, false},
[]bool{false, true, false, false, false},
[]bool{false, false, true, false, false},
}
fmt.Println(m.Cycles())
}
Output: [[0] [1 4 2 3]]
func (Matrix) Inverse ¶ added in v0.3.0
Inverse inverts a permutation matrix, changing its direction.
Example ¶
package main
import (
"fmt"
"github.com/ryanjoneil/ap"
)
func main() {
m := ap.Matrix{
[]bool{true, false, false, false, false},
[]bool{false, false, false, false, true},
[]bool{false, false, false, true, false},
[]bool{false, true, false, false, false},
[]bool{false, false, true, false, false},
}
for _, row := range m.Inverse() {
fmt.Println(row)
}
}
Output: [true false false false false] [false false false true false] [false false false false true] [false false true false false] [false true false false false]
func (Matrix) Permutation ¶ added in v0.3.0
func (m Matrix) Permutation() Permutation
Permutation converts a matrix assignment representation into a permutation.
Example ¶
package main
import (
"fmt"
"github.com/ryanjoneil/ap"
)
func main() {
m := ap.Matrix{
[]bool{true, false, false, false, false},
[]bool{false, false, false, false, true},
[]bool{false, false, false, true, false},
[]bool{false, true, false, false, false},
[]bool{false, false, true, false, false},
}
fmt.Println(m.Permutation())
}
Output: [0 4 3 1 2]
type Permutation ¶ added in v0.3.0
type Permutation []int
A Permutation P maps elements from one set U = {0,...,n-1} to another set V = {0,...,n-1}, where P[u] = v means that u ∈ U is assigned to v ∈ V.
p := ap.Permutation{1, 0, 2} // Assign 0 to 1, 1 to 0, and 2 to itself.
func (Permutation) Cycles ¶ added in v0.3.0
func (p Permutation) Cycles() Cycles
Cycles convert a permutation representation of an assignment to a cyclical representation of that assignment. If the result contains a single cycle, then that cycle is a Hamiltonian cycle.
p := ap.Permutation{1, 0, 2, 6, 5, 3, 4}
p.Cycles() // {{0, 1}, {2}, {3, 6, 4, 5}}
Example ¶
package main
import (
"fmt"
"github.com/ryanjoneil/ap"
)
func main() {
p := ap.Permutation{1, 0, 2, 6, 5, 3, 4}
fmt.Println(p.Cycles())
}
Output: [[0 1] [2] [3 6 4 5]]
func (Permutation) Inverse ¶ added in v0.3.0
func (p Permutation) Inverse() Permutation
Inverse converts an assignment from a set U to a set V to an assignment from V to U. If, for example, U is the left hand side of a bipartite matching and V is the right hand side, this function essentially swaps their sides.
Example ¶
package main
import (
"fmt"
"github.com/ryanjoneil/ap"
)
func main() {
p := ap.Permutation{1, 0, 2, 6, 5, 3, 4}
fmt.Println(p.Inverse())
}
Output: [1 0 2 5 6 4 3]
func (Permutation) Matrix ¶ added in v0.3.0
func (p Permutation) Matrix() Matrix
Matrix converts a permutation into a square matrix.
Example ¶
package main
import (
"fmt"
"github.com/ryanjoneil/ap"
)
func main() {
p := ap.Permutation{1, 0, 2, 6, 5, 3, 4}
for _, row := range p.Matrix() {
fmt.Println(row)
}
}
Output: [false true false false false false false] [true false false false false false false] [false false true false false false false] [false false false false false false true] [false false false false false true false] [false false false true false false false] [false false false false true false false]
type ReducedCoster ¶ added in v0.3.0
A ReducedCoster provides a method for computing the reduced cost of assigning u ∈ U to v ∈ V, where u and v are both integers from 0 to n-1. The reduced cost of a basic edge (already part of an assignment) is zero, since it does not change the solution. Introducing a nonbasic edge (not in the assignment) may change the resulting assignment's overall cost.