diff --git a/bench/cec2020/inputData.go b/bench/cec2020/inputData.go index 82708d5..88ba390 100644 --- a/bench/cec2020/inputData.go +++ b/bench/cec2020/inputData.go @@ -18,8 +18,9 @@ var inputData embed.FS // LoadInputData loads matrices and shift/rotation/shuffle input data and // returns them as maps. -func LoadInputData(nx int) map[int][][]float64 { +func LoadInputData(nx int) (map[int][][]float64, map[int][][]float64) { matrices := make(map[int][][]float64) + shiftData := make(map[int][][]float64) prefix := "input_data" @@ -28,6 +29,7 @@ func LoadInputData(nx int) map[int][][]float64 { for _, v := range funcNums { fnameMatrix := fmt.Sprintf("%s/M_%d_D%d.txt", prefix, v, nx) + fnameShift := fmt.Sprintf("%s/shift_data_%d.txt", prefix, v) { matrix, err := inputData.Open(fnameMatrix) @@ -42,9 +44,23 @@ func LoadInputData(nx int) map[int][][]float64 { matrices[v] = m } + + { + shift, err := inputData.Open(fnameShift) + if err != nil { + log.Printf("shift: could not read %s, err: %q\n", fnameShift, err) + continue + } + + defer shift.Close() + + s := parseShiftData(shift, nx) + + shiftData[v] = s + } } - return matrices + return matrices, shiftData } func parseMatrix(f fs.File, dim int) [][]float64 { @@ -79,3 +95,35 @@ func parseMatrix(f fs.File, dim int) [][]float64 { return fMatrix } + +func parseShiftData(f fs.File, dim int) [][]float64 { + if f == nil { + log.Fatalln("shift file is nil, exiting...") + } + + shrows := make([]string, 0, dim) + fSh := make([][]float64, 0, dim) + scanner := bufio.NewScanner(f) + + for scanner.Scan() { + shrows = append(shrows, scanner.Text()) + } + + for _, r := range shrows { + row := strings.Fields(r) + flrow := make([]float64, 0, len(row)) + + for _, col := range row { + fl, err := strconv.ParseFloat(col, 64) + if err != nil { + log.Fatalf("parseShiftData: could not parse value to float64, col: %s, error: %q, bailing...", col, err) + } + + flrow = append(flrow, fl) + } + + fSh = append(fSh, flrow) + } + + return fSh +} diff --git a/bench/cec2020/inputData_test.go b/bench/cec2020/inputData_test.go index ed5f929..70a984e 100644 --- a/bench/cec2020/inputData_test.go +++ b/bench/cec2020/inputData_test.go @@ -10,18 +10,34 @@ import ( func TestLoadInputData(t *testing.T) { dim := 10 fnum := 1 - m := LoadInputData(dim) - want := -0.6013070130189602 - got := m[fnum][0][0] + m, s := LoadInputData(dim) + wantM := -0.6013070130189602 + gotM := m[fnum][0][0] - if want != got { - t.Errorf("unexpected result, want: %v, got: %v", want, got) + // test matrix data. + if wantM != gotM { + t.Errorf("unexpected result, want: %v, got: %v", wantM, gotM) } - want = 9.2845625214477390e-01 - got = m[fnum][1][1] + wantM = 9.2845625214477390e-01 + gotM = m[fnum][1][1] - if want != got { - t.Errorf("unexpected result, want: %v, got: %v", want, got) + if wantM != gotM { + t.Errorf("unexpected result, want: %v, got: %v", wantM, gotM) + } + + // test shiftData. + wantS := -70.42955971808618 + gotS := s[fnum][0][1] + + if wantS != gotS { + t.Errorf("unexpected result, want: %v, got: %v", wantS, gotS) + } + + wantS = 36.645777950364064 + gotS = s[fnum][0][10] + + if wantS != gotS { + t.Errorf("unexpected result, want: %v, got: %v", wantS, gotS) } }