From f02501ff0ab96980da41ef0b4309d5414b722b17 Mon Sep 17 00:00:00 2001 From: akushman Date: Sat, 3 Aug 2019 11:32:23 +0300 Subject: [PATCH] #4. Change Base struct fields to unexported --- hungarian.go | 132 +++++++++++++++++++++++----------------------- hungarian_test.go | 2 +- 2 files changed, 67 insertions(+), 67 deletions(-) diff --git a/hungarian.go b/hungarian.go index e87af16..ae96c91 100644 --- a/hungarian.go +++ b/hungarian.go @@ -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 @@ -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 } } } @@ -29,56 +29,56 @@ 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] } } } @@ -86,55 +86,55 @@ func (b *Base) reduceByMinMore() { } 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) @@ -142,9 +142,9 @@ func (b *Base) setValues() { // 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) } } } @@ -156,12 +156,12 @@ 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) } } } @@ -169,21 +169,21 @@ func (b *Base) removeExtra() { // 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 @@ -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 @@ -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 @@ -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 } } @@ -266,5 +266,5 @@ func SolveMin(matrix [][]float64) map[int]map[int]float64 { b.checkAndReplace() - return b.ReducedExtremums + return b.reducedExtremums } diff --git a/hungarian_test.go b/hungarian_test.go index 4cc8b27..4a48d32 100644 --- a/hungarian_test.go +++ b/hungarian_test.go @@ -1,7 +1,7 @@ package hungarian_test import ( - "go-hungarian" + "hungarian" "testing" )