Commit de1b05f0 authored by CedricPump's avatar CedricPump
Browse files

- init

- added tspreader
parent 5d8bc1b2
.idea/
data/
# PVS_TSP
# Programmierung verteilter Systeme - Traveling Salesman Problem
Semester: SS2021
Author: Cedric Pump
Mat.Nr.: 229810
## Aufgabe
package main
import (
"fmt"
"log"
"pvs_tsp/tspreader"
)
func main() {
spec, e := tspreader.Read("./data/swiss42.tsp")
if e != nil {
log.Fatal(e.Error())
}
fmt.Println(spec)
}
\ No newline at end of file
package tspreader
import (
"bufio"
"errors"
"fmt"
"log"
"os"
"strconv"
"strings"
)
type TspSpec struct {
name string
problemType string
comment string
dimension int
edgeWeightType string
edgeWeightFormat string
data *TspData
}
type TspData struct {
datatype string
values*[][] float64
}
func NewTspData() *TspSpec {
return &TspSpec{name: "", problemType: "TSP", comment: "", dimension: 0, edgeWeightType: "", data: NewTspDataArray()}
}
func NewTspDataArray() *TspData {
return &TspData{datatype: "EDGE_WEIGHT_SECTION", values: &[][]float64{}}
}
func (t TspSpec) String() string {
return fmt.Sprintf("%s - %s", t.name, t.problemType)
}
// Read Reads TSP data from .tsp file
func Read(path string) (*TspSpec, error) {
file, err := os.Open(path)
if(err != nil) {
log.Fatal(err)
return nil, errors.New("unable to open file")
}
scanner := bufio.NewScanner(file)
spec := NewTspData()
dataLine := -1
arr := [][]float64{}
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line,":") {
strs := strings.Split(line, ":")
fmt.Println(strs)
switch strings.ReplaceAll(strs[0]," ","") {
case "NAME": {
spec.name = strings.ReplaceAll(strs[1]," ","")
}
case "TYPE": {
spec.problemType = strings.ReplaceAll(strs[1]," ","")
}
case "COMMENT": {
spec.comment = strings.ReplaceAll(strs[1]," ","")
}
case "DIMENSION": {
i, e := strconv.ParseInt(strings.ReplaceAll(strs[1]," ",""),10,64)
if(e == nil) {
spec.dimension = int(i)
}
}
case "EDGE_WEIGHT_TYPE": {
spec.edgeWeightType = strings.ReplaceAll(strs[1]," ","")
}
case "EDGE_WEIGHT_FORMAT": {
spec.edgeWeightFormat = strings.ReplaceAll(strs[1]," ","")
}
}
} else {
if dataLine == -1 {
spec.data.datatype = strings.ReplaceAll(line," ","")
dataLine++
} else {
entries := strings.Split(line, " ")
arr = append(arr, []float64{})
for _, e := range entries{
f, err := strconv.ParseFloat(e, 64)
if err == nil {
log.Println(f)
arr[dataLine] = append(arr[dataLine], f)
}
}
dataLine++
}
}
}
spec.data.values = &arr
return spec, nil
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment