-
Notifications
You must be signed in to change notification settings - Fork 0
/
queens.go
285 lines (242 loc) · 7.7 KB
/
queens.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package main
import (
"time"
"math/rand"
"encoding/json"
"fmt"
"net/http"
)
type Position struct {
X int `json:"x"`
Y int `json:"y"`
}
type Board struct {
Columns, Rows, QueenCount, MaxQueensOnSight int
InitialQueens, Queens []Position
Grid [][]bool
QueensOnSight [][]int
CandidatePositions []Position
Fitness int
AddedQueens []Position
}
type IncomingMessage struct {
Columns, Rows, Max_queens_on_sight float64
Initial_queens []map[string]float64
}
type OutgoingMessage struct {
Added_queens []Position `json:"added_queens"`
}
func (board *Board) InitializeGrid() {
board.Grid = make([][]bool, board.Rows)
for row := 0; row < board.Rows; row++ {
board.Grid[row] = make([]bool, board.Columns)
for col := 0; col < board.Columns; col++{
board.Grid[row][col] = false
}
}
}
func (board *Board) InitializeQueensOnSight() {
board.QueensOnSight = make([][]int, board.Rows)
for row := 0; row < board.Rows; row++ {
board.QueensOnSight[row] = make([]int, board.Columns)
for col := 0; col < board.Columns; col++{
board.QueensOnSight[row][col] = 0
}
}
}
func (board *Board) QueensToGrid(){
for _, queen := range board.Queens {
board.Grid[queen.X][queen.Y] = true
}
}
func (board *Board) HasQueen(x int, y int) bool {
return board.Grid[x][y]
}
func (board *Board) AddQueens(queens []Position){
board.Queens = make([]Position, len(queens))
copy(board.Queens, queens)
board.InitializeGrid()
board.InitializeQueensOnSight()
board.QueensToGrid()
board.UpdateQueensOnSight()
board.Fitness = board.GetFitness()
}
func (board *Board) Initalize(message IncomingMessage) {
//fmt.Println(message)
board.Rows = int(message.Rows)
board.Columns = int(message.Columns)
board.MaxQueensOnSight = int(message.Max_queens_on_sight)
for _, position := range message.Initial_queens {
var pos Position
pos.X = int(position["x"])
pos.Y = int(position["y"])
board.InitialQueens = append(board.InitialQueens, pos)
}
board.AddQueens(board.InitialQueens)
board.AddedQueens = make([]Position, 0)
}
func (board *Board) FindQueens (start_x int, start_y int, dir_x int, dir_y int) int {
// finds amount of queens in a certain direction
total := 0
row := start_x
col:= start_y
i := 0
for ;row >= 0 && row < board.Rows && col >= 0 && col < board.Columns ;{
if i != 0 && board.Grid[row][col] == true {
total++
}
row+=dir_x
col+=dir_y
i++
}
return total
}
func (board *Board) UpdateQueensOnSight (){
//for every position (queen or no queen) find out the queens on sight
// FIXME: can probably done in a go routine
board.CandidatePositions = make([]Position, 0)
for row := 0; row < board.Rows; row++ {
for col := 0; col < board.Columns; col++ {
queensOnSight := 0
//North
queensOnSight += board.FindQueens(row, col, -1, 0)
//NorthEast
queensOnSight += board.FindQueens(row, col, -1, 1)
//East
queensOnSight += board.FindQueens(row, col, 0, 1)
//SouthEast
queensOnSight += board.FindQueens(row, col, 1, 1)
//South
queensOnSight += board.FindQueens(row, col, 1, 0)
//SouthWest
queensOnSight += board.FindQueens(row, col, 1, -1)
//West
queensOnSight += board.FindQueens(row, col, 0, -1)
//NorthWest
queensOnSight += board.FindQueens(row, col, -1, -1)
board.QueensOnSight[row][col] = queensOnSight
if queensOnSight <= board.MaxQueensOnSight && board.Grid[row][col] == false{
candidatePosition := Position{X:row, Y:col}
board.CandidatePositions = append(board.CandidatePositions, candidatePosition)
// FIXME: sort these
}
}
}
}
func (board *Board) RemoveQueen(position Position) {
// Find a queen to remove
// FIXME: Better to remove the queen with most others on sight
rand.Seed(time.Now().Unix())
//toRemove := board.AddedQueens[rand.Intn(len(board.AddedQueens) - 0) + 0]
// remove from queens
// remove from added queens
}
func (board *Board) AddQueen(position Position) {
// Add queen to board, updates grid, queens on sight
board.Queens = append(board.Queens, position)
board.AddedQueens = append(board.AddedQueens, position)
board.QueensToGrid()
board.UpdateQueensOnSight()
board.Fitness = board.GetFitness()
}
func (board *Board) GetFitness() int {
queen_count := 0
for row := 0; row < board.Rows; row++ {
for col := 0; col < board.Columns; col++ {
if board.Grid[row][col] == true {
if board.QueensOnSight[row][col] > board.MaxQueensOnSight {
return 0
} else {
queen_count++
}
}
}
}
return queen_count
}
func (board *Board) Display() {
for row := 0; row < board.Rows; row++ {
for col := 0; col < board.Columns; col++ {
if board.Grid[row][col] == true {
fmt.Printf("Q ")
} else {
fmt.Printf(". ")
}
}
fmt.Println("")
}
}
func (board *Board) GetCandidatePosition() Position {
// Return a new random position where qwe can place a queen
rnd := rand.Intn(len(board.CandidatePositions))
//fmt.Printf("idx: %d", rnd)
//fmt.Printf("len: %d\n", len(board.CandidatePositions))
//fmt.Printf("pos: x, y")
return board.CandidatePositions[rnd]
}
func (board *Board) DisplayQueensOnSight() {
for row := 0; row < board.Rows; row++ {
for col := 0; col < board.Columns; col++ {
fmt.Printf("%d\t", board.QueensOnSight[row][col])
}
fmt.Println("")
}
}
func (board *Board) FindBest(best *[]Position, message IncomingMessage) {
// FIXME: add a timer
bestFitness := 0
fitnessRepeat := 0
lastFitness := 0
max := 1000
if board.Rows * board.Columns > 2000 {
max = 100
} else {
max = 1000
}
for i:=0; i < max; i++ {
if len(board.CandidatePositions) > 0 {
board.AddQueen(board.GetCandidatePosition())
if board.Fitness > bestFitness {
// board.Display()
//fmt.Println("")
*best = make([]Position, len(board.AddedQueens))
copy(*best, board.AddedQueens)
bestFitness = board.Fitness
}
} else if fitnessRepeat > 10 {
board.Initalize(message)
} else {
board.Initalize(message)
}
if board.Fitness == lastFitness{
fitnessRepeat++
}
lastFitness=board.Fitness
}
}
func MaxQueensHandler(writer http.ResponseWriter, request *http.Request) {
rand.Seed(time.Now().Unix())
// handle incoming json
var message IncomingMessage
json_blob := make([]byte, request.ContentLength)
request.Body.Read(json_blob)
json.Unmarshal(json_blob, &message)
// fmt.Println(string(json_blob))
// fmt.Println(message)
var board Board
board.Initalize(message)
bestPositions := make([]Position, 0)
board.FindBest(&bestPositions, message)
board.AddQueens(bestPositions)
// board.Display()
// board.DisplayQueensOnSight()
// Send back json
var out OutgoingMessage
out.Added_queens = bestPositions
bytes, _ := json.Marshal(out)
writer.Write(bytes)
}
func main () {
http.HandleFunc("/max_queens", MaxQueensHandler)
http.ListenAndServe(":8080", nil)
}