-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyze.go
175 lines (135 loc) · 4.75 KB
/
analyze.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
/*
Copyright © 2020 NAME HERE <EMAIL ADDRESS>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd
import (
"encoding/csv"
"fmt"
"os"
"strconv"
"sync"
"github.com/jhoffe/2048_go/pkg/montecarlo"
"github.com/spf13/cobra"
)
var SampleSize int
var Step bool
var ItersA int
var IterStepSize int
var DepthA int
var DepthStepSize int
var Result string
var Concurrent int
type WaitGroupPool struct {
size int
pool chan byte
waitGroup sync.WaitGroup
}
func NewWaitGroupPool(size int) *WaitGroupPool {
wg := &WaitGroupPool{
size: size,
}
if size > 0 {
wg.pool = make(chan byte, size)
}
return wg
}
func (wg *WaitGroupPool) BlockAdd() {
if wg.size > 0 {
wg.pool <- 1
}
wg.waitGroup.Add(1)
}
func (wg *WaitGroupPool) Done() {
if wg.size > 0 {
<-wg.pool
}
wg.waitGroup.Add(1)
}
func (wg *WaitGroupPool) Wait() {
wg.waitGroup.Wait()
}
// analyzeCmd represents the analyze command
var analyzeCmd = &cobra.Command{
Use: "analyze",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
file, err := os.Create(Result)
if err != nil {
panic(err)
}
writer := csv.NewWriter(file)
var mu sync.Mutex
defer func() {
writer.Flush()
file.Close()
}()
writer.Write([]string{"N", "R", "D", "Score", "HighestBrick", "Time"})
wg := NewWaitGroupPool(Concurrent)
for n := 0; n < SampleSize; n++ {
if Step {
for r := IterStepSize; r <= ItersA; r = r + IterStepSize {
for d := DepthStepSize; d <= DepthA; d = d + DepthStepSize {
wg.BlockAdd()
go func(wg *WaitGroupPool, r, d, n int) {
fmt.Printf("Beginning R=%d and D=%d and N=%d \n", r, d, n)
g, duration := montecarlo.Run(r, d, montecarlo.ScoreRewardFunction)
fmt.Printf("Finished in %dms with a score of: %d and highest brick: %d. For R=%d and D=%d and N=%d \n",
duration, g.Score, g.GetHighestBrickValue(), r, d, n)
mu.Lock()
writer.Write([]string{strconv.Itoa(n), strconv.Itoa(r), strconv.Itoa(d), strconv.Itoa(int(g.Score)),
strconv.Itoa(g.GetHighestBrickValue()), strconv.Itoa(int(duration))})
mu.Unlock()
wg.Done()
}(wg, r, d, n)
}
}
} else {
wg.BlockAdd()
go func(wg *WaitGroupPool, r, d, n int) {
fmt.Printf("Beginning R=%d and D=%d and N=%d \n", ItersA, DepthA, SampleSize)
g, duration := montecarlo.Run(ItersA, DepthA, montecarlo.ScoreRewardFunction)
fmt.Printf("Finished in %dms with a score of: %d and highest brick: %d. For R=%d and D=%d and N=%d \n",
duration, g.Score, g.GetHighestBrickValue(), ItersA, DepthA, SampleSize)
mu.Lock()
writer.Write([]string{strconv.Itoa(n), strconv.Itoa(ItersA), strconv.Itoa(DepthA), strconv.Itoa(int(g.Score)),
strconv.Itoa(g.GetHighestBrickValue()), strconv.Itoa(int(duration))})
mu.Unlock()
wg.Done()
}(wg, ItersA, DepthA, n)
}
}
wg.Wait()
},
}
func init() {
rootCmd.AddCommand(analyzeCmd)
analyzeCmd.Flags().IntVarP(&SampleSize, "size", "n", 30, "")
analyzeCmd.Flags().BoolVarP(&Step, "step", "s", false, "")
analyzeCmd.Flags().IntVarP(&ItersA, "iters", "i", 50, "The amount of iterations to take over each random tree")
analyzeCmd.Flags().IntVarP(&IterStepSize, "itersteps", "I", 1, "The step size between each run")
analyzeCmd.Flags().IntVarP(&DepthA, "depth", "d", 200, "The depth of the tree to crawl. If 0, until the game is done")
analyzeCmd.Flags().IntVarP(&DepthStepSize, "depthsteps", "D", 1, "The step size between each run")
analyzeCmd.Flags().StringVarP(&Result, "result", "r", "results.csv", "")
analyzeCmd.Flags().IntVarP(&Concurrent, "concurrent", "c", 20, "")
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// analyzeCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// analyzeCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}