math-optim/bench/cec2020/inputData.go
leo 80f061db9a
All checks were successful
continuous-integration/drone/push Build is passing
cec2020: add inputData{,_test}.go + input_data/
2023-02-14 21:11:11 +01:00

82 lines
1.5 KiB
Go

// Copyright 2023 wanderer <a_mirre at utb dot cz>
// SPDX-License-Identifier: GPL-3.0-or-later
package cec2020
import (
"bufio"
"embed"
"fmt"
"io/fs"
"log"
"strconv"
"strings"
)
//go:embed input_data/*
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 {
matrices := make(map[int][][]float64)
prefix := "input_data"
// filenames indices.
funcNums := []int{1, 2, 3, 7, 4, 16, 6, 22, 24, 25}
for _, v := range funcNums {
fnameMatrix := fmt.Sprintf("%s/M_%d_D%d.txt", prefix, v, nx)
{
matrix, err := inputData.Open(fnameMatrix)
if err != nil {
log.Printf("matrix: could not read %s, err: %q\n", fnameMatrix, err)
continue
}
defer matrix.Close()
m := parseMatrix(matrix, nx)
matrices[v] = m
}
}
return matrices
}
func parseMatrix(f fs.File, dim int) [][]float64 {
if f == nil {
log.Fatalln("matrix file is nil, exiting...")
}
var mrows []string
fMatrix := make([][]float64, 0, dim)
scanner := bufio.NewScanner(f)
for scanner.Scan() {
mrows = append(mrows, scanner.Text())
}
for _, r := range mrows {
row := strings.Fields(r)
flrow := make([]float64, 0, len(row))
for _, val := range row {
fl, err := strconv.ParseFloat(val, 64)
if err != nil {
log.Fatalf("parseMatrix: could not parse value to float64, val: %s, error: %q, bailing...", val, err)
}
flrow = append(flrow, fl)
}
fMatrix = append(fMatrix, flrow)
}
return fMatrix
}