@@ -25,7 +25,7 @@ type searchLimits struct {
2525 moveTime int // in milliseconds
2626 infinite bool
2727 startTime time.Time
28- nextTime time.Time
28+ lastTime time.Time
2929
3030 //////////////// current //////////
3131 stop bool
@@ -89,62 +89,72 @@ func (pv *pvList) String() string {
8989func engine () (toEngine chan bool , frEngine chan string ) {
9090 frEngine = make (chan string )
9191 toEngine = make (chan bool )
92- go rootx (toEngine , frEngine )
92+ go root (toEngine , frEngine )
9393
9494 return
9595}
9696
9797//TODO root: Aspiration search
9898func root (toEngine chan bool , frEngine chan string ) {
99- var depth int
99+ var depth , alpha , beta int
100100 var pv pvList
101101 var childPV pvList
102+ var ml moveList
102103 childPV .new ()
103104 pv .new ()
105+ ml .new (60 )
104106 b := & board
105- ml := make (moveList , 0 , 60 )
106107 for _ = range toEngine {
107- limits .startTime , limits .nextTime = time .Now (), time .Now ()
108- alpha , beta := minEval , maxEval
109-
108+ limits .startTime , limits .lastTime = time .Now (), time .Now ()
110109 cntNodes = 0
110+
111111 killers .clear ()
112112 ml .clear ()
113113 pv .clear ()
114- genAndSort (b , & ml )
114+
115+ genAndSort (0 , b , & ml )
115116 bm := ml [0 ]
116117 bs := noScore
117118 depth = limits .depth
118- for depth = 1 ; depth <= limits .depth ; depth ++ {
119- bs = noScore
120- for ix := range ml {
121- mv := & ml [ix ]
119+ for depth = 1 ; depth <= limits .depth && ! limits .stop ; depth ++ {
120+ ml .sort ()
121+ bs = noScore // bm keeps the best from prev iteration in case of immediate stop before first is done in this iterastion
122+ alpha , beta = minEval , maxEval
123+ for ix , mv := range ml {
122124 childPV .clear ()
123125
124- b .move (* mv )
126+ b .move (mv )
125127 tell ("info depth " , strconv .Itoa (depth ), " currmove " , mv .String (), " currmovenumber " , strconv .Itoa (ix + 1 ))
128+
126129 score := - search (- beta , - alpha , depth - 1 , 1 , & childPV , b )
127- b .unmove (* mv )
130+ b .unmove (mv )
128131 if limits .stop {
129132 break
130133 }
131- mv .packEval (score )
134+ ml [ ix ] .packEval (score )
132135 if score > bs {
133136 bs = score
134- pv .catenate (* mv , & childPV )
137+ pv .catenate (mv , & childPV )
135138
136- bm = * mv
139+ bm = ml [ ix ]
137140 alpha = score
138141
139142 t1 := time .Since (limits .startTime )
140143 tell (fmt .Sprintf ("info score cp %v depth %v nodes %v time %v pv " , bm .eval (), depth , cntNodes , int (t1 .Seconds ()* 1000 )), pv .String ())
141144 }
145+
142146 }
143147 ml .sort ()
144148 }
145-
149+ //
150+ // time, nps, ebf
146151 t1 := time .Since (limits .startTime )
147- tell (fmt .Sprintf ("info score cp %v depth %v nodes %v time %v pv " , bm .eval (), depth - 1 , cntNodes , int (t1 .Seconds ()* 1000 )), pv .String ())
152+ nps := float64 (0 )
153+ if t1 .Seconds () != 0 {
154+ nps = float64 (cntNodes ) / t1 .Seconds ()
155+ }
156+
157+ tell (fmt .Sprintf ("info score cp %v depth %v nodes %v time %v nps %v pv %v" , bm .eval (), depth - 1 , cntNodes , int (t1 .Seconds ()* 1000 ), uint (nps ), pv .String ()))
148158 frEngine <- fmt .Sprintf ("bestmove %v%v" , sq2Fen [bm .fr ()], sq2Fen [bm .to ()])
149159 }
150160}
@@ -163,15 +173,22 @@ func root(toEngine chan bool, frEngine chan string) {
163173func search (alpha , beta , depth , ply int , pv * pvList , b * boardStruct ) int {
164174 cntNodes ++
165175 if depth <= 0 {
166- return signEval (b .stm , evaluate (b ))
176+ //return signEval(b.stm, evaluate(b))
177+ return qs (beta , b )
167178 }
168179 pv .clear ()
169- ml := make (moveList , 0 , 60 )
180+
181+ // trans retrieve here
182+
183+ var ml moveList
184+ ml .new (60 )
185+
170186 //genAndSort(b, &ml)
171187 genInOrder (b , & ml , ply )
172188
173- bm , bs := noMove , noScore
174- childPV := make (pvList , 0 , maxPly )
189+ bs := noScore
190+ var childPV pvList
191+ childPV .new () // TODO? make it smaller for each depth maxDepth-ply
175192 for _ , mv := range ml {
176193 if ! b .move (mv ) {
177194 continue
@@ -186,6 +203,10 @@ func search(alpha, beta, depth, ply int, pv *pvList, b *boardStruct) int {
186203 if score > bs {
187204 bs = score
188205 pv .catenate (mv , & childPV )
206+ if score > alpha {
207+ alpha = score
208+ // trans.store here
209+ }
189210
190211 if score >= beta { // beta cutoff
191212 // add killer and update history
@@ -194,17 +215,11 @@ func search(alpha, beta, depth, ply int, pv *pvList, b *boardStruct) int {
194215 }
195216 return score
196217 }
197- if score > alpha {
198- bm = mv
199- _ = bm
200- alpha = score
201- }
202-
203218 }
204- if time .Since (limits .nextTime ) >= time .Duration (time .Second ) {
219+ if time .Since (limits .lastTime ) >= time .Duration (time .Second ) {
205220 t1 := time .Since (limits .startTime )
206221 tell (fmt .Sprintf ("info time %v nodes %v nps %v" , int (t1 .Seconds ()* 1000 ), cntNodes , cntNodes / uint64 (t1 .Seconds ())))
207- limits .nextTime = time .Now ()
222+ limits .lastTime = time .Now ()
208223 }
209224
210225 if limits .stop {
@@ -243,14 +258,15 @@ func qs(beta int, b *boardStruct) int {
243258
244259 see := see (fr , to , b )
245260
246- if see < 0 {
247- continue // equal captures not interesting
248- }
249261 if see == 0 && mv .cp () == empty {
250262 // must be a promotion that didn't captureand was not captured
251263 see = pieceVal [wQ ] - pieceVal [wP ]
252264 }
253265
266+ if see <= 0 {
267+ continue // equal captures not interesting
268+ }
269+
254270 sc := ev + see
255271 if sc > bs {
256272 bs = sc
@@ -356,7 +372,7 @@ func see(fr, to int, b *boardStruct) int {
356372 return captureList [0 ]
357373}
358374
359- func genAndSort (b * boardStruct , ml * moveList ) {
375+ /* func genAndSort(b *boardStruct, ml *moveList) {
360376 ml.clear()
361377 b.genAllLegals(ml)
362378 for ix, mv := range *ml {
@@ -368,6 +384,31 @@ func genAndSort(b *boardStruct, ml *moveList) {
368384 (*ml)[ix].packEval(v)
369385 }
370386
387+ ml.sort()
388+ } */
389+ func genAndSort (ply int , b * boardStruct , ml * moveList ) {
390+ if ply > maxPly {
391+ panic ("wtf maxply" )
392+ }
393+
394+ ml .clear ()
395+ b .genAllLegals (ml )
396+
397+ for ix , mv := range * ml {
398+ b .move (mv )
399+ v := evaluate (b )
400+ b .unmove (mv )
401+ if killers [ply ].k1 .cmp (mv ) {
402+ v += 1000
403+ } else if killers [ply ].k2 .cmp (mv ) {
404+ v += 900
405+ }
406+
407+ v = signEval (b .stm , v )
408+
409+ (* ml )[ix ].packEval (v )
410+ }
411+
371412 ml .sort ()
372413}
373414
0 commit comments