Skip to content

Commit

Permalink
#4. Change Base struct fields to unexported
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurkushman committed Aug 3, 2019
1 parent eda6544 commit f02501f
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 67 deletions.
132 changes: 66 additions & 66 deletions hungarian.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
)

type Base struct {
Matrix [][]float64
Reduced [][]float64
Extremums map[int]float64
ReducedExtremums map[int]map[int]float64
matrix [][]float64
reduced [][]float64
extremums map[int]float64
reducedExtremums map[int]map[int]float64
}

const ReduceDivisor = 5
Expand All @@ -17,10 +17,10 @@ func (b *Base) reduceByMax() {
// collect extremums
b.findMaxExtremums()

for k, row := range b.Matrix {
for k, row := range b.matrix {
for key, el := range row {
if (el - b.Extremums[k]) < 0 {
b.Reduced[k][key] = (el - b.Extremums[k]) * -1
if (el - b.extremums[k]) < 0 {
b.reduced[k][key] = (el - b.extremums[k]) * -1
}
}
}
Expand All @@ -29,122 +29,122 @@ func (b *Base) reduceByMax() {
// reduces previously reduced matrix with min (to find maximums)
// and simple reducer for minimums
func (b *Base) reduceByMin() {
for i := 0; i < int(math.Round(float64(len(b.Matrix))/ReduceDivisor)); i++ {
for i := 0; i < len(b.Matrix); i++ {
b.Extremums[i] = math.MaxInt64
for i := 0; i < int(math.Round(float64(len(b.matrix))/ReduceDivisor)); i++ {
for i := 0; i < len(b.matrix); i++ {
b.extremums[i] = math.MaxInt64
}

// rows reduction
b.findMinRowExtremums()

for k, row := range b.Reduced {
for k, row := range b.reduced {
for key, el := range row {
b.Reduced[k][key] = el - b.Extremums[k]
b.reduced[k][key] = el - b.extremums[k]
}
}

// re-init
for i := 0; i < len(b.Matrix); i++ {
b.Extremums[i] = math.MaxInt64
for i := 0; i < len(b.matrix); i++ {
b.extremums[i] = math.MaxInt64
}

// cols reduction
b.findMinColExtremums()
for k, row := range b.Reduced {
for k, row := range b.reduced {
for key, el := range row {
b.Reduced[k][key] = el - b.Extremums[key]
b.reduced[k][key] = el - b.extremums[key]
}
}
}
}

func (b *Base) reduceByMinMore() {
for i := 0; i < int(math.Round(float64(len(b.Matrix))/ReduceDivisor)); i++ {
for i := 0; i < len(b.Matrix); i++ {
b.Extremums[i] = math.MaxInt64
for i := 0; i < int(math.Round(float64(len(b.matrix))/ReduceDivisor)); i++ {
for i := 0; i < len(b.matrix); i++ {
b.extremums[i] = math.MaxInt64
}

// rows reduction
for k, row := range b.Reduced {
for k, row := range b.reduced {
for _, el := range row {

// trying to find more min values > 0
if el < b.Extremums[k] && el > 0 {
b.Extremums[k] = el
if el < b.extremums[k] && el > 0 {
b.extremums[k] = el
}
}
}

for k, row := range b.Reduced {
for k, row := range b.reduced {
for key, el := range row {
if el > 0 {
b.Reduced[k][key] = el - b.Extremums[k]
b.reduced[k][key] = el - b.extremums[k]
}
}
}
}
}

func (b *Base) findMaxExtremums() {
for k, row := range b.Matrix {
for k, row := range b.matrix {
for _, el := range row {
if el > b.Extremums[k] {
b.Extremums[k] = el
if el > b.extremums[k] {
b.extremums[k] = el
}
}
}
}

func (b *Base) findMinRowExtremums() {
for k, row := range b.Reduced {
for k, row := range b.reduced {
for _, el := range row {
if el < b.Extremums[k] {
b.Extremums[k] = el
if el < b.extremums[k] {
b.extremums[k] = el
}
}
}
}

func (b *Base) findMinColExtremums() {
for _, row := range b.Reduced {
for _, row := range b.reduced {
for k, el := range row {
if el < b.Extremums[k] {
b.Extremums[k] = el
if el < b.extremums[k] {
b.extremums[k] = el
}
}
}
}

func (b *Base) setValues() {
for k, row := range b.Reduced {
for k, row := range b.reduced {
for key, el := range row {

// if max/min el then check crossing and choose those that not
if el == 0 {
if b.ReducedExtremums[k] == nil {
b.ReducedExtremums[k] = make(map[int]float64, len(b.Matrix))
if b.reducedExtremums[k] == nil {
b.reducedExtremums[k] = make(map[int]float64, len(b.matrix))
}
b.ReducedExtremums[k][key] = b.Matrix[k][key]
b.reducedExtremums[k][key] = b.matrix[k][key]
}
}
}

for k, row := range b.ReducedExtremums {
for k, row := range b.reducedExtremums {
for key := range row {

// don`t touch single elements
if len(row) > 1 {
for rk, rrow := range b.ReducedExtremums {
for rk, rrow := range b.reducedExtremums {
for rkey := range rrow {

// check if position is free (the same col and another row)
if k != rk && key == rkey {

// del extremum in row where more elms
if len(rrow) > len(row) {
delete(b.ReducedExtremums[rk], rkey)
delete(b.reducedExtremums[rk], rkey)
} else {
delete(b.ReducedExtremums[k], key)
delete(b.reducedExtremums[k], key)
}
}
}
Expand All @@ -156,34 +156,34 @@ func (b *Base) setValues() {
}

func (b *Base) removeExtra() {
for k, row := range b.ReducedExtremums {
for k, row := range b.reducedExtremums {
for key := range row {

// if there are still > 1 - tear down
if len(row) > 1 {
delete(b.ReducedExtremums[k], key)
delete(b.reducedExtremums[k], key)
}
}
}
}

// checks if there are still elements that crossing and replaces them with those that not
func (b *Base) checkAndReplace() {
for k, v := range b.ReducedExtremums {
for k, v := range b.reducedExtremums {
for i := range v {

// check keys
for rk, rv := range b.ReducedExtremums {
for rk, rv := range b.reducedExtremums {
for j := range rv {

// index is not the same but keys are
if k != rk && i == j {
for mik, miv := range b.Matrix[rk] {
for mik, miv := range b.matrix[rk] {
thereIs := false

// check if there is no such el at all
// or check all values against this row in matrix
for _, rrv := range b.ReducedExtremums {
for _, rrv := range b.reducedExtremums {
for jj := range rrv {
if jj == mik {
thereIs = true
Expand All @@ -193,8 +193,8 @@ func (b *Base) checkAndReplace() {

// replace to inexistent element
if thereIs == false {
delete(b.ReducedExtremums[rk], j)
b.ReducedExtremums[rk][mik] = miv
delete(b.reducedExtremums[rk], j)
b.reducedExtremums[rk][mik] = miv

// here is a recursive call only if we've got similar element and replace em
// to check whether there are others, otherwise we don't need an extra checks
Expand All @@ -210,16 +210,16 @@ func (b *Base) checkAndReplace() {

func SolveMax(matrix [][]float64) map[int]map[int]float64 {
var b = Base{
Matrix: matrix,
Reduced: [][]float64{},
Extremums: map[int]float64{},
ReducedExtremums: map[int]map[int]float64{},
matrix: matrix,
reduced: [][]float64{},
extremums: map[int]float64{},
reducedExtremums: map[int]map[int]float64{},
}

// inti reduced matrix with zeroes
b.Reduced = make([][]float64, len(matrix))
for i := range b.Reduced {
b.Reduced[i] = make([]float64, len(matrix))
b.reduced = make([][]float64, len(matrix))
for i := range b.reduced {
b.reduced[i] = make([]float64, len(matrix))
}

// reduce matrix by max
Expand All @@ -233,26 +233,26 @@ func SolveMax(matrix [][]float64) map[int]map[int]float64 {

b.checkAndReplace()

return b.ReducedExtremums
return b.reducedExtremums
}

func SolveMin(matrix [][]float64) map[int]map[int]float64 {
var b = Base{
Matrix: matrix,
Reduced: [][]float64{},
Extremums: map[int]float64{},
ReducedExtremums: map[int]map[int]float64{},
matrix: matrix,
reduced: [][]float64{},
extremums: map[int]float64{},
reducedExtremums: map[int]map[int]float64{},
}

// inti reduced matrix with zeroes
b.Reduced = make([][]float64, len(matrix))
for i := range b.Reduced {
b.Reduced[i] = make([]float64, len(matrix))
b.reduced = make([][]float64, len(matrix))
for i := range b.reduced {
b.reduced[i] = make([]float64, len(matrix))
}

for i, row := range matrix {
for j, v := range row {
b.Reduced[i][j] = v
b.reduced[i][j] = v
}
}

Expand All @@ -266,5 +266,5 @@ func SolveMin(matrix [][]float64) map[int]map[int]float64 {

b.checkAndReplace()

return b.ReducedExtremums
return b.reducedExtremums
}
2 changes: 1 addition & 1 deletion hungarian_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package hungarian_test

import (
"go-hungarian"
"hungarian"
"testing"
)

Expand Down

0 comments on commit f02501f

Please sign in to comment.