-
Notifications
You must be signed in to change notification settings - Fork 0
/
io.go
181 lines (163 loc) · 3.98 KB
/
io.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package main
import (
"encoding/json"
"fmt"
"io"
"os"
)
type Config struct {
Log_file string `json:"log file"`
Random_seed int64 `json:"random seed"`
Metrics struct {
Initial_state bool `json:"initial state"`
Num_misplaced_tiles bool `json:"num misplaced tiles"`
Max_solution_length bool `json:"max solution length"`
Status bool `json:"status"`
Execution_time bool `json:"execution time"`
Solution_length bool `json:"solution length"`
Nodes_explored bool `json:"nodes explored"`
Frontier_size bool `json:"frontier size"`
Nodes_evaluated bool `json:"nodes evaluated"`
Solution_path bool `json:"solution path"`
} `json:"metrics"`
Default_inputs struct {
Heuristics []int `json:"heuristics"`
Time_limit int `json:"time limit"`
} `json:"default inputs"`
Inputs []struct {
Size int `json:"size"`
Swaps int `json:"swaps"`
Misplaced int `json:"misplaced"`
Heuristics []int `json:"heuristics"`
Time_limit int `json:"time limit"`
Use_prev_move bool `json:"use prev move"`
} `json:"inputs"`
}
func ConfigExists() bool {
if _, err := os.Stat(config_file); err == nil {
return true
} else {
return false
}
}
func readConfig() Config {
// Open our jsonFile
jsonFile, err := os.Open(config_file)
if err != nil {
panic(err)
}
defer jsonFile.Close()
// read our opened jsonFile as a byte array.
byteValue, err2 := io.ReadAll(jsonFile)
if err2 != nil {
fmt.Println(err)
os.Exit(1)
}
var config Config
if err3 := json.Unmarshal(byteValue, &config); err3 != nil {
fmt.Printf("Invalid config file. Delete %v and run program to generate a new one\n", config_file)
os.Exit(1)
}
for _, input := range config.Inputs {
if (input.Misplaced != 0) && (input.Swaps != 0) {
panic("cannot specify both swaps and misplaced in config.inputs")
}
}
return config
}
func createConfig() {
var contents = []string{
"{",
"\t\"log file\": \"log\",",
"\t\"random seed\": 0,",
"\t\"metrics\": {",
"\t\t\"initial state\": true,",
"\t\t\"num misplaced tiles\": true,",
"\t\t\"max solution length\": true,",
"\t\t\"status\": true,",
"\t\t\"execution time\": true,",
"\t\t\"solution length\": true,",
"\t\t\"nodes explored\": true,",
"\t\t\"frontier size\": true,",
"\t\t\"nodes evaluated\": true,",
"\t\t\"solution path\": false",
"\t},",
"\t\"default inputs\": {",
"\t\t\"heuristics\": [2],",
"\t\t\"time limit\": 60",
"\t},",
"\t\"inputs\": [",
"\t\t{",
"\t\t\t\"size\": 2,",
"\t\t\t\"misplaced\": 3",
"\t\t},",
"\t\t{",
"\t\t\t\"size\": 9,",
"\t\t\t\"swaps\": 40",
"\t\t},",
"\t\t{",
"\t\t\t\"size\": 3,",
"\t\t\t\"misplaced\": 9,",
"\t\t\t\"heuristics\": [1, 2, 3, 4]",
"\t\t},",
"\t\t{",
"\t\t\t\"size\": 3,",
"\t\t\t\"swaps\": 20",
"\t\t},",
"\t\t{",
"\t\t\t\"size\": 4,",
"\t\t\t\"swaps\": 20,",
"\t\t\t\"heuristics\": [1, 2, 3, 4]",
"\t\t},",
"\t\t{",
"\t\t\t\"size\": 5,",
"\t\t\t\"misplaced\": 10,",
"\t\t\t\"time limit\": 100",
"\t\t},",
"\t\t{",
"\t\t\t\"size\": 4,",
"\t\t\t\"misplaced\": 16,",
"\t\t\t\"time limit\": 0",
"\t\t},",
"\t\t{",
"\t\t\t\"size\": 4,",
"\t\t\t\"misplaced\": 25,",
"\t\t\t\"time limit\": 0",
"\t\t},",
"\t\t{",
"\t\t\t\"size\": 4,",
"\t\t\t\"misplaced\": 25,",
"\t\t\t\"time limit\": 0,",
"\t\t\t\"use prev move\": true",
"\t\t},",
"\t\t{",
"\t\t\t\"size\": 5,",
"\t\t\t\"misplaced\": 15,",
"\t\t\t\"time limit\": 60",
"\t\t}",
"\t]",
"}",
}
f, err := os.Create(config_file)
if err != nil {
panic(err)
}
defer f.Close()
for _, line := range contents {
_, err := f.WriteString(line + "\n")
if err != nil {
panic(err)
}
}
fmt.Printf("Config created at %v\n", config_file)
}
func openLogFile(filename string) *os.File {
f, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
if err != nil {
panic(err)
}
return f
}
func logFileSpacer() string {
return "\n+----------------------------------+\n\n"
}